📄 iterators.java
字号:
/* * LingPipe v. 3.5 * Copyright (C) 2003-2008 Alias-i * * This program is licensed under the Alias-i Royalty Free License * Version 1 WITHOUT ANY WARRANTY, without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Alias-i * Royalty Free License Version 1 for more details. * * You should have received a copy of the Alias-i Royalty Free License * Version 1 along with this program; if not, visit * http://alias-i.com/lingpipe/licenses/lingpipe-license-1.txt or contact * Alias-i, Inc. at 181 North 11th Street, Suite 401, Brooklyn, NY 11211, * +1 (718) 290-9170. */package com.aliasi.util;import java.util.ArrayList;import java.util.Iterator;import java.util.NoSuchElementException;/** * Static utility methods for iteration. * * @author Bob Carpenter * @version 3.0 * @since LingPipe2.0 */public class Iterators { // do not allow instances private Iterators() { /* no instances */ } /** * An <code>Iterator.Array</code> iterates over the elements of an * array specified at construction time. The array is not copied, * so any changes in the underlying array are reflected in the * iterator. * * <P><I>Implementation Note:</I> this class does not automatically * free references in the underlying array, because the array * may be used elswhere. If reference freeing is critical here, * a call to <code>remove()</code> after ever <code>next()</code> * will free the references in the array. * * @author Bob Carpenter * @version 3.0 * @since LingPipe1.0 */ public static class Array<E> implements Iterator<E> { /** * Array to iterate over. */ private final E[] mMembers; /** * Current position of next element to return in array. */ private int mPosition = 0; /** * Construct an array iterator from the specified array. * * @param members Array basis of the constructed iterator. */ public Array(E[] members) { mMembers = members; } /** * Returns <code>true</code> if this iterator has more * elements. * * @return <code>true</code> if this iterator has more * elements. */ public boolean hasNext() { return mPosition < mMembers.length; } /** * Returns the next element in the array. * * @return Next element in the array. * @throws NoSuchElementException If there are no more * elements left in the array to return. */ public E next() { if (!hasNext()) throw new NoSuchElementException(); return mMembers[mPosition++]; } /** * Sets position in underlying array corresponding to the most * recently returned token to <code>null</code>. * * @throws IllegalStateException If the <code>next</code> * method has not been called, or the <code>remove</code> * method has already been called after the last call to the * next method. */ public void remove() { if (mPosition < 1) throw new IllegalStateException("Next not yet called."); if (mMembers[mPosition-1] == null) throw new IllegalStateException("Remove already called."); mMembers[mPosition-1] = null; } } /** * An <code>Iterators.ArraySlice</code> iterates over a slice of * an array specified by start index and length. * * @author Bob Carpenter * @version 3.0 * @since LingPipe1.0 */ public static class ArraySlice<E> implements Iterator<E> { /** * The underlying objects represented in this array iterator. */ private final E[] mObjects; /** * Index of next element to return. */ private int mNext; /** * Index one past the index of the last element to return. */ private final int mLast; /** * Construct an iterator over the specified array of objects * that begins at the object at the specified start index * and carries through to the other objects. * * @param objects Array of objects over which to iterate. * @param start Index of first object to return. */ public ArraySlice(E[] objects, int start, int length) { mObjects = objects; mNext = start; mLast = start + length; } /** * Returns <code>true</code> if there are more objects * to return. * * @return <code>true</code> if this iterator has more * elements. */ public boolean hasNext() { return mNext < mLast; } /** * Returns the next object for this iterator, throwing * an exception if there are no more objects left. * * @return Next object from this iterator. * @throws NoSuchElementException If there are no more * elements to return. */ public E next() { if (!hasNext()) throw new NoSuchElementException(); return mObjects[mNext++]; } /** * Throws an <code>UnsupportedOperationException</code>. * * @throws UnsupportedOperationException If called. */ public void remove() { throw new UnsupportedOperationException(); } } /** * An <code>Iterator.Empty</code> is an iterator with no * objects. The method {@link #hasNext()} always returns * <code>false</code>, while the methods {@link #next()} * and {@link #remove()} always throw exceptions. * * @author Bob Carpenter * @version 3.0 * @since LingPipe3.0 */ public static class Empty<E> implements Iterator<E> { public Empty() { /* do nothing */ } /** * Always returns <code>false</code>. * * @return <code>false</code>. */ public boolean hasNext() { return false; } /** * Calling this method throws a no-such-element exception. * * @return Always throws an exception. * @throws NoSuchElementException Always. */ public E next() { String msg = "No elements in empty iterator."; throw new NoSuchElementException(msg); } /** * Calling this method throws an illegal state exception. * * @throws IllegalStateException Always. */ public void remove() { String msg = "No elements to remove in empty iterator."; throw new IllegalStateException(msg); } } /** * An <code>Iterator.Singleton</code> is an iterator over a * singleton object. * * @author Bob Carpenter * @version 3.0 * @since LingPipe2.0 */ public static class Singleton<E> implements Iterator<E> { /** * The single object to return. */ private E mMember; /** * <code>true</code> if the single element has not already * been returned. */ private boolean mHasNext = true; /** * Construct a singleton iterator that returns the specified * object. * * @param member Single member over which to iterate. */ public Singleton(E member) { mMember = member; } /** * Returns <code>true</code> if the single member has not * already been returned. * * @return <code>true</code> if the single member has not * already been returned. */ public boolean hasNext() { return mHasNext; } /** * Returns the singleton member if it has not yet been * returned, otherwise throw an exception. * * @return Singleton member if it has not yet been returned. * @throws NoSuchElementException If the singleton member has * already been returned and no elements remain. */ public E next() { if (!mHasNext) throw new NoSuchElementException(); mHasNext = false; E result = mMember; mMember = null; return result; } /** * Throws an unsupported operation exception. * * @throws UnsupportedOperationException Whenver called. */ public void remove() { String msg = "This iterator does not support remove()." + " class=" + this.getClass(); throw new UnsupportedOperationException(msg); } } /** * An <code>Iterators.Pair</code> provides an iterator * over a sequence of exactly two elements. * * @author Bob Carpenter * @version 3.0 * @since LingPipe2.0 */ public static class Pair<E> implements Iterator<E> { /** * Number of members returned so far. */ private int mMembersReturned = 0; private E mMember1; private E mMember2; /** * Construct a pair iterator based on the containing * pair set. */ public Pair(E member1, E member2) { mMember1 = member1; mMember2 = member2; } /** * Returns <code>true</code> if there are more elements * left to return. * * @return <code>true</code> if there are more elements * left to return. */ public boolean hasNext() { return mMembersReturned < 2; } /** * Returns the next object in this pair iterator, * or throws an exception if there aren't any more. * * @return Next object in this pair iterator. */ public E next() { if (mMembersReturned == 0) { ++mMembersReturned; E result1 = mMember1; mMember1 = null; return result1; } if (mMembersReturned == 1) { ++mMembersReturned; E result2 = mMember2; mMember2 = null; return result2; } throw new NoSuchElementException(); } /** * Throws an unsupported operation exception. * * @throws UnsupportedOperationException Whenver called. */ public void remove() { String msg = "This iterator does not support remove()." + " class=" + this.getClass(); throw new UnsupportedOperationException(msg); } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -