File: PropertyEventNameMatcher.java

package info (click to toggle)
libglazedlists-java 1.9.1-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 3,012 kB
  • sloc: java: 22,561; xml: 940; makefile: 5
file content (87 lines) | stat: -rw-r--r-- 3,810 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/* Glazed Lists                                                 (c) 2003-2007 */
/* http://publicobject.com/glazedlists/                      publicobject.com,*/
/*                                                     O'Dell Engineering Ltd.*/
package ca.odell.glazedlists.impl.matchers;

import ca.odell.glazedlists.matchers.Matcher;

import java.beans.PropertyChangeEvent;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

/**
 * <code>PropertyEventNameMatcher</code> matches {@link java.beans.PropertyChangeEvent}s by property name.
 * One or more property names to match or filter against may be given. The concrete behaviour of
 * a PropertyEventNameMatcher depends on the {@link #matchPropertyNames} property. If you want to
 * match property change events against a known set of property names, use a value of <code>true</code>
 * for the #matchPropertyNames} property. Alternatively, when you specify <code>false</code>,
 * the specified property names will serve as an exclude list, e.g. if an event matches a specified
 * property name, it will be filtered out.
 * 
 * @see #isMatchPropertyNames()
 * 
 * @author Holger Brands
 */
public final class PropertyEventNameMatcher implements Matcher<PropertyChangeEvent> {

    /** Property names to consider. */
    private final Set<String> propertyNames = new HashSet<String>();

    /**
     * Specifies how to use the {@link #propertyNames} to match property change events.
     * 
     * @see #isMatchPropertyNames()
     */
    private boolean matchPropertyNames;
   
    /**
     * Creates a PropertyEventNameMatcher.
     * 
     * @param matchPropertyNames if <code>true</code> the property names are used to match events
     *        by name, if <code>false</code> they are used to filter events
     * @param properties the property names to consider
     * 
     * @see #isMatchPropertyNames()
     */
    public PropertyEventNameMatcher(boolean matchPropertyNames, String... properties) {
        if (properties == null) throw new IllegalArgumentException("Array of property names may not be null");
        this.matchPropertyNames = matchPropertyNames;
        for (int i = 0, n = properties.length; i < n; i++) {
            propertyNames.add(properties[i]);
        }
    }
    
    /**
     * Creates a PropertyEventNameMatcher.
     * 
     * @param matchPropertyNames if <code>true</code> the property names are used to match
     *        events by name, if <code>false</code> they are used to filter events
     * @param properties the property names to consider
     * 
     * @see #isMatchPropertyNames()
     */    
    public PropertyEventNameMatcher(boolean matchPropertyNames, Collection<String> properties) {
        if (properties == null) throw new IllegalArgumentException("Collection of property names may not be null");
        this.matchPropertyNames = matchPropertyNames;
        propertyNames.addAll(properties);
    }
    
    /** {@inheritDoc} */
    public boolean matches(PropertyChangeEvent event) {
        final boolean containsProperty = propertyNames.contains(event.getPropertyName());
        return matchPropertyNames ? containsProperty : !containsProperty;
    }

    /**
     * Determines how to use the {@link #propertyNames} to match the property change events. If
     * <code>true</code>, the specified property names serve as a positive list, e.g. if the
     * property name of an event is contained in the {@link #propertyNames}, the event is matched.
     * If <code>false</code>, the specified property names serve as a negative list, e.g. if the
     * property name of an event is contained in the {@link #propertyNames}, the event is
     * <strong>not</strong> matched.
     */
    public boolean isMatchPropertyNames() {
        return matchPropertyNames;
    }
}