View Javadoc

1   package cz.cuni.amis.pogamut.usar2004.agent.module.datatypes;
2   
3   import cz.cuni.amis.pogamut.usar2004.agent.module.configuration.SuperConfiguration;
4   import java.util.ArrayList;
5   import java.util.HashMap;
6   import java.util.List;
7   import java.util.Map;
8   
9   /**
10   * Container of configuration message representatives. It is basicly a hashmap
11   * of maps. There is geometry messages logged by type and than by name.
12   *
13   * @author vejmanm
14   */
15  public class ConfigContainer extends HashMap<String, Map<String, SuperConfiguration>>
16  {
17      /**
18       * Gets configuration message representatives from local hashmap. Returns
19       * null if none matches or this hash map is empty.
20       *
21       * @param type String representing the type of Configuration to return
22       * @return Returns List of specified type of Configuration representatives.
23       */
24      public List<SuperConfiguration> getConfigurationsByType(String type)
25      {
26          if(this.isEmpty() || type == null)
27          {
28              return null;
29          }
30          if(this.containsKey(type))
31          {
32              return new ArrayList<SuperConfiguration>(this.get(type).values());
33          }
34          return null;
35      }
36  
37      /**
38       * Iterates through local hashmap values and seeks match. Returns null if
39       * this hash map is empty. Note, that if <B>type</B> = UNKNOWN_Configuration
40       * it returns all unknown Configurations.
41       *
42       * @param type ConfigurationType representing the type of Configuration to
43       * return
44       * @return Returns List of all Configurations that suit input
45       * ConfigurationType.
46       */
47      public List<SuperConfiguration> getConfigurationsByConfigType(ConfigType type)
48      {
49          if(this.isEmpty() || type == null)
50          {
51              return null;
52          }
53          List<SuperConfiguration> list = new ArrayList<SuperConfiguration>();
54          for(Map<String, SuperConfiguration> map : this.values())
55          {
56              if(map == null || map.isEmpty())
57              {
58                  continue;
59              }
60              if(map.values().iterator().next().getConfigType() == type)
61              {
62                  list.addAll(map.values());
63              }
64          }
65          return list;
66      }
67  
68      /**
69       * Adds every object that can be casted to initial class to the output list.
70       * Note that if You feed this method with SuperClass it will return all
71       * available submodules.
72       *
73       * @param c Class representing the type of which the return list should be
74       * @return Returns a list of eligible objects, that can be casted to Class c
75       */
76      public List<SuperConfiguration> getConfigurationsByClass(Class c)
77      {
78          if(c == null || this.isEmpty())
79          {
80              return null;
81          }
82          List<SuperConfiguration> list = new ArrayList<SuperConfiguration>();
83          for(Map<String, SuperConfiguration> map : this.values())
84          {
85              for(SuperConfiguration cnf : map.values())
86              {
87                  if(c.isInstance(cnf))
88                  {
89                      list.add(cnf);
90                  }
91              }
92          }
93          return list;
94      }
95  
96      /**
97       * Gets configuration message representatives from local hashmap specified
98       * by type and by name. Returns null if none matches or this hash map is
99       * empty.
100      *
101      * @param type String representing the type of configuration to return.
102      * @param name String representing the name of configuration to return.
103      * @return Returns List of specified type of Configuration representative.
104      */
105     public SuperConfiguration getConfigurationByTypeName(String type, String name)
106     {
107         if(this.isEmpty())
108         {
109             return null;
110         }
111         if(this.containsKey(type) && !this.get(type).isEmpty())
112         {
113             return this.get(type).get(name);
114         }
115         return null;
116     }
117 
118     /**
119      * For each type of Configuration it adds all individuals to the returnee
120      * List as a couple (Type, Name)
121      *
122      * @return returns Map of couples (Type/Name) of non empty Configurations
123      */
124     public List<MessageDescriptor> getNonEmptyDescription()
125     {
126         List<MessageDescriptor> list = new ArrayList<MessageDescriptor>();
127         for(String type : this.keySet())
128         {
129             for(String name : this.get(type).keySet())
130             {
131                 list.add(new MessageDescriptor(type, name));
132             }
133         }
134         return list;
135     }
136 }