package org.lionsoul.jcseg.tokenizer.core;

import java.io.IOException;
import java.lang.reflect.Constructor;

import org.lionsoul.jcseg.tokenizer.Dictionary;

/**
 * <p>
 * Dictionary Factory to create Dictionary instance
 * a path of the class that has extends the ADictionary class must be given first
 * </p>
 * 
 * @author    chenxin<chenxin619315@gmail.com>
 */
public class DictionaryFactory {
    
    private DictionaryFactory() {}
    
    /**
     * create a new ADictionary instance
     * 
     * @param     _class
     * @return    ADictionary
     */
    public static ADictionary createDictionary(
            Class<? extends ADictionary> _class, Class<?>[] paramType, Object[] args)
    {
        try {
            Constructor<?> cons = _class.getConstructor(paramType);
            return ( ( ADictionary ) cons.newInstance(args) );
        } catch ( Exception e ) {
            System.err.println("can't create the ADictionary instance " +
                    "with classpath ["+_class.getName()+"]");
            e.printStackTrace();
        }
        return null;
    }
    
    /**
     * create a default ADictionary instance of class com.webssky.jcseg.Dictionary
     * 
     * @param    config
     * @param    sync
     * @return    ADictionary
     */
    public static ADictionary createDefaultDictionary( 
            JcsegTaskConfig config, boolean sync ) 
    {
        ADictionary dic = createDictionary(Dictionary.class,
                    new Class[]{JcsegTaskConfig.class, Boolean.class},
                    new Object[]{config, sync});
        try {
            //load lexicon from more than one path.
            String[] lexpath = config.getLexiconPath();
            if ( lexpath == null ) 
                throw new IOException("Invalid lexicon path, " +
                        "make sure the JcsegTaskConfig is initialized.");
            
            //load word item from all the directories.
            for ( String lpath : lexpath )
                dic.loadFromLexiconDirectory(lpath);
            if ( dic.getConfig().isAutoload() ) dic.startAutoload();
        } catch (IOException e) {
            e.printStackTrace();
        }
        
        return dic;
    }
    
    public static ADictionary createDefaultDictionary( JcsegTaskConfig config ) {
        return createDefaultDictionary(config, config.isAutoload());
    }
}

@@ -392,17 +392,222 @@
 Factory 
-%7B
+%0A%7B%0A    /**%0A     * singleton lock %0A    */%0A    private final static Object LOCK = new Object();%0A    %0A    /**%0A     * singleton dictionary object %0A    */%0A    private static ADictionary singletonDic = null;%0A    
 %0A    %0A  
@@ -2473,17 +2473,16 @@
 tionary(
- 
 JcsegTas
@@ -2495,17 +2495,16 @@
 g config
- 
 ) %7B%0A    
@@ -2574,10 +2574,388 @@
 ;%0A    %7D%0A
+    %0A    /**%0A     * create a singleton ADictionary object%0A     * %0A     * @return  ADictionary%0A    */%0A    public static ADictionary createSingletonDictionary(JcsegTaskConfig config) %7B%0A        synchronized (LOCK) %7B%0A            if ( singletonDic == null ) %7B%0A                singletonDic = createDefaultDictionary(config);%0A            %7D%0A        %7D%0A        return singletonDic;%0A    %7D%0A
 %7D%0A

add singleton ADictionary create interface