001: /*
002: * Copyright 1999-2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: /*
017: * $Id: XBoolean.java,v 1.18 2004/12/15 17:35:55 jycli Exp $
018: */
019: package org.apache.xpath.objects;
020:
021: /**
022: * This class represents an XPath boolean object, and is capable of
023: * converting the boolean to other types, such as a string.
024: * @xsl.usage advanced
025: */
026: public class XBoolean extends XObject {
027: static final long serialVersionUID = -2964933058866100881L;
028:
029: /**
030: * A true boolean object so we don't have to keep creating them.
031: * @xsl.usage internal
032: */
033: public static final XBoolean S_TRUE = new XBooleanStatic(true);
034:
035: /**
036: * A true boolean object so we don't have to keep creating them.
037: * @xsl.usage internal
038: */
039: public static final XBoolean S_FALSE = new XBooleanStatic(false);
040:
041: /** Value of the object.
042: * @serial */
043: private final boolean m_val;
044:
045: /**
046: * Construct a XBoolean object.
047: *
048: * @param b Value of the boolean object
049: */
050: public XBoolean(boolean b) {
051:
052: super ();
053:
054: m_val = b;
055: }
056:
057: /**
058: * Construct a XBoolean object.
059: *
060: * @param b Value of the boolean object
061: */
062: public XBoolean(Boolean b) {
063:
064: super ();
065:
066: m_val = b.booleanValue();
067: m_obj = b;
068: }
069:
070: /**
071: * Tell that this is a CLASS_BOOLEAN.
072: *
073: * @return type of CLASS_BOOLEAN
074: */
075: public int getType() {
076: return CLASS_BOOLEAN;
077: }
078:
079: /**
080: * Given a request type, return the equivalent string.
081: * For diagnostic purposes.
082: *
083: * @return type string "#BOOLEAN"
084: */
085: public String getTypeString() {
086: return "#BOOLEAN";
087: }
088:
089: /**
090: * Cast result object to a number.
091: *
092: * @return numeric value of the object value
093: */
094: public double num() {
095: return m_val ? 1.0 : 0.0;
096: }
097:
098: /**
099: * Cast result object to a boolean.
100: *
101: * @return The object value as a boolean
102: */
103: public boolean bool() {
104: return m_val;
105: }
106:
107: /**
108: * Cast result object to a string.
109: *
110: * @return The object's value as a string
111: */
112: public String str() {
113: return m_val ? "true" : "false";
114: }
115:
116: /**
117: * Return a java object that's closest to the representation
118: * that should be handed to an extension.
119: *
120: * @return The object's value as a java object
121: */
122: public Object object() {
123: if (null == m_obj)
124: m_obj = new Boolean(m_val);
125: return m_obj;
126: }
127:
128: /**
129: * Tell if two objects are functionally equal.
130: *
131: * @param obj2 Object to compare to this
132: *
133: * @return True if the two objects are equal
134: *
135: * @throws javax.xml.transform.TransformerException
136: */
137: public boolean equals(XObject obj2) {
138:
139: // In order to handle the 'all' semantics of
140: // nodeset comparisons, we always call the
141: // nodeset function.
142: if (obj2.getType() == XObject.CLASS_NODESET)
143: return obj2.equals(this );
144:
145: try {
146: return m_val == obj2.bool();
147: } catch (javax.xml.transform.TransformerException te) {
148: throw new org.apache.xml.utils.WrappedRuntimeException(te);
149: }
150: }
151:
152: }
|