samples/deadlock/test/org/apidesign/javamonitorflaws/MultiplyCache.java
author Jaroslav Tulach <jtulach@netbeans.org>
Wed, 11 Feb 2009 10:16:43 +0100
changeset 318 38ebce3000fd
parent 317 e101649dbd17
child 319 6c1d8b5553d8
permissions -rw-r--r--
Shortening lines to less than 78 characters
     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(
    27         PropertyChangeListener listener
    28     ) {
    29         if (pcs == null) {
    30             pcs = new PropertyChangeSupport(this);
    31         }
    32         pcs.addPropertyChangeListener(listener);
    33     }
    34     public synchronized void removePropertyChangeListener(
    35         PropertyChangeListener listener
    36     ) {
    37         if (pcs != null) {
    38             pcs.removePropertyChangeListener(listener);
    39         }
    40     }
    41 
    42     @Override
    43     protected Integer createItem(String f) {
    44         return f.length() * getMultiply();
    45     }
    46 }
    47 // END: monitor.pitfalls.subclass
    48 
    49