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 double value.
30   * 
31   * @author Ramon Servadei
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       * 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 DoubleField(String identity, double 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 DoubleField(String identity, String value)
80      {
81          this(identity, Double.valueOf(value).doubleValue());
82      }
83  
84      /**
85       * Get the double value of the field
86       * 
87       * @return the double value of this field
88       */
89      public double get()
90      {
91          return this.value;
92      }
93  
94      /**
95       * Set the double value for the field. If the field value changes, an event
96       * is generated.
97       * 
98       * @param value
99       *            the new double 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(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 }