View Javadoc

1   package org.lsst.ccs.bus;
2   
3   import java.io.Serializable;
4   
5   /**
6    * This class contains information about an object and it class.
7    * this information can be used in different ways:
8    * <UL>
9    *     <LI/> to store a primitive as an object (then is will be known for instance that the Object
10   *     is not, for instance,  of type <TT>Integer</TT> but of type <TT>int</TT>;
11   *     <LI/> to store a complex Object where the code that read may not have acces to the class.
12   *     for this purpose the Object is "frozen" in a <TT>DataCapsule</TT>
13   * </UL>
14   * <P/>
15   * Beware: the name of the class does not guarantees that the  "frozen" object can be deserialized
16   * since the version of the class can differ.
17   * @author bamade
18   */
19  // Date: 05/04/13
20  
21  public class ObjectNType implements Serializable {
22      public static final Class[] WELL_KNOWN_TYPES = {
23              //primitives are well known org.lsst.gruth.types
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       * keeps an object in a serialization-safe manner.
39       * @param clazz mostly used to note an object of a primitive type (e.g. Integer.TYPE)
40       * @param value can be null
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       * general constructor to keep an object safely
63       * @param value
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       * is the class a "well known type" (primitive or defined in the constant array WELL_KNOWN_TYPES)
88       * @param clazz
89       * @return
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      * the name of the class of the contained Object.
105      * @return can be null!
106      */
107     //@Nullable
108     public String getClassName() {
109         return className;
110     }
111 
112     /**
113      * is the data to be considered of a primitive type
114      * @return
115      */
116     public boolean isOfPrimitiveType() {
117         return primitiveType;
118     }
119 
120     /**
121      * being of a "well known type" means that the data has not been "safely" Serialized
122      * @return
123      */
124     public boolean isOfWellKnownType() {
125         return ofWellKnownType;
126     }
127 
128     /**
129      * return the data contained in a safe way: if the class of the stored Object is not in the ClassPath
130      * an Exception is thrown
131      * @return
132      * @throws ClassNotFoundException
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      * gets the content "as is" (without deserializing)
144      * @return
145      */
146     public Object getRawData() {
147         return data ;
148     }
149 
150     public String toString() {
151         return String.valueOf(data) + (className!= null? " ["+className+"]" : "") ;
152     }
153 }