|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| |
|
17 |
| |
|
18 |
| |
|
19 |
| |
|
20 |
| |
|
21 |
| |
|
22 |
| |
|
23 |
| |
|
24 |
| |
|
25 |
| |
|
26 |
| |
|
27 |
| |
|
28 |
| |
|
29 |
| |
|
30 |
| |
|
31 |
| |
|
32 |
| |
|
33 |
| |
|
34 |
| |
|
35 |
| package edu.rice.cs.plt.collect; |
|
36 |
| |
|
37 |
| import java.util.AbstractCollection; |
|
38 |
| import java.util.Iterator; |
|
39 |
| import java.util.Collection; |
|
40 |
| import java.io.Serializable; |
|
41 |
| import edu.rice.cs.plt.iter.SizedIterable; |
|
42 |
| import edu.rice.cs.plt.iter.IterUtil; |
|
43 |
| import edu.rice.cs.plt.iter.ImmutableIterator; |
|
44 |
| import edu.rice.cs.plt.object.Composite; |
|
45 |
| import edu.rice.cs.plt.object.ObjectUtil; |
|
46 |
| |
|
47 |
| |
|
48 |
| |
|
49 |
| |
|
50 |
| |
|
51 |
| |
|
52 |
| public class IterableCollection<E> extends AbstractCollection<E> |
|
53 |
| implements SizedIterable<E>, Composite, Serializable { |
|
54 |
| |
|
55 |
| private final Iterable<? extends E> _iter; |
|
56 |
| private final boolean _fixedSize; |
|
57 |
| private int _size; |
|
58 |
| |
|
59 |
74
| public IterableCollection(Iterable<? extends E> iter) {
|
|
60 |
74
| _iter = iter;
|
|
61 |
74
| _fixedSize = IterUtil.hasFixedSize(iter);
|
|
62 |
74
| _size = -1;
|
|
63 |
| } |
|
64 |
| |
|
65 |
0
| public int compositeHeight() { return ObjectUtil.compositeHeight(_iter) + 1; }
|
|
66 |
0
| public int compositeSize() { return ObjectUtil.compositeSize(_iter) + 1; }
|
|
67 |
| |
|
68 |
0
| @Override public boolean isEmpty() {
|
|
69 |
0
| if (_size == -1) { return IterUtil.isEmpty(_iter); }
|
|
70 |
0
| else { return _size == 0; }
|
|
71 |
| } |
|
72 |
| |
|
73 |
74
| public int size() {
|
|
74 |
74
| if (_fixedSize) {
|
|
75 |
74
| if (_size == -1) { _size = IterUtil.sizeOf(_iter); }
|
|
76 |
74
| return _size;
|
|
77 |
| } |
|
78 |
0
| else { return IterUtil.sizeOf(_iter); }
|
|
79 |
| } |
|
80 |
| |
|
81 |
0
| public int size(int bound) {
|
|
82 |
0
| if (_fixedSize) {
|
|
83 |
0
| if (_size == -1) {
|
|
84 |
0
| int result = IterUtil.sizeOf(_iter, bound);
|
|
85 |
0
| if (result < bound) { _size = result; }
|
|
86 |
0
| return result;
|
|
87 |
| } |
|
88 |
0
| else { return (_size < bound) ? _size : bound; }
|
|
89 |
| } |
|
90 |
0
| else { return IterUtil.sizeOf(_iter, bound); }
|
|
91 |
| } |
|
92 |
| |
|
93 |
0
| public boolean isInfinite() { return IterUtil.isInfinite(_iter); }
|
|
94 |
0
| public boolean hasFixedSize() { return _fixedSize; }
|
|
95 |
0
| public boolean isStatic() { return _fixedSize && IterUtil.isStatic(_iter); }
|
|
96 |
| |
|
97 |
0
| @Override public boolean contains(Object o) {
|
|
98 |
0
| return IterUtil.contains(_iter, o);
|
|
99 |
| } |
|
100 |
| |
|
101 |
74
| public Iterator<E> iterator() { return new ImmutableIterator<E>(_iter.iterator()); }
|
|
102 |
| |
|
103 |
0
| @Override public boolean add(E o) { throw new UnsupportedOperationException(); }
|
|
104 |
0
| @Override public boolean addAll(Collection<? extends E> c) { throw new UnsupportedOperationException(); }
|
|
105 |
0
| @Override public boolean remove(Object o) { throw new UnsupportedOperationException(); }
|
|
106 |
0
| @Override public boolean removeAll(Collection<?> c) { throw new UnsupportedOperationException(); }
|
|
107 |
0
| @Override public boolean retainAll(Collection<?> c) { throw new UnsupportedOperationException(); }
|
|
108 |
0
| @Override public void clear() { throw new UnsupportedOperationException(); }
|
|
109 |
| |
|
110 |
0
| public String toString() { return _iter.toString(); }
|
|
111 |
0
| public boolean equals(Object o) {
|
|
112 |
0
| if (this == o) { return true; }
|
|
113 |
0
| else if (!(o instanceof IterableCollection<?>)) { return false; }
|
|
114 |
0
| else { return _iter.equals(((IterableCollection<?>) o)._iter); }
|
|
115 |
| } |
|
116 |
0
| public int hashCode() { return IterableCollection.class.hashCode() ^ _iter.hashCode(); }
|
|
117 |
| } |