View Javadoc

1   package cz.cuni.amis.pogamut.usar2004.agent.module.datatypes;
2   
3   import cz.cuni.amis.pogamut.usar2004.agent.module.sensor.SuperSensor;
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 sensor message representatives. Note that this is equvalent with
11   * SensorsContainerQueued. The difference is that the non queued version throws
12   * out records as soon as new comes along. It writes them over. This is needed
13   * when we do not care about precision - when we want to know what is happening
14   * right now!
15   *
16   * @author vejmanm
17   */
18  public class SensorsContainer extends HashMap<String, Map<String, SuperSensor>>
19  {
20      /**
21       * Gets sensor message representatives from local hashmap. Returns null if
22       * none matches or this hash map is empty.
23       *
24       * @param type String representing the type of sensor to return.
25       * @return Returns List of specified type of Sensor module.
26       */
27      public List<SuperSensor> getSensorsByType(String type)
28      {
29          if(this.isEmpty() || type == null)
30          {
31              return null;
32          }
33          if(this.containsKey(type))
34          {
35              return new ArrayList<SuperSensor>(this.get(type).values());
36          }
37          return null;
38      }
39  
40      /**
41       * Iterates through local hashmap values and seeks match. Returns null if
42       * this hash map is empty. Note, that if <B>type</B> = UNKNOWN_SENSOR it
43       * returns all unknown sensors.
44       *
45       * @param type SensorType representing the type of sensor to return.
46       * @return Returns List of all sensors that suit input SensorType.
47       */
48      public List<SuperSensor> getSensorsBySensorType(SensorType type)
49      {
50          if(this.isEmpty() || type == null)
51          {
52              return null;
53          }
54          List<SuperSensor> list = new ArrayList<SuperSensor>();
55          for(Map<String, SuperSensor> map : this.values())
56          {
57              if(map == null || map.isEmpty())
58              {
59                  continue;
60              }
61              if(map.values().iterator().next().getSensorType() == type)
62              {
63                  list.addAll(map.values());
64              }
65          }
66          return list;
67      }
68  
69      /**
70       * Adds every object that can be casted to initial class to the output list.
71       * Note that if feeded with SuperSensor class it will return all available
72       * submodules.
73       *
74       * @param c Class representing the type of which the return list should be.
75       * @return Returns a list of eligible objects, that can be casted to Class
76       * c.
77       */
78      public List<SuperSensor> getSensorsByClass(Class c)
79      {
80          if(c == null)
81          {
82              return null;
83          }
84          List<SuperSensor> list = new ArrayList<SuperSensor>();
85          for(Map<String, SuperSensor> map : this.values())
86          {
87              for(SuperSensor sensor : map.values())
88              {
89                  if(c.isInstance(sensor))
90                  {
91                      list.add(sensor);
92                  }
93              }
94          }
95          return list;
96      }
97  
98      /**
99       * Gets sensor message representatives from local hashmap specified by type
100      * and by name. Returns null if none matches or this hash map is empty.
101      *
102      * @param type String representing the type of sensor to return.
103      * @param name String representing the name of sensor to return.
104      * @return Returns List of specified type of Sensor module.
105      */
106     public SuperSensor getSensorByTypeName(String type, String name)
107     {
108         if(this.isEmpty())
109         {
110             return null;
111         }
112         if(this.containsKey(type) && !this.get(type).isEmpty())
113         {
114             return this.get(type).get(name);
115         }
116         return null;
117     }
118 
119     /**
120      * For each type of sensor it adds all individuals to the returnee List as a
121      * couple (Type, Name)
122      *
123      * @return returns Map of couples (Type/Name) of non empty sensor
124      * representatives.
125      */
126     public List<MessageDescriptor> getNonEmptyDescription()
127     {
128         if(this.isEmpty())
129         {
130             return null;
131         }
132         List<MessageDescriptor> list = new ArrayList<MessageDescriptor>();
133         for(String type : this.keySet())
134         {
135             for(String name : this.get(type).keySet())
136             {
137                 list.add(new MessageDescriptor(type, name));
138             }
139         }
140         return list;
141     }
142 }