com.databasesandlife.util.gwtsafe.IdentityHashSet Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of java-common Show documentation
Show all versions of java-common Show documentation
Utility classes developed at Adrian Smith Software (A.S.S.)
The newest version!
package com.databasesandlife.util.gwtsafe;
import java.io.Serializable;
import java.util.Collection;
import java.util.IdentityHashMap;
import java.util.Iterator;
/**
* Like a {@link java.util.HashSet} but compares objects based on the Java object identity.
*
* Does not implement {@link java.util.Set} interface because it is less generic than it could be;
* and in the project where this was required implementing the interface was not required.
*
* @author This source is copyright Adrian Smith and licensed under the LGPL 3.
* @see Project on GitHub
*/
public class IdentityHashSet implements Iterable, Serializable {
protected final IdentityHashMap map = new IdentityHashMap<>();
// Constructors
public IdentityHashSet() { }
public IdentityHashSet(Collection collection) { for (var x : collection) add(x); }
// Mutators
public void add(T obj) { map.put(obj, null); }
public void addAll(Collection objects) { for (var o : objects) add(o); }
public void addAll(T[] objects) { for (var o : objects) add(o); }
public void remove(T obj) { map.remove(obj); }
// Query
public boolean contains(T obj) { return map.containsKey(obj); }
public Iterator iterator() { return map.keySet().iterator(); }
public int size() { return map.size(); }
public boolean isEmpty() { return map.isEmpty(); }
}