Gehe zu deutscher Webseite

ViaThinkSoft CodeLib

This article is in:
CodeLibProgramming aidsJava

SimpleCounter.java

package de.viathinksoft.distributed.apps.sandbox;

import java.math.BigInteger;
import java.util.Iterator;

public class SimpleCounter implements Iterator<BigInteger>, Iterable<BigInteger> {
        
        private BigInteger position = BigInteger.ZERO;
        
        public SimpleCounter(BigInteger start) {
                this.position = start; 
        }

        @Override
        public boolean hasNext() {
                return true;
        }

        @Override
        public BigInteger next() {
                position = position.add(BigInteger.ONE);
                return position;
        }

        @Override
        public void remove() {
                throw new UnsupportedOperationException();
        }

        @Override
        public Iterator<BigInteger> iterator() {
                return this;
        }
}

Example:

public static void main(String args[]) {
        SimpleCounter b = new SimpleCounter(new BigInteger("5"));
        for (SectionImmortableNumber i : b) {
                System.out.println(i);
        }
}
Daniel Marschall
ViaThinkSoft Co-Founder