1 /*
   2  * reserved comment block
   3  * DO NOT REMOVE OR ALTER!
   4  */
   5 /*
   6  * Copyright 1999-2004 The Apache Software Foundation.
   7  *
   8  * Licensed under the Apache License, Version 2.0 (the "License");
   9  * you may not use this file except in compliance with the License.
  10  * 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  * $Id: XBoolean.java,v 1.2.4.2 2005/09/14 20:34:45 jeffsuttor Exp $
  22  */
  23 package com.sun.org.apache.xpath.internal.objects;
  24 
  25 /**
  26  * This class represents an XPath boolean object, and is capable of
  27  * converting the boolean to other types, such as a string.
  28  * @xsl.usage advanced
  29  */
  30 public class XBoolean extends XObject
  31 {
  32     static final long serialVersionUID = -2964933058866100881L;
  33 
  34   /**
  35    * A true boolean object so we don't have to keep creating them.
  36    * @xsl.usage internal
  37    */
  38   public static final XBoolean S_TRUE = new XBooleanStatic(true);
  39 
  40   /**
  41    * A true boolean object so we don't have to keep creating them.
  42    * @xsl.usage internal
  43    */
  44   public static final XBoolean S_FALSE = new XBooleanStatic(false);
  45 
  46   /** Value of the object.
  47    *  @serial         */
  48   private final boolean m_val;
  49 
  50   /**
  51    * Construct a XBoolean object.
  52    *
  53    * @param b Value of the boolean object
  54    */
  55   public XBoolean(boolean b)
  56   {
  57 
  58     super();
  59 
  60     m_val = b;
  61   }
  62 
  63   /**
  64    * Construct a XBoolean object.
  65    *
  66    * @param b Value of the boolean object
  67    */
  68   public XBoolean(Boolean b)
  69   {
  70 
  71     super();
  72 
  73     m_val = b.booleanValue();
  74     setObject(b);
  75   }
  76 
  77 
  78   /**
  79    * Tell that this is a CLASS_BOOLEAN.
  80    *
  81    * @return type of CLASS_BOOLEAN
  82    */
  83   public int getType()
  84   {
  85     return CLASS_BOOLEAN;
  86   }
  87 
  88   /**
  89    * Given a request type, return the equivalent string.
  90    * For diagnostic purposes.
  91    *
  92    * @return type string "#BOOLEAN"
  93    */
  94   public String getTypeString()
  95   {
  96     return "#BOOLEAN";
  97   }
  98 
  99   /**
 100    * Cast result object to a number.
 101    *
 102    * @return numeric value of the object value
 103    */
 104   public double num()
 105   {
 106     return m_val ? 1.0 : 0.0;
 107   }
 108 
 109   /**
 110    * Cast result object to a boolean.
 111    *
 112    * @return The object value as a boolean
 113    */
 114   public boolean bool()
 115   {
 116     return m_val;
 117   }
 118 
 119   /**
 120    * Cast result object to a string.
 121    *
 122    * @return The object's value as a string
 123    */
 124   public String str()
 125   {
 126     return m_val ? "true" : "false";
 127   }
 128 
 129   /**
 130    * Return a java object that's closest to the representation
 131    * that should be handed to an extension.
 132    *
 133    * @return The object's value as a java object
 134    */
 135   public Object object()
 136   {
 137     if(null == m_obj)
 138       setObject(Boolean.valueOf(m_val));
 139     return m_obj;
 140   }
 141 
 142   /**
 143    * Tell if two objects are functionally equal.
 144    *
 145    * @param obj2 Object to compare to this
 146    *
 147    * @return True if the two objects are equal
 148    *
 149    * @throws javax.xml.transform.TransformerException
 150    */
 151   public boolean equals(XObject obj2)
 152   {
 153 
 154     // In order to handle the 'all' semantics of
 155     // nodeset comparisons, we always call the
 156     // nodeset function.
 157     if (obj2.getType() == XObject.CLASS_NODESET)
 158       return obj2.equals(this);
 159 
 160     try
 161     {
 162       return m_val == obj2.bool();
 163     }
 164     catch(javax.xml.transform.TransformerException te)
 165     {
 166       throw new com.sun.org.apache.xml.internal.utils.WrappedRuntimeException(te);
 167     }
 168   }
 169 
 170 }