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 StringField extends AbstractField
34 {
35 private final static AsyncLog LOG = new AsyncLog(StringField.class);
36
37 @Override
38 protected AsyncLog getLog()
39 {
40 return LOG;
41 }
42
43 private String value;
44
45 public StringField(String identity)
46 {
47 this(identity, IPermissionProfile.DEFAULT_APPLICATION,
48 IPermissionProfile.DEFAULT_PERMISSION);
49 }
50
51 public StringField(String identity, byte application, short permission)
52 {
53 super(identity, Type.STRING_FIELD, application, permission);
54 }
55
56
57
58
59
60
61
62
63
64 public StringField(String identity, String value)
65 {
66 this(identity);
67 set(value);
68 }
69
70
71
72
73
74
75 public String get()
76 {
77 return this.value;
78 }
79
80
81
82
83
84
85
86
87
88
89 public boolean set(String value)
90 {
91
92 if (this.value == value
93 || (this.value != null && this.value.equals(value)))
94 {
95 return false;
96 }
97 this.value = value;
98 notifyEvent(this);
99 return true;
100 }
101
102 @Override
103 protected boolean doReadState(IOperationScope scope, byte[] buffer,
104 int start, int numberOfBytes) throws Exception
105 {
106 if (scope.getPermissionProfile().contains(getApplication(),
107 getPermission()))
108 {
109 return set(ByteReader.readString(buffer, start, numberOfBytes));
110 }
111 if (getLog().isDebugEnabled())
112 {
113 getLog().debug("Not permissioned for " + this);
114 }
115 return false;
116 }
117
118 @Override
119 protected boolean doWriteState(IOperationScope scope, IWireIdentity wireId,
120 byte[][] headerBuffer, int[] headerBufferPosition, byte[][] dataBuffer,
121 int[] dataBufferPosition, boolean completeState) throws Exception
122 {
123 FieldWriter.writeStringField(wireId, this.value, headerBuffer,
124 headerBufferPosition, dataBuffer, dataBufferPosition);
125 return true;
126 }
127
128 @Override
129 public int hashCode()
130 {
131 final int prime = 31;
132 int result = super.hashCode();
133 result = prime * result + ((value == null) ? 0 : value.hashCode());
134 return result;
135 }
136
137 @Override
138 public boolean equals(Object obj)
139 {
140 if (is.same(this, obj))
141 {
142 return true;
143 }
144 if (is.differentClass(this, obj))
145 {
146 return false;
147 }
148 if (!super.equals(obj))
149 {
150 return false;
151 }
152 final StringField other = (StringField) obj;
153 return is.eq(this.value, other.value);
154 }
155
156 public String getValueAsString()
157 {
158 return EMPTY_STRING + this.value;
159 }
160
161 public Object getValue()
162 {
163 return get();
164 }
165
166 public boolean setValueFromString(String value)
167 {
168 return set(value);
169 }
170
171 }