Skip to content

Commit

Permalink
And there's the iterators.
Browse files Browse the repository at this point in the history
  • Loading branch information
tommyettinger committed Mar 30, 2023
1 parent a9f66b7 commit a87ee5c
Show file tree
Hide file tree
Showing 8 changed files with 12 additions and 184 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,14 @@

import com.github.tommyettinger.function.BooleanConsumer;

import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.PrimitiveIterator;
import java.util.function.Consumer;

/**
* An Iterator specialized for {@code boolean} values.
* This is a {@link PrimitiveIterator}, like the existing {@link OfInt}
* and {@link OfLong} interfaces, but it can't be a nested interface like
* those because it is defined outside of the JDK.
* This iterates over primitive booleans using {@link #nextBoolean()}.
*/
public interface BooleanIterator extends PrimitiveIterator<Boolean, BooleanConsumer> {
public interface BooleanIterator extends Iterator<Boolean> {
/**
* Returns the next {@code boolean} element in the iteration.
*
Expand All @@ -52,7 +49,6 @@ public interface BooleanIterator extends PrimitiveIterator<Boolean, BooleanConsu
* action.accept(nextBoolean());
* }</pre>
*/
@Override
default void forEachRemaining (BooleanConsumer action) {
while (hasNext()) {action.accept(nextBoolean());}
}
Expand All @@ -67,23 +63,4 @@ default void forEachRemaining (BooleanConsumer action) {
default Boolean next () {
return nextBoolean();
}

/**
* {@inheritDoc}
*
* @implSpec If the action is an instance of {@code LongConsumer} then it is cast
* to {@code LongConsumer} and passed to {@link #forEachRemaining};
* otherwise the action is adapted to an instance of
* {@code LongConsumer}, by boxing the argument of {@code LongConsumer},
* and then passed to {@link #forEachRemaining}.
*/
@Override
default void forEachRemaining (Consumer<? super Boolean> action) {
if (action instanceof BooleanConsumer) {
forEachRemaining((BooleanConsumer)action);
} else {
forEachRemaining((BooleanConsumer)action::accept);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,14 @@

import com.github.tommyettinger.function.ByteConsumer;

import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.PrimitiveIterator;
import java.util.function.Consumer;

/**
* An Iterator specialized for {@code byte} values.
* This is a {@link PrimitiveIterator}, like the existing {@link OfInt}
* and {@link OfLong} interfaces, but it can't be a nested interface like
* those because it is defined outside of the JDK.
* This iterates over primitive bytes using {@link #nextByte()}.
*/
public interface ByteIterator extends PrimitiveIterator<Byte, ByteConsumer> {
public interface ByteIterator extends Iterator<Byte> {
/**
* Returns the next {@code byte} element in the iteration.
*
Expand All @@ -52,7 +49,6 @@ public interface ByteIterator extends PrimitiveIterator<Byte, ByteConsumer> {
* action.accept(nextByte());
* }</pre>
*/
@Override
default void forEachRemaining (ByteConsumer action) {
while (hasNext()) {action.accept(nextByte());}
}
Expand All @@ -67,23 +63,4 @@ default void forEachRemaining (ByteConsumer action) {
default Byte next () {
return nextByte();
}

/**
* {@inheritDoc}
*
* @implSpec If the action is an instance of {@code LongConsumer} then it is cast
* to {@code LongConsumer} and passed to {@link #forEachRemaining};
* otherwise the action is adapted to an instance of
* {@code LongConsumer}, by boxing the argument of {@code LongConsumer},
* and then passed to {@link #forEachRemaining}.
*/
@Override
default void forEachRemaining (Consumer<? super Byte> action) {
if (action instanceof ByteConsumer) {
forEachRemaining((ByteConsumer)action);
} else {
forEachRemaining((ByteConsumer)action::accept);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,14 @@

import com.github.tommyettinger.function.CharConsumer;

import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.PrimitiveIterator;
import java.util.function.Consumer;

/**
* An Iterator specialized for {@code char} values.
* This is a {@link PrimitiveIterator}, like the existing {@link OfInt}
* and {@link OfLong} interfaces, but it can't be a nested interface like
* those because it is defined outside of the JDK.
* This iterates over primitive chars using {@link #nextChar()}.
*/
public interface CharIterator extends PrimitiveIterator<Character, CharConsumer> {
public interface CharIterator extends Iterator<Character> {
/**
* Returns the next {@code char} element in the iteration.
*
Expand All @@ -52,7 +49,6 @@ public interface CharIterator extends PrimitiveIterator<Character, CharConsumer>
* action.accept(nextChar());
* }</pre>
*/
@Override
default void forEachRemaining (CharConsumer action) {
while (hasNext()) {action.accept(nextChar());}
}
Expand All @@ -67,23 +63,4 @@ default void forEachRemaining (CharConsumer action) {
default Character next () {
return nextChar();
}

/**
* {@inheritDoc}
*
* @implSpec If the action is an instance of {@code LongConsumer} then it is cast
* to {@code LongConsumer} and passed to {@link #forEachRemaining};
* otherwise the action is adapted to an instance of
* {@code LongConsumer}, by boxing the argument of {@code LongConsumer},
* and then passed to {@link #forEachRemaining}.
*/
@Override
default void forEachRemaining (Consumer<? super Character> action) {
if (action instanceof CharConsumer) {
forEachRemaining((CharConsumer)action);
} else {
forEachRemaining((CharConsumer)action::accept);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.function.Consumer;

/**
* An Iterator specialized for {@code double} values.
Expand Down Expand Up @@ -67,23 +66,4 @@ default void forEachRemaining (DoubleConsumer action) {
default Double next () {
return nextDouble();
}

/**
* {@inheritDoc}
*
* @implSpec If the action is an instance of {@code DoubleConsumer} then it is cast
* to {@code DoubleConsumer} and passed to {@link #forEachRemaining};
* otherwise the action is adapted to an instance of
* {@code DoubleConsumer}, by boxing the argument of {@code DoubleConsumer},
* and then passed to {@link #forEachRemaining}.
*/
@Override
default void forEachRemaining (Consumer<? super Double> action) {
if (action instanceof DoubleConsumer) {
forEachRemaining((DoubleConsumer)action);
} else {
forEachRemaining((DoubleConsumer)action::accept);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.function.Consumer;

/**
* An Iterator specialized for {@code float} values.
Expand Down Expand Up @@ -64,23 +63,4 @@ default void forEachRemaining (FloatConsumer action) {
default Float next () {
return nextFloat();
}

/**
* {@inheritDoc}
*
* @implSpec If the action is an instance of {@code FloatConsumer} then it is cast
* to {@code FloatConsumer} and passed to {@link #forEachRemaining};
* otherwise the action is adapted to an instance of
* {@code FloatConsumer}, by boxing the argument of {@code FloatConsumer},
* and then passed to {@link #forEachRemaining}.
*/
@Override
default void forEachRemaining (Consumer<? super Float> action) {
if (action instanceof FloatConsumer) {
forEachRemaining((FloatConsumer)action);
} else {
forEachRemaining((FloatConsumer)action::accept);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.function.Consumer;

/**
* An Iterator specialized for {@code int} values.
Expand Down Expand Up @@ -67,23 +66,4 @@ default void forEachRemaining (IntConsumer action) {
default Integer next () {
return nextInt();
}

/**
* {@inheritDoc}
*
* @implSpec If the action is an instance of {@code IntConsumer} then it is cast
* to {@code IntConsumer} and passed to {@link #forEachRemaining};
* otherwise the action is adapted to an instance of
* {@code IntConsumer}, by boxing the argument of {@code IntConsumer},
* and then passed to {@link #forEachRemaining}.
*/
@Override
default void forEachRemaining (Consumer<? super Integer> action) {
if (action instanceof IntConsumer) {
forEachRemaining((IntConsumer)action);
} else {
forEachRemaining((IntConsumer)action::accept);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.function.Consumer;

/**
* An Iterator specialized for {@code long} values.
Expand Down Expand Up @@ -67,23 +66,4 @@ default void forEachRemaining (LongConsumer action) {
default Long next () {
return nextLong();
}

/**
* {@inheritDoc}
*
* @implSpec If the action is an instance of {@code LongConsumer} then it is cast
* to {@code LongConsumer} and passed to {@link #forEachRemaining};
* otherwise the action is adapted to an instance of
* {@code LongConsumer}, by boxing the argument of {@code LongConsumer},
* and then passed to {@link #forEachRemaining}.
*/
@Override
default void forEachRemaining (Consumer<? super Long> action) {
if (action instanceof LongConsumer) {
forEachRemaining((LongConsumer)action);
} else {
forEachRemaining((LongConsumer)action::accept);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,14 @@

import com.github.tommyettinger.function.ShortConsumer;

import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.PrimitiveIterator;
import java.util.function.Consumer;

/**
* An Iterator specialized for {@code short} values.
* This is a {@link PrimitiveIterator}, like the existing {@link OfInt}
* and {@link OfLong} interfaces, but it can't be a nested interface like
* those because it is defined outside of the JDK.
* This iterates over primitive shorts using {@link #nextShort()}.
*/
public interface ShortIterator extends PrimitiveIterator<Short, ShortConsumer> {
public interface ShortIterator extends Iterator<Short> {
/**
* Returns the next {@code short} element in the iteration.
*
Expand All @@ -52,7 +49,6 @@ public interface ShortIterator extends PrimitiveIterator<Short, ShortConsumer> {
* action.accept(nextShort());
* }</pre>
*/
@Override
default void forEachRemaining (ShortConsumer action) {
while (hasNext()) {action.accept(nextShort());}
}
Expand All @@ -67,23 +63,4 @@ default void forEachRemaining (ShortConsumer action) {
default Short next () {
return nextShort();
}

/**
* {@inheritDoc}
*
* @implSpec If the action is an instance of {@code LongConsumer} then it is cast
* to {@code LongConsumer} and passed to {@link #forEachRemaining};
* otherwise the action is adapted to an instance of
* {@code LongConsumer}, by boxing the argument of {@code LongConsumer},
* and then passed to {@link #forEachRemaining}.
*/
@Override
default void forEachRemaining (Consumer<? super Short> action) {
if (action instanceof ShortConsumer) {
forEachRemaining((ShortConsumer)action);
} else {
forEachRemaining((ShortConsumer)action::accept);
}
}

}

0 comments on commit a87ee5c

Please sign in to comment.