OnJava8-Examples/arrays/SuppliersTest.java

41 lines
1.3 KiB
Java
Raw Normal View History

2015-11-03 12:00:44 -08:00
// arrays/SuppliersTest.java
2015-12-15 11:47:04 -08:00
// (c)2016 MindView LLC: see Copyright.txt
2015-11-15 15:51:35 -08:00
// We make no guarantees that this code is fit for any purpose.
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
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
public class SuppliersTest {
2015-06-15 17:47:35 -07:00
public static int size = 10;
public static void test(Class<?> surroundingClass) {
for(Class<?> type : surroundingClass.getClasses()) {
System.out.print(type.getSimpleName() + ": ");
try {
2015-11-03 12:00:44 -08:00
Supplier<?> g = (Supplier<?>)type.newInstance();
2015-06-15 17:47:35 -07:00
for(int i = 0; i < size; i++)
2015-11-03 12:00:44 -08:00
System.out.printf(g.get() + " ");
2015-06-15 17:47:35 -07:00
System.out.println();
} catch(InstantiationException |
IllegalAccessException e) {
throw new RuntimeException(e);
}
}
}
public static void main(String[] args) {
2015-11-03 12:00:44 -08:00
test(CountingSupplier.class);
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
Double: 0.0 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0
Float: 0.0 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0
Long: 0 1 2 3 4 5 6 7 8 9
Integer: 0 1 2 3 4 5 6 7 8 9
Short: 0 1 2 3 4 5 6 7 8 9
String: abcdefg hijklmn opqrstu vwxyzAB CDEFGHI JKLMNOP
QRSTUVW XYZabcd efghijk lmnopqr
Character: a b c d e f g h i j
Byte: 0 1 2 3 4 5 6 7 8 9
Boolean: true false true false true false true false true
false
2015-09-07 11:44:36 -06:00
*/