1
2
3
4
5
6 package org.weda.store.impl;
7
8 import java.util.HashMap;
9 import java.util.List;
10 import java.util.Map;
11 import org.weda.store.SummaryFunction;
12 import org.weda.store.SummaryFunctionRegistry;
13 import org.weda.store.SummaryFunctionRegistryException;
14
15 /**
16 *
17 * @author Mikhail Titov
18 */
19 public class SummaryFunctionRegistryImpl implements SummaryFunctionRegistry{
20 private Map<String, Class> functions;
21
22 public void init() throws SummaryFunctionRegistryException {
23 try{
24 for (Class functionClass: functions.values())
25 if (!SummaryFunction.class.isAssignableFrom(functionClass))
26 throw new SummaryFunctionRegistryException(
27 String.format(
28 "Class (%s) is not a summary function class. " +
29 "All summary functions must have %s " +
30 "as superinterface"
31 , functionClass.getName()
32 , SummaryFunction.class.getName()));
33 }catch(Exception e){
34 throw new SummaryFunctionRegistryException(
35 "Error while initializing SummaryFunctionRegistry"
36 , e);
37 }
38 }
39
40 public SummaryFunction getSummaryFunction(String name)
41 throws SummaryFunctionRegistryException
42 {
43 Class functionClass = functions.get(name);
44 if (functionClass != null)
45 try{
46 return (SummaryFunction)functionClass.newInstance();
47 }catch(Exception e){
48 throw new SummaryFunctionRegistryException(
49 String.format(
50 "Error while creating instance of function (%s)"
51 , name)
52 , e);
53 }
54 else
55 throw new SummaryFunctionRegistryException(
56 String.format(
57 "Function (%s) not registered in " +
58 "SummaryFunctionRegistry"
59 , name));
60 }
61
62 public void setFunctions(Map<String, Class> functions) {
63 this.functions = functions;
64 }
65
66 }