Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
ObservableSortedSet |
|
| 1.2;1.2 |
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.Comparator; | |
19 | import java.util.SortedSet; | |
20 | ||
21 | /** | |
22 | * Decorates a <code>SortedSet</code> implementation to observe modifications. | |
23 | * <p> | |
24 | * Each modifying method call made on this <code>SortedSet</code> is forwarded to a | |
25 | * {@link ModificationHandler}. | |
26 | * The handler manages the event, notifying listeners and optionally vetoing changes. | |
27 | * The default handler is | |
28 | * {@link org.apache.commons.events.observable.standard.StandardModificationHandler StandardModificationHandler}. | |
29 | * See this class for details of configuration available. | |
30 | * | |
31 | * @since Commons Events 1.0 | |
32 | * @version $Revision: 155443 $ $Date: 2005-02-26 13:19:51 +0000 (Sat, 26 Feb 2005) $ | |
33 | * | |
34 | * @author Stephen Colebourne | |
35 | */ | |
36 | public class ObservableSortedSet extends ObservableSet implements SortedSet { | |
37 | ||
38 | // Factories | |
39 | //----------------------------------------------------------------------- | |
40 | /** | |
41 | * Factory method to create an observable set. | |
42 | * <p> | |
43 | * A {@link org.apache.commons.events.observable.standard.StandardModificationHandler} will be created. | |
44 | * This can be accessed by {@link #getHandler()} to add listeners. | |
45 | * | |
46 | * @param set the set to decorate, must not be null | |
47 | * @return the observed Set | |
48 | * @throws IllegalArgumentException if the collection is null | |
49 | */ | |
50 | public static ObservableSortedSet decorate(final SortedSet set) { | |
51 | 0 | return new ObservableSortedSet(set, null); |
52 | } | |
53 | ||
54 | /** | |
55 | * Factory method to create an observable set using a listener or a handler. | |
56 | * <p> | |
57 | * A lot of functionality is available through this method. | |
58 | * If you don't need the extra functionality, simply implement the | |
59 | * {@link org.apache.commons.events.observable.standard.StandardModificationListener} | |
60 | * interface and pass it in as the second parameter. | |
61 | * <p> | |
62 | * Internally, an <code>ObservableSortedSet</code> relies on a {@link ModificationHandler}. | |
63 | * The handler receives all the events and processes them, typically by | |
64 | * calling listeners. Different handler implementations can be plugged in | |
65 | * to provide a flexible event system. | |
66 | * <p> | |
67 | * The handler implementation is determined by the listener parameter via | |
68 | * the registered factories. The listener may be a manually configured | |
69 | * <code>ModificationHandler</code> instance. | |
70 | * <p> | |
71 | * The listener is defined as an Object for maximum flexibility. | |
72 | * It does not have to be a listener in the classic JavaBean sense. | |
73 | * It is entirely up to the factory and handler as to how the parameter | |
74 | * is interpretted. An IllegalArgumentException is thrown if no suitable | |
75 | * handler can be found for this listener. | |
76 | * <p> | |
77 | * A <code>null</code> listener will create a | |
78 | * {@link org.apache.commons.events.observable.standard.StandardModificationHandler}. | |
79 | * | |
80 | * @param set the set to decorate, must not be null | |
81 | * @param listener set listener, may be null | |
82 | * @return the observed set | |
83 | * @throws IllegalArgumentException if the set is null | |
84 | * @throws IllegalArgumentException if there is no valid handler for the listener | |
85 | */ | |
86 | public static ObservableSortedSet decorate( | |
87 | final SortedSet set, | |
88 | final Object listener) { | |
89 | ||
90 | 0 | if (set == null) { |
91 | 0 | throw new IllegalArgumentException("SortedSet must not be null"); |
92 | } | |
93 | 0 | return new ObservableSortedSet(set, listener); |
94 | } | |
95 | ||
96 | // Constructors | |
97 | //----------------------------------------------------------------------- | |
98 | /** | |
99 | * Constructor that wraps (not copies) and takes a handler. | |
100 | * <p> | |
101 | * The handler implementation is determined by the listener parameter via | |
102 | * the registered factories. The listener may be a manually configured | |
103 | * <code>ModificationHandler</code> instance. | |
104 | * | |
105 | * @param set the set to decorate, must not be null | |
106 | * @param listener the listener, may be null | |
107 | * @throws IllegalArgumentException if the set is null | |
108 | */ | |
109 | protected ObservableSortedSet( | |
110 | final SortedSet set, | |
111 | final Object listener) { | |
112 | 0 | super(set, listener); |
113 | 0 | } |
114 | ||
115 | /** | |
116 | * Typecast the collection to a SortedSet. | |
117 | * | |
118 | * @return the wrapped collection as a SortedSet | |
119 | */ | |
120 | private SortedSet getSortedSet() { | |
121 | 0 | return (SortedSet) getCollection(); |
122 | } | |
123 | ||
124 | // SortedSet API | |
125 | //----------------------------------------------------------------------- | |
126 | public Comparator comparator() { | |
127 | 0 | return getSortedSet().comparator(); |
128 | } | |
129 | ||
130 | public Object first() { | |
131 | 0 | return getSortedSet().first(); |
132 | } | |
133 | ||
134 | public Object last() { | |
135 | 0 | return getSortedSet().last(); |
136 | } | |
137 | ||
138 | //----------------------------------------------------------------------- | |
139 | public SortedSet subSet(Object fromElement, Object toElement) { | |
140 | 0 | SortedSet subSet = getSortedSet().subSet(fromElement, toElement); |
141 | 0 | return new ObservableSortedSet(subSet, getHandler().createSubSetHandler(fromElement, toElement)); |
142 | } | |
143 | ||
144 | public SortedSet headSet(Object toElement) { | |
145 | 0 | SortedSet headSet = getSortedSet().headSet(toElement); |
146 | 0 | return new ObservableSortedSet(headSet, getHandler().createHeadSetHandler(toElement)); |
147 | } | |
148 | ||
149 | public SortedSet tailSet(Object fromElement) { | |
150 | 0 | SortedSet tailSet = getSortedSet().tailSet(fromElement); |
151 | 0 | return new ObservableSortedSet(tailSet, getHandler().createTailSetHandler(fromElement)); |
152 | } | |
153 | ||
154 | } |