PageRenderTime 27ms CodeModel.GetById 7ms app.highlight 15ms RepoModel.GetById 1ms app.codeStats 1ms

/interpreter/tags/at_build150307/src/edu/vub/at/objects/natives/NATStripe.java

http://ambienttalk.googlecode.com/
Java | 182 lines | 92 code | 24 blank | 66 comment | 9 complexity | 97021a2b5bb00bceb33f418112b49203 MD5 | raw file
  1/**
  2 * AmbientTalk/2 Project
  3 * NATStripe.java created on 18-feb-2007 at 15:59:20
  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.ATObject;
 33import edu.vub.at.objects.ATStripe;
 34import edu.vub.at.objects.ATTable;
 35import edu.vub.at.objects.coercion.NativeStripes;
 36import edu.vub.at.objects.grammar.ATSymbol;
 37import edu.vub.at.objects.mirrors.NativeClosure;
 38import edu.vub.at.objects.natives.grammar.AGSymbol;
 39
 40/**
 41 * The native implementation of AmbientTalk stripe objects.
 42 *
 43 * @author tvcutsem
 44 */
 45public class NATStripe extends NATByCopy implements ATStripe {
 46
 47	private final ATSymbol stripeName_;
 48	private final ATTable parentStripes_;
 49	
 50	public static ATStripe[] toStripeArray(ATTable stripes) throws InterpreterException {
 51		if (stripes == NATTable.EMPTY) {
 52			return NATObject._NO_STRIPES_;
 53		}
 54		ATObject[] unwrapped = stripes.asNativeTable().elements_;
 55		ATStripe[] unwrappedStripes = new ATStripe[unwrapped.length];
 56		for (int i = 0; i < unwrappedStripes.length; i++) {
 57			unwrappedStripes[i] = unwrapped[i].asStripe();
 58		}
 59		return unwrappedStripes;
 60	}
 61	
 62	public static NATStripe atValue(String stripeName) {
 63		return atValue(AGSymbol.jAlloc(stripeName));
 64	}
 65	
 66	public static NATStripe atValue(ATSymbol stripeName) {
 67		return new NATStripe(stripeName,
 68				             NATTable.atValue(new ATObject[] { OBJRootStripe._INSTANCE_ }));
 69	}
 70	
 71	public static NATStripe atValue(String stripeName, NATStripe singleParent) {
 72		return new NATStripe(AGSymbol.jAlloc(stripeName),
 73				             NATTable.atValue(new ATObject[] { singleParent }));
 74	}
 75	
 76	/**
 77	 * Stripes should not be created directly because it should be verified
 78	 * that their list of parent stripes is never empty. Stripes created
 79	 * with an empty parent list automatically get assigned the root stripe
 80	 * as their single parent.
 81	 */
 82	public static NATStripe atValue(ATSymbol stripeName, ATTable parentStripes) {
 83		if (parentStripes == NATTable.EMPTY) {
 84			return new NATStripe(stripeName, NATTable.atValue(new ATObject[] { OBJRootStripe._INSTANCE_ }));
 85		} else {
 86			return new NATStripe(stripeName, parentStripes);
 87		}
 88	}
 89	
 90	/**
 91	 * The constructor is declared protected such that it cannot be used externally,
 92	 * but can be used by the OBJRootStripe class to create a stripe with an empty
 93	 * parent table, which is normally not allowed. Hence, by construction the only
 94	 * stripe with an empty parent table is the root stripe. 
 95	 */
 96	protected NATStripe(ATSymbol stripeName, ATTable parentStripes) {
 97		stripeName_ = stripeName;
 98		parentStripes_ = parentStripes;
 99	}
100
101	public ATSymbol base_getStripeName() throws InterpreterException {
102		return stripeName_;
103	}
104
105	public ATTable base_getParentStripes() throws InterpreterException {
106		return parentStripes_;
107	}
108
109	/**
110	 * Native implementation of:
111	 * 
112	 *	def isSubstripeOf(superstripe) {
113	 *		  (superstripe.name() == name).or:
114	 *			  { (superstripes.find: { |sstripe|
115	 *				  sstripe.isSubstripeOf(superstripe) }) != nil }
116	 *	};
117	 */
118	public ATBoolean base_isSubstripeOf(final ATStripe superstripe) throws InterpreterException {
119		if (superstripe.base_getStripeName().equals(stripeName_)) {
120			return NATBoolean._TRUE_;
121		} else {
122			ATObject found = parentStripes_.base_find_(new NativeClosure(this) {
123				public ATObject base_apply(ATTable args) throws InterpreterException {
124					ATStripe sstripe = get(args, 1).asStripe();
125					return sstripe.base_isSubstripeOf(superstripe);
126				}
127			});
128			return NATBoolean.atValue(found != NATNil._INSTANCE_);
129		}
130	}
131	
132	/**
133	 * Identity of stripes is based on their name
134	 */
135	public boolean equals(Object other) {
136		try {
137			return (other instanceof NATStripe) &&
138			       ((ATStripe) other).base_getStripeName().equals(stripeName_);
139		} catch (InterpreterException e) {
140			return false;
141		}
142	}
143	
144	public boolean isStripe() { return true; }
145	
146	public ATStripe asStripe() { return this; }
147	
148	public NATText meta_print() throws InterpreterException {
149		return NATText.atValue("<stripe:"+stripeName_+">");
150	}
151	
152    public ATTable meta_getStripes() throws InterpreterException {
153    	return NATTable.of(NativeStripes._STRIPE_);
154    }
155	
156	/**
157	 * The root stripe of the stripe hierarchy: every stripe eventually
158	 * has this stripe as its parent.
159	 */
160	public static class OBJRootStripe extends NATStripe implements ATStripe {
161		
162		private final static AGSymbol _ROOT_NAME_ = AGSymbol.jAlloc("Stripe");
163		
164		public static final OBJRootStripe _INSTANCE_ = new OBJRootStripe();
165		
166		/**
167		 * The root stripe is named `Stripe and has no parent stripes
168		 */
169		private OBJRootStripe() {
170			super(_ROOT_NAME_, NATTable.EMPTY);
171		}
172
173		/**
174		 * The root stripe is only a substripe of the root stripe itself
175		 */
176		public ATBoolean base_isSubstripeOf(ATStripe superstripe) throws InterpreterException {
177			return NATBoolean.atValue(superstripe.base_getStripeName().equals(_ROOT_NAME_));
178		}
179		
180	}
181	
182}