samples/deadlock/test/org/apidesign/javamonitorflaws/MultiplyCache.java
author Jaroslav Tulach <jtulach@netbeans.org>
Wed, 11 Feb 2009 08:52:00 +0100
changeset 317 e101649dbd17
parent 316 41a4abecb600
child 318 38ebce3000fd
permissions -rw-r--r--
Marking code snippets for the "Java Monitor" wiki page
     1 package org.apidesign.javamonitorflaws;
     2 
     3 import org.apidesign.javamonitorflaws.Cache;
     4 import java.beans.PropertyChangeListener;
     5 import java.beans.PropertyChangeSupport;
     6 
     7 /**
     8  *
     9  * @author Jaroslav Tulach <jtulach@netbeans.org>
    10  */
    11 // BEGIN: monitor.pitfalls.subclass
    12 public class MultiplyCache extends Cache<String,Integer> {
    13     private PropertyChangeSupport pcs;
    14     private int multiply;
    15     public static final String PROP_MULTIPLY = "multiply";
    16 
    17     public synchronized int getMultiply() {
    18         return multiply;
    19     }
    20     public synchronized void setMultiply(int multiply) {
    21         int oldMultiply = this.multiply;
    22         this.multiply = multiply;
    23         pcs.firePropertyChange(PROP_MULTIPLY, oldMultiply, multiply);
    24     }
    25 
    26     public synchronized void addPropertyChangeListener(PropertyChangeListener listener) {
    27         if (pcs == null) {
    28             pcs = new PropertyChangeSupport(this);
    29         }
    30         pcs.addPropertyChangeListener(listener);
    31     }
    32     public synchronized void removePropertyChangeListener(PropertyChangeListener listener) {
    33         if (pcs != null) {
    34             pcs.removePropertyChangeListener(listener);
    35         }
    36     }
    37 
    38     @Override
    39     protected Integer createItem(String f) {
    40         return f.length() * getMultiply();
    41     }
    42 }
    43 // END: monitor.pitfalls.subclass
    44 
    45