All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.yestech.lib.lang.ArraySet Maven / Gradle / Ivy

Go to download

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.

There is a newer version: 1.2.0
Show newest version
/*
 * 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) backingList.clone();
            return newSet;
        } catch (CloneNotSupportedException e) {
            throw new InternalError();
        }
    }

    /**
     * Save the state of this ArraySet instance to a stream (that is,
     * serialize this set).
     *
     * @serialData The size of the set (the number of elements it contains)
     *		   (int), followed by all of its elements (each an Object) in
     *             no particular order.
     */
    private synchronized void writeObject(java.io.ObjectOutputStream s)
            throws java.io.IOException {
        // Write out any hidden serialization magic
        s.defaultWriteObject();

        // Write out size
        s.writeInt(backingList.size());

        // Write out all elements in the proper order.
        for (Iterator i = backingList.listIterator(); i.hasNext();)
            s.writeObject(i.next());
    }

    /**
     * Reconstitute the ArraySet instance from a stream (that is,
     * deserialize it).
     */
    private synchronized void readObject(java.io.ObjectInputStream s)
            throws java.io.IOException, ClassNotFoundException {
        // Read in any hidden serialization magic
        s.defaultReadObject();

        // Read in size
        int size = s.readInt();
        backingList = new ArrayList(size);

        // Read in all elements in the proper order.
        for (int i = 0; i < size; i++) {
            E e = (E)s.readObject();
            backingList.add(e);
        }
    }

}