diff --git a/src/builder/java/speiger/src/builder/modules/FunctionModule.java b/src/builder/java/speiger/src/builder/modules/FunctionModule.java index 42ce610..5a84b95 100644 --- a/src/builder/java/speiger/src/builder/modules/FunctionModule.java +++ b/src/builder/java/speiger/src/builder/modules/FunctionModule.java @@ -34,7 +34,8 @@ public class FunctionModule extends BaseModule protected void loadBlockades() { if(keyType.isObject()) addBlockedFiles("Consumer", "Comparator"); - if(!MODULE.isEnabled()) addBlockedFiles("Consumer", "BiConsumer", "Comparator", "Supplier", "Function", "UnaryOperator"); + if(!MODULE.isEnabled()) addBlockedFiles("Consumer", "BiConsumer", "Comparator", "Supplier", "Optional", "Function", "UnaryOperator"); + if(!keyType.needsCustomJDKType()) addBlockedFiles("Optional"); } @Override @@ -60,6 +61,7 @@ public class FunctionModule extends BaseModule } else addBiRequirement("Function"); addRemapper("BiConsumer", "%sConsumer"); + addRemapper("Optional", "Optional%s"); } @Override @@ -94,6 +96,8 @@ public class FunctionModule extends BaseModule addFunctionMappers("PREDICATE", "%sPredicate"); addClassMapper("SUPPLIER", "Supplier"); + addSimpleMapper("OPTIONAL", keyType.isObject() ? "Optional" : String.format("Optional%s", keyType.getFileType())); + addSimpleMapper("VALUE_OPTIONAL", valueType.isObject() ? "Optional" : String.format("Optional%s", valueType.getFileType())); addAbstractMapper("SINGLE_UNARY_OPERATOR", "%1$s%1$sUnaryOperator"); addBiClassMapper("UNARY_OPERATOR", "UnaryOperator", ""); if(keyType.isObject()) diff --git a/src/builder/java/speiger/src/builder/modules/JavaModule.java b/src/builder/java/speiger/src/builder/modules/JavaModule.java index da22499..d507a38 100644 --- a/src/builder/java/speiger/src/builder/modules/JavaModule.java +++ b/src/builder/java/speiger/src/builder/modules/JavaModule.java @@ -60,6 +60,8 @@ public class JavaModule extends BaseModule addSimpleMapper("APPLY_KEY_VALUE", keyType.isObject() ? "apply" : "applyAs"+keyType.getNonFileType()); addSimpleMapper("APPLY_VALUE", valueType.isObject() ? "apply" : "applyAs"+valueType.getNonFileType()); addSimpleMapper("APPLY_CAST", "applyAs"+keyType.getCustomJDKType().getNonFileType()); + addSimpleMapper("GET_OPTIONAL", keyType.isObject() ? "ofNullable" : "of"); + addSimpleMapper("GET_OPTIONAL_VALUE", valueType.isObject() ? "ofNullable" : "of"); //Shared by Maps and Pairs so moved to java. addFunctionMappers("ENTRY_KEY", "get%sKey"); diff --git a/src/builder/resources/speiger/assets/collections/templates/collections/Iterable.template b/src/builder/resources/speiger/assets/collections/templates/collections/Iterable.template index 1b48b6d..a67d5f9 100644 --- a/src/builder/resources/speiger/assets/collections/templates/collections/Iterable.template +++ b/src/builder/resources/speiger/assets/collections/templates/collections/Iterable.template @@ -5,6 +5,9 @@ import java.util.function.Consumer; #if JDK_FUNCTION import java.util.function.PREDICATE; #endif +#if JDK_TYPE +import java.util.OPTIONAL; +#endif #if !TYPE_OBJECT @@ -30,6 +33,9 @@ import speiger.src.collections.PACKAGE.collections.OUTPUT_ITERABLE; #endif #enditerate #endif +#if !JDK_TYPE +import speiger.src.collections.PACKAGE.functions.OPTIONAL; +#endif import speiger.src.collections.PACKAGE.functions.function.TO_OBJECT_FUNCTION; import speiger.src.collections.ints.functions.consumer.BI_FROM_INT_CONSUMER; import speiger.src.collections.objects.functions.consumer.BI_FROM_OBJECT_CONSUMER; @@ -408,13 +414,13 @@ public interface ITERABLE KEY_GENERIC_TYPE extends Iterable * @param filter that should be applied * @return the found value or the null equivalent variant. */ - default KEY_TYPE findFirst(PREDICATE KEY_GENERIC_TYPE filter) { + default OPTIONAL KEY_GENERIC_TYPE findFirst(PREDICATE KEY_GENERIC_TYPE filter) { Objects.requireNonNull(filter); for(ITERATOR KEY_GENERIC_TYPE iter = iterator();iter.hasNext();) { KEY_TYPE entry = iter.NEXT(); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return OPTIONAL.GET_OPTIONAL(entry); } - return EMPTY_VALUE; + return OPTIONAL.empty(); } #if !TYPE_OBJECT @@ -459,7 +465,7 @@ public interface ITERABLE KEY_GENERIC_TYPE extends Iterable * @param operator the operation that should be applied * @return the reduction result, returns null value if nothing was found */ - default KEY_TYPE reduce(UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) { + default OPTIONAL KEY_GENERIC_TYPE reduce(UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) { Objects.requireNonNull(operator); KEY_TYPE state = EMPTY_VALUE; boolean empty = true; @@ -471,7 +477,7 @@ public interface ITERABLE KEY_GENERIC_TYPE extends Iterable } state = operator.APPLY_VALUE(state, iter.NEXT()); } - return state; + return empty ? OPTIONAL.empty() : OPTIONAL.GET_OPTIONAL(state); } /** diff --git a/src/builder/resources/speiger/assets/collections/templates/functions/Optional.template b/src/builder/resources/speiger/assets/collections/templates/functions/Optional.template new file mode 100644 index 0000000..2929c88 --- /dev/null +++ b/src/builder/resources/speiger/assets/collections/templates/functions/Optional.template @@ -0,0 +1,89 @@ +package speiger.src.collections.PACKAGE.functions; + +import java.util.NoSuchElementException; +import java.util.function.Supplier; + +public final class OPTIONAL KEY_GENERIC_TYPE { + private static final OPTIONAL NO_GENERIC_TYPE EMPTY = new OPTIONALBRACES(); + + private final boolean isPresent; + private final KEY_TYPE value; + + private OPTIONAL() { + this.isPresent = false; + this.value = EMPTY_VALUE; + } + + private OPTIONAL(KEY_TYPE value) { + this.isPresent = true; + this.value = value; + } + + public static OPTIONAL KEY_GENERIC_TYPE empty() { + return EMPTY; + } + + public static OPTIONAL KEY_GENERIC_TYPE of(KEY_TYPE value) { + return new OPTIONALBRACES(value); + } + + public KEY_TYPE SUPPLY_GET() { + if(!isPresent) throw new NoSuchElementException("No value present"); + return value; + } + + public boolean isPresent() { + return isPresent; + } + + public boolean isEmpty() { + return !isPresent; + } + + public void ifPresent(CONSUMER KEY_GENERIC_TYPE consumer) { + if(isPresent) consumer.accept(value); + } + + public void ifPresentOrElse(CONSUMER KEY_GENERIC_TYPE action, Runnable emptyAction) { + if (isPresent) action.accept(value); + else emptyAction.run(); + } + + public KEY_TYPE orElse(KEY_TYPE other) { + return isPresent ? value : other; + } + + public KEY_TYPE orElseGet(SUPPLIER KEY_GENERIC_TYPE other) { + return isPresent ? value : other.SUPPLY_GET(); + } + + public KEY_TYPE orElseThrow() { + if (!isPresent) throw new NoSuchElementException("No value present"); + return value; + } + + public KEY_TYPE orElseThrow(Supplier exceptionSupplier) throws X { + if(isPresent) return value; + else throw exceptionSupplier.get(); + } + + @Override + public boolean equals(Object obj) { + if(obj == this) return true; + if(obj instanceof OPTIONAL) { + OPTIONAL KEY_GENERIC_TYPE other = (OPTIONAL KEY_GENERIC_TYPE)obj; + return (isPresent && other.isPresent ? KEY_EQUALS(value, other.value) : isPresent == other.isPresent); + } + return false; + } + + @Override + public int hashCode() { + return isPresent ? KEY_TO_HASH(value) : 0; + } + + @Override + public String toString() { + return isPresent ? "OPTIONAL["+value+"]" : "OPTIONAL.empty"; + } +} \ No newline at end of file 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 7659012..bf88d2f 100644 --- a/src/builder/resources/speiger/assets/collections/templates/lists/ArrayList.template +++ b/src/builder/resources/speiger/assets/collections/templates/lists/ArrayList.template @@ -7,6 +7,9 @@ import java.util.Comparator; import java.util.Collection; import java.util.Iterator; import java.util.Objects; +#if JDK_TYPE +import java.util.OPTIONAL; +#endif #if TYPE_OBJECT #if IARRAY_FEATURE || TYPE_OBJECT import java.util.function.Consumer; @@ -33,6 +36,9 @@ import speiger.src.collections.PACKAGE.collections.COLLECTION; import speiger.src.collections.PACKAGE.collections.STACK; #endif import speiger.src.collections.PACKAGE.collections.ITERATOR; +#if !JDK_TYPE +import speiger.src.collections.PACKAGE.functions.OPTIONAL; +#endif #if !TYPE_OBJECT import speiger.src.collections.PACKAGE.functions.COMPARATOR; import speiger.src.collections.PACKAGE.functions.CONSUMER; @@ -682,12 +688,12 @@ public class ARRAY_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE } @Override - public KEY_TYPE findFirst(PREDICATE KEY_GENERIC_TYPE filter) { + public OPTIONAL KEY_GENERIC_TYPE findFirst(PREDICATE KEY_GENERIC_TYPE filter) { Objects.requireNonNull(filter); for(int i = 0;i operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); MAP.Entry KEY_VALUE_GENERIC_TYPE state = null; boolean empty = true; @@ -863,11 +870,11 @@ public class CONCURRENT_HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY seg.unlockRead(stamp); } } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public MAP.Entry KEY_VALUE_GENERIC_TYPE findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); MapEntry entry = new MapEntry(); for(int i = 0,m=segments.length;i operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); MAP.Entry KEY_VALUE_GENERIC_TYPE state = null; boolean empty = true; @@ -888,21 +893,21 @@ public class LINKED_CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends CUSTOM_HASH_M state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public MAP.Entry KEY_VALUE_GENERIC_TYPE findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); MapEntry entry = new MapEntry(); int index = firstIndex; while(index != -1) { entry.set(index); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); index = (int)links[index]; } - return null; + return Optional.empty(); } @Override @@ -1160,7 +1165,7 @@ public class LINKED_CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends CUSTOM_HASH_M #endif @Override - public KEY_TYPE reduce(SINGLE_UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) { + public OPTIONAL KEY_GENERIC_TYPE reduce(SINGLE_UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) { Objects.requireNonNull(operator); KEY_TYPE state = EMPTY_KEY_VALUE; boolean empty = true; @@ -1175,19 +1180,19 @@ public class LINKED_CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends CUSTOM_HASH_M state = operator.APPLY_KEY_VALUE(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OPTIONAL.empty() : OPTIONAL.GET_OPTIONAL(state); } @Override - public KEY_TYPE findFirst(PREDICATE KEY_GENERIC_TYPE filter) { + public OPTIONAL KEY_GENERIC_TYPE findFirst(PREDICATE KEY_GENERIC_TYPE filter) { Objects.requireNonNull(filter); - if(size() <= 0) return EMPTY_KEY_VALUE; + if(size() <= 0) return OPTIONAL.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OPTIONAL.GET_OPTIONAL(keys[index]); index = (int)links[index]; } - return EMPTY_KEY_VALUE; + return OPTIONAL.empty(); } @Override @@ -1343,7 +1348,7 @@ public class LINKED_CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends CUSTOM_HASH_M #endif @Override - public VALUE_TYPE reduce(VALUE_SINGLE_UNARY_OPERATOR VALUE_VALUE_GENERIC_TYPE operator) { + public VALUE_OPTIONAL VALUE_GENERIC_TYPE reduce(VALUE_SINGLE_UNARY_OPERATOR VALUE_VALUE_GENERIC_TYPE operator) { Objects.requireNonNull(operator); VALUE_TYPE state = EMPTY_VALUE; boolean empty = true; @@ -1358,19 +1363,19 @@ public class LINKED_CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends CUSTOM_HASH_M state = operator.APPLY_VALUE(state, values[index]); index = (int)links[index]; } - return state; + return empty ? VALUE_OPTIONAL.empty() : VALUE_OPTIONAL.GET_OPTIONAL_VALUE(state); } @Override - public VALUE_TYPE findFirst(VALUE_PREDICATE VALUE_GENERIC_TYPE filter) { + public VALUE_OPTIONAL VALUE_GENERIC_TYPE findFirst(VALUE_PREDICATE VALUE_GENERIC_TYPE filter) { Objects.requireNonNull(filter); - if(size() <= 0) return EMPTY_VALUE; + if(size() <= 0) return VALUE_OPTIONAL.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return VALUE_OPTIONAL.GET_OPTIONAL_VALUE(values[index]); index = (int)links[index]; } - return EMPTY_VALUE; + return VALUE_OPTIONAL.empty(); } @Override diff --git a/src/builder/resources/speiger/assets/collections/templates/maps/impl/customHash/OpenCustomHashMap.template b/src/builder/resources/speiger/assets/collections/templates/maps/impl/customHash/OpenCustomHashMap.template index 76d1e51..97012e8 100644 --- a/src/builder/resources/speiger/assets/collections/templates/maps/impl/customHash/OpenCustomHashMap.template +++ b/src/builder/resources/speiger/assets/collections/templates/maps/impl/customHash/OpenCustomHashMap.template @@ -5,14 +5,17 @@ import java.util.ConcurrentModificationException; import java.util.Map; import java.util.NoSuchElementException; import java.util.Objects; +import java.util.Optional; import java.util.function.Consumer; import java.util.function.BiFunction; import java.util.function.Predicate; #if !TYPE_OBJECT && JDK_TYPE import java.util.function.PREDICATE; +import java.util.OPTIONAL; #endif #if !SAME_TYPE && JDK_VALUE && !VALUE_OBJECT import java.util.function.VALUE_PREDICATE; +import java.util.VALUE_OPTIONAL; #endif #if !TYPE_OBJECT @@ -37,9 +40,12 @@ import speiger.src.collections.PACKAGE.functions.function.UNARY_OPERATOR; #if !SAME_TYPE import speiger.src.collections.PACKAGE.functions.function.SINGLE_UNARY_OPERATOR; #endif -#if !TYPE_OBJECT && !VALUE_BOOLEAN && !JDK_TYPE +#if !TYPE_OBJECT && !JDK_TYPE +import speiger.src.collections.PACKAGE.functions.OPTIONAL; +#if !VALUE_BOOLEAN import speiger.src.collections.PACKAGE.functions.function.PREDICATE; #endif +#endif import speiger.src.collections.PACKAGE.maps.abstracts.ABSTRACT_MAP; import speiger.src.collections.PACKAGE.maps.interfaces.MAP; #if !TYPE_OBJECT @@ -75,6 +81,7 @@ import speiger.src.collections.objects.functions.consumer.VALUE_BI_FROM_OBJECT_C #endif #if !JDK_VALUE import speiger.src.collections.VALUE_PACKAGE.functions.function.VALUE_PREDICATE; +import speiger.src.collections.VALUE_PACKAGE.functions.VALUE_OPTIONAL; #endif #endif import speiger.src.collections.objects.collections.ObjectIterator; @@ -1090,7 +1097,7 @@ public class CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VAL } @Override - public MAP.Entry KEY_VALUE_GENERIC_TYPE reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); MAP.Entry KEY_VALUE_GENERIC_TYPE state = null; boolean empty = true; @@ -1107,25 +1114,25 @@ public class CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VAL } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public MAP.Entry KEY_VALUE_GENERIC_TYPE findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); MapEntry entry = new MapEntry(); if(containsNull) { entry.set(nullIndex); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } for(int i = nullIndex-1;i>=0;i--) { if(!strategy.equals(keys[i], EMPTY_KEY_VALUE)) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1331,7 +1338,7 @@ public class CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VAL #endif @Override - public KEY_TYPE reduce(SINGLE_UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) { + public OPTIONAL KEY_GENERIC_TYPE reduce(SINGLE_UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) { Objects.requireNonNull(operator); KEY_TYPE state = EMPTY_KEY_VALUE; boolean empty = true; @@ -1348,18 +1355,18 @@ public class CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VAL } state = operator.APPLY_KEY_VALUE(state, keys[i]); } - return state; + return empty ? OPTIONAL.empty() : OPTIONAL.GET_OPTIONAL(state); } @Override - public KEY_TYPE findFirst(PREDICATE KEY_GENERIC_TYPE filter) { + public OPTIONAL KEY_GENERIC_TYPE findFirst(PREDICATE KEY_GENERIC_TYPE filter) { Objects.requireNonNull(filter); - if(size() <= 0) return EMPTY_KEY_VALUE; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OPTIONAL.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OPTIONAL.GET_OPTIONAL(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], EMPTY_KEY_VALUE) && filter.test(keys[i])) return keys[i]; + if(!strategy.equals(keys[i], EMPTY_KEY_VALUE) && filter.test(keys[i])) return OPTIONAL.GET_OPTIONAL(keys[i]); } - return EMPTY_KEY_VALUE; + return OPTIONAL.empty(); } @Override @@ -1497,7 +1504,7 @@ public class CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VAL #endif @Override - public VALUE_TYPE reduce(VALUE_SINGLE_UNARY_OPERATOR VALUE_VALUE_GENERIC_TYPE operator) { + public VALUE_OPTIONAL VALUE_GENERIC_TYPE reduce(VALUE_SINGLE_UNARY_OPERATOR VALUE_VALUE_GENERIC_TYPE operator) { Objects.requireNonNull(operator); VALUE_TYPE state = EMPTY_VALUE; boolean empty = true; @@ -1514,18 +1521,18 @@ public class CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VAL } state = operator.APPLY_VALUE(state, values[i]); } - return state; + return empty ? VALUE_OPTIONAL.empty() : VALUE_OPTIONAL.GET_OPTIONAL_VALUE(state); } @Override - public VALUE_TYPE findFirst(VALUE_PREDICATE VALUE_GENERIC_TYPE filter) { + public VALUE_OPTIONAL VALUE_GENERIC_TYPE findFirst(VALUE_PREDICATE VALUE_GENERIC_TYPE filter) { Objects.requireNonNull(filter); - if(size() <= 0) return EMPTY_VALUE; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return VALUE_OPTIONAL.empty(); + if(containsNull && filter.test(values[nullIndex])) return VALUE_OPTIONAL.GET_OPTIONAL_VALUE(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], EMPTY_KEY_VALUE) && filter.test(values[i])) return values[i]; + if(!strategy.equals(keys[i], EMPTY_KEY_VALUE) && filter.test(values[i])) return VALUE_OPTIONAL.GET_OPTIONAL_VALUE(values[i]); } - return EMPTY_VALUE; + return VALUE_OPTIONAL.empty(); } @Override 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 3ac8a24..9c27197 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 @@ -4,14 +4,17 @@ import java.util.Arrays; import java.util.Map; import java.util.NoSuchElementException; import java.util.Objects; +import java.util.Optional; import java.util.function.Consumer; import java.util.function.BiFunction; import java.util.function.Predicate; #if !TYPE_OBJECT && JDK_TYPE import java.util.function.PREDICATE; +import java.util.OPTIONAL; #endif #if !SAME_TYPE && JDK_VALUE && !VALUE_OBJECT import java.util.function.VALUE_PREDICATE; +import java.util.VALUE_OPTIONAL; #endif import speiger.src.collections.PACKAGE.collections.BI_ITERATOR; @@ -27,6 +30,7 @@ import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.VALUE_BI_FROM_INT_CONSUMER; #endif #if !TYPE_OBJECT && !JDK_TYPE +import speiger.src.collections.PACKAGE.functions.OPTIONAL; import speiger.src.collections.PACKAGE.functions.function.PREDICATE; #endif #if !TYPE_INT || !SAME_TYPE @@ -58,6 +62,7 @@ import speiger.src.collections.objects.functions.consumer.VALUE_BI_FROM_OBJECT_C #endif #if !JDK_VALUE import speiger.src.collections.VALUE_PACKAGE.functions.function.VALUE_PREDICATE; +import speiger.src.collections.VALUE_PACKAGE.functions.VALUE_OPTIONAL; #endif #endif #if !TYPE_OBJECT @@ -877,7 +882,7 @@ public class LINKED_HASH_MAP KEY_VALUE_GENERIC_TYPE extends HASH_MAP KEY_VALUE_G } @Override - public MAP.Entry KEY_VALUE_GENERIC_TYPE reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); MAP.Entry KEY_VALUE_GENERIC_TYPE state = null; boolean empty = true; @@ -892,21 +897,21 @@ public class LINKED_HASH_MAP KEY_VALUE_GENERIC_TYPE extends HASH_MAP KEY_VALUE_G state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public MAP.Entry KEY_VALUE_GENERIC_TYPE findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); MapEntry entry = new MapEntry(); int index = firstIndex; while(index != -1) { entry.set(index); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); index = (int)links[index]; } - return null; + return Optional.empty(); } @Override @@ -1159,7 +1164,7 @@ public class LINKED_HASH_MAP KEY_VALUE_GENERIC_TYPE extends HASH_MAP KEY_VALUE_G #endif @Override - public KEY_TYPE reduce(SINGLE_UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) { + public OPTIONAL KEY_GENERIC_TYPE reduce(SINGLE_UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) { Objects.requireNonNull(operator); KEY_TYPE state = EMPTY_KEY_VALUE; boolean empty = true; @@ -1174,19 +1179,19 @@ public class LINKED_HASH_MAP KEY_VALUE_GENERIC_TYPE extends HASH_MAP KEY_VALUE_G state = operator.APPLY_KEY_VALUE(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OPTIONAL.empty() : OPTIONAL.GET_OPTIONAL(state); } @Override - public KEY_TYPE findFirst(PREDICATE KEY_GENERIC_TYPE filter) { + public OPTIONAL KEY_GENERIC_TYPE findFirst(PREDICATE KEY_GENERIC_TYPE filter) { Objects.requireNonNull(filter); - if(size() <= 0) return EMPTY_KEY_VALUE; + if(size() <= 0) return OPTIONAL.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OPTIONAL.GET_OPTIONAL(keys[index]); index = (int)links[index]; } - return EMPTY_KEY_VALUE; + return OPTIONAL.empty(); } @Override @@ -1344,7 +1349,7 @@ public class LINKED_HASH_MAP KEY_VALUE_GENERIC_TYPE extends HASH_MAP KEY_VALUE_G #endif @Override - public VALUE_TYPE reduce(VALUE_SINGLE_UNARY_OPERATOR VALUE_VALUE_GENERIC_TYPE operator) { + public VALUE_OPTIONAL VALUE_GENERIC_TYPE reduce(VALUE_SINGLE_UNARY_OPERATOR VALUE_VALUE_GENERIC_TYPE operator) { Objects.requireNonNull(operator); VALUE_TYPE state = EMPTY_VALUE; boolean empty = true; @@ -1359,19 +1364,19 @@ public class LINKED_HASH_MAP KEY_VALUE_GENERIC_TYPE extends HASH_MAP KEY_VALUE_G state = operator.APPLY_VALUE(state, values[index]); index = (int)links[index]; } - return state; + return empty ? VALUE_OPTIONAL.empty() : VALUE_OPTIONAL.GET_OPTIONAL_VALUE(state); } @Override - public VALUE_TYPE findFirst(VALUE_PREDICATE VALUE_GENERIC_TYPE filter) { + public VALUE_OPTIONAL VALUE_GENERIC_TYPE findFirst(VALUE_PREDICATE VALUE_GENERIC_TYPE filter) { Objects.requireNonNull(filter); - if(size() <= 0) return EMPTY_VALUE; + if(size() <= 0) return VALUE_OPTIONAL.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return VALUE_OPTIONAL.GET_OPTIONAL_VALUE(values[index]); index = (int)links[index]; } - return EMPTY_VALUE; + return VALUE_OPTIONAL.empty(); } @Override diff --git a/src/builder/resources/speiger/assets/collections/templates/maps/impl/hash/OpenHashMap.template b/src/builder/resources/speiger/assets/collections/templates/maps/impl/hash/OpenHashMap.template index e7e4eb7..a3676a5 100644 --- a/src/builder/resources/speiger/assets/collections/templates/maps/impl/hash/OpenHashMap.template +++ b/src/builder/resources/speiger/assets/collections/templates/maps/impl/hash/OpenHashMap.template @@ -5,14 +5,17 @@ import java.util.ConcurrentModificationException; import java.util.Map; import java.util.NoSuchElementException; import java.util.Objects; +import java.util.Optional; import java.util.function.Consumer; import java.util.function.BiFunction; import java.util.function.Predicate; #if !TYPE_OBJECT && JDK_TYPE import java.util.function.PREDICATE; +import java.util.OPTIONAL; #endif #if !SAME_TYPE && JDK_VALUE && !VALUE_OBJECT import java.util.function.VALUE_PREDICATE; +import java.util.VALUE_OPTIONAL; #endif #if !TYPE_OBJECT @@ -37,9 +40,12 @@ import speiger.src.collections.PACKAGE.functions.function.UNARY_OPERATOR; #if !SAME_TYPE import speiger.src.collections.PACKAGE.functions.function.SINGLE_UNARY_OPERATOR; #endif -#if !TYPE_OBJECT && !VALUE_BOOLEAN && !JDK_TYPE +#if !TYPE_OBJECT && !JDK_TYPE +import speiger.src.collections.PACKAGE.functions.OPTIONAL; +#if !VALUE_BOOLEAN import speiger.src.collections.PACKAGE.functions.function.PREDICATE; #endif +#endif import speiger.src.collections.PACKAGE.maps.abstracts.ABSTRACT_MAP; import speiger.src.collections.PACKAGE.maps.interfaces.MAP; #if !TYPE_OBJECT @@ -73,6 +79,7 @@ import speiger.src.collections.objects.functions.consumer.VALUE_BI_FROM_OBJECT_C #if !JDK_VALUE import speiger.src.collections.VALUE_PACKAGE.functions.function.VALUE_PREDICATE; +import speiger.src.collections.VALUE_PACKAGE.functions.VALUE_OPTIONAL; #endif #endif import speiger.src.collections.objects.collections.ObjectIterator; @@ -1049,7 +1056,7 @@ public class HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_GENE } @Override - public MAP.Entry KEY_VALUE_GENERIC_TYPE reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); MAP.Entry KEY_VALUE_GENERIC_TYPE state = null; boolean empty = true; @@ -1066,25 +1073,25 @@ public class HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_GENE } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public MAP.Entry KEY_VALUE_GENERIC_TYPE findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); MapEntry entry = new MapEntry(); if(containsNull) { entry.set(nullIndex); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } for(int i = nullIndex-1;i>=0;i--) { if(KEY_EQUALS_NOT_NULL(keys[i])) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1286,7 +1293,7 @@ public class HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_GENE #endif @Override - public KEY_TYPE reduce(SINGLE_UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) { + public OPTIONAL KEY_GENERIC_TYPE reduce(SINGLE_UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) { Objects.requireNonNull(operator); KEY_TYPE state = EMPTY_KEY_VALUE; boolean empty = true; @@ -1303,18 +1310,18 @@ public class HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_GENE } state = operator.APPLY_KEY_VALUE(state, keys[i]); } - return state; + return empty ? OPTIONAL.empty() : OPTIONAL.GET_OPTIONAL(state); } @Override - public KEY_TYPE findFirst(PREDICATE KEY_GENERIC_TYPE filter) { + public OPTIONAL KEY_GENERIC_TYPE findFirst(PREDICATE KEY_GENERIC_TYPE filter) { Objects.requireNonNull(filter); - if(size() <= 0) return EMPTY_KEY_VALUE; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OPTIONAL.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OPTIONAL.GET_OPTIONAL(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(KEY_EQUALS_NOT_NULL(keys[i]) && filter.test(keys[i])) return keys[i]; + if(KEY_EQUALS_NOT_NULL(keys[i]) && filter.test(keys[i])) return OPTIONAL.GET_OPTIONAL(keys[i]); } - return EMPTY_KEY_VALUE; + return OPTIONAL.empty(); } @Override @@ -1452,7 +1459,7 @@ public class HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_GENE #endif @Override - public VALUE_TYPE reduce(VALUE_SINGLE_UNARY_OPERATOR VALUE_VALUE_GENERIC_TYPE operator) { + public VALUE_OPTIONAL VALUE_GENERIC_TYPE reduce(VALUE_SINGLE_UNARY_OPERATOR VALUE_VALUE_GENERIC_TYPE operator) { Objects.requireNonNull(operator); VALUE_TYPE state = EMPTY_VALUE; boolean empty = true; @@ -1469,18 +1476,18 @@ public class HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_GENE } state = operator.APPLY_VALUE(state, values[i]); } - return state; + return empty ? VALUE_OPTIONAL.empty() : VALUE_OPTIONAL.GET_OPTIONAL_VALUE(state); } @Override - public VALUE_TYPE findFirst(VALUE_PREDICATE VALUE_GENERIC_TYPE filter) { + public VALUE_OPTIONAL VALUE_GENERIC_TYPE findFirst(VALUE_PREDICATE VALUE_GENERIC_TYPE filter) { Objects.requireNonNull(filter); - if(size() <= 0) return EMPTY_VALUE; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return VALUE_OPTIONAL.empty(); + if(containsNull && filter.test(values[nullIndex])) return VALUE_OPTIONAL.GET_OPTIONAL_VALUE(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(KEY_EQUALS_NOT_NULL(keys[i]) && filter.test(values[i])) return values[i]; + if(KEY_EQUALS_NOT_NULL(keys[i]) && filter.test(values[i])) return VALUE_OPTIONAL.GET_OPTIONAL_VALUE(values[i]); } - return EMPTY_VALUE; + return VALUE_OPTIONAL.empty(); } @Override diff --git a/src/builder/resources/speiger/assets/collections/templates/maps/impl/immutable/ImmutableOpenHashMap.template b/src/builder/resources/speiger/assets/collections/templates/maps/impl/immutable/ImmutableOpenHashMap.template index 3434913..7269b24 100644 --- a/src/builder/resources/speiger/assets/collections/templates/maps/impl/immutable/ImmutableOpenHashMap.template +++ b/src/builder/resources/speiger/assets/collections/templates/maps/impl/immutable/ImmutableOpenHashMap.template @@ -4,14 +4,17 @@ import java.util.Arrays; import java.util.Map; import java.util.NoSuchElementException; import java.util.Objects; +import java.util.Optional; import java.util.function.Consumer; import java.util.function.BiFunction; import java.util.function.Predicate; #if !TYPE_OBJECT && JDK_TYPE import java.util.function.PREDICATE; +import java.util.OPTIONAL; #endif #if !SAME_TYPE && JDK_VALUE && !VALUE_OBJECT import java.util.function.VALUE_PREDICATE; +import java.util.VALUE_OPTIONAL; #endif #if !TYPE_OBJECT @@ -26,9 +29,12 @@ import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; #if !SAME_TYPE && !TYPE_INT import speiger.src.collections.ints.functions.consumer.VALUE_BI_FROM_INT_CONSUMER; #endif -#if !TYPE_OBJECT && !VALUE_BOOLEAN && !JDK_TYPE +#if !TYPE_OBJECT && !JDK_TYPE +import speiger.src.collections.PACKAGE.functions.OPTIONAL; +#if !VALUE_BOOLEAN import speiger.src.collections.PACKAGE.functions.function.PREDICATE; #endif +#endif #if !TYPE_INT || !SAME_TYPE import speiger.src.collections.PACKAGE.functions.consumer.BI_CONSUMER; #endif @@ -59,6 +65,7 @@ import speiger.src.collections.objects.functions.consumer.VALUE_BI_FROM_OBJECT_C #endif #if !JDK_VALUE import speiger.src.collections.VALUE_PACKAGE.functions.function.VALUE_PREDICATE; +import speiger.src.collections.VALUE_PACKAGE.functions.VALUE_OPTIONAL; #endif #endif #if !TYPE_OBJECT @@ -783,7 +790,7 @@ public class IMMUTABLE_HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_ } @Override - public MAP.Entry KEY_VALUE_GENERIC_TYPE reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); MAP.Entry KEY_VALUE_GENERIC_TYPE state = null; boolean empty = true; @@ -798,21 +805,21 @@ public class IMMUTABLE_HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_ state = operator.apply(state, new BasicEntryKV_BRACES(keys[index], values[index])); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public MAP.Entry KEY_VALUE_GENERIC_TYPE findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry KEY_VALUE_GENERIC_TYPE entry = new BasicEntryKV_BRACES(); int index = firstIndex; while(index != -1) { entry.set(keys[index], values[index]); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); index = (int)links[index]; } - return null; + return Optional.empty(); } @Override @@ -1037,7 +1044,7 @@ public class IMMUTABLE_HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_ #endif @Override - public KEY_TYPE reduce(SINGLE_UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) { + public OPTIONAL KEY_GENERIC_TYPE reduce(SINGLE_UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) { Objects.requireNonNull(operator); KEY_TYPE state = EMPTY_KEY_VALUE; boolean empty = true; @@ -1052,19 +1059,19 @@ public class IMMUTABLE_HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_ state = operator.APPLY_KEY_VALUE(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OPTIONAL.empty() : OPTIONAL.GET_OPTIONAL(state); } @Override - public KEY_TYPE findFirst(PREDICATE KEY_GENERIC_TYPE filter) { + public OPTIONAL KEY_GENERIC_TYPE findFirst(PREDICATE KEY_GENERIC_TYPE filter) { Objects.requireNonNull(filter); - if(size() <= 0) return EMPTY_KEY_VALUE; + if(size() <= 0) return OPTIONAL.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OPTIONAL.GET_OPTIONAL(keys[index]); index = (int)links[index]; } - return EMPTY_KEY_VALUE; + return OPTIONAL.empty(); } @Override @@ -1213,7 +1220,7 @@ public class IMMUTABLE_HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_ #endif @Override - public VALUE_TYPE reduce(VALUE_SINGLE_UNARY_OPERATOR VALUE_VALUE_GENERIC_TYPE operator) { + public VALUE_OPTIONAL VALUE_GENERIC_TYPE reduce(VALUE_SINGLE_UNARY_OPERATOR VALUE_VALUE_GENERIC_TYPE operator) { Objects.requireNonNull(operator); VALUE_TYPE state = EMPTY_VALUE; boolean empty = true; @@ -1228,19 +1235,19 @@ public class IMMUTABLE_HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_ state = operator.APPLY_VALUE(state, values[index]); index = (int)links[index]; } - return state; + return empty ? VALUE_OPTIONAL.empty() : VALUE_OPTIONAL.GET_OPTIONAL_VALUE(state); } @Override - public VALUE_TYPE findFirst(VALUE_PREDICATE VALUE_GENERIC_TYPE filter) { + public VALUE_OPTIONAL VALUE_GENERIC_TYPE findFirst(VALUE_PREDICATE VALUE_GENERIC_TYPE filter) { Objects.requireNonNull(filter); - if(size() <= 0) return EMPTY_VALUE; + if(size() <= 0) return VALUE_OPTIONAL.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return VALUE_OPTIONAL.GET_OPTIONAL_VALUE(values[index]); index = (int)links[index]; } - return EMPTY_VALUE; + return VALUE_OPTIONAL.empty(); } @Override diff --git a/src/builder/resources/speiger/assets/collections/templates/maps/impl/misc/ArrayMap.template b/src/builder/resources/speiger/assets/collections/templates/maps/impl/misc/ArrayMap.template index 6a678b4..9097ac9 100644 --- a/src/builder/resources/speiger/assets/collections/templates/maps/impl/misc/ArrayMap.template +++ b/src/builder/resources/speiger/assets/collections/templates/maps/impl/misc/ArrayMap.template @@ -4,14 +4,17 @@ import java.util.Arrays; import java.util.Map; import java.util.NoSuchElementException; import java.util.Objects; +import java.util.Optional; import java.util.function.Consumer; import java.util.function.BiFunction; import java.util.function.Predicate; #if !TYPE_OBJECT && JDK_TYPE import java.util.function.PREDICATE; +import java.util.OPTIONAL; #endif #if !SAME_TYPE && JDK_VALUE && !VALUE_OBJECT import java.util.function.VALUE_PREDICATE; +import java.util.VALUE_OPTIONAL; #endif import speiger.src.collections.PACKAGE.collections.BI_ITERATOR; @@ -26,9 +29,12 @@ import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; #if !SAME_TYPE && !TYPE_INT import speiger.src.collections.ints.functions.consumer.VALUE_BI_FROM_INT_CONSUMER; #endif -#if !TYPE_OBJECT && !VALUE_BOOLEAN && !JDK_TYPE +#if !TYPE_OBJECT && !JDK_TYPE +import speiger.src.collections.PACKAGE.functions.OPTIONAL; +#if!VALUE_BOOLEAN import speiger.src.collections.PACKAGE.functions.function.PREDICATE; #endif +#endif #if !TYPE_INT || !SAME_TYPE import speiger.src.collections.PACKAGE.functions.consumer.BI_CONSUMER; #endif @@ -66,6 +72,7 @@ import speiger.src.collections.objects.functions.consumer.VALUE_BI_FROM_OBJECT_C #endif #if !JDK_VALUE import speiger.src.collections.VALUE_PACKAGE.functions.function.VALUE_PREDICATE; +import speiger.src.collections.VALUE_PACKAGE.functions.VALUE_OPTIONAL; #endif #endif #if !VALUE_OBJECT @@ -1009,7 +1016,7 @@ public class ARRAY_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_GEN } @Override - public MAP.Entry KEY_VALUE_GENERIC_TYPE reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); MAP.Entry KEY_VALUE_GENERIC_TYPE state = null; boolean empty = true; @@ -1021,19 +1028,19 @@ public class ARRAY_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_GEN } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public MAP.Entry KEY_VALUE_GENERIC_TYPE findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); MapEntry entry = new MapEntry(); for(int i = 0;i operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); MAP.Entry KEY_VALUE_GENERIC_TYPE state = null; boolean empty = true; @@ -2038,19 +2045,19 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_ } state = operator.apply(state, new BasicEntryKV_BRACES(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public MAP.Entry KEY_VALUE_GENERIC_TYPE findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry KEY_VALUE_GENERIC_TYPE subEntry = new BasicEntryKV_BRACES(); for(Node KEY_VALUE_GENERIC_TYPE entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) { subEntry.set(entry.key, entry.value); - if(filter.test(subEntry)) return subEntry; + if(filter.test(subEntry)) return Optional.ofNullable(subEntry); } - return null; + return Optional.empty(); } @Override @@ -2165,7 +2172,7 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_ #endif @Override - public VALUE_TYPE reduce(VALUE_SINGLE_UNARY_OPERATOR VALUE_VALUE_GENERIC_TYPE operator) { + public VALUE_OPTIONAL VALUE_GENERIC_TYPE reduce(VALUE_SINGLE_UNARY_OPERATOR VALUE_VALUE_GENERIC_TYPE operator) { Objects.requireNonNull(operator); VALUE_TYPE state = EMPTY_VALUE; boolean empty = true; @@ -2177,15 +2184,15 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_ } state = operator.APPLY_VALUE(state, entry.value); } - return state; + return empty ? VALUE_OPTIONAL.empty() : VALUE_OPTIONAL.GET_OPTIONAL_VALUE(state); } @Override - public VALUE_TYPE findFirst(VALUE_PREDICATE VALUE_GENERIC_TYPE filter) { + public VALUE_OPTIONAL VALUE_GENERIC_TYPE findFirst(VALUE_PREDICATE VALUE_GENERIC_TYPE filter) { Objects.requireNonNull(filter); for(Node KEY_VALUE_GENERIC_TYPE entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return EMPTY_VALUE; + if(filter.test(entry.value)) return VALUE_OPTIONAL.GET_OPTIONAL_VALUE(entry.value); + return VALUE_OPTIONAL.empty(); } @Override @@ -2521,7 +2528,7 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_ #endif @Override - public VALUE_TYPE reduce(VALUE_SINGLE_UNARY_OPERATOR VALUE_VALUE_GENERIC_TYPE operator) { + public VALUE_OPTIONAL VALUE_GENERIC_TYPE reduce(VALUE_SINGLE_UNARY_OPERATOR VALUE_VALUE_GENERIC_TYPE operator) { Objects.requireNonNull(operator); VALUE_TYPE state = EMPTY_VALUE; boolean empty = true; @@ -2533,15 +2540,15 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_ } state = operator.APPLY_VALUE(state, entry.value); } - return state; + return empty ? VALUE_OPTIONAL.empty() : VALUE_OPTIONAL.GET_OPTIONAL_VALUE(state); } @Override - public VALUE_TYPE findFirst(VALUE_PREDICATE VALUE_GENERIC_TYPE filter) { + public VALUE_OPTIONAL VALUE_GENERIC_TYPE findFirst(VALUE_PREDICATE VALUE_GENERIC_TYPE filter) { Objects.requireNonNull(filter); for(Node KEY_VALUE_GENERIC_TYPE entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return EMPTY_VALUE; + if(filter.test(entry.value)) return VALUE_OPTIONAL.GET_OPTIONAL_VALUE(entry.value); + return VALUE_OPTIONAL.empty(); } @Override @@ -2687,7 +2694,7 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_ } @Override - public MAP.Entry KEY_VALUE_GENERIC_TYPE reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); MAP.Entry KEY_VALUE_GENERIC_TYPE state = null; boolean empty = true; @@ -2699,19 +2706,19 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_ } state = operator.apply(state, new BasicEntryKV_BRACES(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public MAP.Entry KEY_VALUE_GENERIC_TYPE findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry KEY_VALUE_GENERIC_TYPE subEntry = new BasicEntryKV_BRACES(); for(Node KEY_VALUE_GENERIC_TYPE entry = first;entry != null;entry = entry.next()) { subEntry.set(entry.key, entry.value); - if(filter.test(subEntry)) return subEntry; + if(filter.test(subEntry)) return Optional.ofNullable(subEntry); } - return null; + return Optional.empty(); } @Override diff --git a/src/builder/resources/speiger/assets/collections/templates/maps/impl/tree/RBTreeMap.template b/src/builder/resources/speiger/assets/collections/templates/maps/impl/tree/RBTreeMap.template index 128c436..68bc672 100644 --- a/src/builder/resources/speiger/assets/collections/templates/maps/impl/tree/RBTreeMap.template +++ b/src/builder/resources/speiger/assets/collections/templates/maps/impl/tree/RBTreeMap.template @@ -6,15 +6,18 @@ import java.util.Map; import java.util.Comparator; #endif import java.util.Objects; +import java.util.Optional; import java.util.NoSuchElementException; import java.util.function.Consumer; import java.util.function.BiFunction; import java.util.function.Predicate; #if !TYPE_OBJECT && JDK_TYPE import java.util.function.PREDICATE; +import java.util.OPTIONAL; #endif #if !SAME_TYPE && JDK_VALUE && !VALUE_OBJECT import java.util.function.VALUE_PREDICATE; +import java.util.VALUE_OPTIONAL; #endif import speiger.src.collections.PACKAGE.collections.BI_ITERATOR; @@ -36,9 +39,12 @@ import speiger.src.collections.PACKAGE.functions.function.FUNCTION; #if !TYPE_INT || !SAME_TYPE import speiger.src.collections.PACKAGE.functions.consumer.BI_CONSUMER; #endif -#if !TYPE_OBJECT && !VALUE_BOOLEAN && !JDK_TYPE +#if !TYPE_OBJECT && !JDK_TYPE +import speiger.src.collections.PACKAGE.functions.OPTIONAL; +#if!VALUE_BOOLEAN import speiger.src.collections.PACKAGE.functions.function.PREDICATE; #endif +#endif import speiger.src.collections.PACKAGE.functions.function.UNARY_OPERATOR; #if !SAME_TYPE import speiger.src.collections.PACKAGE.functions.function.SINGLE_UNARY_OPERATOR; @@ -72,6 +78,7 @@ import speiger.src.collections.objects.functions.consumer.VALUE_BI_FROM_OBJECT_C #endif #if !JDK_VALUE import speiger.src.collections.VALUE_PACKAGE.functions.function.VALUE_PREDICATE; +import speiger.src.collections.VALUE_PACKAGE.functions.VALUE_OPTIONAL; #endif #endif #if !TYPE_OBJECT && !VALUE_OBJECT @@ -1379,7 +1386,7 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G #endif @Override - public KEY_TYPE reduce(SINGLE_UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) { + public OPTIONAL KEY_GENERIC_TYPE reduce(SINGLE_UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) { Objects.requireNonNull(operator); KEY_TYPE state = EMPTY_KEY_VALUE; boolean empty = true; @@ -1391,15 +1398,15 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G } state = operator.APPLY_KEY_VALUE(state, entry.key); } - return state; + return empty ? OPTIONAL.empty() : OPTIONAL.GET_OPTIONAL(state); } @Override - public KEY_TYPE findFirst(PREDICATE KEY_GENERIC_TYPE filter) { + public OPTIONAL KEY_GENERIC_TYPE findFirst(PREDICATE KEY_GENERIC_TYPE filter) { Objects.requireNonNull(filter); for(Node KEY_VALUE_GENERIC_TYPE entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.test(entry.key)) return entry.key; - return EMPTY_KEY_VALUE; + if(filter.test(entry.key)) return OPTIONAL.GET_OPTIONAL(entry.key); + return OPTIONAL.empty(); } @Override @@ -2092,7 +2099,7 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G } @Override - public MAP.Entry KEY_VALUE_GENERIC_TYPE reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); MAP.Entry KEY_VALUE_GENERIC_TYPE state = null; boolean empty = true; @@ -2104,19 +2111,19 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G } state = operator.apply(state, new BasicEntryKV_BRACES(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public MAP.Entry KEY_VALUE_GENERIC_TYPE findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry KEY_VALUE_GENERIC_TYPE subEntry = new BasicEntryKV_BRACES(); for(Node KEY_VALUE_GENERIC_TYPE entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) { subEntry.set(entry.key, entry.value); - if(filter.test(subEntry)) return subEntry; + if(filter.test(subEntry)) return Optional.ofNullable(subEntry); } - return null; + return Optional.empty(); } @Override @@ -2231,7 +2238,7 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G #endif @Override - public VALUE_TYPE reduce(VALUE_SINGLE_UNARY_OPERATOR VALUE_VALUE_GENERIC_TYPE operator) { + public VALUE_OPTIONAL VALUE_GENERIC_TYPE reduce(VALUE_SINGLE_UNARY_OPERATOR VALUE_VALUE_GENERIC_TYPE operator) { Objects.requireNonNull(operator); VALUE_TYPE state = EMPTY_VALUE; boolean empty = true; @@ -2243,15 +2250,15 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G } state = operator.APPLY_VALUE(state, entry.value); } - return state; + return empty ? VALUE_OPTIONAL.empty() : VALUE_OPTIONAL.GET_OPTIONAL_VALUE(state); } @Override - public VALUE_TYPE findFirst(VALUE_PREDICATE VALUE_GENERIC_TYPE filter) { + public VALUE_OPTIONAL VALUE_GENERIC_TYPE findFirst(VALUE_PREDICATE VALUE_GENERIC_TYPE filter) { Objects.requireNonNull(filter); for(Node KEY_VALUE_GENERIC_TYPE entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return EMPTY_VALUE; + if(filter.test(entry.value)) return VALUE_OPTIONAL.GET_OPTIONAL_VALUE(entry.value); + return VALUE_OPTIONAL.empty(); } @Override @@ -2587,7 +2594,7 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G #endif @Override - public VALUE_TYPE reduce(VALUE_SINGLE_UNARY_OPERATOR VALUE_VALUE_GENERIC_TYPE operator) { + public VALUE_OPTIONAL VALUE_GENERIC_TYPE reduce(VALUE_SINGLE_UNARY_OPERATOR VALUE_VALUE_GENERIC_TYPE operator) { Objects.requireNonNull(operator); VALUE_TYPE state = EMPTY_VALUE; boolean empty = true; @@ -2599,15 +2606,15 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G } state = operator.APPLY_VALUE(state, entry.value); } - return state; + return empty ? VALUE_OPTIONAL.empty() : VALUE_OPTIONAL.GET_OPTIONAL_VALUE(state); } @Override - public VALUE_TYPE findFirst(VALUE_PREDICATE VALUE_GENERIC_TYPE filter) { + public VALUE_OPTIONAL VALUE_GENERIC_TYPE findFirst(VALUE_PREDICATE VALUE_GENERIC_TYPE filter) { Objects.requireNonNull(filter); for(Node KEY_VALUE_GENERIC_TYPE entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return EMPTY_VALUE; + if(filter.test(entry.value)) return VALUE_OPTIONAL.GET_OPTIONAL_VALUE(entry.value); + return VALUE_OPTIONAL.empty(); } @Override @@ -2753,7 +2760,7 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G } @Override - public MAP.Entry KEY_VALUE_GENERIC_TYPE reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); MAP.Entry KEY_VALUE_GENERIC_TYPE state = null; boolean empty = true; @@ -2765,19 +2772,19 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G } state = operator.apply(state, new BasicEntryKV_BRACES(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public MAP.Entry KEY_VALUE_GENERIC_TYPE findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry KEY_VALUE_GENERIC_TYPE subEntry = new BasicEntryKV_BRACES(); for(Node KEY_VALUE_GENERIC_TYPE entry = first;entry != null;entry = entry.next()) { subEntry.set(entry.key, entry.value); - if(filter.test(subEntry)) return subEntry; + if(filter.test(subEntry)) return Optional.ofNullable(subEntry); } - return null; + return Optional.empty(); } @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 0b9947c..b60eed8 100644 --- a/src/builder/resources/speiger/assets/collections/templates/queues/ArrayFIFOQueue.template +++ b/src/builder/resources/speiger/assets/collections/templates/queues/ArrayFIFOQueue.template @@ -7,12 +7,18 @@ import java.util.function.Consumer; import java.util.function.BiFunction; #endif import java.util.Objects; +#if JDK_TYPE +import java.util.OPTIONAL; +#endif import java.util.NoSuchElementException; #if JDK_FUNCTION import java.util.function.PREDICATE; #endif import speiger.src.collections.PACKAGE.collections.ITERATOR; +#if !JDK_TYPE +import speiger.src.collections.PACKAGE.functions.OPTIONAL; +#endif #if !TYPE_OBJECT import speiger.src.collections.PACKAGE.functions.COMPARATOR; import speiger.src.collections.PACKAGE.functions.CONSUMER; @@ -318,17 +324,17 @@ public class ARRAY_FIFO_QUEUE KEY_GENERIC_TYPE extends ABSTRACT_PRIORITY_QUEUE K } @Override - public KEY_TYPE findFirst(PREDICATE KEY_GENERIC_TYPE filter) { + public OPTIONAL KEY_GENERIC_TYPE findFirst(PREDICATE KEY_GENERIC_TYPE filter) { Objects.requireNonNull(filter); for(int i = 0,m=size();i=0;i--) { - if(!strategy.equals(keys[i], EMPTY_KEY_VALUE) && filter.test(keys[i])) return keys[i]; + if(!strategy.equals(keys[i], EMPTY_KEY_VALUE) && filter.test(keys[i])) return OPTIONAL.GET_OPTIONAL(keys[i]); } - return EMPTY_VALUE; + return OPTIONAL.empty(); } @Override 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 d462181..0756904 100644 --- a/src/builder/resources/speiger/assets/collections/templates/sets/OpenHashSet.template +++ b/src/builder/resources/speiger/assets/collections/templates/sets/OpenHashSet.template @@ -6,6 +6,9 @@ import java.util.ConcurrentModificationException; import java.util.Iterator; import java.util.NoSuchElementException; import java.util.Objects; +#if JDK_TYPE +import java.util.OPTIONAL; +#endif #if TYPE_OBJECT import java.util.function.Consumer; import java.util.function.BiFunction; @@ -20,6 +23,9 @@ import speiger.src.collections.PACKAGE.collections.ITERATOR; import speiger.src.collections.PACKAGE.utils.ITERATORS; import speiger.src.collections.PACKAGE.functions.CONSUMER; #endif +#if !JDK_TYPE +import speiger.src.collections.PACKAGE.functions.OPTIONAL; +#endif import speiger.src.collections.ints.functions.consumer.BI_FROM_INT_CONSUMER; import speiger.src.collections.objects.functions.consumer.BI_FROM_OBJECT_CONSUMER; #if !JDK_FUNCTION @@ -498,7 +504,7 @@ public class HASH_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE imp #endif @Override - public KEY_TYPE reduce(UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) { + public OPTIONAL KEY_GENERIC_TYPE reduce(UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) { Objects.requireNonNull(operator); KEY_TYPE state = EMPTY_VALUE; boolean empty = true; @@ -515,18 +521,18 @@ public class HASH_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE imp } state = operator.APPLY_VALUE(state, keys[i]); } - return state; + return empty ? OPTIONAL.empty() : OPTIONAL.GET_OPTIONAL(state); } @Override - public KEY_TYPE findFirst(PREDICATE KEY_GENERIC_TYPE filter) { + public OPTIONAL KEY_GENERIC_TYPE findFirst(PREDICATE KEY_GENERIC_TYPE filter) { Objects.requireNonNull(filter); - if(size() <= 0) return EMPTY_VALUE; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OPTIONAL.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OPTIONAL.GET_OPTIONAL(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(KEY_EQUALS_NOT_NULL(keys[i]) && filter.test(keys[i])) return keys[i]; + if(KEY_EQUALS_NOT_NULL(keys[i]) && filter.test(keys[i])) return OPTIONAL.GET_OPTIONAL(keys[i]); } - return EMPTY_VALUE; + return OPTIONAL.empty(); } @Override 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 987eefa..27b2008 100644 --- a/src/builder/resources/speiger/assets/collections/templates/sets/RBTreeSet.template +++ b/src/builder/resources/speiger/assets/collections/templates/sets/RBTreeSet.template @@ -2,6 +2,9 @@ package speiger.src.collections.PACKAGE.sets; import java.util.Collection; import java.util.Collections; +#if JDK_TYPE +import java.util.OPTIONAL; +#endif #if TYPE_OBJECT import java.util.Comparator; import java.util.function.Consumer; @@ -19,6 +22,9 @@ import speiger.src.collections.PACKAGE.collections.BI_ITERATOR; import speiger.src.collections.PACKAGE.functions.COMPARATOR; import speiger.src.collections.PACKAGE.functions.CONSUMER; #endif +#if !JDK_TYPE +import speiger.src.collections.PACKAGE.functions.OPTIONAL; +#endif import speiger.src.collections.ints.functions.consumer.BI_FROM_INT_CONSUMER; import speiger.src.collections.objects.functions.consumer.BI_FROM_OBJECT_CONSUMER; #if !JDK_FUNCTION @@ -427,7 +433,7 @@ public class RB_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE #endif @Override - public KEY_TYPE reduce(UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) { + public OPTIONAL KEY_GENERIC_TYPE reduce(UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) { Objects.requireNonNull(operator); KEY_TYPE state = EMPTY_VALUE; boolean empty = true; @@ -439,16 +445,16 @@ public class RB_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE } state = operator.APPLY_VALUE(state, entry.key); } - return state; + return empty ? OPTIONAL.empty() : OPTIONAL.GET_OPTIONAL(state); } @Override - public KEY_TYPE findFirst(PREDICATE KEY_GENERIC_TYPE filter) { + public OPTIONAL KEY_GENERIC_TYPE findFirst(PREDICATE KEY_GENERIC_TYPE filter) { Objects.requireNonNull(filter); for(Entry KEY_GENERIC_TYPE entry = first;entry != null;entry = entry.next()) { - if(filter.test(entry.key)) return entry.key; + if(filter.test(entry.key)) return OPTIONAL.GET_OPTIONAL(entry.key); } - return EMPTY_VALUE; + return OPTIONAL.empty(); } @Override @@ -1374,7 +1380,7 @@ public class RB_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE #endif @Override - public KEY_TYPE reduce(UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) { + public OPTIONAL KEY_GENERIC_TYPE reduce(UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) { Objects.requireNonNull(operator); KEY_TYPE state = EMPTY_VALUE; boolean empty = true; @@ -1386,16 +1392,16 @@ public class RB_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE } state = operator.APPLY_VALUE(state, entry.key); } - return state; + return empty ? OPTIONAL.empty() : OPTIONAL.GET_OPTIONAL(state); } @Override - public KEY_TYPE findFirst(PREDICATE KEY_GENERIC_TYPE filter) { + public OPTIONAL KEY_GENERIC_TYPE findFirst(PREDICATE KEY_GENERIC_TYPE filter) { Objects.requireNonNull(filter); for(Entry KEY_GENERIC_TYPE entry = start();entry != null && inRange(entry.key);entry = next(entry)) { - if(filter.test(entry.key)) return entry.key; + if(filter.test(entry.key)) return OPTIONAL.GET_OPTIONAL(entry.key); } - return EMPTY_VALUE; + return OPTIONAL.empty(); } @Override 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 200f46c..b57980a 100644 --- a/src/builder/resources/speiger/assets/collections/templates/utils/Collections.template +++ b/src/builder/resources/speiger/assets/collections/templates/utils/Collections.template @@ -8,6 +8,9 @@ import java.util.ConcurrentModificationException; import java.util.Iterator; import java.util.NoSuchElementException; import java.util.Objects; +#if JDK_TYPE +import java.util.OPTIONAL; +#endif #if TYPE_OBJECT import java.util.Comparator; import java.util.function.BiFunction; @@ -30,6 +33,9 @@ import speiger.src.collections.PACKAGE.collections.ITERATOR; #if !TYPE_OBJECT import speiger.src.collections.PACKAGE.functions.COMPARATOR; #endif +#if !JDK_TYPE +import speiger.src.collections.PACKAGE.functions.OPTIONAL; +#endif #if !TYPE_OBJECT import speiger.src.collections.objects.utils.ObjectArrays; import speiger.src.collections.PACKAGE.functions.CONSUMER; @@ -869,9 +875,9 @@ public class COLLECTIONS public KEY_TYPE reduce(KEY_TYPE identity, UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) { synchronized(mutex) { return c.reduce(identity, operator); } } #endif @Override - public KEY_TYPE reduce(UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) { synchronized(mutex) { return c.reduce(operator); } } + public OPTIONAL KEY_GENERIC_TYPE reduce(UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) { synchronized(mutex) { return c.reduce(operator); } } @Override - public KEY_TYPE findFirst(PREDICATE KEY_GENERIC_TYPE filter) { synchronized(mutex) { return c.findFirst(filter); } } + public OPTIONAL KEY_GENERIC_TYPE findFirst(PREDICATE KEY_GENERIC_TYPE filter) { synchronized(mutex) { return c.findFirst(filter); } } @Override public int count(PREDICATE KEY_GENERIC_TYPE filter) { synchronized(mutex) { return c.count(filter); } } } @@ -1021,9 +1027,9 @@ public class COLLECTIONS public KEY_TYPE reduce(KEY_TYPE identity, UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) { return c.reduce(identity, operator); } #endif @Override - public KEY_TYPE reduce(UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) { return c.reduce(operator); } + public OPTIONAL KEY_GENERIC_TYPE reduce(UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) { return c.reduce(operator); } @Override - public KEY_TYPE findFirst(PREDICATE KEY_GENERIC_TYPE filter) { return c.findFirst(filter); } + public OPTIONAL KEY_GENERIC_TYPE findFirst(PREDICATE KEY_GENERIC_TYPE filter) { return c.findFirst(filter); } @Override public int count(PREDICATE KEY_GENERIC_TYPE filter) { return c.count(filter); } } diff --git a/src/builder/resources/speiger/assets/collections/templates/utils/PriorityQueues.template b/src/builder/resources/speiger/assets/collections/templates/utils/PriorityQueues.template index d83a50d..6701f11 100644 --- a/src/builder/resources/speiger/assets/collections/templates/utils/PriorityQueues.template +++ b/src/builder/resources/speiger/assets/collections/templates/utils/PriorityQueues.template @@ -4,16 +4,22 @@ package speiger.src.collections.PACKAGE.utils; import java.util.Collection; import java.util.Comparator; import java.util.function.Consumer; +import java.util.function.BiFunction; #endif #if JDK_FUNCTION import java.util.function.PREDICATE; #endif - +#if JDK_TYPE +import java.util.OPTIONAL; +#endif import speiger.src.collections.PACKAGE.collections.ITERATOR; import speiger.src.collections.PACKAGE.collections.COLLECTION; #if !TYPE_OBJECT import speiger.src.collections.PACKAGE.functions.COMPARATOR; #endif +#if !JDK_TYPE +import speiger.src.collections.PACKAGE.functions.OPTIONAL; +#endif #if DEQUEUE_FEATURE import speiger.src.collections.PACKAGE.queues.PRIORITY_DEQUEUE; #endif @@ -24,6 +30,7 @@ import speiger.src.collections.PACKAGE.functions.CONSUMER; #if !JDK_FUNCTION import speiger.src.collections.PACKAGE.functions.function.PREDICATE; #endif +import speiger.src.collections.PACKAGE.functions.function.UNARY_OPERATOR; import speiger.src.collections.objects.functions.consumer.BI_FROM_OBJECT_CONSUMER; /** @@ -140,7 +147,15 @@ public class PRIORITY_QUEUES @Override public boolean matchesAll(PREDICATE KEY_GENERIC_TYPE filter) { synchronized(mutex) { return queue.matchesAll(filter); } } @Override - public KEY_TYPE findFirst(PREDICATE KEY_GENERIC_TYPE filter) { synchronized(mutex) { return queue.findFirst(filter); } } + public OPTIONAL KEY_GENERIC_TYPE findFirst(PREDICATE KEY_GENERIC_TYPE filter) { synchronized(mutex) { return queue.findFirst(filter); } } + #if TYPE_OBJECT + public KEY_SPECIAL_TYPE reduce(KEY_SPECIAL_TYPE identity, BiFunction operator) { synchronized(mutex) { return queue.reduce(identity, operator); } } +#else + @Override + public KEY_TYPE reduce(KEY_TYPE identity, UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) { synchronized(mutex) { return queue.reduce(identity, operator); } } +#endif + @Override + public OPTIONAL KEY_GENERIC_TYPE reduce(UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) { synchronized(mutex) { return queue.reduce(operator); } } @Override public int count(PREDICATE KEY_GENERIC_TYPE filter) { synchronized(mutex) { return queue.count(filter); } } } diff --git a/src/builder/resources/speiger/assets/testers/templates/tests/collection/CollectionForEachTester.template b/src/builder/resources/speiger/assets/testers/templates/tests/collection/CollectionForEachTester.template index 3bfcf89..d011ef1 100644 --- a/src/builder/resources/speiger/assets/testers/templates/tests/collection/CollectionForEachTester.template +++ b/src/builder/resources/speiger/assets/testers/templates/tests/collection/CollectionForEachTester.template @@ -25,6 +25,15 @@ public class FILE_KEY_TYPECollectionForEachTester KEY_GENERIC_TYPE extends ABSTR collection.forEach(elements::add); HELPERS.assertContentsAnyOrder(elements, createSamplesArray()); } + +#ignore + @CollectionFeature.Require(absent = KNOWN_ORDER) + public void testIndexedForEachUnknownOrder() { +#endignore + LIST KEY_GENERIC_TYPE elements = new ARRAY_LISTBRACES(); + collection.forEachIndexed((K, V) -> elements.add(V)); + HELPERS.assertContentsAnyOrder(elements, createSamplesArray()); + } #ignore @CollectionFeature.Require(absent = KNOWN_ORDER) @@ -43,6 +52,15 @@ public class FILE_KEY_TYPECollectionForEachTester KEY_GENERIC_TYPE extends ABSTR collection.forEach(elements::add); assertEquals("Different ordered iteration", HELPERS.copyToList(getOrderedElements()), elements); } + +#ignore + @CollectionFeature.Require(KNOWN_ORDER) + public void testIndexedForEachKnownOrder() { +#endignore + LIST KEY_GENERIC_TYPE elements = new ARRAY_LISTBRACES(); + collection.forEachIndexed((K, V) -> elements.add(V)); + assertEquals("Different ordered iteration", HELPERS.copyToList(getOrderedElements()), elements); + } #ignore @CollectionFeature.Require(KNOWN_ORDER) diff --git a/src/builder/resources/speiger/assets/testers/templates/tests/iterable/IterableFindFirstTester.template b/src/builder/resources/speiger/assets/testers/templates/tests/iterable/IterableFindFirstTester.template index c84d013..4de8d9a 100644 --- a/src/builder/resources/speiger/assets/testers/templates/tests/iterable/IterableFindFirstTester.template +++ b/src/builder/resources/speiger/assets/testers/templates/tests/iterable/IterableFindFirstTester.template @@ -4,6 +4,12 @@ package speiger.src.testers.PACKAGE.tests.iterable; import java.util.Objects; #endif +#if JDK_TYPE +import java.util.OPTIONAL; +#else +import speiger.src.collections.PACKAGE.functions.OPTIONAL; +#endif + import org.junit.Ignore; import com.google.common.collect.testing.features.CollectionSize; @@ -27,17 +33,17 @@ public class FILE_KEY_TYPEIterableFindFirstTester KEY_GENERIC_TYPE extends ABSTR @CollectionSize.Require(absent = CollectionSize.ZERO) #endignore public void testIterableFindFirst_FindFirstElements() { - assertEquals("First Element should be found", e0(), container.findFirst(T -> KEY_EQUALS(T, e0()))); + assertEquals("First Element should be found", e0(), container.findFirst(T -> KEY_EQUALS(T, e0())).SUPPLY_GET()); } public void testIterableFindFirst_FindNothing() { - assertEquals("No element should be found", EMPTY_KEY_VALUE, container.findFirst(T -> KEY_EQUALS(T, e4()))); + assertEquals("No element should be found", OPTIONAL.empty(), container.findFirst(T -> KEY_EQUALS(T, e4()))); } #ignore @CollectionSize.Require(CollectionSize.SEVERAL) #endignore public void testIterableFindFirst_FindLastElement() { - assertEquals("Last Element should be found", e2(), container.findFirst(T -> KEY_EQUALS(T, e2()))); + assertEquals("Last Element should be found", e2(), container.findFirst(T -> KEY_EQUALS(T, e2())).SUPPLY_GET()); } } diff --git a/src/builder/resources/speiger/assets/testers/templates/tests/iterable/IterableReduceTester.template b/src/builder/resources/speiger/assets/testers/templates/tests/iterable/IterableReduceTester.template index 4335ede..09e09cb 100644 --- a/src/builder/resources/speiger/assets/testers/templates/tests/iterable/IterableReduceTester.template +++ b/src/builder/resources/speiger/assets/testers/templates/tests/iterable/IterableReduceTester.template @@ -3,9 +3,16 @@ package speiger.src.testers.PACKAGE.tests.iterable; #if TYPE_OBJECT import java.util.Objects; +#endif +#if JDK_TYPE +import java.util.OPTIONAL; +#else +import speiger.src.collections.PACKAGE.functions.OPTIONAL; #endif import org.junit.Ignore; +import com.google.common.collect.testing.features.CollectionSize; + import speiger.src.testers.PACKAGE.tests.base.ABSTRACT_COLLECTION_TESTER; @Ignore @@ -29,7 +36,7 @@ public class FILE_KEY_TYPEIterableReduceTester KEY_GENERIC_TYPE extends ABSTRACT } public void testIterableReduce() { - assertEquals("The sum of the collection should match", getSum(), collection.reduce(this::sum)); + assertEquals("The sum of the collection should match", size == CollectionSize.ZERO ? OPTIONAL.empty() : OPTIONAL.GET_OPTIONAL(getSum()), collection.reduce(this::sum)); } public void testIterableExtraReduce() { diff --git a/src/main/java/speiger/src/collections/booleans/functions/OptionalBoolean.java b/src/main/java/speiger/src/collections/booleans/functions/OptionalBoolean.java new file mode 100644 index 0000000..94988af --- /dev/null +++ b/src/main/java/speiger/src/collections/booleans/functions/OptionalBoolean.java @@ -0,0 +1,89 @@ +package speiger.src.collections.booleans.functions; + +import java.util.NoSuchElementException; +import java.util.function.Supplier; + +public final class OptionalBoolean { + private static final OptionalBoolean EMPTY = new OptionalBoolean(); + + private final boolean isPresent; + private final boolean value; + + private OptionalBoolean() { + this.isPresent = false; + this.value = false; + } + + private OptionalBoolean(boolean value) { + this.isPresent = true; + this.value = value; + } + + public static OptionalBoolean empty() { + return EMPTY; + } + + public static OptionalBoolean of(boolean value) { + return new OptionalBoolean(value); + } + + public boolean getAsBoolean() { + if(!isPresent) throw new NoSuchElementException("No value present"); + return value; + } + + public boolean isPresent() { + return isPresent; + } + + public boolean isEmpty() { + return !isPresent; + } + + public void ifPresent(BooleanConsumer consumer) { + if(isPresent) consumer.accept(value); + } + + public void ifPresentOrElse(BooleanConsumer action, Runnable emptyAction) { + if (isPresent) action.accept(value); + else emptyAction.run(); + } + + public boolean orElse(boolean other) { + return isPresent ? value : other; + } + + public boolean orElseGet(BooleanSupplier other) { + return isPresent ? value : other.getAsBoolean(); + } + + public boolean orElseThrow() { + if (!isPresent) throw new NoSuchElementException("No value present"); + return value; + } + + public boolean orElseThrow(Supplier exceptionSupplier) throws X { + if(isPresent) return value; + else throw exceptionSupplier.get(); + } + + @Override + public boolean equals(Object obj) { + if(obj == this) return true; + if(obj instanceof OptionalBoolean) { + OptionalBoolean other = (OptionalBoolean)obj; + return (isPresent && other.isPresent ? value == other.value : isPresent == other.isPresent); + } + return false; + } + + @Override + public int hashCode() { + return isPresent ? Boolean.hashCode(value) : 0; + } + + @Override + public String toString() { + return isPresent ? "OptionalBoolean["+value+"]" : "OptionalBoolean.empty"; + } +} \ No newline at end of file diff --git a/src/main/java/speiger/src/collections/bytes/functions/OptionalByte.java b/src/main/java/speiger/src/collections/bytes/functions/OptionalByte.java new file mode 100644 index 0000000..44f3a15 --- /dev/null +++ b/src/main/java/speiger/src/collections/bytes/functions/OptionalByte.java @@ -0,0 +1,89 @@ +package speiger.src.collections.bytes.functions; + +import java.util.NoSuchElementException; +import java.util.function.Supplier; + +public final class OptionalByte { + private static final OptionalByte EMPTY = new OptionalByte(); + + private final boolean isPresent; + private final byte value; + + private OptionalByte() { + this.isPresent = false; + this.value = (byte)0; + } + + private OptionalByte(byte value) { + this.isPresent = true; + this.value = value; + } + + public static OptionalByte empty() { + return EMPTY; + } + + public static OptionalByte of(byte value) { + return new OptionalByte(value); + } + + public byte getAsByte() { + if(!isPresent) throw new NoSuchElementException("No value present"); + return value; + } + + public boolean isPresent() { + return isPresent; + } + + public boolean isEmpty() { + return !isPresent; + } + + public void ifPresent(ByteConsumer consumer) { + if(isPresent) consumer.accept(value); + } + + public void ifPresentOrElse(ByteConsumer action, Runnable emptyAction) { + if (isPresent) action.accept(value); + else emptyAction.run(); + } + + public byte orElse(byte other) { + return isPresent ? value : other; + } + + public byte orElseGet(ByteSupplier other) { + return isPresent ? value : other.getAsByte(); + } + + public byte orElseThrow() { + if (!isPresent) throw new NoSuchElementException("No value present"); + return value; + } + + public byte orElseThrow(Supplier exceptionSupplier) throws X { + if(isPresent) return value; + else throw exceptionSupplier.get(); + } + + @Override + public boolean equals(Object obj) { + if(obj == this) return true; + if(obj instanceof OptionalByte) { + OptionalByte other = (OptionalByte)obj; + return (isPresent && other.isPresent ? value == other.value : isPresent == other.isPresent); + } + return false; + } + + @Override + public int hashCode() { + return isPresent ? Byte.hashCode(value) : 0; + } + + @Override + public String toString() { + return isPresent ? "OptionalByte["+value+"]" : "OptionalByte.empty"; + } +} \ No newline at end of file diff --git a/src/main/java/speiger/src/collections/chars/functions/OptionalChar.java b/src/main/java/speiger/src/collections/chars/functions/OptionalChar.java new file mode 100644 index 0000000..63e0135 --- /dev/null +++ b/src/main/java/speiger/src/collections/chars/functions/OptionalChar.java @@ -0,0 +1,89 @@ +package speiger.src.collections.chars.functions; + +import java.util.NoSuchElementException; +import java.util.function.Supplier; + +public final class OptionalChar { + private static final OptionalChar EMPTY = new OptionalChar(); + + private final boolean isPresent; + private final char value; + + private OptionalChar() { + this.isPresent = false; + this.value = (char)0; + } + + private OptionalChar(char value) { + this.isPresent = true; + this.value = value; + } + + public static OptionalChar empty() { + return EMPTY; + } + + public static OptionalChar of(char value) { + return new OptionalChar(value); + } + + public char getAsChar() { + if(!isPresent) throw new NoSuchElementException("No value present"); + return value; + } + + public boolean isPresent() { + return isPresent; + } + + public boolean isEmpty() { + return !isPresent; + } + + public void ifPresent(CharConsumer consumer) { + if(isPresent) consumer.accept(value); + } + + public void ifPresentOrElse(CharConsumer action, Runnable emptyAction) { + if (isPresent) action.accept(value); + else emptyAction.run(); + } + + public char orElse(char other) { + return isPresent ? value : other; + } + + public char orElseGet(CharSupplier other) { + return isPresent ? value : other.getAsChar(); + } + + public char orElseThrow() { + if (!isPresent) throw new NoSuchElementException("No value present"); + return value; + } + + public char orElseThrow(Supplier exceptionSupplier) throws X { + if(isPresent) return value; + else throw exceptionSupplier.get(); + } + + @Override + public boolean equals(Object obj) { + if(obj == this) return true; + if(obj instanceof OptionalChar) { + OptionalChar other = (OptionalChar)obj; + return (isPresent && other.isPresent ? value == other.value : isPresent == other.isPresent); + } + return false; + } + + @Override + public int hashCode() { + return isPresent ? Character.hashCode(value) : 0; + } + + @Override + public String toString() { + return isPresent ? "OptionalChar["+value+"]" : "OptionalChar.empty"; + } +} \ No newline at end of file diff --git a/src/main/java/speiger/src/collections/floats/functions/OptionalFloat.java b/src/main/java/speiger/src/collections/floats/functions/OptionalFloat.java new file mode 100644 index 0000000..0a6b2f7 --- /dev/null +++ b/src/main/java/speiger/src/collections/floats/functions/OptionalFloat.java @@ -0,0 +1,89 @@ +package speiger.src.collections.floats.functions; + +import java.util.NoSuchElementException; +import java.util.function.Supplier; + +public final class OptionalFloat { + private static final OptionalFloat EMPTY = new OptionalFloat(); + + private final boolean isPresent; + private final float value; + + private OptionalFloat() { + this.isPresent = false; + this.value = 0F; + } + + private OptionalFloat(float value) { + this.isPresent = true; + this.value = value; + } + + public static OptionalFloat empty() { + return EMPTY; + } + + public static OptionalFloat of(float value) { + return new OptionalFloat(value); + } + + public float getAsFloat() { + if(!isPresent) throw new NoSuchElementException("No value present"); + return value; + } + + public boolean isPresent() { + return isPresent; + } + + public boolean isEmpty() { + return !isPresent; + } + + public void ifPresent(FloatConsumer consumer) { + if(isPresent) consumer.accept(value); + } + + public void ifPresentOrElse(FloatConsumer action, Runnable emptyAction) { + if (isPresent) action.accept(value); + else emptyAction.run(); + } + + public float orElse(float other) { + return isPresent ? value : other; + } + + public float orElseGet(FloatSupplier other) { + return isPresent ? value : other.getAsFloat(); + } + + public float orElseThrow() { + if (!isPresent) throw new NoSuchElementException("No value present"); + return value; + } + + public float orElseThrow(Supplier exceptionSupplier) throws X { + if(isPresent) return value; + else throw exceptionSupplier.get(); + } + + @Override + public boolean equals(Object obj) { + if(obj == this) return true; + if(obj instanceof OptionalFloat) { + OptionalFloat other = (OptionalFloat)obj; + return (isPresent && other.isPresent ? Float.floatToIntBits(value) == Float.floatToIntBits(other.value) : isPresent == other.isPresent); + } + return false; + } + + @Override + public int hashCode() { + return isPresent ? Float.hashCode(value) : 0; + } + + @Override + public String toString() { + return isPresent ? "OptionalFloat["+value+"]" : "OptionalFloat.empty"; + } +} \ No newline at end of file diff --git a/src/main/java/speiger/src/collections/shorts/functions/OptionalShort.java b/src/main/java/speiger/src/collections/shorts/functions/OptionalShort.java new file mode 100644 index 0000000..ac6b0ac --- /dev/null +++ b/src/main/java/speiger/src/collections/shorts/functions/OptionalShort.java @@ -0,0 +1,89 @@ +package speiger.src.collections.shorts.functions; + +import java.util.NoSuchElementException; +import java.util.function.Supplier; + +public final class OptionalShort { + private static final OptionalShort EMPTY = new OptionalShort(); + + private final boolean isPresent; + private final short value; + + private OptionalShort() { + this.isPresent = false; + this.value = (short)0; + } + + private OptionalShort(short value) { + this.isPresent = true; + this.value = value; + } + + public static OptionalShort empty() { + return EMPTY; + } + + public static OptionalShort of(short value) { + return new OptionalShort(value); + } + + public short getAsShort() { + if(!isPresent) throw new NoSuchElementException("No value present"); + return value; + } + + public boolean isPresent() { + return isPresent; + } + + public boolean isEmpty() { + return !isPresent; + } + + public void ifPresent(ShortConsumer consumer) { + if(isPresent) consumer.accept(value); + } + + public void ifPresentOrElse(ShortConsumer action, Runnable emptyAction) { + if (isPresent) action.accept(value); + else emptyAction.run(); + } + + public short orElse(short other) { + return isPresent ? value : other; + } + + public short orElseGet(ShortSupplier other) { + return isPresent ? value : other.getAsShort(); + } + + public short orElseThrow() { + if (!isPresent) throw new NoSuchElementException("No value present"); + return value; + } + + public short orElseThrow(Supplier exceptionSupplier) throws X { + if(isPresent) return value; + else throw exceptionSupplier.get(); + } + + @Override + public boolean equals(Object obj) { + if(obj == this) return true; + if(obj instanceof OptionalShort) { + OptionalShort other = (OptionalShort)obj; + return (isPresent && other.isPresent ? value == other.value : isPresent == other.isPresent); + } + return false; + } + + @Override + public int hashCode() { + return isPresent ? Short.hashCode(value) : 0; + } + + @Override + public String toString() { + return isPresent ? "OptionalShort["+value+"]" : "OptionalShort.empty"; + } +} \ No newline at end of file diff --git a/src/test/java/speiger/src/collections/ints/base/BaseIntIterableTest.java b/src/test/java/speiger/src/collections/ints/base/BaseIntIterableTest.java index 26387d3..26199eb 100644 --- a/src/test/java/speiger/src/collections/ints/base/BaseIntIterableTest.java +++ b/src/test/java/speiger/src/collections/ints/base/BaseIntIterableTest.java @@ -96,7 +96,7 @@ public abstract class BaseIntIterableTest public void testStreamFindFirst() { if(getValidIterableTests().contains(IterableTest.STREAM_FIND_FIRST)) { int expected = IntStream.of(TEST_ARRAY).filter(T -> T / 50 > 0).findFirst().getAsInt(); - int actual = create(TEST_ARRAY).findFirst(T -> T / 50 > 0); + int actual = create(TEST_ARRAY).findFirst(T -> T / 50 > 0).getAsInt(); Assert.assertEquals(expected, actual); } }