Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
IdentitySet |
|
| 0.0;0 |
1 | /* | |
2 | Copyright 2004-2008 Paul R. Holser, Jr. All rights reserved. | |
3 | Licensed under the Academic Free License version 3.0 | |
4 | */ | |
5 | ||
6 | package jaggregate; | |
7 | ||
8 | import java.io.IOException; | |
9 | import java.io.ObjectInputStream; | |
10 | import java.io.ObjectOutputStream; | |
11 | import java.io.Serializable; | |
12 | ||
13 | import static jaggregate.IdentityDictionary.*; | |
14 | ||
15 | /** | |
16 | * A set for which <dfn>equivalence</dfn> is defined by {@code ==}. | |
17 | * | |
18 | * @param <E> a restriction on the types of elements that may be included in the set | |
19 | * | |
20 | * @author <a href="mailto:pholser@alumni.rice.edu">Paul Holser</a> | |
21 | * @version $Id: IdentitySet.java,v 1.4 2008/05/07 06:00:48 pholser Exp $ | |
22 | */ | |
23 | 85 | public class IdentitySet<E> extends AbstractSet<E> implements Serializable { |
24 | private static final long serialVersionUID = -1L; | |
25 | ||
26 | /** | |
27 | * Creates an empty set. | |
28 | */ | |
29 | 238 | public IdentitySet() { |
30 | // empty on purpose | |
31 | 238 | } |
32 | ||
33 | /** | |
34 | * Creates a set containing the elements in the given collection. | |
35 | * | |
36 | * @param elements elements to add to the new set | |
37 | * @throws NullPointerException if {@code elements} is {@code null} | |
38 | */ | |
39 | public IdentitySet( Collection<? extends E> elements ) { | |
40 | 13 | super( elements ); |
41 | 12 | } |
42 | ||
43 | /** | |
44 | * Creates a set containing the elements in the given array. | |
45 | * | |
46 | * @param elements elements to add to the new set | |
47 | * @throws NullPointerException if {@code elements} is {@code null} | |
48 | */ | |
49 | public IdentitySet( E... elements ) { | |
50 | 116 | super( elements ); |
51 | 115 | } |
52 | ||
53 | /** | |
54 | * Creates a set containing the elements in the given iterable object. | |
55 | * | |
56 | * @param elements elements to add to the new set | |
57 | * @throws NullPointerException if {@code elements} is {@code null} | |
58 | */ | |
59 | public IdentitySet( Iterable<? extends E> elements ) { | |
60 | 2 | super( elements ); |
61 | 1 | } |
62 | ||
63 | /** | |
64 | * Creates an empty set. | |
65 | * | |
66 | * @param <T> the type of elements allowed in the new set | |
67 | * @return a new empty set | |
68 | */ | |
69 | public static <T> IdentitySet<T> emptyIdentitySet() { | |
70 | 238 | return new IdentitySet<T>(); |
71 | } | |
72 | ||
73 | /** | |
74 | * Creates a set containing the elements in the given collection. | |
75 | * | |
76 | * @param <T> the type of elements allowed in the new set | |
77 | * @param elements elements to add to the new set | |
78 | * @return the new set | |
79 | * @throws NullPointerException if {@code elements} is {@code null} | |
80 | */ | |
81 | public static <T> IdentitySet<T> identitySetFrom( Collection<? extends T> elements ) { | |
82 | 5 | return new IdentitySet<T>( elements ); |
83 | } | |
84 | ||
85 | /** | |
86 | * Creates a set containing the elements in the given array. | |
87 | * | |
88 | * @param <T> the type of elements allowed in the new set | |
89 | * @param elements elements to add to the new set | |
90 | * @return the new set | |
91 | * @throws NullPointerException if {@code elements} is {@code null} | |
92 | */ | |
93 | public static <T> IdentitySet<T> identitySetFrom( T[] elements ) { | |
94 | 114 | return new IdentitySet<T>( elements ); |
95 | } | |
96 | ||
97 | /** | |
98 | * Creates a set containing the given elements. | |
99 | * | |
100 | * @param <T> the type of elements allowed in the new set | |
101 | * @param newElement first new element to add | |
102 | * @param restOfNewElements remainder of the elements to add | |
103 | * @return the new set | |
104 | * @throws NullPointerException if {@code restOfNewElements} is {@code null} | |
105 | * @throws IllegalArgumentException if any of the new elements is found to violate | |
106 | * restrictions on the characteristics of valid elements | |
107 | * @see #addAll(Collection) | |
108 | */ | |
109 | public static <T> IdentitySet<T> identitySetWith( T newElement, | |
110 | T... restOfNewElements ) { | |
111 | ||
112 | 81 | IdentitySet<T> newSet = identitySetFrom( restOfNewElements ); |
113 | 81 | newSet.add( newElement ); |
114 | ||
115 | 81 | return newSet; |
116 | } | |
117 | ||
118 | /** | |
119 | * Creates a set containing the elements given by an iterable object. | |
120 | * | |
121 | * @param <T> the type of elements allowed in the new set | |
122 | * @param elements elements to add to the new set | |
123 | * @return the new set | |
124 | * @throws NullPointerException if {@code elements} is {@code null} | |
125 | */ | |
126 | public static <T> IdentitySet<T> identitySetFrom( Iterable<? extends T> elements ) { | |
127 | 2 | return new IdentitySet<T>( elements ); |
128 | } | |
129 | ||
130 | /** | |
131 | * {@inheritDoc} | |
132 | * <p/> | |
133 | * Answers self. | |
134 | */ | |
135 | @Override | |
136 | public IdentitySet<E> toIdentitySet() { | |
137 | 1 | return this; |
138 | } | |
139 | ||
140 | /** | |
141 | * {@inheritDoc} | |
142 | * | |
143 | * @return a set of results | |
144 | */ | |
145 | @Override | |
146 | public <R> IdentitySet<R> collect( | |
147 | UnaryFunctor<? super E, ? extends R> transformer ) { | |
148 | ||
149 | 3 | return (IdentitySet<R>) super.collect( transformer ); |
150 | } | |
151 | ||
152 | /** | |
153 | * {@inheritDoc} | |
154 | * | |
155 | * @return a set of rejects | |
156 | */ | |
157 | @Override | |
158 | public IdentitySet<E> reject( UnaryCondition<? super E> discriminator ) { | |
159 | 2 | return (IdentitySet<E>) super.reject( discriminator ); |
160 | } | |
161 | ||
162 | /** | |
163 | * {@inheritDoc} | |
164 | * | |
165 | * @return a set of selections | |
166 | */ | |
167 | @Override | |
168 | public IdentitySet<E> select( UnaryCondition<? super E> discriminator ) { | |
169 | 27 | return (IdentitySet<E>) super.select( discriminator ); |
170 | } | |
171 | ||
172 | /** | |
173 | * {@inheritDoc} | |
174 | */ | |
175 | @Override | |
176 | protected Class<? extends AbstractSet> getImplementationClass() { | |
177 | 275 | return IdentitySet.class; |
178 | } | |
179 | ||
180 | /** | |
181 | * {@inheritDoc} | |
182 | */ | |
183 | @Override | |
184 | protected AbstractDictionary<E, Object> newEmptyStorage() { | |
185 | 370 | return emptyIdentityDictionary(); |
186 | } | |
187 | ||
188 | /** | |
189 | * {@inheritDoc} | |
190 | */ | |
191 | @Override | |
192 | protected IdentitySet<E> newEmptyExtensibleCollection() { | |
193 | 27 | return newEmptyExtensibleResultCollection(); |
194 | } | |
195 | ||
196 | /** | |
197 | * {@inheritDoc} | |
198 | */ | |
199 | @Override | |
200 | protected <R> IdentitySet<R> newEmptyExtensibleResultCollection() { | |
201 | 29 | return emptyIdentitySet(); |
202 | } | |
203 | ||
204 | /** | |
205 | * {@inheritDoc} | |
206 | */ | |
207 | @Override | |
208 | protected <T> IdentitySet<T> newEmptySet() { | |
209 | 35 | return emptyIdentitySet(); |
210 | } | |
211 | ||
212 | /** | |
213 | * {@inheritDoc} | |
214 | */ | |
215 | @Override | |
216 | protected String asString( E anElement ) { | |
217 | 6 | return Objects.identityToString( anElement ); |
218 | } | |
219 | ||
220 | private void writeObject( ObjectOutputStream output ) throws IOException { | |
221 | 1 | serializeTo( output ); |
222 | 1 | } |
223 | ||
224 | private void readObject( ObjectInputStream input ) | |
225 | throws IOException, ClassNotFoundException { | |
226 | ||
227 | 1 | deserializeFrom( input ); |
228 | 1 | } |
229 | } |