Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
ObservableSet |
|
| 1.6666666666666667;1.667 |
1 | /* | |
2 | * Copyright 2003-2004 The Apache Software Foundation | |
3 | * | |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); | |
5 | * you may not use this file except in compliance with the License. | |
6 | * You may obtain a copy of the License at | |
7 | * | |
8 | * http://www.apache.org/licenses/LICENSE-2.0 | |
9 | * | |
10 | * Unless required by applicable law or agreed to in writing, software | |
11 | * distributed under the License is distributed on an "AS IS" BASIS, | |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 | * See the License for the specific language governing permissions and | |
14 | * limitations under the License. | |
15 | */ | |
16 | package org.apache.commons.events.observable; | |
17 | ||
18 | import java.util.Set; | |
19 | ||
20 | /** | |
21 | * Decorates a <code>Set</code> implementation to observe modifications. | |
22 | * <p> | |
23 | * Each modifying method call made on this <code>Set</code> is forwarded to a | |
24 | * {@link ModificationHandler}. | |
25 | * The handler manages the event, notifying listeners and optionally vetoing changes. | |
26 | * The default handler is | |
27 | * {@link org.apache.commons.events.observable.standard.StandardModificationHandler}. | |
28 | * See this class for details of configuration available. | |
29 | * | |
30 | * @since Commons Events 1.0 | |
31 | * @version $Revision: 155443 $ $Date: 2005-02-26 13:19:51 +0000 (Sat, 26 Feb 2005) $ | |
32 | * | |
33 | * @author Stephen Colebourne | |
34 | */ | |
35 | public class ObservableSet extends ObservableCollection implements Set { | |
36 | ||
37 | // Factories | |
38 | //----------------------------------------------------------------------- | |
39 | /** | |
40 | * Factory method to create an observable set. | |
41 | * <p> | |
42 | * A {@link org.apache.commons.events.observable.standard.StandardModificationHandler} will be created. | |
43 | * This can be accessed by {@link #getHandler()} to add listeners. | |
44 | * | |
45 | * @param set the set to decorate, must not be null | |
46 | * @return the observed Set | |
47 | * @throws IllegalArgumentException if the collection is null | |
48 | */ | |
49 | public static ObservableSet decorate(final Set set) { | |
50 | 0 | return new ObservableSet(set, null); |
51 | } | |
52 | ||
53 | /** | |
54 | * Factory method to create an observable set using a listener or a handler. | |
55 | * <p> | |
56 | * A lot of functionality is available through this method. | |
57 | * If you don't need the extra functionality, simply implement the | |
58 | * {@link org.apache.commons.events.observable.standard.StandardModificationListener} | |
59 | * interface and pass it in as the second parameter. | |
60 | * <p> | |
61 | * Internally, an <code>ObservableSet</code> relies on a {@link ModificationHandler}. | |
62 | * The handler receives all the events and processes them, typically by | |
63 | * calling listeners. Different handler implementations can be plugged in | |
64 | * to provide a flexible event system. | |
65 | * <p> | |
66 | * The handler implementation is determined by the listener parameter via | |
67 | * the registered factories. The listener may be a manually configured | |
68 | * <code>ModificationHandler</code> instance. | |
69 | * <p> | |
70 | * The listener is defined as an Object for maximum flexibility. | |
71 | * It does not have to be a listener in the classic JavaBean sense. | |
72 | * It is entirely up to the factory and handler as to how the parameter | |
73 | * is interpretted. An IllegalArgumentException is thrown if no suitable | |
74 | * handler can be found for this listener. | |
75 | * <p> | |
76 | * A <code>null</code> listener will create a | |
77 | * {@link org.apache.commons.events.observable.standard.StandardModificationHandler}. | |
78 | * | |
79 | * @param set the set to decorate, must not be null | |
80 | * @param listener set listener, may be null | |
81 | * @return the observed set | |
82 | * @throws IllegalArgumentException if the set is null | |
83 | * @throws IllegalArgumentException if there is no valid handler for the listener | |
84 | */ | |
85 | public static ObservableSet decorate( | |
86 | final Set set, | |
87 | final Object listener) { | |
88 | ||
89 | 0 | if (set == null) { |
90 | 0 | throw new IllegalArgumentException("Set must not be null"); |
91 | } | |
92 | 0 | return new ObservableSet(set, listener); |
93 | } | |
94 | ||
95 | // Constructors | |
96 | //----------------------------------------------------------------------- | |
97 | /** | |
98 | * Constructor that wraps (not copies) and takes a handler. | |
99 | * <p> | |
100 | * The handler implementation is determined by the listener parameter via | |
101 | * the registered factories. The listener may be a manually configured | |
102 | * <code>ModificationHandler</code> instance. | |
103 | * | |
104 | * @param set the set to decorate, must not be null | |
105 | * @param listener the listener, may be null | |
106 | * @throws IllegalArgumentException if the set is null | |
107 | */ | |
108 | protected ObservableSet( | |
109 | final Set set, | |
110 | final Object listener) { | |
111 | 0 | super(set, listener); |
112 | 0 | } |
113 | ||
114 | } |