001/*-------------------------------------------------------------------------+
002|                                                                          |
003| Copyright 2005-2011 The ConQAT Project                                   |
004|                                                                          |
005| Licensed under the Apache License, Version 2.0 (the "License");          |
006| you may not use this file except in compliance with the License.         |
007| You may obtain a copy of the License at                                  |
008|                                                                          |
009|    http://www.apache.org/licenses/LICENSE-2.0                            |
010|                                                                          |
011| Unless required by applicable law or agreed to in writing, software      |
012| distributed under the License is distributed on an "AS IS" BASIS,        |
013| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
014| See the License for the specific language governing permissions and      |
015| limitations under the License.                                           |
016+-------------------------------------------------------------------------*/
017package org.conqat.lib.commons.collections;
018
019import java.util.Collection;
020import java.util.IdentityHashMap;
021
022/**
023 * This class implements a set based on referential equality similar to JDK
024 * class {@link IdentityHashMap}. This class can be e.g. used to implement
025 * listener lists that should not rely on the listeners
026 * <code>equals()</code>-methods.
027 * <p>
028 * The implementation is based on class {@link java.util.HashSet} that also uses
029 * an underlying hash map.
030 */
031public class IdentityHashSet<E> extends MapBackedSetBase<E> {
032
033        public IdentityHashSet() {
034                super(new IdentityHashMap<E, Object>());
035        }
036
037        /** Create new identity hash set from an existing collection. */
038        public IdentityHashSet(Collection<? extends E> collection) {
039                this(collection.size());
040                addAll(collection);
041        }
042
043        /** Create new identity hash set with an expected maximum size. */
044        public IdentityHashSet(int expectedMaxSize) {
045                super(new IdentityHashMap<E, Object>(expectedMaxSize));
046        }
047
048        /**
049         * Returns a shallow copy of this <tt>IdentityHashSet</tt> instance: the
050         * elements themselves are not cloned.
051         * 
052         * @return a shallow copy of this set.
053         */
054        @Override
055        public IdentityHashSet<E> clone() {
056                return new IdentityHashSet<>(this);
057        }
058
059}