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