01: package org.apache.commons.events.observable;
02:
03: import java.util.Set;
04: import org.apache.commons.collections.Bag;
05:
06: public class BoundBag extends BoundCollection implements Bag {
07:
08: // Constructors
09: //-----------------------------------------------------------------------
10: protected BoundBag(final Bag source,
11: final CollectionChangeEventFactory eventFactory) {
12:
13: super (source, eventFactory);
14: }
15:
16: protected BoundBag(final Bag source) {
17: super (source);
18: }
19:
20: // Factory methods
21: //-----------------------------------------------------------------------
22: public static BoundBag decorate(final Bag source,
23: final CollectionChangeEventFactory eventFactory) {
24:
25: return new BoundBag(source, eventFactory);
26: }
27:
28: public static BoundBag decorate(final Bag source) {
29: return new BoundBag(source);
30: }
31:
32: // Utility methods
33: //-----------------------------------------------------------------------
34: private Bag getBag() {
35: return (Bag) (getCollection());
36: }
37:
38: // Bag API (methods which don't change the collection)
39: //-----------------------------------------------------------------------
40: public int getCount(Object object) {
41: return getBag().getCount(object);
42: }
43:
44: public Set uniqueSet() {
45: return getBag().uniqueSet();
46: }
47:
48: // Decorated methods
49: //-----------------------------------------------------------------------
50: // overridden because of the contract violation w.r.t. Collections.
51: public boolean add(Object object) {
52: boolean result = getBag().add(object);
53: eventFactory.createAdd(object, true);
54: return result;
55: }
56:
57: public boolean add(Object object, int nCopies) {
58: boolean changed = (nCopies != 0);
59: boolean result = getBag().add(object, nCopies);
60: eventFactory.createAddNCopies(nCopies, object, changed);
61: return result;
62: }
63:
64: public boolean remove(Object object, int nCopies) {
65: boolean changed = getBag().remove(object, nCopies);
66: eventFactory.createRemoveNCopies(nCopies, object, changed);
67: return changed;
68: }
69: }
|