2016-01-25 18:05:55 -08:00
|
|
|
// onjava/Count.java
|
2021-01-31 15:42:31 -07:00
|
|
|
// (c)2021 MindView LLC: see Copyright.txt
|
2016-01-25 18:05:55 -08:00
|
|
|
// We make no guarantees that this code is fit for any purpose.
|
2016-09-23 13:23:35 -06:00
|
|
|
// Visit http://OnJava8.com for more book information.
|
2016-01-25 18:05:55 -08:00
|
|
|
// Generate incremental values of different types
|
|
|
|
package onjava;
|
|
|
|
import java.util.*;
|
|
|
|
import java.util.function.*;
|
|
|
|
import static onjava.ConvertTo.*;
|
|
|
|
|
|
|
|
public interface Count {
|
2017-05-01 14:33:10 -06:00
|
|
|
class Boolean
|
2016-01-25 18:05:55 -08:00
|
|
|
implements Supplier<java.lang.Boolean> {
|
|
|
|
private boolean b = true;
|
2021-01-31 15:42:31 -07:00
|
|
|
@Override public java.lang.Boolean get() {
|
2016-01-25 18:05:55 -08:00
|
|
|
b = !b;
|
|
|
|
return java.lang.Boolean.valueOf(b);
|
|
|
|
}
|
2017-01-20 21:30:44 -08:00
|
|
|
public java.lang.Boolean get(int n) {
|
|
|
|
return get();
|
|
|
|
}
|
2016-01-25 18:05:55 -08:00
|
|
|
public java.lang.Boolean[] array(int sz) {
|
|
|
|
java.lang.Boolean[] result =
|
|
|
|
new java.lang.Boolean[sz];
|
|
|
|
Arrays.setAll(result, n -> get());
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
2017-05-01 14:33:10 -06:00
|
|
|
class Pboolean {
|
2016-01-25 18:05:55 -08:00
|
|
|
private boolean b = true;
|
|
|
|
public boolean get() {
|
|
|
|
b = !b;
|
|
|
|
return b;
|
|
|
|
}
|
|
|
|
public boolean get(int n) { return get(); }
|
|
|
|
public boolean[] array(int sz) {
|
|
|
|
return primitive(new Boolean().array(sz));
|
|
|
|
}
|
|
|
|
}
|
2017-05-01 14:33:10 -06:00
|
|
|
class Byte
|
2016-01-25 18:05:55 -08:00
|
|
|
implements Supplier<java.lang.Byte> {
|
|
|
|
private byte b;
|
|
|
|
@Override
|
|
|
|
public java.lang.Byte get() { return b++; }
|
2017-01-20 21:30:44 -08:00
|
|
|
public java.lang.Byte get(int n) {
|
|
|
|
return get();
|
|
|
|
}
|
2016-01-25 18:05:55 -08:00
|
|
|
public java.lang.Byte[] array(int sz) {
|
2017-01-20 21:30:44 -08:00
|
|
|
java.lang.Byte[] result =
|
|
|
|
new java.lang.Byte[sz];
|
2016-01-25 18:05:55 -08:00
|
|
|
Arrays.setAll(result, n -> get());
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
2017-05-01 14:33:10 -06:00
|
|
|
class Pbyte {
|
2016-01-25 18:05:55 -08:00
|
|
|
private byte b;
|
|
|
|
public byte get() { return b++; }
|
|
|
|
public byte get(int n) { return get(); }
|
|
|
|
public byte[] array(int sz) {
|
|
|
|
return primitive(new Byte().array(sz));
|
|
|
|
}
|
|
|
|
}
|
2016-11-21 12:37:57 -08:00
|
|
|
char[] CHARS =
|
2016-01-25 18:05:55 -08:00
|
|
|
"abcdefghijklmnopqrstuvwxyz".toCharArray();
|
2017-05-01 14:33:10 -06:00
|
|
|
class Character
|
2016-01-25 18:05:55 -08:00
|
|
|
implements Supplier<java.lang.Character> {
|
|
|
|
private int i;
|
2021-01-31 15:42:31 -07:00
|
|
|
@Override public java.lang.Character get() {
|
2016-11-21 12:37:57 -08:00
|
|
|
i = (i + 1) % CHARS.length;
|
|
|
|
return CHARS[i];
|
2016-01-25 18:05:55 -08:00
|
|
|
}
|
|
|
|
public java.lang.Character get(int n) {
|
|
|
|
return get();
|
|
|
|
}
|
|
|
|
public java.lang.Character[] array(int sz) {
|
|
|
|
java.lang.Character[] result =
|
|
|
|
new java.lang.Character[sz];
|
|
|
|
Arrays.setAll(result, n -> get());
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
2017-05-01 14:33:10 -06:00
|
|
|
class Pchar {
|
2016-01-25 18:05:55 -08:00
|
|
|
private int i;
|
|
|
|
public char get() {
|
2016-11-21 12:37:57 -08:00
|
|
|
i = (i + 1) % CHARS.length;
|
|
|
|
return CHARS[i];
|
2016-01-25 18:05:55 -08:00
|
|
|
}
|
|
|
|
public char get(int n) { return get(); }
|
|
|
|
public char[] array(int sz) {
|
|
|
|
return primitive(new Character().array(sz));
|
|
|
|
}
|
|
|
|
}
|
2017-05-01 14:33:10 -06:00
|
|
|
class Short
|
2016-01-25 18:05:55 -08:00
|
|
|
implements Supplier<java.lang.Short> {
|
|
|
|
short s;
|
|
|
|
@Override
|
|
|
|
public java.lang.Short get() { return s++; }
|
2017-01-20 21:30:44 -08:00
|
|
|
public java.lang.Short get(int n) {
|
|
|
|
return get();
|
|
|
|
}
|
2016-01-25 18:05:55 -08:00
|
|
|
public java.lang.Short[] array(int sz) {
|
2017-01-20 21:30:44 -08:00
|
|
|
java.lang.Short[] result =
|
|
|
|
new java.lang.Short[sz];
|
2016-01-25 18:05:55 -08:00
|
|
|
Arrays.setAll(result, n -> get());
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
2017-05-01 14:33:10 -06:00
|
|
|
class Pshort {
|
2016-01-25 18:05:55 -08:00
|
|
|
short s;
|
|
|
|
public short get() { return s++; }
|
|
|
|
public short get(int n) { return get(); }
|
|
|
|
public short[] array(int sz) {
|
|
|
|
return primitive(new Short().array(sz));
|
|
|
|
}
|
|
|
|
}
|
2017-05-01 14:33:10 -06:00
|
|
|
class Integer
|
2016-01-25 18:05:55 -08:00
|
|
|
implements Supplier<java.lang.Integer> {
|
|
|
|
int i;
|
|
|
|
@Override
|
|
|
|
public java.lang.Integer get() { return i++; }
|
2017-01-20 21:30:44 -08:00
|
|
|
public java.lang.Integer get(int n) {
|
|
|
|
return get();
|
|
|
|
}
|
2016-01-25 18:05:55 -08:00
|
|
|
public java.lang.Integer[] array(int sz) {
|
|
|
|
java.lang.Integer[] result =
|
|
|
|
new java.lang.Integer[sz];
|
|
|
|
Arrays.setAll(result, n -> get());
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
2017-05-01 14:33:10 -06:00
|
|
|
class Pint implements IntSupplier {
|
2016-01-25 18:05:55 -08:00
|
|
|
int i;
|
|
|
|
public int get() { return i++; }
|
|
|
|
public int get(int n) { return get(); }
|
|
|
|
@Override
|
|
|
|
public int getAsInt() { return get(); }
|
|
|
|
public int[] array(int sz) {
|
|
|
|
return primitive(new Integer().array(sz));
|
|
|
|
}
|
|
|
|
}
|
2017-05-01 14:33:10 -06:00
|
|
|
class Long
|
2016-01-25 18:05:55 -08:00
|
|
|
implements Supplier<java.lang.Long> {
|
|
|
|
private long l;
|
|
|
|
@Override
|
|
|
|
public java.lang.Long get() { return l++; }
|
2017-01-20 21:30:44 -08:00
|
|
|
public java.lang.Long get(int n) {
|
|
|
|
return get();
|
|
|
|
}
|
2016-01-25 18:05:55 -08:00
|
|
|
public java.lang.Long[] array(int sz) {
|
2017-01-20 21:30:44 -08:00
|
|
|
java.lang.Long[] result =
|
|
|
|
new java.lang.Long[sz];
|
2016-01-25 18:05:55 -08:00
|
|
|
Arrays.setAll(result, n -> get());
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
2017-01-20 21:30:44 -08:00
|
|
|
class Plong implements LongSupplier {
|
2016-01-25 18:05:55 -08:00
|
|
|
private long l;
|
|
|
|
public long get() { return l++; }
|
|
|
|
public long get(int n) { return get(); }
|
|
|
|
@Override
|
|
|
|
public long getAsLong() { return get(); }
|
|
|
|
public long[] array(int sz) {
|
|
|
|
return primitive(new Long().array(sz));
|
|
|
|
}
|
|
|
|
}
|
2017-05-01 14:33:10 -06:00
|
|
|
class Float
|
2016-01-25 18:05:55 -08:00
|
|
|
implements Supplier<java.lang.Float> {
|
|
|
|
private int i;
|
2021-01-31 15:42:31 -07:00
|
|
|
@Override public java.lang.Float get() {
|
2016-01-25 18:05:55 -08:00
|
|
|
return java.lang.Float.valueOf(i++);
|
|
|
|
}
|
2017-01-20 21:30:44 -08:00
|
|
|
public java.lang.Float get(int n) {
|
|
|
|
return get();
|
|
|
|
}
|
2016-01-25 18:05:55 -08:00
|
|
|
public java.lang.Float[] array(int sz) {
|
2017-01-20 21:30:44 -08:00
|
|
|
java.lang.Float[] result =
|
|
|
|
new java.lang.Float[sz];
|
2016-01-25 18:05:55 -08:00
|
|
|
Arrays.setAll(result, n -> get());
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
2017-05-01 14:33:10 -06:00
|
|
|
class Pfloat {
|
2016-01-25 18:05:55 -08:00
|
|
|
private int i;
|
|
|
|
public float get() { return i++; }
|
|
|
|
public float get(int n) { return get(); }
|
|
|
|
public float[] array(int sz) {
|
|
|
|
return primitive(new Float().array(sz));
|
|
|
|
}
|
|
|
|
}
|
2017-05-01 14:33:10 -06:00
|
|
|
class Double
|
2016-01-25 18:05:55 -08:00
|
|
|
implements Supplier<java.lang.Double> {
|
|
|
|
private int i;
|
2021-01-31 15:42:31 -07:00
|
|
|
@Override public java.lang.Double get() {
|
2016-01-25 18:05:55 -08:00
|
|
|
return java.lang.Double.valueOf(i++);
|
|
|
|
}
|
2017-01-20 21:30:44 -08:00
|
|
|
public java.lang.Double get(int n) {
|
|
|
|
return get();
|
|
|
|
}
|
2016-01-25 18:05:55 -08:00
|
|
|
public java.lang.Double[] array(int sz) {
|
|
|
|
java.lang.Double[] result =
|
|
|
|
new java.lang.Double[sz];
|
|
|
|
Arrays.setAll(result, n -> get());
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
2017-05-01 14:33:10 -06:00
|
|
|
class Pdouble implements DoubleSupplier {
|
2016-01-25 18:05:55 -08:00
|
|
|
private int i;
|
|
|
|
public double get() { return i++; }
|
|
|
|
public double get(int n) { return get(); }
|
|
|
|
@Override
|
|
|
|
public double getAsDouble() { return get(0); }
|
|
|
|
public double[] array(int sz) {
|
|
|
|
return primitive(new Double().array(sz));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|