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 long value.
30   * 
31   * @author Ramon Servadei
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       * 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 LongField(String identity, long value)
65      {
66          this(identity);
67          set(value);
68      }
69  
70      /**
71       * Construct the field with the value from a string representation of the
72       * native type
73       * 
74       * @param identity
75       *            the field identity
76       * @param value
77       *            the string representation of the value for the field
78       */
79      public LongField(String identity, String value)
80      {
81          this(identity, Long.valueOf(value).longValue());
82      }
83  
84      /**
85       * Get the long value of the field
86       * 
87       * @return the long value of this field
88       */
89      public long get()
90      {
91          return this.value;
92      }
93  
94      /**
95       * Set the long value for the field. If the field value changes, an event is
96       * generated.
97       * 
98       * @param value
99       *            the new long value for this field
100      * @return <code>true</code> if the value of the field changed
101      * @see #notifyEvent(fulmine.model.event.IEvent)
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 }