1 8 9 package com.sleepycat.persist.impl; 10 11 import java.util.ArrayList ; 12 import java.util.HashMap ; 13 import java.util.IdentityHashMap ; 14 import java.util.List ; 15 import java.util.Map ; 16 import java.util.NoSuchElementException ; 17 18 import com.sleepycat.persist.raw.RawObject; 19 20 33 public class SimpleCatalog implements Catalog { 34 35 private static final Map <String ,Class > keywordToPrimitive; 36 static { 37 keywordToPrimitive = new HashMap <String ,Class >(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 <Class ,Class > primitiveTypeToWrapper; 49 static { 50 primitiveTypeToWrapper = new HashMap <Class ,Class >(8); 51 primitiveTypeToWrapper.put(Boolean.TYPE, Boolean .class); 52 primitiveTypeToWrapper.put(Character.TYPE, Character .class); 53 primitiveTypeToWrapper.put(Byte.TYPE, Byte .class); 54 primitiveTypeToWrapper.put(Short.TYPE, Short .class); 55 primitiveTypeToWrapper.put(Integer.TYPE, Integer .class); 56 primitiveTypeToWrapper.put(Long.TYPE, Long .class); 57 primitiveTypeToWrapper.put(Float.TYPE, Float .class); 58 primitiveTypeToWrapper.put(Double.TYPE, Double .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 type) { 68 return instance.formatMap.containsKey(type.getName()); 69 } 70 71 static Class primitiveToWrapper(Class type) { 72 Class cls = primitiveTypeToWrapper.get(type); 73 if (cls == null) { 74 throw new IllegalStateException (type.getName()); 75 } 76 return cls; 77 } 78 79 public static Class keyClassForName(String className) { 80 Class 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 e) { 87 throw new IllegalArgumentException 88 ("Key class not found: " + className); 89 } 90 } 91 return cls; 92 } 93 94 public static String keyClassName(String className) { 95 Class 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 classForName(String className) 105 throws ClassNotFoundException { 106 107 Class cls = keywordToPrimitive.get(className); 108 if (cls == null) { 109 cls = Class.forName(className); 110 } 111 return cls; 112 } 113 114 static SimpleFormat getSimpleFormat(Class type) { 115 return instance.formatMap.get(type.getName()); 116 } 117 118 static List <Format> copyFormatList() { 119 return new ArrayList <Format>(instance.formatList); 120 } 121 122 static boolean copyMissingFormats(List <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 <SimpleFormat> formatList; 136 private Map <String ,SimpleFormat> formatMap; 137 138 private SimpleCatalog() { 139 140 144 int initCapacity = Format.ID_PREDEFINED * 2; 145 formatList = new ArrayList <SimpleFormat>(initCapacity); 146 formatMap = new HashMap <String ,SimpleFormat>(initCapacity); 147 148 for (int i = 0; i <= Format.ID_PREDEFINED; i += 1) { 149 formatList.add(null); 150 } 151 152 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 174 setFormat(Format.ID_DATE, new SimpleFormat.FDate()); 175 176 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 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 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 214 ("Not a simple type: " + formatId); 215 } 216 return format; 217 } catch (NoSuchElementException e) { 218 throw new IllegalStateException 219 ("Not a simple type: " + formatId); 220 } 221 } 222 223 public Format getFormat(Class cls) { 224 Format format = formatMap.get(cls.getName()); 225 if (format == null) { 226 throw new IllegalArgumentException 227 ("Not a simple type: " + cls.getName()); 228 } 229 return format; 230 } 231 232 public Format getFormat(String className) { 233 return formatMap.get(className); 234 } 235 236 public Format createFormat(String clsName, Map <String ,Format> newFormats) { 237 throw new IllegalStateException (); 238 } 239 240 public Format createFormat(Class type, Map <String ,Format> newFormats) { 241 throw new IllegalStateException (); 242 } 243 244 public boolean isRawAccess() { 245 return false; 246 } 247 248 public Object convertRawObject(RawObject o, IdentityHashMap converted) { 249 throw new IllegalStateException (); 250 } 251 } 252 | Popular Tags |