View Javadoc

1   /*
2      Copyright 2008 Ramon Servadei
3   
4      Licensed under the Apache License, Version 2.0 (the "License");
5      you may not use this file except in compliance with the License.
6      You may obtain a copy of the License at
7   
8          http://www.apache.org/licenses/LICENSE-2.0
9   
10     Unless required by applicable law or agreed to in writing, software
11     distributed under the License is distributed on an "AS IS" BASIS,
12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13     See the License for the specific language governing permissions and
14     limitations under the License.
15   */
16  package fulmine.model.field;
17  
18  import static fulmine.util.Utils.DOT;
19  import static fulmine.util.Utils.safeToString;
20  
21  /**
22   * Provides utility methods for working with {@link IField} instances
23   * 
24   * @author Ramon Servadei
25   */
26  public abstract class FieldUtils
27  {
28  
29      /**
30       * The name of a field that is created from a native type.
31       * 
32       * @see #createFromNative(Object)
33       */
34      public static final String CREATE_FROM_NATIVE = "createFromNative";
35  
36      /**
37       * Create an instance of an {@link IField} using the parameters passed in.
38       * The (String, String) constructor for the field class is located and
39       * invoked.
40       * 
41       * @param <T>
42       * @param field
43       *            the field {@link Class}
44       * @param fieldName
45       *            the field name
46       * @param fieldValue
47       *            the field value
48       * @return the field instance
49       */
50      @SuppressWarnings("unchecked")
51      public static <T extends IField> T createFromString(
52          Class<? extends IField> field, String fieldName, String fieldValue)
53      {
54          try
55          {
56              return (T) field.getConstructor(String.class, String.class).newInstance(
57                  fieldName, fieldValue);
58          }
59          catch (Exception e)
60          {
61              throw new IllegalArgumentException(
62                  "Could not construct an instance of " + field + " with name="
63                      + fieldName + ", value=" + fieldValue, e);
64          }
65      }
66  
67      /**
68       * Create an instance of an {@link IField} using the simple class name and
69       * the name for the field
70       * 
71       * @param <T>
72       * @param simpleClassName
73       *            the simple class name, e.g. StringField
74       * @param fieldName
75       *            the name for the field
76       * @return the field
77       */
78      @SuppressWarnings("unchecked")
79      public static <T extends IField> T createFromString(String simpleClassName,
80          String fieldName)
81      {
82          try
83          {
84              String packageName =
85                  AbstractField.class.getPackage().getName() + DOT;
86              final Class<?> fieldClass =
87                  Class.forName(packageName + simpleClassName);
88              return (T) fieldClass.getConstructor(String.class).newInstance(
89                  fieldName);
90          }
91          catch (Exception e)
92          {
93              throw new IllegalArgumentException(
94                  "Could not construct an instance of " + simpleClassName
95                      + " with name=" + fieldName, e);
96          }
97      }
98  
99      /**
100      * Create an instance of an {@link IField} using the simple class name and
101      * the name for the field
102      * 
103      * @param <T>
104      * @param simpleClassName
105      *            the simple class name, e.g. StringField
106      * @param fieldName
107      *            the name for the field
108      * @param application
109      *            the application code for the field
110      * @param permission
111      *            the permission code for the field
112      * @return the field
113      */
114     @SuppressWarnings({ "unchecked", "boxing" })
115     public static <T extends IField> T createFromString(String simpleClassName,
116         String fieldName, byte application, short permission)
117     {
118         try
119         {
120             String packageName =
121                 AbstractField.class.getPackage().getName() + DOT;
122             final Class<?> fieldClass =
123                 Class.forName(packageName + simpleClassName);
124             return (T) fieldClass.getConstructor(String.class, Byte.TYPE,
125                 Short.TYPE).newInstance(fieldName, application, permission);
126         }
127         catch (Exception e)
128         {
129             throw new IllegalArgumentException(
130                 "Could not construct an instance of " + simpleClassName
131                     + " with name=" + fieldName, e);
132         }
133     }
134 
135     /**
136      * Convert an object from its native object to a framework object
137      * 
138      * @param nativeObject
139      *            the object to convert
140      * @return a framework object
141      * @throws IllegalArgumentException
142      *             if the native object is not supported
143      */
144     @SuppressWarnings("boxing")
145     public static IField createFromNative(Object nativeObject)
146     {
147         if (nativeObject == null)
148         {
149             return null;
150         }
151         if (nativeObject instanceof String)
152         {
153             return new StringField(CREATE_FROM_NATIVE, (String) nativeObject);
154         }
155         if (nativeObject instanceof Boolean)
156         {
157             return new BooleanField(CREATE_FROM_NATIVE, (Boolean) nativeObject);
158         }
159         if (nativeObject instanceof Integer)
160         {
161             return new IntegerField(CREATE_FROM_NATIVE, (Integer) nativeObject);
162         }
163         if (nativeObject instanceof Double)
164         {
165             return new DoubleField(CREATE_FROM_NATIVE, (Double) nativeObject);
166         }
167         if (nativeObject instanceof Long)
168         {
169             return new LongField(CREATE_FROM_NATIVE, (Long) nativeObject);
170         }
171         if (nativeObject instanceof Float)
172         {
173             return new FloatField(CREATE_FROM_NATIVE, (Float) nativeObject);
174         }
175         throw new IllegalArgumentException("Unhandled type "
176             + safeToString(nativeObject.getClass()) + " value="
177             + safeToString(nativeObject));
178     }
179 
180     /**
181      * Convert the native class to the framework class
182      * 
183      * @param nativeType
184      *            the native class type
185      * @return the framework class
186      * @throws IllegalArgumentException
187      *             if the native class is not compatible with the framework
188      */
189     public static Class<? extends IField> getFrameworkClass(Class<?> nativeType)
190     {
191         if (nativeType == null)
192         {
193             return null;
194         }
195         if (String.class.equals(nativeType))
196         {
197             return StringField.class;
198         }
199         if (Boolean.class.equals(nativeType) || Boolean.TYPE.equals(nativeType))
200         {
201             return BooleanField.class;
202         }
203         if (Integer.class.equals(nativeType) || Integer.TYPE.equals(nativeType))
204         {
205             return IntegerField.class;
206         }
207         if (Double.class.equals(nativeType) || Double.TYPE.equals(nativeType))
208         {
209             return DoubleField.class;
210         }
211         if (Long.class.equals(nativeType) || Long.TYPE.equals(nativeType))
212         {
213             return LongField.class;
214         }
215         if (Float.class.equals(nativeType) || Float.TYPE.equals(nativeType))
216         {
217             return FloatField.class;
218         }
219         throw new IllegalArgumentException("Unhandled type "
220             + safeToString(nativeType));
221     }
222 }