OSDN Git Service

f6ce8978af67329032522ce1c7e9b4bad324804c
[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  * @version $Revision$ 
17  */
18 public class TypeAndValueBirthmarkElement extends BirthmarkElement implements Serializable{
19     private static final long serialVersionUID = 237098465735321L;
20
21     private String signature;
22     private Serializable serialValue;
23     private transient Object value;
24
25     /**
26      * @param value
27      */
28     public TypeAndValueBirthmarkElement(String signature, Object value){
29         super(signature + "=" + value);
30         this.signature = signature;
31         setValue(value);
32     }
33
34     public String getSignature(){
35         return signature;
36     }
37
38     public void setValue(Object value){
39         this.value = value;
40         if(signature.length() == 1 && value == null){
41             switch(signature.charAt(0)){
42             case 'Z': value = Boolean.FALSE;  break;
43             case 'D': value = new Double(0d); break;
44             case 'F': value = new Float(0f);  break;
45             case 'C':
46             case 'S':
47             case 'B':
48             case 'I':
49             default:  value = new Integer(0); break;
50             }
51         }
52
53         if(value != null && value instanceof Serializable){
54             serialValue = (Serializable)value;
55         }
56     }
57
58     @Override
59     public Object getValue(){
60         return value;
61     }
62
63     @Override
64     public String toString(){
65         return signature + "=" + value;
66     }
67
68     @Override
69     public int hashCode(){
70         return signature.hashCode() + value.hashCode();
71     }
72
73     @Override
74     public boolean equals(Object o){
75         if(o instanceof TypeAndValueBirthmarkElement){
76             TypeAndValueBirthmarkElement tvbe = (TypeAndValueBirthmarkElement)o;
77
78             if(getSignature().equals(tvbe.getSignature())){
79                 if(getValue() == null && tvbe.getValue() == null){
80                     return true;
81                 }
82                 else if(getValue() != null && tvbe.getValue() != null){
83                     return getValue().equals(tvbe.getValue());
84                 }
85             }
86         }
87         return false;
88     }
89
90     private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException{
91         in.defaultReadObject();
92         if(serialValue != null){
93             value = serialValue;
94         }
95     }
96 }