org.conqat.lib.commons.collections.IdentityHashSet Maven / Gradle / Ivy
Show all versions of teamscale-lib-commons Show documentation
/*
* Copyright (c) CQSE GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.conqat.lib.commons.collections;
import java.util.Collection;
import java.util.IdentityHashMap;
/**
* This class implements a set based on referential equality similar to JDK class
* {@link IdentityHashMap}. This class can be e.g. used to implement listener lists that should not
* rely on the listeners equals()
-methods.
*
* The implementation is based on class {@link java.util.HashSet} that also uses an underlying hash
* map.
*/
public class IdentityHashSet extends MapBackedSetBase {
public IdentityHashSet() {
super(new IdentityHashMap<>());
}
/** Create new identity hash set from an existing collection. */
public IdentityHashSet(Collection collection) {
this(collection.size());
addAll(collection);
}
/** Create new identity hash set with an expected maximum size. */
public IdentityHashSet(int expectedMaxSize) {
super(new IdentityHashMap<>(expectedMaxSize));
}
/**
* Returns a shallow copy of this IdentityHashSet instance: the elements themselves are not
* cloned.
*
* @return a shallow copy of this set.
*/
@Override
public IdentityHashSet clone() {
return new IdentityHashSet<>(this);
}
}