org.yestech.lib.lang.ArraySet Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of yeslib Show documentation
Show all versions of yeslib Show documentation
A collection of classes that can be used across yestech artifacts/components, but must not be dependant
on any yestech component. Most of the code is utility type code. When more than a few classes are
found to be in a package or the package start to handle more that a few reposibilities then a new
independant component is created and the existing code in yeslib is ported to the new component.
/*
* Copyright LGPL3
* YES Technology Association
* http://yestech.org
*
* http://www.opensource.org/licenses/lgpl-3.0.html
*/
package org.yestech.lib.lang;
import java.io.Serializable;
import java.util.AbstractSet;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.Set;
/**
* Implmentation of a {@link java.util.Set} backed by an {@link java.util.ArrayList}.
*
*/
public class ArraySet extends AbstractSet implements Set,
Cloneable, Serializable {
private ArrayList backingList;
public ArraySet() {
this(0);
}
public ArraySet(int size) {
super();
backingList = new ArrayList(size);
}
public ArraySet(Collection c) {
this(c, 0);
addAll(c);
}
public ArraySet(Collection c, int size) {
this(size);
addAll(c);
}
/**
* Returns an iterator over the elements contained in this collection.
*
* @return an iterator over the elements contained in this collection.
*/
public Iterator iterator() {
return backingList.listIterator();
}
/**
* Returns the number of elements in this collection. If the collection
* contains more than Integer.MAX_VALUE elements, returns
* Integer.MAX_VALUE.
*
* @return the number of elements in this collection.
*/
public int size() {
return backingList.size();
}
/**
* Returns true if this set contains no elements.
*
* @return true if this set contains no elements.
*/
public boolean isEmpty() {
return backingList.isEmpty();
}
/**
* Returns true if this set contains the specified element.
*
* @param o element whose presence in this set is to be tested.
* @return true if this set contains the specified element.
*/
public boolean contains(Object o) {
return backingList.contains(o);
}
/**
* Adds the specified element to this set if it is not already
* present.
*
* @param o element to be added to this set.
* @return true if the set did not already contain the specified
* element.
*/
public boolean add(E o) {
boolean found = contains(o);
if (!found) {
backingList.add(o);
}
return found;
}
/**
* Removes the given element from this set if it is present.
*
* @param o object to be removed from this set, if present.
* @return true if the set contained the specified element.
*/
public boolean remove(Object o) {
boolean found = contains(o);
if (found) {
backingList.remove(o);
}
return found;
}
/**
* Removes all of the elements from this set.
*/
public void clear() {
backingList.clear();
}
/**
* Returns a shallow copy of this ArraySet instance: the elements
* themselves are not cloned.
*
* @return a shallow copy of this set.
*/
public Object clone() {
try {
ArraySet newSet = (ArraySet) super.clone();
newSet.backingList = (ArrayList