KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > jcr > DoubleValue


1 /*
2  * $Id: DoubleValue.java,v 1.2 2004/07/24 00:16:21 benjmestrallet Exp $
3  *
4  * Copyright 2002-2004 Day Management AG, Switzerland.
5  *
6  * Licensed under the Day RI License, Version 2.0 (the "License"),
7  * as a reference implementation of the following specification:
8  *
9  * Content Repository API for Java Technology, revision 0.12
10  * <http://www.jcp.org/en/jsr/detail?id=170>
11  *
12  * You may not use this file except in compliance with the License.
13  * You may obtain a copy of the License files at
14  *
15  * http://www.day.com/content/en/licenses/day-ri-license-2.0
16  * http://www.apache.org/licenses/LICENSE-2.0
17  *
18  * Unless required by applicable law or agreed to in writing, software
19  * distributed under the License is distributed on an "AS IS" BASIS,
20  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21  * See the License for the specific language governing permissions and
22  * limitations under the License.
23  */

24 package javax.jcr;
25
26 import java.util.Calendar JavaDoc;
27 import java.util.Date JavaDoc;
28
29 /**
30  * A <code>DoubleValue</code> provides an implementation
31  * of the <code>Value</code> interface representing a double value.
32  *
33  * @author Stefan Guggisberg
34  */

35 public class DoubleValue extends BaseValue {
36
37   public static final int TYPE = PropertyType.DOUBLE;
38
39   private final Double JavaDoc dblNumber;
40
41   /**
42    * Constructs a <code>DoubleValue</code> object representing a double.
43    *
44    * @param dblNumber the double this <code>DoubleValue</code> should represent
45    */

46   public DoubleValue(Double JavaDoc dblNumber) {
47     super(TYPE);
48     this.dblNumber = dblNumber;
49   }
50
51   /**
52    * Constructs a <code>DoubleValue</code> object representing a double.
53    *
54    * @param dbl the double this <code>DoubleValue</code> should represent
55    */

56   public DoubleValue(double dbl) {
57     super(TYPE);
58     this.dblNumber = new Double JavaDoc(dbl);
59   }
60
61   /**
62    * Returns a new <code>DoubleValue</code> initialized to the value
63    * represented by the specified <code>String</code>.
64    *
65    * @param s the string to be parsed.
66    * @return a newly constructed <code>DoubleValue</code> representing the
67    * the specified value.
68    * @throws ValueFormatException If the <code>String</code> does not
69    * contain a parsable <code>double</code>.
70    */

71   public static DoubleValue valueOf(String JavaDoc s) throws ValueFormatException {
72     try {
73       return new DoubleValue(Double.parseDouble(s));
74     } catch (NumberFormatException JavaDoc e) {
75       throw new ValueFormatException("invalid format", e);
76     }
77   }
78
79   /**
80    * Indicates whether some other object is "equal to" this one.
81    * <p/>
82    * The result is <code>true</code> if and only if the argument is not
83    * <code>null</code> and is a <code>DoubleValue</code> object that
84    * represents the same value as this object.
85    *
86    * @param obj the reference object with which to compare.
87    * @return <code>true</code> if this object is the same as the obj
88    * argument; <code>false</code> otherwise.
89    */

90   public boolean equals(Object JavaDoc obj) {
91     if (this == obj) {
92       return true;
93     }
94     if (obj instanceof DoubleValue) {
95       DoubleValue other = (DoubleValue) obj;
96       if (dblNumber == other.dblNumber) {
97         return true;
98       } else if (dblNumber != null && other.dblNumber != null) {
99         return dblNumber.equals(other.dblNumber);
100       }
101     }
102     return false;
103   }
104
105   //----------------------------------------------------------------< Value >
106
/**
107    * @see Value#getDate
108    */

109   public Calendar JavaDoc getDate() throws ValueFormatException, IllegalStateException JavaDoc, RepositoryException {
110     setValueConsumed();
111
112     if (dblNumber != null) {
113       // loosing timezone information...
114
Calendar JavaDoc cal = Calendar.getInstance();
115       cal.setTime(new Date JavaDoc(dblNumber.longValue()));
116       return cal;
117     } else {
118       throw new ValueFormatException("empty value");
119     }
120   }
121
122   /**
123    * @see Value#getLong
124    */

125   public long getLong() throws ValueFormatException, IllegalStateException JavaDoc, RepositoryException {
126     setValueConsumed();
127
128     if (dblNumber != null) {
129       return dblNumber.longValue();
130     } else {
131       throw new ValueFormatException("empty value");
132     }
133   }
134
135   /**
136    * @see Value#getBoolean
137    */

138   public boolean getBoolean() throws ValueFormatException, IllegalStateException JavaDoc, RepositoryException {
139     setValueConsumed();
140
141     throw new ValueFormatException("conversion to boolean failed: inconvertible types");
142   }
143
144   /**
145    * @see Value#getDouble
146    */

147   public double getDouble() throws ValueFormatException, IllegalStateException JavaDoc, RepositoryException {
148     setValueConsumed();
149
150     if (dblNumber != null) {
151       return dblNumber.doubleValue();
152     } else {
153       throw new ValueFormatException("empty value");
154     }
155   }
156
157   /**
158    * @see Value#getString
159    */

160   public String JavaDoc getString() throws ValueFormatException, IllegalStateException JavaDoc, RepositoryException {
161     setValueConsumed();
162
163     if (dblNumber != null) {
164       return dblNumber.toString();
165     } else {
166       throw new ValueFormatException("empty value");
167     }
168   }
169 }
170
Popular Tags