File tree Expand file tree Collapse file tree 1 file changed +38
-0
lines changed Expand file tree Collapse file tree 1 file changed +38
-0
lines changed Original file line number Diff line number Diff line change 1+ import java .util .*;
2+
3+ interface Generator <T > {
4+ T next ();
5+ }
6+
7+ class CollectionData <T > extends ArrayList <T > {
8+ public CollectionData (Generator <T > gen , int quantity ) {
9+ for (int i = 0 ; i < quantity ; i ++)
10+ add (gen .next ());
11+ }
12+
13+ // A generic convenience method:
14+ public static <T > CollectionData <T > list (Generator <T > gen , int quantity ) {
15+ return new CollectionData <T >(gen , quantity );
16+ }
17+ }
18+
19+ class Government implements Generator <String > {
20+ String [] foundation = ("strange women lying in ponds " +
21+ "distributing swords is no basis for a system of " +
22+ "government" ).split (" " );
23+ private int index ;
24+
25+ public String next () {
26+ return foundation [index ++];
27+ }
28+ }
29+
30+ class CollectionDataTest {
31+ public static void main (String [] args ) {
32+ Set <String > set = new LinkedHashSet <String >(
33+ new CollectionData <String >(new Government (), 15 ));
34+ // Using the convenience method:
35+ set .addAll (CollectionData .list (new Government (), 15 ));
36+ System .out .println (set );
37+ }
38+ }
You can’t perform that action at this time.
0 commit comments