OnJava8-Examples/onjava/CountingSupplier.java

86 lines
2.2 KiB
Java
Raw Normal View History

// onjava/CountingSupplier.java
2015-06-15 17:47:35 -07:00
// Simple generator implementations.
package onjava;
2015-11-03 12:00:44 -08:00
import java.util.function.*;
2015-06-15 17:47:35 -07:00
2015-11-03 12:00:44 -08:00
public class CountingSupplier {
2015-06-15 17:47:35 -07:00
public static class
2015-11-03 12:00:44 -08:00
Boolean implements Supplier<java.lang.Boolean> {
2015-06-15 17:47:35 -07:00
private boolean value = false;
@Override
2015-11-03 12:00:44 -08:00
public java.lang.Boolean get() {
2015-06-15 17:47:35 -07:00
value = !value; // Just flips back and forth
return value;
}
}
public static class
2015-11-03 12:00:44 -08:00
Byte implements Supplier<java.lang.Byte> {
2015-06-15 17:47:35 -07:00
private byte value = 0;
@Override
2015-11-03 12:00:44 -08:00
public java.lang.Byte get() { return value++; }
2015-06-15 17:47:35 -07:00
}
static char[] chars = ("abcdefghijklmnopqrstuvwxyz" +
"ABCDEFGHIJKLMNOPQRSTUVWXYZ").toCharArray();
public static class
2015-11-03 12:00:44 -08:00
Character implements Supplier<java.lang.Character> {
2015-06-15 17:47:35 -07:00
int index = -1;
@Override
2015-11-03 12:00:44 -08:00
public java.lang.Character get() {
2015-06-15 17:47:35 -07:00
index = (index + 1) % chars.length;
return chars[index];
}
}
public static class
2015-11-03 12:00:44 -08:00
String implements Supplier<java.lang.String> {
2015-06-15 17:47:35 -07:00
private int length = 7;
2015-11-03 12:00:44 -08:00
Supplier<java.lang.Character> cg = new Character();
2015-06-15 17:47:35 -07:00
public String() {}
public String(int length) { this.length = length; }
@Override
2015-11-03 12:00:44 -08:00
public java.lang.String get() {
2015-06-15 17:47:35 -07:00
char[] buf = new char[length];
for(int i = 0; i < length; i++)
2015-11-03 12:00:44 -08:00
buf[i] = cg.get();
2015-06-15 17:47:35 -07:00
return new java.lang.String(buf);
}
}
public static class
2015-11-03 12:00:44 -08:00
Short implements Supplier<java.lang.Short> {
2015-06-15 17:47:35 -07:00
private short value = 0;
@Override
2015-11-03 12:00:44 -08:00
public java.lang.Short get() { return value++; }
2015-06-15 17:47:35 -07:00
}
public static class
2015-11-03 12:00:44 -08:00
Integer implements Supplier<java.lang.Integer> {
2015-06-15 17:47:35 -07:00
private int value = 0;
@Override
2015-11-03 12:00:44 -08:00
public java.lang.Integer get() { return value++; }
2015-06-15 17:47:35 -07:00
}
public static class
2015-11-03 12:00:44 -08:00
Long implements Supplier<java.lang.Long> {
2015-06-15 17:47:35 -07:00
private long value = 0;
@Override
2015-11-03 12:00:44 -08:00
public java.lang.Long get() { return value++; }
2015-06-15 17:47:35 -07:00
}
public static class
2015-11-03 12:00:44 -08:00
Float implements Supplier<java.lang.Float> {
2015-06-15 17:47:35 -07:00
private float value = 0;
@Override
2015-11-03 12:00:44 -08:00
public java.lang.Float get() {
2015-06-15 17:47:35 -07:00
float result = value;
value += 1.0;
return result;
}
}
public static class
2015-11-03 12:00:44 -08:00
Double implements Supplier<java.lang.Double> {
2015-06-15 17:47:35 -07:00
private double value = 0.0;
@Override
2015-11-03 12:00:44 -08:00
public java.lang.Double get() {
2015-06-15 17:47:35 -07:00
double result = value;
value += 1.0;
return result;
}
}
2015-09-07 11:44:36 -06:00
}