KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > jcr > BooleanValue


1 /*
2  * $Id: BooleanValue.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
28 /**
29  * A <code>BooleanValue</code> provides an implementation
30  * of the <code>Value</code> interface representing a boolean value.
31  *
32  * @author Stefan Guggisberg
33  */

34 public class BooleanValue extends BaseValue {
35
36   public static final int TYPE = PropertyType.BOOLEAN;
37
38   private final Boolean JavaDoc bool;
39
40   /**
41    * Constructs a <code>BooleanValue</code> object representing a boolean.
42    *
43    * @param bool the boolean this <code>BooleanValue</code> should represent
44    */

45   public BooleanValue(Boolean JavaDoc bool) {
46     super(TYPE);
47     this.bool = bool;
48   }
49
50   /**
51    * Constructs a <code>BooleanValue</code> object representing a boolean.
52    *
53    * @param bool the boolean this <code>BooleanValue</code> should represent
54    */

55   public BooleanValue(boolean bool) {
56     super(TYPE);
57     this.bool = new Boolean JavaDoc(bool);
58   }
59
60   /**
61    * Returns a new <code>BooleanValue</code> initialized to the value
62    * represented by the specified <code>String</code>.
63    *
64    * @param s the string to be parsed.
65    * @return a newly constructed <code>BooleanValue</code> representing the
66    * the specified value.
67    */

68   public static BooleanValue valueOf(String JavaDoc s) {
69     return new BooleanValue(Boolean.valueOf(s));
70   }
71
72   /**
73    * Indicates whether some other object is "equal to" this one.
74    * <p/>
75    * The result is <code>true</code> if and only if the argument is not
76    * <code>null</code> and is a <code>BooleanValue</code> object that
77    * represents the same value as this object.
78    *
79    * @param obj the reference object with which to compare.
80    * @return <code>true</code> if this object is the same as the obj
81    * argument; <code>false</code> otherwise.
82    */

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

102   public Calendar JavaDoc getDate() throws ValueFormatException, IllegalStateException JavaDoc, RepositoryException {
103     setValueConsumed();
104
105     throw new ValueFormatException("conversion to date failed: inconvertible types");
106   }
107
108   /**
109    * @see Value#getLong
110    */

111   public long getLong() throws ValueFormatException, IllegalStateException JavaDoc, RepositoryException {
112     setValueConsumed();
113
114     throw new ValueFormatException("conversion to long failed: inconvertible types");
115   }
116
117   /**
118    * @see Value#getBoolean
119    */

120   public boolean getBoolean() throws ValueFormatException, IllegalStateException JavaDoc, RepositoryException {
121     setValueConsumed();
122
123     if (bool != null) {
124       return bool.booleanValue();
125     } else {
126       throw new ValueFormatException("empty value");
127     }
128   }
129
130   /**
131    * @see Value#getDouble
132    */

133   public double getDouble() throws ValueFormatException, IllegalStateException JavaDoc, RepositoryException {
134     setValueConsumed();
135
136     throw new ValueFormatException("conversion to double failed: inconvertible types");
137   }
138
139   /**
140    * @see Value#getString
141    */

142   public String JavaDoc getString() throws ValueFormatException, IllegalStateException JavaDoc, RepositoryException {
143     setValueConsumed();
144
145     if (bool != null) {
146       return bool.toString();
147     } else {
148       throw new ValueFormatException("empty value");
149     }
150   }
151 }
152
Popular Tags