54 lines
1.6 KiB
Java
Raw Normal View History

2015-09-07 11:44:36 -06:00
// containersindepth/MapDataTest.java
2015-06-15 17:47:35 -07:00
import java.util.*;
2015-11-03 12:00:44 -08:00
import java.util.function.*;
import onjava.*;
2015-06-15 17:47:35 -07:00
2015-11-03 12:00:44 -08:00
class Letters implements Supplier<Pair<Integer,String>>,
2015-06-15 17:47:35 -07:00
Iterable<Integer> {
private int size = 9;
private int number = 1;
private char letter = 'A';
@Override
2015-11-03 12:00:44 -08:00
public Pair<Integer,String> get() {
2015-06-15 17:47:35 -07:00
return new Pair<>(number++, "" + letter++);
}
@Override
public Iterator<Integer> iterator() {
return new Iterator<Integer>() {
@Override
public Integer next() { return number++; }
@Override
public boolean hasNext() { return number < size; }
@Override
public void remove() {
throw new UnsupportedOperationException();
}
};
}
}
public class MapDataTest {
public static void main(String[] args) {
2015-11-03 12:00:44 -08:00
// Pair Supplier:
System.out.println(MapData.map(new Letters(), 11));
2015-06-15 17:47:35 -07:00
// Two separate generators:
2015-11-03 12:00:44 -08:00
System.out.println(MapData.map(new CountingSupplier.Character(),
new RandomSupplier.String(3), 8));
// A key Supplier and a single value:
System.out.println(MapData.map(new CountingSupplier.Character(),
2015-06-15 17:47:35 -07:00
"Value", 6));
2015-11-03 12:00:44 -08:00
// An Iterable and a value Supplier:
System.out.println(MapData.map(new Letters(),
new RandomSupplier.String(3)));
2015-06-15 17:47:35 -07:00
// An Iterable and a single value:
2015-11-03 12:00:44 -08:00
System.out.println(MapData.map(new Letters(), "Pop"));
2015-06-15 17:47:35 -07:00
}
2015-09-07 11:44:36 -06:00
}
/* Output:
2015-06-15 17:47:35 -07:00
{1=A, 2=B, 3=C, 4=D, 5=E, 6=F, 7=G, 8=H, 9=I, 10=J, 11=K}
{a=YNz, b=brn, c=yGc, d=FOW, e=ZnT, f=cQr, g=Gse, h=GZM}
{a=Value, b=Value, c=Value, d=Value, e=Value, f=Value}
{1=mJM, 2=RoE, 3=suE, 4=cUO, 5=neO, 6=EdL, 7=smw, 8=HLG}
{1=Pop, 2=Pop, 3=Pop, 4=Pop, 5=Pop, 6=Pop, 7=Pop, 8=Pop}
2015-09-07 11:44:36 -06:00
*/