1 package org.python.core; 3 4 5 10 public class PyObjectArray extends AbstractArray { 11 12 public void remove(int start, int stop) { 13 super.remove(start, stop); 14 } 15 16 19 protected PyObject[] baseArray; 20 21 24 public PyObjectArray() { 25 super(PyObject.class); 26 } 27 28 public PyObjectArray(PyObject[] rawArray) { 29 super(rawArray == null ? 0 : rawArray.length); 30 baseArray = (rawArray == null) ? new PyObject[] {} : rawArray; 31 } 32 33 37 public PyObjectArray(int size) { 38 super(PyObject.class, size); 39 } 40 41 44 public PyObjectArray(PyObjectArray toCopy) { 45 46 super(toCopy); 47 this.baseArray = (PyObject[])toCopy.copyArray(); 48 } 49 50 58 public void add(int index, PyObject value) { 59 makeInsertSpace(index); 60 baseArray[index] = value; 61 } 62 63 71 public int add(PyObject value) { 72 int index = getAddIndex(); 73 baseArray[index] = value; 74 return index; 75 } 76 77 82 public Object clone() { 83 return new PyObjectArray(this); 84 } 85 86 public boolean equals(Object o) { 87 if(o instanceof PyObjectArray) { 88 PyObjectArray arr = (PyObjectArray)o; 89 if (size != arr.size) return false; 90 for (int i = 0; i < size; i++) { 91 PyObject thisElem = baseArray[i]; 92 PyObject otherElem = arr.baseArray[i]; 93 if (thisElem == null) { 94 if (otherElem == null) continue; 95 return false; 96 } 97 if (!thisElem.equals(otherElem)) return false; 98 } 99 return true; 100 } 101 return false; 102 } 103 104 public int hashCode() { 105 int x, y; 106 int len = size; 107 x = 0x345678; 108 109 for (len--; len>=0; len--) { 110 y = baseArray[len].hashCode(); 111 x = (x + x + x) ^ y; 112 } 113 x ^= size; 114 return x; 115 } 116 117 124 protected void discardValues(int from, int to) { 125 for (int i = from; i < to; i++) { 126 baseArray[i] = null; 127 } 128 } 129 130 136 public PyObject get(int index) { 137 138 if (index >= 0 && index < size) { 139 return baseArray[index]; 140 } 141 142 String message = (size == 0) 143 ? "No data was added, unable to get entry at " + index 144 : "Index must be between " + 0 + " and " + 145 (size - 1) + ", but was " + index; 146 throw new ArrayIndexOutOfBoundsException (message); 147 148 } 149 150 162 public Object getArray() { 163 return baseArray; 164 } 165 166 172 public PyObject set(int index, PyObject value) { 173 if (index >= 0 && index < size) { 174 PyObject existing = baseArray[index]; 175 baseArray[index] = value; 176 return existing; 177 } 178 throw new ArrayIndexOutOfBoundsException ( 179 "Index must be between " + 0 + " and " + 180 (size - 1) + ", but was " + index); 181 } 182 183 189 protected void setArray(Object array) { 190 baseArray = (PyObject[]) array; 191 } 192 193 200 public PyObject[] toArray() { 201 return (PyObject[]) copyArray(); 202 } 203 204 public void ensureCapacity(int minCapacity) { 205 super.ensureCapacity(minCapacity); 206 } 207 } 208 209 | Popular Tags |