1 /*
2  * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
3  */
4 /*
5  * Licensed to the Apache Software Foundation (ASF) under one or more
6  * contributor license agreements.  See the NOTICE file distributed with
7  * this work for additional information regarding copyright ownership.
8  * The ASF licenses this file to You under the Apache License, Version 2.0
9  * (the "License"); you may not use this file except in compliance with
10  * the License.  You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20 
21 package com.sun.org.apache.xpath.internal.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(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 com.sun.org.apache.xml.internal.utils.WrappedRuntimeException(te);
165     }
166   }
167 
168 }
169