001: package org.apache.commons.events.observable;
002:
003: import java.util.Collection;
004: import java.util.Map;
005:
006: public class DefaultCollectionChangeEventFactory implements
007: CollectionChangeEventFactory {
008:
009: private Object source = null;
010:
011: /**
012: * Creates a new <code>CollectionChangeEventFactory</code> initialized
013: * to produce events from the provided source. The source must be
014: * one of the observable collection types (e.g.,
015: * <code>BoundCollection</code> or <code>ConstrainedCollection</code>.)
016: *
017: * @param source The observed collection which acts as the source of all
018: * events produced by this factory.
019: * @throws IllegalArgumentException if <code>source</code> is not
020: * one of the observable collection types.
021: */
022: public DefaultCollectionChangeEventFactory(Object source)
023: throws IllegalArgumentException {
024:
025: setCollection(source);
026: }
027:
028: public DefaultCollectionChangeEventFactory() {
029: }
030:
031: /**
032: * This method creates and returns a new
033: * DefaultCollectionChangeEventFactory. As this class does not maintain
034: * any state information other than the event source, and the
035: * event source is the only thing which is not supposed to be
036: * replicated according to the interface contract, this is entirely
037: * proper.
038: * @return A new DefaultCollectionChangeEventFactory with the
039: * source term uninitialized.
040: */
041: public Object clone() {
042: return new DefaultCollectionChangeEventFactory();
043: }
044:
045: public Object getCollection() {
046: return source;
047: }
048:
049: /**
050: * <p>
051: * Sets the source of events produced by this factory. This method may
052: * only be called once. Attempting to change the source of the factory
053: * when it has previously been set will cause this method to
054: * throw an <code>UnsupportedOperationException</code>. This method
055: * is provided so that users may explicitly supply a factory to a
056: * bound or constrained collection decorator.
057: * </p>
058: *
059: * @param source The observed collection which acts as the source of all
060: * events produced by this factory.
061: * @throws IllegalArgumentException if <code>source</code> is not
062: * one of the observable collection types.
063: * @throws UnsupportedOperationException if <code>source</code> has
064: * previously been set.
065: */
066: public void setCollection(Object source)
067: throws IllegalArgumentException,
068: UnsupportedOperationException {
069:
070: // check for an existing collection
071: if (this .source != null) {
072: throw new UnsupportedOperationException(
073: "Changing the event source is not allowed.");
074: }
075:
076: // check that the collection is the correct type.
077: if (!(source instanceof BoundCollection)
078: && !(source instanceof ConstrainedCollection)
079: && !(source instanceof BoundMap)) {
080:
081: throw new IllegalArgumentException(
082: "Source must be a BoundCollection, ConstrainedCollection, "
083: + "or BoundMap.");
084: }
085:
086: this .source = source;
087: }
088:
089: public CollectionChangeEvent createAdd(Object element,
090: boolean changed) {
091: return new CollectionChangeEvent(source,
092: CollectionChangeType.ADD, changed, element);
093: }
094:
095: public CollectionChangeEvent createAddIndexed(int index,
096: Object element, boolean changed) {
097:
098: return new CollectionChangeEvent(source,
099: CollectionChangeType.ADD_INDEXED, changed, element,
100: index);
101: }
102:
103: public CollectionChangeEvent createAddNCopies(int copies,
104: Object element, boolean changed) {
105:
106: return new CollectionChangeEvent(source,
107: CollectionChangeType.ADD_NCOPIES, changed, element,
108: copies);
109: }
110:
111: public CollectionChangeEvent createAddIterated(int index,
112: Object element, boolean changed) {
113:
114: return new CollectionChangeEvent(source,
115: CollectionChangeType.ADD_ITERATED, changed, element,
116: index);
117: }
118:
119: public CollectionChangeEvent createAddAll(Collection element,
120: boolean changed) {
121: return new CollectionChangeEvent(source,
122: CollectionChangeType.ADD_ALL, changed, element);
123: }
124:
125: public CollectionChangeEvent createAddAllIndexed(int index,
126: Collection element, boolean changed) {
127:
128: return new CollectionChangeEvent(source,
129: CollectionChangeType.ADD_ALL_INDEXED, changed, element,
130: index);
131: }
132:
133: public CollectionChangeEvent createClear(boolean changed) {
134:
135: return new CollectionChangeEvent(source,
136: CollectionChangeType.CLEAR, changed);
137: }
138:
139: public CollectionChangeEvent createRemove(Object element,
140: boolean changed) {
141: return new CollectionChangeEvent(source,
142: CollectionChangeType.REMOVE, changed, element);
143: }
144:
145: public CollectionChangeEvent createRemoveIndexed(int index,
146: Object element, boolean changed) {
147:
148: return new CollectionChangeEvent(source,
149: CollectionChangeType.REMOVE_INDEXED, changed, element,
150: index);
151: }
152:
153: public CollectionChangeEvent createRemoveNCopies(int copies,
154: Object element, boolean changed) {
155:
156: return new CollectionChangeEvent(source,
157: CollectionChangeType.REMOVE_NCOPIES, changed, element,
158: copies);
159: }
160:
161: public CollectionChangeEvent createRemoveNext(Object element,
162: boolean changed) {
163:
164: return new CollectionChangeEvent(source,
165: CollectionChangeType.REMOVE_NEXT, changed, element);
166: }
167:
168: public CollectionChangeEvent createRemoveIterated(int index,
169: Object element, boolean changed) {
170:
171: return new CollectionChangeEvent(source,
172: CollectionChangeType.REMOVE_ITERATED, changed, element,
173: index);
174: }
175:
176: public CollectionChangeEvent createRemoveAll(Collection element,
177: boolean changed) {
178:
179: return new CollectionChangeEvent(source,
180: CollectionChangeType.REMOVE_ALL, changed, element);
181: }
182:
183: public CollectionChangeEvent createRetainAll(Collection element,
184: boolean changed) {
185:
186: return new CollectionChangeEvent(source,
187: CollectionChangeType.RETAIN_ALL, changed, element);
188: }
189:
190: public CollectionChangeEvent createSetIndexed(int index,
191: Object oldValue, Object element, boolean changed) {
192:
193: return new CollectionChangeEvent(source,
194: CollectionChangeType.SET_INDEXED, changed, element,
195: oldValue, index);
196: }
197:
198: public CollectionChangeEvent createSetIterated(int index,
199: Object oldValue, Object element, boolean changed) {
200:
201: return new CollectionChangeEvent(source,
202: CollectionChangeType.SET_ITERATED, changed, element,
203: oldValue, index);
204: }
205:
206: public CollectionChangeEvent createPut(Object key, Object value,
207: Object oldValue, boolean changed) {
208:
209: return new CollectionChangeEvent(source,
210: CollectionChangeType.PUT, changed, key, oldValue, value);
211: }
212:
213: public CollectionChangeEvent createPutAll(Map map, boolean changed) {
214:
215: return new CollectionChangeEvent(source,
216: CollectionChangeType.PUT_ALL, changed, map);
217: }
218:
219: }
|