OnJava8-Examples/net/mindview/util/BasicGenerator.java

25 lines
745 B
Java
Raw Normal View History

2015-04-20 15:36:01 -07:00
//: net/mindview/util/BasicGenerator.java
2015-05-29 14:18:51 -07:00
// <20>2015 MindView LLC: see Copyright.txt
2015-04-20 15:36:01 -07:00
// Automatically create a Generator, given a class
// with a default (no-arg) constructor.
package net.mindview.util;
public class BasicGenerator<T> implements Generator<T> {
private Class<T> type;
public BasicGenerator(Class<T> type){ this.type = type; }
2015-05-05 11:20:13 -07:00
@Override
2015-04-20 15:36:01 -07:00
public T next() {
try {
// Assumes type is a public class:
return type.newInstance();
2015-05-06 12:09:38 -07:00
} catch(InstantiationException |
IllegalAccessException e) {
2015-04-20 15:36:01 -07:00
throw new RuntimeException(e);
}
}
// Produce a Default generator given a type token:
public static <T> Generator<T> create(Class<T> type) {
2015-05-05 11:20:13 -07:00
return new BasicGenerator<>(type);
2015-04-20 15:36:01 -07:00
}
} ///:~