KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xpath > objects > XBoolean


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 /*
17  * $Id: XBoolean.java,v 1.16 2004/02/17 04:34:38 minchau Exp $
18  */

19 package org.apache.xpath.objects;
20
21 /**
22  * This class represents an XPath boolean object, and is capable of
23  * converting the boolean to other types, such as a string.
24  * @xsl.usage advanced
25  */

26 public class XBoolean extends XObject
27 {
28
29   /**
30    * A true boolean object so we don't have to keep creating them.
31    * @xsl.usage internal
32    */

33   public static XBoolean S_TRUE = new XBooleanStatic(true);
34
35   /**
36    * A true boolean object so we don't have to keep creating them.
37    * @xsl.usage internal
38    */

39   public static XBoolean S_FALSE = new XBooleanStatic(false);
40
41   /** Value of the object.
42    * @serial */

43   boolean m_val;
44
45   /**
46    * Construct a XBoolean object.
47    *
48    * @param b Value of the boolean object
49    */

50   public XBoolean(boolean b)
51   {
52
53     super();
54
55     m_val = b;
56   }
57   
58   /**
59    * Construct a XBoolean object.
60    *
61    * @param b Value of the boolean object
62    */

63   public XBoolean(Boolean JavaDoc b)
64   {
65
66     super();
67
68     m_val = b.booleanValue();
69     m_obj = b;
70   }
71
72
73   /**
74    * Tell that this is a CLASS_BOOLEAN.
75    *
76    * @return type of CLASS_BOOLEAN
77    */

78   public int getType()
79   {
80     return CLASS_BOOLEAN;
81   }
82
83   /**
84    * Given a request type, return the equivalent string.
85    * For diagnostic purposes.
86    *
87    * @return type string "#BOOLEAN"
88    */

89   public String JavaDoc getTypeString()
90   {
91     return "#BOOLEAN";
92   }
93
94   /**
95    * Cast result object to a number.
96    *
97    * @return numeric value of the object value
98    */

99   public double num()
100   {
101     return m_val ? 1.0 : 0.0;
102   }
103
104   /**
105    * Cast result object to a boolean.
106    *
107    * @return The object value as a boolean
108    */

109   public boolean bool()
110   {
111     return m_val;
112   }
113
114   /**
115    * Cast result object to a string.
116    *
117    * @return The object's value as a string
118    */

119   public String JavaDoc str()
120   {
121     return m_val ? "true" : "false";
122   }
123
124   /**
125    * Return a java object that's closest to the representation
126    * that should be handed to an extension.
127    *
128    * @return The object's value as a java object
129    */

130   public Object JavaDoc object()
131   {
132     if(null == m_obj)
133       m_obj = new Boolean JavaDoc(m_val);
134     return m_obj;
135   }
136
137   /**
138    * Tell if two objects are functionally equal.
139    *
140    * @param obj2 Object to compare to this
141    *
142    * @return True if the two objects are equal
143    *
144    * @throws javax.xml.transform.TransformerException
145    */

146   public boolean equals(XObject obj2)
147   {
148
149     // In order to handle the 'all' semantics of
150
// nodeset comparisons, we always call the
151
// nodeset function.
152
if (obj2.getType() == XObject.CLASS_NODESET)
153       return obj2.equals(this);
154
155     try
156     {
157       return m_val == obj2.bool();
158     }
159     catch(javax.xml.transform.TransformerException JavaDoc te)
160     {
161       throw new org.apache.xml.utils.WrappedRuntimeException(te);
162     }
163   }
164
165 }
166
Popular Tags