001: // Copyright (c) Corporation for National Research Initiatives
002: package org.python.core;
003:
004: import java.util.Collection;
005: import java.util.Collections;
006: import java.util.Iterator;
007: import java.util.List;
008: import java.util.ArrayList;
009: import java.util.ListIterator;
010:
011: /**
012: * A builtin python tuple.
013: */
014:
015: public class PyTuple extends PySequenceList implements ClassDictInit {
016:
017: /** <i>Internal use only. Do not call this method explicit.</i> */
018: public static void classDictInit(PyObject dict)
019: throws PyIgnoreMethodTag {
020: }
021:
022: //~ BEGIN GENERATED REGION -- DO NOT EDIT SEE gexpose.py
023: /* type info */
024:
025: public static final String exposed_name = "tuple";
026:
027: public static final Class exposed_base = PyObject.class;
028:
029: public static void typeSetup(PyObject dict, PyType.Newstyle marker) {
030: class exposed___ne__ extends PyBuiltinMethodNarrow {
031:
032: exposed___ne__(PyObject self, PyBuiltinFunction.Info info) {
033: super (self, info);
034: }
035:
036: public PyBuiltinFunction bind(PyObject self) {
037: return new exposed___ne__(self, info);
038: }
039:
040: public PyObject __call__(PyObject arg0) {
041: PyObject ret = ((PyTuple) self).tuple___ne__(arg0);
042: if (ret == null)
043: return Py.NotImplemented;
044: return ret;
045: }
046:
047: }
048: dict.__setitem__("__ne__", new PyMethodDescr("__ne__",
049: PyTuple.class, 1, 1, new exposed___ne__(null, null)));
050: class exposed___eq__ extends PyBuiltinMethodNarrow {
051:
052: exposed___eq__(PyObject self, PyBuiltinFunction.Info info) {
053: super (self, info);
054: }
055:
056: public PyBuiltinFunction bind(PyObject self) {
057: return new exposed___eq__(self, info);
058: }
059:
060: public PyObject __call__(PyObject arg0) {
061: PyObject ret = ((PyTuple) self).tuple___eq__(arg0);
062: if (ret == null)
063: return Py.NotImplemented;
064: return ret;
065: }
066:
067: }
068: dict.__setitem__("__eq__", new PyMethodDescr("__eq__",
069: PyTuple.class, 1, 1, new exposed___eq__(null, null)));
070: class exposed___lt__ extends PyBuiltinMethodNarrow {
071:
072: exposed___lt__(PyObject self, PyBuiltinFunction.Info info) {
073: super (self, info);
074: }
075:
076: public PyBuiltinFunction bind(PyObject self) {
077: return new exposed___lt__(self, info);
078: }
079:
080: public PyObject __call__(PyObject arg0) {
081: PyObject ret = ((PyTuple) self).tuple___lt__(arg0);
082: if (ret == null)
083: return Py.NotImplemented;
084: return ret;
085: }
086:
087: }
088: dict.__setitem__("__lt__", new PyMethodDescr("__lt__",
089: PyTuple.class, 1, 1, new exposed___lt__(null, null)));
090: class exposed___le__ extends PyBuiltinMethodNarrow {
091:
092: exposed___le__(PyObject self, PyBuiltinFunction.Info info) {
093: super (self, info);
094: }
095:
096: public PyBuiltinFunction bind(PyObject self) {
097: return new exposed___le__(self, info);
098: }
099:
100: public PyObject __call__(PyObject arg0) {
101: PyObject ret = ((PyTuple) self).tuple___le__(arg0);
102: if (ret == null)
103: return Py.NotImplemented;
104: return ret;
105: }
106:
107: }
108: dict.__setitem__("__le__", new PyMethodDescr("__le__",
109: PyTuple.class, 1, 1, new exposed___le__(null, null)));
110: class exposed___gt__ extends PyBuiltinMethodNarrow {
111:
112: exposed___gt__(PyObject self, PyBuiltinFunction.Info info) {
113: super (self, info);
114: }
115:
116: public PyBuiltinFunction bind(PyObject self) {
117: return new exposed___gt__(self, info);
118: }
119:
120: public PyObject __call__(PyObject arg0) {
121: PyObject ret = ((PyTuple) self).tuple___gt__(arg0);
122: if (ret == null)
123: return Py.NotImplemented;
124: return ret;
125: }
126:
127: }
128: dict.__setitem__("__gt__", new PyMethodDescr("__gt__",
129: PyTuple.class, 1, 1, new exposed___gt__(null, null)));
130: class exposed___ge__ extends PyBuiltinMethodNarrow {
131:
132: exposed___ge__(PyObject self, PyBuiltinFunction.Info info) {
133: super (self, info);
134: }
135:
136: public PyBuiltinFunction bind(PyObject self) {
137: return new exposed___ge__(self, info);
138: }
139:
140: public PyObject __call__(PyObject arg0) {
141: PyObject ret = ((PyTuple) self).tuple___ge__(arg0);
142: if (ret == null)
143: return Py.NotImplemented;
144: return ret;
145: }
146:
147: }
148: dict.__setitem__("__ge__", new PyMethodDescr("__ge__",
149: PyTuple.class, 1, 1, new exposed___ge__(null, null)));
150: class exposed___getitem__ extends PyBuiltinMethodNarrow {
151:
152: exposed___getitem__(PyObject self,
153: PyBuiltinFunction.Info info) {
154: super (self, info);
155: }
156:
157: public PyBuiltinFunction bind(PyObject self) {
158: return new exposed___getitem__(self, info);
159: }
160:
161: public PyObject __call__(PyObject arg0) {
162: PyObject ret = ((PyTuple) self).seq___finditem__(arg0);
163: if (ret == null) {
164: throw Py.IndexError("index out of range: " + arg0);
165: }
166: return ret;
167: }
168:
169: }
170: dict.__setitem__("__getitem__", new PyMethodDescr(
171: "__getitem__", PyTuple.class, 1, 1,
172: new exposed___getitem__(null, null)));
173: class exposed___getslice__ extends PyBuiltinMethodNarrow {
174:
175: exposed___getslice__(PyObject self,
176: PyBuiltinFunction.Info info) {
177: super (self, info);
178: }
179:
180: public PyBuiltinFunction bind(PyObject self) {
181: return new exposed___getslice__(self, info);
182: }
183:
184: public PyObject __call__(PyObject arg0, PyObject arg1,
185: PyObject arg2) {
186: return ((PyTuple) self).seq___getslice__(arg0, arg1,
187: arg2);
188: }
189:
190: public PyObject __call__(PyObject arg0, PyObject arg1) {
191: return ((PyTuple) self).seq___getslice__(arg0, arg1);
192: }
193:
194: }
195: dict.__setitem__("__getslice__", new PyMethodDescr(
196: "__getslice__", PyTuple.class, 2, 3,
197: new exposed___getslice__(null, null)));
198: class exposed___contains__ extends PyBuiltinMethodNarrow {
199:
200: exposed___contains__(PyObject self,
201: PyBuiltinFunction.Info info) {
202: super (self, info);
203: }
204:
205: public PyBuiltinFunction bind(PyObject self) {
206: return new exposed___contains__(self, info);
207: }
208:
209: public PyObject __call__(PyObject arg0) {
210: return Py.newBoolean(((PyTuple) self)
211: .tuple___contains__(arg0));
212: }
213:
214: }
215: dict.__setitem__("__contains__", new PyMethodDescr(
216: "__contains__", PyTuple.class, 1, 1,
217: new exposed___contains__(null, null)));
218: class exposed___len__ extends PyBuiltinMethodNarrow {
219:
220: exposed___len__(PyObject self, PyBuiltinFunction.Info info) {
221: super (self, info);
222: }
223:
224: public PyBuiltinFunction bind(PyObject self) {
225: return new exposed___len__(self, info);
226: }
227:
228: public PyObject __call__() {
229: return Py.newInteger(((PyTuple) self).tuple___len__());
230: }
231:
232: }
233: dict.__setitem__("__len__", new PyMethodDescr("__len__",
234: PyTuple.class, 0, 0, new exposed___len__(null, null)));
235: class exposed___add__ extends PyBuiltinMethodNarrow {
236:
237: exposed___add__(PyObject self, PyBuiltinFunction.Info info) {
238: super (self, info);
239: }
240:
241: public PyBuiltinFunction bind(PyObject self) {
242: return new exposed___add__(self, info);
243: }
244:
245: public PyObject __call__(PyObject arg0) {
246: return ((PyTuple) self).tuple___add__(arg0);
247: }
248:
249: }
250: dict.__setitem__("__add__", new PyMethodDescr("__add__",
251: PyTuple.class, 1, 1, new exposed___add__(null, null)));
252: class exposed___reduce__ extends PyBuiltinMethodNarrow {
253:
254: exposed___reduce__(PyObject self,
255: PyBuiltinFunction.Info info) {
256: super (self, info);
257: }
258:
259: public PyBuiltinFunction bind(PyObject self) {
260: return new exposed___reduce__(self, info);
261: }
262:
263: public PyObject __call__() {
264: return ((PyTuple) self).tuple___reduce__();
265: }
266:
267: }
268: dict.__setitem__("__reduce__",
269: new PyMethodDescr("__reduce__", PyTuple.class, 0, 0,
270: new exposed___reduce__(null, null)));
271: class exposed___mul__ extends PyBuiltinMethodNarrow {
272:
273: exposed___mul__(PyObject self, PyBuiltinFunction.Info info) {
274: super (self, info);
275: }
276:
277: public PyBuiltinFunction bind(PyObject self) {
278: return new exposed___mul__(self, info);
279: }
280:
281: public PyObject __call__(PyObject arg0) {
282: return ((PyTuple) self).tuple___mul__(arg0);
283: }
284:
285: }
286: dict.__setitem__("__mul__", new PyMethodDescr("__mul__",
287: PyTuple.class, 1, 1, new exposed___mul__(null, null)));
288: class exposed___rmul__ extends PyBuiltinMethodNarrow {
289:
290: exposed___rmul__(PyObject self, PyBuiltinFunction.Info info) {
291: super (self, info);
292: }
293:
294: public PyBuiltinFunction bind(PyObject self) {
295: return new exposed___rmul__(self, info);
296: }
297:
298: public PyObject __call__(PyObject arg0) {
299: return ((PyTuple) self).tuple___rmul__(arg0);
300: }
301:
302: }
303: dict.__setitem__("__rmul__", new PyMethodDescr("__rmul__",
304: PyTuple.class, 1, 1, new exposed___rmul__(null, null)));
305: class exposed___hash__ extends PyBuiltinMethodNarrow {
306:
307: exposed___hash__(PyObject self, PyBuiltinFunction.Info info) {
308: super (self, info);
309: }
310:
311: public PyBuiltinFunction bind(PyObject self) {
312: return new exposed___hash__(self, info);
313: }
314:
315: public PyObject __call__() {
316: return Py.newInteger(((PyTuple) self).tuple_hashCode());
317: }
318:
319: }
320: dict.__setitem__("__hash__", new PyMethodDescr("__hash__",
321: PyTuple.class, 0, 0, new exposed___hash__(null, null)));
322: class exposed___repr__ extends PyBuiltinMethodNarrow {
323:
324: exposed___repr__(PyObject self, PyBuiltinFunction.Info info) {
325: super (self, info);
326: }
327:
328: public PyBuiltinFunction bind(PyObject self) {
329: return new exposed___repr__(self, info);
330: }
331:
332: public PyObject __call__() {
333: return new PyString(((PyTuple) self).tuple_toString());
334: }
335:
336: }
337: dict.__setitem__("__repr__", new PyMethodDescr("__repr__",
338: PyTuple.class, 0, 0, new exposed___repr__(null, null)));
339: dict.__setitem__("__new__", new PyNewWrapper(PyTuple.class,
340: "__new__", -1, -1) {
341:
342: public PyObject new_impl(boolean init, PyType subtype,
343: PyObject[] args, String[] keywords) {
344: return tuple_new(this , init, subtype, args, keywords);
345: }
346:
347: });
348: }
349:
350: //~ END GENERATED REGION -- DO NOT EDIT SEE gexpose.py
351:
352: private static final PyType TUPLETYPE = PyType
353: .fromClass(PyTuple.class);
354:
355: public PyTuple() {
356: this (TUPLETYPE, Py.EmptyObjects);
357: }
358:
359: public PyTuple(PyObject[] elements) {
360: this (TUPLETYPE, elements);
361: }
362:
363: public PyTuple(PyType subtype, PyObject[] elements) {
364: super (subtype, elements);
365: }
366:
367: final static PyObject tuple_new(PyNewWrapper new_, boolean init,
368: PyType subtype, PyObject[] args, String[] keywords) {
369: ArgParser ap = new ArgParser("tuple", args, keywords,
370: new String[] { "sequence" }, 0);
371: PyObject S = ap.getPyObject(0, null);
372: if (new_.for_type == subtype) {
373: if (S == null) {
374: return new PyTuple();
375: }
376: if (S instanceof PyTupleDerived) {
377: return new PyTuple(((PyTuple) S).getArray());
378: }
379: if (S instanceof PyTuple) {
380: return S;
381: }
382: PyObject iter = S.__iter__();
383: // it's not always possible to know the length of the iterable
384: ArrayList a = new ArrayList(10);
385: for (PyObject item = null; (item = iter.__iternext__()) != null;) {
386: a.add(item);
387: }
388: return new PyTuple((PyObject[]) a.toArray(new PyObject[a
389: .size()]));
390: } else {
391: if (S == null) {
392: return new PyTupleDerived(subtype, Py.EmptyObjects);
393: }
394: PyObject iter = S.__iter__();
395: // it's not always possible to know the length of the iterable
396: ArrayList a = new ArrayList(10);
397: for (PyObject item = null; (item = iter.__iternext__()) != null;) {
398: a.add(item);
399: }
400: return new PyTupleDerived(subtype, (PyObject[]) a
401: .toArray(new PyObject[a.size()]));
402: }
403: }
404:
405: public String safeRepr() throws PyIgnoreMethodTag {
406: return "'tuple' object";
407: }
408:
409: protected PyObject getslice(int start, int stop, int step) {
410: if (step > 0 && stop < start)
411: stop = start;
412: int n = sliceLength(start, stop, step);
413: PyObject[] newArray = new PyObject[n];
414: PyObject[] array = getArray();
415:
416: if (step == 1) {
417: System.arraycopy(array, start, newArray, 0, stop - start);
418: return new PyTuple(newArray);
419: }
420: int j = 0;
421: for (int i = start; j < n; i += step) {
422: newArray[j] = array[i];
423: j++;
424: }
425: return new PyTuple(newArray);
426: }
427:
428: protected PyObject repeat(int count) {
429:
430: PyObject[] array = getArray();
431: int l = size();
432: PyObject[] newArray = new PyObject[l * count];
433: for (int i = 0; i < count; i++) {
434: System.arraycopy(array, 0, newArray, i * l, l);
435: }
436: return new PyTuple(newArray);
437: }
438:
439: public int __len__() {
440: return tuple___len__();
441: }
442:
443: final int tuple___len__() {
444: return size();
445: }
446:
447: final boolean tuple___contains__(PyObject o) {
448: return super .__contains__(o);
449: }
450:
451: final PyObject tuple___ne__(PyObject o) {
452: return super .__ne__(o);
453: }
454:
455: final PyObject tuple___eq__(PyObject o) {
456: return super .__eq__(o);
457: }
458:
459: final PyObject tuple___gt__(PyObject o) {
460: return super .__gt__(o);
461: }
462:
463: final PyObject tuple___ge__(PyObject o) {
464: return super .__ge__(o);
465: }
466:
467: final PyObject tuple___lt__(PyObject o) {
468: return super .__lt__(o);
469: }
470:
471: final PyObject tuple___le__(PyObject o) {
472: return super .__le__(o);
473: }
474:
475: public PyObject __add__(PyObject generic_other) {
476: return tuple___add__(generic_other);
477: }
478:
479: final PyObject tuple___add__(PyObject generic_other) {
480: PyTuple sum = null;
481: if (generic_other instanceof PyTuple) {
482: PyTuple otherTuple = (PyTuple) generic_other;
483: PyObject[] array = getArray();
484: PyObject[] otherArray = otherTuple.getArray();
485: int this Len = size();
486: int otherLen = otherTuple.size();
487: PyObject[] newArray = new PyObject[this Len + otherLen];
488: System.arraycopy(array, 0, newArray, 0, this Len);
489: System
490: .arraycopy(otherArray, 0, newArray, this Len,
491: otherLen);
492: sum = new PyTuple(newArray);
493: }
494: return sum;
495: }
496:
497: final PyObject tuple___mul__(PyObject o) {
498: if (!(o instanceof PyInteger || o instanceof PyLong))
499: throw Py.TypeError("can't multiply sequence to non-int");
500: int count = ((PyInteger) o.__int__()).getValue();
501: return repeat(count);
502: }
503:
504: final PyObject tuple___rmul__(PyObject o) {
505: if (!(o instanceof PyInteger || o instanceof PyLong))
506: throw Py.TypeError("can't multiply sequence to non-int");
507: int count = ((PyInteger) o.__int__()).getValue();
508: return repeat(count);
509: }
510:
511: /**
512: * Used for pickling.
513: *
514: * @return a tuple of (class, tuple)
515: */
516: public PyObject __reduce__() {
517: return tuple___reduce__();
518: }
519:
520: final PyObject tuple___reduce__() {
521: PyTuple newargs = __getnewargs__();
522: return new PyTuple(new PyObject[] { getType(), newargs });
523: }
524:
525: public PyTuple __getnewargs__() {
526: return new PyTuple(
527: new PyObject[] { new PyList(list.getArray()) });
528: }
529:
530: public int hashCode() {
531: return tuple_hashCode();
532: }
533:
534: final int tuple_hashCode() {
535: return super .hashCode();
536: }
537:
538: private String subobjRepr(PyObject o) {
539: if (o == null)
540: return "null";
541: return o.__repr__().toString();
542: }
543:
544: public String toString() {
545: return tuple_toString();
546: }
547:
548: final String tuple_toString() {
549: StringBuffer buf = new StringBuffer("(");
550: PyObject[] array = getArray();
551: int arrayLen = size();
552: for (int i = 0; i < arrayLen - 1; i++) {
553: buf.append(subobjRepr(array[i]));
554: buf.append(", ");
555: }
556: if (arrayLen > 0)
557: buf.append(subobjRepr(array[arrayLen - 1]));
558: if (arrayLen == 1)
559: buf.append(",");
560: buf.append(")");
561: return buf.toString();
562: }
563:
564: public List subList(int fromIndex, int toIndex) {
565: return Collections.unmodifiableList(list.subList(fromIndex,
566: toIndex));
567: }
568:
569: // Make PyTuple immutable from the collections interfaces by overriding
570: // all the mutating methods to throw UnsupportedOperationException exception.
571: // This is how Collections.unmodifiableList() does it.
572: public Iterator iterator() {
573: return new Iterator() {
574: Iterator i = list.iterator();
575:
576: public void remove() {
577: throw new UnsupportedOperationException();
578: }
579:
580: public boolean hasNext() {
581: return i.hasNext();
582: }
583:
584: public Object next() {
585: return i.next();
586: }
587: };
588: }
589:
590: public boolean add(Object o) {
591: throw new UnsupportedOperationException();
592: }
593:
594: public boolean remove(Object o) {
595: throw new UnsupportedOperationException();
596: }
597:
598: public boolean addAll(Collection coll) {
599: throw new UnsupportedOperationException();
600: }
601:
602: public boolean removeAll(Collection coll) {
603: throw new UnsupportedOperationException();
604: }
605:
606: public boolean retainAll(Collection coll) {
607: throw new UnsupportedOperationException();
608: }
609:
610: public void clear() {
611: throw new UnsupportedOperationException();
612: }
613:
614: public Object set(int index, Object element) {
615: throw new UnsupportedOperationException();
616: }
617:
618: public void add(int index, Object element) {
619: throw new UnsupportedOperationException();
620: }
621:
622: public Object remove(int index) {
623: throw new UnsupportedOperationException();
624: }
625:
626: public boolean addAll(int index, Collection c) {
627: throw new UnsupportedOperationException();
628: }
629:
630: public ListIterator listIterator() {
631: return listIterator(0);
632: }
633:
634: public ListIterator listIterator(final int index) {
635: return new ListIterator() {
636: ListIterator i = list.listIterator(index);
637:
638: public boolean hasNext() {
639: return i.hasNext();
640: }
641:
642: public Object next() {
643: return i.next();
644: }
645:
646: public boolean hasPrevious() {
647: return i.hasPrevious();
648: }
649:
650: public Object previous() {
651: return i.previous();
652: }
653:
654: public int nextIndex() {
655: return i.nextIndex();
656: }
657:
658: public int previousIndex() {
659: return i.previousIndex();
660: }
661:
662: public void remove() {
663: throw new UnsupportedOperationException();
664: }
665:
666: public void set(Object o) {
667: throw new UnsupportedOperationException();
668: }
669:
670: public void add(Object o) {
671: throw new UnsupportedOperationException();
672: }
673: };
674: }
675: }
|