1 package org.python.core; 3 4 import java.io.Serializable ; 5 import java.util.AbstractList ; 6 import java.util.Collection ; 7 import java.util.Iterator ; 8 import java.util.RandomAccess ; 9 10 22 public class PyObjectList 23 extends AbstractList implements RandomAccess , Cloneable , Serializable { 24 25 35 36 37 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 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 64 public void add(int index, Object 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 77 public boolean add(Object 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 clone(){ 90 try { 91 PyObjectList tol = (PyObjectList) super.clone(); 92 tol.array = (PyObjectArray) array.clone(); 93 modCount = 0; 94 return tol; 95 } catch (CloneNotSupportedException eCNSE) { 96 throw new InternalError ("Unexpected CloneNotSupportedException.\n" 97 + eCNSE.getMessage()); 98 } 99 } 100 101 public boolean equals(Object 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 115 public Object get(int index) { 116 PyObject obj = array.get(index); 117 return obj.__tojava__(Object .class); 118 } 119 120 PyObject pyget(int index) { 121 return array.get(index); 122 } 123 124 public Object remove(int index) { 125 modCount++; 126 Object 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 139 public Object set(int index, Object element) { 140 return array.set(index, Py.java2py(element) ).__tojava__(Object .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 c) { 152 return addAll(size(), c); 153 } 154 155 public boolean addAll(int index, Collection 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 for (Iterator i = c.iterator(); i.hasNext(); ) { 166 add(i.next()); 167 } 168 } 169 return c.size() > 0; 170 } 171 172 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 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 |