diff --git a/src/java/com/samskivert/util/AbstractIntSet.java b/src/java/com/samskivert/util/AbstractIntSet.java new file mode 100644 index 00000000..d3850325 --- /dev/null +++ b/src/java/com/samskivert/util/AbstractIntSet.java @@ -0,0 +1,61 @@ +// +// $Id$ +// +// samskivert library - useful routines for java programs +// Copyright (C) 2001-2008 Michael Bayne +// +// 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.Iterator; + +/** + * A base class for {@link IntSet} implementations. + */ +public abstract class AbstractIntSet extends AbstractSet + implements IntSet +{ + @Override // from AbstractSet + public Iterator iterator () + { + return interator(); + } + + @Override // from AbstractSet + public boolean add (int t) + { + throw new UnsupportedOperationException(); + } + + /** + * Creates an iterator that provides access to our int values without unboxing. + */ + public abstract Interator interator (); + + /** + * Converts the contents of this set to an int array. + */ + public int[] toIntArray () + { + int[] vals = new int[size()]; + int ii=0; + for (Interator intr = interator(); intr.hasNext(); ) { + vals[ii++] = intr.nextInt(); + } + return vals; + } +} diff --git a/src/java/com/samskivert/util/HashIntMap.java b/src/java/com/samskivert/util/HashIntMap.java index 27d9b7bc..c34014fa 100644 --- a/src/java/com/samskivert/util/HashIntMap.java +++ b/src/java/com/samskivert/util/HashIntMap.java @@ -391,72 +391,50 @@ public class HashIntMap extends AbstractMap } } - protected class IntKeySet extends AbstractSet - implements IntSet - { - @Override public Iterator iterator () { - return interator(); - } - - public Interator interator () { - return new Interator () { - public boolean hasNext () { - return i.hasNext(); - } - public Integer next () { - return i.next().getKey(); - } - public int nextInt () { - return i.next().getIntKey(); - } - public void remove () { - i.remove(); - } - private Iterator> i = intEntrySet().iterator(); - }; - } - - @Override public int size () { - return HashIntMap.this.size(); - } - - @Override public boolean contains (Object t) { - return HashIntMap.this.containsKey(t); - } - - public boolean contains (int t) { - return HashIntMap.this.containsKey(t); - } - - public boolean add (int t) { - throw new UnsupportedOperationException(); - } - - @Override public boolean remove (Object o) { - return (null != HashIntMap.this.remove(o)); - } - - public boolean remove (int value) { - return (null != HashIntMap.this.remove(value)); - } - - public int[] toIntArray () { - int[] vals = new int[size()]; - int ii=0; - for (Interator intr = interator(); intr.hasNext(); ) { - vals[ii++] = intr.nextInt(); - } - return vals; - } - } - // documentation inherited from interface IntMap public IntSet intKeySet () { - // damn Sun bastards made the 'keySet' variable with default access, - // so we can't share it + // damn Sun bastards made the 'keySet' variable with default access, so we can't share it if (_keySet == null) { - _keySet = new IntKeySet(); + _keySet = new AbstractIntSet() { + public Interator interator () { + return new Interator () { + public boolean hasNext () { + return i.hasNext(); + } + public Integer next () { + return i.next().getKey(); + } + public int nextInt () { + return i.next().getIntKey(); + } + public void remove () { + i.remove(); + } + private Iterator> i = intEntrySet().iterator(); + }; + } + + @Override public int size () { + return HashIntMap.this.size(); + } + + @Override public boolean contains (Object t) { + return HashIntMap.this.containsKey(t); + } + + public boolean contains (int t) { + return HashIntMap.this.containsKey(t); + } + + @Override public boolean remove (Object o) { + return (null != HashIntMap.this.remove(o)); + } + + public boolean remove (int value) { + return (null != HashIntMap.this.remove(value)); + } + }; } return _keySet; } diff --git a/src/java/com/samskivert/util/IntIntMap.java b/src/java/com/samskivert/util/IntIntMap.java index 6593854c..bf581e41 100644 --- a/src/java/com/samskivert/util/IntIntMap.java +++ b/src/java/com/samskivert/util/IntIntMap.java @@ -309,6 +309,44 @@ public class IntIntMap return new KeyValueInterator(true, new IntEntryIterator()); } + public IntSet keySet () + { + return new AbstractIntSet() { + public Interator interator () { + return IntIntMap.this.keys(); + } + + @Override public int size () { + return IntIntMap.this.size(); + } + + @Override public boolean contains (Object t) { + return (t instanceof Integer) ? IntIntMap.this.containsKey((Integer)t) : false; + } + + public boolean contains (int t) { + return IntIntMap.this.containsKey(t); + } + + @Override public boolean remove (Object o) { + if (!(o instanceof Integer)) { + return false; + } + return remove((Integer)o); + } + + public boolean remove (int value) { + // we have to check for presence in the map separately because we have no "not in + // the set" return value + if (!IntIntMap.this.containsKey(value)) { + return false; + } + IntIntMap.this.remove(value); + return true; + } + }; + } + public Interator values () { return new KeyValueInterator(false, new IntEntryIterator()); diff --git a/src/java/com/samskivert/util/Interator.java b/src/java/com/samskivert/util/Interator.java index b2c9b524..226f1a35 100644 --- a/src/java/com/samskivert/util/Interator.java +++ b/src/java/com/samskivert/util/Interator.java @@ -23,8 +23,8 @@ package com.samskivert.util; import java.util.Iterator; /** - * Can be used as an Iterator, and all Objects returned should be Integer - * objects, but can also can avoid object creation by calling nextInt(). + * Can be used as an Iterator, and all Objects returned should be Integer objects, but can also can + * avoid boxing by calling {@link #nextInt}. */ public interface Interator extends Iterator {