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

com.squeakysand.commons.util.OrderedSet Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2010-2012 Craig S. Dickson (http://craigsdickson.com)
 *
 * 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 com.squeakysand.commons.util;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.Set;

/**
 * An ordered set implements both the list and set patterns. In a normal set, the elements are unique but there is no
 * defined order to the elements, so random access is not possible. In the OrderedSet the elements are both unique and
 * ordered.
 * @param 
 */
public class OrderedSet implements List, Set {

    private List contents;

    /**
     * Creates a new OrderedSet object.
     */
    public OrderedSet() {
        contents = new ArrayList();
    }

    /** {@inheritDoc} */
    @Override
    public boolean add(E e) {
        int size = contents.size();
        add(contents.size(), e);
        return size != contents.size();
    }

    /** {@inheritDoc} */
    @Override
    public void add(int index, E element) {
        if (!contents.contains(element)) {
            contents.add(index, element);
        }
    }

    /** {@inheritDoc} */
    @Override
    public boolean addAll(Collection c) {
        return addAll(contents.size(), c);
    }

    /** {@inheritDoc} */
    @Override
    public boolean addAll(int index, Collection c) {
        int originalSize = contents.size();
        int position = index;
        for (E e : c) {
            int sizeBefore = contents.size();
            add(position, e);
            int sizeAfter = contents.size();
            if (sizeBefore != sizeAfter) {
                // if the size changed it means the item was added so we need increment
                // the index
                position++;
            }
        }
        int currentSize = contents.size();
        // if the overall size changed then we return true, otherwise false
        return originalSize != currentSize;
    }

    /** {@inheritDoc} */
    @Override
    public void clear() {
        contents.clear();
    }

    /** {@inheritDoc} */
    @Override
    public boolean contains(Object o) {
        return contents.contains(o);
    }

    /** {@inheritDoc} */
    @Override
    public boolean containsAll(Collection c) {
        return contents.containsAll(c);
    }

    /** {@inheritDoc} */
    @Override
    public E get(int index) {
        return contents.get(index);
    }

    /** {@inheritDoc} */
    @Override
    public int indexOf(Object o) {
        return contents.indexOf(o);
    }

    /** {@inheritDoc} */
    @Override
    public boolean isEmpty() {
        return contents.isEmpty();
    }

    /** {@inheritDoc} */
    @Override
    public Iterator iterator() {
        return contents.iterator();
    }

    /** {@inheritDoc} */
    @Override
    public int lastIndexOf(Object o) {
        return contents.lastIndexOf(o);
    }

    /** {@inheritDoc} */
    @Override
    public ListIterator listIterator() {
        return contents.listIterator();
    }

    /** {@inheritDoc} */
    @Override
    public ListIterator listIterator(int index) {
        return contents.listIterator(index);
    }

    /** {@inheritDoc} */
    @Override
    public E remove(int index) {
        return contents.remove(index);
    }

    /** {@inheritDoc} */
    @Override
    public boolean remove(Object o) {
        return contents.remove(o);
    }

    /** {@inheritDoc} */
    @Override
    public boolean removeAll(Collection c) {
        return contents.removeAll(c);
    }

    /** {@inheritDoc} */
    @Override
    public boolean retainAll(Collection c) {
        return contents.retainAll(c);
    }

    /** {@inheritDoc} */
    @Override
    public E set(int index, E element) {
        E result = null;
        if (!contents.contains(element)) {
            result = contents.set(index, element);
        }
        return result;
    }

    /** {@inheritDoc} */
    @Override
    public int size() {
        return contents.size();
    }

    /** {@inheritDoc} */
    @Override
    public List subList(int fromIndex, int toIndex) {
        return contents.subList(fromIndex, toIndex);
    }

    /** {@inheritDoc} */
    @Override
    public Object[] toArray() {
        return contents.toArray();
    }

    /** {@inheritDoc} */
    @Override
    public  T[] toArray(T[] a) {
        return contents.toArray(a);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy