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 com.sun.org.apache.xpath.internal.objects;
  20. /**
  21. * This class represents an XPath boolean object, and is capable of
  22. * converting the boolean to other types, such as a string.
  23. * @xsl.usage advanced
  24. */
  25. public class XBoolean extends XObject
  26. {
  27. /**
  28. * A true boolean object so we don't have to keep creating them.
  29. * @xsl.usage internal
  30. */
  31. public static XBoolean S_TRUE = new XBooleanStatic(true);
  32. /**
  33. * A true boolean object so we don't have to keep creating them.
  34. * @xsl.usage internal
  35. */
  36. public static XBoolean S_FALSE = new XBooleanStatic(false);
  37. /** Value of the object.
  38. * @serial */
  39. boolean m_val;
  40. /**
  41. * Construct a XBoolean object.
  42. *
  43. * @param b Value of the boolean object
  44. */
  45. public XBoolean(boolean b)
  46. {
  47. super();
  48. m_val = b;
  49. }
  50. /**
  51. * Construct a XBoolean object.
  52. *
  53. * @param b Value of the boolean object
  54. */
  55. public XBoolean(Boolean b)
  56. {
  57. super();
  58. m_val = b.booleanValue();
  59. m_obj = b;
  60. }
  61. /**
  62. * Tell that this is a CLASS_BOOLEAN.
  63. *
  64. * @return type of CLASS_BOOLEAN
  65. */
  66. public int getType()
  67. {
  68. return CLASS_BOOLEAN;
  69. }
  70. /**
  71. * Given a request type, return the equivalent string.
  72. * For diagnostic purposes.
  73. *
  74. * @return type string "#BOOLEAN"
  75. */
  76. public String getTypeString()
  77. {
  78. return "#BOOLEAN";
  79. }
  80. /**
  81. * Cast result object to a number.
  82. *
  83. * @return numeric value of the object value
  84. */
  85. public double num()
  86. {
  87. return m_val ? 1.0 : 0.0;
  88. }
  89. /**
  90. * Cast result object to a boolean.
  91. *
  92. * @return The object value as a boolean
  93. */
  94. public boolean bool()
  95. {
  96. return m_val;
  97. }
  98. /**
  99. * Cast result object to a string.
  100. *
  101. * @return The object's value as a string
  102. */
  103. public String str()
  104. {
  105. return m_val ? "true" : "false";
  106. }
  107. /**
  108. * Return a java object that's closest to the representation
  109. * that should be handed to an extension.
  110. *
  111. * @return The object's value as a java object
  112. */
  113. public Object object()
  114. {
  115. if(null == m_obj)
  116. m_obj = new Boolean(m_val);
  117. return m_obj;
  118. }
  119. /**
  120. * Tell if two objects are functionally equal.
  121. *
  122. * @param obj2 Object to compare to this
  123. *
  124. * @return True if the two objects are equal
  125. *
  126. * @throws javax.xml.transform.TransformerException
  127. */
  128. public boolean equals(XObject obj2)
  129. {
  130. // In order to handle the 'all' semantics of
  131. // nodeset comparisons, we always call the
  132. // nodeset function.
  133. if (obj2.getType() == XObject.CLASS_NODESET)
  134. return obj2.equals(this);
  135. try
  136. {
  137. return m_val == obj2.bool();
  138. }
  139. catch(javax.xml.transform.TransformerException te)
  140. {
  141. throw new com.sun.org.apache.xml.internal.utils.WrappedRuntimeException(te);
  142. }
  143. }
  144. }