KickJava   Java API By Example, From Geeks To Geeks.

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


1 //Copyright (c) Corporation for National Research Initiatives
2
package org.python.core;
3
4 import java.io.Serializable JavaDoc;
5 import java.util.AbstractList JavaDoc;
6 import java.util.Collection JavaDoc;
7 import java.util.Iterator JavaDoc;
8 import java.util.RandomAccess JavaDoc;
9
10 /**
11  * <CODE>java.util.List</CODE> implementation using an underlying PyObject
12  * array for higher performance. Jython should use the following methods
13  * where possible, instead of their <CODE>List</CODE> counterparts:
14  * <UL>
15  * <LI>pyadd(int, PyObject)</LI>
16  * <LI>pyadd(PyObject)</LI>
17  * <LI>pyset(PyObject)</LI>
18  * <LI>pyget()</LI>
19  * </UL>
20  * @author Clark Updike
21  */

22 public class PyObjectList
23         extends AbstractList JavaDoc implements RandomAccess JavaDoc, Cloneable JavaDoc, Serializable JavaDoc {
24
25     /* Design note:
26      * This class let's PySequenceList implement java.util.List by delegating
27      * to an instance of this. The major distinction is that the backing array
28      * is PyObject[], not Object[] (as you'd get by delegating to ArrayList).
29      * There are 2 major benefits: 1) A lot of casting can be avoided
30      * internally (although use of PySequenceList descendants as java
31      * collections does involve some casting); 2) PySequenceList descendants
32      * can still do bulk array operations, allowing better performance and
33      * reuse of much of the pre-collections bulk operation implementation.
34      */

35
36
37     /**
38      * Provides mutable operations on a PyObject[] array, including features
39      * that help with implementing java.util.List.
40      */

41     protected PyObjectArray array;
42
43     public PyObjectList() {
44         array = new PyObjectArray();
45     }
46
47     public PyObjectList(PyObject[] pyObjArr) {
48         array = new PyObjectArray(pyObjArr);
49         array.baseArray = pyObjArr;
50     }
51
52     public PyObjectList(Collection JavaDoc c) {
53         array = new PyObjectArray();
54         array.appendArray(c.toArray());
55     }
56
57     public PyObjectList(int size) {
58         array = new PyObjectArray(size);
59     }
60
61     /**
62      * For internal jython usage, use {@link #pyadd(int, PyObject)}.
63      */

64     public void add(int index, Object JavaDoc element) {
65         array.add(index, Py.java2py(element));
66         modCount += array.getModCountIncr();
67     }
68
69     public void pyadd(int index, PyObject element) {
70         array.add(index, element);
71         modCount += array.getModCountIncr();
72     }
73
74     /**
75      * For internal jython usage, use {@link #pyadd(PyObject)}.
76      */

77     public boolean add(Object JavaDoc o) {
78         array.add(Py.java2py(o));
79         modCount += array.getModCountIncr();
80         return true;
81     }
82
83     public boolean pyadd(PyObject o) {
84         array.add(o);
85         modCount += array.getModCountIncr();
86         return true;
87     }
88
89     public Object JavaDoc clone(){
90         try {
91             PyObjectList tol = (PyObjectList) super.clone();
92             tol.array = (PyObjectArray) array.clone();
93             modCount = 0;
94             return tol;
95         } catch (CloneNotSupportedException JavaDoc eCNSE) {
96             throw new InternalError JavaDoc("Unexpected CloneNotSupportedException.\n"
97               + eCNSE.getMessage());
98         }
99     }
100
101     public boolean equals(Object JavaDoc o) {
102         if(o instanceof PyObjectList) {
103             return array.equals(((PyObjectList)o).array);
104         }
105         return false;
106     }
107
108     public int hashCode() {
109         return array.hashCode();
110     }
111
112     /**
113      * Use <code>pyget(int)</code> for internal jython usage.
114      */

115     public Object JavaDoc get(int index) {
116         PyObject obj = array.get(index);
117         return obj.__tojava__(Object JavaDoc.class);
118     }
119
120     PyObject pyget(int index) {
121         return array.get(index);
122     }
123
124     public Object JavaDoc remove(int index) {
125         modCount++;
126         Object JavaDoc existing = array.get(index);
127         array.remove(index);
128         return existing;
129     }
130
131     public void remove(int start, int stop) {
132         modCount++;
133         array.remove(start, stop);
134     }
135
136     /**
137      * Use <code>pyset(int, PyObject)</code> for internal jython usage.
138      */

139     public Object JavaDoc set(int index, Object JavaDoc element) {
140         return array.set(index, Py.java2py(element) ).__tojava__(Object JavaDoc.class);
141     }
142
143     PyObject pyset(int index, PyObject element) {
144         return array.set(index, element);
145     }
146
147     public int size() {
148         return array.getSize();
149     }
150
151     public boolean addAll(Collection JavaDoc c) {
152         return addAll(size(), c);
153     }
154
155     public boolean addAll(int index, Collection JavaDoc c) {
156         if (c instanceof PySequenceList) {
157             PySequenceList cList = (PySequenceList)c;
158             PyObject[] cArray = cList.getArray();
159             int cOrigSize = cList.size();
160             array.makeInsertSpace(index, cOrigSize);
161             array.replaceSubArray(index, index + cOrigSize, cArray, 0, cOrigSize);
162         } else {
163             // need to use add to convert anything pulled from a collection
164
// into a PyObject
165
for (Iterator JavaDoc i = c.iterator(); i.hasNext(); ) {
166                 add(i.next());
167             }
168         }
169         return c.size() > 0;
170     }
171
172         /**
173          * Get the backing array. The array should generally not be modified.
174          * To get a copy of the array, see {@link #toArray()} which returns a copy.
175          *
176          * @return backing array object
177          */

178     PyObject[] getArray() {
179         return (PyObject[])array.getArray();
180     }
181
182         void ensureCapacity(int minCapacity) {
183             array.ensureCapacity(minCapacity);
184         }
185
186     void replaceSubArray(int destStart, int destStop, Object JavaDoc srcArray, int srcStart, int srcStop) {
187         array.replaceSubArray(destStart, destStop, srcArray, srcStart, srcStop);
188     }
189
190     void setSize(int count) {
191         array.setSize(count);
192     }
193 }
194
Popular Tags