View Javadoc

1   /*
2      Copyright 2007 Ramon Servadei
3   
4      Licensed under the Apache License, Version 2.0 (the "License");
5      you may not use this file except in compliance with the License.
6      You may obtain a copy of the License at
7   
8          http://www.apache.org/licenses/LICENSE-2.0
9   
10     Unless required by applicable law or agreed to in writing, software
11     distributed under the License is distributed on an "AS IS" BASIS,
12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13     See the License for the specific language governing permissions and
14     limitations under the License.
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   * Field implementation for a string value.
30   * 
31   * @author Ramon Servadei
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       * Construct the field with the value from the native type
58       * 
59       * @param identity
60       *            the field identity
61       * @param value
62       *            the native value for the field
63       */
64      public StringField(String identity, String value)
65      {
66          this(identity);
67          set(value);
68      }
69  
70      /**
71       * Get the string value of the field
72       * 
73       * @return the string value of this field
74       */
75      public String get()
76      {
77          return this.value;
78      }
79  
80      /**
81       * Set the string value for the field. If the field value changes, an event
82       * is generated.
83       * 
84       * @param value
85       *            the new string value for this field
86       * @return <code>true</code> if the value of the field changed
87       * @see #notifyEvent(fulmine.model.event.IEvent)
88       */
89      public boolean set(String value)
90      {
91          // check for either being null or the same reference or the same string
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 }