001: /*=============================================================================
002: * Copyright Texas Instruments 2000-2004. All Rights Reserved.
003: *
004: * This program is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2 of the License, or (at your option) any later version.
008: *
009: * This program is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: *
018: * $ProjectHeader: OSCRIPT 0.155 Fri, 20 Dec 2002 18:34:22 -0800 rclark $
019: */
020:
021: package oscript.data;
022:
023: import oscript.exceptions.*;
024:
025: /**
026: * A boolean type, can have either the value <i>true</i> or <i>false</i>.
027: *
028: * @author Rob Clark (rob@ti.com)
029: */
030: public class OBoolean extends OObject implements java.io.Externalizable {
031: /**
032: * The type object for an instance of Boolean.
033: */
034: public final static Value TYPE = BuiltinType
035: .makeBuiltinType("oscript.data.OBoolean");
036: public final static String PARENT_TYPE_NAME = "oscript.data.OObject";
037: public final static String TYPE_NAME = "Boolean";
038: public final static String[] MEMBER_NAMES = new String[] {
039: "castToBoolean", "castToString", "castToJavaObject",
040: "bopLogicalOr", "bopLogicalOrR", "bopLogicalAnd",
041: "bopLogicalAndR", "bopEquals", "bopEqualsR",
042: "bopNotEquals", "bopNotEqualsR", "uopLogicalNot" };
043:
044: public static final OBoolean TRUE = new OBoolean(true);
045: public static final OBoolean FALSE = new OBoolean(false);
046:
047: /*=======================================================================*/
048: /**
049: */
050: public static final OBoolean makeBoolean(boolean booleanVal) {
051: return booleanVal ? TRUE : FALSE;
052: }
053:
054: /*=======================================================================*/
055: // members:
056: private boolean booleanVal;
057:
058: // Externalizable support:
059: public OBoolean() {
060: }
061:
062: /**
063: * Derived class that implements {@link java.io.Externalizable} must
064: * call this if it overrides it. It should override it to save/restore
065: * it's own state.
066: */
067: public void readExternal(java.io.ObjectInput in)
068: throws java.io.IOException {
069: booleanVal = in.readBoolean();
070: }
071:
072: /**
073: * Derived class that implements {@link java.io.Externalizable} must
074: * call this if it overrides it. It should override it to save/restore
075: * it's own state.
076: */
077: public void writeExternal(java.io.ObjectOutput out)
078: throws java.io.IOException {
079: out.writeBoolean(booleanVal);
080: }
081:
082: /*=======================================================================*/
083:
084: /*=======================================================================*/
085: /**
086: * Class Constructor
087: *
088: * @param booleanVal the value of this boolean
089: */
090: public OBoolean(boolean booleanVal) {
091: super ();
092: this .booleanVal = booleanVal;
093: }
094:
095: /*=======================================================================*/
096: /**
097: * Class Constructor. This is the constructor that is called via the
098: * <code>BuiltinType</code> instance.
099: *
100: * @param args arguments to this constructor
101: * @throws PackagedScriptObjectException(Exception) if wrong number of args
102: */
103: public OBoolean(oscript.util.MemberTable args) {
104: super ();
105:
106: if (args.length() != 1) {
107: throw PackagedScriptObjectException
108: .makeExceptionWrapper(new OIllegalArgumentException(
109: "wrong number of args!"));
110: } else {
111: booleanVal = args.referenceAt(0).castToBoolean();
112: }
113: }
114:
115: /*=======================================================================*/
116: /**
117: * Get the type of this object. The returned type doesn't have to take
118: * into account the possibility of a script type extending a built-in
119: * type, since that is handled by {@link #getType}.
120: *
121: * @return the object's type
122: */
123: protected Value getTypeImpl() {
124: return TYPE;
125: }
126:
127: /*=======================================================================*/
128: /**
129: * Convert this object to a native java <code>boolean</code> value.
130: *
131: * @return a boolean value
132: * @throws PackagedScriptObjectException(NoSuchMethodException)
133: */
134: public boolean castToBoolean() throws PackagedScriptObjectException {
135: return booleanVal;
136: }
137:
138: /*=======================================================================*/
139: /**
140: * Convert this object to a native java <code>String</code> value.
141: *
142: * @return a String value
143: * @throws PackagedScriptObjectException(NoSuchMethodException)
144: */
145: public String castToString() throws PackagedScriptObjectException {
146: return booleanVal ? "true" : "false";
147: }
148:
149: /*=======================================================================*/
150: /**
151: * Convert this object to a native java <code>Object</code> value.
152: *
153: * @return a java object
154: * @throws PackagedScriptObjectException(NoSuchMemberException)
155: */
156: public Object castToJavaObject()
157: throws PackagedScriptObjectException {
158: return booleanVal ? Boolean.TRUE : Boolean.FALSE;
159: }
160:
161: /*=======================================================================*/
162: /* The binary operators:
163: */
164:
165: /*=======================================================================*/
166: /**
167: * Perform the logical OR operation.
168: *
169: * @param val the other value
170: * @return the result
171: * @throws PackagedScriptObjectException(NoSuchMethodException)
172: */
173: public Value bopLogicalOr(Value val)
174: throws PackagedScriptObjectException {
175: try {
176: return makeBoolean(booleanVal || val.castToBoolean());
177: } catch (PackagedScriptObjectException e) {
178: return val.bopLogicalOrR(this , e);
179: }
180: }
181:
182: public Value bopLogicalOrR(Value val,
183: PackagedScriptObjectException e)
184: throws PackagedScriptObjectException {
185: return makeBoolean(val.castToBoolean() || booleanVal);
186: }
187:
188: /*=======================================================================*/
189: /**
190: * Perform the logical AND operation.
191: *
192: * @param val the other value
193: * @return the result
194: * @throws PackagedScriptObjectException(NoSuchMethodException)
195: */
196: public Value bopLogicalAnd(Value val)
197: throws PackagedScriptObjectException {
198: try {
199: return makeBoolean(booleanVal && val.castToBoolean());
200: } catch (PackagedScriptObjectException e) {
201: return val.bopLogicalAndR(this , e);
202: }
203: }
204:
205: public Value bopLogicalAndR(Value val,
206: PackagedScriptObjectException e)
207: throws PackagedScriptObjectException {
208: return makeBoolean(val.castToBoolean() && booleanVal);
209: }
210:
211: /*=======================================================================*/
212: /**
213: * Perform the "==" operation.
214: *
215: * @param val the other value
216: * @return the result
217: * @throws PackagedScriptObjectException(NoSuchMethodException)
218: */
219: public Value bopEquals(Value val)
220: throws PackagedScriptObjectException {
221: try {
222: return makeBoolean(booleanVal == val.castToBoolean());
223: } catch (PackagedScriptObjectException e) {
224: return val.bopEqualsR(this , e);
225: }
226: }
227:
228: public Value bopEqualsR(Value val, PackagedScriptObjectException e)
229: throws PackagedScriptObjectException {
230: return makeBoolean(val.castToBoolean() == booleanVal);
231: }
232:
233: /*=======================================================================*/
234: /**
235: * Perform the "!=" operation.
236: *
237: * @param val the other value
238: * @return the result
239: * @throws PackagedScriptObjectException(NoSuchMethodException)
240: */
241: public Value bopNotEquals(Value val)
242: throws PackagedScriptObjectException {
243: try {
244: return makeBoolean(booleanVal != val.castToBoolean());
245: } catch (PackagedScriptObjectException e) {
246: return val.bopNotEqualsR(this , e);
247: }
248: }
249:
250: public Value bopNotEqualsR(Value val,
251: PackagedScriptObjectException e)
252: throws PackagedScriptObjectException {
253: return makeBoolean(val.castToBoolean() != booleanVal);
254: }
255:
256: /*=======================================================================*/
257: /**
258: * Perform the "!" operation.
259: *
260: * @param val the other value
261: * @return the result
262: * @throws PackagedScriptObjectException(NoSuchMethodException)
263: */
264: public Value uopLogicalNot() throws PackagedScriptObjectException {
265: return makeBoolean(!booleanVal);
266: }
267: }
268:
269: /*
270: * Local Variables:
271: * tab-width: 2
272: * indent-tabs-mode: nil
273: * mode: java
274: * c-indentation-style: java
275: * c-basic-offset: 2
276: * eval: (c-set-offset 'substatement-open '0)
277: * eval: (c-set-offset 'case-label '+)
278: * eval: (c-set-offset 'inclass '+)
279: * eval: (c-set-offset 'inline-open '0)
280: * End:
281: */
|