OnJava8-Examples/com/mindviewinc/util/CountingSupplier.java
2015-11-03 12:00:44 -08:00

86 lines
2.3 KiB
Java

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