KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > jcr > StringValue


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

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

46   public StringValue(String JavaDoc text) {
47     super(TYPE);
48     this.text = text;
49   }
50
51   /**
52    * Indicates whether some other object is "equal to" this one.
53    * <p/>
54    * The result is <code>true</code> if and only if the argument is not
55    * <code>null</code> and is a <code>StringValue</code> object that
56    * represents the same value as this object.
57    *
58    * @param obj the reference object with which to compare.
59    * @return <code>true</code> if this object is the same as the obj
60    * argument; <code>false</code> otherwise.
61    */

62   public boolean equals(Object JavaDoc obj) {
63     if (this == obj) {
64       return true;
65     }
66     if (obj instanceof StringValue) {
67       StringValue other = (StringValue) obj;
68       if (text == other.text) {
69         return true;
70       } else if (text != null && other.text != null) {
71         return text.equals(other.text);
72       }
73     }
74     return false;
75   }
76
77   //----------------------------------------------------------------< Value >
78
/**
79    * @see Value#getDate
80    */

81   public Calendar JavaDoc getDate() throws ValueFormatException, IllegalStateException JavaDoc, RepositoryException {
82     setValueConsumed();
83
84     if (text != null) {
85       Calendar JavaDoc cal = ISO8601.parse(text);
86       if (cal == null) {
87         throw new ValueFormatException("not a valid date format");
88       } else {
89         return cal;
90       }
91     } else {
92       throw new ValueFormatException("empty value");
93     }
94   }
95
96   /**
97    * @see Value#getLong
98    */

99   public long getLong() throws ValueFormatException, IllegalStateException JavaDoc, RepositoryException {
100     setValueConsumed();
101
102     if (text != null) {
103       try {
104         return Long.parseLong(text);
105       } catch (NumberFormatException JavaDoc e) {
106         throw new ValueFormatException("conversion to long failed", e);
107       }
108     } else {
109       throw new ValueFormatException("empty value");
110     }
111   }
112
113   /**
114    * @see Value#getBoolean
115    */

116   public boolean getBoolean() throws ValueFormatException, IllegalStateException JavaDoc, RepositoryException {
117     setValueConsumed();
118
119     if (text != null) {
120       return Boolean.valueOf(text).booleanValue();
121     } else {
122       throw new ValueFormatException("empty value");
123     }
124   }
125
126   /**
127    * @see Value#getDouble
128    */

129   public double getDouble() throws ValueFormatException, IllegalStateException JavaDoc, RepositoryException {
130     setValueConsumed();
131
132     if (text != null) {
133       try {
134         return Double.parseDouble(text);
135       } catch (NumberFormatException JavaDoc e) {
136         throw new ValueFormatException("conversion to double failed", e);
137       }
138     } else {
139       throw new ValueFormatException("empty value");
140     }
141   }
142
143   /**
144    * @see Value#getString
145    */

146   public String JavaDoc getString() throws ValueFormatException, IllegalStateException JavaDoc, RepositoryException {
147     setValueConsumed();
148
149     if (text != null) {
150       return text;
151     } else {
152       throw new ValueFormatException("empty value");
153     }
154   }
155 }
156
Popular Tags