PageRenderTime 45ms CodeModel.GetById 16ms app.highlight 22ms RepoModel.GetById 2ms app.codeStats 0ms

/interpreter/tags/at2-build270707/src/edu/vub/at/objects/natives/NATBoolean.java

http://ambienttalk.googlecode.com/
Java | 168 lines | 89 code | 38 blank | 41 comment | 2 complexity | 057167152f89b20cc63d513fb1f0a0c2 MD5 | raw file
  1/**
  2 * AmbientTalk/2 Project
  3 * NATBoolean.java created on Jul 23, 2006 at 12:52:29 PM
  4 * (c) Programming Technology Lab, 2006 - 2007
  5 * Authors: Tom Van Cutsem & Stijn Mostinckx
  6 * 
  7 * Permission is hereby granted, free of charge, to any person
  8 * obtaining a copy of this software and associated documentation
  9 * files (the "Software"), to deal in the Software without
 10 * restriction, including without limitation the rights to use,
 11 * copy, modify, merge, publish, distribute, sublicense, and/or
 12 * sell copies of the Software, and to permit persons to whom the
 13 * Software is furnished to do so, subject to the following
 14 * conditions:
 15 *
 16 * The above copyright notice and this permission notice shall be
 17 * included in all copies or substantial portions of the Software.
 18 *
 19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 20 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 21 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 22 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 23 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 24 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 25 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 26 * OTHER DEALINGS IN THE SOFTWARE.
 27 */
 28package edu.vub.at.objects.natives;
 29
 30import edu.vub.at.exceptions.InterpreterException;
 31import edu.vub.at.objects.ATBoolean;
 32import edu.vub.at.objects.ATClosure;
 33import edu.vub.at.objects.ATObject;
 34import edu.vub.at.objects.ATTable;
 35import edu.vub.at.objects.coercion.NativeTypeTags;
 36
 37/**
 38 * NATBoolean is simply a container class for ambienttalk booleans. The native 
 39 * implementations of true and false can be accessed using the class's atValue
 40 * method.
 41 * 
 42 * @author smostinc
 43 */
 44public abstract class NATBoolean extends NATByCopy implements ATBoolean {
 45	
 46	/**
 47	 * Returns the corresponding ATBoolean given a java truth value. This constructor
 48	 * function is to be used only when the result to be given out depends on a 
 49	 * dynamic test, else the static fields _TRUE_ and _FALSE_ should be used instead.
 50	 */
 51	public static ATBoolean atValue(boolean b) {
 52		if (b) {
 53			return _TRUE_;
 54		} else {
 55			return _FALSE_;
 56		}
 57	}
 58	
 59	public final boolean javaValue;
 60	
 61	private NATBoolean(boolean b) {
 62		javaValue = b;
 63	}
 64	
 65	public boolean isNativeBoolean() {
 66		return true;
 67	}
 68
 69	public ATBoolean asBoolean() {
 70		return this;
 71	}
 72	
 73	public NATBoolean asNativeBoolean() {
 74		return this;
 75	}
 76	
 77	public ATObject meta_clone() throws InterpreterException {
 78		return this;
 79	}
 80	
 81    public ATTable meta_typeTags() throws InterpreterException {
 82    	return NATTable.of(NativeTypeTags._BOOLEAN_, NativeTypeTags._ISOLATE_);
 83    }
 84	
 85	public static class NATTrue extends NATBoolean {
 86		
 87		public static NATTrue _INSTANCE_ = new NATTrue();
 88		
 89		public NATTrue() { super(true); }
 90		
 91		public NATText meta_print() throws InterpreterException { return NATText.atValue("true"); }
 92		
 93		// base interface for true
 94		
 95		public ATObject base_ifTrue_(ATClosure clo) throws InterpreterException {
 96			return clo.base_apply(NATTable.EMPTY);
 97		}
 98
 99		public ATObject base_ifFalse_(ATClosure clo) throws InterpreterException {
100			return OBJNil._INSTANCE_;
101		}
102		
103		public ATObject base_ifTrue_ifFalse_(ATClosure consequent, ATClosure alternative) throws InterpreterException {
104			return consequent.base_apply(NATTable.EMPTY);
105		}
106		
107		public ATBoolean base_and_(ATClosure other) throws InterpreterException {
108			return other.base_apply(NATTable.EMPTY).asBoolean();
109		}
110		
111		public ATBoolean base_or_(ATClosure other) throws InterpreterException {
112			return this;
113		}
114		
115		public ATBoolean base_not() {
116			return NATFalse._INSTANCE_;
117		}
118		
119		public ATObject meta_resolve() throws InterpreterException {
120			return NATTrue._INSTANCE_;
121		}
122		
123	}
124
125	public static class NATFalse extends NATBoolean {
126		
127		public static NATFalse _INSTANCE_ = new NATFalse();
128		
129		public NATFalse() { super(false); }
130		
131		public NATText meta_print() throws InterpreterException { return NATText.atValue("false"); }
132
133		// base interface for false
134		
135		public ATObject base_ifTrue_(ATClosure clo) throws InterpreterException {
136			return OBJNil._INSTANCE_;
137		}
138
139		public ATObject base_ifFalse_(ATClosure clo) throws InterpreterException {
140			return clo.base_apply(NATTable.EMPTY);
141		}
142		
143		public ATObject base_ifTrue_ifFalse_(ATClosure consequent, ATClosure alternative) throws InterpreterException {
144			return alternative.base_apply(NATTable.EMPTY);
145		}
146		
147		public ATBoolean base_and_(ATClosure other) throws InterpreterException {
148			return this;
149		}
150		
151		public ATBoolean base_or_(ATClosure other) throws InterpreterException {
152			return other.base_apply(NATTable.EMPTY).asBoolean();
153		}
154		
155		public ATBoolean base_not() {
156			return NATTrue._INSTANCE_;
157		}
158		
159		public ATObject meta_resolve() throws InterpreterException {
160			return NATFalse._INSTANCE_;
161		}
162		
163	}
164	
165	public static final NATTrue _TRUE_ = NATTrue._INSTANCE_;
166	public static final NATFalse _FALSE_ = NATFalse._INSTANCE_;
167
168}