meaningless comment
[ephemerata.git] / KezvhLib / src-lib / net / kezvh / collections / views / CollectionView.java
blob525726c2e8401d26d947674f66a076004c0e7885
1 /**
3 */
4 package net.kezvh.collections.views;
6 import java.util.AbstractCollection;
7 import java.util.Collection;
8 import java.util.Iterator;
10 import net.kezvh.functional.Operations;
11 import net.kezvh.functional.lambda.L1;
13 /**
14 * @author afflux
15 * @param <D> the destination type
16 * @param <S> the source type
18 public class CollectionView<S, D> extends AbstractCollection<D> {
20 private final Collection<S> innerCollection;
21 private final L1<D, S> mappingFunction;
22 private final L1<S, D> inverseFunction;
24 /**
25 * @param innerCollection collection to map
26 * @param mappingFunction function to map collection
28 @SuppressWarnings("unchecked")
29 public CollectionView(final Collection<S> innerCollection, final L1<D, S> mappingFunction) {
30 this(innerCollection, mappingFunction, (L1<S, D>) Operations.UNSUPPORTED);
33 /**
34 * @param innerCollection COMMENT
35 * @param mappingFunction COMMENT
36 * @param inverseFunction COMMENT
38 public CollectionView(final Collection<S> innerCollection, final L1<D, S> mappingFunction, final L1<S, D> inverseFunction) {
39 super();
40 if (innerCollection == null)
41 throw new IllegalArgumentException();
42 this.innerCollection = innerCollection;
43 this.mappingFunction = mappingFunction;
44 this.inverseFunction = inverseFunction;
47 /**
48 * @see java.util.AbstractCollection#iterator()
49 * @return iterator
51 @Override
52 public Iterator<D> iterator() {
53 return new IteratorView<S, D>(this.mappingFunction, this.innerCollection.iterator());
56 /**
57 * @see java.util.AbstractCollection#size()
58 * @return size of the collecion
60 @Override
61 public int size() {
62 return this.innerCollection.size();
65 /**
66 * @return the mappingFunction
68 public L1<D, ? super S> getMappingFunction() {
69 return this.mappingFunction;
72 @Override
73 public boolean add(final D o) {
74 return this.innerCollection.add(this.inverseFunction.op(o));
77 /**
78 * @see java.lang.Object#hashCode()
79 * @return COMMENT
81 @Override
82 public int hashCode() {
83 final int prime = 31;
84 int result = 1;
85 result = prime * result + ((this.innerCollection == null) ? 0 : this.innerCollection.hashCode());
86 result = prime * result + ((this.inverseFunction == null) ? 0 : this.inverseFunction.hashCode());
87 result = prime * result + ((this.mappingFunction == null) ? 0 : this.mappingFunction.hashCode());
88 return result;
91 /**
92 * @see java.lang.Object#equals(java.lang.Object)
93 * @param obj COMMENT
94 * @return COMMENT
96 @Override
97 public boolean equals(final Object obj) {
98 if (this == obj)
99 return true;
100 if (obj == null)
101 return false;
102 if (this.getClass() != obj.getClass())
103 return false;
104 final CollectionView<?, ?> other = (CollectionView<?, ?>) obj;
105 if (this.innerCollection == null) {
106 if (other.innerCollection != null)
107 return false;
108 } else if (!this.innerCollection.equals(other.innerCollection))
109 return false;
110 if (this.inverseFunction == null) {
111 if (other.inverseFunction != null)
112 return false;
113 } else if (!this.inverseFunction.equals(other.inverseFunction))
114 return false;
115 if (this.mappingFunction == null) {
116 if (other.mappingFunction != null)
117 return false;
118 } else if (!this.mappingFunction.equals(other.mappingFunction))
119 return false;
120 return true;
124 * @see java.util.AbstractCollection#addAll(java.util.Collection)
125 * @param c COMMENT
126 * @return COMMENT
128 @SuppressWarnings("unchecked")
129 @Override
130 public boolean addAll(final Collection<? extends D> c) {
131 final Collection<S> inverse = new CollectionView<D, S>((Collection<D>) c, this.inverseFunction, this.mappingFunction);
132 return this.innerCollection.addAll(inverse);
136 * @see java.util.AbstractCollection#clear()
138 @Override
139 public void clear() {
140 this.innerCollection.clear();
144 * @see java.util.AbstractCollection#contains(java.lang.Object)
145 * @param o COMMENT
146 * @return COMMENT
148 @SuppressWarnings("unchecked")
149 @Override
150 public boolean contains(final Object o) {
151 if (this.inverseFunction.equals(Operations.UNSUPPORTED))
152 return super.contains(o);
153 return this.innerCollection.contains(this.inverseFunction.op((D) o));
157 * @see java.util.AbstractCollection#containsAll(java.util.Collection)
158 * @param c COMMENT
159 * @return COMMENT
161 @SuppressWarnings("unchecked")
162 @Override
163 public boolean containsAll(final Collection<?> c) {
164 if (this.inverseFunction.equals(Operations.UNSUPPORTED))
165 return super.containsAll(c);
167 final Collection<S> inverse = new CollectionView<D, S>((Collection<D>) c, this.inverseFunction, this.mappingFunction);
168 return this.innerCollection.containsAll(inverse);
172 * @see java.util.AbstractCollection#remove(java.lang.Object)
173 * @param o COMMENT
174 * @return COMMENT
176 @SuppressWarnings("unchecked")
177 @Override
178 public boolean remove(final Object o) {
179 if (this.inverseFunction.equals(Operations.UNSUPPORTED))
180 super.remove(o);
182 return this.innerCollection.remove(this.inverseFunction.op((D) o));
186 * @see java.util.AbstractCollection#isEmpty()
187 * @return COMMENT
189 @Override
190 public boolean isEmpty() {
191 return this.innerCollection.isEmpty();
195 * @see java.util.AbstractCollection#removeAll(java.util.Collection)
196 * @param c COMMENT
197 * @return COMMENT
199 @SuppressWarnings("unchecked")
200 @Override
201 public boolean removeAll(final Collection<?> c) {
202 if (this.inverseFunction.equals(Operations.UNSUPPORTED))
203 return super.removeAll(c);
205 final Collection<S> inverse = new CollectionView<D, S>((Collection<D>) c, this.inverseFunction, this.mappingFunction);
206 return this.innerCollection.removeAll(inverse);
210 * @see java.util.AbstractCollection#retainAll(java.util.Collection)
211 * @param c COMMENT
212 * @return COMMENT
214 @SuppressWarnings("unchecked")
215 @Override
216 public boolean retainAll(final Collection<?> c) {
217 if (this.inverseFunction.equals(Operations.UNSUPPORTED))
218 return super.removeAll(c);
220 final Collection<S> inverse = new CollectionView<D, S>((Collection<D>) c, this.inverseFunction, this.mappingFunction);
221 return this.innerCollection.removeAll(inverse);