KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sleepycat > persist > impl > SimpleCatalog


1 /*-
2  * See the file LICENSE for redistribution information.
3  *
4  * Copyright (c) 2002,2006 Oracle. All rights reserved.
5  *
6  * $Id: SimpleCatalog.java,v 1.19 2006/11/14 23:30:49 mark Exp $
7  */

8
9 package com.sleepycat.persist.impl;
10
11 import java.util.ArrayList JavaDoc;
12 import java.util.HashMap JavaDoc;
13 import java.util.IdentityHashMap JavaDoc;
14 import java.util.List JavaDoc;
15 import java.util.Map JavaDoc;
16 import java.util.NoSuchElementException JavaDoc;
17
18 import com.sleepycat.persist.raw.RawObject;
19
20 /**
21  * A static catalog containing simple types only. Once created, this catalog
22  * is immutable.
23  *
24  * For bindings accessed by a PersistComparator during recovery, the
25  * SimpleCatalog provides formats for all simple types. To reduce redundant
26  * format objects, the SimpleCatalog's formats are copied when creating a
27  * regular PersistCatalog.
28  *
29  * This class also contains utility methods for dealing with primitives.
30  *
31  * @author Mark Hayes
32  */

33 public class SimpleCatalog implements Catalog {
34
35     private static final Map JavaDoc<String JavaDoc,Class JavaDoc> keywordToPrimitive;
36     static {
37         keywordToPrimitive = new HashMap JavaDoc<String JavaDoc,Class JavaDoc>(8);
38         keywordToPrimitive.put("boolean", Boolean.TYPE);
39         keywordToPrimitive.put("char", Character.TYPE);
40         keywordToPrimitive.put("byte", Byte.TYPE);
41         keywordToPrimitive.put("short", Short.TYPE);
42         keywordToPrimitive.put("int", Integer.TYPE);
43         keywordToPrimitive.put("long", Long.TYPE);
44         keywordToPrimitive.put("float", Float.TYPE);
45         keywordToPrimitive.put("double", Double.TYPE);
46     }
47
48     private static final Map JavaDoc<Class JavaDoc,Class JavaDoc> primitiveTypeToWrapper;
49     static {
50         primitiveTypeToWrapper = new HashMap JavaDoc<Class JavaDoc,Class JavaDoc>(8);
51         primitiveTypeToWrapper.put(Boolean.TYPE, Boolean JavaDoc.class);
52         primitiveTypeToWrapper.put(Character.TYPE, Character JavaDoc.class);
53         primitiveTypeToWrapper.put(Byte.TYPE, Byte JavaDoc.class);
54         primitiveTypeToWrapper.put(Short.TYPE, Short JavaDoc.class);
55         primitiveTypeToWrapper.put(Integer.TYPE, Integer JavaDoc.class);
56         primitiveTypeToWrapper.put(Long.TYPE, Long JavaDoc.class);
57         primitiveTypeToWrapper.put(Float.TYPE, Float JavaDoc.class);
58         primitiveTypeToWrapper.put(Double.TYPE, Double JavaDoc.class);
59     }
60
61     private static final SimpleCatalog instance = new SimpleCatalog();
62
63     static SimpleCatalog getInstance() {
64         return instance;
65     }
66
67     static boolean isSimpleType(Class JavaDoc type) {
68         return instance.formatMap.containsKey(type.getName());
69     }
70
71     static Class JavaDoc primitiveToWrapper(Class JavaDoc type) {
72         Class JavaDoc cls = primitiveTypeToWrapper.get(type);
73         if (cls == null) {
74             throw new IllegalStateException JavaDoc(type.getName());
75         }
76         return cls;
77     }
78
79     public static Class JavaDoc keyClassForName(String JavaDoc className) {
80         Class JavaDoc cls = keywordToPrimitive.get(className);
81         if (cls != null) {
82             cls = primitiveTypeToWrapper.get(cls);
83         } else {
84             try {
85                 cls = Class.forName(className);
86             } catch (ClassNotFoundException JavaDoc e) {
87                 throw new IllegalArgumentException JavaDoc
88                     ("Key class not found: " + className);
89             }
90         }
91         return cls;
92     }
93
94     public static String JavaDoc keyClassName(String JavaDoc className) {
95         Class JavaDoc cls = keywordToPrimitive.get(className);
96         if (cls != null) {
97             cls = primitiveTypeToWrapper.get(cls);
98             return cls.getName();
99         } else {
100             return className;
101         }
102     }
103
104     public static Class JavaDoc classForName(String JavaDoc className)
105         throws ClassNotFoundException JavaDoc {
106
107         Class JavaDoc cls = keywordToPrimitive.get(className);
108         if (cls == null) {
109             cls = Class.forName(className);
110         }
111         return cls;
112     }
113
114     static SimpleFormat getSimpleFormat(Class JavaDoc type) {
115         return instance.formatMap.get(type.getName());
116     }
117
118     static List JavaDoc<Format> copyFormatList() {
119         return new ArrayList JavaDoc<Format>(instance.formatList);
120     }
121
122     static boolean copyMissingFormats(List JavaDoc<Format> copyToList) {
123         boolean anyCopied = false;
124         for (int i = 0; i <= Format.ID_PREDEFINED; i += 1) {
125             Format thisFormat = instance.formatList.get(i);
126             Format otherFormat = copyToList.get(i);
127             if (thisFormat != null && otherFormat == null) {
128                 copyToList.set(i, thisFormat);
129                 anyCopied = true;
130             }
131         }
132         return anyCopied;
133     }
134
135     private List JavaDoc<SimpleFormat> formatList;
136     private Map JavaDoc<String JavaDoc,SimpleFormat> formatMap;
137
138     private SimpleCatalog() {
139
140         /*
141          * Reserve slots for all predefined IDs, so that that next ID assigned
142          * will be Format.ID_PREDEFINED plus one.
143          */

144         int initCapacity = Format.ID_PREDEFINED * 2;
145         formatList = new ArrayList JavaDoc<SimpleFormat>(initCapacity);
146         formatMap = new HashMap JavaDoc<String JavaDoc,SimpleFormat>(initCapacity);
147
148         for (int i = 0; i <= Format.ID_PREDEFINED; i += 1) {
149             formatList.add(null);
150         }
151
152         /* Initialize all predefined formats. */
153         setFormat(Format.ID_BOOL, new SimpleFormat.FBool(true));
154         setFormat(Format.ID_BOOL_W, new SimpleFormat.FBool(false));
155         setFormat(Format.ID_BYTE, new SimpleFormat.FByte(true));
156         setFormat(Format.ID_BYTE_W, new SimpleFormat.FByte(false));
157         setFormat(Format.ID_SHORT, new SimpleFormat.FShort(true));
158         setFormat(Format.ID_SHORT_W, new SimpleFormat.FShort(false));
159         setFormat(Format.ID_INT, new SimpleFormat.FInt(true));
160         setFormat(Format.ID_INT_W, new SimpleFormat.FInt(false));
161         setFormat(Format.ID_LONG, new SimpleFormat.FLong(true));
162         setFormat(Format.ID_LONG_W, new SimpleFormat.FLong(false));
163         setFormat(Format.ID_FLOAT, new SimpleFormat.FFloat(true));
164         setFormat(Format.ID_FLOAT_W, new SimpleFormat.FFloat(false));
165         setFormat(Format.ID_DOUBLE, new SimpleFormat.FDouble(true));
166         setFormat(Format.ID_DOUBLE_W, new SimpleFormat.FDouble(false));
167         setFormat(Format.ID_CHAR, new SimpleFormat.FChar(true));
168         setFormat(Format.ID_CHAR_W, new SimpleFormat.FChar(false));
169         setFormat(Format.ID_STRING, new SimpleFormat.FString());
170         setFormat(Format.ID_BIGINT, new SimpleFormat.FBigInt());
171         /*
172         setFormat(Format.ID_BIGDEC, new SimpleFormat.FBigDec());
173         */

174         setFormat(Format.ID_DATE, new SimpleFormat.FDate());
175
176         /* Tell primitives about their wrapper class. */
177         setWrapper(Format.ID_BOOL, Format.ID_BOOL_W);
178         setWrapper(Format.ID_BYTE, Format.ID_BYTE_W);
179         setWrapper(Format.ID_SHORT, Format.ID_SHORT_W);
180         setWrapper(Format.ID_INT, Format.ID_INT_W);
181         setWrapper(Format.ID_LONG, Format.ID_LONG_W);
182         setWrapper(Format.ID_FLOAT, Format.ID_FLOAT_W);
183         setWrapper(Format.ID_DOUBLE, Format.ID_DOUBLE_W);
184         setWrapper(Format.ID_CHAR, Format.ID_CHAR_W);
185     }
186
187     /**
188      * Sets a format for which space in the formatList has been preallocated,
189      * and makes it the current format for the class.
190      */

191     private void setFormat(int id, SimpleFormat format) {
192         format.setId(id);
193         format.initializeIfNeeded(this);
194         formatList.set(id, format);
195         formatMap.put(format.getClassName(), format);
196     }
197
198     /**
199      * Tells a primitive format about the format for its corresponding
200      * primitive wrapper class.
201      */

202     private void setWrapper(int primitiveId, int wrapperId) {
203         SimpleFormat primitiveFormat = formatList.get(primitiveId);
204         SimpleFormat wrapperFormat = formatList.get(wrapperId);
205         primitiveFormat.setWrapperFormat(wrapperFormat);
206     }
207
208     public Format getFormat(int formatId) {
209         Format format;
210         try {
211             format = formatList.get(formatId);
212             if (format == null) {
213                 throw new IllegalStateException JavaDoc
214                     ("Not a simple type: " + formatId);
215             }
216             return format;
217         } catch (NoSuchElementException JavaDoc e) {
218             throw new IllegalStateException JavaDoc
219                 ("Not a simple type: " + formatId);
220         }
221     }
222
223     public Format getFormat(Class JavaDoc cls) {
224         Format format = formatMap.get(cls.getName());
225         if (format == null) {
226             throw new IllegalArgumentException JavaDoc
227                 ("Not a simple type: " + cls.getName());
228         }
229         return format;
230     }
231
232     public Format getFormat(String JavaDoc className) {
233         return formatMap.get(className);
234     }
235
236     public Format createFormat(String JavaDoc clsName, Map JavaDoc<String JavaDoc,Format> newFormats) {
237         throw new IllegalStateException JavaDoc();
238     }
239
240     public Format createFormat(Class JavaDoc type, Map JavaDoc<String JavaDoc,Format> newFormats) {
241         throw new IllegalStateException JavaDoc();
242     }
243
244     public boolean isRawAccess() {
245         return false;
246     }
247
248     public Object JavaDoc convertRawObject(RawObject o, IdentityHashMap JavaDoc converted) {
249         throw new IllegalStateException JavaDoc();
250     }
251 }
252
Popular Tags