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.*;
5   
6   /**
7    * Container of sensor message representatives. Note that this is equvalent with
8    * SensorsContainer. The difference is that the Queued version does not throw
9    * out records until it is requiered to do so. It lines them up to lists. This
10   * is needed when we want the best quality readings and regular ticks are not
11   * enough to keep up with the speed of sensor messages being recieved.
12   *
13   * @author vejmanm
14   */
15  public class SensorsContainerQueued extends HashMap<String, Map<String, Queue<SuperSensor>>>
16  {
17      /**
18       * Gets sensor 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 sensor to return.
22       * @return Returns List of specified type of Sensor module.
23       */
24      public List<SuperSensor> getSensorsByType(String type)
25      {
26          if(this.isEmpty() || type == null)
27          {
28              return null;
29          }
30          if(this.containsKey(type))
31          {
32              List<SuperSensor> allAvailible = new ArrayList<SuperSensor>();
33              for(String string : this.get(type).keySet())
34              {
35                  allAvailible.addAll(this.get(type).get(string));
36                  this.get(type).get(string).clear();
37              }
38              return allAvailible;
39          }
40          return null;
41      }
42  
43      /**
44       * Iterates through local hashmap values and seeks match. Returns null if
45       * this hash map is empty. Note, that if <B>type</B> = UNKNOWN_SENSOR it
46       * returns all unknown sensors.
47       *
48       * @param type SensorType representing the type of sensor to return.
49       * @return Returns List of all sensors that suit input SensorType.
50       */
51      public List<SuperSensor> getSensorsBySensorType(SensorType type)
52      {
53          if(this.isEmpty() || type == null)
54          {
55              return null;
56          }
57          List<SuperSensor> allAvailible = new ArrayList<SuperSensor>();
58          for(Map<String, Queue<SuperSensor>> map : this.values())
59          {
60              if(map == null || map.isEmpty())
61              {
62                  continue;
63              }
64              for(String string : map.keySet())
65              {
66                  if(map.get(string).isEmpty())
67                  {
68                      continue;
69                  }
70                  if(map.get(string).peek().getSensorType() == type)
71                  {
72                      allAvailible.addAll(map.get(string));
73                      map.get(string).clear();
74                  }
75              }
76          }
77          return allAvailible;
78      }
79  
80      /**
81       * Adds every object that can be casted to initial class to the output list.
82       * Note that if feeded with SuperSensor class it will return all available
83       * submodules.
84       *
85       * @param c Class representing the type of which the return list should be.
86       * @return Returns a list of eligible objects, that can be casted to Class
87       * c.
88       */
89      public List<SuperSensor> getSensorsByClass(Class c)
90      {
91          if(c == null)
92          {
93              return null;
94          }
95          List<SuperSensor> allAvailible = new ArrayList<SuperSensor>();
96          for(Map<String, Queue<SuperSensor>> map : this.values())
97          {
98              for(Queue<SuperSensor> queue : map.values())
99              {
100                 if(queue.isEmpty())
101                 {
102                     continue;
103                 }
104                 if(c.isInstance(queue.peek()))
105                 {
106                     allAvailible.addAll(queue);
107                     queue.clear();
108                 }
109             }
110         }
111         return allAvailible;
112     }
113 
114     /**
115      * Gets sensor message representatives from local hashmap specified by type
116      * and by name. Returns null if none matches or this hash map is empty.
117      *
118      * @param type String representing the type of sensor to return.
119      * @param name String representing the name of sensor to return.
120      * @return Returns List of specified type of Sensor module.
121      */
122     public List<SuperSensor> getSensorByTypeName(String type, String name)
123     {
124         if(this.isEmpty())
125         {
126             return null;
127         }
128         if(this.containsKey(type) && !this.get(type).isEmpty())
129         {
130             List<SuperSensor> allAvailible = new ArrayList<SuperSensor>();
131             allAvailible.addAll(this.get(type).get(name));
132             this.get(type).get(name).clear();
133         }
134         return null;
135     }
136 
137     /**
138      * For each type of sensor it adds all individuals to the returnee List as a
139      * couple (Type, Name)
140      *
141      * @return returns Map of couples (Type/Name) of non empty sensor
142      * representatives.
143      */
144     public List<MessageDescriptor> getNonEmptyDescription()
145     {
146         if(this.isEmpty())
147         {
148             return null;
149         }
150         List<MessageDescriptor> list = new ArrayList<MessageDescriptor>();
151         for(String type : this.keySet())
152         {
153             for(String name : this.get(type).keySet())
154             {
155                 list.add(new MessageDescriptor(type, name));
156             }
157         }
158         return list;
159     }
160 
161     /**
162      * Iterates through local hashmap and seeks for the specified sensor type.
163      * If found and is not empty returns true. False otherwise.
164      *
165      * @param type SensorType is the type of sensor we want to be able to work
166      * with.
167      * @return Returns true if map of specified sensor type is not null nor
168      * empty. False otherwise.
169      */
170     public boolean isReady(SensorType type)
171     {
172         if(this.isEmpty())
173         {
174             return false;
175         }
176         for(Map<String, Queue<SuperSensor>> map : this.values())
177         {
178             if(map == null || map.isEmpty())
179             {
180                 continue;
181             }
182             for(String string : map.keySet())
183             {
184                 if(map.get(string).isEmpty())
185                 {
186                     continue;
187                 }
188                 if(map.get(string).peek().getSensorType() == type)
189                 {
190                     return true;
191                 }
192             }
193         }
194         return false;
195     }
196 }