Author: [log in to unmask]
Date: Tue Dec 16 17:01:27 2014
New Revision: 1769
Log:
Attempt to create a Clusterer by canonical class name if it is not found by its short name.
Modified:
java/trunk/ecal-recon/src/main/java/org/hps/recon/ecal/cluster/ClustererFactory.java
Modified: java/trunk/ecal-recon/src/main/java/org/hps/recon/ecal/cluster/ClustererFactory.java
=============================================================================
--- java/trunk/ecal-recon/src/main/java/org/hps/recon/ecal/cluster/ClustererFactory.java (original)
+++ java/trunk/ecal-recon/src/main/java/org/hps/recon/ecal/cluster/ClustererFactory.java Tue Dec 16 17:01:27 2014
@@ -4,11 +4,23 @@
import org.lcsim.conditions.ConditionsManager;
/**
- * This is a convenience class for creating different kinds of clustering algorithms.
+ * <p>
+ * This is a convenience class for creating different kinds of clustering algorithms via their name.
+ * <p>
+ * The currently available types include:
+ * <ul>
+ * <li>{@link LegacyClusterer}</li>
+ * <li>{@link SimpleClasInnerCalClusterer}</li>
+ * <li>{@link ClasInnerCalClusterer</li>
+ * </ul>
+ *
* @author Jeremy McCormick <[log in to unmask]>
*/
public final class ClustererFactory {
+ /**
+ * We don't want this class to be instantiated.
+ */
private ClustererFactory() {
}
@@ -21,17 +33,29 @@
*/
public static Clusterer create(String name, double[] cuts) {
Clusterer clusterer;
- System.out.println("simple name:" + LegacyClusterer.class.getSimpleName());
- if (LegacyClusterer.class.getSimpleName().equals(name)) {
+ if (LegacyClusterer.class.getSimpleName().equals(name)) {
+ // Test Run legacy clusterer.
clusterer = new LegacyClusterer();
} else if (SimpleClasInnerCalClusterer.class.getSimpleName().equals(name)) {
+ // Simple IC clusterer.
clusterer = new SimpleClasInnerCalClusterer();
+ } else if (ClasInnerCalClusterer.class.getSimpleName().equals(name)) {
+ // Full IC algorithm.
+ clusterer = new ClasInnerCalClusterer();
} else {
- throw new IllegalArgumentException("Unknown clusterer type: " + name);
+ // Try to instantiate the class from the name argument, assuming it is a canonical class name.
+ try {
+ clusterer = fromCanonicalClassName(name);
+ } catch (Exception e) {
+ // Okay nothing worked, so we have a problem!
+ throw new IllegalArgumentException("Unknown Clusterer type " + name + " cannot be instantiated.", e);
+ }
}
+ // Add the Clusterer as a conditions listener so it can set itself up via the conditions system.
if (clusterer instanceof ConditionsListener) {
ConditionsManager.defaultInstance().addConditionsListener((ConditionsListener) clusterer);
}
+ // Set cuts if they were provided.
if (cuts != null) {
clusterer.setCuts(cuts);
}
@@ -40,10 +64,32 @@
/**
* Create a clustering algorithm with default cut values.
+ * This is just a simple wrapper to {@link #create(String, double[])}.
* @param name The name of the clustering algorithm.
* @return The clustering algorithm.
*/
public static Clusterer create(String name) {
return create(name, null);
}
+
+ /**
+ * Attempt to create a Clusterer object from the canonical class name.
+ * @param canonicalName
+ * @return
+ * @throw IllegalArgumentException if the class does not implement the Clusterer interface.
+ */
+ private static Clusterer fromCanonicalClassName(String canonicalName) {
+ Clusterer clusterer = null;
+ try {
+ Class<?> clustererClass = Class.forName(canonicalName);
+ Object object = clustererClass.newInstance();
+ if (!(object instanceof Clusterer)) {
+ throw new IllegalArgumentException("The class " + canonicalName + " does not implement the Clusterer interface.");
+ }
+ clusterer = (Clusterer) object;
+ } catch (Exception e) {
+ throw new RuntimeException(e);
+ }
+ return clusterer;
+ }
}
|