View Javadoc

1   package org.lsst.ccs.command;
2   
3   import java.lang.reflect.Method;
4   import java.util.HashMap;
5   import java.util.Map;
6   
7   /**
8    * static utilities to handle <TT>LocalCommandDictionary</TT>
9    * @author bamade
10   */
11  public class LocalBuilders {
12  
13  	private static Map<Class, LocalCommandDictionary> mapBuilders = new HashMap<>();
14  	private static InputConversionEngine engine = new InputConversionEngine();
15  
16  	/**
17  	 * *
18  	 * this factory gets a <TT>LocalCommandDictionary</TT> which might be in
19  	 * a local cache.
20  	 *
21  	 * @ImplNote (by bamade) This is useful for subsystems that use many
22  	 * instances of the same class and may be more useful if we implement
23  	 *  <TT>LocalCommandDictionary</TT> classes systematically as
24  	 * <TT>CompositeCommandDictionaryBuilder</TT> that delegates to
25  	 * super-class (by limiting the way we climb up the class hierarchy to
26  	 * classes in "org.lsst.ccs" packages)
27  	 * @return
28  	 */
29  	public static synchronized LocalCommandDictionary factory(Class clazz) {
30  		return buildFor(clazz);
31  	}
32  
33  	private static LocalCommandDictionary buildFor(Class clazz) {
34  		LocalCommandDictionary res = mapBuilders.get(clazz);
35  		if (res == null) {
36  			res = new CommandDictionaryBuilder(clazz);
37  			mapBuilders.put(clazz, res);
38  		}
39  		return res;
40  
41  	}
42  
43  	/***
44  	 * converts an unparsed command into a command that can be invoked
45  	 * @param string the text to be parsed
46  	 * @param meth the Method ready to be called
47  	 * @return a <TT>RawCommand</TT> object wher real parameter can be fetched
48  	 * @throws CommandInvocationException if parsing does not behave well
49  	 */
50  	public static RawCommand toRawCommand(String string, Method meth) throws CommandInvocationException {
51  		TokenizedCommand cmd = new TokenizedCommand(string) ;
52  		return RawCommand.toRawCommand(cmd, meth, engine) ;
53  	}
54  
55  	public static RawCommand toRawCommand(TokenizedCommand cmd, Method meth) throws CommandInvocationException {
56  		return RawCommand.toRawCommand(cmd, meth, engine) ;
57  	}
58  }