1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package fulmine.model.field;
17
18 import static fulmine.util.Utils.EMPTY_STRING;
19 import fulmine.Type;
20 import fulmine.context.IPermissionProfile;
21 import fulmine.protocol.specification.ByteReader;
22 import fulmine.protocol.specification.FieldWriter;
23 import fulmine.protocol.wire.IWireIdentity;
24 import fulmine.protocol.wire.operation.IOperationScope;
25 import fulmine.util.log.AsyncLog;
26 import fulmine.util.reference.is;
27
28
29
30
31
32
33 public final class DoubleField extends AbstractField
34 {
35 private final static AsyncLog LOG = new AsyncLog(DoubleField.class);
36
37 @Override
38 protected AsyncLog getLog()
39 {
40 return LOG;
41 }
42
43 private double value;
44
45 public DoubleField(String identity)
46 {
47 this(identity, IPermissionProfile.DEFAULT_APPLICATION,
48 IPermissionProfile.DEFAULT_PERMISSION);
49 }
50
51 public DoubleField(String identity, byte application, short permission)
52 {
53 super(identity, Type.DOUBLE_FIELD, application, permission);
54 }
55
56
57
58
59
60
61
62
63
64 public DoubleField(String identity, double value)
65 {
66 this(identity);
67 set(value);
68 }
69
70
71
72
73
74
75
76
77
78
79 public DoubleField(String identity, String value)
80 {
81 this(identity, Double.valueOf(value).doubleValue());
82 }
83
84
85
86
87
88
89 public double get()
90 {
91 return this.value;
92 }
93
94
95
96
97
98
99
100
101
102
103 public boolean set(double value)
104 {
105 if (Double.doubleToRawLongBits(this.value) == Double.doubleToRawLongBits(value))
106 {
107 return false;
108 }
109 this.value = value;
110 notifyEvent(this);
111 return true;
112 }
113
114 @Override
115 protected boolean doReadState(IOperationScope scope, byte[] buffer,
116 int start, int numberOfBytes) throws Exception
117 {
118 if (scope.getPermissionProfile().contains(getApplication(),
119 getPermission()))
120 {
121 return set(ByteReader.readDouble(buffer, start, numberOfBytes));
122 }
123 if (getLog().isDebugEnabled())
124 {
125 getLog().debug("Not permissioned for " + this);
126 }
127 return false;
128 }
129
130 @Override
131 protected boolean doWriteState(IOperationScope scope, IWireIdentity wireId,
132 byte[][] headerBuffer, int[] headerBufferPosition, byte[][] dataBuffer,
133 int[] dataBufferPosition, boolean completeState) throws Exception
134 {
135 FieldWriter.writeDoubleField(wireId, this.value, headerBuffer,
136 headerBufferPosition, dataBuffer, dataBufferPosition);
137 return true;
138 }
139
140 @Override
141 public int hashCode()
142 {
143 final int prime = 31;
144 int result = 1;
145 long temp;
146 temp = Double.doubleToLongBits(value);
147 result = prime * result + (int) (temp ^ (temp >>> 32));
148 return result;
149 }
150
151 @Override
152 public boolean equals(Object obj)
153 {
154 if (is.same(this, obj))
155 {
156 return true;
157 }
158 if (is.differentClass(this, obj))
159 {
160 return false;
161 }
162 if (!super.equals(obj))
163 {
164 return false;
165 }
166 final DoubleField other = (DoubleField) obj;
167 return is.eq(this.value, other.value);
168 }
169
170 public String getValueAsString()
171 {
172 return EMPTY_STRING + this.value;
173 }
174
175 public Object getValue()
176 {
177 return Double.valueOf(get());
178 }
179
180 public boolean setValueFromString(String value)
181 {
182 return set(Double.parseDouble(value));
183 }
184 }