com.samskivert.util.AbstractIntSet Maven / Gradle / Ivy
Show all versions of samskivert Show documentation
//
// $Id: AbstractIntSet.java 2974 2011-01-01 05:09:33Z [email protected] $
//
// samskivert library - useful routines for java programs
// Copyright (C) 2001-2011 Michael Bayne, et al.
//
// 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 com.samskivert.util;
import java.util.AbstractSet;
import java.util.Collection;
import java.util.Iterator;
/**
* A base class for {@link IntSet} implementations.
*
* All you really need to do is implement interable, but you'll almost certainly want
* to implement size and contains for enhanced performance.
*
* To implement a modifiable IntSet, the programmer must additionally override this class's
* add and remove methods, which will otherwise throw an
* UnsupportedOperationException.
*/
public abstract class AbstractIntSet extends AbstractSet
implements IntSet
{
/**
* Add all of the values in the supplied array to the set.
*
* @param values elements to be added to this set.
*
* @return true if this set did not already contain all of the specified elements.
*/
public boolean add (int[] values)
{
boolean modified = false;
int vlength = values.length;
for (int i = 0; i < vlength; i++) {
modified = (add(values[i]) || modified);
}
return modified;
}
/**
* Removes all values in the supplied array from the set. Any values that are in the array but
* not in the set are simply ignored.
*
* @param values elements to be removed from the set.
*
* @return true if this set contained any of the specified elements (which will have
* been removed).
*/
public boolean remove (int[] values)
{
boolean modified = false;
int vcount = values.length;
for (int i = 0; i < vcount; i++) {
modified = (remove(values[i]) || modified);
}
return modified;
}
/**
* {@inheritDoc}
*
* This implementation iterates over the ints in the collection, checking each one in turn
* to see if it's the specified value.
*/
// from IntSet
public boolean contains (int value)
{
// dumb implementation. You should override.
for (Interator it = interator(); it.hasNext(); ) {
if (it.nextInt() == value) {
return true;
}
}
return false;
}
/**
* {@inheritDoc}
*
*
This implementation simply counts the elements in the interator.
*/
@Override
public int size ()
{
// dumb implementation. You should override.
int size = 0;
for (Interator it = interator(); (size < Integer.MAX_VALUE) && it.hasNext(); it.nextInt()) {
size++;
}
return size;
}
/**
* {@inheritDoc}
*
*
This implementation simply checks to see if the interator has a first element.
*/
@Override
public boolean isEmpty ()
{
// possibly dumb implementation. Override if you can do better.
return !interator().hasNext();
}
// from IntSet
public boolean add (int value)
{
throw new UnsupportedOperationException();
}
// from IntSet
public boolean remove (int value)
{
throw new UnsupportedOperationException();
}
/**
* {@inheritDoc}
*
*
This implementation returns an array containing all the elements returned by the
* interator.
*/
// from IntSet
public int[] toIntArray ()
{
int[] vals = new int[size()];
int ii = 0;
for (Interator it = interator(); (ii < Integer.MAX_VALUE) && it.hasNext(); ) {
vals[ii++] = it.nextInt();
}
return vals;
}
@Override // from AbstractSet
public Iterator iterator ()
{
return interator();
}
@Override // from AbstractSet
public boolean contains (Object o)
{
// cope with null or non-Integer
return (o instanceof Integer) && contains(((Integer)o).intValue());
}
@Override // from AbstractSet
public boolean add (Integer i)
{
return add(i.intValue()); // will NPE
}
@Override // from AbstractSet
public boolean remove (Object o)
{
// cope with null or non-Integer
return (o instanceof Integer) && remove(((Integer)o).intValue());
}
@Override // from AbstractSet
public boolean equals (Object o)
{
if (o == this) {
return true;
}
if (o instanceof IntSet) {
IntSet that = (IntSet)o;
return (this.size() == that.size()) && this.containsAll(that);
}
return super.equals(o);
}
@Override // from AbstractSet
public int hashCode ()
{
int h = 0;
for (Interator it = interator(); it.hasNext(); ) {
h += it.nextInt();
}
return h;
}
@Override // from AbstractSet
public String toString ()
{
StringBuilder sb = new StringBuilder("[");
Interator it = interator();
if (it.hasNext()) {
sb.append(it.nextInt());
while (it.hasNext()) {
sb.append(", ").append(it.nextInt());
}
}
return sb.append(']').toString();
}
@Override // from AbstractSet
public boolean containsAll (Collection> c)
{
if (c instanceof Interable) {
for (Interator it = ((Interable) c).interator(); it.hasNext(); ) {
if (!contains(it.nextInt())) {
return false;
}
}
return true;
}
return super.containsAll(c);
}
@Override // from AbstractSet
public boolean addAll (Collection extends Integer> c)
{
if (c instanceof Interable) {
boolean modified = false;
for (Interator it = ((Interable) c).interator(); it.hasNext(); ) {
if (add(it.nextInt())) {
modified = true;
}
}
return modified;
}
return super.addAll(c);
}
@Override // from AbstractSet
public boolean removeAll (Collection> c)
{
if (c instanceof Interable) {
boolean modified = false;
for (Interator it = ((Interable)c).interator(); it.hasNext(); ) {
if (remove(it.nextInt())) {
modified = true;
}
}
return modified;
}
return super.removeAll(c);
}
@Override // from AbstractSet
public boolean retainAll (Collection> c)
{
if (c instanceof IntSet) {
IntSet that = (IntSet)c;
boolean modified = false;
for (Interator it = interator(); it.hasNext(); ) {
if (!that.contains(it.nextInt())) {
it.remove();
modified = true;
}
}
return modified;
}
return super.retainAll(c);
}
}