001: /*=============================================================================
002: * Copyright Texas Instruments 2000-2003. 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:
019: package oscript.data;
020:
021: import oscript.exceptions.*;
022:
023: /**
024: * An <code>OSpecial</code> is used for different special values that
025: * aren't really members of any other type. The values of Special are
026: * unique, and special care is taken when constructing, and serializing
027: * and unserializing the special to ensure that this property is
028: * preservied... ie. there are never two instances of <code>NULL</code>.
029: *
030: * @author Rob Clark (rob@ti.com)
031: */
032: class OSpecial extends Value {
033: private static java.util.Hashtable specials = new java.util.Hashtable();
034:
035: private String str;
036:
037: static Value makeSpecial(String str) {
038: Value val = null;
039:
040: synchronized (specials) {
041: val = (Value) (specials.get(str));
042:
043: if (val == null) {
044: val = new OSpecial(str);
045: specials.put(str, val);
046: }
047: }
048:
049: return val;
050: }
051:
052: /*=======================================================================*/
053: /**
054: * Class Constructor.
055: *
056: * @param str the string representation of this special
057: */
058: protected OSpecial(String str) {
059: super ();
060:
061: synchronized (specials) {
062: if (specials.get(str) != null)
063: throw new ProgrammingErrorException(
064: "duplicate special!");
065: }
066:
067: this .str = str;
068: }
069:
070: /*=======================================================================*/
071: /**
072: * Class Constructor.
073: *
074: * @param args arguments to this constructor
075: * @throws PackagedScriptObjectException(Exception) if wrong number of args
076: */
077: public OSpecial(oscript.util.MemberTable args) {
078: super ();
079:
080: // you can't instantiate this!
081: throw PackagedScriptObjectException
082: .makeExceptionWrapper(new OIllegalArgumentException(
083: "wrong number of args!"));
084: }
085:
086: /*=======================================================================*/
087: /**
088: * Get the type of this object. The returned type doesn't have to take
089: * into account the possibility of a script type extending a built-in
090: * type, since that is handled by {@link #getType}.
091: *
092: * @return the object's type
093: */
094: protected Value getTypeImpl() {
095: return this ;
096: }
097:
098: /*=======================================================================*/
099: /**
100: * If this object is a type, determine if an instance of this type is
101: * an instance of the specified type, ie. if this is <code>type</code>,
102: * or a subclass.
103: *
104: * @param type the type to compare this type to
105: * @return <code>true</code> or <code>false</code>
106: * @throws PackagedScriptObjectException(NoSuchMemberException)
107: */
108: public boolean isA(Value type) {
109: return bopEquals(type).castToBoolean();
110: }
111:
112: /*=======================================================================*/
113: /**
114: * Convert this object to a native java <code>String</code> value.
115: *
116: * @return a String value
117: * @throws PackagedScriptObjectException(NoSuchMemberException)
118: */
119: public String castToString() throws PackagedScriptObjectException {
120: return str;
121: }
122:
123: /*=======================================================================*/
124: /**
125: * Convert this object to a native java <code>Object</code> value.
126: *
127: * @return a java object
128: * @throws PackagedScriptObjectException(NoSuchMemberException)
129: */
130: public Object castToJavaObject()
131: throws PackagedScriptObjectException {
132: return null;
133: }
134:
135: protected PackagedScriptObjectException noSuchMember(String member) {
136: return PackagedScriptObjectException
137: .makeExceptionWrapper(new ONullReferenceException(str));
138: }
139:
140: /*=======================================================================*/
141: // maintains unique-ness of a OSpecial:
142: Object readResolve() throws java.io.ObjectStreamException {
143: Object obj;
144:
145: synchronized (specials) {
146: obj = specials.get(str);
147:
148: if (obj == null) {
149: specials.put(str, this );
150: obj = this ;
151: }
152: }
153:
154: return obj;
155: }
156: }
157:
158: /*
159: * Local Variables:
160: * tab-width: 2
161: * indent-tabs-mode: nil
162: * mode: java
163: * c-indentation-style: java
164: * c-basic-offset: 2
165: * eval: (c-set-offset 'substatement-open '0)
166: * eval: (c-set-offset 'case-label '+)
167: * eval: (c-set-offset 'inclass '+)
168: * eval: (c-set-offset 'inline-open '0)
169: * End:
170: */
|