13514
|
1 /*
|
|
2 * Copyright (c) 2013 Oracle and/or its affiliates. All rights reserved. This
|
|
3 * code is released under a tri EPL/GPL/LGPL license. You can use it,
|
|
4 * redistribute it and/or modify it under the terms of the:
|
|
5 *
|
|
6 * Eclipse Public License version 1.0
|
|
7 * GNU General Public License version 2
|
|
8 * GNU Lesser General Public License version 2.1
|
|
9 */
|
|
10 package com.oracle.truffle.ruby.runtime.core;
|
|
11
|
|
12 import java.util.*;
|
|
13
|
|
14 import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
|
|
15 import com.oracle.truffle.ruby.runtime.objects.*;
|
|
16
|
|
17 /**
|
|
18 * Represents the Ruby {@code Hash} class.
|
|
19 */
|
|
20 public class RubyHash extends RubyObject {
|
|
21
|
|
22 /**
|
|
23 * The class from which we create the object that is {@code Hash}. A subclass of
|
|
24 * {@link RubyClass} so that we can override {@link #newInstance} and allocate a
|
|
25 * {@link RubyHash} rather than a normal {@link RubyBasicObject}.
|
|
26 */
|
|
27 public static class RubyHashClass extends RubyClass {
|
|
28
|
|
29 public RubyHashClass(RubyClass objectClass) {
|
|
30 super(null, objectClass, "Hash");
|
|
31 }
|
|
32
|
|
33 @Override
|
|
34 public RubyBasicObject newInstance() {
|
|
35 return new RubyHash(this);
|
|
36 }
|
|
37
|
|
38 }
|
|
39
|
|
40 public final Map<Object, Object> storage = new LinkedHashMap<>();
|
|
41 @CompilationFinal public RubyProc defaultBlock = null;
|
|
42
|
|
43 public RubyHash(RubyClass rubyClass, RubyProc defaultBlock) {
|
|
44 super(rubyClass);
|
|
45 initialize(defaultBlock);
|
|
46 }
|
|
47
|
|
48 public RubyHash(RubyClass rubyClass) {
|
|
49 super(rubyClass);
|
|
50 }
|
|
51
|
|
52 public void initialize(RubyProc setDefaultBlock) {
|
|
53 defaultBlock = setDefaultBlock;
|
|
54 }
|
|
55
|
|
56 @Override
|
|
57 public Object dup() {
|
|
58 final RubyHash newHash = new RubyHash(rubyClass);
|
|
59 newHash.setInstanceVariables(getInstanceVariables());
|
|
60 newHash.storage.putAll(storage);
|
|
61 return newHash;
|
|
62 }
|
|
63
|
|
64 public void put(Object key, Object value) {
|
|
65 checkFrozen();
|
|
66
|
|
67 storage.put(key, value);
|
|
68 }
|
|
69
|
|
70 public Object get(Object key) {
|
|
71 return storage.get(key);
|
|
72 }
|
|
73
|
|
74 public Map<Object, Object> getMap() {
|
|
75 return storage;
|
|
76 }
|
|
77
|
|
78 @Override
|
|
79 public String toString() {
|
|
80 final StringBuilder builder = new StringBuilder();
|
|
81 builder.append("{");
|
|
82
|
|
83 for (Map.Entry<Object, Object> entry : storage.entrySet()) {
|
|
84 if (builder.length() > 1) {
|
|
85 builder.append(", ");
|
|
86 }
|
|
87
|
|
88 builder.append(entry.getKey().toString());
|
|
89 builder.append("=>");
|
|
90 builder.append(entry.getValue().toString());
|
|
91 }
|
|
92
|
|
93 builder.append("}");
|
|
94 return builder.toString();
|
|
95 }
|
|
96
|
|
97 @Override
|
|
98 public int hashCode() {
|
|
99 final int prime = 31;
|
|
100 int result = 1;
|
|
101 result = prime * result + ((storage == null) ? 0 : storage.hashCode());
|
|
102 return result;
|
|
103 }
|
|
104
|
|
105 @Override
|
|
106 public boolean equals(Object obj) {
|
|
107 if (this == obj) {
|
|
108 return true;
|
|
109 }
|
|
110 if (obj == null) {
|
|
111 return false;
|
|
112 }
|
|
113 if (!(obj instanceof RubyHash)) {
|
|
114 return false;
|
|
115 }
|
|
116 RubyHash other = (RubyHash) obj;
|
|
117 if (storage == null) {
|
|
118 if (other.storage != null) {
|
|
119 return false;
|
|
120 }
|
|
121 } else if (!storage.equals(other.storage)) {
|
|
122 return false;
|
|
123 }
|
|
124 return true;
|
|
125 }
|
|
126
|
|
127 }
|