1 package org.lsst.ccs.bus;
2
3 import java.io.Serializable;
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 public class ObjectNType implements Serializable {
22 public static final Class[] WELL_KNOWN_TYPES = {
23
24 String.class,
25 Number.class,
26 byte[].class,
27 int[].class,
28 double[].class,
29 String[].class,
30 };
31 private static final long serialVersionUID = -4360711926765274975L;
32 private final String className ;
33 private final boolean ofWellKnownType;
34 private final boolean primitiveType ;
35 private final Object data ;
36
37
38
39
40
41
42 public ObjectNType(Class clazz, Serializable value) {
43 if (null == value) {
44 this.data = value;
45 this.className = (clazz!= null? clazz.getCanonicalName(): null) ;
46 ofWellKnownType = true;
47 primitiveType = true ;
48 return;
49 }
50 this.className = clazz.getCanonicalName();
51 ofWellKnownType = isWellKnownType(clazz) ;
52 if(ofWellKnownType) {
53 this.data = value ;
54 primitiveType = clazz.isPrimitive() ;
55 } else {
56 this.data = new DataCapsule(value);
57 primitiveType = false ;
58 }
59 }
60
61
62
63
64
65 public ObjectNType(Serializable value) {
66 this(value != null?value.getClass(): null, value) ;
67 }
68
69 public ObjectNType(int value) {
70 this(Integer.TYPE, value) ;
71 }
72 public ObjectNType(long value) {
73 this(Long.TYPE, value) ;
74 }
75 public ObjectNType(float value) {
76 this(Float.TYPE, value) ;
77 }
78 public ObjectNType(double value) {
79 this(Double.TYPE, value) ;
80 }
81 public ObjectNType(char value) {
82 this(Character.TYPE, value) ;
83 }
84
85
86
87
88
89
90
91 public static boolean isWellKnownType(Class clazz) {
92 if(clazz.isPrimitive()) {
93 return true;
94 }
95 for (Class knownClass : WELL_KNOWN_TYPES) {
96 if (knownClass.isAssignableFrom(clazz)) {
97 return true;
98 }
99 }
100 return false ;
101 }
102
103
104
105
106
107
108 public String getClassName() {
109 return className;
110 }
111
112
113
114
115
116 public boolean isOfPrimitiveType() {
117 return primitiveType;
118 }
119
120
121
122
123
124 public boolean isOfWellKnownType() {
125 return ofWellKnownType;
126 }
127
128
129
130
131
132
133
134 public Object getData() throws ClassNotFoundException {
135 if(ofWellKnownType) {
136 return data;
137 }
138 DataCapsule capsule = (DataCapsule) data ;
139 return capsule.getData() ;
140 }
141
142
143
144
145
146 public Object getRawData() {
147 return data ;
148 }
149
150 public String toString() {
151 return String.valueOf(data) + (className!= null? " ["+className+"]" : "") ;
152 }
153 }