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.array;
|
|
11
|
|
12 import java.util.*;
|
|
13
|
|
14 import com.oracle.truffle.api.*;
|
|
15 import com.oracle.truffle.ruby.runtime.*;
|
|
16
|
|
17 /**
|
|
18 * A store for a pair of objects.
|
|
19 */
|
|
20 public final class ObjectImmutablePairArrayStore extends BaseArrayStore {
|
|
21
|
|
22 private final Object first;
|
|
23 private final Object second;
|
|
24
|
|
25 public ObjectImmutablePairArrayStore(Object first, Object second) {
|
|
26 size = 2;
|
|
27 capacity = 2;
|
|
28 this.first = first;
|
|
29 this.second = second;
|
|
30 }
|
|
31
|
|
32 @Override
|
|
33 public int size() {
|
|
34 return 2;
|
|
35 }
|
|
36
|
|
37 @Override
|
|
38 public Object get(int normalisedIndex) {
|
|
39 switch (normalisedIndex) {
|
|
40 case 0:
|
|
41 return first;
|
|
42 case 1:
|
|
43 return second;
|
|
44 default:
|
|
45 return NilPlaceholder.INSTANCE;
|
|
46 }
|
|
47 }
|
|
48
|
|
49 @Override
|
|
50 public ArrayStore getRange(int normalisedBegin, int truncatedNormalisedExclusiveEnd) {
|
|
51 if (normalisedBegin >= size) {
|
|
52 return null; // Represents Nil
|
|
53 }
|
|
54
|
|
55 return new ObjectArrayStore(Arrays.copyOfRange(new Object[]{first, second}, normalisedBegin, truncatedNormalisedExclusiveEnd));
|
|
56 }
|
|
57
|
|
58 @Override
|
|
59 public void set(int normalisedIndex, Object value) throws GeneraliseArrayStoreException {
|
|
60 CompilerDirectives.transferToInterpreter();
|
|
61 throw new GeneraliseArrayStoreException();
|
|
62 }
|
|
63
|
|
64 @Override
|
|
65 public void setRangeSingle(int normalisedBegin, int truncatedNormalisedExclusiveEnd, Object value) throws GeneraliseArrayStoreException {
|
|
66 CompilerDirectives.transferToInterpreter();
|
|
67 throw new GeneraliseArrayStoreException();
|
|
68 }
|
|
69
|
|
70 @Override
|
|
71 public void setRangeArray(int normalisedBegin, int normalisedExclusiveEnd, ArrayStore other) throws GeneraliseArrayStoreException {
|
|
72 CompilerDirectives.transferToInterpreter();
|
|
73 throw new GeneraliseArrayStoreException();
|
|
74 }
|
|
75
|
|
76 @Override
|
|
77 public void insert(int normalisedIndex, Object value) throws GeneraliseArrayStoreException {
|
|
78 CompilerDirectives.transferToInterpreter();
|
|
79 throw new GeneraliseArrayStoreException();
|
|
80 }
|
|
81
|
|
82 @Override
|
|
83 public void push(Object value) throws GeneraliseArrayStoreException {
|
|
84 CompilerDirectives.transferToInterpreter();
|
|
85 throw new GeneraliseArrayStoreException();
|
|
86 }
|
|
87
|
|
88 @Override
|
|
89 public Object deleteAt(int normalisedIndex) {
|
|
90 throw new UnsupportedOperationException();
|
|
91 }
|
|
92
|
|
93 @Override
|
|
94 public ArrayStore dup() {
|
|
95 return this;
|
|
96 }
|
|
97
|
|
98 @Override
|
|
99 public boolean contains(Object value) {
|
|
100 return first.equals(value) || second.equals(value);
|
|
101 }
|
|
102
|
|
103 @Override
|
|
104 public ArrayStore generalizeFor(Object type) {
|
|
105 return new ObjectArrayStore(toObjectArray());
|
|
106 }
|
|
107
|
|
108 @Override
|
|
109 public Object getIndicativeValue() {
|
|
110 return 0;
|
|
111 }
|
|
112
|
|
113 @Override
|
|
114 protected void setCapacityByCopying(int newCapacity) {
|
|
115 throw new UnsupportedOperationException();
|
|
116 }
|
|
117
|
|
118 @Override
|
|
119 protected void setCapacityWithNewArray(int newCapacity) {
|
|
120 throw new UnsupportedOperationException();
|
|
121 }
|
|
122
|
|
123 @Override
|
|
124 protected Object getValuesArrayObject() {
|
|
125 return new Object[]{first, second};
|
|
126 }
|
|
127
|
|
128 @Override
|
|
129 public Object[] toObjectArray() {
|
|
130 return new Object[]{first, second};
|
|
131 }
|
|
132
|
|
133 @Override
|
|
134 public boolean equals(ArrayStore other) {
|
|
135 if (other instanceof ObjectImmutablePairArrayStore) {
|
|
136 return equals((ObjectImmutablePairArrayStore) other);
|
|
137 } else {
|
|
138 return super.equals(other);
|
|
139 }
|
|
140 }
|
|
141
|
|
142 public boolean equals(ObjectImmutablePairArrayStore other) {
|
|
143 if (other == null) {
|
|
144 return false;
|
|
145 } else if (other == this) {
|
|
146 return true;
|
|
147 } else {
|
|
148 return other.first == first && other.second == second;
|
|
149 }
|
|
150 }
|
|
151 }
|