KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > python > core > PyObjectArray


1 //Copyright (c) Corporation for National Research Initiatives
2
package org.python.core;
3
4
5 /**
6  * Provides mutable behavior on a PyObject array. Supports operations for
7  * implementing <CODE>java.util.List</CODE>.
8  * @author Clark Updike
9  */

10 public class PyObjectArray extends AbstractArray {
11
12     public void remove(int start, int stop) {
13         super.remove(start, stop);
14     }
15
16     /**
17      * The underlying array used for storing the data.
18      */

19     protected PyObject[] baseArray;
20
21     /**
22      * Create the array with the specified size.
23      */

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     /**
34      * Create the array with the specified size.
35      * @param size number of int values initially allowed in array
36      */

37     public PyObjectArray(int size) {
38         super(PyObject.class, size);
39     }
40
41     /**
42      * @param toCopy
43      */

44     public PyObjectArray(PyObjectArray toCopy) {
45
46         super(toCopy);
47         this.baseArray = (PyObject[])toCopy.copyArray();
48     }
49
50     /**
51      * Add a value at a specified index in the array.
52      * <P><CODE>AbstractList</CODE> subclasses should update their
53      * <CODE>modCount</CODE> after calling this method.
54      *
55      * @param index index position at which to insert element
56      * @param value value to be inserted into array
57      */

58     public void add(int index, PyObject value) {
59         makeInsertSpace(index);
60         baseArray[index] = value;
61     }
62
63     /**
64      * Add a value to the array, appending it after the current values.
65      * <P><CODE>AbstractList</CODE> subclasses should update their
66      * <CODE>modCount</CODE> after calling this method.
67      *
68      * @param value value to be added
69      * @return index number of added element
70      */

71     public int add(PyObject value) {
72         int index = getAddIndex();
73         baseArray[index] = value;
74         return index;
75     }
76
77     /**
78      * Duplicates the object with the generic call.
79      *
80      * @return a copy of the object
81      */

82     public Object JavaDoc clone() {
83         return new PyObjectArray(this);
84     }
85
86     public boolean equals(Object JavaDoc 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     /**
118      * Discards values for a range of indices from the array. For the array of
119      * <code>int</code> values, just sets the values to null.
120      *
121      * @param from index of first value to be discarded
122      * @param to index past last value to be discarded
123      */

124     protected void discardValues(int from, int to) {
125         for (int i = from; i < to; i++) {
126             baseArray[i] = null;
127         }
128     }
129
130     /**
131      * Retrieve the value present at an index position in the array.
132      *
133      * @param index index position for value to be retrieved
134      * @return value from position in the array
135      */

136     public PyObject get(int index) {
137
138         if (index >= 0 && index < size) {
139             return baseArray[index];
140         }
141
142         String JavaDoc 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 JavaDoc(message);
147
148     }
149
150     /**
151      * Get the backing array. This method is used by the type-agnostic base
152      * class code to access the array used for type-specific storage. The array
153      * should generally not be modified. To get a copy of the array, see
154      * {@link #toArray()} which returns a copy. Note that
155      * <CODE>getSize()</CODE> should be used to determine the number of elements
156      * in the array, not the array's length (which may reflect excess capacity).
157      * <CODE>toArray()</CODE> returns an array whose length equals the value
158      * returned by <CODE>getSize()</CODE>.
159      *
160      * @return backing array object
161      */

162     public Object JavaDoc getArray() {
163         return baseArray;
164     }
165
166     /**
167      * Set the value at an index position in the array.
168      *
169      * @param index index position to be set
170      * @param value value to be set
171      */

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 JavaDoc(
179                 "Index must be between " + 0 + " and " +
180                 (size - 1) + ", but was " + index);
181     }
182
183     /**
184      * Set the backing array. This method is used by the type-agnostic base
185      * class code to set the array used for type-specific storage.
186      *
187      * @param array the backing array object
188      */

189     protected void setArray(Object JavaDoc array) {
190         baseArray = (PyObject[]) array;
191     }
192
193     /**
194      * Constructs and returns a simple array containing the same data as held
195      * in this growable array. The array's length matches the value returned
196      * by <CODE>getSize()</CODE>
197      *
198      * @return array containing a copy of the data
199      */

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