View Javadoc
1   package de.fhdw.wtf.context.model.collections;
2   
3   import java.util.Iterator;
4   import java.util.Vector;
5   
6   import de.fhdw.wtf.context.exception.FrameworkException;
7   import de.fhdw.wtf.context.model.Anything;
8   import de.fhdw.wtf.context.model.collections.functors.Predicate;
9   import de.fhdw.wtf.context.model.collections.functors.Procedure;
10  
11  /**
12   * A class to represent a list of values in no-db context.
13   * 
14   * @param <T>
15   *            any subtype of anything.
16   */
17  public class TransientList<T extends Anything> extends MutableList<T> {
18  	
19  	/**
20  	 * The Elements in the Collection.
21  	 */
22  	private final java.util.List<T> elements;
23  	
24  	/**
25  	 * The constructor for a new Collection.
26  	 */
27  	public TransientList() {
28  		this.elements = new Vector<>();
29  	}
30  	
31  	@Override
32  	public Iterator<T> iterator() {
33  		return this.elements.iterator();
34  	}
35  	
36  	@Override
37  	public Collection<T> union(final Collection<? extends T> otherCollection) {
38  		if (otherCollection instanceof ImmutableCollection) {
39  			this.addImmutableCollection((ImmutableCollection<? extends T>) otherCollection);
40  		} else if (otherCollection instanceof MutableCollection) {
41  			this.addMutableCollection((MutableCollection<? extends T>) otherCollection);
42  		} else {
43  			throw new FrameworkException("Collection type is not supported");
44  		}
45  		return this;
46  	}
47  	
48  	/**
49  	 * Adds the elements of a MutableCollection to this list.
50  	 * 
51  	 * @param otherCollection
52  	 *            The MutableCollection.
53  	 */
54  	private void addMutableCollection(final MutableCollection<? extends T> otherCollection) {
55  		final Iterator<? extends T> iterator = otherCollection.iterator();
56  		while (iterator.hasNext()) {
57  			final T current = iterator.next();
58  			this.elements.add(current);
59  		}
60  	}
61  	
62  	/**
63  	 * Adds the elements of an ImmutableCollection to this list.
64  	 * 
65  	 * @param otherCollection
66  	 *            The ImmutableCollection.
67  	 */
68  	private void addImmutableCollection(final ImmutableCollection<? extends T> otherCollection) {
69  		if (!otherCollection.isEmpty()) {
70  			this.elements.add(otherCollection.front());
71  			this.addImmutableCollection(otherCollection.tail());
72  		}
73  	}
74  	
75  	@Override
76  	public boolean contains(final T element) {
77  		return this.elements.contains(element);
78  	}
79  	
80  	@Override
81  	public void insert(final T element) {
82  		this.elements.add(element);
83  	}
84  	
85  	@Override
86  	public void apply(final Procedure<T> procedure) {
87  		final Iterator<T> iterator = this.iterator();
88  		while (iterator.hasNext()) {
89  			final T current = iterator.next();
90  			procedure.op(current);
91  		}
92  	}
93  	
94  	@Override
95  	public void remove(final Predicate<T> predicate) {
96  		final Iterator<T> iterator = this.iterator();
97  		while (iterator.hasNext()) {
98  			final T current = iterator.next();
99  			if (predicate.p(current)) {
100 				iterator.remove();
101 			}
102 		}
103 	}
104 	
105 	@Override
106 	public boolean isEmpty() {
107 		return this.elements.isEmpty();
108 	}
109 	
110 	@Override
111 	public ImmutableCollection<T> copy() {
112 		ImmutableCollection<T> result = new ImmutableList<>();
113 		final Iterator<T> iterator = this.elements.iterator();
114 		while (iterator.hasNext()) {
115 			final T current = iterator.next();
116 			result = result.add(current);
117 		}
118 		return result;
119 	}
120 	
121 	@Override
122 	public boolean equals(final Object obj) {
123 		if (obj instanceof TransientList<?>) {
124 			final TransientList<?> other = (TransientList<?>) obj;
125 			return this.elements.equals(other.elements);
126 		}
127 		if (obj instanceof MutableCollection<?>) {
128 			try {
129 				@SuppressWarnings("unchecked")
130 				final MutableCollection<T> other = (MutableCollection<T>) obj;
131 				final Iterator<T> thisIterator = this.iterator();
132 				while (thisIterator.hasNext()) {
133 					final T current = thisIterator.next();
134 					if (!other.contains(current)) {
135 						return false;
136 					}
137 				}
138 				final Iterator<T> otherIterator = other.iterator();
139 				while (otherIterator.hasNext()) {
140 					final T current = otherIterator.next();
141 					if (!this.contains(current)) {
142 						return false;
143 					}
144 				}
145 				return true;
146 			} catch (final ClassCastException e) {
147 				return false;
148 			}
149 		}
150 		return false;
151 	}
152 	
153 	@Override
154 	public int hashCode() {
155 		return this.elements.hashCode();
156 	}
157 	
158 	@Override
159 	public String toString() {
160 		return this.elements.toString();
161 	}
162 	
163 }