View Javadoc

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