diff --git a/src/builder/resources/speiger/assets/collections/templates/lists/ArrayList.template b/src/builder/resources/speiger/assets/collections/templates/lists/ArrayList.template index bf88d2f..e87e743 100644 --- a/src/builder/resources/speiger/assets/collections/templates/lists/ArrayList.template +++ b/src/builder/resources/speiger/assets/collections/templates/lists/ArrayList.template @@ -9,6 +9,13 @@ import java.util.Iterator; import java.util.Objects; #if JDK_TYPE import java.util.OPTIONAL; +#if SPLIT_ITERATOR_FEATURE && STREAM_FEATURE +#if TYPE_OBJECT +import java.util.stream.Stream; +import java.util.stream.Collector; +#endif +#endif + #endif #if TYPE_OBJECT #if IARRAY_FEATURE || TYPE_OBJECT @@ -225,6 +232,42 @@ public class ARRAY_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE return list; } +#endif +#if JDK_TYPE && SPLIT_ITERATOR_FEATURE && STREAM_FEATURE +#if TYPE_OBJECT + /** + * Creates a Collector for a ArrayList + * @Type(T) + * @return a collector + */ + public static Collector, ObjectArrayList> toList() { + return Collector.of(ObjectArrayList::new, ObjectArrayList::add, ObjectArrayList::merge); + } + + /** + * Collects a Stream to a ArrayList + * @Type(T) + * @return a list with the contents of the Stream + */ + public static ARRAY_LIST KEY_GENERIC_TYPE toList(Stream stream) { + return stream.collect(ObjectArrayList::new, ObjectArrayList::add, ObjectArrayList::merge); + } + + private ObjectArrayList merge(ObjectArrayList a) { + addAll(a); + return this; + } + +#else + /** + * Collects a Stream to a ArrayList + * @return a list with the contents of the Stream + */ + public static ARRAY_LIST toList(JAVA_STREAM stream) { + return stream.collect(ARRAY_LIST::new, ARRAY_LIST::add, ARRAY_LIST::addAll); + } + +#endif #endif /** * Appends the specified element to the end of this list. diff --git a/src/builder/resources/speiger/assets/collections/templates/lists/CopyOnWriteList.template b/src/builder/resources/speiger/assets/collections/templates/lists/CopyOnWriteList.template index 8d818ff..7017489 100644 --- a/src/builder/resources/speiger/assets/collections/templates/lists/CopyOnWriteList.template +++ b/src/builder/resources/speiger/assets/collections/templates/lists/CopyOnWriteList.template @@ -10,6 +10,12 @@ import java.util.NoSuchElementException; import java.util.Objects; #if JDK_TYPE import java.util.OPTIONAL; +#if SPLIT_ITERATOR_FEATURE && STREAM_FEATURE +#if TYPE_OBJECT +import java.util.stream.Stream; +import java.util.stream.Collector; +#endif +#endif #endif import java.util.concurrent.locks.ReentrantLock; import java.util.function.Consumer; @@ -172,7 +178,42 @@ public class COPY_ON_WRITE_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENER } #endif +#if JDK_TYPE && SPLIT_ITERATOR_FEATURE && STREAM_FEATURE +#if TYPE_OBJECT + /** + * Creates a Collector for a CopyOnWriteArrayList + * @Type(T) + * @return a collector + */ + public static Collector, CopyOnWriteObjectArrayList> toList() { + return Collector.of(CopyOnWriteObjectArrayList::new, CopyOnWriteObjectArrayList::add, CopyOnWriteObjectArrayList::merge); + } + /** + * Collects a Stream to a CopyOnWriteArrayList + * @Type(T) + * @return a list with the contents of the Stream + */ + public static CopyOnWriteObjectArrayList KEY_GENERIC_TYPE toList(Stream stream) { + return stream.collect(CopyOnWriteObjectArrayList::new, CopyOnWriteObjectArrayList::add, CopyOnWriteObjectArrayList::merge); + } + + private CopyOnWriteObjectArrayList merge(CopyOnWriteObjectArrayList a) { + addAll(a); + return this; + } + +#else + /** + * Collects a Stream to a CopyOnWriteArrayList + * @return a list with the contents of the Stream + */ + public static COPY_ON_WRITE_LIST toList(JAVA_STREAM stream) { + return stream.collect(COPY_ON_WRITE_LIST::new, COPY_ON_WRITE_LIST::add, COPY_ON_WRITE_LIST::addAll); + } + +#endif +#endif private void setArray(KEY_TYPE[] data) { this.data = data; } diff --git a/src/builder/resources/speiger/assets/collections/templates/lists/ImmutableList.template b/src/builder/resources/speiger/assets/collections/templates/lists/ImmutableList.template index a75fcfe..d7a00b1 100644 --- a/src/builder/resources/speiger/assets/collections/templates/lists/ImmutableList.template +++ b/src/builder/resources/speiger/assets/collections/templates/lists/ImmutableList.template @@ -3,12 +3,25 @@ package speiger.src.collections.PACKAGE.lists; import java.util.Arrays; #if TYPE_OBJECT import java.util.Comparator; +#else if !TYPE_BOOLEAN +import java.util.stream.JAVA_STREAM; +import java.util.stream.StreamSupport; #endif import java.util.Collection; import java.util.NoSuchElementException; import java.util.Objects; #if JDK_TYPE import java.util.OPTIONAL; +#if SPLIT_ITERATOR_FEATURE && STREAM_FEATURE +#if PRIMITIVES +import java.util.function.Supplier; +#else +import java.util.stream.Stream; +import java.util.stream.Collector; +#endif + +import speiger.src.collections.PACKAGE.utils.COLLECTIONS; +#endif #endif #if TYPE_OBJECT import java.util.function.BiFunction; @@ -23,6 +36,7 @@ import java.util.function.PREDICATE; #if !JDK_FUNCTION import java.util.function.JAVA_PREDICATE; #endif + import java.util.function.JAVA_UNARY_OPERATOR; #endif @@ -42,10 +56,6 @@ import speiger.src.collections.PACKAGE.functions.function.PREDICATE; import speiger.src.collections.PACKAGE.functions.function.UNARY_OPERATOR; import speiger.src.collections.objects.utils.ObjectArrays; import speiger.src.collections.PACKAGE.utils.ITERATORS; -#if PRIMITIVES && SPLIT_ITERATOR_FEATURE && STREAM_FEATURE -import java.util.stream.JAVA_STREAM; -import java.util.stream.StreamSupport; -#endif #if SPLIT_ITERATOR_FEATURE import speiger.src.collections.PACKAGE.collections.SPLIT_ITERATOR; import speiger.src.collections.PACKAGE.utils.SPLIT_ITERATORS; @@ -126,6 +136,42 @@ public class IMMUTABLE_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_T data = Arrays.copyOfRange(a, offset, offset+length); } +#if JDK_TYPE && SPLIT_ITERATOR_FEATURE && STREAM_FEATURE +#if TYPE_OBJECT + /** + * Creates a Collector for a ImmutableList + * @Type(T) + * @return a collector + */ + public static Collector, ImmutableObjectList> toList() { + return Collector.of(COLLECTIONS::wrapper, COLLECTION::add, ImmutableObjectList::merge, E -> new IMMUTABLE_LIST<>(E.toArray((T[])new Object[E.size()]))); + } + + /** + * Collects a Stream to a ImmutableList + * @Type(T) + * @return a list with the contents of the Stream + */ + public static ImmutableObjectList KEY_GENERIC_TYPE toList(Stream stream) { + return stream.collect(toList()); + } + + private static COLLECTION merge(COLLECTION a, COLLECTION b) { + a.addAll(b); + return a; + } + +#else + /** + * Collects a Stream to a ImmutableList + * @return a list with the contents of the Stream + */ + public static IMMUTABLE_LIST toList(JAVA_STREAM stream) { + return new IMMUTABLE_LIST(stream.collect((Supplier)COLLECTIONS::wrapper, COLLECTION::add, COLLECTION::addAll).TO_ARRAY()); + } + +#endif +#endif @Override public boolean add(KEY_TYPE e) { throw new UnsupportedOperationException(); } @Override 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 dc1134e..aa749f8 100644 --- a/src/builder/resources/speiger/assets/collections/templates/lists/LinkedList.template +++ b/src/builder/resources/speiger/assets/collections/templates/lists/LinkedList.template @@ -13,6 +13,12 @@ import java.util.Iterator; import java.util.Objects; #if JDK_TYPE import java.util.OPTIONAL; +#if SPLIT_ITERATOR_FEATURE && STREAM_FEATURE +#if TYPE_OBJECT +import java.util.stream.Stream; +import java.util.stream.Collector; +#endif +#endif #endif import java.util.NoSuchElementException; #if SPLIT_ITERATOR_FEATURE @@ -160,6 +166,42 @@ public class LINKED_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE for(int i = offset,m=offset+length;i Collector, ObjectLinkedList> toList() { + return Collector.of(ObjectLinkedList::new, ObjectLinkedList::add, ObjectLinkedList::merge); + } + + /** + * Collects a Stream to a LinkedList + * @Type(T) + * @return a list with the contents of the Stream + */ + public static ObjectLinkedList KEY_GENERIC_TYPE toList(Stream stream) { + return stream.collect(ObjectLinkedList::new, ObjectLinkedList::add, ObjectLinkedList::merge); + } + + private ObjectLinkedList merge(ObjectLinkedList a) { + addAll(a); + return this; + } + +#else + /** + * Collects a Stream to a LinkedList + * @return a list with the contents of the Stream + */ + public static LINKED_LIST toList(JAVA_STREAM stream) { + return stream.collect(LINKED_LIST::new, LINKED_LIST::add, LINKED_LIST::addAll); + } + +#endif +#endif @Override public boolean add(KEY_TYPE e) { add(size(), e); diff --git a/src/builder/resources/speiger/assets/collections/templates/maps/impl/customHash/LinkedOpenCustomHashMap.template b/src/builder/resources/speiger/assets/collections/templates/maps/impl/customHash/LinkedOpenCustomHashMap.template index 54fdcca..987ccf3 100644 --- a/src/builder/resources/speiger/assets/collections/templates/maps/impl/customHash/LinkedOpenCustomHashMap.template +++ b/src/builder/resources/speiger/assets/collections/templates/maps/impl/customHash/LinkedOpenCustomHashMap.template @@ -238,13 +238,13 @@ public class LINKED_CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends CUSTOM_HASH_M if(containsNull) { VALUE_TYPE lastValue = values[nullIndex]; values[nullIndex] = value; - moveToFirstIndex(nullIndex); + moveToFirstIndex(nullIndex, false); return lastValue; } values[nullIndex] = value; containsNull = true; onNodeAdded(nullIndex); - moveToFirstIndex(nullIndex); + moveToFirstIndex(nullIndex, true); } else { int pos = HashUtil.mix(strategy.hashCode(key)) & mask; @@ -252,7 +252,7 @@ public class LINKED_CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends CUSTOM_HASH_M if(strategy.equals(keys[pos], key)) { VALUE_TYPE lastValue = values[pos]; values[pos] = value; - moveToFirstIndex(pos); + moveToFirstIndex(pos, false); return lastValue; } pos = ++pos & mask; @@ -260,7 +260,7 @@ public class LINKED_CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends CUSTOM_HASH_M keys[pos] = key; values[pos] = value; onNodeAdded(pos); - moveToFirstIndex(pos); + moveToFirstIndex(pos, true); } if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor)); return getDefaultReturnValue(); @@ -272,13 +272,13 @@ public class LINKED_CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends CUSTOM_HASH_M if(containsNull) { VALUE_TYPE lastValue = values[nullIndex]; values[nullIndex] = value; - moveToLastIndex(nullIndex); + moveToLastIndex(nullIndex, false); return lastValue; } values[nullIndex] = value; containsNull = true; onNodeAdded(nullIndex); - moveToLastIndex(nullIndex); + moveToLastIndex(nullIndex, true); } else { int pos = HashUtil.mix(strategy.hashCode(key)) & mask; @@ -286,7 +286,7 @@ public class LINKED_CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends CUSTOM_HASH_M if(strategy.equals(keys[pos], key)) { VALUE_TYPE lastValue = values[pos]; values[pos] = value; - moveToLastIndex(pos); + moveToLastIndex(pos, false); return lastValue; } pos = ++pos & mask; @@ -294,7 +294,7 @@ public class LINKED_CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends CUSTOM_HASH_M keys[pos] = key; values[pos] = value; onNodeAdded(pos); - moveToLastIndex(pos); + moveToLastIndex(pos, true); } if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor)); return getDefaultReturnValue(); @@ -307,7 +307,7 @@ public class LINKED_CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends CUSTOM_HASH_M values[nullIndex] = value; containsNull = true; onNodeAdded(nullIndex); - moveToFirstIndex(nullIndex); + moveToFirstIndex(nullIndex, true); } else { int pos = HashUtil.mix(strategy.hashCode(key)) & mask; @@ -318,7 +318,7 @@ public class LINKED_CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends CUSTOM_HASH_M keys[pos] = key; values[pos] = value; onNodeAdded(pos); - moveToFirstIndex(pos); + moveToFirstIndex(pos, true); } if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor)); return getDefaultReturnValue(); @@ -331,7 +331,7 @@ public class LINKED_CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends CUSTOM_HASH_M values[nullIndex] = value; containsNull = true; onNodeAdded(nullIndex); - moveToLastIndex(nullIndex); + moveToLastIndex(nullIndex, true); } else { int pos = HashUtil.mix(strategy.hashCode(key)) & mask; @@ -342,7 +342,7 @@ public class LINKED_CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends CUSTOM_HASH_M keys[pos] = key; values[pos] = value; onNodeAdded(pos); - moveToLastIndex(pos); + moveToLastIndex(pos, true); } if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor)); return getDefaultReturnValue(); @@ -353,7 +353,7 @@ public class LINKED_CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends CUSTOM_HASH_M if(isEmpty() || strategy.equals(FIRST_ENTRY_KEY(), key)) return false; if(strategy.equals(key, EMPTY_KEY_VALUE)) { if(containsNull) { - moveToFirstIndex(nullIndex); + moveToFirstIndex(nullIndex, false); return true; } } @@ -361,7 +361,7 @@ public class LINKED_CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends CUSTOM_HASH_M int pos = HashUtil.mix(strategy.hashCode(key)) & mask; while(!strategy.equals(keys[pos], EMPTY_KEY_VALUE)) { if(strategy.equals(keys[pos], key)) { - moveToFirstIndex(pos); + moveToFirstIndex(pos, false); return true; } pos = ++pos & mask; @@ -375,7 +375,7 @@ public class LINKED_CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends CUSTOM_HASH_M if(isEmpty() || strategy.equals(LAST_ENTRY_KEY(), key)) return false; if(strategy.equals(key, EMPTY_KEY_VALUE)) { if(containsNull) { - moveToLastIndex(nullIndex); + moveToLastIndex(nullIndex, false); return true; } } @@ -383,7 +383,7 @@ public class LINKED_CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends CUSTOM_HASH_M int pos = HashUtil.mix(strategy.hashCode(key)) & mask; while(!strategy.equals(keys[pos], EMPTY_KEY_VALUE)) { if(strategy.equals(keys[pos], key)) { - moveToLastIndex(pos); + moveToLastIndex(pos, false); return true; } pos = ++pos & mask; @@ -396,7 +396,7 @@ public class LINKED_CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends CUSTOM_HASH_M public VALUE_TYPE getAndMoveToFirst(KEY_TYPE key) { int index = findIndex(key); if(index < 0) return getDefaultReturnValue(); - moveToFirstIndex(index); + moveToFirstIndex(index, false); return values[index]; } @@ -404,7 +404,7 @@ public class LINKED_CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends CUSTOM_HASH_M public VALUE_TYPE getAndMoveToLast(KEY_TYPE key) { int index = findIndex(key); if(index < 0) return getDefaultReturnValue(); - moveToLastIndex(index); + moveToLastIndex(index, false); return values[index]; } @@ -580,8 +580,8 @@ public class LINKED_CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends CUSTOM_HASH_M containsNull = false; } - protected void moveToFirstIndex(int startPos) { - if(size == 1 || firstIndex == startPos) return; + protected void moveToFirstIndex(int startPos, boolean adding) { + if(size == (adding ? 0 : 1) || firstIndex == startPos) return; if(lastIndex == startPos) { lastIndex = (int)(links[startPos] >>> 32); links[lastIndex] |= 0xFFFFFFFFL; @@ -598,8 +598,8 @@ public class LINKED_CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends CUSTOM_HASH_M firstIndex = startPos; } - protected void moveToLastIndex(int startPos) { - if(size == 1 || lastIndex == startPos) return; + protected void moveToLastIndex(int startPos, boolean adding) { + if(size == (adding ? 0 : 1) || lastIndex == startPos) return; if(firstIndex == startPos) { firstIndex = (int)links[startPos]; links[lastIndex] |= 0xFFFFFFFF00000000L; diff --git a/src/builder/resources/speiger/assets/collections/templates/maps/impl/hash/LinkedOpenHashMap.template b/src/builder/resources/speiger/assets/collections/templates/maps/impl/hash/LinkedOpenHashMap.template index 8cec210..bf0681d 100644 --- a/src/builder/resources/speiger/assets/collections/templates/maps/impl/hash/LinkedOpenHashMap.template +++ b/src/builder/resources/speiger/assets/collections/templates/maps/impl/hash/LinkedOpenHashMap.template @@ -215,13 +215,13 @@ public class LINKED_HASH_MAP KEY_VALUE_GENERIC_TYPE extends HASH_MAP KEY_VALUE_G if(containsNull) { VALUE_TYPE lastValue = values[nullIndex]; values[nullIndex] = value; - moveToFirstIndex(nullIndex); + moveToFirstIndex(nullIndex, false); return lastValue; } values[nullIndex] = value; containsNull = true; onNodeAdded(nullIndex); - moveToFirstIndex(nullIndex); + moveToFirstIndex(nullIndex, true); } else { int pos = HashUtil.mix(KEY_TO_HASH(key)) & mask; @@ -229,7 +229,7 @@ public class LINKED_HASH_MAP KEY_VALUE_GENERIC_TYPE extends HASH_MAP KEY_VALUE_G if(KEY_EQUALS(keys[pos], key)) { VALUE_TYPE lastValue = values[pos]; values[pos] = value; - moveToFirstIndex(pos); + moveToFirstIndex(pos, false); return lastValue; } pos = ++pos & mask; @@ -237,7 +237,7 @@ public class LINKED_HASH_MAP KEY_VALUE_GENERIC_TYPE extends HASH_MAP KEY_VALUE_G keys[pos] = key; values[pos] = value; onNodeAdded(pos); - moveToFirstIndex(pos); + moveToFirstIndex(pos, true); } if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor)); return getDefaultReturnValue(); @@ -249,13 +249,13 @@ public class LINKED_HASH_MAP KEY_VALUE_GENERIC_TYPE extends HASH_MAP KEY_VALUE_G if(containsNull) { VALUE_TYPE lastValue = values[nullIndex]; values[nullIndex] = value; - moveToLastIndex(nullIndex); + moveToLastIndex(nullIndex, false); return lastValue; } values[nullIndex] = value; containsNull = true; onNodeAdded(nullIndex); - moveToLastIndex(nullIndex); + moveToLastIndex(nullIndex, true); } else { int pos = HashUtil.mix(KEY_TO_HASH(key)) & mask; @@ -263,7 +263,7 @@ public class LINKED_HASH_MAP KEY_VALUE_GENERIC_TYPE extends HASH_MAP KEY_VALUE_G if(KEY_EQUALS(keys[pos], key)) { VALUE_TYPE lastValue = values[pos]; values[pos] = value; - moveToLastIndex(pos); + moveToLastIndex(pos, false); return lastValue; } pos = ++pos & mask; @@ -271,7 +271,7 @@ public class LINKED_HASH_MAP KEY_VALUE_GENERIC_TYPE extends HASH_MAP KEY_VALUE_G keys[pos] = key; values[pos] = value; onNodeAdded(pos); - moveToLastIndex(pos); + moveToLastIndex(pos, true); } if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor)); return getDefaultReturnValue(); @@ -284,7 +284,7 @@ public class LINKED_HASH_MAP KEY_VALUE_GENERIC_TYPE extends HASH_MAP KEY_VALUE_G values[nullIndex] = value; containsNull = true; onNodeAdded(nullIndex); - moveToFirstIndex(nullIndex); + moveToFirstIndex(nullIndex, true); } else { int pos = HashUtil.mix(KEY_TO_HASH(key)) & mask; @@ -295,7 +295,7 @@ public class LINKED_HASH_MAP KEY_VALUE_GENERIC_TYPE extends HASH_MAP KEY_VALUE_G keys[pos] = key; values[pos] = value; onNodeAdded(pos); - moveToFirstIndex(pos); + moveToFirstIndex(pos, true); } if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor)); return getDefaultReturnValue(); @@ -308,7 +308,7 @@ public class LINKED_HASH_MAP KEY_VALUE_GENERIC_TYPE extends HASH_MAP KEY_VALUE_G values[nullIndex] = value; containsNull = true; onNodeAdded(nullIndex); - moveToLastIndex(nullIndex); + moveToLastIndex(nullIndex, true); } else { int pos = HashUtil.mix(KEY_TO_HASH(key)) & mask; @@ -319,7 +319,7 @@ public class LINKED_HASH_MAP KEY_VALUE_GENERIC_TYPE extends HASH_MAP KEY_VALUE_G keys[pos] = key; values[pos] = value; onNodeAdded(pos); - moveToLastIndex(pos); + moveToLastIndex(pos, true); } if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor)); return getDefaultReturnValue(); @@ -330,7 +330,7 @@ public class LINKED_HASH_MAP KEY_VALUE_GENERIC_TYPE extends HASH_MAP KEY_VALUE_G if(isEmpty() || KEY_EQUALS(FIRST_ENTRY_KEY(), key)) return false; if(KEY_EQUALS_NULL(key)) { if(containsNull) { - moveToFirstIndex(nullIndex); + moveToFirstIndex(nullIndex, false); return true; } } @@ -338,7 +338,7 @@ public class LINKED_HASH_MAP KEY_VALUE_GENERIC_TYPE extends HASH_MAP KEY_VALUE_G int pos = HashUtil.mix(KEY_TO_HASH(key)) & mask; while(KEY_EQUALS_NOT_NULL(keys[pos])) { if(KEY_EQUALS(keys[pos], key)) { - moveToFirstIndex(pos); + moveToFirstIndex(pos, false); return true; } pos = ++pos & mask; @@ -352,7 +352,7 @@ public class LINKED_HASH_MAP KEY_VALUE_GENERIC_TYPE extends HASH_MAP KEY_VALUE_G if(isEmpty() || KEY_EQUALS(LAST_ENTRY_KEY(), key)) return false; if(KEY_EQUALS_NULL(key)) { if(containsNull) { - moveToLastIndex(nullIndex); + moveToLastIndex(nullIndex, false); return true; } } @@ -360,7 +360,7 @@ public class LINKED_HASH_MAP KEY_VALUE_GENERIC_TYPE extends HASH_MAP KEY_VALUE_G int pos = HashUtil.mix(KEY_TO_HASH(key)) & mask; while(KEY_EQUALS_NOT_NULL(keys[pos])) { if(KEY_EQUALS(keys[pos], key)) { - moveToLastIndex(pos); + moveToLastIndex(pos, false); return true; } pos = ++pos & mask; @@ -373,7 +373,7 @@ public class LINKED_HASH_MAP KEY_VALUE_GENERIC_TYPE extends HASH_MAP KEY_VALUE_G public VALUE_TYPE getAndMoveToFirst(KEY_TYPE key) { int index = findIndex(key); if(index < 0) return getDefaultReturnValue(); - moveToFirstIndex(index); + moveToFirstIndex(index, false); return values[index]; } @@ -381,7 +381,7 @@ public class LINKED_HASH_MAP KEY_VALUE_GENERIC_TYPE extends HASH_MAP KEY_VALUE_G public VALUE_TYPE getAndMoveToLast(KEY_TYPE key) { int index = findIndex(key); if(index < 0) return getDefaultReturnValue(); - moveToLastIndex(index); + moveToLastIndex(index, false); return values[index]; } @@ -584,8 +584,8 @@ public class LINKED_HASH_MAP KEY_VALUE_GENERIC_TYPE extends HASH_MAP KEY_VALUE_G containsNull = false; } - protected void moveToFirstIndex(int startPos) { - if(size == 1 || firstIndex == startPos) return; + protected void moveToFirstIndex(int startPos, boolean adding) { + if(size == (adding ? 0 : 1) || firstIndex == startPos) return; if(lastIndex == startPos) { lastIndex = (int)(links[startPos] >>> 32); links[lastIndex] |= 0xFFFFFFFFL; @@ -602,8 +602,8 @@ public class LINKED_HASH_MAP KEY_VALUE_GENERIC_TYPE extends HASH_MAP KEY_VALUE_G firstIndex = startPos; } - protected void moveToLastIndex(int startPos) { - if(size == 1 || lastIndex == startPos) return; + protected void moveToLastIndex(int startPos, boolean adding) { + if(size == (adding ? 0 : 1) || lastIndex == startPos) return; if(firstIndex == startPos) { firstIndex = (int)links[startPos]; links[lastIndex] |= 0xFFFFFFFF00000000L; 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 b60eed8..970a3cf 100644 --- a/src/builder/resources/speiger/assets/collections/templates/queues/ArrayFIFOQueue.template +++ b/src/builder/resources/speiger/assets/collections/templates/queues/ArrayFIFOQueue.template @@ -9,6 +9,14 @@ import java.util.function.BiFunction; import java.util.Objects; #if JDK_TYPE import java.util.OPTIONAL; +#if SPLIT_ITERATOR_FEATURE && STREAM_FEATURE +#if !TYPE_OBJECT +import java.util.stream.JAVA_STREAM; +#else +import java.util.stream.Stream; +import java.util.stream.Collector; +#endif +#endif #endif import java.util.NoSuchElementException; #if JDK_FUNCTION @@ -105,6 +113,47 @@ public class ARRAY_FIFO_QUEUE KEY_GENERIC_TYPE extends ABSTRACT_PRIORITY_QUEUE K this(MIN_CAPACITY); } +#if JDK_TYPE && SPLIT_ITERATOR_FEATURE && STREAM_FEATURE +#if TYPE_OBJECT + /** + * Creates a Collector for a ArrayFIFOQueue + * @Type(T) + * @return a collector + */ + public static Collector, ARRAY_FIFO_QUEUE> toQueue() { + return Collector.of(ARRAY_FIFO_QUEUE::new, ARRAY_FIFO_QUEUE::enqueue, ARRAY_FIFO_QUEUE::merge); + } + + /** + * Collects a Stream to a ArrayFIFOQueue + * @Type(T) + * @return a queue with the contents of the Stream + */ + public static ARRAY_FIFO_QUEUE KEY_GENERIC_TYPE toQueue(Stream stream) { + return stream.collect(ARRAY_FIFO_QUEUE::new, ARRAY_FIFO_QUEUE::enqueue, ARRAY_FIFO_QUEUE::merge); + } + + private ARRAY_FIFO_QUEUE merge(ARRAY_FIFO_QUEUE a) { + enqueueAll(a.toArray((T[])new Object[a.size()])); + return this; + } + +#else + /** + * Collects a Stream to a ArrayFIFOQueue + * @return a queue with the contents of the Stream + */ + public static ARRAY_FIFO_QUEUE toQueue(JAVA_STREAM stream) { + return stream.collect(ARRAY_FIFO_QUEUE::new, ARRAY_FIFO_QUEUE::enqueue, ARRAY_FIFO_QUEUE::merge); + } + + private ARRAY_FIFO_QUEUE merge(ARRAY_FIFO_QUEUE a) { + enqueueAll(a.TO_ARRAY()); + return this; + } + +#endif +#endif @Override public ITERATOR KEY_GENERIC_TYPE iterator() { return new Iter(); 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 620398b..860024b 100644 --- a/src/builder/resources/speiger/assets/collections/templates/queues/ArrayPriorityQueue.template +++ b/src/builder/resources/speiger/assets/collections/templates/queues/ArrayPriorityQueue.template @@ -10,6 +10,14 @@ import java.util.function.BiFunction; import java.util.Objects; #if JDK_TYPE import java.util.OPTIONAL; +#if SPLIT_ITERATOR_FEATURE && STREAM_FEATURE +#if !TYPE_OBJECT +import java.util.stream.JAVA_STREAM; +#else +import java.util.stream.Stream; +import java.util.stream.Collector; +#endif +#endif #endif #if JDK_FUNCTION import java.util.function.PREDICATE; @@ -196,7 +204,47 @@ public class ARRAY_PRIORITY_QUEUE KEY_GENERIC_TYPE extends ABSTRACT_PRIORITY_QUE queue.size = size; return queue; } +#if JDK_TYPE && SPLIT_ITERATOR_FEATURE && STREAM_FEATURE +#if TYPE_OBJECT + /** + * Creates a Collector for a ArrayPriorityQueue + * @Type(T) + * @return a collector + */ + public static Collector, ARRAY_PRIORITY_QUEUE> toQueue() { + return Collector.of(ARRAY_PRIORITY_QUEUE::new, ARRAY_PRIORITY_QUEUE::enqueue, ARRAY_PRIORITY_QUEUE::merge); + } + /** + * Collects a Stream to a ArrayPriorityQueue + * @Type(T) + * @return a queue with the contents of the Stream + */ + public static ARRAY_PRIORITY_QUEUE KEY_GENERIC_TYPE toQueue(Stream stream) { + return stream.collect(ARRAY_PRIORITY_QUEUE::new, ARRAY_PRIORITY_QUEUE::enqueue, ARRAY_PRIORITY_QUEUE::merge); + } + + private ARRAY_PRIORITY_QUEUE merge(ARRAY_PRIORITY_QUEUE a) { + enqueueAll(a.toArray((T[])new Object[a.size()])); + return this; + } + +#else + /** + * Collects a Stream to a ArrayPriorityQueue + * @return a queue with the contents of the Stream + */ + public static ARRAY_PRIORITY_QUEUE toQueue(JAVA_STREAM stream) { + return stream.collect(ARRAY_PRIORITY_QUEUE::new, ARRAY_PRIORITY_QUEUE::enqueue, ARRAY_PRIORITY_QUEUE::merge); + } + + private ARRAY_PRIORITY_QUEUE merge(ARRAY_PRIORITY_QUEUE a) { + enqueueAll(a.TO_ARRAY()); + return this; + } + +#endif +#endif @Override public void enqueue(KEY_TYPE e) { 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)); 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 0dcb8f5..69423c1 100644 --- a/src/builder/resources/speiger/assets/collections/templates/queues/HeapPriorityQueue.template +++ b/src/builder/resources/speiger/assets/collections/templates/queues/HeapPriorityQueue.template @@ -10,6 +10,14 @@ import java.util.function.BiFunction; import java.util.Objects; #if JDK_TYPE import java.util.OPTIONAL; +#if SPLIT_ITERATOR_FEATURE && STREAM_FEATURE +#if !TYPE_OBJECT +import java.util.stream.JAVA_STREAM; +#else +import java.util.stream.Stream; +import java.util.stream.Collector; +#endif +#endif #endif #if JDK_FUNCTION import java.util.function.PREDICATE; @@ -199,6 +207,47 @@ public class HEAP_PRIORITY_QUEUE KEY_GENERIC_TYPE extends ABSTRACT_PRIORITY_QUEU return queue; } +#if JDK_TYPE && SPLIT_ITERATOR_FEATURE && STREAM_FEATURE +#if TYPE_OBJECT + /** + * Creates a Collector for a ArrayFIFOQueue + * @Type(T) + * @return a collector + */ + public static Collector, HEAP_PRIORITY_QUEUE> toQueue() { + return Collector.of(HEAP_PRIORITY_QUEUE::new, HEAP_PRIORITY_QUEUE::enqueue, HEAP_PRIORITY_QUEUE::merge); + } + + /** + * Collects a Stream to a ArrayFIFOQueue + * @Type(T) + * @return a queue with the contents of the Stream + */ + public static HEAP_PRIORITY_QUEUE KEY_GENERIC_TYPE toQueue(Stream stream) { + return stream.collect(HEAP_PRIORITY_QUEUE::new, HEAP_PRIORITY_QUEUE::enqueue, HEAP_PRIORITY_QUEUE::merge); + } + + private HEAP_PRIORITY_QUEUE merge(HEAP_PRIORITY_QUEUE a) { + enqueueAll(a.toArray((T[])new Object[a.size()])); + return this; + } + +#else + /** + * Collects a Stream to a ArrayFIFOQueue + * @return a queue with the contents of the Stream + */ + public static HEAP_PRIORITY_QUEUE toQueue(JAVA_STREAM stream) { + return stream.collect(HEAP_PRIORITY_QUEUE::new, HEAP_PRIORITY_QUEUE::enqueue, HEAP_PRIORITY_QUEUE::merge); + } + + private HEAP_PRIORITY_QUEUE merge(HEAP_PRIORITY_QUEUE a) { + enqueueAll(a.TO_ARRAY()); + return this; + } + +#endif +#endif @Override public int size() { return size; diff --git a/src/builder/resources/speiger/assets/collections/templates/sets/AVLTreeSet.template b/src/builder/resources/speiger/assets/collections/templates/sets/AVLTreeSet.template index b83198f..1fbefd8 100644 --- a/src/builder/resources/speiger/assets/collections/templates/sets/AVLTreeSet.template +++ b/src/builder/resources/speiger/assets/collections/templates/sets/AVLTreeSet.template @@ -12,6 +12,12 @@ import java.util.NoSuchElementException; import java.util.Objects; #if JDK_TYPE import java.util.OPTIONAL; +#if !TYPE_OBJECT +import java.util.stream.JAVA_STREAM; +#else +import java.util.stream.Stream; +import java.util.stream.Collector; +#endif #endif #if JDK_FUNCTION import java.util.function.PREDICATE; @@ -214,6 +220,42 @@ public class AVL_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE while(iterator.hasNext()) add(iterator.NEXT()); } +#if JDK_TYPE && SPLIT_ITERATOR_FEATURE && STREAM_FEATURE +#if TYPE_OBJECT + /** + * Creates a Collector for a AVLTreeSet + * @Type(T) + * @return a collector + */ + public static Collector, AVL_TREE_SET> toSet() { + return Collector.of(AVL_TREE_SET::new, AVL_TREE_SET::add, AVL_TREE_SET::merge); + } + + /** + * Collects a Stream to a AVLTreeSet + * @Type(T) + * @return a set with the contents of the Stream + */ + public static AVL_TREE_SET KEY_GENERIC_TYPE toSet(Stream stream) { + return stream.collect(AVL_TREE_SET::new, AVL_TREE_SET::add, AVL_TREE_SET::merge); + } + + private AVL_TREE_SET merge(AVL_TREE_SET a) { + addAll(a); + return this; + } + +#else + /** + * Collects a Stream to a AVLTreeSet + * @return a set with the contents of the Stream + */ + public static AVL_TREE_SET toSet(JAVA_STREAM stream) { + return stream.collect(AVL_TREE_SET::new, AVL_TREE_SET::add, AVL_TREE_SET::addAll); + } + +#endif +#endif #if !TYPE_OBJECT @Override public void setDefaultMaxValue(KEY_TYPE value) { defaultMaxNotFound = value; } diff --git a/src/builder/resources/speiger/assets/collections/templates/sets/ArraySet.template b/src/builder/resources/speiger/assets/collections/templates/sets/ArraySet.template index 74558f6..9aacaca 100644 --- a/src/builder/resources/speiger/assets/collections/templates/sets/ArraySet.template +++ b/src/builder/resources/speiger/assets/collections/templates/sets/ArraySet.template @@ -10,6 +10,12 @@ import java.util.NoSuchElementException; import java.util.Objects; #if JDK_TYPE import java.util.OPTIONAL; +#if !TYPE_OBJECT +import java.util.stream.JAVA_STREAM; +#else +import java.util.stream.Stream; +import java.util.stream.Collector; +#endif #endif import java.util.Set; #if JDK_FUNCTION @@ -74,7 +80,7 @@ public class ARRAY_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE im * @param array the array that should be used for set. */ public ARRAY_SET(KEY_TYPE[] array) { - this(array, array.length); + this(array, 0, array.length); } /** @@ -88,6 +94,18 @@ public class ARRAY_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE im addAll(array, length); } + /** + * Constructur using initial Array + * @param array the array that should be used for set. + * @param offset the starting offset of where the array should be copied from + * @param length the amount of elements present within the array + * @throws NegativeArraySizeException if the length is negative + */ + public ARRAY_SET(KEY_TYPE[] array, int offset, int length) { + this(length); + addAll(array, offset, length); + } + /** * A Helper constructor that allows to create a Set with exactly the same values as the provided collection. * @param c the elements that should be added to the set. @@ -131,6 +149,42 @@ public class ARRAY_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE im for(ITERATOR KEY_GENERIC_TYPE iter = s.iterator();iter.hasNext();data[size++] = iter.NEXT()); } +#if JDK_TYPE && SPLIT_ITERATOR_FEATURE && STREAM_FEATURE +#if TYPE_OBJECT + /** + * Creates a Collector for a ArraySet + * @Type(T) + * @return a collector + */ + public static Collector, ARRAY_SET> toList() { + return Collector.of(ARRAY_SET::new, ARRAY_SET::add, ARRAY_SET::merge); + } + + /** + * Collects a Stream to a ArraySet + * @Type(T) + * @return a set with the contents of the Stream + */ + public static ARRAY_SET KEY_GENERIC_TYPE toList(Stream stream) { + return stream.collect(ARRAY_SET::new, ARRAY_SET::add, ARRAY_SET::merge); + } + + private ARRAY_SET merge(ARRAY_SET a) { + addAll(a); + return this; + } + +#else + /** + * Collects a Stream to a ArraySet + * @return a set with the contents of the Stream + */ + public static ARRAY_SET toList(JAVA_STREAM stream) { + return stream.collect(ARRAY_SET::new, ARRAY_SET::add, ARRAY_SET::addAll); + } + +#endif +#endif @Override public boolean add(KEY_TYPE o) { int index = findIndex(o); diff --git a/src/builder/resources/speiger/assets/collections/templates/sets/LinkedOpenCustomHashSet.template b/src/builder/resources/speiger/assets/collections/templates/sets/LinkedOpenCustomHashSet.template index de92850..4f1bbc0 100644 --- a/src/builder/resources/speiger/assets/collections/templates/sets/LinkedOpenCustomHashSet.template +++ b/src/builder/resources/speiger/assets/collections/templates/sets/LinkedOpenCustomHashSet.template @@ -11,6 +11,12 @@ import java.util.Iterator; import java.util.NoSuchElementException; #if JDK_TYPE import java.util.OPTIONAL; +#if !TYPE_OBJECT +import java.util.stream.JAVA_STREAM; +#else +import java.util.stream.Stream; +import java.util.stream.Collector; +#endif #endif #if JDK_FUNCTION import java.util.function.PREDICATE; @@ -240,6 +246,42 @@ public class LINKED_CUSTOM_HASH_SET KEY_GENERIC_TYPE extends CUSTOM_HASH_SET KEY while(iterator.hasNext()) add(iterator.NEXT()); } +#endif +#if JDK_TYPE && SPLIT_ITERATOR_FEATURE && STREAM_FEATURE +#if TYPE_OBJECT + /** + * Creates a Collector for a LinkedCustomHashSet + * @Type(T) + * @return a collector + */ + public static Collector, LINKED_CUSTOM_HASH_SET> toLinkedSet(STRATEGY KEY_SUPER_GENERIC_TYPE strategy) { + return Collector.of(() -> new LINKED_CUSTOM_HASH_SET<>(strategy), LINKED_CUSTOM_HASH_SET::add, LINKED_CUSTOM_HASH_SET::merge); + } + + /** + * Collects a Stream to a LinkedCustomHashSet + * @Type(T) + * @return a set with the contents of the Stream + */ + public static LINKED_CUSTOM_HASH_SET KEY_GENERIC_TYPE toLinkedSet(Stream stream, STRATEGY KEY_SUPER_GENERIC_TYPE strategy) { + return stream.collect(() -> new LINKED_CUSTOM_HASH_SET<>(strategy), LINKED_CUSTOM_HASH_SET::add, LINKED_CUSTOM_HASH_SET::merge); + } + + private LINKED_CUSTOM_HASH_SET merge(LINKED_CUSTOM_HASH_SET a) { + addAll(a); + return this; + } + +#else + /** + * Collects a Stream to a LinkedCustomHashSet + * @return a set with the contents of the Stream + */ + public static LINKED_CUSTOM_HASH_SET toList(JAVA_STREAM stream, STRATEGY KEY_SUPER_GENERIC_TYPE strategy) { + return stream.collect(() -> new LINKED_CUSTOM_HASH_SET(strategy), LINKED_CUSTOM_HASH_SET::add, LINKED_CUSTOM_HASH_SET::addAll); + } + +#endif #endif @Override public void addFirst(KEY_TYPE o) { diff --git a/src/builder/resources/speiger/assets/collections/templates/sets/LinkedOpenHashSet.template b/src/builder/resources/speiger/assets/collections/templates/sets/LinkedOpenHashSet.template index 81f4244..c63bec4 100644 --- a/src/builder/resources/speiger/assets/collections/templates/sets/LinkedOpenHashSet.template +++ b/src/builder/resources/speiger/assets/collections/templates/sets/LinkedOpenHashSet.template @@ -7,6 +7,12 @@ import java.util.NoSuchElementException; import java.util.Objects; #if JDK_TYPE import java.util.OPTIONAL; +#if !TYPE_OBJECT +import java.util.stream.JAVA_STREAM; +#else +import java.util.stream.Stream; +import java.util.stream.Collector; +#endif #endif #if TYPE_OBJECT import java.util.function.Consumer; @@ -211,6 +217,42 @@ public class LINKED_HASH_SET KEY_GENERIC_TYPE extends HASH_SET KEY_GENERIC_TYPE while(iterator.hasNext()) add(iterator.NEXT()); } +#endif +#if JDK_TYPE && SPLIT_ITERATOR_FEATURE && STREAM_FEATURE +#if TYPE_OBJECT + /** + * Creates a Collector for a LinkedHashSet + * @Type(T) + * @return a collector + */ + public static Collector, LINKED_HASH_SET> toLinkedSet() { + return Collector.of(LINKED_HASH_SET::new, LINKED_HASH_SET::add, LINKED_HASH_SET::merge); + } + + /** + * Collects a Stream to a LinkedHashSet + * @Type(T) + * @return a set with the contents of the Stream + */ + public static LINKED_HASH_SET KEY_GENERIC_TYPE toLinkedSet(Stream stream) { + return stream.collect(LINKED_HASH_SET::new, LINKED_HASH_SET::add, LINKED_HASH_SET::merge); + } + + private LINKED_HASH_SET merge(LINKED_HASH_SET a) { + addAll(a); + return this; + } + +#else + /** + * Collects a Stream to a LinkedHashSet + * @return a set with the contents of the Stream + */ + public static LINKED_HASH_SET toLinkedSet(JAVA_STREAM stream) { + return stream.collect(LINKED_HASH_SET::new, LINKED_HASH_SET::add, LINKED_HASH_SET::addAll); + } + +#endif #endif @Override public void addFirst(KEY_TYPE o) { diff --git a/src/builder/resources/speiger/assets/collections/templates/sets/OpenCustomHashSet.template b/src/builder/resources/speiger/assets/collections/templates/sets/OpenCustomHashSet.template index b9339f3..95c7656 100644 --- a/src/builder/resources/speiger/assets/collections/templates/sets/OpenCustomHashSet.template +++ b/src/builder/resources/speiger/assets/collections/templates/sets/OpenCustomHashSet.template @@ -8,6 +8,12 @@ import java.util.NoSuchElementException; import java.util.Objects; #if JDK_TYPE import java.util.OPTIONAL; +#if !TYPE_OBJECT +import java.util.stream.JAVA_STREAM; +#else +import java.util.stream.Stream; +import java.util.stream.Collector; +#endif #endif #if TYPE_OBJECT import java.util.function.Consumer; @@ -257,6 +263,42 @@ public class CUSTOM_HASH_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_T while(iterator.hasNext()) add(iterator.NEXT()); } +#endif +#if JDK_TYPE && SPLIT_ITERATOR_FEATURE && STREAM_FEATURE +#if TYPE_OBJECT + /** + * Creates a Collector for a CustomHashSet + * @Type(T) + * @return a collector + */ + public static Collector, CUSTOM_HASH_SET> toSet(STRATEGY KEY_SUPER_GENERIC_TYPE strategy) { + return Collector.of(() -> new CUSTOM_HASH_SET<>(strategy), CUSTOM_HASH_SET::add, CUSTOM_HASH_SET::merge); + } + + /** + * Collects a Stream to a CustomHashSet + * @Type(T) + * @return a set with the contents of the Stream + */ + public static CUSTOM_HASH_SET KEY_GENERIC_TYPE toSet(Stream stream, STRATEGY KEY_SUPER_GENERIC_TYPE strategy) { + return stream.collect(() -> new CUSTOM_HASH_SET<>(strategy), CUSTOM_HASH_SET::add, CUSTOM_HASH_SET::merge); + } + + private CUSTOM_HASH_SET merge(CUSTOM_HASH_SET a) { + addAll(a); + return this; + } + +#else + /** + * Collects a Stream to a CustomHashSet + * @return a set with the contents of the Stream + */ + public static CUSTOM_HASH_SET toSet(JAVA_STREAM stream, STRATEGY KEY_SUPER_GENERIC_TYPE strategy) { + return stream.collect(() -> new CUSTOM_HASH_SET(strategy), CUSTOM_HASH_SET::add, CUSTOM_HASH_SET::addAll); + } + +#endif #endif /** * Helper getter function to get the current strategy diff --git a/src/builder/resources/speiger/assets/collections/templates/sets/OpenHashSet.template b/src/builder/resources/speiger/assets/collections/templates/sets/OpenHashSet.template index 0756904..e83b1cc 100644 --- a/src/builder/resources/speiger/assets/collections/templates/sets/OpenHashSet.template +++ b/src/builder/resources/speiger/assets/collections/templates/sets/OpenHashSet.template @@ -8,6 +8,12 @@ import java.util.NoSuchElementException; import java.util.Objects; #if JDK_TYPE import java.util.OPTIONAL; +#if !TYPE_OBJECT +import java.util.stream.JAVA_STREAM; +#else +import java.util.stream.Stream; +import java.util.stream.Collector; +#endif #endif #if TYPE_OBJECT import java.util.function.Consumer; @@ -224,6 +230,42 @@ public class HASH_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE imp while(iterator.hasNext()) add(iterator.NEXT()); } +#endif +#if JDK_TYPE && SPLIT_ITERATOR_FEATURE && STREAM_FEATURE +#if TYPE_OBJECT + /** + * Creates a Collector for a HashSet + * @Type(T) + * @return a collector + */ + public static Collector, HASH_SET> toSet() { + return Collector.of(HASH_SET::new, HASH_SET::add, HASH_SET::merge); + } + + /** + * Collects a Stream to a HashSet + * @Type(T) + * @return a set with the contents of the Stream + */ + public static HASH_SET KEY_GENERIC_TYPE toSet(Stream stream) { + return stream.collect(HASH_SET::new, HASH_SET::add, HASH_SET::merge); + } + + private HASH_SET merge(HASH_SET a) { + addAll(a); + return this; + } + +#else + /** + * Collects a Stream to a HashSet + * @return a set with the contents of the Stream + */ + public static HASH_SET toSet(JAVA_STREAM stream) { + return stream.collect(HASH_SET::new, HASH_SET::add, HASH_SET::addAll); + } + +#endif #endif @Override public boolean add(KEY_TYPE o) { diff --git a/src/builder/resources/speiger/assets/collections/templates/sets/RBTreeSet.template b/src/builder/resources/speiger/assets/collections/templates/sets/RBTreeSet.template index 27b2008..e5651da 100644 --- a/src/builder/resources/speiger/assets/collections/templates/sets/RBTreeSet.template +++ b/src/builder/resources/speiger/assets/collections/templates/sets/RBTreeSet.template @@ -4,6 +4,12 @@ import java.util.Collection; import java.util.Collections; #if JDK_TYPE import java.util.OPTIONAL; +#if !TYPE_OBJECT +import java.util.stream.JAVA_STREAM; +#else +import java.util.stream.Stream; +import java.util.stream.Collector; +#endif #endif #if TYPE_OBJECT import java.util.Comparator; @@ -214,6 +220,42 @@ public class RB_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE while(iterator.hasNext()) add(iterator.NEXT()); } +#if JDK_TYPE && SPLIT_ITERATOR_FEATURE && STREAM_FEATURE +#if TYPE_OBJECT + /** + * Creates a Collector for a RBTreeSet + * @Type(T) + * @return a collector + */ + public static Collector, RB_TREE_SET> toSet() { + return Collector.of(RB_TREE_SET::new, RB_TREE_SET::add, RB_TREE_SET::merge); + } + + /** + * Collects a Stream to a RBTreeSet + * @Type(T) + * @return a set with the contents of the Stream + */ + public static RB_TREE_SET KEY_GENERIC_TYPE toSet(Stream stream) { + return stream.collect(RB_TREE_SET::new, RB_TREE_SET::add, RB_TREE_SET::merge); + } + + private RB_TREE_SET merge(RB_TREE_SET a) { + addAll(a); + return this; + } + +#else + /** + * Collects a Stream to a RBTreeSet + * @return a set with the contents of the Stream + */ + public static RB_TREE_SET toSet(JAVA_STREAM stream) { + return stream.collect(RB_TREE_SET::new, RB_TREE_SET::add, RB_TREE_SET::addAll); + } + +#endif +#endif #if !TYPE_OBJECT @Override public void setDefaultMaxValue(KEY_TYPE value) { defaultMaxNotFound = value; } diff --git a/src/builder/resources/speiger/assets/collections/templates/utils/AsyncBuilder.template b/src/builder/resources/speiger/assets/collections/templates/utils/AsyncBuilder.template index d6ab499..de2aa62 100644 --- a/src/builder/resources/speiger/assets/collections/templates/utils/AsyncBuilder.template +++ b/src/builder/resources/speiger/assets/collections/templates/utils/AsyncBuilder.template @@ -7,6 +7,11 @@ import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; import java.util.concurrent.locks.LockSupport; import java.util.function.Consumer; +#if JDK_TYPE +import java.util.OPTIONAL; +#else +import speiger.src.collections.PACKAGE.functions.OPTIONAL; +#endif #if !TYPE_OBJECT import speiger.src.collections.PACKAGE.functions.CONSUMER; @@ -299,9 +304,8 @@ public class ASYNC_BUILDER KEY_GENERIC_TYPE * @param operator that reduces the elements. * @return self with the reduce action applied */ - public ASYNC_BUILDER KEY_GENERIC_TYPE reduce(UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) { - task = new SimpleReduceTaskBRACES(iterable.iterator(), operator); - return this; + public ObjectAsyncBuilder reduce(UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) { + return new ObjectAsyncBuilder<>(new SimpleReduceTaskBRACES(iterable.iterator(), operator)); } #if TYPE_OBJECT @@ -435,9 +439,8 @@ public class ASYNC_BUILDER KEY_GENERIC_TYPE * @param filter that decides the desired elements * @return self with the findFirst function applied */ - public ASYNC_BUILDER KEY_GENERIC_TYPE findFirst(PREDICATE KEY_GENERIC_TYPE filter) { - task = new FindFirstTaskBRACES(iterable.iterator(), filter); - return this; + public ObjectAsyncBuilder findFirst(PREDICATE KEY_GENERIC_TYPE filter) { + return new ObjectAsyncBuilder<>(new FindFirstTaskBRACES(iterable.iterator(), filter)); } #if INT_ASYNC_MODULE @@ -590,7 +593,7 @@ public class ASYNC_BUILDER KEY_GENERIC_TYPE } #endif - private static class SimpleReduceTask KEY_GENERIC_TYPE extends BASE_TASK KEY_GENERIC_TYPE + private static class SimpleReduceTask KEY_GENERIC_TYPE extends BaseObjectTask { ITERATOR KEY_GENERIC_TYPE iter; UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator; @@ -600,6 +603,7 @@ public class ASYNC_BUILDER KEY_GENERIC_TYPE public SimpleReduceTask(ITERATOR KEY_GENERIC_TYPE iter, UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) { this.iter = iter; this.operator = operator; + this.setResult(OPTIONAL.empty()); } @Override @@ -614,7 +618,7 @@ public class ASYNC_BUILDER KEY_GENERIC_TYPE } } if(!iter.hasNext()) { - setResult(value); + setResult(OPTIONAL.GET_OPTIONAL(value)); return true; } return false; @@ -689,7 +693,7 @@ public class ASYNC_BUILDER KEY_GENERIC_TYPE } #endif - private static class FindFirstTask KEY_GENERIC_TYPE extends BASE_TASK KEY_GENERIC_TYPE + private static class FindFirstTask KEY_GENERIC_TYPE extends BaseObjectTask { ITERATOR KEY_GENERIC_TYPE iter; PREDICATE KEY_GENERIC_TYPE filter; @@ -697,6 +701,7 @@ public class ASYNC_BUILDER KEY_GENERIC_TYPE public FindFirstTask(ITERATOR KEY_GENERIC_TYPE iter, PREDICATE KEY_GENERIC_TYPE filter) { this.iter = iter; this.filter = filter; + this.setResult(OPTIONAL.empty()); } @Override @@ -704,7 +709,7 @@ public class ASYNC_BUILDER KEY_GENERIC_TYPE while(shouldRun() && iter.hasNext()) { KEY_TYPE entry = iter.NEXT(); if(filter.test(iter.NEXT())) { - setResult(entry); + setResult(OPTIONAL.GET_OPTIONAL(entry)); return true; } } diff --git a/src/builder/resources/speiger/assets/collections/templates/utils/Collections.template b/src/builder/resources/speiger/assets/collections/templates/utils/Collections.template index b57980a..0c114b7 100644 --- a/src/builder/resources/speiger/assets/collections/templates/utils/Collections.template +++ b/src/builder/resources/speiger/assets/collections/templates/utils/Collections.template @@ -147,7 +147,10 @@ public class COLLECTIONS return new SingletonCollectionBRACES(element); } - protected static GENERIC_KEY_BRACES CollectionWrapper KEY_GENERIC_TYPE wrapper() { + /** + * Internal Use mainly. Its a collection wrapper with 0 dependencies for those actions where a collector is needed. + */ + public static GENERIC_KEY_BRACES CollectionWrapper KEY_GENERIC_TYPE wrapper() { return new CollectionWrapperBRACES(); } diff --git a/src/builder/resources/speiger/assets/testers/templates/builder/maps/OrderedMapTestSuiteBuilder.template b/src/builder/resources/speiger/assets/testers/templates/builder/maps/OrderedMapTestSuiteBuilder.template index 03cc30b..c63401e 100644 --- a/src/builder/resources/speiger/assets/testers/templates/builder/maps/OrderedMapTestSuiteBuilder.template +++ b/src/builder/resources/speiger/assets/testers/templates/builder/maps/OrderedMapTestSuiteBuilder.template @@ -79,7 +79,7 @@ public class ORDERED_MAP_TEST_BUILDER KEY_VALUE_GENERIC_TYPE extends MAP_TEST_BU features.addAll(parentBuilder.getFeatures()); features.remove(SpecialFeature.COPYING); features.remove(SpecialFeature.CHILDREN_COPY); - return ORDERED_MAP_TEST_BUILDER.using(new DERIVED_MAP_GENERATORS.ReverseTestOrderedMapGenerator(delegate)) + return ORDERED_MAP_TEST_BUILDER.using(new DERIVED_MAP_GENERATORS.ReverseTestOrderedMapGeneratorKV_BRACES(delegate)) .named(parentBuilder.getName() + " reversed").withFeatures(features) .suppressing(parentBuilder.getSuppressedTests()).createTestSuite(); } diff --git a/src/builder/resources/speiger/assets/testers/templates/tests/maps/OrderedMapPutTester.template b/src/builder/resources/speiger/assets/testers/templates/tests/maps/OrderedMapPutTester.template index 4aa0596..f78a68b 100644 --- a/src/builder/resources/speiger/assets/testers/templates/tests/maps/OrderedMapPutTester.template +++ b/src/builder/resources/speiger/assets/testers/templates/tests/maps/OrderedMapPutTester.template @@ -23,7 +23,6 @@ public class FILE_KEY_TYPE2FILE_VALUE_TYPEOrderedMapPutTester KEY_VALUE_GENERIC_ private ObjectList values; private KEY_TYPE a; private VALUE_TYPE aValue; - private VALUE_TYPE cValue; @Override public void setUp() throws Exception { @@ -33,9 +32,6 @@ public class FILE_KEY_TYPE2FILE_VALUE_TYPEOrderedMapPutTester KEY_VALUE_GENERIC_ if (values.size() >= 1) { a = values.get(0).ENTRY_KEY(); aValue = values.get(0).ENTRY_VALUE(); - if (values.size() >= 3) { - cValue = values.get(2).ENTRY_VALUE(); - } } } @@ -44,10 +40,10 @@ public class FILE_KEY_TYPE2FILE_VALUE_TYPEOrderedMapPutTester KEY_VALUE_GENERIC_ @CollectionSize.Require(absent = ZERO) #endignore public void testPutFirst() { - assertEquals(aValue, orderedMap.putFirst(a, cValue)); - assertNotEquals(cValue, orderedMap.FIRST_ENTRY_VALUE()); + assertEquals(aValue, orderedMap.putFirst(a, v3())); + assertNotEquals(v3(), orderedMap.FIRST_ENTRY_VALUE()); assertEquals(orderedMap.getDefaultReturnValue(), orderedMap.putFirst(k4(), v4())); - assertNotEquals(v4(), orderedMap.FIRST_ENTRY_VALUE()); + assertEquals(v4(), orderedMap.FIRST_ENTRY_VALUE()); assertEquals(e4(), orderedMap.firstEntry()); } @@ -57,10 +53,10 @@ public class FILE_KEY_TYPE2FILE_VALUE_TYPEOrderedMapPutTester KEY_VALUE_GENERIC_ @CollectionSize.Require(absent = ZERO) #endignore public void testPutLast() { - assertEquals(aValue, orderedMap.putLast(a, cValue)); - assertNotEquals(cValue, orderedMap.LAST_ENTRY_VALUE()); + assertEquals(aValue, orderedMap.putLast(a, v3())); + assertNotEquals(v3(), orderedMap.LAST_ENTRY_VALUE()); assertEquals(orderedMap.getDefaultReturnValue(), orderedMap.putLast(k4(), v4())); - assertNotEquals(v4(), orderedMap.LAST_ENTRY_VALUE()); + assertEquals(v4(), orderedMap.LAST_ENTRY_VALUE()); assertEquals(e4(), orderedMap.lastEntry()); }