001    /*
002     * $Id: LongIterable.java 3300 2010-08-29 19:48:28Z kredel $
003     */
004    
005    package edu.jas.util;
006    
007    
008    import java.util.Iterator;
009    import java.util.NoSuchElementException;
010    
011    
012    /**
013     * Iterable for Long.
014     * @author Heinz Kredel
015     */
016    public class LongIterable implements Iterable<Long> {
017    
018    
019        private boolean nonNegative = true;
020    
021    
022        private long upperBound = Long.MAX_VALUE;
023    
024    
025        /**
026         * Constructor.
027         */
028        public LongIterable() {
029        }
030    
031    
032        /**
033         * Constructor.
034         */
035        public LongIterable(long ub) {
036            upperBound = ub;
037        }
038    
039    
040        /** Set the upper bound for the iterator.
041         * @param ub an upper bound for the iterator elements. 
042         */
043        public void setUpperBound(long ub) {
044            upperBound = ub;
045        }
046    
047    
048        /** Get the upper bound for the iterator.
049         * @return the upper bound for the iterator elements. 
050         */
051        public long getUpperBound() {
052            return upperBound;
053        }
054    
055    
056        /** Set the iteration algorithm to all elements.
057         */
058        public void setAllIterator() {
059            nonNegative = false;
060        }
061    
062    
063        /** Set the iteration algorithm to non-negative elements.
064         */
065        public void setNonNegativeIterator() {
066            nonNegative = true;
067        }
068    
069    
070        /**
071         * Get an iterator over Long.
072         * @return an iterator.
073         */
074        public Iterator<Long> iterator() {
075            return new LongIterator(nonNegative,upperBound);
076        }
077    
078    }
079    
080    
081    /**
082     * Long iterator.
083     * @author Heinz Kredel
084     */
085    class LongIterator implements Iterator<Long> {
086    
087    
088        /**
089         * data structure.
090         */
091        long current;
092    
093    
094        boolean empty;
095    
096    
097        final boolean nonNegative;
098    
099    
100        protected long upperBound;
101    
102    
103        /** Set the upper bound for the iterator.
104         * @param ub an upper bound for the iterator elements. 
105         */
106        public void setUpperBound(long ub) {
107            upperBound = ub;
108        }
109    
110    
111        /** Get the upper bound for the iterator.
112         * @return the upper bound for the iterator elements. 
113         */
114        public long getUpperBound() {
115            return upperBound;
116        }
117    
118    
119        /**
120         * Long iterator constructor.
121         */
122        public LongIterator() {
123            this(false,Long.MAX_VALUE);
124        }
125    
126    
127        /**
128         * Long iterator constructor.
129         * @param nn true for an iterator over non-negative longs, false for all elements iterator.
130         * @param ub an upper bound for the entrys.
131         */
132        public LongIterator(boolean nn, long ub) {
133            current = 0L;
134            //System.out.println("current = " + current);
135            empty = false;
136            nonNegative = nn;
137            upperBound = ub;
138            //System.out.println("upperBound = " + upperBound);
139        }
140    
141    
142        /**
143         * Test for availability of a next long.
144         * @return true if the iteration has more Longs, else false.
145         */
146        public synchronized boolean hasNext() {
147            return !empty;
148        }
149    
150    
151        /**
152         * Get next Long.
153         * @return next Long.
154         */
155        public synchronized Long next() {
156            if (empty) {
157                throw new NoSuchElementException("invalid call of next()");
158            }
159            Long res = new Long(current);
160            if ( nonNegative ) {
161                current++;
162            } else if ( current > 0L ) {
163                current = -current;
164            } else {
165                current = -current;
166                current++;
167            }
168            if ( current > upperBound ) {
169                empty = true;
170            }
171            return res;
172        }
173    
174    
175        /**
176         * Remove a tuple if allowed.
177         */
178        public void remove() {
179            throw new UnsupportedOperationException("cannnot remove elements");
180        }
181    
182    }