001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements. See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership. The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the  "License");
007     * you may not use this file except in compliance with the License.
008     * You may obtain a copy of the License at
009     *
010     *     http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing, software
013     * distributed under the License is distributed on an "AS IS" BASIS,
014     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    /*
019     * $Id: XBoolean.java 1225282 2011-12-28 18:55:38Z mrglavas $
020     */
021    package org.apache.xpath.objects;
022    
023    /**
024     * This class represents an XPath boolean object, and is capable of
025     * converting the boolean to other types, such as a string.
026     * @xsl.usage advanced
027     */
028    public class XBoolean extends XObject
029    {
030        static final long serialVersionUID = -2964933058866100881L;
031    
032      /**
033       * A true boolean object so we don't have to keep creating them.
034       * @xsl.usage internal
035       */
036      public static final XBoolean S_TRUE = new XBooleanStatic(true);
037    
038      /**
039       * A true boolean object so we don't have to keep creating them.
040       * @xsl.usage internal
041       */
042      public static final XBoolean S_FALSE = new XBooleanStatic(false);
043    
044      /** Value of the object.
045       *  @serial         */
046      private final boolean m_val;
047    
048      /**
049       * Construct a XBoolean object.
050       *
051       * @param b Value of the boolean object
052       */
053      public XBoolean(boolean b)
054      {
055    
056        super();
057    
058        m_val = b;
059      }
060      
061      /**
062       * Construct a XBoolean object.
063       *
064       * @param b Value of the boolean object
065       */
066      public XBoolean(Boolean b)
067      {
068    
069        super();
070    
071        m_val = b.booleanValue();
072        setObject(b);
073      }
074    
075    
076      /**
077       * Tell that this is a CLASS_BOOLEAN.
078       *
079       * @return type of CLASS_BOOLEAN
080       */
081      public int getType()
082      {
083        return CLASS_BOOLEAN;
084      }
085    
086      /**
087       * Given a request type, return the equivalent string.
088       * For diagnostic purposes.
089       *
090       * @return type string "#BOOLEAN"
091       */
092      public String getTypeString()
093      {
094        return "#BOOLEAN";
095      }
096    
097      /**
098       * Cast result object to a number.
099       *
100       * @return numeric value of the object value
101       */
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       */
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       */
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       */
133      public Object object()
134      {
135        if(null == m_obj)
136          setObject(m_val ? Boolean.TRUE : Boolean.FALSE);
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       */
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    }