View Javadoc

1   /**
2    * 2007, Digitalis Informatica. All rights reserved. Distribuicao e Gestao de Informatica, Lda. Estrada de Paco de Arcos
3    * num.9 - Piso -1 2780-666 Paco de Arcos Telefone: (351) 21 4408990 Fax: (351) 21 4408999 http://www.digitalis.pt
4    */
5   package pt.digitalis.dif.dem.objects.parameters;
6   
7   import java.util.HashMap;
8   import java.util.Map;
9   
10  import pt.digitalis.dif.exception.objects.ParameterException;
11  
12  /**
13   * A list of parameters
14   * 
15   * @author Pedro Viegas <a href="mailto:pviegas@digitalis.pt">pviegas@digitalis.pt</a>
16   * @created Nov 22, 2007
17   */
18  public class ParameterList {
19  
20      /** the parameters */
21      @SuppressWarnings({"rawtypes"})
22      private Map<String, IParameter> parameters = new HashMap<String, IParameter>();
23  
24      /**
25       * Adds a parameter to the list
26       * 
27       * @param parameter
28       *            the parameter to add
29       */
30      public void addParameter(IParameter<?> parameter)
31      {
32          parameters.put(parameter.getId(), parameter);
33      }
34  
35      /**
36       * Adds a parameter list to the current one
37       * 
38       * @param list
39       *            the parameter list to add
40       * @return the object with the new list added
41       */
42      public ParameterList addParameters(ParameterList list)
43      {
44          parameters.putAll(list.getParameters());
45  
46          return this;
47      }
48  
49      /**
50       * Creates a new instance of the current object
51       * 
52       * @return {@link ParameterList} clone
53       * @throws CloneNotSupportedException
54       */
55      public ParameterList cloneMe() throws CloneNotSupportedException
56      {
57          ParameterList parameterListObj = new ParameterList();
58  
59          for (String key: parameters.keySet())
60              parameterListObj.parameters.put(key, parameters.get(key).clone());
61  
62          return parameterListObj;
63      }
64  
65      /**
66       * Gets a parameter by it's id
67       * 
68       * @param id
69       *            the id to search
70       * @return the parameter
71       */
72      public IParameter<?> getParameter(String id)
73      {
74          if (id != null)
75              id = id.toLowerCase();
76  
77          return parameters.get(id);
78      }
79  
80      /**
81       * Gets a parameter by it's id
82       * 
83       * @param <T>
84       *            the type of the parameter
85       * @param id
86       *            the id to search
87       * @param clazz
88       *            the class of the parameter
89       * @return the parameter
90       */
91      @SuppressWarnings("unchecked")
92      public <T> IParameter<T> getParameter(String id, Class<T> clazz)
93      {
94          return parameters.get(id.toLowerCase());
95      }
96  
97      /**
98       * @return the parameters
99       */
100     @SuppressWarnings("rawtypes")
101     public Map<String, IParameter> getParameters()
102     {
103         return parameters;
104     }
105 
106     /**
107      * Updates a parameter in the list. If it does not exist it is added to the list
108      * 
109      * @param parameter
110      *            the parameter to update
111      * @throws ParameterException
112      *             if the parameter cannot be updated
113      */
114     @SuppressWarnings({"rawtypes", "unchecked"})
115     public void updatesParameter(IParameter parameter) throws ParameterException
116     {
117         IParameter old = parameters.get(parameter.getId());
118 
119         if (old == null)
120             addParameter(parameter);
121         else
122             old.setValue(parameter.getValue(null), null);
123     }
124 }