KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > versant > core > metadata > MDStaticUtils


1
2 /*
3  * Copyright (c) 1998 - 2005 Versant Corporation
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  * Versant Corporation - initial API and implementation
11  */

12 package com.versant.core.metadata;
13
14 import com.versant.core.util.IntObjectHashMap;
15
16 import java.math.BigDecimal JavaDoc;
17 import java.math.BigInteger JavaDoc;
18 import java.util.*;
19
20
21 /**
22  * Static utility methods to convert statics to and from Strings.
23  */

24 public class MDStaticUtils implements MDStatics {
25
26     public static final int[] primToNumberMapping = new int[]{
27         -1,
28         BOOLEANW,
29         BYTEW,
30         SHORTW,
31         INTW,
32         LONGW,
33         FLOATW,
34         DOUBLEW,
35         CHAR,
36         BOOLEANW,
37         BYTEW,
38         SHORTW,
39         INTW,
40         LONGW,
41         FLOATW,
42         DOUBLEW,
43         CHARW,
44         STRING,
45         BIGDECIMAL,
46         BIGINTEGER,
47         DATE,
48         LOCALE,
49     };
50
51     protected static final Map TYPE_MAP = new HashMap();
52     protected static final Map TYPE_CODE_MAP = new HashMap();
53     private static final IntObjectHashMap CODE_TYPE_MAP = new IntObjectHashMap();
54     private static final Map TYPE_NAME_MAP = new HashMap();
55
56     private static void put(String JavaDoc name, Class JavaDoc t, int code) {
57         TYPE_MAP.put(name, t);
58         TYPE_NAME_MAP.put(t, name);
59         TYPE_CODE_MAP.put(t, new Integer JavaDoc(code));
60         CODE_TYPE_MAP.put(code, name);
61     }
62
63     private static void put(String JavaDoc name, String JavaDoc name2, Class JavaDoc t, int code) {
64         TYPE_MAP.put(name2, t);
65         put(name, t, code);
66     }
67
68     static {
69         put("boolean", Boolean.TYPE, BOOLEAN);
70         put("Boolean", "java.lang.Boolean", Boolean JavaDoc.class, BOOLEANW);
71         put("byte", Byte.TYPE, BYTE);
72         put("Byte", "java.lang.Byte", Byte JavaDoc.class, BYTEW);
73         put("short", Short.TYPE, SHORT);
74         put("Short", "java.lang.Short", Short JavaDoc.class, SHORTW);
75         put("int", Integer.TYPE, INT);
76         put("Integer", "java.lang.Integer", Integer JavaDoc.class, INTW);
77         put("long", Long.TYPE, LONG);
78         put("Long", "java.lang.Long", Long JavaDoc.class, LONGW);
79         put("float", Float.TYPE, FLOAT);
80         put("Float", "java.lang.Float", Float JavaDoc.class, FLOATW);
81         put("double", Double.TYPE, DOUBLE);
82         put("Double", "java.lang.Double", Double JavaDoc.class, DOUBLEW);
83         put("char", Character.TYPE, CHAR);
84         put("Character", "java.lang.Character", Character JavaDoc.class, CHARW);
85
86         put("String", "java.lang.String", String JavaDoc.class, STRING);
87         put("BigDecimal", "java.math.BigDecimal", BigDecimal JavaDoc.class, BIGDECIMAL);
88         put("BigInteger", "java.math.BigInteger", BigInteger JavaDoc.class, BIGINTEGER);
89         put("Date", "java.util.Date", Date.class, DATE);
90         put("Locale", "java.util.Locale", Locale.class, LOCALE);
91
92         //Collections
93
put("Collection", "java.util.Collection", java.util.Collection JavaDoc.class,
94                 COLLECTION);
95         put("List", "java.util.List", java.util.List JavaDoc.class, LIST);
96         put("ArrayList", "java.util.ArrayList", java.util.ArrayList JavaDoc.class,
97                 ARRAYLIST);
98         put("LinkedList", "java.util.LinkedList", java.util.LinkedList JavaDoc.class,
99                 LINKEDLIST);
100         put("Vector", "java.util.Vector", java.util.Vector JavaDoc.class, VECTOR);
101
102         put("Set", "java.util.Set", java.util.Set JavaDoc.class, SET);
103         put("HashSet", "java.util.HashSet", java.util.HashSet JavaDoc.class, HASHSET);
104         put("TreeSet", "java.util.TreeSet", java.util.TreeSet JavaDoc.class, TREESET);
105         put("SortedSet", "java.util.SortedSet", java.util.SortedSet JavaDoc.class,
106                 SORTEDSET);
107
108
109
110         //Maps
111
put("Map", "java.util.Map", java.util.Map JavaDoc.class, MAP);
112         put("HashMap", "java.util.HashMap", java.util.HashMap JavaDoc.class, HASHMAP);
113         put("TreeMap", "java.util.TreeMap", java.util.TreeMap JavaDoc.class, TREEMAP);
114         put("SortedMap", "java.util.SortedMap", java.util.SortedMap JavaDoc.class,
115                 SORTEDMAP);
116         put("Hashtable", "java.util.Hashtable", java.util.Hashtable JavaDoc.class,
117                 HASHTABLE);
118     }
119
120     protected MDStaticUtils() {
121     }
122
123     /**
124      * Convert the name of a simple class (int, Integer, String etc.) into
125      * its Class object or null if not found.
126      */

127     public static final Class JavaDoc toSimpleClass(String JavaDoc name) {
128         return (Class JavaDoc)TYPE_MAP.get(name);
129     }
130
131     /**
132      * Convert a simple class (Integer.TYPE, Integer.class, String.class etc.)
133      * into a type code (INT, INTW, STRING etc.) or less than 0 if not found.
134      */

135     public static final int toTypeCode(Class JavaDoc cls) {
136         
137         Integer JavaDoc i = (Integer JavaDoc)TYPE_CODE_MAP.get(cls);
138         return i == null ? -1 : i.intValue();
139     }
140
141     public static String JavaDoc toSimpleName(Class JavaDoc cls) {
142         return (String JavaDoc)TYPE_NAME_MAP.get(cls);
143     }
144
145     /**
146      * Return the simple name for a type code.
147      *
148      * @param code
149      * @return
150      */

151     public static String JavaDoc toSimpleName(int code) {
152         String JavaDoc name = (String JavaDoc)CODE_TYPE_MAP.get(code);
153         return name;
154     }
155
156     public static String JavaDoc toTriStateString(int t) {
157         switch (t) {
158             case NOT_SET:
159                 return "notset";
160             case TRUE:
161                 return "true";
162             case FALSE:
163                 return "false";
164         }
165         return "unknown(" + t + ")";
166     }
167
168     public static String JavaDoc toIdentityTypeString(int identityType) {
169         switch (identityType) {
170             case IDENTITY_TYPE_APPLICATION:
171                 return "application";
172             case IDENTITY_TYPE_DATASTORE:
173                 return "datastore";
174             case IDENTITY_TYPE_NONDURABLE:
175                 return "none";
176         }
177         return "unknown(" + identityType + ")";
178     }
179
180     public static String JavaDoc toPersistenceModifierString(int pm) {
181         switch (pm) {
182             case PERSISTENCE_MODIFIER_PERSISTENT:
183                 return "persistent";
184             case PERSISTENCE_MODIFIER_TRANSACTIONAL:
185                 return "transactional";
186             case PERSISTENCE_MODIFIER_NONE:
187                 return "none";
188             case NOT_SET:
189                 return "";
190         }
191         return "unknown(" + pm + ")";
192     }
193
194     public static String JavaDoc toNullValueString(int nullValue) {
195         switch (nullValue) {
196             case NULL_VALUE_EXCEPTION:
197                 return "exception";
198             case NULL_VALUE_DEFAULT:
199                 return "default";
200             case NULL_VALUE_NONE:
201                 return "none";
202         }
203         return "unknown(" + nullValue + ")";
204     }
205
206     public static String JavaDoc toCategoryString(int category) {
207         switch (category) {
208             case CATEGORY_SIMPLE:
209                 return "Simple";
210             case CATEGORY_REF:
211                 return "Ref";
212             case CATEGORY_POLYREF:
213                 return "PolyRef";
214             case CATEGORY_COLLECTION:
215                 return "Collection";
216             case CATEGORY_ARRAY:
217                 return "Array";
218             case CATEGORY_MAP:
219                 return "Map";
220             case CATEGORY_TRANSACTIONAL:
221                 return "Transactional";
222             case CATEGORY_NONE:
223                 return "None";
224             case CATEGORY_DATASTORE_PK:
225                 return "Datastore Pk";
226             case CATEGORY_OPT_LOCKING:
227                 return "Opt Locking";
228             case CATEGORY_CLASS_ID:
229                 return "Class Id";
230             case CATEGORY_EXTERNALIZED:
231                 return "Externalized";
232         }
233         return "unknown(" + category + ")";
234     }
235
236     public static String JavaDoc toAutoSetString(int autoSet) {
237         switch (autoSet) {
238             case AUTOSET_NO:
239                 return "NONE";
240             case AUTOSET_CREATED:
241                 return "CREATED";
242             case AUTOSET_MODIFIED:
243                 return "MODIFIED";
244             case AUTOSET_BOTH:
245                 return "BOTH";
246         }
247         return "unknown(" + autoSet + ")";
248     }
249
250     public static String JavaDoc toCacheString(int cache) {
251         switch (cache) {
252             case CACHE_STRATEGY_NO:
253                 return "no";
254             case CACHE_STRATEGY_YES:
255                 return "yes";
256             case CACHE_STRATEGY_ALL:
257                 return "all";
258         }
259         return "unknown(" + cache + ")";
260     }
261
262     public static final boolean isIntegerType(int type) {
263         return isSignedIntegerType(type)
264
265                 ;
266     }
267
268     public static final boolean isSignedIntegerType(int type) {
269         if ((type >= BOOLEAN && type <= LONGW)
270                 || type == CHARW
271         ) {
272             return true;
273         } else {
274             return false;
275         }
276     }
277
278
279
280 }
281
282
Popular Tags