1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package pl.edu.agh.cast.data.model.property;
19
20 import java.io.Serializable;
21 import java.util.Arrays;
22
23
24
25
26
27
28
29
30 public abstract class Property<T extends MetaProperty> implements Serializable {
31
32
33
34
35 private static final long serialVersionUID = 6713226359813402652L;
36
37 private T metaProperty;
38
39
40
41
42
43
44
45 protected Property(T metaProperty) {
46 if (metaProperty == null) {
47 throw new IllegalArgumentException("Meta property cannot be null");
48 }
49 this.metaProperty = metaProperty;
50 }
51
52
53
54
55
56
57 public abstract Object getValue();
58
59
60
61
62
63
64
65
66
67
68 public abstract void setValue(Object value);
69
70
71
72
73
74
75 public T getMetaProperty() {
76 return this.metaProperty;
77 }
78
79
80
81
82
83
84
85
86 protected boolean isValidValue(Object value) {
87 return this.getMetaProperty().getType().getValidator().isValidClass(value);
88 }
89
90
91
92
93
94
95 @SuppressWarnings("unchecked")
96 @Override
97 public boolean equals(Object other) {
98 if (other == null || !(other instanceof Property)) {
99 return false;
100 }
101
102 Property<T> otherProperty = (Property<T>)other;
103
104 if (!this.getMetaProperty().equals(otherProperty.getMetaProperty())) {
105 return false;
106 }
107
108 Object thisValue = null;
109 try {
110 thisValue = this.getValue();
111 } catch (PropertyException e) {
112
113 }
114
115 Object otherValue = null;
116 try {
117 otherValue = otherProperty.getValue();
118 } catch (PropertyException e) {
119
120 }
121
122 if (thisValue == null) {
123 return otherValue == null;
124 } else {
125 if (thisValue.getClass().isArray() && otherValue.getClass().isArray()) {
126 return Arrays.deepEquals((Object[])thisValue, (Object[])otherValue);
127 } else {
128 return thisValue.equals(otherValue);
129 }
130 }
131 }
132
133
134
135
136
137
138 @Override
139 public int hashCode() {
140 final int prime = 31;
141 int result = prime;
142
143 result = prime * result + this.getMetaProperty().hashCode();
144 try {
145 int valResult = 0;
146 if (this.getValue() != null) {
147 if (this.getValue().getClass().isArray()) {
148 valResult = Arrays.deepHashCode((Object[])getValue());
149 } else {
150 valResult = this.getValue().hashCode();
151 }
152 }
153
154 result = prime * result + valResult;
155 } catch (PropertyException e) {
156
157 }
158 return result;
159 }
160
161
162
163
164
165
166 @Override
167 public String toString() {
168 return getValue() == null ? "NULL" : getValue().toString();
169 }
170
171 }