KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > jcr > DateValue


1 /*
2  * $Id: DateValue.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 javax.jcr.util.ISO8601;
27 import java.util.Calendar JavaDoc;
28
29 /**
30  * A <code>DateValue</code> provides an implementation
31  * of the <code>Value</code> interface representing a date value.
32  *
33  * @author Stefan Guggisberg
34  */

35 public class DateValue extends BaseValue {
36
37   public static final int TYPE = PropertyType.DATE;
38
39   private final Calendar JavaDoc date;
40
41   /**
42    * Constructs a <code>DateValue</code> object representing a date.
43    *
44    * @param date the date this <code>DateValue</code> should represent
45    * s
46    */

47   public DateValue(Calendar JavaDoc date) {
48     super(TYPE);
49     this.date = date;
50   }
51
52   /**
53    * Returns a new <code>DateValue</code> initialized to the value
54    * represented by the specified <code>String</code>.
55    * <p/>
56    * The specified <code>String</code> must be a ISO8601-compliant date/time
57    * string.
58    *
59    * @param s the string to be parsed.
60    * @return a newly constructed <code>DateValue</code> representing the
61    * the specified value.
62    * @throws ValueFormatException If the <code>String</code> is not a valid
63    * ISO8601-compliant date/time string.
64    * @see javax.jcr.util.ISO8601
65    */

66   public static DateValue valueOf(String JavaDoc s) throws ValueFormatException {
67     Calendar JavaDoc cal = ISO8601.parse(s);
68     if (cal != null) {
69       return new DateValue(cal);
70     } else {
71       throw new ValueFormatException("not a valid date format");
72     }
73   }
74
75   /**
76    * Indicates whether some other object is "equal to" this one.
77    * <p/>
78    * The result is <code>true</code> if and only if the argument is not
79    * <code>null</code> and is a <code>DateValue</code> object that
80    * represents the same value as this object.
81    *
82    * @param obj the reference object with which to compare.
83    * @return <code>true</code> if this object is the same as the obj
84    * argument; <code>false</code> otherwise.
85    */

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

105   public Calendar JavaDoc getDate() throws ValueFormatException, IllegalStateException JavaDoc, RepositoryException {
106     setValueConsumed();
107
108     if (date != null) {
109       return date;
110     } else {
111       throw new ValueFormatException("empty value");
112     }
113   }
114
115   /**
116    * @see Value#getLong
117    */

118   public long getLong() throws ValueFormatException, IllegalStateException JavaDoc, RepositoryException {
119     setValueConsumed();
120
121     if (date != null) {
122       return date.getTime().getTime();
123     } else {
124       throw new ValueFormatException("empty value");
125     }
126   }
127
128   /**
129    * @see Value#getBoolean
130    */

131   public boolean getBoolean() throws ValueFormatException, IllegalStateException JavaDoc, RepositoryException {
132     setValueConsumed();
133
134     if (date != null) {
135       throw new ValueFormatException("cannot convert date to boolean");
136     } else {
137       throw new ValueFormatException("empty value");
138     }
139   }
140
141   /**
142    * @see Value#getDouble
143    */

144   public double getDouble() throws ValueFormatException, IllegalStateException JavaDoc, RepositoryException {
145     setValueConsumed();
146
147     if (date != null) {
148       long ms = date.getTime().getTime();
149       if (ms <= Double.MAX_VALUE) {
150         return ms;
151       }
152       throw new ValueFormatException("conversion from date to double failed: inconvertible types");
153     } else {
154       throw new ValueFormatException("empty value");
155     }
156   }
157
158   /**
159    * @see Value#getString
160    */

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