File: PyReversedIterator.java

package info (click to toggle)
jython 2.5.3-16%2Bdeb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 43,772 kB
  • ctags: 106,434
  • sloc: python: 351,322; java: 216,349; xml: 1,584; sh: 330; perl: 114; ansic: 102; makefile: 45
file content (34 lines) | stat: -rw-r--r-- 793 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package org.python.core;

/**
 * An iterator that yields the objects from a sequence-like object in reverse
 * order. 
 */
public class PyReversedIterator extends PyIterator {

    /**
     * Creates an iterator that first yields the item at __len__ - 1 on seq and
     * returns the objects in descending order from there down to 0.
     * 
     * @param seq -
     *            an object that supports __getitem__ and __len__
     */
    public PyReversedIterator(PyObject seq) {
        this.seq = seq;
        idx = seq.__len__();
        if(idx > 0) {
            idx = idx - 1;
        }
    }

    public PyObject __iternext__() {
        if(idx >= 0) {
            return seq.__finditem__(idx--);
        }
        return null;
    }

    private PyObject seq;

    private int idx;
}