1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the  "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 /*
19  * $Id: XBoolean.java 469368 2006-10-31 04:41:36Z minchau $
20  */
21 package org.apache.xpath.objects;
22 
23 /**
24  * This class represents an XPath boolean object, and is capable of
25  * converting the boolean to other types, such as a string.
26  * @xsl.usage advanced
27  */
28 public class XBoolean extends XObject
29 {
30     static final long serialVersionUID = -2964933058866100881L;
31 
32   /**
33    * A true boolean object so we don't have to keep creating them.
34    * @xsl.usage internal
35    */
36   public static final XBoolean S_TRUE = new XBooleanStatic(true);
37 
38   /**
39    * A true boolean object so we don't have to keep creating them.
40    * @xsl.usage internal
41    */
42   public static final XBoolean S_FALSE = new XBooleanStatic(false);
43 
44   /** Value of the object.
45    *  @serial         */
46   private final boolean m_val;
47 
48   /**
49    * Construct a XBoolean object.
50    *
51    * @param b Value of the boolean object
52    */
XBoolean(boolean b)53   public XBoolean(boolean b)
54   {
55 
56     super();
57 
58     m_val = b;
59   }
60 
61   /**
62    * Construct a XBoolean object.
63    *
64    * @param b Value of the boolean object
65    */
XBoolean(Boolean b)66   public XBoolean(Boolean b)
67   {
68 
69     super();
70 
71     m_val = b.booleanValue();
72     setObject(b);
73   }
74 
75 
76   /**
77    * Tell that this is a CLASS_BOOLEAN.
78    *
79    * @return type of CLASS_BOOLEAN
80    */
getType()81   public int getType()
82   {
83     return CLASS_BOOLEAN;
84   }
85 
86   /**
87    * Given a request type, return the equivalent string.
88    * For diagnostic purposes.
89    *
90    * @return type string "#BOOLEAN"
91    */
getTypeString()92   public String getTypeString()
93   {
94     return "#BOOLEAN";
95   }
96 
97   /**
98    * Cast result object to a number.
99    *
100    * @return numeric value of the object value
101    */
num()102   public double num()
103   {
104     return m_val ? 1.0 : 0.0;
105   }
106 
107   /**
108    * Cast result object to a boolean.
109    *
110    * @return The object value as a boolean
111    */
bool()112   public boolean bool()
113   {
114     return m_val;
115   }
116 
117   /**
118    * Cast result object to a string.
119    *
120    * @return The object's value as a string
121    */
str()122   public String str()
123   {
124     return m_val ? "true" : "false";
125   }
126 
127   /**
128    * Return a java object that's closest to the representation
129    * that should be handed to an extension.
130    *
131    * @return The object's value as a java object
132    */
object()133   public Object object()
134   {
135     if(null == m_obj)
136       setObject(new Boolean(m_val));
137     return m_obj;
138   }
139 
140   /**
141    * Tell if two objects are functionally equal.
142    *
143    * @param obj2 Object to compare to this
144    *
145    * @return True if the two objects are equal
146    *
147    * @throws javax.xml.transform.TransformerException
148    */
equals(XObject obj2)149   public boolean equals(XObject obj2)
150   {
151 
152     // In order to handle the 'all' semantics of
153     // nodeset comparisons, we always call the
154     // nodeset function.
155     if (obj2.getType() == XObject.CLASS_NODESET)
156       return obj2.equals(this);
157 
158     try
159     {
160       return m_val == obj2.bool();
161     }
162     catch(javax.xml.transform.TransformerException te)
163     {
164       throw new org.apache.xml.utils.WrappedRuntimeException(te);
165     }
166   }
167 
168 }
169