001: //Copyright (c) Corporation for National Research Initiatives
002: package org.python.core;
003:
004: /**
005: * Provides mutable behavior on a PyObject array. Supports operations for
006: * implementing <CODE>java.util.List</CODE>.
007: * @author Clark Updike
008: */
009: public class PyObjectArray extends AbstractArray {
010:
011: public void remove(int start, int stop) {
012: super .remove(start, stop);
013: }
014:
015: /**
016: * The underlying array used for storing the data.
017: */
018: protected PyObject[] baseArray;
019:
020: /**
021: * Create the array with the specified size.
022: */
023: public PyObjectArray() {
024: super (PyObject.class);
025: }
026:
027: public PyObjectArray(PyObject[] rawArray) {
028: super (rawArray == null ? 0 : rawArray.length);
029: baseArray = (rawArray == null) ? new PyObject[] {} : rawArray;
030: }
031:
032: /**
033: * Create the array with the specified size.
034: * @param size number of int values initially allowed in array
035: */
036: public PyObjectArray(int size) {
037: super (PyObject.class, size);
038: }
039:
040: /**
041: * @param toCopy
042: */
043: public PyObjectArray(PyObjectArray toCopy) {
044:
045: super (toCopy);
046: this .baseArray = (PyObject[]) toCopy.copyArray();
047: }
048:
049: /**
050: * Add a value at a specified index in the array.
051: * <P><CODE>AbstractList</CODE> subclasses should update their
052: * <CODE>modCount</CODE> after calling this method.
053: *
054: * @param index index position at which to insert element
055: * @param value value to be inserted into array
056: */
057: public void add(int index, PyObject value) {
058: makeInsertSpace(index);
059: baseArray[index] = value;
060: }
061:
062: /**
063: * Add a value to the array, appending it after the current values.
064: * <P><CODE>AbstractList</CODE> subclasses should update their
065: * <CODE>modCount</CODE> after calling this method.
066: *
067: * @param value value to be added
068: * @return index number of added element
069: */
070: public int add(PyObject value) {
071: int index = getAddIndex();
072: baseArray[index] = value;
073: return index;
074: }
075:
076: /**
077: * Duplicates the object with the generic call.
078: *
079: * @return a copy of the object
080: */
081: public Object clone() {
082: return new PyObjectArray(this );
083: }
084:
085: public boolean equals(Object o) {
086: if (o instanceof PyObjectArray) {
087: PyObjectArray arr = (PyObjectArray) o;
088: if (size != arr.size)
089: return false;
090: for (int i = 0; i < size; i++) {
091: PyObject this Elem = baseArray[i];
092: PyObject otherElem = arr.baseArray[i];
093: if (this Elem == null) {
094: if (otherElem == null)
095: continue;
096: return false;
097: }
098: if (!this Elem.equals(otherElem))
099: return false;
100: }
101: return true;
102: }
103: return false;
104: }
105:
106: public int hashCode() {
107: int x, y;
108: int len = size;
109: x = 0x345678;
110:
111: for (len--; len >= 0; len--) {
112: y = baseArray[len].hashCode();
113: x = (x + x + x) ^ y;
114: }
115: x ^= size;
116: return x;
117: }
118:
119: /**
120: * Discards values for a range of indices from the array. For the array of
121: * <code>int</code> values, just sets the values to null.
122: *
123: * @param from index of first value to be discarded
124: * @param to index past last value to be discarded
125: */
126: protected void discardValues(int from, int to) {
127: for (int i = from; i < to; i++) {
128: baseArray[i] = null;
129: }
130: }
131:
132: /**
133: * Retrieve the value present at an index position in the array.
134: *
135: * @param index index position for value to be retrieved
136: * @return value from position in the array
137: */
138: public PyObject get(int index) {
139:
140: if (index >= 0 && index < size) {
141: return baseArray[index];
142: }
143:
144: String message = (size == 0) ? "No data was added, unable to get entry at "
145: + index
146: : "Index must be between " + 0 + " and " + (size - 1)
147: + ", but was " + index;
148: throw new ArrayIndexOutOfBoundsException(message);
149:
150: }
151:
152: /**
153: * Get the backing array. This method is used by the type-agnostic base
154: * class code to access the array used for type-specific storage. The array
155: * should generally not be modified. To get a copy of the array, see
156: * {@link #toArray()} which returns a copy. Note that
157: * <CODE>getSize()</CODE> should be used to determine the number of elements
158: * in the array, not the array's length (which may reflect excess capacity).
159: * <CODE>toArray()</CODE> returns an array whose length equals the value
160: * returned by <CODE>getSize()</CODE>.
161: *
162: * @return backing array object
163: */
164: public Object getArray() {
165: return baseArray;
166: }
167:
168: /**
169: * Set the value at an index position in the array.
170: *
171: * @param index index position to be set
172: * @param value value to be set
173: */
174: public PyObject set(int index, PyObject value) {
175: if (index >= 0 && index < size) {
176: PyObject existing = baseArray[index];
177: baseArray[index] = value;
178: return existing;
179: }
180: throw new ArrayIndexOutOfBoundsException(
181: "Index must be between " + 0 + " and " + (size - 1)
182: + ", but was " + index);
183: }
184:
185: /**
186: * Set the backing array. This method is used by the type-agnostic base
187: * class code to set the array used for type-specific storage.
188: *
189: * @param array the backing array object
190: */
191: protected void setArray(Object array) {
192: baseArray = (PyObject[]) array;
193: }
194:
195: /**
196: * Constructs and returns a simple array containing the same data as held
197: * in this growable array. The array's length matches the value returned
198: * by <CODE>getSize()</CODE>
199: *
200: * @return array containing a copy of the data
201: */
202: public PyObject[] toArray() {
203: return (PyObject[]) copyArray();
204: }
205:
206: public void ensureCapacity(int minCapacity) {
207: super.ensureCapacity(minCapacity);
208: }
209: }
|