1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package net.sf.collections15.collection;
17
18 import java.util.Collection;
19 import java.util.Iterator;
20
21 import net.sf.collections15.BoundedCollection;
22 import net.sf.collections15.iterators.UnmodifiableIterator;
23
24
25 /***
26 * <code>UnmodifiableBoundedCollection</code> decorates another
27 * <code>BoundedCollection</code> to ensure it can't be altered.
28 * <p>
29 * If a BoundedCollection is first wrapped in some other collection decorator,
30 * such as synchronized or predicated, the BoundedCollection methods are no
31 * longer accessible.
32 * The factory on this class will attempt to retrieve the bounded nature by
33 * examining the package scope variables.
34 * <p>
35 * This class is Serializable from Commons Collections 3.1.
36 *
37 * @since Commons Collections 3.0
38 * @version $Revision: 1.1 $ $Date: 2005/05/25 21:25:42 $
39 *
40 * @author Stephen Colebourne
41 */
42 public final class UnmodifiableBoundedCollection<E>
43 extends AbstractSerializableCollectionDecorator<E>
44 implements BoundedCollection<E> {
45
46 /*** Serialization version */
47 private static final long serialVersionUID = 3257002159609362480L;
48
49
50 /***
51 * Factory method to create an unmodifiable bounded collection.
52 *
53 * @param coll the <code>BoundedCollection</code> to decorate, must not be null
54 * @return a new unmodifiable bounded collection
55 * @throws IllegalArgumentException if bag is null
56 */
57 public static <T> BoundedCollection<T> decorate(BoundedCollection<T> coll) {
58 return new UnmodifiableBoundedCollection<T>(coll);
59 }
60
61 /***
62 * Factory method to create an unmodifiable bounded collection.
63 * <p>
64 * This method is capable of drilling down through up to 1000 other decorators
65 * to find a suitable BoundedCollection.
66 *
67 * @param coll the <code>BoundedCollection</code> to decorate, must not be null
68 * @return a new unmodifiable bounded collection
69 * @throws IllegalArgumentException if bag is null
70 */
71 public static <T> BoundedCollection<T> decorateUsing(Collection<T> coll) {
72 if (coll == null) {
73 throw new IllegalArgumentException("The collection must not be null");
74 }
75
76
77 for (int i = 0; i < 1000; i++) {
78 if (coll instanceof BoundedCollection) {
79 break;
80 } else if (coll instanceof AbstractCollectionDecorator) {
81 coll = ((AbstractCollectionDecorator<T>) coll).collection;
82 } else if (coll instanceof SynchronizedCollection) {
83 coll = ((SynchronizedCollection<T>) coll).collection;
84 } else {
85 break;
86 }
87 }
88
89 if (coll instanceof BoundedCollection == false) {
90 throw new IllegalArgumentException("The collection is not a bounded collection");
91 }
92 return new UnmodifiableBoundedCollection<T>((BoundedCollection<T>) coll);
93 }
94
95 /***
96 * Constructor that wraps (not copies).
97 *
98 * @param coll the collection to decorate, must not be null
99 * @throws IllegalArgumentException if coll is null
100 */
101 private UnmodifiableBoundedCollection(BoundedCollection<E> coll) {
102 super(coll);
103 }
104
105
106 public Iterator<E> iterator() {
107 return UnmodifiableIterator.<E>decorate(getCollection().iterator());
108 }
109
110 public boolean add(E object) {
111 throw new UnsupportedOperationException();
112 }
113
114 public boolean addAll(Collection<? extends E> coll) {
115 throw new UnsupportedOperationException();
116 }
117
118 public void clear() {
119 throw new UnsupportedOperationException();
120 }
121
122 public boolean remove(Object object) {
123 throw new UnsupportedOperationException();
124 }
125
126 public boolean removeAll(Collection<?> coll) {
127 throw new UnsupportedOperationException();
128 }
129
130 public boolean retainAll(Collection<?> coll) {
131 throw new UnsupportedOperationException();
132 }
133
134
135 public boolean isFull() {
136 return ((BoundedCollection<E>) collection).isFull();
137 }
138
139 public int maxSize() {
140 return ((BoundedCollection<E>) collection).maxSize();
141 }
142
143 }