1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package pl.edu.agh.cast.model.attributes;
19
20 import java.io.Serializable;
21
22 import org.eclipse.osgi.util.NLS;
23
24 import pl.edu.agh.cast.util.Messages;
25
26 import com.thoughtworks.xstream.annotations.XStreamAlias;
27
28
29
30
31
32
33 @XStreamAlias("attributeValue")
34 public final class AttributeValue implements Cloneable, Serializable {
35
36 private static final long serialVersionUID = 3043241931570536587L;
37
38 @XStreamAlias("attribute")
39 private Attribute attribute;
40
41 @XStreamAlias("value")
42 private Object value;
43
44
45
46
47
48
49
50 public AttributeValue(Attribute attribute, Object value) {
51 super();
52 if (!attribute.validateValue(value)) {
53 throw new IllegalArgumentException("Value " + value
54 + " is not valid class for type " + attribute.getType());
55 }
56
57 this.attribute = attribute;
58 this.value = value;
59 }
60
61
62
63
64
65
66 public AttributeValue copy() {
67 return new AttributeValue(attribute, value);
68 }
69
70 public Object getValue() {
71 return value;
72 }
73
74 public Attribute getAttribute() {
75 return attribute;
76 }
77
78
79
80
81
82 @Override
83 public boolean equals(Object obj) {
84 if (obj == this) {
85 return true;
86 }
87 if (!(obj instanceof AttributeValue)) {
88 return false;
89 }
90 AttributeValue other = (AttributeValue)obj;
91 if (!this.attribute.equals(other.attribute)) {
92 return false;
93 }
94 if (this.value == null) {
95 if (other.value != null) {
96 return false;
97 }
98 } else if (!this.value.equals(other.value)) {
99 return false;
100 }
101 return true;
102 }
103
104
105
106
107
108 @Override
109 public int hashCode() {
110 if (this.value == null) {
111 return attribute.hashCode();
112 } else {
113 return attribute.hashCode() ^ value.hashCode();
114 }
115 }
116
117
118
119
120
121 @Override
122 public String toString() {
123 return NLS.bind("AttributeValue \"{0}\" for \"{1}\"", value, attribute);
124 }
125
126
127
128
129
130
131 public String format() {
132 if (null == value) {
133 return Messages.AttributeEditorDialog_5;
134 } else {
135 return getAttribute().getType().getValidator().convertToStringFormat(value);
136 }
137 }
138 }