View Javadoc

1   package cz.cuni.amis.pogamut.usar2004.agent.module.master;
2   
3   import cz.cuni.amis.pogamut.usar2004.agent.module.configuration.SuperConfiguration;
4   import cz.cuni.amis.pogamut.usar2004.agent.module.datatypes.*;
5   import cz.cuni.amis.pogamut.usar2004.agent.module.geometry.SuperGeometry;
6   import cz.cuni.amis.pogamut.usar2004.agent.module.response.SuperResponse;
7   import cz.cuni.amis.pogamut.usar2004.agent.module.sensor.*;
8   import cz.cuni.amis.pogamut.usar2004.agent.module.state.SuperState;
9   import java.lang.reflect.Constructor;
10  
11  /**
12   * Abstract class that uses static methods to offer instances of objects
13   * specified by input variables.
14   *
15   * @author vejmanm
16   */
17  public abstract class ModuleInstanceProvider
18  {
19      /**
20       * Class should be valid Sensor representative - SuperSensor offspring. Such
21       * class gets particular ctor and creates new instance which is returned.
22       *
23       * @param clazz Class representing possible valid Sensor
24       * @return Returns Class instance relevant to input Class
25       */
26      public static SuperSensor getSensorInstanceByClass(Class clazz)
27      {
28          try
29          {
30              Constructor ctor = clazz.getConstructor();
31              SuperSensor instance = (SuperSensor) ctor.newInstance();
32              return instance;
33          }
34          catch(Exception e)
35          {
36              System.out.println(e.getMessage() + " " + e.toString());
37              return null;
38          }
39      }
40  
41      /**
42       * Asks SensorType enum if it knows SensorType represented by string
43       * <B>type</B>. If it does, it also contains Class reference. This reference
44       * is then instantiated and returned. If it does not, it returns instance of
45       * base class SuperSensor which is represented by SensorType.UNKNOWN_SENSOR.
46       *
47       * @param type String representing possible valid SensorType.
48       * @return Returns Class instance relevant to input String.
49       */
50      public static SuperSensor getSensorInstanceByType(String type)
51      {
52          SensorType senType = SensorType.getType(type);
53  
54          try
55          {
56              Class clazz = senType.getModuleClass();
57              Constructor ctor = clazz.getConstructor();
58              SuperSensor instance = (SuperSensor) ctor.newInstance();
59              return instance;
60          }
61          catch(Exception e)
62          {
63              System.out.println(e.getMessage() + " " + e.toString());
64              return null;
65          }
66      }
67  
68      /**
69       * Asks GeometryType enum if it knows GeometryType represented by string
70       * <B>type</B>. If it does, it also contains Class reference. This reference
71       * is then instantiated and returned. If it does not, it returns instance of
72       * base class SuperGeometry which is represented by
73       * GeometryType.SENSOR_EFFECTER.
74       *
75       * @param type String representing possible valid GeometryType.
76       * @return Returns Class instance relevant to input String.
77       */
78      public static SuperGeometry getGeometryInstanceByType(String type)
79      {
80          GeometryType geoType = GeometryType.getType(type);
81  
82          try
83          {
84              Class clazz = geoType.getModuleClass();
85              Constructor ctor = clazz.getConstructor();
86              SuperGeometry instance = (SuperGeometry) ctor.newInstance();
87              return instance;
88          }
89          catch(Exception e)
90          {
91              System.out.println(e.getMessage() + " " + e.toString());
92              return null;
93          }
94      }
95  
96      /**
97       * Asks ConfigType enum if it knows ConfigType represented by string
98       * <B>type</B>. If it does, it also contains Class reference. This reference
99       * is then instantiated and returned. If it does not, it returns instance of
100      * base class SuperConfiguration which is represented by ConfigType.SENSOR.
101      *
102      * @param type String representing possible valid ConfigType.
103      * @return Returns Class instance relevant to input String.
104      */
105     public static SuperConfiguration getConfigInstanceByType(String type)
106     {
107         ConfigType confType = ConfigType.getType(type);
108 
109         try
110         {
111             Class clazz = confType.getModuleClass();
112             Constructor ctor = clazz.getConstructor();
113             SuperConfiguration instance = (SuperConfiguration) ctor.newInstance();
114             return instance;
115         }
116         catch(Exception e)
117         {
118             System.out.println(e.getMessage() + " " + e.toString());
119             return null;
120         }
121     }
122 
123     /**
124      * Asks ResponseType enum if it knows ResponseType represented by string
125      * <B>type</B>. If it does, it also contains Class reference. This reference
126      * is then instantiated and returned. If it does not, it returns instance of
127      * ResponseSensorEffecter which is represented by
128      * ResponseType.SENSOR_EFFECTER.
129      *
130      * @param type String representing possible valid ResponseType.
131      * @return Returns Class instance relevant to input String.
132      */
133     public static SuperResponse getResponseInstanceByType(String type)
134     {
135         ResponseType resType = ResponseType.getType(type);
136 
137         try
138         {
139             Class clazz = resType.getModuleClass();
140             Constructor ctor = clazz.getConstructor();
141             SuperResponse instance = (SuperResponse) ctor.newInstance();
142             return instance;
143         }
144         catch(Exception e)
145         {
146             System.out.println(e.getMessage() + " " + e.toString());
147             return null;
148         }
149     }
150 
151     /**
152      * Asks VehicleType (enum) if it knows VehicleType represented by string
153      * <B>type</B>. If it does, it also contains Class reference. This reference
154      * is then instantiated and returned. If it does not, it returns instance of
155      * SuperState which is represented by VehicleType.UNKNOW.
156      *
157      * @param type String representing possible valid VehicleType.
158      * @return Returns Class instance relevant to input String.
159      */
160     public static SuperState getStateInstanceByType(String type)
161     {
162         VehicleType resType = VehicleType.getType(type);
163 
164         try
165         {
166             Class clazz = resType.getModuleClass();
167             Constructor ctor = clazz.getConstructor();
168             SuperState instance = (SuperState) ctor.newInstance();
169             return instance;
170         }
171         catch(Exception e)
172         {
173             System.out.println(e.getMessage() + " " + e.toString());
174             return null;
175         }
176     }
177 }