package positronic.util;
/**
* Description: Works just like java.util.ArrayList, except it will refuse to
* add an element if it is equal to an element already appearing in the
* ArrayListSet. In other words, it's an ArrayList which maintains uniqueness
* like java.util.Set does.
* It also provides a sort function, if all of the members are mutually
* Comparable.
*
* @author Kerry Michael Soileau
*
* ksoileau@yahoo.com
* http://kerrysoileau.com/index.html
*
* @version 1.4
* @version 1.3 void sort() method added March 4, 2009.
*/
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.Set;
public class ArrayListSet extends ArrayList implements Set
{
private static final long serialVersionUID = -2554176229274064937L;
//Simple demo --- may be deleted at will.
public static void main(String[] args)
{
ArrayListSet vectorSet1 = new ArrayListSet();
vectorSet1.addE("Leda");
vectorSet1.addE("Tyndareus");
vectorSet1.addE("Helen");
vectorSet1.addE("");
vectorSet1.addE("Thyestes");
vectorSet1.addE("Tyndareus");
vectorSet1.addE("Tantalus");
vectorSet1.addE("");
vectorSet1.addE("Leda");
vectorSet1.addE("Tyndareus");
//vectorSet1.add(new Integer(32));
vectorSet1.addE("Agamemnon");
vectorSet1.addE("Aerope");
vectorSet1.addE("Leda");
//vectorSet1.add(new Integer(33));
vectorSet1.addE("Thyestes");
for(int i=0;i c)
{
add(c);
}
/**
* Constructs a new set containing space for the specified number of objects.
*
* @param n The capacity of the new set.
*/
public ArrayListSet(int n)
{
super(n);
}
/**
* Adds all of the elements in the specified collection to this set.
*
* @param c elements to be added
* @return true if this set changed as a result of the call.
*/
public boolean add(Collection c)
{
boolean changed=false;
if (c.size() > 0)
{
Iterator it=c.iterator();
while(it.hasNext())
{
E nextobj=it.next();
boolean res=this.addE(nextobj);
if(res) changed=true;
}
}
return changed;
}
/**
* 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.
*/
@SuppressWarnings("unchecked")
public boolean add(Integer o)
{
for(int i=0;itrue if the set did not already contain the specified
* element.
*/
public boolean addE(E o)
{
for(int i=0;i clone()
{
ArrayListSet ret=new ArrayListSet();
Object[] a=super.toArray();
for(Object b : a)
ret.addE((E)b);
return ret;
}
@SuppressWarnings("unchecked")
private Comparable[] extracted()
{
return super.toArray(new Comparable[0]);
}
@SuppressWarnings("unchecked")
private E extracted(Comparable[] varray, int i)
{
return (E) varray[i];
}
@Override
@SuppressWarnings("unchecked")
public E set(int i, Object object) {
return super.set(i, (E) object);
}
/**
* If all of the members of this are Comparable with one another, this
* method will reorder them into increasing order.
*
*/
public void sort()
{
Comparable[] varray=extracted();
if(varray==null || varray.length==0 || !(varray[0] instanceof Comparable))
return;
Arrays.sort(varray);
super.clear();
for(int i=0;i