1 4 package com.tc.object; 5 6 import com.tc.object.dna.impl.ClassInstance; 7 import com.tc.object.dna.impl.ClassLoaderInstance; 8 import com.tc.object.dna.impl.EnumInstance; 9 import com.tc.object.dna.impl.UTF8ByteDataHolder; 10 import com.tc.util.Assert; 11 import com.tc.util.ClassUtils; 12 13 import java.math.BigDecimal ; 14 import java.math.BigInteger ; 15 import java.util.Collections ; 16 import java.util.HashMap ; 17 import java.util.Map ; 18 19 22 public class LiteralValues { 23 public final static String ENUM_CLASS_DOTS = "java.lang.Enum"; 24 25 public final static int INTEGER = 0; 27 public final static int LONG = 1; 28 public final static int CHARACTER = 2; 29 public final static int FLOAT = 3; 30 public final static int DOUBLE = 4; 31 public final static int BYTE = 5; 32 public final static int STRING = 6; 33 public final static int BOOLEAN = 7; 34 public final static int SHORT = 8; 35 public final static int ARRAY = 9; 36 public final static int OBJECT = 10; 37 public final static int OBJECT_ID = 11; 38 public final static int STRING_BYTES = 12; 39 public final static int JAVA_LANG_CLASS = 13; 40 public final static int JAVA_LANG_CLASS_HOLDER = 14; 41 public final static int STACK_TRACE_ELEMENT = 15; 42 public final static int BIG_INTEGER = 16; 43 public final static int BIG_DECIMAL = 17; 44 public final static int JAVA_LANG_CLASSLOADER = 18; 45 public final static int JAVA_LANG_CLASSLOADER_HOLDER = 19; 46 public final static int ENUM = 20; 47 public final static int ENUM_HOLDER = 21; 48 49 private final Map values; 50 51 public LiteralValues() { 52 super(); 53 54 Map tmp = new HashMap (); 55 56 addMapping(tmp, Integer .class.getName(), INTEGER); 57 addMapping(tmp, int.class.getName(), INTEGER); 58 addMapping(tmp, Long .class.getName(), LONG); 59 addMapping(tmp, long.class.getName(), LONG); 60 addMapping(tmp, Character .class.getName(), CHARACTER); 61 addMapping(tmp, char.class.getName(), CHARACTER); 62 addMapping(tmp, Float .class.getName(), FLOAT); 63 addMapping(tmp, float.class.getName(), FLOAT); 64 addMapping(tmp, Double .class.getName(), DOUBLE); 65 addMapping(tmp, double.class.getName(), DOUBLE); 66 addMapping(tmp, Byte .class.getName(), BYTE); 67 addMapping(tmp, byte.class.getName(), BYTE); 68 addMapping(tmp, String .class.getName(), STRING); 69 addMapping(tmp, UTF8ByteDataHolder.class.getName(), STRING_BYTES); 70 addMapping(tmp, Short .class.getName(), SHORT); 71 addMapping(tmp, short.class.getName(), SHORT); 72 addMapping(tmp, Boolean .class.getName(), BOOLEAN); 73 addMapping(tmp, boolean.class.getName(), BOOLEAN); 74 75 addMapping(tmp, BigInteger .class.getName(), BIG_INTEGER); 76 addMapping(tmp, BigDecimal .class.getName(), BIG_DECIMAL); 77 78 addMapping(tmp, java.lang.Class .class.getName(), JAVA_LANG_CLASS); 79 addMapping(tmp, ClassInstance.class.getName(), JAVA_LANG_CLASS_HOLDER); 80 addMapping(tmp, ObjectID.class.getName(), OBJECT_ID); 81 addMapping(tmp, StackTraceElement .class.getName(), STACK_TRACE_ELEMENT); 82 83 addMapping(tmp, ClassLoaderInstance.class.getName(), JAVA_LANG_CLASSLOADER_HOLDER); 84 addMapping(tmp, ENUM_CLASS_DOTS, ENUM); 85 addMapping(tmp, EnumInstance.class.getName(), ENUM_HOLDER); 86 87 values = Collections.unmodifiableMap(tmp); 88 } 89 90 public int valueFor(Object pojo) { 91 if (pojo instanceof ClassLoader ) { return JAVA_LANG_CLASSLOADER; } 92 93 Class clazz = pojo.getClass(); 94 int i = valueForClassName(clazz.getName()); 95 if(i == OBJECT && ClassUtils.isEnum(pojo.getClass())) { return ENUM; } 96 return i; 97 } 98 99 public boolean isLiteral(String className) { 100 int i = valueForClassName(className); 101 return i != OBJECT && i != ARRAY; 102 } 103 104 public boolean isLiteralInstance(Object obj) { 105 if (obj == null) { return false; } 106 int i = valueFor(obj); 107 return i != OBJECT && i != ARRAY; 108 } 109 110 private static void addMapping(Map map, String className, int i) { 111 Object prev = map.put(className, new Integer (i)); 112 Assert.assertNull(className, prev); 113 } 114 115 public int valueForClassName(String className) { 116 if ((className != null) && className.startsWith("[")) { return ARRAY; } 117 Integer i = (Integer ) values.get(className); 118 if (i == null) return OBJECT; 119 return i.intValue(); 120 } 121 } 122 | Popular Tags |