From 068cc4a4f7538467a9efea06c64179d00582c967 Mon Sep 17 00:00:00 2001 From: Speiger Date: Tue, 17 May 2022 10:45:05 +0200 Subject: [PATCH] Fixed Bugs with Java Iterators not throwing the correct exception. --- .../templates/lists/LinkedList.template | 4 +-- .../templates/queues/ArrayFIFOQueue.template | 1 + .../queues/ArrayPriorityQueue.template | 3 +- .../queues/HeapPriorityQueue.template | 3 +- .../templates/utils/Iterators.template | 21 +++++++------- .../templates/utils/Lists.template | 2 +- .../collections/templates/utils/Sets.template | 7 +++-- .../templates/utils/Splititerators.template | 7 ++++- .../templates/utils/maps/Maps.template | 29 ++++++++++--------- 9 files changed, 44 insertions(+), 33 deletions(-) diff --git a/src/builder/resources/speiger/assets/collections/templates/lists/LinkedList.template b/src/builder/resources/speiger/assets/collections/templates/lists/LinkedList.template index 7264d35f..a77f9f6c 100644 --- a/src/builder/resources/speiger/assets/collections/templates/lists/LinkedList.template +++ b/src/builder/resources/speiger/assets/collections/templates/lists/LinkedList.template @@ -1263,7 +1263,7 @@ public class LINKED_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE @Override public long estimateSize() { - return list.size - index; + return (long)list.size - (long)index; } @Override @@ -1329,7 +1329,7 @@ public class LINKED_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE @Override public long estimateSize() { - return list.size - index; + return (long)list.size - (long)index; } @Override diff --git a/src/builder/resources/speiger/assets/collections/templates/queues/ArrayFIFOQueue.template b/src/builder/resources/speiger/assets/collections/templates/queues/ArrayFIFOQueue.template index 58091ef3..14d40825 100644 --- a/src/builder/resources/speiger/assets/collections/templates/queues/ArrayFIFOQueue.template +++ b/src/builder/resources/speiger/assets/collections/templates/queues/ArrayFIFOQueue.template @@ -434,6 +434,7 @@ public class ARRAY_FIFO_QUEUE KEY_GENERIC_TYPE extends ABSTRACT_PRIORITY_QUEUE K @Override public KEY_TYPE NEXT() { + if(!hasNext()) throw new NoSuchElementException(); KEY_TYPE value = array[index]; removeIndex(index); index = ++index % array.length; diff --git a/src/builder/resources/speiger/assets/collections/templates/queues/ArrayPriorityQueue.template b/src/builder/resources/speiger/assets/collections/templates/queues/ArrayPriorityQueue.template index 537fe905..38400b0b 100644 --- a/src/builder/resources/speiger/assets/collections/templates/queues/ArrayPriorityQueue.template +++ b/src/builder/resources/speiger/assets/collections/templates/queues/ArrayPriorityQueue.template @@ -187,7 +187,7 @@ public class ARRAY_PRIORITY_QUEUE KEY_GENERIC_TYPE extends ABSTRACT_PRIORITY_QUE @Override 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){ int compare = comparator == null ? COMPAREABLE_TO_KEY(e, array[firstIndex]) : comparator.compare(e, array[firstIndex]); if(compare < 0) firstIndex = size; @@ -414,6 +414,7 @@ public class ARRAY_PRIORITY_QUEUE KEY_GENERIC_TYPE extends ABSTRACT_PRIORITY_QUE @Override public KEY_TYPE NEXT() { + if(!hasNext()) throw new NoSuchElementException(); return dequeue(); } } diff --git a/src/builder/resources/speiger/assets/collections/templates/queues/HeapPriorityQueue.template b/src/builder/resources/speiger/assets/collections/templates/queues/HeapPriorityQueue.template index 82d4f44e..806446ae 100644 --- a/src/builder/resources/speiger/assets/collections/templates/queues/HeapPriorityQueue.template +++ b/src/builder/resources/speiger/assets/collections/templates/queues/HeapPriorityQueue.template @@ -207,7 +207,7 @@ public class HEAP_PRIORITY_QUEUE KEY_GENERIC_TYPE extends ABSTRACT_PRIORITY_QUEU @Override 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; ARRAYS.shiftUp(array, size-1, comparator); } @@ -389,6 +389,7 @@ public class HEAP_PRIORITY_QUEUE KEY_GENERIC_TYPE extends ABSTRACT_PRIORITY_QUEU @Override public KEY_TYPE NEXT() { + if(!hasNext()) throw new NoSuchElementException(); return dequeue(); } } diff --git a/src/builder/resources/speiger/assets/collections/templates/utils/Iterators.template b/src/builder/resources/speiger/assets/collections/templates/utils/Iterators.template index 807298b1..7b068356 100644 --- a/src/builder/resources/speiger/assets/collections/templates/utils/Iterators.template +++ b/src/builder/resources/speiger/assets/collections/templates/utils/Iterators.template @@ -34,7 +34,7 @@ public class ITERATORS /** * 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. @@ -735,7 +735,7 @@ public class ITERATORS @Override public KEY_TYPE NEXT() { - return EMPTY_KEY_VALUE; + throw new NoSuchElementException(); } @Override @@ -745,7 +745,7 @@ public class ITERATORS @Override public KEY_TYPE PREVIOUS() { - return EMPTY_KEY_VALUE; + throw new NoSuchElementException(); } @Override @@ -787,6 +787,7 @@ public class ITERATORS @Override public KEY_TYPE NEXT() { + if(!hasNext()) throw new NoSuchElementException(); return a[from++]; } @@ -854,7 +855,7 @@ public class ITERATORS @Override public T next() { - if(!hasNext()) throw new IllegalStateException("End of Iterator"); + if(!hasNext()) throw new NoSuchElementException(); T result = last.next(); foundNext = false; return result; @@ -890,7 +891,7 @@ public class ITERATORS @Override public T next() { - if(!hasNext()) throw new IllegalStateException("End of Iterator"); + if(!hasNext()) throw new NoSuchElementException(); T result = last.next(); foundNext = false; return result; @@ -921,7 +922,7 @@ public class ITERATORS @Override public KEY_TYPE NEXT() { - if(!hasNext()) throw new IllegalStateException("End of Iterator"); + if(!hasNext()) throw new NoSuchElementException(); return sortedElements.GET_KEY(index++); } } @@ -971,7 +972,7 @@ public class ITERATORS @Override public KEY_TYPE NEXT() { - if(!hasNext()) throw new IllegalStateException("End of Iterator"); + if(!hasNext()) throw new NoSuchElementException(); foundNext = false; return lastFound; } @@ -1008,7 +1009,7 @@ public class ITERATORS @Override public KEY_TYPE NEXT() { - if(!hasNext()) throw new IllegalStateException("End of Iterator"); + if(!hasNext()) throw new NoSuchElementException(); foundNext = false; return lastFound; } @@ -1031,7 +1032,7 @@ public class ITERATORS @Override public KEY_TYPE NEXT() { - if(!hasNext()) throw new IllegalStateException("End of Iterator"); + if(!hasNext()) throw new NoSuchElementException(); limit--; return iterator.NEXT(); } @@ -1054,7 +1055,7 @@ public class ITERATORS @Override public KEY_TYPE NEXT() { - if(!hasNext()) throw new IllegalStateException("End of Iterator"); + if(!hasNext()) throw new NoSuchElementException(); KEY_TYPE result = iterator.NEXT(); action.accept(result); return result; diff --git a/src/builder/resources/speiger/assets/collections/templates/utils/Lists.template b/src/builder/resources/speiger/assets/collections/templates/utils/Lists.template index d5346624..4023ae5d 100644 --- a/src/builder/resources/speiger/assets/collections/templates/utils/Lists.template +++ b/src/builder/resources/speiger/assets/collections/templates/utils/Lists.template @@ -27,7 +27,7 @@ public class LISTS /** * 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. diff --git a/src/builder/resources/speiger/assets/collections/templates/utils/Sets.template b/src/builder/resources/speiger/assets/collections/templates/utils/Sets.template index 5eea6457..5af4654d 100644 --- a/src/builder/resources/speiger/assets/collections/templates/utils/Sets.template +++ b/src/builder/resources/speiger/assets/collections/templates/utils/Sets.template @@ -1,5 +1,6 @@ package speiger.src.collections.PACKAGE.utils; +import java.util.NoSuchElementException; #if TYPE_BOOLEAN import speiger.src.collections.booleans.collections.BooleanIterator; import speiger.src.collections.booleans.sets.AbstractBooleanSet; @@ -34,7 +35,7 @@ public class SETS /** * 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 @@ -216,10 +217,10 @@ public class SETS return new ITERATOR KEY_GENERIC_TYPE() { boolean next = true; @Override - public boolean hasNext() { return next = false; } + public boolean hasNext() { return next; } @Override public KEY_TYPE NEXT() { - if(!next) throw new IllegalStateException(); + if(!hasNext()) throw new NoSuchElementException(); next = false; return element; } diff --git a/src/builder/resources/speiger/assets/collections/templates/utils/Splititerators.template b/src/builder/resources/speiger/assets/collections/templates/utils/Splititerators.template index ed5d8254..10ce19f6 100644 --- a/src/builder/resources/speiger/assets/collections/templates/utils/Splititerators.template +++ b/src/builder/resources/speiger/assets/collections/templates/utils/Splititerators.template @@ -1,6 +1,7 @@ package speiger.src.collections.PACKAGE.utils; import java.util.Comparator; +import java.util.NoSuchElementException; import java.util.Spliterator; #if PRIMITIVES import java.util.Spliterator.JAVA_SPLIT_ITERATOR; @@ -329,7 +330,11 @@ public class SPLIT_ITERATORS } @Override - public KEY_TYPE NEXT() { return array[index++]; } + public KEY_TYPE NEXT() { + if(!hasNext()) throw new NoSuchElementException(); + return array[index++]; + } + @Override public boolean hasNext() { return index < fence; } } diff --git a/src/builder/resources/speiger/assets/collections/templates/utils/maps/Maps.template b/src/builder/resources/speiger/assets/collections/templates/utils/maps/Maps.template index 51d807f2..cc14a3b4 100644 --- a/src/builder/resources/speiger/assets/collections/templates/utils/maps/Maps.template +++ b/src/builder/resources/speiger/assets/collections/templates/utils/maps/Maps.template @@ -57,7 +57,21 @@ public class MAPS /** * 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 /** @@ -104,19 +118,6 @@ public class MAPS } #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.