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