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 LongField extends AbstractField
34 {
35 private final static AsyncLog LOG = new AsyncLog(LongField.class);
36
37 @Override
38 protected AsyncLog getLog()
39 {
40 return LOG;
41 }
42
43 private long value;
44
45 public LongField(String id)
46 {
47 this(id, IPermissionProfile.DEFAULT_APPLICATION,
48 IPermissionProfile.DEFAULT_PERMISSION);
49 }
50
51 public LongField(String id, byte application, short permission)
52 {
53 super(id, Type.LONG_FIELD, application, permission);
54 }
55
56
57
58
59
60
61
62
63
64 public LongField(String identity, long value)
65 {
66 this(identity);
67 set(value);
68 }
69
70
71
72
73
74
75
76
77
78
79 public LongField(String identity, String value)
80 {
81 this(identity, Long.valueOf(value).longValue());
82 }
83
84
85
86
87
88
89 public long get()
90 {
91 return this.value;
92 }
93
94
95
96
97
98
99
100
101
102
103 public boolean set(long value)
104 {
105 if (this.value == value)
106 {
107 return false;
108 }
109 this.value = value;
110 notifyEvent(this);
111 return true;
112 }
113
114 @Override
115 protected final 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.readLong(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.writeLongField(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 result = prime * result + (int) (value ^ (value >>> 32));
146 return result;
147 }
148
149 @Override
150 public boolean equals(Object obj)
151 {
152 if (is.same(this, obj))
153 {
154 return true;
155 }
156 if (is.differentClass(this, obj))
157 {
158 return false;
159 }
160 if (!super.equals(obj))
161 {
162 return false;
163 }
164 final LongField other = (LongField) obj;
165 return is.eq(this.value, other.value);
166 }
167
168 public String getValueAsString()
169 {
170 return EMPTY_STRING + this.value;
171 }
172
173 public Object getValue()
174 {
175 return Long.valueOf(get());
176 }
177
178 public boolean setValueFromString(String value)
179 {
180 return set(Long.parseLong(value));
181 }
182 }