/*
* Copyright (C) 2002-2019 Sebastiano Vigna
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package it.unimi.dsi.fastutil.chars;
import static it.unimi.dsi.fastutil.BigArrays.grow;
import static it.unimi.dsi.fastutil.BigArrays.length;
import static it.unimi.dsi.fastutil.BigArrays.set;
import static it.unimi.dsi.fastutil.BigArrays.trim;
import java.util.Iterator;
import java.util.ListIterator;
import java.util.NoSuchElementException;
import java.util.Objects;
A class providing static methods and objects that do useful things with
type-specific iterators.
See Also: - Iterator
/**
* A class providing static methods and objects that do useful things with
* type-specific iterators.
*
* @see Iterator
*/
public final class CharIterators {
private CharIterators() {
}
A class returning no elements and a type-specific iterator interface.
This class may be useful to implement your own in case you subclass a
type-specific iterator.
/**
* A class returning no elements and a type-specific iterator interface.
*
* <p>
* This class may be useful to implement your own in case you subclass a
* type-specific iterator.
*/
public static class EmptyIterator implements CharListIterator, java.io.Serializable, Cloneable {
private static final long serialVersionUID = -7046029254386353129L;
protected EmptyIterator() {
}
@Override
public boolean hasNext() {
return false;
}
@Override
public boolean hasPrevious() {
return false;
}
@Override
public char nextChar() {
throw new NoSuchElementException();
}
@Override
public char previousChar() {
throw new NoSuchElementException();
}
@Override
public int nextIndex() {
return 0;
}
@Override
public int previousIndex() {
return -1;
}
@Override
public int skip(int n) {
return 0;
};
@Override
public int back(int n) {
return 0;
};
@Override
public Object clone() {
return EMPTY_ITERATOR;
}
private Object readResolve() {
return EMPTY_ITERATOR;
}
}
An empty iterator. It is serializable and cloneable.
The class of this objects represent an abstract empty iterator that can
iterate as a type-specific (list) iterator.
/**
* An empty iterator. It is serializable and cloneable.
*
* <p>
* The class of this objects represent an abstract empty iterator that can
* iterate as a type-specific (list) iterator.
*/
public static final EmptyIterator EMPTY_ITERATOR = new EmptyIterator();
An iterator returning a single element. /** An iterator returning a single element. */
private static class SingletonIterator implements CharListIterator {
private final char element;
private int curr;
public SingletonIterator(final char element) {
this.element = element;
}
@Override
public boolean hasNext() {
return curr == 0;
}
@Override
public boolean hasPrevious() {
return curr == 1;
}
@Override
public char nextChar() {
if (!hasNext())
throw new NoSuchElementException();
curr = 1;
return element;
}
@Override
public char previousChar() {
if (!hasPrevious())
throw new NoSuchElementException();
curr = 0;
return element;
}
@Override
public int nextIndex() {
return curr;
}
@Override
public int previousIndex() {
return curr - 1;
}
}
Returns an immutable iterator that iterates just over the given element.
Params: - element –
the only element to be returned by a type-specific list iterator.
Returns: an immutable iterator that iterates just over element
.
/**
* Returns an immutable iterator that iterates just over the given element.
*
* @param element
* the only element to be returned by a type-specific list iterator.
* @return an immutable iterator that iterates just over {@code element}.
*/
public static CharListIterator singleton(final char element) {
return new SingletonIterator(element);
}
A class to wrap arrays in iterators. /** A class to wrap arrays in iterators. */
private static class ArrayIterator implements CharListIterator {
private final char[] array;
private final int offset, length;
private int curr;
public ArrayIterator(final char[] array, final int offset, final int length) {
this.array = array;
this.offset = offset;
this.length = length;
}
@Override
public boolean hasNext() {
return curr < length;
}
@Override
public boolean hasPrevious() {
return curr > 0;
}
@Override
public char nextChar() {
if (!hasNext())
throw new NoSuchElementException();
return array[offset + curr++];
}
@Override
public char previousChar() {
if (!hasPrevious())
throw new NoSuchElementException();
return array[offset + --curr];
}
@Override
public int skip(int n) {
if (n <= length - curr) {
curr += n;
return n;
}
n = length - curr;
curr = length;
return n;
}
@Override
public int back(int n) {
if (n <= curr) {
curr -= n;
return n;
}
n = curr;
curr = 0;
return n;
}
@Override
public int nextIndex() {
return curr;
}
@Override
public int previousIndex() {
return curr - 1;
}
}
Wraps the given part of an array into a type-specific list iterator.
The type-specific list iterator returned by this method will iterate length
times, returning consecutive elements of the given array starting from the one with index offset
.
Params: - array –
an array to wrap into a type-specific list iterator.
- offset –
the first element of the array to be returned.
- length –
the number of elements to return.
Returns: an iterator that will return length
elements of array
starting at position offset
.
/**
* Wraps the given part of an array into a type-specific list iterator.
*
* <p>
* The type-specific list iterator returned by this method will iterate
* {@code length} times, returning consecutive elements of the given array
* starting from the one with index {@code offset}.
*
* @param array
* an array to wrap into a type-specific list iterator.
* @param offset
* the first element of the array to be returned.
* @param length
* the number of elements to return.
* @return an iterator that will return {@code length} elements of {@code array}
* starting at position {@code offset}.
*/
public static CharListIterator wrap(final char[] array, final int offset, final int length) {
CharArrays.ensureOffsetLength(array, offset, length);
return new ArrayIterator(array, offset, length);
}
Wraps the given array into a type-specific list iterator.
The type-specific list iterator returned by this method will return all
elements of the given array.
Params: - array –
an array to wrap into a type-specific list iterator.
Returns: an iterator that will the elements of array
.
/**
* Wraps the given array into a type-specific list iterator.
*
* <p>
* The type-specific list iterator returned by this method will return all
* elements of the given array.
*
* @param array
* an array to wrap into a type-specific list iterator.
* @return an iterator that will the elements of {@code array}.
*/
public static CharListIterator wrap(final char[] array) {
return new ArrayIterator(array, 0, array.length);
}
Unwraps an iterator into an array starting at a given offset for a given
number of elements.
This method iterates over the given type-specific iterator and stores the elements returned, up to a maximum of length
, in the given array starting at offset
. The number of actually unwrapped elements is returned (it may be less than max
if the iterator emits less than max
elements).
Params: - i –
a type-specific iterator.
- array –
an array to contain the output of the iterator.
- offset –
the first element of the array to be returned.
- max –
the maximum number of elements to unwrap.
Returns: the number of elements unwrapped.
/**
* Unwraps an iterator into an array starting at a given offset for a given
* number of elements.
*
* <p>
* This method iterates over the given type-specific iterator and stores the
* elements returned, up to a maximum of {@code length}, in the given array
* starting at {@code offset}. The number of actually unwrapped elements is
* returned (it may be less than {@code max} if the iterator emits less than
* {@code max} elements).
*
* @param i
* a type-specific iterator.
* @param array
* an array to contain the output of the iterator.
* @param offset
* the first element of the array to be returned.
* @param max
* the maximum number of elements to unwrap.
* @return the number of elements unwrapped.
*/
public static int unwrap(final CharIterator i, final char array[], int offset, final int max) {
if (max < 0)
throw new IllegalArgumentException("The maximum number of elements (" + max + ") is negative");
if (offset < 0 || offset + max > array.length)
throw new IllegalArgumentException();
int j = max;
while (j-- != 0 && i.hasNext())
array[offset++] = i.nextChar();
return max - j - 1;
}
Unwraps an iterator into an array.
This method iterates over the given type-specific iterator and stores the
elements returned in the given array. The iteration will stop when the
iterator has no more elements or when the end of the array has been reached.
Params: - i –
a type-specific iterator.
- array –
an array to contain the output of the iterator.
Returns: the number of elements unwrapped.
/**
* Unwraps an iterator into an array.
*
* <p>
* This method iterates over the given type-specific iterator and stores the
* elements returned in the given array. The iteration will stop when the
* iterator has no more elements or when the end of the array has been reached.
*
* @param i
* a type-specific iterator.
* @param array
* an array to contain the output of the iterator.
* @return the number of elements unwrapped.
*/
public static int unwrap(final CharIterator i, final char array[]) {
return unwrap(i, array, 0, array.length);
}
Unwraps an iterator, returning an array, with a limit on the number of
elements.
This method iterates over the given type-specific iterator and returns an array containing the elements returned by the iterator. At most max
elements will be returned.
Params: - i –
a type-specific iterator.
- max –
the maximum number of elements to be unwrapped.
Returns: an array containing the elements returned by the iterator (at most max
).
/**
* Unwraps an iterator, returning an array, with a limit on the number of
* elements.
*
* <p>
* This method iterates over the given type-specific iterator and returns an
* array containing the elements returned by the iterator. At most {@code max}
* elements will be returned.
*
* @param i
* a type-specific iterator.
* @param max
* the maximum number of elements to be unwrapped.
* @return an array containing the elements returned by the iterator (at most
* {@code max}).
*/
public static char[] unwrap(final CharIterator i, int max) {
if (max < 0)
throw new IllegalArgumentException("The maximum number of elements (" + max + ") is negative");
char array[] = new char[16];
int j = 0;
while (max-- != 0 && i.hasNext()) {
if (j == array.length)
array = CharArrays.grow(array, j + 1);
array[j++] = i.nextChar();
}
return CharArrays.trim(array, j);
}
Unwraps an iterator, returning an array.
This method iterates over the given type-specific iterator and returns an
array containing the elements returned by the iterator.
Params: - i –
a type-specific iterator.
Returns: an array containing the elements returned by the iterator.
/**
* Unwraps an iterator, returning an array.
*
* <p>
* This method iterates over the given type-specific iterator and returns an
* array containing the elements returned by the iterator.
*
* @param i
* a type-specific iterator.
* @return an array containing the elements returned by the iterator.
*/
public static char[] unwrap(final CharIterator i) {
return unwrap(i, Integer.MAX_VALUE);
}
Unwraps an iterator into a big array starting at a given offset for a given
number of elements.
This method iterates over the given type-specific iterator and stores the elements returned, up to a maximum of length
, in the given big array starting at offset
. The number of actually unwrapped elements is returned (it may be less than max
if the iterator emits less than max
elements).
Params: - i –
a type-specific iterator.
- array –
a big array to contain the output of the iterator.
- offset –
the first element of the array to be returned.
- max –
the maximum number of elements to unwrap.
Returns: the number of elements unwrapped.
/**
* Unwraps an iterator into a big array starting at a given offset for a given
* number of elements.
*
* <p>
* This method iterates over the given type-specific iterator and stores the
* elements returned, up to a maximum of {@code length}, in the given big array
* starting at {@code offset}. The number of actually unwrapped elements is
* returned (it may be less than {@code max} if the iterator emits less than
* {@code max} elements).
*
* @param i
* a type-specific iterator.
* @param array
* a big array to contain the output of the iterator.
* @param offset
* the first element of the array to be returned.
* @param max
* the maximum number of elements to unwrap.
* @return the number of elements unwrapped.
*/
public static long unwrap(final CharIterator i, final char array[][], long offset, final long max) {
if (max < 0)
throw new IllegalArgumentException("The maximum number of elements (" + max + ") is negative");
if (offset < 0 || offset + max > length(array))
throw new IllegalArgumentException();
long j = max;
while (j-- != 0 && i.hasNext())
set(array, offset++, i.nextChar());
return max - j - 1;
}
Unwraps an iterator into a big array.
This method iterates over the given type-specific iterator and stores the
elements returned in the given big array. The iteration will stop when the
iterator has no more elements or when the end of the array has been reached.
Params: - i –
a type-specific iterator.
- array –
a big array to contain the output of the iterator.
Returns: the number of elements unwrapped.
/**
* Unwraps an iterator into a big array.
*
* <p>
* This method iterates over the given type-specific iterator and stores the
* elements returned in the given big array. The iteration will stop when the
* iterator has no more elements or when the end of the array has been reached.
*
* @param i
* a type-specific iterator.
* @param array
* a big array to contain the output of the iterator.
* @return the number of elements unwrapped.
*/
public static long unwrap(final CharIterator i, final char array[][]) {
return unwrap(i, array, 0, length(array));
}
Unwraps an iterator into a type-specific collection, with a limit on the
number of elements.
This method iterates over the given type-specific iterator and stores the elements returned, up to a maximum of max
, in the given type-specific collection. The number of actually unwrapped elements is returned (it may be less than max
if the iterator emits less than max
elements).
Params: - i –
a type-specific iterator.
- c –
a type-specific collection array to contain the output of the
iterator.
- max –
the maximum number of elements to unwrap.
Returns: the number of elements unwrapped. Note that this is the number of
elements returned by the iterator, which is not necessarily the
number of elements that have been added to the collection (because of
duplicates).
/**
* Unwraps an iterator into a type-specific collection, with a limit on the
* number of elements.
*
* <p>
* This method iterates over the given type-specific iterator and stores the
* elements returned, up to a maximum of {@code max}, in the given type-specific
* collection. The number of actually unwrapped elements is returned (it may be
* less than {@code max} if the iterator emits less than {@code max} elements).
*
* @param i
* a type-specific iterator.
* @param c
* a type-specific collection array to contain the output of the
* iterator.
* @param max
* the maximum number of elements to unwrap.
* @return the number of elements unwrapped. Note that this is the number of
* elements returned by the iterator, which is not necessarily the
* number of elements that have been added to the collection (because of
* duplicates).
*/
public static int unwrap(final CharIterator i, final CharCollection c, final int max) {
if (max < 0)
throw new IllegalArgumentException("The maximum number of elements (" + max + ") is negative");
int j = max;
while (j-- != 0 && i.hasNext())
c.add(i.nextChar());
return max - j - 1;
}
Unwraps an iterator, returning a big array, with a limit on the number of
elements.
This method iterates over the given type-specific iterator and returns a big array containing the elements returned by the iterator. At most max
elements will be returned.
Params: - i –
a type-specific iterator.
- max –
the maximum number of elements to be unwrapped.
Returns: a big array containing the elements returned by the iterator (at most max
).
/**
* Unwraps an iterator, returning a big array, with a limit on the number of
* elements.
*
* <p>
* This method iterates over the given type-specific iterator and returns a big
* array containing the elements returned by the iterator. At most {@code max}
* elements will be returned.
*
* @param i
* a type-specific iterator.
* @param max
* the maximum number of elements to be unwrapped.
* @return a big array containing the elements returned by the iterator (at most
* {@code max}).
*/
public static char[][] unwrapBig(final CharIterator i, long max) {
if (max < 0)
throw new IllegalArgumentException("The maximum number of elements (" + max + ") is negative");
char array[][] = CharBigArrays.newBigArray(16);
long j = 0;
while (max-- != 0 && i.hasNext()) {
if (j == length(array))
array = grow(array, j + 1);
set(array, j++, i.nextChar());
}
return trim(array, j);
}
Unwraps an iterator, returning a big array.
This method iterates over the given type-specific iterator and returns a big
array containing the elements returned by the iterator.
Params: - i –
a type-specific iterator.
Returns: a big array containing the elements returned by the iterator.
/**
* Unwraps an iterator, returning a big array.
*
* <p>
* This method iterates over the given type-specific iterator and returns a big
* array containing the elements returned by the iterator.
*
* @param i
* a type-specific iterator.
* @return a big array containing the elements returned by the iterator.
*/
public static char[][] unwrapBig(final CharIterator i) {
return unwrapBig(i, Long.MAX_VALUE);
}
Unwraps an iterator into a type-specific collection.
This method iterates over the given type-specific iterator and stores the
elements returned in the given type-specific collection. The returned count
on the number unwrapped elements is a long, so that it will work also with
very large collections.
Params: - i –
a type-specific iterator.
- c –
a type-specific collection to contain the output of the iterator.
Returns: the number of elements unwrapped. Note that this is the number of
elements returned by the iterator, which is not necessarily the
number of elements that have been added to the collection (because of
duplicates).
/**
* Unwraps an iterator into a type-specific collection.
*
* <p>
* This method iterates over the given type-specific iterator and stores the
* elements returned in the given type-specific collection. The returned count
* on the number unwrapped elements is a long, so that it will work also with
* very large collections.
*
* @param i
* a type-specific iterator.
* @param c
* a type-specific collection to contain the output of the iterator.
* @return the number of elements unwrapped. Note that this is the number of
* elements returned by the iterator, which is not necessarily the
* number of elements that have been added to the collection (because of
* duplicates).
*/
public static long unwrap(final CharIterator i, final CharCollection c) {
long n = 0;
while (i.hasNext()) {
c.add(i.nextChar());
n++;
}
return n;
}
Pours an iterator into a type-specific collection, with a limit on the number
of elements.
This method iterates over the given type-specific iterator and adds the returned elements to the given collection (up to max
).
Params: - i –
a type-specific iterator.
- s –
a type-specific collection.
- max –
the maximum number of elements to be poured.
Returns: the number of elements poured. Note that this is the number of
elements returned by the iterator, which is not necessarily the
number of elements that have been added to the collection (because of
duplicates).
/**
* Pours an iterator into a type-specific collection, with a limit on the number
* of elements.
*
* <p>
* This method iterates over the given type-specific iterator and adds the
* returned elements to the given collection (up to {@code max}).
*
* @param i
* a type-specific iterator.
* @param s
* a type-specific collection.
* @param max
* the maximum number of elements to be poured.
* @return the number of elements poured. Note that this is the number of
* elements returned by the iterator, which is not necessarily the
* number of elements that have been added to the collection (because of
* duplicates).
*/
public static int pour(final CharIterator i, final CharCollection s, final int max) {
if (max < 0)
throw new IllegalArgumentException("The maximum number of elements (" + max + ") is negative");
int j = max;
while (j-- != 0 && i.hasNext())
s.add(i.nextChar());
return max - j - 1;
}
Pours an iterator into a type-specific collection.
This method iterates over the given type-specific iterator and adds the
returned elements to the given collection.
Params: - i –
a type-specific iterator.
- s –
a type-specific collection.
Returns: the number of elements poured. Note that this is the number of
elements returned by the iterator, which is not necessarily the
number of elements that have been added to the collection (because of
duplicates).
/**
* Pours an iterator into a type-specific collection.
*
* <p>
* This method iterates over the given type-specific iterator and adds the
* returned elements to the given collection.
*
* @param i
* a type-specific iterator.
* @param s
* a type-specific collection.
* @return the number of elements poured. Note that this is the number of
* elements returned by the iterator, which is not necessarily the
* number of elements that have been added to the collection (because of
* duplicates).
*/
public static int pour(final CharIterator i, final CharCollection s) {
return pour(i, s, Integer.MAX_VALUE);
}
Pours an iterator, returning a type-specific list, with a limit on the number
of elements.
This method iterates over the given type-specific iterator and returns a type-specific list containing the returned elements (up to max
). Iteration on the returned list is guaranteed to produce the elements in the same order in which they appeared in the iterator.
Params: - i –
a type-specific iterator.
- max –
the maximum number of elements to be poured.
Returns: a type-specific list containing the returned elements, up to max
.
/**
* Pours an iterator, returning a type-specific list, with a limit on the number
* of elements.
*
* <p>
* This method iterates over the given type-specific iterator and returns a
* type-specific list containing the returned elements (up to {@code max}).
* Iteration on the returned list is guaranteed to produce the elements in the
* same order in which they appeared in the iterator.
*
*
* @param i
* a type-specific iterator.
* @param max
* the maximum number of elements to be poured.
* @return a type-specific list containing the returned elements, up to
* {@code max}.
*/
public static CharList pour(final CharIterator i, int max) {
final CharArrayList l = new CharArrayList();
pour(i, l, max);
l.trim();
return l;
}
Pours an iterator, returning a type-specific list.
This method iterates over the given type-specific iterator and returns a list
containing the returned elements. Iteration on the returned list is
guaranteed to produce the elements in the same order in which they appeared
in the iterator.
Params: - i –
a type-specific iterator.
Returns: a type-specific list containing the returned elements.
/**
* Pours an iterator, returning a type-specific list.
*
* <p>
* This method iterates over the given type-specific iterator and returns a list
* containing the returned elements. Iteration on the returned list is
* guaranteed to produce the elements in the same order in which they appeared
* in the iterator.
*
* @param i
* a type-specific iterator.
* @return a type-specific list containing the returned elements.
*/
public static CharList pour(final CharIterator i) {
return pour(i, Integer.MAX_VALUE);
}
private static class IteratorWrapper implements CharIterator {
final Iterator<Character> i;
public IteratorWrapper(final Iterator<Character> i) {
this.i = i;
}
@Override
public boolean hasNext() {
return i.hasNext();
}
@Override
public void remove() {
i.remove();
}
@Override
public char nextChar() {
return (i.next()).charValue();
}
}
Wraps a standard iterator into a type-specific iterator.
This method wraps a standard iterator into a type-specific one which will handle the type conversions for you. Of course, any attempt to wrap an iterator returning the instances of the wrong class will generate a ClassCastException
. The returned iterator is backed by i
: changes to one of the iterators will affect the other, too.
If i
is already type-specific, it will returned and no new object will be generated.
Params: - i –
an iterator.
Returns: a type-specific iterator backed by i
.
/**
* Wraps a standard iterator into a type-specific iterator.
*
* <p>
* This method wraps a standard iterator into a type-specific one which will
* handle the type conversions for you. Of course, any attempt to wrap an
* iterator returning the instances of the wrong class will generate a
* {@link ClassCastException}. The returned iterator is backed by {@code i}:
* changes to one of the iterators will affect the other, too.
*
* <p>
* If {@code i} is already type-specific, it will returned and no new object
* will be generated.
*
* @param i
* an iterator.
* @return a type-specific iterator backed by {@code i}.
*/
@SuppressWarnings({"unchecked", "rawtypes"})
public static CharIterator asCharIterator(final Iterator i) {
if (i instanceof CharIterator)
return (CharIterator) i;
return new IteratorWrapper(i);
}
private static class ListIteratorWrapper implements CharListIterator {
final ListIterator<Character> i;
public ListIteratorWrapper(final ListIterator<Character> i) {
this.i = i;
}
@Override
public boolean hasNext() {
return i.hasNext();
}
@Override
public boolean hasPrevious() {
return i.hasPrevious();
}
@Override
public int nextIndex() {
return i.nextIndex();
}
@Override
public int previousIndex() {
return i.previousIndex();
}
@Override
public void set(char k) {
i.set(Character.valueOf(k));
}
@Override
public void add(char k) {
i.add(Character.valueOf(k));
}
@Override
public void remove() {
i.remove();
}
@Override
public char nextChar() {
return (i.next()).charValue();
}
@Override
public char previousChar() {
return (i.previous()).charValue();
}
}
Wraps a standard list iterator into a type-specific list iterator.
This method wraps a standard list iterator into a type-specific one which will handle the type conversions for you. Of course, any attempt to wrap an iterator returning the instances of the wrong class will generate a ClassCastException
. The returned iterator is backed by i
: changes to one of the iterators will affect the other, too.
If i
is already type-specific, it will returned and no new object will be generated.
Params: - i –
a list iterator.
Returns: a type-specific list iterator backed by i
.
/**
* Wraps a standard list iterator into a type-specific list iterator.
*
* <p>
* This method wraps a standard list iterator into a type-specific one which
* will handle the type conversions for you. Of course, any attempt to wrap an
* iterator returning the instances of the wrong class will generate a
* {@link ClassCastException}. The returned iterator is backed by {@code i}:
* changes to one of the iterators will affect the other, too.
*
* <p>
* If {@code i} is already type-specific, it will returned and no new object
* will be generated.
*
* @param i
* a list iterator.
* @return a type-specific list iterator backed by {@code i}.
*/
@SuppressWarnings({"unchecked", "rawtypes"})
public static CharListIterator asCharIterator(final ListIterator i) {
if (i instanceof CharListIterator)
return (CharListIterator) i;
return new ListIteratorWrapper(i);
}
public static boolean any(final CharIterator iterator, final java.util.function.IntPredicate predicate) {
return indexOf(iterator, predicate) != -1;
}
public static boolean all(final CharIterator iterator, final java.util.function.IntPredicate predicate) {
Objects.requireNonNull(predicate);
do {
if (!iterator.hasNext())
return true;
} while (predicate.test(iterator.nextChar()));
return false;
}
public static int indexOf(final CharIterator iterator, final java.util.function.IntPredicate predicate) {
Objects.requireNonNull(predicate);
for (int i = 0; iterator.hasNext(); ++i) {
if (predicate.test(iterator.nextChar()))
return i;
}
return -1;
}
private static class IntervalIterator implements CharListIterator {
private final char from, to;
char curr;
public IntervalIterator(final char from, final char to) {
this.from = this.curr = from;
this.to = to;
}
@Override
public boolean hasNext() {
return curr < to;
}
@Override
public boolean hasPrevious() {
return curr > from;
}
@Override
public char nextChar() {
if (!hasNext())
throw new NoSuchElementException();
return curr++;
}
@Override
public char previousChar() {
if (!hasPrevious())
throw new NoSuchElementException();
return --curr;
}
@Override
public int nextIndex() {
return curr - from;
}
@Override
public int previousIndex() {
return curr - from - 1;
}
@Override
public int skip(int n) {
if (curr + n <= to) {
curr += n;
return n;
}
n = to - curr;
curr = to;
return n;
}
@Override
public int back(int n) {
if (curr - n >= from) {
curr -= n;
return n;
}
n = curr - from;
curr = from;
return n;
}
}
Creates a type-specific list iterator over an interval.
The type-specific list iterator returned by this method will return the elements from
, from+1
,…, to-1
.
Params: - from –
the starting element (inclusive).
- to –
the ending element (exclusive).
Returns: a type-specific list iterator enumerating the elements from from
to to
.
/**
* Creates a type-specific list iterator over an interval.
*
* <p>
* The type-specific list iterator returned by this method will return the
* elements {@code from}, {@code from+1},…, {@code to-1}.
*
* @param from
* the starting element (inclusive).
* @param to
* the ending element (exclusive).
* @return a type-specific list iterator enumerating the elements from
* {@code from} to {@code to}.
*/
public static CharListIterator fromTo(final char from, final char to) {
return new IntervalIterator(from, to);
}
private static class IteratorConcatenator implements CharIterator {
final CharIterator a[];
int offset, length, lastOffset = -1;
public IteratorConcatenator(final CharIterator a[], int offset, int length) {
this.a = a;
this.offset = offset;
this.length = length;
advance();
}
private void advance() {
while (length != 0) {
if (a[offset].hasNext())
break;
length--;
offset++;
}
return;
}
@Override
public boolean hasNext() {
return length > 0;
}
@Override
public char nextChar() {
if (!hasNext())
throw new NoSuchElementException();
char next = a[lastOffset = offset].nextChar();
advance();
return next;
}
@Override
public void remove() {
if (lastOffset == -1)
throw new IllegalStateException();
a[lastOffset].remove();
}
@Override
public int skip(int n) {
lastOffset = -1;
int skipped = 0;
while (skipped < n && length != 0) {
skipped += a[offset].skip(n - skipped);
if (a[offset].hasNext())
break;
length--;
offset++;
}
return skipped;
}
}
Concatenates all iterators contained in an array.
This method returns an iterator that will enumerate in order the elements
returned by all iterators contained in the given array.
Params: - a –
an array of iterators.
Returns: an iterator obtained by concatenation.
/**
* Concatenates all iterators contained in an array.
*
* <p>
* This method returns an iterator that will enumerate in order the elements
* returned by all iterators contained in the given array.
*
* @param a
* an array of iterators.
* @return an iterator obtained by concatenation.
*/
public static CharIterator concat(final CharIterator a[]) {
return concat(a, 0, a.length);
}
Concatenates a sequence of iterators contained in an array.
This method returns an iterator that will enumerate in order the elements returned by a[offset]
, then those returned by a[offset + 1]
, and so on up to a[offset + length - 1]
.
Params: - a –
an array of iterators.
- offset –
the index of the first iterator to concatenate.
- length –
the number of iterators to concatenate.
Returns: an iterator obtained by concatenation of length
elements of a
starting at offset
.
/**
* Concatenates a sequence of iterators contained in an array.
*
* <p>
* This method returns an iterator that will enumerate in order the elements
* returned by {@code a[offset]}, then those returned by {@code a[offset + 1]},
* and so on up to {@code a[offset + length - 1]}.
*
* @param a
* an array of iterators.
* @param offset
* the index of the first iterator to concatenate.
* @param length
* the number of iterators to concatenate.
* @return an iterator obtained by concatenation of {@code length} elements of
* {@code a} starting at {@code offset}.
*/
public static CharIterator concat(final CharIterator a[], final int offset, final int length) {
return new IteratorConcatenator(a, offset, length);
}
An unmodifiable wrapper class for iterators. /** An unmodifiable wrapper class for iterators. */
public static class UnmodifiableIterator implements CharIterator {
protected final CharIterator i;
public UnmodifiableIterator(final CharIterator i) {
this.i = i;
}
@Override
public boolean hasNext() {
return i.hasNext();
}
@Override
public char nextChar() {
return i.nextChar();
}
}
Returns an unmodifiable iterator backed by the specified iterator.
Params: - i –
the iterator to be wrapped in an unmodifiable iterator.
Returns: an unmodifiable view of the specified iterator.
/**
* Returns an unmodifiable iterator backed by the specified iterator.
*
* @param i
* the iterator to be wrapped in an unmodifiable iterator.
* @return an unmodifiable view of the specified iterator.
*/
public static CharIterator unmodifiable(final CharIterator i) {
return new UnmodifiableIterator(i);
}
An unmodifiable wrapper class for bidirectional iterators. /** An unmodifiable wrapper class for bidirectional iterators. */
public static class UnmodifiableBidirectionalIterator implements CharBidirectionalIterator {
protected final CharBidirectionalIterator i;
public UnmodifiableBidirectionalIterator(final CharBidirectionalIterator i) {
this.i = i;
}
@Override
public boolean hasNext() {
return i.hasNext();
}
@Override
public boolean hasPrevious() {
return i.hasPrevious();
}
@Override
public char nextChar() {
return i.nextChar();
}
@Override
public char previousChar() {
return i.previousChar();
}
}
Returns an unmodifiable bidirectional iterator backed by the specified
bidirectional iterator.
Params: - i –
the bidirectional iterator to be wrapped in an unmodifiable
bidirectional iterator.
Returns: an unmodifiable view of the specified bidirectional iterator.
/**
* Returns an unmodifiable bidirectional iterator backed by the specified
* bidirectional iterator.
*
* @param i
* the bidirectional iterator to be wrapped in an unmodifiable
* bidirectional iterator.
* @return an unmodifiable view of the specified bidirectional iterator.
*/
public static CharBidirectionalIterator unmodifiable(final CharBidirectionalIterator i) {
return new UnmodifiableBidirectionalIterator(i);
}
An unmodifiable wrapper class for list iterators. /** An unmodifiable wrapper class for list iterators. */
public static class UnmodifiableListIterator implements CharListIterator {
protected final CharListIterator i;
public UnmodifiableListIterator(final CharListIterator i) {
this.i = i;
}
@Override
public boolean hasNext() {
return i.hasNext();
}
@Override
public boolean hasPrevious() {
return i.hasPrevious();
}
@Override
public char nextChar() {
return i.nextChar();
}
@Override
public char previousChar() {
return i.previousChar();
}
@Override
public int nextIndex() {
return i.nextIndex();
}
@Override
public int previousIndex() {
return i.previousIndex();
}
}
Returns an unmodifiable list iterator backed by the specified list iterator.
Params: - i –
the list iterator to be wrapped in an unmodifiable list iterator.
Returns: an unmodifiable view of the specified list iterator.
/**
* Returns an unmodifiable list iterator backed by the specified list iterator.
*
* @param i
* the list iterator to be wrapped in an unmodifiable list iterator.
* @return an unmodifiable view of the specified list iterator.
*/
public static CharListIterator unmodifiable(final CharListIterator i) {
return new UnmodifiableListIterator(i);
}
}