KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > jcr > LongValue


1 /*
2  * $Id: LongValue.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>LongValue</code> provides an implementation
31  * of the <code>Value</code> interface representing a long value.
32  *
33  * @author Stefan Guggisberg
34  */

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

46   public LongValue(Long JavaDoc lNumber) {
47     super(TYPE);
48     this.lNumber = lNumber;
49   }
50
51   /**
52    * Constructs a <code>LongValue</code> object representing a long.
53    *
54    * @param l the long this <code>LongValue</code> should represent
55    */

56   public LongValue(long l) {
57     super(TYPE);
58     this.lNumber = new Long JavaDoc(l);
59   }
60
61   /**
62    * Returns a new <code>LongValue</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>LongValue</code> representing the
67    * the specified value.
68    * @throws ValueFormatException If the <code>String</code> does not
69    * contain a parsable <code>long</code>.
70    */

71   public static LongValue valueOf(String JavaDoc s) throws ValueFormatException {
72     try {
73       return new LongValue(Long.parseLong(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>LongValue</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 LongValue) {
95       LongValue other = (LongValue) obj;
96       if (lNumber == other.lNumber) {
97         return true;
98       } else if (lNumber != null && other.lNumber != null) {
99         return lNumber.equals(other.lNumber);
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 (lNumber != null) {
113       // loosing timezone information...
114
Calendar JavaDoc cal = Calendar.getInstance();
115       cal.setTime(new Date JavaDoc(lNumber.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 (lNumber != null) {
129       return lNumber.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 (lNumber != null) {
151       return lNumber.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 (lNumber != null) {
164       return lNumber.toString();
165     } else {
166       throw new ValueFormatException("empty value");
167     }
168   }
169 }
170
Popular Tags