package speiger.src.collections.PACKAGE.utils; import java.util.Iterator; import speiger.src.collections.PACKAGE.collections.ITERATOR; import speiger.src.collections.PACKAGE.lists.LIST_ITERATOR; import speiger.src.collections.PACKAGE.collections.BI_ITERATOR; /** * A Helper class for Iterators */ public class ITERATORS { public static final EmptyIterator NO_GENERIC_TYPE EMPTY = new EmptyIteratorBRACES(); /** * Returns a Immutable EmptyIterator instance that is automatically casted. * @return an empty iterator */ public static GENERIC_KEY_BRACES EmptyIterator KEY_GENERIC_TYPE emptyIterator() { #if TYPE_OBJECT return (EmptyIterator)EMPTY; #else return EMPTY; #endif } public static GENERIC_KEY_BRACES BI_ITERATOR KEY_GENERIC_TYPE invert(BI_ITERATOR KEY_GENERIC_TYPE it) { return new BI_ITERATOR KEY_GENERIC_TYPE() { @Override public KEY_TYPE NEXT() { return it.PREVIOUS(); } @Override public boolean hasNext() { return it.hasPrevious(); } @Override public boolean hasPrevious() { return it.hasNext(); } @Override public KEY_TYPE PREVIOUS() { return it.NEXT(); } @Override public void remove() { it.remove(); } }; } public static GENERIC_KEY_BRACES LIST_ITERATOR KEY_GENERIC_TYPE invert(LIST_ITERATOR KEY_GENERIC_TYPE it) { return new LIST_ITERATOR KEY_GENERIC_TYPE() { @Override public KEY_TYPE NEXT() { return it.PREVIOUS(); } @Override public boolean hasNext() { return it.hasPrevious(); } @Override public boolean hasPrevious() { return it.hasNext(); } @Override public KEY_TYPE PREVIOUS() { return it.NEXT(); } @Override public void remove() { it.remove(); } @Override public int nextIndex() { return it.previousIndex(); } @Override public int previousIndex() { return it.nextIndex(); } @Override public void set(KEY_TYPE e) { it.set(e); } @Override public void add(KEY_TYPE e) { it.add(e); } }; } /** * Returns a Immutable Iterator instance based on the instance given. * @param l that should be made immutable/unmodifyable * @return a unmodifiable iterator wrapper. If the Iterator already a unmodifyable wrapper then it just returns itself. */ public static GENERIC_KEY_BRACES ITERATOR KEY_GENERIC_TYPE unmodifiable(ITERATOR KEY_GENERIC_TYPE iterator) { return iterator instanceof UnmodifiableIterator ? iterator : new UnmodifiableIteratorBRACES(iterator); } public static GENERIC_KEY_BRACES BI_ITERATOR KEY_GENERIC_TYPE unmodifiable(BI_ITERATOR KEY_GENERIC_TYPE iterator) { return iterator instanceof UnmodifiableBiIterator ? iterator : new UnmodifiableBiIteratorBRACES(iterator); } /** * Returns a Immutable ListIterator instance based on the instance given. * @param l that should be made immutable/unmodifyable * @return a unmodifiable listiterator wrapper. If the ListIterator already a unmodifyable wrapper then it just returns itself. */ public static GENERIC_KEY_BRACES LIST_ITERATOR KEY_GENERIC_TYPE unmodifiable(LIST_ITERATOR KEY_GENERIC_TYPE iterator) { return iterator instanceof UnmodifiableListIterator ? iterator : new UnmodifiableListIteratorBRACES(iterator); } #if !TYPE_OBJECT public static ITERATOR wrap(Iterator iterator) { return iterator instanceof ITERATOR ? (ITERATOR)iterator : new IteratorWrapper(iterator); } #endif /** * Returns a Array Wrapping iterator * @param a the array that should be wrapped * @return a Iterator that is wrapping a array. */ public static GENERIC_KEY_BRACES ArrayIterator KEY_GENERIC_TYPE wrap(KEY_TYPE[] a) { return wrap(a, 0, a.length); } /** * Returns a Array Wrapping iterator * @param a the array that should be wrapped. * @param start the index to be started from. * @param end the index that should be ended. * @return a Iterator that is wrapping a array. */ public static GENERIC_KEY_BRACES ArrayIterator KEY_GENERIC_TYPE wrap(KEY_TYPE[] a, int start, int end) { return new ArrayIteratorBRACES(a, start, end); } /** * Iterates over a iterator and inserts the values into the array and returns the amount that was inserted * @param a where the elements should be inserted * @param i the source iterator * @return the amount of elements that were inserted into the array. */ public static GENERIC_KEY_BRACES int unwrap(KEY_TYPE[] a, Iterator i) { return unwrap(a, i, 0, a.length); } /** * Iterates over a iterator and inserts the values into the array and returns the amount that was inserted * @param a where the elements should be inserted * @param i the source iterator * @param offset the array offset where the start should be * @return the amount of elements that were inserted into the array. */ public static GENERIC_KEY_BRACES int unwrap(KEY_TYPE[] a, Iterator i, int offset) { return unwrap(a, i, offset, a.length - offset); } /** * Iterates over a iterator and inserts the values into the array and returns the amount that was inserted * @param a where the elements should be inserted * @param i the source iterator * @param offset the array offset where the start should be * @param max the maximum values that should be extracted from the source * @return the amount of elements that were inserted into the array. * @throws IllegalStateException if max is smaller the 0 or if the maximum index is larger then the array */ public static GENERIC_KEY_BRACES int unwrap(KEY_TYPE[] a, Iterator i, int offset, int max) { if(max < 0) throw new IllegalStateException("The max size is smaller then 0"); if(offset + max > a.length) throw new IllegalStateException("largest array index exceeds array size"); int index = 0; for(;index a.length) throw new IllegalStateException("largest array index exceeds array size"); int index = 0; for(;index a.length) throw new IllegalStateException("largest array index exceeds array size"); int index = 0; for(;index iter; public IteratorWrapper(Iterator iter) { this.iter = iter; } @Override public boolean hasNext() { return iter.hasNext(); } @Override public KEY_TYPE NEXT() { return OBJ_TO_KEY(iter.next()); } @Override @Deprecated public CLASS_TYPE next() { return iter.next(); } } #endif private static class UnmodifiableListIterator KEY_GENERIC_TYPE implements LIST_ITERATOR KEY_GENERIC_TYPE { LIST_ITERATOR KEY_GENERIC_TYPE iter; UnmodifiableListIterator(LIST_ITERATOR KEY_GENERIC_TYPE iter) { this.iter = iter; } @Override public boolean hasNext() { return iter.hasNext(); } @Override public boolean hasPrevious() { return iter.hasPrevious(); } @Override public int nextIndex() { return iter.nextIndex(); } @Override public int previousIndex() { return iter.previousIndex(); } @Override public void remove() { throw new UnsupportedOperationException(); } @Override public KEY_TYPE PREVIOUS() { return iter.PREVIOUS(); } @Override public KEY_TYPE NEXT() { return iter.NEXT(); } @Override public void set(KEY_TYPE e) { throw new UnsupportedOperationException(); } @Override public void add(KEY_TYPE e) { throw new UnsupportedOperationException(); } } private static class UnmodifiableBiIterator KEY_GENERIC_TYPE implements BI_ITERATOR KEY_GENERIC_TYPE { BI_ITERATOR KEY_GENERIC_TYPE iter; UnmodifiableBiIterator(BI_ITERATOR KEY_GENERIC_TYPE iter) { this.iter = iter; } @Override public KEY_TYPE NEXT() { return iter.NEXT(); } @Override public boolean hasNext() { return iter.hasNext(); } @Override public boolean hasPrevious() { return iter.hasPrevious(); } @Override public KEY_TYPE PREVIOUS() { return iter.PREVIOUS(); } } private static class UnmodifiableIterator KEY_GENERIC_TYPE implements ITERATOR KEY_GENERIC_TYPE { ITERATOR KEY_GENERIC_TYPE iterator; UnmodifiableIterator(ITERATOR KEY_GENERIC_TYPE iterator) { this.iterator = iterator; } @Override public boolean hasNext() { return iterator.hasNext(); } @Override public KEY_TYPE NEXT() { return iterator.NEXT(); } } private static class EmptyIterator KEY_GENERIC_TYPE implements LIST_ITERATOR KEY_GENERIC_TYPE { @Override public boolean hasNext() { return false; } @Override public KEY_TYPE NEXT() { return EMPTY_KEY_VALUE; } @Override public boolean hasPrevious() { return false; } @Override public KEY_TYPE PREVIOUS() { return EMPTY_KEY_VALUE; } @Override public int nextIndex() { return 0; } @Override public int previousIndex() { return 0; } @Override public void remove() { throw new UnsupportedOperationException(); } @Override public void set(KEY_TYPE e) { throw new UnsupportedOperationException(); } @Override public void add(KEY_TYPE e) { throw new UnsupportedOperationException(); } } private static class ArrayIterator KEY_GENERIC_TYPE implements ITERATOR KEY_GENERIC_TYPE { KEY_TYPE[] a; int from; int to; ArrayIterator(KEY_TYPE[] a, int from, int to) { this.a = a; this.from = from; this.to = to; } @Override public boolean hasNext() { return from < to; } @Override public KEY_TYPE NEXT() { return a[from++]; } @Override public int skip(int amount) { if(amount < 0) throw new IllegalStateException("Negative Numbers are not allowed"); int left = Math.min(amount, to - from); from += left; return amount - left; } } }