OSDN Git Service

d7de4c1b87b3ed4772b55bd93cb956493594f3e5
[stigmata/stigmata.git] / src / main / java / jp / sourceforge / stigmata / birthmarks / cvfv / TypeAndValueBirthmarkElement.java
1 package jp.sourceforge.stigmata.birthmarks.cvfv;
2
3 /*
4  * $Id$
5  */
6
7 import java.io.IOException;
8 import java.io.ObjectInputStream;
9 import java.io.Serializable;
10
11 import jp.sourceforge.stigmata.BirthmarkElement;
12
13 /**
14  * 
15  * @author Haruaki TAMADA
16  */
17 public class TypeAndValueBirthmarkElement extends BirthmarkElement implements Serializable{
18     private static final long serialVersionUID = 237098465735321L;
19
20     private String signature;
21     private Serializable serialValue;
22     private transient Object value;
23
24     /**
25      * @param value
26      */
27     public TypeAndValueBirthmarkElement(String signature, Object value){
28         super(signature + "=" + value);
29         this.signature = signature;
30         setValue(value);
31     }
32
33     public String getSignature(){
34         return signature;
35     }
36
37     public void setValue(Object value){
38         this.value = value;
39         if(signature.length() == 1 && value == null){
40             switch(signature.charAt(0)){
41             case 'Z': value = Boolean.FALSE;  break;
42             case 'D': value = new Double(0d); break;
43             case 'F': value = new Float(0f);  break;
44             case 'C':
45             case 'S':
46             case 'B':
47             case 'I':
48             default:  value = new Integer(0); break;
49             }
50         }
51
52         if(value != null && value instanceof Serializable){
53             serialValue = (Serializable)value;
54         }
55     }
56
57     @Override
58     public Object getValue(){
59         return value;
60     }
61
62     @Override
63     public String toString(){
64         return signature + "=" + value;
65     }
66
67     @Override
68     public int hashCode(){
69         return signature.hashCode() + value.hashCode();
70     }
71
72     @Override
73     public boolean equals(Object o){
74         if(o instanceof TypeAndValueBirthmarkElement){
75             TypeAndValueBirthmarkElement tvbe = (TypeAndValueBirthmarkElement)o;
76
77             if(getSignature().equals(tvbe.getSignature())){
78                 if(getValue() == null && tvbe.getValue() == null){
79                     return true;
80                 }
81                 else if(getValue() != null && tvbe.getValue() != null){
82                     return getValue().equals(tvbe.getValue());
83                 }
84             }
85         }
86         return false;
87     }
88
89     private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException{
90         in.defaultReadObject();
91         if(serialValue != null){
92             value = serialValue;
93         }
94     }
95 }