samples/deadlock/test/org/apidesign/javamonitorflaws/MultiplyCache.java
author Jaroslav Tulach <jtulach@netbeans.org>
Tue, 10 Feb 2009 18:36:21 +0100
changeset 315 08dd52950883
child 316 41a4abecb600
permissions -rw-r--r--
Example for the "java monitor pitfalls" chapter
     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 public class MultiplyCache extends Cache<String,Integer> {
    12     private PropertyChangeSupport pcs;
    13     private int multiply;
    14     public static final String PROP_MULTIPLY = "multiply";
    15 
    16     public synchronized int getMultiply() {
    17         return multiply;
    18     }
    19     public synchronized void setMultiply(int multiply) {
    20         int oldMultiply = this.multiply;
    21         this.multiply = multiply;
    22         pcs.firePropertyChange(PROP_MULTIPLY, oldMultiply, multiply);
    23     }
    24 
    25     public synchronized void addPropertyChangeListener(PropertyChangeListener listener) {
    26         if (pcs == null) {
    27             pcs = new PropertyChangeSupport(this);
    28         }
    29         pcs.addPropertyChangeListener(listener);
    30     }
    31     public void removePropertyChangeListener(PropertyChangeListener listener) {
    32         if (pcs != null) {
    33             pcs.removePropertyChangeListener(listener);
    34         }
    35     }
    36 
    37     @Override
    38     protected Integer createItem(String f) {
    39         return f.length() * getMultiply();
    40     }
    41 
    42 
    43 }