PageRenderTime 36ms CodeModel.GetById 30ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/interpreter/tags/at2dist220411/src/edu/vub/at/objects/natives/MethodDictionary.java

http://ambienttalk.googlecode.com/
Java | 134 lines | 61 code | 24 blank | 49 comment | 3 complexity | 3df501100b243384c83ac79dc0269d8e MD5 | raw file
  1/**
  2 * AmbientTalk/2 Project
  3 * MethodDictionary.java created on Oct 10, 2006 at 1:10:24 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 java.io.Serializable;
 31import java.util.Collection;
 32import java.util.HashMap;
 33import java.util.Map;
 34import java.util.Set;
 35
 36/**
 37 * Instances of this class implement a dictionary mapping selectors to AmbientTalk methods.
 38 * These method dictionaries are a traditional candidate for being shared among different
 39 * clones of an object, improving the space efficiency of the language. 
 40 * 
 41 * The advantage of maps lies in the fact that they can be used implicitly to keep track
 42 * of which objects were cloned from one another. As said above clones share the dictionaries.
 43 * To ensure that this relation is upheld when one clone makes an idiosyncratic change, 
 44 * we store from which method dictionary the altered copy was created.
 45 *
 46 * @author smostinc
 47 */
 48public class MethodDictionary implements Map, Cloneable, Serializable {
 49
 50	private static final int _DEFAULT_SIZE_ = 8;
 51	
 52	private MethodDictionary parent_ = null;
 53	
 54	private HashMap methods_;
 55	
 56	public MethodDictionary() {
 57		methods_ = new HashMap(_DEFAULT_SIZE_);
 58	}
 59	
 60	// used internally to clone the method dictionary
 61	private MethodDictionary(HashMap methods, MethodDictionary parent) {
 62		this.methods_ = methods;
 63		this.parent_ = parent;
 64	}
 65
 66	public void clear() {
 67		methods_.clear();
 68	}
 69
 70	public boolean containsKey(Object key) {
 71		return methods_.containsKey(key);
 72	}
 73
 74	public boolean containsValue(Object value) {
 75		return methods_.containsKey(value);
 76	}
 77
 78	public Set entrySet() {
 79		return methods_.entrySet();
 80	}
 81
 82	public Object get(Object key) {
 83		return methods_.get(key);
 84	}
 85
 86	public boolean isEmpty() {
 87		return methods_.isEmpty();
 88	}
 89
 90	public Set keySet() {
 91		return methods_.keySet();
 92	}
 93
 94	public Object put(Object key, Object value) {
 95		return methods_.put(key, value);
 96	}
 97
 98	public void putAll(Map keyValuePairs) {
 99		methods_.putAll(keyValuePairs);
100	}
101
102	public Object remove(Object key) {
103		return methods_.remove(key);
104	}
105
106	public int size() {
107		return methods_.size();
108
109	}
110
111	public Collection values() {
112		return methods_.values();
113	}
114	
115	/* 
116	 * Creates a shallow copy of the method dictionary to allow for addition of methods to an object 
117	 * which has already been cloned. Keeps track of the original MethodDictionary to be able to derive
118	 * whether method dictionaries have a common origin.
119	 */
120	protected Object clone() {
121		return new MethodDictionary((HashMap)methods_.clone(), this);
122	}
123	
124	/**
125	 * Checks whether both MethodDictionaries are equal or whether the passed object is a
126	 * MethodDictionary from which this one (indirectly) originates. 
127	 */
128	public boolean isDerivedFrom(MethodDictionary aMethodDictionary) {
129		return (this == aMethodDictionary) ||
130			((parent_ != null) && parent_.isDerivedFrom(aMethodDictionary));
131	}
132
133
134}