AbstractIntCollection.java :  » Development » PCJ » bak » pcj » Java Open Source

Java Open Source » Development » PCJ 
PCJ » bak » pcj » AbstractIntCollection.java
/*
 *  Primitive Collections for Java.
 *  Copyright (C) 2002, 2003  Sren Bak
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
package bak.pcj;

import bak.pcj.util.Exceptions;

/**
 *  This class represents an abstract base for implementing
 *  collections of int values. All operations that can be implemented
 *  using iterators are implemented as such. In most cases, this is
 *  hardly an efficient solution, and at least some of those
 *  methods should be overridden by sub-classes.
 *
 *  <p>In this implementation, <tt>size()</tt> is calculated by
 *  iterating over the collection. Make sure that <tt>size()</tt>
 *  is overwritten or that iterators do not depend on the
 *  <tt>size()</tt> method.
 *
 *  @author     S&oslash;ren Bak
 *  @version    1.3     21-08-2003 20:16
 *  @since      1.0
 */
public abstract class AbstractIntCollection implements IntCollection {

    /** Default constructor to be invoked by sub-classes. */
    protected AbstractIntCollection() { }

    /**
     *  Throws <tt>UnsupportedOperationException</tt>.
     *
     *  @throws     UnsupportedOperationException
     *              unconditionally.
     */
    public boolean add(int v)
    { Exceptions.unsupported("add"); return false; }

    public boolean addAll(IntCollection c) {
        IntIterator i = c.iterator();  //  Throws NullPointerException
        boolean result = false;
        while (i.hasNext())
            result = result | add(i.next());
        return result;
    }

    public void clear() {
        IntIterator i = iterator();
        while (i.hasNext()) {
            i.next();
            i.remove();
        }
    }

    public boolean contains(int v) {
        IntIterator i = iterator();
        while (i.hasNext())
            if (i.next() == v)
                return true;
        return false;
    }

    public boolean containsAll(IntCollection c) {
        IntIterator i = c.iterator();  //  Throws NullPointerException
        while (i.hasNext())
            if (!contains(i.next()))
                return false;
        return true;
    }

    public boolean isEmpty()
    { return size() == 0; }

    public boolean remove(int v) {
        IntIterator i = iterator();
        boolean result = false;
        while (i.hasNext()) {
            if (i.next() == v) {
                i.remove();
                result = true;
                break;
            }
        }
        return result;
    }

    public boolean removeAll(IntCollection c) {
        if (c == null)
            Exceptions.nullArgument("collection");
        IntIterator i = iterator();
        boolean result = false;
        while (i.hasNext()) {
            if (c.contains(i.next())) {
                i.remove();
                result = true;
            }
        }
        return result;
    }

    public boolean retainAll(IntCollection c) {
        if (c == null)
            Exceptions.nullArgument("collection");
        IntIterator i = iterator();
        boolean result = false;
        while (i.hasNext()) {
            if (!c.contains(i.next())) {
                i.remove();
                result = true;
            }
        }
        return result;
    }

    public int size() {
        IntIterator i = iterator();
        int size = 0;
        while (i.hasNext()) {
            i.next();
            size++;
        }
        return size;
    }

    public int[] toArray() {
        return toArray(null);
    }

    public int[] toArray(int[] a) {
        int size = size();
        if (a == null || a.length < size)
            a = new int[size];
        IntIterator i = iterator();
        int index = 0;
        while (i.hasNext()) {
            a[index] = i.next();
            index++;
        }
        return a;
    }

    /**
     *  Does nothing. Sub-classes may provide an implementation to
     *  minimize memory usage, but this is not required since many
     *  implementations will always have minimal memory usage.
     */
    public void trimToSize()
    { }

    /**
     *  Returns a string representation of this collection.
     *
     *  @return     a string representation of this collection.
     */
    public String toString() {
        StringBuffer s = new StringBuffer();
        s.append('[');
        IntIterator i = iterator();
        while (i.hasNext()) {
            if (s.length() > 1)
                s.append(',');
            s.append(bak.pcj.util.Display.display(i.next()));
        }
        s.append(']');
        return s.toString();
    }

}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.