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
jtulach@315
     1
package org.apidesign.javamonitorflaws;
jtulach@315
     2
jtulach@315
     3
import org.apidesign.javamonitorflaws.Cache;
jtulach@315
     4
import java.beans.PropertyChangeListener;
jtulach@315
     5
import java.beans.PropertyChangeSupport;
jtulach@315
     6
jtulach@315
     7
/**
jtulach@315
     8
 *
jtulach@315
     9
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jtulach@315
    10
 */
jtulach@315
    11
public class MultiplyCache extends Cache<String,Integer> {
jtulach@315
    12
    private PropertyChangeSupport pcs;
jtulach@315
    13
    private int multiply;
jtulach@315
    14
    public static final String PROP_MULTIPLY = "multiply";
jtulach@315
    15
jtulach@315
    16
    public synchronized int getMultiply() {
jtulach@315
    17
        return multiply;
jtulach@315
    18
    }
jtulach@315
    19
    public synchronized void setMultiply(int multiply) {
jtulach@315
    20
        int oldMultiply = this.multiply;
jtulach@315
    21
        this.multiply = multiply;
jtulach@315
    22
        pcs.firePropertyChange(PROP_MULTIPLY, oldMultiply, multiply);
jtulach@315
    23
    }
jtulach@315
    24
jtulach@315
    25
    public synchronized void addPropertyChangeListener(PropertyChangeListener listener) {
jtulach@315
    26
        if (pcs == null) {
jtulach@315
    27
            pcs = new PropertyChangeSupport(this);
jtulach@315
    28
        }
jtulach@315
    29
        pcs.addPropertyChangeListener(listener);
jtulach@315
    30
    }
jtulach@315
    31
    public void removePropertyChangeListener(PropertyChangeListener listener) {
jtulach@315
    32
        if (pcs != null) {
jtulach@315
    33
            pcs.removePropertyChangeListener(listener);
jtulach@315
    34
        }
jtulach@315
    35
    }
jtulach@315
    36
jtulach@315
    37
    @Override
jtulach@315
    38
    protected Integer createItem(String f) {
jtulach@315
    39
        return f.length() * getMultiply();
jtulach@315
    40
    }
jtulach@315
    41
jtulach@315
    42
jtulach@315
    43
}