KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > object > LiteralValues


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

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 JavaDoc;
14 import java.math.BigInteger JavaDoc;
15 import java.util.Collections JavaDoc;
16 import java.util.HashMap JavaDoc;
17 import java.util.Map JavaDoc;
18
19 /**
20  * Responsuible for handling literals
21  */

22 public class LiteralValues {
23   public final static String JavaDoc ENUM_CLASS_DOTS = "java.lang.Enum";
24
25   // XXX:: If you are adding more types, please see PhysicalStateClassLoader and DNAEncoding
26
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 JavaDoc values;
50
51   public LiteralValues() {
52     super();
53
54     Map JavaDoc tmp = new HashMap JavaDoc();
55
56     addMapping(tmp, Integer JavaDoc.class.getName(), INTEGER);
57     addMapping(tmp, int.class.getName(), INTEGER);
58     addMapping(tmp, Long JavaDoc.class.getName(), LONG);
59     addMapping(tmp, long.class.getName(), LONG);
60     addMapping(tmp, Character JavaDoc.class.getName(), CHARACTER);
61     addMapping(tmp, char.class.getName(), CHARACTER);
62     addMapping(tmp, Float JavaDoc.class.getName(), FLOAT);
63     addMapping(tmp, float.class.getName(), FLOAT);
64     addMapping(tmp, Double JavaDoc.class.getName(), DOUBLE);
65     addMapping(tmp, double.class.getName(), DOUBLE);
66     addMapping(tmp, Byte JavaDoc.class.getName(), BYTE);
67     addMapping(tmp, byte.class.getName(), BYTE);
68     addMapping(tmp, String JavaDoc.class.getName(), STRING);
69     addMapping(tmp, UTF8ByteDataHolder.class.getName(), STRING_BYTES);
70     addMapping(tmp, Short JavaDoc.class.getName(), SHORT);
71     addMapping(tmp, short.class.getName(), SHORT);
72     addMapping(tmp, Boolean JavaDoc.class.getName(), BOOLEAN);
73     addMapping(tmp, boolean.class.getName(), BOOLEAN);
74
75     addMapping(tmp, BigInteger JavaDoc.class.getName(), BIG_INTEGER);
76     addMapping(tmp, BigDecimal JavaDoc.class.getName(), BIG_DECIMAL);
77
78     addMapping(tmp, java.lang.Class JavaDoc.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 JavaDoc.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 JavaDoc pojo) {
91     if (pojo instanceof ClassLoader JavaDoc) { return JAVA_LANG_CLASSLOADER; }
92
93     Class JavaDoc 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 JavaDoc className) {
100     int i = valueForClassName(className);
101     return i != OBJECT && i != ARRAY;
102   }
103
104   public boolean isLiteralInstance(Object JavaDoc 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 JavaDoc map, String JavaDoc className, int i) {
111     Object JavaDoc prev = map.put(className, new Integer JavaDoc(i));
112     Assert.assertNull(className, prev);
113   }
114
115   public int valueForClassName(String JavaDoc className) {
116     if ((className != null) && className.startsWith("[")) { return ARRAY; }
117     Integer JavaDoc i = (Integer JavaDoc) values.get(className);
118     if (i == null) return OBJECT;
119     return i.intValue();
120   }
121 }
122
Popular Tags