KickJava   Java API By Example, From Geeks To Geeks.

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


1 // Copyright (c) Corporation for National Research Initiatives
2
package org.python.core;
3
4 import java.util.Collection JavaDoc;
5 import java.util.Collections JavaDoc;
6 import java.util.Iterator JavaDoc;
7 import java.util.List JavaDoc;
8 import java.util.ArrayList JavaDoc;
9 import java.util.ListIterator JavaDoc;
10
11 /**
12  * A builtin python tuple.
13  */

14
15 public class PyTuple extends PySequenceList implements ClassDictInit
16 {
17
18     /** <i>Internal use only. Do not call this method explicit.</i> */
19     public static void classDictInit(PyObject dict)throws PyIgnoreMethodTag {}
20     /* type info */
21
22     public static final String JavaDoc exposed_name="tuple";
23
24     public static void typeSetup(PyObject dict,PyType.Newstyle marker) {
25         class exposed___ne__ extends PyBuiltinFunctionNarrow {
26
27             private PyTuple self;
28
29             public PyObject getSelf() {
30                 return self;
31             }
32
33             exposed___ne__(PyTuple self,PyBuiltinFunction.Info info) {
34                 super(info);
35                 this.self=self;
36             }
37
38             public PyBuiltinFunction makeBound(PyObject self) {
39                 return new exposed___ne__((PyTuple)self,info);
40             }
41
42             public PyObject __call__(PyObject arg0) {
43                 PyObject ret=self.tuple___ne__(arg0);
44                 if (ret==null)
45                     return Py.NotImplemented;
46                 return ret;
47             }
48
49             public PyObject inst_call(PyObject gself,PyObject arg0) {
50                 PyTuple self=(PyTuple)gself;
51                 PyObject ret=self.tuple___ne__(arg0);
52                 if (ret==null)
53                     return Py.NotImplemented;
54                 return ret;
55             }
56
57         }
58         dict.__setitem__("__ne__",new PyMethodDescr("__ne__",PyTuple.class,1,1,new exposed___ne__(null,null)));
59         class exposed___eq__ extends PyBuiltinFunctionNarrow {
60
61             private PyTuple self;
62
63             public PyObject getSelf() {
64                 return self;
65             }
66
67             exposed___eq__(PyTuple self,PyBuiltinFunction.Info info) {
68                 super(info);
69                 this.self=self;
70             }
71
72             public PyBuiltinFunction makeBound(PyObject self) {
73                 return new exposed___eq__((PyTuple)self,info);
74             }
75
76             public PyObject __call__(PyObject arg0) {
77                 PyObject ret=self.tuple___eq__(arg0);
78                 if (ret==null)
79                     return Py.NotImplemented;
80                 return ret;
81             }
82
83             public PyObject inst_call(PyObject gself,PyObject arg0) {
84                 PyTuple self=(PyTuple)gself;
85                 PyObject ret=self.tuple___eq__(arg0);
86                 if (ret==null)
87                     return Py.NotImplemented;
88                 return ret;
89             }
90
91         }
92         dict.__setitem__("__eq__",new PyMethodDescr("__eq__",PyTuple.class,1,1,new exposed___eq__(null,null)));
93         class exposed___contains__ extends PyBuiltinFunctionNarrow {
94
95             private PyTuple self;
96
97             public PyObject getSelf() {
98                 return self;
99             }
100
101             exposed___contains__(PyTuple self,PyBuiltinFunction.Info info) {
102                 super(info);
103                 this.self=self;
104             }
105
106             public PyBuiltinFunction makeBound(PyObject self) {
107                 return new exposed___contains__((PyTuple)self,info);
108             }
109
110             public PyObject __call__(PyObject arg0) {
111                 return Py.newBoolean(self.tuple___contains__(arg0));
112              }
113
114             public PyObject inst_call(PyObject gself,PyObject arg0) {
115                 PyTuple self=(PyTuple)gself;
116                 return Py.newBoolean(self.tuple___contains__(arg0));
117             }
118
119         }
120         dict.__setitem__("__contains__",new PyMethodDescr("__contains__",PyTuple.class,1,1,new exposed___contains__(null,null)));
121         class exposed___len__ extends PyBuiltinFunctionNarrow {
122
123             private PyTuple self;
124
125             public PyObject getSelf() {
126                 return self;
127             }
128
129             exposed___len__(PyTuple self,PyBuiltinFunction.Info info) {
130                 super(info);
131                 this.self=self;
132             }
133
134             public PyBuiltinFunction makeBound(PyObject self) {
135                 return new exposed___len__((PyTuple)self,info);
136             }
137
138             public PyObject __call__() {
139                 return Py.newInteger(self.tuple___len__());
140             }
141
142             public PyObject inst_call(PyObject gself) {
143                 PyTuple self=(PyTuple)gself;
144                 return Py.newInteger(self.tuple___len__());
145             }
146
147         }
148         dict.__setitem__("__len__",new PyMethodDescr("__len__",PyTuple.class,0,0,new exposed___len__(null,null)));
149         class exposed___add__ extends PyBuiltinFunctionNarrow {
150
151             private PyTuple self;
152
153             public PyObject getSelf() {
154                 return self;
155             }
156
157             exposed___add__(PyTuple self,PyBuiltinFunction.Info info) {
158                 super(info);
159                 this.self=self;
160             }
161
162             public PyBuiltinFunction makeBound(PyObject self) {
163                 return new exposed___add__((PyTuple)self,info);
164             }
165
166             public PyObject __call__(PyObject arg0) {
167                 return self.tuple___add__(arg0);
168             }
169
170             public PyObject inst_call(PyObject gself,PyObject arg0) {
171                 PyTuple self=(PyTuple)gself;
172                 return self.tuple___add__(arg0);
173             }
174
175         }
176         dict.__setitem__("__add__",new PyMethodDescr("__add__",PyTuple.class,1,1,new exposed___add__(null,null)));
177         class exposed___mul__ extends PyBuiltinFunctionNarrow {
178
179             private PyTuple self;
180
181             public PyObject getSelf() {
182                 return self;
183             }
184
185             exposed___mul__(PyTuple self,PyBuiltinFunction.Info info) {
186                 super(info);
187                 this.self=self;
188             }
189
190             public PyBuiltinFunction makeBound(PyObject self) {
191                 return new exposed___mul__((PyTuple)self,info);
192             }
193
194             public PyObject __call__(PyObject arg0) {
195                 return self.tuple___mul__(arg0);
196             }
197
198             public PyObject inst_call(PyObject gself,PyObject arg0) {
199                 PyTuple self=(PyTuple)gself;
200                 return self.tuple___mul__(arg0);
201             }
202
203         }
204         dict.__setitem__("__mul__",new PyMethodDescr("__mul__",PyTuple.class,1,1,new exposed___mul__(null,null)));
205         class exposed___rmul__ extends PyBuiltinFunctionNarrow {
206
207             private PyTuple self;
208
209             public PyObject getSelf() {
210                 return self;
211             }
212
213             exposed___rmul__(PyTuple self,PyBuiltinFunction.Info info) {
214                 super(info);
215                 this.self=self;
216             }
217
218             public PyBuiltinFunction makeBound(PyObject self) {
219                 return new exposed___rmul__((PyTuple)self,info);
220             }
221
222             public PyObject __call__(PyObject arg0) {
223                 return self.tuple___rmul__(arg0);
224             }
225
226             public PyObject inst_call(PyObject gself,PyObject arg0) {
227                 PyTuple self=(PyTuple)gself;
228                 return self.tuple___rmul__(arg0);
229             }
230
231         }
232         dict.__setitem__("__rmul__",new PyMethodDescr("__rmul__",PyTuple.class,1,1,new exposed___rmul__(null,null)));
233         class exposed___hash__ extends PyBuiltinFunctionNarrow {
234
235             private PyTuple self;
236
237             public PyObject getSelf() {
238                 return self;
239             }
240
241             exposed___hash__(PyTuple self,PyBuiltinFunction.Info info) {
242                 super(info);
243                 this.self=self;
244             }
245
246             public PyBuiltinFunction makeBound(PyObject self) {
247                 return new exposed___hash__((PyTuple)self,info);
248             }
249
250             public PyObject __call__() {
251                 return Py.newInteger(self.tuple_hashCode());
252             }
253
254             public PyObject inst_call(PyObject gself) {
255                 PyTuple self=(PyTuple)gself;
256                 return Py.newInteger(self.tuple_hashCode());
257             }
258
259         }
260         dict.__setitem__("__hash__",new PyMethodDescr("__hash__",PyTuple.class,0,0,new exposed___hash__(null,null)));
261         class exposed___repr__ extends PyBuiltinFunctionNarrow {
262
263             private PyTuple self;
264
265             public PyObject getSelf() {
266                 return self;
267             }
268
269             exposed___repr__(PyTuple self,PyBuiltinFunction.Info info) {
270                 super(info);
271                 this.self=self;
272             }
273
274             public PyBuiltinFunction makeBound(PyObject self) {
275                 return new exposed___repr__((PyTuple)self,info);
276             }
277
278             public PyObject __call__() {
279                 return new PyString(self.tuple_toString());
280             }
281
282             public PyObject inst_call(PyObject gself) {
283                 PyTuple self=(PyTuple)gself;
284                 return new PyString(self.tuple_toString());
285             }
286
287         }
288         dict.__setitem__("__repr__",new PyMethodDescr("__repr__",PyTuple.class,0,0,new exposed___repr__(null,null)));
289         dict.__setitem__("__new__",new PyNewWrapper(PyTuple.class,"__new__",-1,-1) {
290                 public PyObject new_impl(boolean init,PyType subtype,PyObject[]args,String JavaDoc[]keywords) {
291                     return tuple_new(this,init,subtype,args,keywords);
292                 }
293             });
294     }
295
296     public PyTuple() {
297         this(Py.EmptyObjects);
298     }
299
300     public PyTuple(PyObject[] elements) {
301         super(elements);
302     }
303
304     final static PyObject tuple_new(PyObject new_, boolean init, PyType subtype,
305             PyObject[] args, String JavaDoc[] keywords) {
306         ArgParser ap = new ArgParser("tuple", args, keywords, new String JavaDoc[] { "S" }, 0);
307         PyObject S = ap.getPyObject(0, null);
308         if (S == null)
309             return new PyTuple();
310         if (S instanceof PyTuple)
311             return S;
312         PyObject iter = S.__iter__();
313         // it's not always possible to know the length of the iterable
314
ArrayList JavaDoc a = new ArrayList JavaDoc(10);
315         for (PyObject item = null; (item = iter.__iternext__()) != null; ) {
316             a.add(item);
317         }
318         return new PyTuple((PyObject[])a.toArray(new PyObject[a.size()]));
319     }
320
321     public String JavaDoc safeRepr() throws PyIgnoreMethodTag {
322         return "'tuple' object";
323     }
324
325     protected PyObject getslice(int start, int stop, int step) {
326         if (step > 0 && stop < start)
327             stop = start;
328         int n = sliceLength(start, stop, step);
329         PyObject[] newArray = new PyObject[n];
330         PyObject[] array = getArray();
331
332         if (step == 1) {
333             System.arraycopy(array, start, newArray, 0, stop-start);
334             return new PyTuple(newArray);
335         }
336         int j = 0;
337         for (int i=start; j<n; i+=step) {
338             newArray[j] = array[i];
339             j++;
340         }
341         return new PyTuple(newArray);
342     }
343
344     protected PyObject repeat(int count) {
345
346         PyObject[] array = getArray();
347         int l = size();
348         PyObject[] newArray = new PyObject[l*count];
349         for (int i=0; i<count; i++) {
350             System.arraycopy(array, 0, newArray, i*l, l);
351         }
352         return new PyTuple(newArray);
353     }
354
355     public int __len__() {
356         return tuple___len__();
357     }
358
359     final int tuple___len__() {
360         return size();
361     }
362
363     final boolean tuple___contains__(PyObject o) {
364         return super.__contains__(o);
365     }
366
367     final PyObject tuple___ne__(PyObject o) {
368         return super.__ne__(o);
369     }
370
371     final PyObject tuple___eq__(PyObject o) {
372         return super.__eq__(o);
373     }
374
375     public PyObject __add__(PyObject generic_other) {
376         return tuple___add__(generic_other);
377     }
378
379     final PyObject tuple___add__(PyObject generic_other) {
380         PyTuple sum = null;
381         if (generic_other instanceof PyTuple) {
382             PyTuple otherTuple = (PyTuple)generic_other;
383             PyObject[] array = getArray();
384             PyObject[] otherArray = otherTuple.getArray();
385             int thisLen = size();
386             int otherLen = otherTuple.size();
387             PyObject[] newArray = new PyObject[thisLen + otherLen];
388             System.arraycopy(array, 0, newArray, 0, thisLen);
389             System.arraycopy(otherArray, 0, newArray, thisLen, otherLen);
390             sum = new PyTuple(newArray);
391         }
392         return sum;
393     }
394
395     final PyObject tuple___mul__(PyObject o) {
396         if (!(o instanceof PyInteger || o instanceof PyLong))
397             throw Py.TypeError("can't multiply sequence to non-int");
398         int count = ((PyInteger)o.__int__()).getValue();
399         return repeat(count);
400     }
401
402     final PyObject tuple___rmul__(PyObject o) {
403         if (!(o instanceof PyInteger || o instanceof PyLong))
404             throw Py.TypeError("can't multiply sequence to non-int");
405         int count = ((PyInteger)o.__int__()).getValue();
406         return repeat(count);
407     }
408
409     public int hashCode() {
410         return tuple_hashCode();
411     }
412
413     final int tuple_hashCode() {
414         return super.hashCode();
415     }
416
417     private String JavaDoc subobjRepr(PyObject o) {
418         if (o == null)
419             return "null";
420         return o.__repr__().toString();
421     }
422
423     public String JavaDoc toString() {
424         return tuple_toString();
425     }
426
427     final String JavaDoc tuple_toString() {
428         StringBuffer JavaDoc buf = new StringBuffer JavaDoc("(");
429         PyObject[] array = getArray();
430         int arrayLen = size();
431         for (int i = 0; i < arrayLen-1; i++) {
432             buf.append(subobjRepr(array[i]));
433             buf.append(", ");
434         }
435         if (arrayLen > 0)
436             buf.append(subobjRepr(array[arrayLen-1]));
437         if (arrayLen == 1)
438             buf.append(",");
439         buf.append(")");
440         return buf.toString();
441     }
442
443     public List JavaDoc subList(int fromIndex, int toIndex) {
444         return Collections.unmodifiableList(list.subList(fromIndex, toIndex));
445     }
446
447     // Make PyTuple immutable from the collections interfaces by overriding
448
// all the mutating methods to throw UnsupportedOperationException exception.
449
// This is how Collections.unmodifiableList() does it.
450
public Iterator JavaDoc iterator() {
451         return new Iterator JavaDoc() {
452             Iterator JavaDoc i = list.iterator();
453             public void remove() {
454                 throw new UnsupportedOperationException JavaDoc();
455             }
456             public boolean hasNext() {
457                 return i.hasNext();
458                 }
459             public Object JavaDoc next() {
460                 return i.next();
461                 }
462         };
463     }
464
465     public boolean add(Object JavaDoc o){
466         throw new UnsupportedOperationException JavaDoc();
467     }
468
469     public boolean remove(Object JavaDoc o) {
470         throw new UnsupportedOperationException JavaDoc();
471     }
472
473     public boolean addAll(Collection JavaDoc coll) {
474         throw new UnsupportedOperationException JavaDoc();
475     }
476
477     public boolean removeAll(Collection JavaDoc coll) {
478         throw new UnsupportedOperationException JavaDoc();
479     }
480
481     public boolean retainAll(Collection JavaDoc coll) {
482         throw new UnsupportedOperationException JavaDoc();
483     }
484
485     public void clear() {
486         throw new UnsupportedOperationException JavaDoc();
487     }
488
489     public Object JavaDoc set(int index, Object JavaDoc element) {
490         throw new UnsupportedOperationException JavaDoc();
491     }
492
493     public void add(int index, Object JavaDoc element) {
494         throw new UnsupportedOperationException JavaDoc();
495     }
496
497     public Object JavaDoc remove(int index) {
498         throw new UnsupportedOperationException JavaDoc();
499     }
500
501     public boolean addAll(int index, Collection JavaDoc c) {
502         throw new UnsupportedOperationException JavaDoc();
503     }
504
505     public ListIterator JavaDoc listIterator() {
506         return listIterator(0);
507     }
508
509     public ListIterator JavaDoc listIterator(final int index) {
510         return new ListIterator JavaDoc() {
511             ListIterator JavaDoc i = list.listIterator(index);
512
513             public boolean hasNext() {return i.hasNext();}
514             public Object JavaDoc next() {return i.next();}
515             public boolean hasPrevious() {return i.hasPrevious();}
516             public Object JavaDoc previous() {return i.previous();}
517             public int nextIndex() {return i.nextIndex();}
518             public int previousIndex() {return i.previousIndex();}
519
520             public void remove() {
521                 throw new UnsupportedOperationException JavaDoc();
522             }
523
524             public void set(Object JavaDoc o) {
525                 throw new UnsupportedOperationException JavaDoc();
526             }
527
528             public void add(Object JavaDoc o) {
529                 throw new UnsupportedOperationException JavaDoc();
530             }
531         };
532     }
533 }
534
Popular Tags