Fixed Bugs with Java Iterators not throwing the correct exception.

This commit is contained in:
Speiger 2022-05-17 10:45:05 +02:00
parent 31b34f5de1
commit 068cc4a4f7
9 changed files with 44 additions and 33 deletions

View File

@ -1263,7 +1263,7 @@ public class LINKED_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE
@Override @Override
public long estimateSize() { public long estimateSize() {
return list.size - index; return (long)list.size - (long)index;
} }
@Override @Override
@ -1329,7 +1329,7 @@ public class LINKED_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE
@Override @Override
public long estimateSize() { public long estimateSize() {
return list.size - index; return (long)list.size - (long)index;
} }
@Override @Override

View File

@ -434,6 +434,7 @@ public class ARRAY_FIFO_QUEUE KEY_GENERIC_TYPE extends ABSTRACT_PRIORITY_QUEUE K
@Override @Override
public KEY_TYPE NEXT() { public KEY_TYPE NEXT() {
if(!hasNext()) throw new NoSuchElementException();
KEY_TYPE value = array[index]; KEY_TYPE value = array[index];
removeIndex(index); removeIndex(index);
index = ++index % array.length; index = ++index % array.length;

View File

@ -187,7 +187,7 @@ public class ARRAY_PRIORITY_QUEUE KEY_GENERIC_TYPE extends ABSTRACT_PRIORITY_QUE
@Override @Override
public void enqueue(KEY_TYPE e) { public void enqueue(KEY_TYPE e) {
if(size == array.length) array = Arrays.copyOf(array, (int)Math.max(Math.min((long)array.length + (array.length >> 1), SanityChecks.MAX_ARRAY_SIZE), size+1)); if(size == array.length) array = Arrays.copyOf(array, (int)Math.max(Math.min((long)array.length + (long)(array.length >> 1), (long)SanityChecks.MAX_ARRAY_SIZE), size+1));
if(firstIndex != -1){ if(firstIndex != -1){
int compare = comparator == null ? COMPAREABLE_TO_KEY(e, array[firstIndex]) : comparator.compare(e, array[firstIndex]); int compare = comparator == null ? COMPAREABLE_TO_KEY(e, array[firstIndex]) : comparator.compare(e, array[firstIndex]);
if(compare < 0) firstIndex = size; if(compare < 0) firstIndex = size;
@ -414,6 +414,7 @@ public class ARRAY_PRIORITY_QUEUE KEY_GENERIC_TYPE extends ABSTRACT_PRIORITY_QUE
@Override @Override
public KEY_TYPE NEXT() { public KEY_TYPE NEXT() {
if(!hasNext()) throw new NoSuchElementException();
return dequeue(); return dequeue();
} }
} }

View File

@ -207,7 +207,7 @@ public class HEAP_PRIORITY_QUEUE KEY_GENERIC_TYPE extends ABSTRACT_PRIORITY_QUEU
@Override @Override
public void enqueue(KEY_TYPE e) { public void enqueue(KEY_TYPE e) {
if(size == array.length) array = Arrays.copyOf(array, (int)Math.max(Math.min((long)array.length + (array.length >> 1), SanityChecks.MAX_ARRAY_SIZE), size+1)); if(size == array.length) array = Arrays.copyOf(array, (int)Math.max(Math.min((long)array.length + (long)(array.length >> 1), (long)SanityChecks.MAX_ARRAY_SIZE), size+1));
array[size++] = e; array[size++] = e;
ARRAYS.shiftUp(array, size-1, comparator); ARRAYS.shiftUp(array, size-1, comparator);
} }
@ -389,6 +389,7 @@ public class HEAP_PRIORITY_QUEUE KEY_GENERIC_TYPE extends ABSTRACT_PRIORITY_QUEU
@Override @Override
public KEY_TYPE NEXT() { public KEY_TYPE NEXT() {
if(!hasNext()) throw new NoSuchElementException();
return dequeue(); return dequeue();
} }
} }

View File

@ -34,7 +34,7 @@ public class ITERATORS
/** /**
* Empty Iterator Reference * Empty Iterator Reference
*/ */
public static final EmptyIterator NO_GENERIC_TYPE EMPTY = new EmptyIteratorBRACES(); private static final EmptyIterator NO_GENERIC_TYPE EMPTY = new EmptyIteratorBRACES();
/** /**
* Returns a Immutable EmptyIterator instance that is automatically casted. * Returns a Immutable EmptyIterator instance that is automatically casted.
@ -735,7 +735,7 @@ public class ITERATORS
@Override @Override
public KEY_TYPE NEXT() { public KEY_TYPE NEXT() {
return EMPTY_KEY_VALUE; throw new NoSuchElementException();
} }
@Override @Override
@ -745,7 +745,7 @@ public class ITERATORS
@Override @Override
public KEY_TYPE PREVIOUS() { public KEY_TYPE PREVIOUS() {
return EMPTY_KEY_VALUE; throw new NoSuchElementException();
} }
@Override @Override
@ -787,6 +787,7 @@ public class ITERATORS
@Override @Override
public KEY_TYPE NEXT() { public KEY_TYPE NEXT() {
if(!hasNext()) throw new NoSuchElementException();
return a[from++]; return a[from++];
} }
@ -854,7 +855,7 @@ public class ITERATORS
@Override @Override
public T next() { public T next() {
if(!hasNext()) throw new IllegalStateException("End of Iterator"); if(!hasNext()) throw new NoSuchElementException();
T result = last.next(); T result = last.next();
foundNext = false; foundNext = false;
return result; return result;
@ -890,7 +891,7 @@ public class ITERATORS
@Override @Override
public T next() { public T next() {
if(!hasNext()) throw new IllegalStateException("End of Iterator"); if(!hasNext()) throw new NoSuchElementException();
T result = last.next(); T result = last.next();
foundNext = false; foundNext = false;
return result; return result;
@ -921,7 +922,7 @@ public class ITERATORS
@Override @Override
public KEY_TYPE NEXT() { public KEY_TYPE NEXT() {
if(!hasNext()) throw new IllegalStateException("End of Iterator"); if(!hasNext()) throw new NoSuchElementException();
return sortedElements.GET_KEY(index++); return sortedElements.GET_KEY(index++);
} }
} }
@ -971,7 +972,7 @@ public class ITERATORS
@Override @Override
public KEY_TYPE NEXT() { public KEY_TYPE NEXT() {
if(!hasNext()) throw new IllegalStateException("End of Iterator"); if(!hasNext()) throw new NoSuchElementException();
foundNext = false; foundNext = false;
return lastFound; return lastFound;
} }
@ -1008,7 +1009,7 @@ public class ITERATORS
@Override @Override
public KEY_TYPE NEXT() { public KEY_TYPE NEXT() {
if(!hasNext()) throw new IllegalStateException("End of Iterator"); if(!hasNext()) throw new NoSuchElementException();
foundNext = false; foundNext = false;
return lastFound; return lastFound;
} }
@ -1031,7 +1032,7 @@ public class ITERATORS
@Override @Override
public KEY_TYPE NEXT() { public KEY_TYPE NEXT() {
if(!hasNext()) throw new IllegalStateException("End of Iterator"); if(!hasNext()) throw new NoSuchElementException();
limit--; limit--;
return iterator.NEXT(); return iterator.NEXT();
} }
@ -1054,7 +1055,7 @@ public class ITERATORS
@Override @Override
public KEY_TYPE NEXT() { public KEY_TYPE NEXT() {
if(!hasNext()) throw new IllegalStateException("End of Iterator"); if(!hasNext()) throw new NoSuchElementException();
KEY_TYPE result = iterator.NEXT(); KEY_TYPE result = iterator.NEXT();
action.accept(result); action.accept(result);
return result; return result;

View File

@ -27,7 +27,7 @@ public class LISTS
/** /**
* Empty List reference * Empty List reference
*/ */
public static final EmptyList NO_GENERIC_TYPE EMPTY = new EmptyListBRACES(); private static final EmptyList NO_GENERIC_TYPE EMPTY = new EmptyListBRACES();
/** /**
* Returns a Immutable EmptyList instance that is automatically casted. * Returns a Immutable EmptyList instance that is automatically casted.

View File

@ -1,5 +1,6 @@
package speiger.src.collections.PACKAGE.utils; package speiger.src.collections.PACKAGE.utils;
import java.util.NoSuchElementException;
#if TYPE_BOOLEAN #if TYPE_BOOLEAN
import speiger.src.collections.booleans.collections.BooleanIterator; import speiger.src.collections.booleans.collections.BooleanIterator;
import speiger.src.collections.booleans.sets.AbstractBooleanSet; import speiger.src.collections.booleans.sets.AbstractBooleanSet;
@ -34,7 +35,7 @@ public class SETS
/** /**
* Empty Set Variable * Empty Set Variable
*/ */
public static final SET NO_GENERIC_TYPE EMPTY = new EmptySetBRACES(); private static final SET NO_GENERIC_TYPE EMPTY = new EmptySetBRACES();
/** /**
* EmptySet getter * EmptySet getter
@ -216,10 +217,10 @@ public class SETS
return new ITERATOR KEY_GENERIC_TYPE() { return new ITERATOR KEY_GENERIC_TYPE() {
boolean next = true; boolean next = true;
@Override @Override
public boolean hasNext() { return next = false; } public boolean hasNext() { return next; }
@Override @Override
public KEY_TYPE NEXT() { public KEY_TYPE NEXT() {
if(!next) throw new IllegalStateException(); if(!hasNext()) throw new NoSuchElementException();
next = false; next = false;
return element; return element;
} }

View File

@ -1,6 +1,7 @@
package speiger.src.collections.PACKAGE.utils; package speiger.src.collections.PACKAGE.utils;
import java.util.Comparator; import java.util.Comparator;
import java.util.NoSuchElementException;
import java.util.Spliterator; import java.util.Spliterator;
#if PRIMITIVES #if PRIMITIVES
import java.util.Spliterator.JAVA_SPLIT_ITERATOR; import java.util.Spliterator.JAVA_SPLIT_ITERATOR;
@ -329,7 +330,11 @@ public class SPLIT_ITERATORS
} }
@Override @Override
public KEY_TYPE NEXT() { return array[index++]; } public KEY_TYPE NEXT() {
if(!hasNext()) throw new NoSuchElementException();
return array[index++];
}
@Override @Override
public boolean hasNext() { return index < fence; } public boolean hasNext() { return index < fence; }
} }

View File

@ -57,7 +57,21 @@ public class MAPS
/** /**
* Empty Map Variable * Empty Map Variable
*/ */
public static final MAP NO_KV_GENERIC_TYPE EMPTY = new EmptyMapKV_BRACES(); private static final MAP NO_KV_GENERIC_TYPE EMPTY = new EmptyMapKV_BRACES();
/**
* Empty Map getter function that autocasts to the desired Key and Value
* @Type(T)
* @ValueType(V)
* @return empty map of desired type
*/
public static GENERIC_KEY_VALUE_BRACES MAP KEY_VALUE_GENERIC_TYPE empty() {
#if TYPE_OBJECT || VALUE_OBJECT
return (MAP KEY_VALUE_GENERIC_TYPE)EMPTY;
#else
return EMPTY;
#endif
}
#endif #endif
/** /**
@ -104,19 +118,6 @@ public class MAPS
} }
#if !TYPE_BOOLEAN #if !TYPE_BOOLEAN
/**
* Empty Map getter function that autocasts to the desired Key and Value
* @Type(T)
* @ValueType(V)
* @return empty map of desired type
*/
public static GENERIC_KEY_VALUE_BRACES MAP KEY_VALUE_GENERIC_TYPE empty() {
#if TYPE_OBJECT || VALUE_OBJECT
return (MAP KEY_VALUE_GENERIC_TYPE)EMPTY;
#else
return EMPTY;
#endif
}
/** /**
* Helper function that creates a Helper wrapper to synchronize access into the map. * Helper function that creates a Helper wrapper to synchronize access into the map.