From 8e050fa4bbc40fe35bad9e664f31bf9d7dfc6459 Mon Sep 17 00:00:00 2001 From: ray Date: Thu, 25 Aug 2005 17:49:32 +0000 Subject: [PATCH] Added "Interator" and plugged in some quick usage. I'll add more soon. git-svn-id: https://samskivert.googlecode.com/svn/trunk@1700 6335cc39-0255-0410-8fd6-9bcaacd3b74c --- .../java/com/samskivert/util/ArrayIntSet.java | 20 ++++++++--- .../src/java/com/samskivert/util/IntSet.java | 5 +++ .../java/com/samskivert/util/Interator.java | 33 +++++++++++++++++++ 3 files changed, 53 insertions(+), 5 deletions(-) create mode 100644 projects/samskivert/src/java/com/samskivert/util/Interator.java diff --git a/projects/samskivert/src/java/com/samskivert/util/ArrayIntSet.java b/projects/samskivert/src/java/com/samskivert/util/ArrayIntSet.java index 28c34b87..279e24d8 100644 --- a/projects/samskivert/src/java/com/samskivert/util/ArrayIntSet.java +++ b/projects/samskivert/src/java/com/samskivert/util/ArrayIntSet.java @@ -96,22 +96,26 @@ public class ArrayIntSet extends AbstractSet return (binarySearch(value) >= 0); } - // documentation inherited from interface - public Iterator iterator () + // documentation inherited from interface IntSet + public Interator interator () { - return new Iterator() { + return new Interator() { public boolean hasNext () { return (_pos < _size); } - public Object next () { + public int nextInt () { if (_pos == _size) { throw new NoSuchElementException(); } else { - return new Integer(_values[_pos++]); + return _values[_pos++]; } } + public Object next () { + return new Integer(nextInt()); + } + public void remove () { throw new UnsupportedOperationException(); } @@ -120,6 +124,12 @@ public class ArrayIntSet extends AbstractSet }; } + // documentation inherited from interface + public Iterator iterator () + { + return interator(); + } + // documentation inherited from interface public Object[] toArray () { diff --git a/projects/samskivert/src/java/com/samskivert/util/IntSet.java b/projects/samskivert/src/java/com/samskivert/util/IntSet.java index 10edec39..677278db 100644 --- a/projects/samskivert/src/java/com/samskivert/util/IntSet.java +++ b/projects/samskivert/src/java/com/samskivert/util/IntSet.java @@ -74,6 +74,11 @@ public interface IntSet extends Set */ public boolean remove (int value); + /** + * Return an Interator that iterates over the ints in this set. + */ + public Interator interator (); + /** * Returns an array containing all of the elements in this set. Obeys * the general contract of the Collection.toArray method. diff --git a/projects/samskivert/src/java/com/samskivert/util/Interator.java b/projects/samskivert/src/java/com/samskivert/util/Interator.java new file mode 100644 index 00000000..0f075330 --- /dev/null +++ b/projects/samskivert/src/java/com/samskivert/util/Interator.java @@ -0,0 +1,33 @@ +// +// samskivert library - useful routines for java programs +// Copyright (C) 2001-2004 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.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(). + */ +public interface Interator extends Iterator +{ + /** + * @return the next int value from this Iterator. + */ + public int nextInt (); +}