homedukeAlgorithmen & Datenstrukturen mit Java: ds.util.Iterator Prof. Dr. Uwe Schmidt FH Wedel

ds.util.Iterator

   1package ds.util;
   2
   3/** This iterator supports a few more operation
   4    than the java.util one. These operations can
   5    be used by all classes supporting the Iterator
   6    interface and give us generally applicable
   7    operations for a lot of containers.
   8    These iterators have much in common with lazy
   9    list in Haskell
  10
  11    .1 The remove operation, which is a wart,
  12       always raises an execpton.
  13
  14    .2 An iterator can show it's elements.
  15       So we have a show method
  16       for all classes supporting ds.util.Iterator.
  17
  18    .3 An iterator can count it's elements.
  19       Same advantage as in .2
  20
  21    .4 An iterator can fold it's elements,
  22       this corresponds to a Haskell foldl.
  23
  24    .5 An iterator can build a new iterator with
  25       all elements processed element wise by a
  26       unary function, corresponds to Haskell map.
  27
  28 */
  29
  30public abstract class Iterator<E>
  31    implements java.util.Iterator<E> {
  32
  33    /** get rid of repeated implementations
  34        of unsupported operation remove
  35    */
  36    public void remove() {
  37        throw
  38            new UnsupportedOperationException
  39            ("iterator does not support remove");
  40    }
  41
  42    /** An iterator can count it's elements.
  43     */
  44    public int size() {
  45        int res = 0;
  46        while ( hasNext() ) {
  47            ++res;
  48            next();
  49        }
  50        return
  51            res;
  52    }
  53
  54    /** An iterator can fold it's elements,
  55        a generalisation of size
  56    */
  57    public <R> R fold(R nFunction2<R,E,R> f) {
  58        R res = n;
  59        while ( hasNext() ) {
  60            E x = next();
  61            res = f.apply(resx);
  62        }
  63        return
  64            res;
  65    }
  66
  67    /** An iterator can generate a another iterator
  68        containig all elements processed
  69        by a unary function
  70    */
  71    public <R> Iterator<R> map(Function<E,R> f) {
  72        return
  73            new MapIterator<E,R>(thisf);
  74    }
  75
  76    /** All quantifier for iterators
  77    */
  78    public boolean all(Predicate<E> p) {
  79        boolean res = true;
  80        while ( res && hasNext() ) {
  81            res = p.isTrue(next());
  82        }
  83        return
  84            res;
  85    }
  86
  87    /** Any quantifier for iterators
  88    */
  89    public boolean any(Predicate<E> p) {
  90        boolean res = false;
  91        while ( ! res && hasNext() ) {
  92            res = p.isTrue(next());
  93        }
  94        return
  95            res;
  96    }
  97
  98    /** An iterator can show itself,
  99        but afterwards it becomes useless,
 100        all elements are already consumed
 101    */
 102    public String toString() {
 103        StringBuffer res = new StringBuffer();
 104
 105        if ( hasNext() ) {
 106            boolean first = true;
 107
 108            while ( hasNext() ) {
 109                E x = next();
 110                res.append(first ? "[" : ",");
 111                res.append(x);
 112                first = false;
 113            }
 114        } else {
 115            res.append("[");
 116        }
 117        res.append("]");
 118
 119        return
 120            new String(res);
 121    }
 122}

Die Quelle: Iterator.java


Letzte Änderung: 11.12.2015
© Prof. Dr. Uwe Schmidt
Prof. Dr. Uwe Schmidt FH Wedel