TriggerFactory.java

codebase/src/net/sourceforge/pain/logic/trigger/TriggerFactory.java · 1.2 KB

From the archive pain_0_44.tgz

Download the archive   « Back to the archive contents

package net.sourceforge.pain.logic.trigger;

import net.sourceforge.pain.data.*;
import net.sourceforge.pain.logic.*;

import java.util.*;

public final class TriggerFactory {

    public static final String TRIGGER_PACKAGE_PREFIX = TriggerData.TRIGGER_LOGIC_PACKAGE_PREFIX;
    private static final Map cache = new HashMap();
    private static Object[] paramsValues = new TriggerData[1];
    private static Class[] paramsTypes = new Class[]{TriggerData.class};


    private TriggerFactory() {
    }

    //todo: test cache performance: do we need it at all -->> yes reflection 10 times slower (jdk1.4.2)
    
    public static Trigger provideTrigger(TriggerData td) throws Exception {
        String classNameSuffix = td.getTriggerClassName();
        Trigger sample = (Trigger) cache.get(classNameSuffix);
        if (sample == null) {
            Class c = Class.forName(TRIGGER_PACKAGE_PREFIX + classNameSuffix);
            sample = (Trigger) c.getDeclaredConstructor(paramsTypes).newInstance(paramsValues);
            cache.put(classNameSuffix, sample);
        }
        paramsValues[0] = td;
        Trigger t = sample.newInstance(td);
        paramsValues[0] = null;
        return t;
    }
}

Site Stats

Snippets: 1791
Codebases: 571