Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 29f4b231befdd3bb74cf5d8558e5dc968b94aae9 (plain) (blame)
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*******************************************************************************
 * Copyright (c) 2008, 2015 Matthew Hall and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Matthew Hall - initial API and implementation (bug 215531)
 *     Matthew Hall - bug 124684
 *         (through ViewerElementSet.java)
 *     Matthew Hall - bugs 262269, 303847
 *     Stefan Xenos <sxenos@gmail.com> - Bug 335792
 ******************************************************************************/

package org.eclipse.core.internal.databinding.identity;

import java.lang.reflect.Array;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

/**
 * A {@link Set} of elements where elements are added, removed and compared by
 * identity. Elements of the set are compared using <code>==</code> instead of
 * {@link #equals(Object)}.
 * <p>
 * This class is <i>not</i> a strict implementation the {@link Set} interface.
 * It intentionally violates the {@link Set} contract, which requires the use of
 * {@link #equals(Object)} when comparing elements.
 *
 * @param <E>
 *            the type of the elements in this collection
 * @since 1.2
 */
public class IdentitySet<E> implements Set<E> {
	private final Set<IdentityWrapper<E>> wrappedSet;

	/**
	 * Constructs an IdentitySet.
	 */
	public IdentitySet() {
		this.wrappedSet = new HashSet<>();
	}

	/**
	 * Constructs an IdentitySet containing all the unique instances in the
	 * specified collection.
	 *
	 * @param collection
	 *            the collection whose elements are to be added to this set.
	 */
	public IdentitySet(Collection<? extends E> collection) {
		this();
		addAll(collection);
	}

	@Override
	public boolean add(E o) {
		return wrappedSet.add(IdentityWrapper.wrap(o));
	}

	@Override
	public boolean addAll(Collection<? extends E> c) {
		boolean changed = false;
		for (E element : c) {
			changed |= wrappedSet.add(IdentityWrapper.wrap(element));
		}
		return changed;
	}

	@Override
	public void clear() {
		wrappedSet.clear();
	}

	@Override
	public boolean contains(Object o) {
		return wrappedSet.contains(IdentityWrapper.wrap(o));
	}

	@Override
	public boolean containsAll(Collection<?> c) {
		for (Object element : c)
			if (!wrappedSet.contains(IdentityWrapper.wrap(element)))
				return false;
		return true;
	}

	@Override
	public boolean isEmpty() {
		return wrappedSet.isEmpty();
	}

	@Override
	public Iterator<E> iterator() {
		final Iterator<IdentityWrapper<E>> wrappedIterator = wrappedSet
				.iterator();
		return new Iterator<E>() {
			@Override
			public boolean hasNext() {
				return wrappedIterator.hasNext();
			}

			@Override
			public E next() {
				return wrappedIterator.next().unwrap();
			}

			@Override
			public void remove() {
				wrappedIterator.remove();
			}
		};
	}

	@Override
	public boolean remove(Object o) {
		return wrappedSet.remove(IdentityWrapper.wrap(o));
	}

	@Override
	public boolean removeAll(Collection<?> c) {
		boolean changed = false;
		for (Object element : c)
			changed |= remove(element);
		return changed;
	}

	@Override
	public boolean retainAll(Collection<?> c) {
		// Have to do this the slow way to ensure correct comparisons. i.e.
		// cannot delegate to c.contains(it) since we can't be sure will
		// compare elements the way we want.
		boolean changed = false;
		Object[] retainAll = c.toArray();
		outer: for (Iterator<E> iterator = iterator(); iterator.hasNext();) {
			E element = iterator.next();
			for (int i = 0; i < retainAll.length; i++) {
				if (element == retainAll[i]) {
					continue outer;
				}
			}
			iterator.remove();
			changed = true;
		}
		return changed;
	}

	@Override
	public int size() {
		return wrappedSet.size();
	}

	@Override
	public Object[] toArray() {
		return toArray(new Object[wrappedSet.size()]);
	}

	@SuppressWarnings("unchecked")
	@Override
	public <T> T[] toArray(T[] a) {
		int size = wrappedSet.size();
		T[] result = a;
		if (a.length < size) {
			result = (T[]) Array.newInstance(a.getClass().getComponentType(), size);
		}

		int i = 0;
		for (IdentityWrapper<? extends E> wrapper : wrappedSet) {
			result[i++] = (T) wrapper.unwrap();
		}
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (obj == this)
			return true;
		if (!(obj instanceof Set))
			return false;
		Set<?> that = (Set<?>) obj;
		return size() == that.size() && containsAll(that);
	}

	@Override
	public int hashCode() {
		int hash = 0;
		for (E element : this) {
			hash += element == null ? 0 : element.hashCode();
		}
		return hash;
	}
}

Back to the top