com.xenoamess.commons.primitive.iterators.CharIterator Maven / Gradle / Ivy
Show all versions of commonx Show documentation
/*
* Copyright (c) 1997, 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code 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 General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.xenoamess.commons.primitive.iterators;
import com.xenoamess.commons.primitive.Primitive;
import com.xenoamess.commons.primitive.functions.CharConsumer;
import java.util.Collection;
import java.util.Iterator;
import java.util.ListIterator;
import java.util.Objects;
import java.util.function.Consumer;
/**
* An iterator over a collection. {@code Iterator} takes the place of
* {@link java.util.Enumeration} in the Java Collections Framework. Iterators
* differ from enumerations in two ways:
*
*
* - Iterators allow the caller to remove elements from the
* underlying collection during the iteration with well-defined
* semantics.
*
- Method names have been improved.
*
*
* This interface is a member of the
*
* Java Collections Framework.
*
* @author Josh Bloch
* @author XenoAmess
* @version 0.8.0
* @apiNote An {@link java.util.Enumeration} can be converted into an {@code Iterator} by
* using the {@link java.util.Enumeration#asIterator} method.
* @see Collection
* @see ListIterator
* @see Iterable
* @see Iterator
* @since 1.2
*/
public interface CharIterator extends Iterator, Primitive {
/**
* {@inheritDoc}
*/
@Override
default Character next() {
return this.nextPrimitive();
}
/**
* Primitive replacement of next()
*
* @return the next element in the iteration
* @see #next()
*/
char nextPrimitive();
/**
* {@inheritDoc}
*
* Performs the given action for each remaining element until all elements
* have been processed or the action throws an exception. Actions are
* performed in the order of iteration, if that order is specified.
* Exceptions thrown by the action are relayed to the caller.
*
* The behavior of an iterator is unspecified if the action modifies the
* collection in any way (even by calling the {@link #remove remove} method
* or other mutator methods of {@code Iterator} subtypes),
* unless an overriding class has specified a concurrent modification policy.
*
* Subsequent behavior of an iterator is unspecified if the action throws an
* exception.
*
* @implSpec
The default implementation behaves as if:
*
{@code
* while (hasNext())
* action.accept(next());
* }
* @since 1.8
*/
@Override
default void forEachRemaining(Consumer super Character> action) {
Objects.requireNonNull(action);
if (action instanceof CharConsumer) {
CharConsumer actionCharConsumer = (CharConsumer) action;
while (hasNext()) {
actionCharConsumer.acceptPrimitive(nextPrimitive());
}
} else {
while (hasNext()) {
action.accept(nextPrimitive());
}
}
}
}