KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > object > dna > impl > ClassInstance


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

5 package com.tc.object.dna.impl;
6
7 import com.tc.object.loaders.ClassProvider;
8
9 import java.io.Serializable JavaDoc;
10 import java.util.HashMap JavaDoc;
11 import java.util.Map JavaDoc;
12
13 public class ClassInstance implements Serializable JavaDoc {
14   private final static Map JavaDoc PRIMITIVE_TYPE_MAP = new HashMap JavaDoc();
15
16   private final UTF8ByteDataHolder name;
17   private final UTF8ByteDataHolder loaderDef;
18   
19   static {
20     PRIMITIVE_TYPE_MAP.put(Integer.TYPE.getName(), Integer.TYPE);
21     PRIMITIVE_TYPE_MAP.put(Short.TYPE.getName(), Short.TYPE);
22     PRIMITIVE_TYPE_MAP.put(Long.TYPE.getName(), Long.TYPE);
23     PRIMITIVE_TYPE_MAP.put(Byte.TYPE.getName(), Byte.TYPE);
24     PRIMITIVE_TYPE_MAP.put(Double.TYPE.getName(), Double.TYPE);
25     PRIMITIVE_TYPE_MAP.put(Float.TYPE.getName(), Float.TYPE);
26     PRIMITIVE_TYPE_MAP.put(Double.TYPE.getName(), Double.TYPE);
27     PRIMITIVE_TYPE_MAP.put(Boolean.TYPE.getName(), Boolean.TYPE);
28     PRIMITIVE_TYPE_MAP.put(Void.TYPE.getName(), Void.TYPE);
29   }
30   
31   private static Class JavaDoc getPrimitiveClass(String JavaDoc className) {
32     return (Class JavaDoc)PRIMITIVE_TYPE_MAP.get(className);
33   }
34
35   // Used in tests
36
ClassInstance(String JavaDoc className, String JavaDoc loaderDefinition) {
37     this(new UTF8ByteDataHolder(className), new UTF8ByteDataHolder(loaderDefinition));
38   }
39
40   public ClassInstance(UTF8ByteDataHolder className, UTF8ByteDataHolder loaderDefinition) {
41     name = className;
42     loaderDef = loaderDefinition;
43   }
44
45   public Class JavaDoc asClass(ClassProvider classProvider) throws ClassNotFoundException JavaDoc {
46     String JavaDoc classLoaderdef = loaderDef.asString();
47     String JavaDoc className = name.asString();
48     Class JavaDoc clazz = getPrimitiveClass(className);
49     if (clazz != null) { return clazz; }
50     return classProvider.getClassFor(className, classLoaderdef);
51   }
52
53   public boolean equals(Object JavaDoc obj) {
54     if (obj instanceof ClassInstance) {
55       ClassInstance other = (ClassInstance) obj;
56       return this.name.equals(other.name) && this.loaderDef.equals(other.loaderDef);
57     }
58     return false;
59   }
60
61   public int hashCode() {
62     int hash = 17;
63     hash = (37 * hash) + name.hashCode();
64     hash = (37 * hash) + loaderDef.hashCode();
65     return hash;
66   }
67
68   public UTF8ByteDataHolder getLoaderDef() {
69     return loaderDef;
70   }
71
72   public UTF8ByteDataHolder getName() {
73     return name;
74   }
75
76   public String JavaDoc toString() {
77     return "Class(" + name.asString() + "," + loaderDef.asString() + ")";
78   }
79
80 }
81
Popular Tags