OnJava8-Examples/generics/Generators.java

32 lines
820 B
Java
Raw Normal View History

2015-04-20 15:36:01 -07:00
//: generics/Generators.java
2015-05-29 14:18:51 -07:00
// <20>2015 MindView LLC: see Copyright.txt
2015-04-20 15:36:01 -07:00
// A utility to use with Generators.
import generics.coffee.*;
import java.util.*;
import net.mindview.util.*;
public class Generators {
public static <T> Collection<T>
fill(Collection<T> coll, Generator<T> gen, int n) {
for(int i = 0; i < n; i++)
coll.add(gen.next());
return coll;
2015-05-18 23:05:20 -07:00
}
2015-04-20 15:36:01 -07:00
public static void main(String[] args) {
Collection<Coffee> coffee = fill(
2015-05-05 11:20:13 -07:00
new ArrayList<>(), new CoffeeGenerator(), 4);
2015-04-20 15:36:01 -07:00
for(Coffee c : coffee)
System.out.println(c);
Collection<Integer> fnumbers = fill(
2015-05-05 11:20:13 -07:00
new ArrayList<>(), new Fibonacci(), 12);
2015-04-20 15:36:01 -07:00
for(int i : fnumbers)
System.out.print(i + ", ");
}
} /* Output:
Americano 0
Latte 1
Americano 2
Mocha 3
1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144,
*///:~