diff --git a/src/builder/java/speiger/src/builder/modules/MapModule.java b/src/builder/java/speiger/src/builder/modules/MapModule.java index 41dba85..87ce295 100644 --- a/src/builder/java/speiger/src/builder/modules/MapModule.java +++ b/src/builder/java/speiger/src/builder/modules/MapModule.java @@ -198,6 +198,7 @@ public class MapModule extends BaseModule addBiRequirement("SortedMapNavigationTester"); addBiRequirement("OrderedMapNavigationTester"); addBiRequirement("OrderedMapMoveTester"); + addBiRequirement("OrderedMapPutTester"); addBiRequirement("MapConstructorTester"); addRemapper("TestMapGenerator", "Test%sMapGenerator"); 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 b921b89..54fdcca 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 @@ -713,6 +713,10 @@ public class LINKED_CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends CUSTOM_HASH_M } private class MapEntrySet extends AbstractObjectSet implements ORDERED_MAP.FastOrderedSet KEY_VALUE_GENERIC_TYPE { + @Override + public void addFirst(MAP.Entry KEY_VALUE_GENERIC_TYPE o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(MAP.Entry KEY_VALUE_GENERIC_TYPE o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(MAP.Entry KEY_VALUE_GENERIC_TYPE o) { throw new UnsupportedOperationException(); } @Override @@ -1004,10 +1008,13 @@ public class LINKED_CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends CUSTOM_HASH_M public boolean add(KEY_TYPE o) { throw new UnsupportedOperationException(); } - + + @Override + public void addFirst(KEY_TYPE o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(KEY_TYPE o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(KEY_TYPE o) { throw new UnsupportedOperationException(); } - @Override public boolean addAndMoveToLast(KEY_TYPE o) { throw new UnsupportedOperationException(); } 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 9c27197..8cec210 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 @@ -717,6 +717,10 @@ public class LINKED_HASH_MAP KEY_VALUE_GENERIC_TYPE extends HASH_MAP KEY_VALUE_G } private class MapEntrySet extends AbstractObjectSet implements ORDERED_MAP.FastOrderedSet KEY_VALUE_GENERIC_TYPE { + @Override + public void addFirst(MAP.Entry KEY_VALUE_GENERIC_TYPE o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(MAP.Entry KEY_VALUE_GENERIC_TYPE o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(MAP.Entry KEY_VALUE_GENERIC_TYPE o) { throw new UnsupportedOperationException(); } @Override @@ -1003,6 +1007,12 @@ public class LINKED_HASH_MAP KEY_VALUE_GENERIC_TYPE extends HASH_MAP KEY_VALUE_G #endif @Override public boolean add(KEY_TYPE o) { throw new UnsupportedOperationException(); } + + @Override + public void addFirst(KEY_TYPE o) { throw new UnsupportedOperationException(); } + + @Override + public void addLast(KEY_TYPE o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(KEY_TYPE o) { throw new UnsupportedOperationException(); } 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 7269b24..c9936fe 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 @@ -637,6 +637,10 @@ public class IMMUTABLE_HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_ } private class MapEntrySet extends AbstractObjectSet implements ORDERED_MAP.FastOrderedSet KEY_VALUE_GENERIC_TYPE { + @Override + public void addFirst(MAP.Entry KEY_VALUE_GENERIC_TYPE o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(MAP.Entry KEY_VALUE_GENERIC_TYPE o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(MAP.Entry KEY_VALUE_GENERIC_TYPE o) { throw new UnsupportedOperationException(); } @Override @@ -894,6 +898,12 @@ public class IMMUTABLE_HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_ throw new UnsupportedOperationException(); } + @Override + public void addFirst(KEY_TYPE o) { throw new UnsupportedOperationException(); } + + @Override + public void addLast(KEY_TYPE o) { throw new UnsupportedOperationException(); } + @Override public boolean addAndMoveToFirst(KEY_TYPE o) { throw new UnsupportedOperationException(); } 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 9097ac9..b69a432 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 @@ -868,6 +868,10 @@ public class ARRAY_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_GEN } private class MapEntrySet extends AbstractObjectSet implements ORDERED_MAP.FastOrderedSet KEY_VALUE_GENERIC_TYPE { + @Override + public void addFirst(MAP.Entry KEY_VALUE_GENERIC_TYPE o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(MAP.Entry KEY_VALUE_GENERIC_TYPE o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(MAP.Entry KEY_VALUE_GENERIC_TYPE o) { throw new UnsupportedOperationException(); } @Override @@ -1126,6 +1130,10 @@ public class ARRAY_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_GEN @Override public boolean add(KEY_TYPE o) { throw new UnsupportedOperationException(); } @Override + public void addFirst(KEY_TYPE o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(KEY_TYPE o) { throw new UnsupportedOperationException(); } + @Override public boolean addAndMoveToFirst(KEY_TYPE o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToLast(KEY_TYPE o) { throw new UnsupportedOperationException(); } diff --git a/src/builder/resources/speiger/assets/collections/templates/maps/impl/misc/LinkedEnumMap.template b/src/builder/resources/speiger/assets/collections/templates/maps/impl/misc/LinkedEnumMap.template index 35a913a..0429062 100644 --- a/src/builder/resources/speiger/assets/collections/templates/maps/impl/misc/LinkedEnumMap.template +++ b/src/builder/resources/speiger/assets/collections/templates/maps/impl/misc/LinkedEnumMap.template @@ -473,6 +473,10 @@ public class LINKED_ENUM_MAP KEY_ENUM_VALUE_GENERIC_TYPE extends ENUM_MAP KEY_VA } private class MapEntrySet extends AbstractObjectSet implements ORDERED_MAP.FastOrderedSet KEY_VALUE_GENERIC_TYPE { + @Override + public void addFirst(MAP.Entry KEY_VALUE_GENERIC_TYPE o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(MAP.Entry KEY_VALUE_GENERIC_TYPE o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(MAP.Entry KEY_VALUE_GENERIC_TYPE o) { throw new UnsupportedOperationException(); } @Override @@ -637,7 +641,10 @@ public class LINKED_ENUM_MAP KEY_ENUM_VALUE_GENERIC_TYPE extends ENUM_MAP KEY_VA public boolean add(T o) { throw new UnsupportedOperationException(); } - + @Override + public void addFirst(T o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(T o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(T o) { throw new UnsupportedOperationException(); } diff --git a/src/builder/resources/speiger/assets/collections/templates/sets/AbstractSet.template b/src/builder/resources/speiger/assets/collections/templates/sets/AbstractSet.template index 468c4b8..c9b2b15 100644 --- a/src/builder/resources/speiger/assets/collections/templates/sets/AbstractSet.template +++ b/src/builder/resources/speiger/assets/collections/templates/sets/AbstractSet.template @@ -73,7 +73,17 @@ public abstract class ABSTRACT_SET KEY_GENERIC_TYPE extends ABSTRACT_COLLECTION public BI_ITERATOR KEY_GENERIC_TYPE reverseIterator() { return set.iterator(); } - + + @Override + public void addFirst(KEY_TYPE o) { + set.addLast(o); + } + + @Override + public void addLast(KEY_TYPE o) { + set.addFirst(o); + } + @Override #if !TYPE_OBJECT public boolean remove(KEY_TYPE o) { 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 eacee22..74558f6 100644 --- a/src/builder/resources/speiger/assets/collections/templates/sets/ArraySet.template +++ b/src/builder/resources/speiger/assets/collections/templates/sets/ArraySet.template @@ -153,6 +153,25 @@ public class ARRAY_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE im } #endif + @Override + public void addFirst(KEY_TYPE o) { + int index = findIndex(o); + if(index == -1) { + if(data.length == size) data = Arrays.copyOf(data, size == 0 ? 2 : size * 2); + System.arraycopy(data, 0, data, 1, size++); + data[0] = o; + } + } + + @Override + public void addLast(KEY_TYPE o) { + int index = findIndex(o); + if(index == -1) { + if(data.length == size) data = Arrays.copyOf(data, size == 0 ? 2 : size * 2); + data[size++] = o; + } + } + @Override public boolean addAndMoveToFirst(KEY_TYPE o) { int index = findIndex(o); diff --git a/src/builder/resources/speiger/assets/collections/templates/sets/ImmutableOpenHashSet.template b/src/builder/resources/speiger/assets/collections/templates/sets/ImmutableOpenHashSet.template index d2fcd4e..ba70686 100644 --- a/src/builder/resources/speiger/assets/collections/templates/sets/ImmutableOpenHashSet.template +++ b/src/builder/resources/speiger/assets/collections/templates/sets/ImmutableOpenHashSet.template @@ -264,6 +264,10 @@ public class IMMUTABLE_HASH_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERI @Override public boolean addAll(COLLECTION KEY_GENERIC_TYPE c) { throw new UnsupportedOperationException(); } @Override + public void addFirst(KEY_TYPE o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(KEY_TYPE o) { throw new UnsupportedOperationException(); } + @Override public boolean addAndMoveToFirst(KEY_TYPE o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToLast(KEY_TYPE o) { throw new UnsupportedOperationException(); } 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 b9950b0..de92850 100644 --- a/src/builder/resources/speiger/assets/collections/templates/sets/LinkedOpenCustomHashSet.template +++ b/src/builder/resources/speiger/assets/collections/templates/sets/LinkedOpenCustomHashSet.template @@ -241,6 +241,46 @@ public class LINKED_CUSTOM_HASH_SET KEY_GENERIC_TYPE extends CUSTOM_HASH_SET KEY } #endif + @Override + public void addFirst(KEY_TYPE o) { + if(strategy.equals(o, EMPTY_KEY_VALUE)) { + if(containsNull) return; + containsNull = true; + onNodeAdded(nullIndex); + moveToFirstIndex(nullIndex); + } + else { + int pos = HashUtil.mix(strategy.hashCode(o)) & mask; + while(!strategy.equals(keys[pos], EMPTY_KEY_VALUE)) { + if(strategy.equals(keys[pos], o)) return; + pos = ++pos & mask; + } + keys[pos] = o; + onNodeAdded(pos); + moveToFirstIndex(pos); + } + if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor)); + } + + @Override + public void addLast(KEY_TYPE o) { + if(strategy.equals(o, EMPTY_KEY_VALUE)) { + if(containsNull) return; + containsNull = true; + onNodeAdded(nullIndex); + } + else { + int pos = HashUtil.mix(strategy.hashCode(o)) & mask; + while(!strategy.equals(keys[pos], EMPTY_KEY_VALUE)) { + if(strategy.equals(keys[pos], o)) return; + pos = ++pos & mask; + } + keys[pos] = o; + onNodeAdded(pos); + } + if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor)); + } + @Override public boolean addAndMoveToFirst(KEY_TYPE o) { if(strategy.equals(o, EMPTY_KEY_VALUE)) { 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 8247c8d..81f4244 100644 --- a/src/builder/resources/speiger/assets/collections/templates/sets/LinkedOpenHashSet.template +++ b/src/builder/resources/speiger/assets/collections/templates/sets/LinkedOpenHashSet.template @@ -212,6 +212,46 @@ public class LINKED_HASH_SET KEY_GENERIC_TYPE extends HASH_SET KEY_GENERIC_TYPE } #endif + @Override + public void addFirst(KEY_TYPE o) { + if(KEY_EQUALS_NULL(o)) { + if(containsNull) return; + containsNull = true; + onNodeAdded(nullIndex); + moveToFirstIndex(nullIndex); + } + else { + int pos = HashUtil.mix(KEY_TO_HASH(o)) & mask; + while(KEY_EQUALS_NOT_NULL(keys[pos])) { + if(KEY_EQUALS(keys[pos], o)) return; + pos = ++pos & mask; + } + keys[pos] = o; + onNodeAdded(pos); + moveToFirstIndex(pos); + } + if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor)); + } + + @Override + public void addLast(KEY_TYPE o) { + if(KEY_EQUALS_NULL(o)) { + if(containsNull) return; + containsNull = true; + onNodeAdded(nullIndex); + } + else { + int pos = HashUtil.mix(KEY_TO_HASH(o)) & mask; + while(KEY_EQUALS_NOT_NULL(keys[pos])) { + if(KEY_EQUALS(keys[pos], o)) return; + pos = ++pos & mask; + } + keys[pos] = o; + onNodeAdded(pos); + } + if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor)); + } + @Override public boolean addAndMoveToFirst(KEY_TYPE o) { if(KEY_EQUALS_NULL(o)) { diff --git a/src/builder/resources/speiger/assets/collections/templates/sets/OrderedSet.template b/src/builder/resources/speiger/assets/collections/templates/sets/OrderedSet.template index 7a31a48..ce1791c 100644 --- a/src/builder/resources/speiger/assets/collections/templates/sets/OrderedSet.template +++ b/src/builder/resources/speiger/assets/collections/templates/sets/OrderedSet.template @@ -5,6 +5,7 @@ import java.util.SequencedSet; #endif import speiger.src.collections.PACKAGE.collections.BI_ITERATOR; +import speiger.src.collections.PACKAGE.collections.ORDERED_COLLECTION; #if SPLIT_ITERATOR_FEATURE import speiger.src.collections.PACKAGE.collections.SPLIT_ITERATOR; import speiger.src.collections.PACKAGE.utils.SPLIT_ITERATORS; @@ -23,9 +24,9 @@ import speiger.src.collections.PACKAGE.utils.SETS; * @Type(T) */ #if JAVA_VERSION>=21 -public interface ORDERED_SET KEY_GENERIC_TYPE extends SET KEY_GENERIC_TYPE, SequencedSet +public interface ORDERED_SET KEY_GENERIC_TYPE extends SET KEY_GENERIC_TYPE, ORDERED_COLLECTION KEY_GENERIC_TYPE, SequencedSet #else -public interface ORDERED_SET KEY_GENERIC_TYPE extends SET KEY_GENERIC_TYPE +public interface ORDERED_SET KEY_GENERIC_TYPE extends SET KEY_GENERIC_TYPE, ORDERED_COLLECTION KEY_GENERIC_TYPE #endif { /** diff --git a/src/builder/resources/speiger/assets/collections/templates/utils/Sets.template b/src/builder/resources/speiger/assets/collections/templates/utils/Sets.template index 78b0e8f..dc28f72 100644 --- a/src/builder/resources/speiger/assets/collections/templates/utils/Sets.template +++ b/src/builder/resources/speiger/assets/collections/templates/utils/Sets.template @@ -360,6 +360,10 @@ public class SETS s = c; } + @Override + public void addFirst(KEY_TYPE o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(KEY_TYPE o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(KEY_TYPE o) { throw new UnsupportedOperationException(); } @Override @@ -650,7 +654,11 @@ public class SETS super(c, mutex); s = c; } - + + @Override + public void addFirst(KEY_TYPE o) { synchronized(mutex) { s.addFirst(o); } } + @Override + public void addLast(KEY_TYPE o) { synchronized(mutex) { s.addLast(o); } } @Override public boolean addAndMoveToFirst(KEY_TYPE o) { synchronized(mutex) { return s.addAndMoveToFirst(o); } } @Override diff --git a/src/builder/resources/speiger/assets/collections/templates/utils/maps/Maps.template b/src/builder/resources/speiger/assets/collections/templates/utils/maps/Maps.template index 6bf3db4..7318693 100644 --- a/src/builder/resources/speiger/assets/collections/templates/utils/maps/Maps.template +++ b/src/builder/resources/speiger/assets/collections/templates/utils/maps/Maps.template @@ -754,7 +754,11 @@ public class MAPS super(c); set = c; } - + + @Override + public void addFirst(MAP.Entry KEY_VALUE_GENERIC_TYPE o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(MAP.Entry KEY_VALUE_GENERIC_TYPE o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(MAP.Entry KEY_VALUE_GENERIC_TYPE o) { throw new UnsupportedOperationException(); } @Override 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 5568ad5..03cc30b 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 @@ -20,6 +20,7 @@ import speiger.src.testers.PACKAGE.generators.maps.TEST_ORDERED_MAP_GENERATOR; import speiger.src.testers.PACKAGE.impl.maps.DERIVED_MAP_GENERATORS; import speiger.src.testers.PACKAGE.tests.maps.FILE_KEY_TYPE2FILE_VALUE_TYPEOrderedMapMoveTester; import speiger.src.testers.PACKAGE.tests.maps.FILE_KEY_TYPE2FILE_VALUE_TYPEOrderedMapNavigationTester; +import speiger.src.testers.PACKAGE.tests.maps.FILE_KEY_TYPE2FILE_VALUE_TYPEOrderedMapPutTester; #if !TYPE_OBJECT import speiger.src.testers.objects.builder.ObjectSetTestSuiteBuilder; import speiger.src.testers.objects.generators.TestObjectSetGenerator; @@ -41,6 +42,7 @@ public class ORDERED_MAP_TEST_BUILDER KEY_VALUE_GENERIC_TYPE extends MAP_TEST_BU List> testers = super.getTesters(); testers.add(FILE_KEY_TYPE2FILE_VALUE_TYPEOrderedMapMoveTester.class); testers.add(FILE_KEY_TYPE2FILE_VALUE_TYPEOrderedMapNavigationTester.class); + testers.add(FILE_KEY_TYPE2FILE_VALUE_TYPEOrderedMapPutTester.class); return testers; } diff --git a/src/builder/resources/speiger/assets/testers/templates/tests/maps/MapContainsTester.template b/src/builder/resources/speiger/assets/testers/templates/tests/maps/MapContainsTester.template index f6ccbb3..1a0ff8b 100644 --- a/src/builder/resources/speiger/assets/testers/templates/tests/maps/MapContainsTester.template +++ b/src/builder/resources/speiger/assets/testers/templates/tests/maps/MapContainsTester.template @@ -27,6 +27,7 @@ public class FILE_KEY_TYPE2FILE_VALUE_TYPEMapContainsTester KEY_VALUE_GENERIC_TY #ignore @CollectionSize.Require(absent = ZERO) + @SuppressWarnings("unlikely-arg-type") #endignore public void testContainsObject_yes() { assertTrue("contains(present) should return true", getMap().ENTRY_SET().contains(new AbstractMap.SimpleEntry<>(e0()))); @@ -36,6 +37,7 @@ public class FILE_KEY_TYPE2FILE_VALUE_TYPEMapContainsTester KEY_VALUE_GENERIC_TY assertFalse("contains(notPresent) should return false", getMap().ENTRY_SET().contains(e3())); } + @SuppressWarnings("unlikely-arg-type") public void testContainsObject_no() { assertFalse("contains(notPresent) should return false", getMap().ENTRY_SET().contains(new AbstractMap.SimpleEntry<>(e3()))); } diff --git a/src/builder/resources/speiger/assets/testers/templates/tests/maps/MapRemoveEntryTester.template b/src/builder/resources/speiger/assets/testers/templates/tests/maps/MapRemoveEntryTester.template index 80c5161..f08fe85 100644 --- a/src/builder/resources/speiger/assets/testers/templates/tests/maps/MapRemoveEntryTester.template +++ b/src/builder/resources/speiger/assets/testers/templates/tests/maps/MapRemoveEntryTester.template @@ -136,6 +136,7 @@ public class FILE_KEY_TYPE2FILE_VALUE_TYPEMapRemoveEntryTester KEY_VALUE_GENERIC #ignore @CollectionSize.Require(ONE) @MapFeature.Require(SUPPORTS_REMOVE) + @SuppressWarnings("unlikely-arg-type") #endignore public void testRemove_supportedObjectEntryPresent() { assertTrue(getMap().ENTRY_SET().remove(new AbstractMap.SimpleEntry<>(e0()))); @@ -154,6 +155,7 @@ public class FILE_KEY_TYPE2FILE_VALUE_TYPEMapRemoveEntryTester KEY_VALUE_GENERIC #ignore @CollectionSize.Require(ONE) @MapFeature.Require(SUPPORTS_REMOVE) + @SuppressWarnings("unlikely-arg-type") #endignore public void testRemove_supportedObjectEntryMissing() { assertFalse(getMap().ENTRY_SET().remove(new AbstractMap.SimpleEntry<>(e3()))); 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 new file mode 100644 index 0000000..4aa0596 --- /dev/null +++ b/src/builder/resources/speiger/assets/testers/templates/tests/maps/OrderedMapPutTester.template @@ -0,0 +1,122 @@ +package speiger.src.testers.PACKAGE.tests.maps; + +#ignore +import static com.google.common.collect.testing.features.CollectionSize.ZERO; +import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_PUT; +import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_REMOVE; +import static org.junit.Assert.assertNotEquals; +#endignore + +import com.google.common.collect.testing.features.CollectionSize; +import com.google.common.collect.testing.features.MapFeature; + +import speiger.src.collections.PACKAGE.maps.interfaces.MAP; +import speiger.src.collections.PACKAGE.maps.interfaces.ORDERED_MAP; +import speiger.src.collections.objects.lists.ObjectList; +import speiger.src.testers.PACKAGE.tests.base.maps.ABSTRACT_MAP_TESTER; +import speiger.src.testers.objects.utils.ObjectHelpers; + +@SuppressWarnings("javadoc") +public class FILE_KEY_TYPE2FILE_VALUE_TYPEOrderedMapPutTester KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP_TESTER KEY_VALUE_GENERIC_TYPE +{ + private ORDERED_MAP KEY_VALUE_GENERIC_TYPE orderedMap; + private ObjectList values; + private KEY_TYPE a; + private VALUE_TYPE aValue; + private VALUE_TYPE cValue; + + @Override + public void setUp() throws Exception { + super.setUp(); + orderedMap = (ORDERED_MAP KEY_VALUE_GENERIC_TYPE)getMap(); + values = ObjectHelpers.copyToList(getSampleElements(getSubjectGenerator().getCollectionSize().getNumElements())); + 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(); + } + } + } + +#ignore + @MapFeature.Require(SUPPORTS_PUT) + @CollectionSize.Require(absent = ZERO) +#endignore + public void testPutFirst() { + assertEquals(aValue, orderedMap.putFirst(a, cValue)); + assertNotEquals(cValue, orderedMap.FIRST_ENTRY_VALUE()); + assertEquals(orderedMap.getDefaultReturnValue(), orderedMap.putFirst(k4(), v4())); + assertNotEquals(v4(), orderedMap.FIRST_ENTRY_VALUE()); + assertEquals(e4(), orderedMap.firstEntry()); + } + + +#ignore + @MapFeature.Require(SUPPORTS_PUT) + @CollectionSize.Require(absent = ZERO) +#endignore + public void testPutLast() { + assertEquals(aValue, orderedMap.putLast(a, cValue)); + assertNotEquals(cValue, orderedMap.LAST_ENTRY_VALUE()); + assertEquals(orderedMap.getDefaultReturnValue(), orderedMap.putLast(k4(), v4())); + assertNotEquals(v4(), orderedMap.LAST_ENTRY_VALUE()); + assertEquals(e4(), orderedMap.lastEntry()); + } + +#ignore + @MapFeature.Require(SUPPORTS_REMOVE) + @CollectionSize.Require(CollectionSize.ONE) +#endignore + public void testPollFirst() { + assertFalse(orderedMap.isEmpty()); + assertEquals(e0(), orderedMap.pollFirstEntry()); + assertTrue(orderedMap.isEmpty()); + } + +#ignore + @MapFeature.Require(SUPPORTS_REMOVE) + @CollectionSize.Require(CollectionSize.ONE) +#endignore + public void testPollLast() { + assertFalse(orderedMap.isEmpty()); + assertEquals(e0(), orderedMap.pollLastEntry()); + assertTrue(orderedMap.isEmpty()); + } + +#ignore + @MapFeature.Require(absent = SUPPORTS_PUT) +#endignore + public void testUnsupportedPutFirst() { + try { orderedMap.putFirst(a, aValue); } + catch (UnsupportedOperationException tolerated) {} + expectUnchanged(); + } + +#ignore + @MapFeature.Require(absent = SUPPORTS_PUT) +#endignore + public void testUnsupportedPutLast() { + try { orderedMap.putLast(a, aValue); } + catch (UnsupportedOperationException tolerated) {} + expectUnchanged(); + } + +#ignore + @MapFeature.Require(absent = SUPPORTS_REMOVE) +#endignore + public void testUnsupportedPollFirst() { + try { orderedMap.pollFirstEntry(); } + catch (UnsupportedOperationException tolerated) {} + expectUnchanged(); + } + +#ignore + @MapFeature.Require(absent = SUPPORTS_REMOVE) +#endignore + public void testUnsupportedPollLast() { + try { orderedMap.pollFirstEntry(); } + catch (UnsupportedOperationException tolerated) {} + expectUnchanged(); + } +} \ No newline at end of file diff --git a/src/builder/resources/speiger/assets/testers/templates/tests/queue/iterators/QueueFindFirstTester.template b/src/builder/resources/speiger/assets/testers/templates/tests/queue/iterators/QueueFindFirstTester.template index aa90be0..a109ac9 100644 --- a/src/builder/resources/speiger/assets/testers/templates/tests/queue/iterators/QueueFindFirstTester.template +++ b/src/builder/resources/speiger/assets/testers/templates/tests/queue/iterators/QueueFindFirstTester.template @@ -2,6 +2,11 @@ package speiger.src.testers.PACKAGE.tests.queue.iterators; #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; @@ -27,17 +32,17 @@ public class FILE_KEY_TYPEQueueFindFirstTester KEY_GENERIC_TYPE extends ABSTRACT @CollectionSize.Require(absent = CollectionSize.ZERO) #endignore public void testQueueFindFirst_FindFirstElements() { - assertEquals("First Element should be found", e0(), queue.findFirst(T -> KEY_EQUALS(T, e0()))); + assertEquals("First Element should be found", e0(), queue.findFirst(T -> KEY_EQUALS(T, e0())).SUPPLY_GET()); } public void testQueueFindFirst_FindNothing() { - assertEquals("No element should be found", EMPTY_KEY_VALUE, queue.findFirst(T -> KEY_EQUALS(T, e4()))); + assertEquals("No element should be found", OPTIONAL.empty(), queue.findFirst(T -> KEY_EQUALS(T, e4()))); } #ignore @CollectionSize.Require(CollectionSize.SEVERAL) #endignore public void testQueueFindFirst_FindLastElement() { - assertEquals("Last Element should be found", e2(), queue.findFirst(T -> KEY_EQUALS(T, e2()))); + assertEquals("Last Element should be found", e2(), queue.findFirst(T -> KEY_EQUALS(T, e2())).SUPPLY_GET()); } } diff --git a/src/builder/resources/speiger/assets/testers/templates/tests/queue/iterators/QueueReduceTester.template b/src/builder/resources/speiger/assets/testers/templates/tests/queue/iterators/QueueReduceTester.template index 5cc29b6..c4517fa 100644 --- a/src/builder/resources/speiger/assets/testers/templates/tests/queue/iterators/QueueReduceTester.template +++ b/src/builder/resources/speiger/assets/testers/templates/tests/queue/iterators/QueueReduceTester.template @@ -3,10 +3,16 @@ package speiger.src.testers.PACKAGE.tests.queue.iterators; #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 speiger.src.testers.PACKAGE.tests.base.ABSTRACT_QUEUE_TESTER; +import com.google.common.collect.testing.features.CollectionSize; @Ignore @SuppressWarnings("javadoc") @@ -29,7 +35,7 @@ public class FILE_KEY_TYPEQueueReduceTester KEY_GENERIC_TYPE extends ABSTRACT_QU } public void testQueueReduce() { - assertEquals("The sum of the queue should match", getSum(), queue.reduce(this::sum)); + assertEquals("The sum of the queue should match", size == CollectionSize.ZERO ? OPTIONAL.empty() : OPTIONAL.GET_OPTIONAL(getSum()), queue.reduce(this::sum)); } public void testQueueExtraReduce() { diff --git a/src/main/java/speiger/src/collections/booleans/collections/BooleanIterable.java b/src/main/java/speiger/src/collections/booleans/collections/BooleanIterable.java index 09d2edb..a8f5d0e 100644 --- a/src/main/java/speiger/src/collections/booleans/collections/BooleanIterable.java +++ b/src/main/java/speiger/src/collections/booleans/collections/BooleanIterable.java @@ -7,6 +7,7 @@ import java.util.function.Consumer; import speiger.src.collections.booleans.functions.BooleanConsumer; import speiger.src.collections.booleans.functions.BooleanComparator; import speiger.src.collections.objects.collections.ObjectIterable; +import speiger.src.collections.booleans.functions.OptionalBoolean; import speiger.src.collections.booleans.functions.function.BooleanFunction; import speiger.src.collections.ints.functions.consumer.IntBooleanConsumer; import speiger.src.collections.objects.functions.consumer.ObjectBooleanConsumer; @@ -271,13 +272,13 @@ public interface BooleanIterable extends Iterable * @param filter that should be applied * @return the found value or the null equivalent variant. */ - default boolean findFirst(BooleanPredicate filter) { + default OptionalBoolean findFirst(BooleanPredicate filter) { Objects.requireNonNull(filter); for(BooleanIterator iter = iterator();iter.hasNext();) { boolean entry = iter.nextBoolean(); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return OptionalBoolean.of(entry); } - return false; + return OptionalBoolean.empty(); } /** @@ -302,7 +303,7 @@ public interface BooleanIterable extends Iterable * @param operator the operation that should be applied * @return the reduction result, returns null value if nothing was found */ - default boolean reduce(BooleanBooleanUnaryOperator operator) { + default OptionalBoolean reduce(BooleanBooleanUnaryOperator operator) { Objects.requireNonNull(operator); boolean state = false; boolean empty = true; @@ -314,7 +315,7 @@ public interface BooleanIterable extends Iterable } state = operator.applyAsBoolean(state, iter.nextBoolean()); } - return state; + return empty ? OptionalBoolean.empty() : OptionalBoolean.of(state); } /** diff --git a/src/main/java/speiger/src/collections/booleans/lists/BooleanArrayList.java b/src/main/java/speiger/src/collections/booleans/lists/BooleanArrayList.java index c313109..b485f44 100644 --- a/src/main/java/speiger/src/collections/booleans/lists/BooleanArrayList.java +++ b/src/main/java/speiger/src/collections/booleans/lists/BooleanArrayList.java @@ -10,6 +10,7 @@ import java.util.function.UnaryOperator; import speiger.src.collections.booleans.collections.BooleanCollection; import speiger.src.collections.booleans.collections.BooleanStack; import speiger.src.collections.booleans.collections.BooleanIterator; +import speiger.src.collections.booleans.functions.OptionalBoolean; import speiger.src.collections.booleans.functions.BooleanComparator; import speiger.src.collections.booleans.functions.BooleanConsumer; import speiger.src.collections.objects.functions.consumer.ObjectBooleanConsumer; @@ -510,12 +511,12 @@ public class BooleanArrayList extends AbstractBooleanList implements IBooleanArr } @Override - public boolean findFirst(BooleanPredicate filter) { + public OptionalBoolean findFirst(BooleanPredicate filter) { Objects.requireNonNull(filter); for(int i = 0;i * @param filter that should be applied * @return the found value or the null equivalent variant. */ - default byte findFirst(BytePredicate filter) { + default OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); for(ByteIterator iter = iterator();iter.hasNext();) { byte entry = iter.nextByte(); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return OptionalByte.of(entry); } - return (byte)0; + return OptionalByte.empty(); } /** @@ -312,7 +313,7 @@ public interface ByteIterable extends Iterable * @param operator the operation that should be applied * @return the reduction result, returns null value if nothing was found */ - default byte reduce(ByteByteUnaryOperator operator) { + default OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -324,7 +325,7 @@ public interface ByteIterable extends Iterable } state = operator.applyAsByte(state, iter.nextByte()); } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } /** diff --git a/src/main/java/speiger/src/collections/bytes/lists/ByteArrayList.java b/src/main/java/speiger/src/collections/bytes/lists/ByteArrayList.java index 87e4817..6afc5bf 100644 --- a/src/main/java/speiger/src/collections/bytes/lists/ByteArrayList.java +++ b/src/main/java/speiger/src/collections/bytes/lists/ByteArrayList.java @@ -12,6 +12,7 @@ import java.nio.ByteBuffer; import speiger.src.collections.bytes.collections.ByteCollection; import speiger.src.collections.bytes.collections.ByteStack; import speiger.src.collections.bytes.collections.ByteIterator; +import speiger.src.collections.bytes.functions.OptionalByte; import speiger.src.collections.bytes.functions.ByteComparator; import speiger.src.collections.bytes.functions.ByteConsumer; import speiger.src.collections.objects.functions.consumer.ObjectByteConsumer; @@ -519,12 +520,12 @@ public class ByteArrayList extends AbstractByteList implements IByteArray, ByteS } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); for(int i = 0;i operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Byte2BooleanMap.Entry state = null; boolean empty = true; @@ -779,11 +782,11 @@ public class Byte2BooleanConcurrentOpenHashMap extends AbstractByte2BooleanMap i seg.unlockRead(stamp); } } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Byte2BooleanMap.Entry 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); Byte2ByteMap.Entry state = null; boolean empty = true; @@ -785,11 +787,11 @@ public class Byte2ByteConcurrentOpenHashMap extends AbstractByte2ByteMap impleme seg.unlockRead(stamp); } } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Byte2ByteMap.Entry 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); Byte2CharMap.Entry state = null; boolean empty = true; @@ -792,11 +795,11 @@ public class Byte2CharConcurrentOpenHashMap extends AbstractByte2CharMap impleme seg.unlockRead(stamp); } } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Byte2CharMap.Entry 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); Byte2DoubleMap.Entry state = null; boolean empty = true; @@ -792,11 +795,11 @@ public class Byte2DoubleConcurrentOpenHashMap extends AbstractByte2DoubleMap imp seg.unlockRead(stamp); } } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Byte2DoubleMap.Entry 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); Byte2FloatMap.Entry state = null; boolean empty = true; @@ -792,11 +795,11 @@ public class Byte2FloatConcurrentOpenHashMap extends AbstractByte2FloatMap imple seg.unlockRead(stamp); } } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Byte2FloatMap.Entry 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); Byte2IntMap.Entry state = null; boolean empty = true; @@ -792,11 +795,11 @@ public class Byte2IntConcurrentOpenHashMap extends AbstractByte2IntMap implement seg.unlockRead(stamp); } } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Byte2IntMap.Entry 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); Byte2LongMap.Entry state = null; boolean empty = true; @@ -792,11 +795,11 @@ public class Byte2LongConcurrentOpenHashMap extends AbstractByte2LongMap impleme seg.unlockRead(stamp); } } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Byte2LongMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); MapEntry entry = new MapEntry(); for(int i = 0,m=segments.length;i extends AbstractByte2ObjectMap< } @Override - public Byte2ObjectMap.Entry reduce(ObjectObjectUnaryOperator, Byte2ObjectMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Byte2ObjectMap.Entry> operator) { Objects.requireNonNull(operator); Byte2ObjectMap.Entry state = null; boolean empty = true; @@ -739,11 +741,11 @@ public class Byte2ObjectConcurrentOpenHashMap extends AbstractByte2ObjectMap< seg.unlockRead(stamp); } } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Byte2ObjectMap.Entry findFirst(Predicate> filter) { + public Optional> findFirst(Predicate> filter) { Objects.requireNonNull(filter); MapEntry entry = new MapEntry(); for(int i = 0,m=segments.length;i extends AbstractByte2ObjectMap< try { while(index != -1) { entry.set(index, i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); index = (int)seg.links[index]; } } @@ -761,7 +763,7 @@ public class Byte2ObjectConcurrentOpenHashMap extends AbstractByte2ObjectMap< seg.unlockRead(stamp); } } - return null; + return Optional.empty(); } @Override @@ -1022,7 +1024,7 @@ public class Byte2ObjectConcurrentOpenHashMap extends AbstractByte2ObjectMap< } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -1046,11 +1048,11 @@ public class Byte2ObjectConcurrentOpenHashMap extends AbstractByte2ObjectMap< seg.unlockRead(stamp); } } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); for(int i = 0,m=segments.length;i seg = segments[i]; @@ -1058,7 +1060,7 @@ public class Byte2ObjectConcurrentOpenHashMap extends AbstractByte2ObjectMap< try { int index = seg.firstIndex; while(index != -1){ - if(filter.test(seg.keys[index])) return seg.keys[index]; + if(filter.test(seg.keys[index])) return OptionalByte.of(seg.keys[index]); index = (int)seg.links[index]; } } @@ -1066,7 +1068,7 @@ public class Byte2ObjectConcurrentOpenHashMap extends AbstractByte2ObjectMap< seg.unlockRead(stamp); } } - return (byte)0; + return OptionalByte.empty(); } @Override @@ -1258,7 +1260,7 @@ public class Byte2ObjectConcurrentOpenHashMap extends AbstractByte2ObjectMap< } @Override - public V reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); V state = null; boolean empty = true; @@ -1282,20 +1284,20 @@ public class Byte2ObjectConcurrentOpenHashMap extends AbstractByte2ObjectMap< seg.unlockRead(stamp); } } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public V findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); for(int i = 0,m=segments.length;i seg = segments[i]; long stamp = seg.readLock(); try { int index = seg.firstIndex; while(index != -1){ - if(filter.test(seg.values[index])) return seg.values[index]; + if(filter.test(seg.values[index])) return Optional.ofNullable(seg.values[index]); index = (int)seg.links[index]; } } @@ -1303,7 +1305,7 @@ public class Byte2ObjectConcurrentOpenHashMap extends AbstractByte2ObjectMap< seg.unlockRead(stamp); } } - return null; + return Optional.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/bytes/maps/impl/concurrent/Byte2ShortConcurrentOpenHashMap.java b/src/main/java/speiger/src/collections/bytes/maps/impl/concurrent/Byte2ShortConcurrentOpenHashMap.java index 8ea1dd6..65e0e6c 100644 --- a/src/main/java/speiger/src/collections/bytes/maps/impl/concurrent/Byte2ShortConcurrentOpenHashMap.java +++ b/src/main/java/speiger/src/collections/bytes/maps/impl/concurrent/Byte2ShortConcurrentOpenHashMap.java @@ -4,6 +4,7 @@ import java.util.Arrays; import java.util.Map; import java.util.NoSuchElementException; import java.util.Objects; +import java.util.Optional; import java.util.concurrent.locks.StampedLock; import java.util.function.Consumer; import java.util.function.Predicate; @@ -19,6 +20,7 @@ import speiger.src.collections.bytes.functions.consumer.ByteShortConsumer; import speiger.src.collections.bytes.functions.function.Byte2ShortFunction; import speiger.src.collections.bytes.functions.function.ByteShortUnaryOperator; import speiger.src.collections.bytes.functions.function.ByteByteUnaryOperator; +import speiger.src.collections.bytes.functions.OptionalByte; import speiger.src.collections.bytes.functions.function.BytePredicate; import speiger.src.collections.bytes.maps.abstracts.AbstractByte2ShortMap; import speiger.src.collections.bytes.maps.interfaces.Byte2ShortMap; @@ -37,6 +39,7 @@ import speiger.src.collections.shorts.functions.ShortConsumer; import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectShortConsumer; import speiger.src.collections.shorts.functions.function.ShortPredicate; +import speiger.src.collections.shorts.functions.OptionalShort; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.objects.sets.AbstractObjectSet; import speiger.src.collections.objects.sets.ObjectSet; @@ -768,7 +771,7 @@ public class Byte2ShortConcurrentOpenHashMap extends AbstractByte2ShortMap imple } @Override - public Byte2ShortMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Byte2ShortMap.Entry state = null; boolean empty = true; @@ -792,11 +795,11 @@ public class Byte2ShortConcurrentOpenHashMap extends AbstractByte2ShortMap imple seg.unlockRead(stamp); } } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Byte2ShortMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); MapEntry entry = new MapEntry(); for(int i = 0,m=segments.length;i implements Byte2BooleanOrderedMap.FastOrderedSet { + @Override + public void addFirst(Byte2BooleanMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Byte2BooleanMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Byte2BooleanMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -836,7 +843,7 @@ public class Byte2BooleanLinkedOpenCustomHashMap extends Byte2BooleanOpenCustomH } @Override - public Byte2BooleanMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Byte2BooleanMap.Entry state = null; boolean empty = true; @@ -851,21 +858,21 @@ public class Byte2BooleanLinkedOpenCustomHashMap extends Byte2BooleanOpenCustomH state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Byte2BooleanMap.Entry 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 @@ -944,10 +951,13 @@ public class Byte2BooleanLinkedOpenCustomHashMap extends Byte2BooleanOpenCustomH public boolean add(byte o) { throw new UnsupportedOperationException(); } - + + @Override + public void addFirst(byte o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(byte o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(byte o) { throw new UnsupportedOperationException(); } - @Override public boolean addAndMoveToLast(byte o) { throw new UnsupportedOperationException(); } @@ -1090,7 +1100,7 @@ public class Byte2BooleanLinkedOpenCustomHashMap extends Byte2BooleanOpenCustomH } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -1105,19 +1115,19 @@ public class Byte2BooleanLinkedOpenCustomHashMap extends Byte2BooleanOpenCustomH state = operator.applyAsByte(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (byte)0; + if(size() <= 0) return OptionalByte.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalByte.of(keys[index]); index = (int)links[index]; } - return (byte)0; + return OptionalByte.empty(); } @Override @@ -1252,7 +1262,7 @@ public class Byte2BooleanLinkedOpenCustomHashMap extends Byte2BooleanOpenCustomH } @Override - public boolean reduce(BooleanBooleanUnaryOperator operator) { + public OptionalBoolean reduce(BooleanBooleanUnaryOperator operator) { Objects.requireNonNull(operator); boolean state = false; boolean empty = true; @@ -1267,19 +1277,19 @@ public class Byte2BooleanLinkedOpenCustomHashMap extends Byte2BooleanOpenCustomH state = operator.applyAsBoolean(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalBoolean.empty() : OptionalBoolean.of(state); } @Override - public boolean findFirst(BooleanPredicate filter) { + public OptionalBoolean findFirst(BooleanPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return false; + if(size() <= 0) return OptionalBoolean.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalBoolean.of(values[index]); index = (int)links[index]; } - return false; + return OptionalBoolean.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/bytes/maps/impl/customHash/Byte2BooleanOpenCustomHashMap.java b/src/main/java/speiger/src/collections/bytes/maps/impl/customHash/Byte2BooleanOpenCustomHashMap.java index fb5bfb6..b004103 100644 --- a/src/main/java/speiger/src/collections/bytes/maps/impl/customHash/Byte2BooleanOpenCustomHashMap.java +++ b/src/main/java/speiger/src/collections/bytes/maps/impl/customHash/Byte2BooleanOpenCustomHashMap.java @@ -5,6 +5,7 @@ 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; @@ -19,6 +20,7 @@ import speiger.src.collections.bytes.functions.consumer.ByteBooleanConsumer; import speiger.src.collections.bytes.functions.function.BytePredicate; import speiger.src.collections.bytes.functions.function.ByteBooleanUnaryOperator; import speiger.src.collections.bytes.functions.function.ByteByteUnaryOperator; +import speiger.src.collections.bytes.functions.OptionalByte; import speiger.src.collections.bytes.maps.abstracts.AbstractByte2BooleanMap; import speiger.src.collections.bytes.maps.interfaces.Byte2BooleanMap; import speiger.src.collections.bytes.sets.AbstractByteSet; @@ -36,6 +38,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectBooleanConsumer; import speiger.src.collections.booleans.functions.function.BooleanPredicate; +import speiger.src.collections.booleans.functions.OptionalBoolean; import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.sets.AbstractObjectSet; import speiger.src.collections.objects.sets.ObjectSet; @@ -930,7 +933,7 @@ public class Byte2BooleanOpenCustomHashMap extends AbstractByte2BooleanMap imple } @Override - public Byte2BooleanMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Byte2BooleanMap.Entry state = null; boolean empty = true; @@ -947,25 +950,25 @@ public class Byte2BooleanOpenCustomHashMap extends AbstractByte2BooleanMap imple } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Byte2BooleanMap.Entry 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], (byte)0)) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1139,7 +1142,7 @@ public class Byte2BooleanOpenCustomHashMap extends AbstractByte2BooleanMap imple } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -1156,18 +1159,18 @@ public class Byte2BooleanOpenCustomHashMap extends AbstractByte2BooleanMap imple } state = operator.applyAsByte(state, keys[i]); } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (byte)0; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OptionalByte.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OptionalByte.of(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], (byte)0) && filter.test(keys[i])) return keys[i]; + if(!strategy.equals(keys[i], (byte)0) && filter.test(keys[i])) return OptionalByte.of(keys[i]); } - return (byte)0; + return OptionalByte.empty(); } @Override @@ -1282,7 +1285,7 @@ public class Byte2BooleanOpenCustomHashMap extends AbstractByte2BooleanMap imple } @Override - public boolean reduce(BooleanBooleanUnaryOperator operator) { + public OptionalBoolean reduce(BooleanBooleanUnaryOperator operator) { Objects.requireNonNull(operator); boolean state = false; boolean empty = true; @@ -1299,18 +1302,18 @@ public class Byte2BooleanOpenCustomHashMap extends AbstractByte2BooleanMap imple } state = operator.applyAsBoolean(state, values[i]); } - return state; + return empty ? OptionalBoolean.empty() : OptionalBoolean.of(state); } @Override - public boolean findFirst(BooleanPredicate filter) { + public OptionalBoolean findFirst(BooleanPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return false; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return OptionalBoolean.empty(); + if(containsNull && filter.test(values[nullIndex])) return OptionalBoolean.of(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], (byte)0) && filter.test(values[i])) return values[i]; + if(!strategy.equals(keys[i], (byte)0) && filter.test(values[i])) return OptionalBoolean.of(values[i]); } - return false; + return OptionalBoolean.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/bytes/maps/impl/customHash/Byte2ByteLinkedOpenCustomHashMap.java b/src/main/java/speiger/src/collections/bytes/maps/impl/customHash/Byte2ByteLinkedOpenCustomHashMap.java index 54d2a1b..0440238 100644 --- a/src/main/java/speiger/src/collections/bytes/maps/impl/customHash/Byte2ByteLinkedOpenCustomHashMap.java +++ b/src/main/java/speiger/src/collections/bytes/maps/impl/customHash/Byte2ByteLinkedOpenCustomHashMap.java @@ -4,6 +4,7 @@ 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; @@ -13,6 +14,7 @@ import speiger.src.collections.bytes.functions.ByteConsumer; import speiger.src.collections.objects.functions.consumer.ObjectByteConsumer; import speiger.src.collections.ints.functions.consumer.IntByteConsumer; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; +import speiger.src.collections.bytes.functions.OptionalByte; import speiger.src.collections.bytes.functions.function.BytePredicate; import speiger.src.collections.bytes.functions.consumer.ByteByteConsumer; import speiger.src.collections.bytes.functions.function.ByteByteUnaryOperator; @@ -664,6 +666,10 @@ public class Byte2ByteLinkedOpenCustomHashMap extends Byte2ByteOpenCustomHashMap } private class MapEntrySet extends AbstractObjectSet implements Byte2ByteOrderedMap.FastOrderedSet { + @Override + public void addFirst(Byte2ByteMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Byte2ByteMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Byte2ByteMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -829,7 +835,7 @@ public class Byte2ByteLinkedOpenCustomHashMap extends Byte2ByteOpenCustomHashMap } @Override - public Byte2ByteMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Byte2ByteMap.Entry state = null; boolean empty = true; @@ -844,21 +850,21 @@ public class Byte2ByteLinkedOpenCustomHashMap extends Byte2ByteOpenCustomHashMap state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Byte2ByteMap.Entry 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 @@ -937,10 +943,13 @@ public class Byte2ByteLinkedOpenCustomHashMap extends Byte2ByteOpenCustomHashMap public boolean add(byte o) { throw new UnsupportedOperationException(); } - + + @Override + public void addFirst(byte o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(byte o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(byte o) { throw new UnsupportedOperationException(); } - @Override public boolean addAndMoveToLast(byte o) { throw new UnsupportedOperationException(); } @@ -1083,7 +1092,7 @@ public class Byte2ByteLinkedOpenCustomHashMap extends Byte2ByteOpenCustomHashMap } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -1098,19 +1107,19 @@ public class Byte2ByteLinkedOpenCustomHashMap extends Byte2ByteOpenCustomHashMap state = operator.applyAsByte(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (byte)0; + if(size() <= 0) return OptionalByte.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalByte.of(keys[index]); index = (int)links[index]; } - return (byte)0; + return OptionalByte.empty(); } @Override @@ -1245,7 +1254,7 @@ public class Byte2ByteLinkedOpenCustomHashMap extends Byte2ByteOpenCustomHashMap } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -1260,19 +1269,19 @@ public class Byte2ByteLinkedOpenCustomHashMap extends Byte2ByteOpenCustomHashMap state = operator.applyAsByte(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (byte)0; + if(size() <= 0) return OptionalByte.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalByte.of(values[index]); index = (int)links[index]; } - return (byte)0; + return OptionalByte.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/bytes/maps/impl/customHash/Byte2ByteOpenCustomHashMap.java b/src/main/java/speiger/src/collections/bytes/maps/impl/customHash/Byte2ByteOpenCustomHashMap.java index 616f06d..3e9fede 100644 --- a/src/main/java/speiger/src/collections/bytes/maps/impl/customHash/Byte2ByteOpenCustomHashMap.java +++ b/src/main/java/speiger/src/collections/bytes/maps/impl/customHash/Byte2ByteOpenCustomHashMap.java @@ -5,6 +5,7 @@ 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; @@ -17,6 +18,7 @@ import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.bytes.functions.consumer.ByteByteConsumer; import speiger.src.collections.bytes.functions.function.ByteUnaryOperator; import speiger.src.collections.bytes.functions.function.ByteByteUnaryOperator; +import speiger.src.collections.bytes.functions.OptionalByte; import speiger.src.collections.bytes.functions.function.BytePredicate; import speiger.src.collections.bytes.maps.abstracts.AbstractByte2ByteMap; import speiger.src.collections.bytes.maps.interfaces.Byte2ByteMap; @@ -944,7 +946,7 @@ public class Byte2ByteOpenCustomHashMap extends AbstractByte2ByteMap implements } @Override - public Byte2ByteMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Byte2ByteMap.Entry state = null; boolean empty = true; @@ -961,25 +963,25 @@ public class Byte2ByteOpenCustomHashMap extends AbstractByte2ByteMap implements } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Byte2ByteMap.Entry 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], (byte)0)) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1153,7 +1155,7 @@ public class Byte2ByteOpenCustomHashMap extends AbstractByte2ByteMap implements } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -1170,18 +1172,18 @@ public class Byte2ByteOpenCustomHashMap extends AbstractByte2ByteMap implements } state = operator.applyAsByte(state, keys[i]); } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (byte)0; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OptionalByte.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OptionalByte.of(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], (byte)0) && filter.test(keys[i])) return keys[i]; + if(!strategy.equals(keys[i], (byte)0) && filter.test(keys[i])) return OptionalByte.of(keys[i]); } - return (byte)0; + return OptionalByte.empty(); } @Override @@ -1296,7 +1298,7 @@ public class Byte2ByteOpenCustomHashMap extends AbstractByte2ByteMap implements } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -1313,18 +1315,18 @@ public class Byte2ByteOpenCustomHashMap extends AbstractByte2ByteMap implements } state = operator.applyAsByte(state, values[i]); } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (byte)0; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return OptionalByte.empty(); + if(containsNull && filter.test(values[nullIndex])) return OptionalByte.of(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], (byte)0) && filter.test(values[i])) return values[i]; + if(!strategy.equals(keys[i], (byte)0) && filter.test(values[i])) return OptionalByte.of(values[i]); } - return (byte)0; + return OptionalByte.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/bytes/maps/impl/customHash/Byte2CharLinkedOpenCustomHashMap.java b/src/main/java/speiger/src/collections/bytes/maps/impl/customHash/Byte2CharLinkedOpenCustomHashMap.java index 8af011a..3d84a4e 100644 --- a/src/main/java/speiger/src/collections/bytes/maps/impl/customHash/Byte2CharLinkedOpenCustomHashMap.java +++ b/src/main/java/speiger/src/collections/bytes/maps/impl/customHash/Byte2CharLinkedOpenCustomHashMap.java @@ -4,6 +4,7 @@ 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; @@ -14,6 +15,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectByteConsumer; import speiger.src.collections.ints.functions.consumer.IntByteConsumer; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntCharConsumer; +import speiger.src.collections.bytes.functions.OptionalByte; import speiger.src.collections.bytes.functions.function.BytePredicate; import speiger.src.collections.bytes.functions.consumer.ByteCharConsumer; import speiger.src.collections.bytes.functions.function.ByteByteUnaryOperator; @@ -33,6 +35,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectCharConsumer; import speiger.src.collections.chars.functions.function.CharPredicate; +import speiger.src.collections.chars.functions.OptionalChar; import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOperator; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.objects.lists.ObjectListIterator; @@ -671,6 +674,10 @@ public class Byte2CharLinkedOpenCustomHashMap extends Byte2CharOpenCustomHashMap } private class MapEntrySet extends AbstractObjectSet implements Byte2CharOrderedMap.FastOrderedSet { + @Override + public void addFirst(Byte2CharMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Byte2CharMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Byte2CharMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -836,7 +843,7 @@ public class Byte2CharLinkedOpenCustomHashMap extends Byte2CharOpenCustomHashMap } @Override - public Byte2CharMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Byte2CharMap.Entry state = null; boolean empty = true; @@ -851,21 +858,21 @@ public class Byte2CharLinkedOpenCustomHashMap extends Byte2CharOpenCustomHashMap state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Byte2CharMap.Entry 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 @@ -944,10 +951,13 @@ public class Byte2CharLinkedOpenCustomHashMap extends Byte2CharOpenCustomHashMap public boolean add(byte o) { throw new UnsupportedOperationException(); } - + + @Override + public void addFirst(byte o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(byte o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(byte o) { throw new UnsupportedOperationException(); } - @Override public boolean addAndMoveToLast(byte o) { throw new UnsupportedOperationException(); } @@ -1090,7 +1100,7 @@ public class Byte2CharLinkedOpenCustomHashMap extends Byte2CharOpenCustomHashMap } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -1105,19 +1115,19 @@ public class Byte2CharLinkedOpenCustomHashMap extends Byte2CharOpenCustomHashMap state = operator.applyAsByte(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (byte)0; + if(size() <= 0) return OptionalByte.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalByte.of(keys[index]); index = (int)links[index]; } - return (byte)0; + return OptionalByte.empty(); } @Override @@ -1252,7 +1262,7 @@ public class Byte2CharLinkedOpenCustomHashMap extends Byte2CharOpenCustomHashMap } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -1267,19 +1277,19 @@ public class Byte2CharLinkedOpenCustomHashMap extends Byte2CharOpenCustomHashMap state = operator.applyAsChar(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (char)0; + if(size() <= 0) return OptionalChar.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalChar.of(values[index]); index = (int)links[index]; } - return (char)0; + return OptionalChar.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/bytes/maps/impl/customHash/Byte2CharOpenCustomHashMap.java b/src/main/java/speiger/src/collections/bytes/maps/impl/customHash/Byte2CharOpenCustomHashMap.java index f0db65c..ae1d65c 100644 --- a/src/main/java/speiger/src/collections/bytes/maps/impl/customHash/Byte2CharOpenCustomHashMap.java +++ b/src/main/java/speiger/src/collections/bytes/maps/impl/customHash/Byte2CharOpenCustomHashMap.java @@ -5,6 +5,7 @@ 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; @@ -19,6 +20,7 @@ import speiger.src.collections.bytes.functions.consumer.ByteCharConsumer; import speiger.src.collections.bytes.functions.function.Byte2CharFunction; import speiger.src.collections.bytes.functions.function.ByteCharUnaryOperator; import speiger.src.collections.bytes.functions.function.ByteByteUnaryOperator; +import speiger.src.collections.bytes.functions.OptionalByte; import speiger.src.collections.bytes.functions.function.BytePredicate; import speiger.src.collections.bytes.maps.abstracts.AbstractByte2CharMap; import speiger.src.collections.bytes.maps.interfaces.Byte2CharMap; @@ -37,6 +39,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectCharConsumer; import speiger.src.collections.chars.functions.function.CharPredicate; +import speiger.src.collections.chars.functions.OptionalChar; import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.sets.AbstractObjectSet; import speiger.src.collections.objects.sets.ObjectSet; @@ -952,7 +955,7 @@ public class Byte2CharOpenCustomHashMap extends AbstractByte2CharMap implements } @Override - public Byte2CharMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Byte2CharMap.Entry state = null; boolean empty = true; @@ -969,25 +972,25 @@ public class Byte2CharOpenCustomHashMap extends AbstractByte2CharMap implements } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Byte2CharMap.Entry 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], (byte)0)) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1161,7 +1164,7 @@ public class Byte2CharOpenCustomHashMap extends AbstractByte2CharMap implements } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -1178,18 +1181,18 @@ public class Byte2CharOpenCustomHashMap extends AbstractByte2CharMap implements } state = operator.applyAsByte(state, keys[i]); } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (byte)0; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OptionalByte.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OptionalByte.of(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], (byte)0) && filter.test(keys[i])) return keys[i]; + if(!strategy.equals(keys[i], (byte)0) && filter.test(keys[i])) return OptionalByte.of(keys[i]); } - return (byte)0; + return OptionalByte.empty(); } @Override @@ -1304,7 +1307,7 @@ public class Byte2CharOpenCustomHashMap extends AbstractByte2CharMap implements } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -1321,18 +1324,18 @@ public class Byte2CharOpenCustomHashMap extends AbstractByte2CharMap implements } state = operator.applyAsChar(state, values[i]); } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (char)0; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return OptionalChar.empty(); + if(containsNull && filter.test(values[nullIndex])) return OptionalChar.of(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], (byte)0) && filter.test(values[i])) return values[i]; + if(!strategy.equals(keys[i], (byte)0) && filter.test(values[i])) return OptionalChar.of(values[i]); } - return (char)0; + return OptionalChar.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/bytes/maps/impl/customHash/Byte2DoubleLinkedOpenCustomHashMap.java b/src/main/java/speiger/src/collections/bytes/maps/impl/customHash/Byte2DoubleLinkedOpenCustomHashMap.java index 9eb2bbb..6f4754c 100644 --- a/src/main/java/speiger/src/collections/bytes/maps/impl/customHash/Byte2DoubleLinkedOpenCustomHashMap.java +++ b/src/main/java/speiger/src/collections/bytes/maps/impl/customHash/Byte2DoubleLinkedOpenCustomHashMap.java @@ -4,10 +4,12 @@ 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; import java.util.function.DoublePredicate; +import java.util.OptionalDouble; import speiger.src.collections.bytes.collections.ByteBidirectionalIterator; import speiger.src.collections.bytes.functions.ByteConsumer; @@ -15,6 +17,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectByteConsumer; import speiger.src.collections.ints.functions.consumer.IntByteConsumer; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntDoubleConsumer; +import speiger.src.collections.bytes.functions.OptionalByte; import speiger.src.collections.bytes.functions.function.BytePredicate; import speiger.src.collections.bytes.functions.consumer.ByteDoubleConsumer; import speiger.src.collections.bytes.functions.function.ByteByteUnaryOperator; @@ -671,6 +674,10 @@ public class Byte2DoubleLinkedOpenCustomHashMap extends Byte2DoubleOpenCustomHas } private class MapEntrySet extends AbstractObjectSet implements Byte2DoubleOrderedMap.FastOrderedSet { + @Override + public void addFirst(Byte2DoubleMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Byte2DoubleMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Byte2DoubleMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -836,7 +843,7 @@ public class Byte2DoubleLinkedOpenCustomHashMap extends Byte2DoubleOpenCustomHas } @Override - public Byte2DoubleMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Byte2DoubleMap.Entry state = null; boolean empty = true; @@ -851,21 +858,21 @@ public class Byte2DoubleLinkedOpenCustomHashMap extends Byte2DoubleOpenCustomHas state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Byte2DoubleMap.Entry 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 @@ -944,10 +951,13 @@ public class Byte2DoubleLinkedOpenCustomHashMap extends Byte2DoubleOpenCustomHas public boolean add(byte o) { throw new UnsupportedOperationException(); } - + + @Override + public void addFirst(byte o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(byte o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(byte o) { throw new UnsupportedOperationException(); } - @Override public boolean addAndMoveToLast(byte o) { throw new UnsupportedOperationException(); } @@ -1090,7 +1100,7 @@ public class Byte2DoubleLinkedOpenCustomHashMap extends Byte2DoubleOpenCustomHas } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -1105,19 +1115,19 @@ public class Byte2DoubleLinkedOpenCustomHashMap extends Byte2DoubleOpenCustomHas state = operator.applyAsByte(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (byte)0; + if(size() <= 0) return OptionalByte.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalByte.of(keys[index]); index = (int)links[index]; } - return (byte)0; + return OptionalByte.empty(); } @Override @@ -1252,7 +1262,7 @@ public class Byte2DoubleLinkedOpenCustomHashMap extends Byte2DoubleOpenCustomHas } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -1267,19 +1277,19 @@ public class Byte2DoubleLinkedOpenCustomHashMap extends Byte2DoubleOpenCustomHas state = operator.applyAsDouble(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0D; + if(size() <= 0) return OptionalDouble.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalDouble.of(values[index]); index = (int)links[index]; } - return 0D; + return OptionalDouble.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/bytes/maps/impl/customHash/Byte2DoubleOpenCustomHashMap.java b/src/main/java/speiger/src/collections/bytes/maps/impl/customHash/Byte2DoubleOpenCustomHashMap.java index 96a025b..52c79ff 100644 --- a/src/main/java/speiger/src/collections/bytes/maps/impl/customHash/Byte2DoubleOpenCustomHashMap.java +++ b/src/main/java/speiger/src/collections/bytes/maps/impl/customHash/Byte2DoubleOpenCustomHashMap.java @@ -5,10 +5,12 @@ 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; import java.util.function.DoublePredicate; +import java.util.OptionalDouble; import speiger.src.collections.bytes.collections.ByteIterator; import speiger.src.collections.bytes.functions.ByteConsumer; @@ -20,6 +22,7 @@ import speiger.src.collections.bytes.functions.consumer.ByteDoubleConsumer; import speiger.src.collections.bytes.functions.function.Byte2DoubleFunction; import speiger.src.collections.bytes.functions.function.ByteDoubleUnaryOperator; import speiger.src.collections.bytes.functions.function.ByteByteUnaryOperator; +import speiger.src.collections.bytes.functions.OptionalByte; import speiger.src.collections.bytes.functions.function.BytePredicate; import speiger.src.collections.bytes.maps.abstracts.AbstractByte2DoubleMap; import speiger.src.collections.bytes.maps.interfaces.Byte2DoubleMap; @@ -952,7 +955,7 @@ public class Byte2DoubleOpenCustomHashMap extends AbstractByte2DoubleMap impleme } @Override - public Byte2DoubleMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Byte2DoubleMap.Entry state = null; boolean empty = true; @@ -969,25 +972,25 @@ public class Byte2DoubleOpenCustomHashMap extends AbstractByte2DoubleMap impleme } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Byte2DoubleMap.Entry 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], (byte)0)) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1161,7 +1164,7 @@ public class Byte2DoubleOpenCustomHashMap extends AbstractByte2DoubleMap impleme } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -1178,18 +1181,18 @@ public class Byte2DoubleOpenCustomHashMap extends AbstractByte2DoubleMap impleme } state = operator.applyAsByte(state, keys[i]); } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (byte)0; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OptionalByte.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OptionalByte.of(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], (byte)0) && filter.test(keys[i])) return keys[i]; + if(!strategy.equals(keys[i], (byte)0) && filter.test(keys[i])) return OptionalByte.of(keys[i]); } - return (byte)0; + return OptionalByte.empty(); } @Override @@ -1304,7 +1307,7 @@ public class Byte2DoubleOpenCustomHashMap extends AbstractByte2DoubleMap impleme } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -1321,18 +1324,18 @@ public class Byte2DoubleOpenCustomHashMap extends AbstractByte2DoubleMap impleme } state = operator.applyAsDouble(state, values[i]); } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0D; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return OptionalDouble.empty(); + if(containsNull && filter.test(values[nullIndex])) return OptionalDouble.of(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], (byte)0) && filter.test(values[i])) return values[i]; + if(!strategy.equals(keys[i], (byte)0) && filter.test(values[i])) return OptionalDouble.of(values[i]); } - return 0D; + return OptionalDouble.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/bytes/maps/impl/customHash/Byte2FloatLinkedOpenCustomHashMap.java b/src/main/java/speiger/src/collections/bytes/maps/impl/customHash/Byte2FloatLinkedOpenCustomHashMap.java index d248aee..a6412c2 100644 --- a/src/main/java/speiger/src/collections/bytes/maps/impl/customHash/Byte2FloatLinkedOpenCustomHashMap.java +++ b/src/main/java/speiger/src/collections/bytes/maps/impl/customHash/Byte2FloatLinkedOpenCustomHashMap.java @@ -4,6 +4,7 @@ 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; @@ -14,6 +15,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectByteConsumer; import speiger.src.collections.ints.functions.consumer.IntByteConsumer; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntFloatConsumer; +import speiger.src.collections.bytes.functions.OptionalByte; import speiger.src.collections.bytes.functions.function.BytePredicate; import speiger.src.collections.bytes.functions.consumer.ByteFloatConsumer; import speiger.src.collections.bytes.functions.function.ByteByteUnaryOperator; @@ -33,6 +35,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectFloatConsumer; import speiger.src.collections.floats.functions.function.FloatPredicate; +import speiger.src.collections.floats.functions.OptionalFloat; import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOperator; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.objects.lists.ObjectListIterator; @@ -671,6 +674,10 @@ public class Byte2FloatLinkedOpenCustomHashMap extends Byte2FloatOpenCustomHashM } private class MapEntrySet extends AbstractObjectSet implements Byte2FloatOrderedMap.FastOrderedSet { + @Override + public void addFirst(Byte2FloatMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Byte2FloatMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Byte2FloatMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -836,7 +843,7 @@ public class Byte2FloatLinkedOpenCustomHashMap extends Byte2FloatOpenCustomHashM } @Override - public Byte2FloatMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Byte2FloatMap.Entry state = null; boolean empty = true; @@ -851,21 +858,21 @@ public class Byte2FloatLinkedOpenCustomHashMap extends Byte2FloatOpenCustomHashM state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Byte2FloatMap.Entry 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 @@ -944,10 +951,13 @@ public class Byte2FloatLinkedOpenCustomHashMap extends Byte2FloatOpenCustomHashM public boolean add(byte o) { throw new UnsupportedOperationException(); } - + + @Override + public void addFirst(byte o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(byte o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(byte o) { throw new UnsupportedOperationException(); } - @Override public boolean addAndMoveToLast(byte o) { throw new UnsupportedOperationException(); } @@ -1090,7 +1100,7 @@ public class Byte2FloatLinkedOpenCustomHashMap extends Byte2FloatOpenCustomHashM } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -1105,19 +1115,19 @@ public class Byte2FloatLinkedOpenCustomHashMap extends Byte2FloatOpenCustomHashM state = operator.applyAsByte(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (byte)0; + if(size() <= 0) return OptionalByte.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalByte.of(keys[index]); index = (int)links[index]; } - return (byte)0; + return OptionalByte.empty(); } @Override @@ -1252,7 +1262,7 @@ public class Byte2FloatLinkedOpenCustomHashMap extends Byte2FloatOpenCustomHashM } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -1267,19 +1277,19 @@ public class Byte2FloatLinkedOpenCustomHashMap extends Byte2FloatOpenCustomHashM state = operator.applyAsFloat(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0F; + if(size() <= 0) return OptionalFloat.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalFloat.of(values[index]); index = (int)links[index]; } - return 0F; + return OptionalFloat.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/bytes/maps/impl/customHash/Byte2FloatOpenCustomHashMap.java b/src/main/java/speiger/src/collections/bytes/maps/impl/customHash/Byte2FloatOpenCustomHashMap.java index 452bc6d..c576f40 100644 --- a/src/main/java/speiger/src/collections/bytes/maps/impl/customHash/Byte2FloatOpenCustomHashMap.java +++ b/src/main/java/speiger/src/collections/bytes/maps/impl/customHash/Byte2FloatOpenCustomHashMap.java @@ -5,6 +5,7 @@ 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; @@ -19,6 +20,7 @@ import speiger.src.collections.bytes.functions.consumer.ByteFloatConsumer; import speiger.src.collections.bytes.functions.function.Byte2FloatFunction; import speiger.src.collections.bytes.functions.function.ByteFloatUnaryOperator; import speiger.src.collections.bytes.functions.function.ByteByteUnaryOperator; +import speiger.src.collections.bytes.functions.OptionalByte; import speiger.src.collections.bytes.functions.function.BytePredicate; import speiger.src.collections.bytes.maps.abstracts.AbstractByte2FloatMap; import speiger.src.collections.bytes.maps.interfaces.Byte2FloatMap; @@ -37,6 +39,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectFloatConsumer; import speiger.src.collections.floats.functions.function.FloatPredicate; +import speiger.src.collections.floats.functions.OptionalFloat; import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.sets.AbstractObjectSet; import speiger.src.collections.objects.sets.ObjectSet; @@ -952,7 +955,7 @@ public class Byte2FloatOpenCustomHashMap extends AbstractByte2FloatMap implement } @Override - public Byte2FloatMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Byte2FloatMap.Entry state = null; boolean empty = true; @@ -969,25 +972,25 @@ public class Byte2FloatOpenCustomHashMap extends AbstractByte2FloatMap implement } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Byte2FloatMap.Entry 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], (byte)0)) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1161,7 +1164,7 @@ public class Byte2FloatOpenCustomHashMap extends AbstractByte2FloatMap implement } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -1178,18 +1181,18 @@ public class Byte2FloatOpenCustomHashMap extends AbstractByte2FloatMap implement } state = operator.applyAsByte(state, keys[i]); } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (byte)0; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OptionalByte.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OptionalByte.of(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], (byte)0) && filter.test(keys[i])) return keys[i]; + if(!strategy.equals(keys[i], (byte)0) && filter.test(keys[i])) return OptionalByte.of(keys[i]); } - return (byte)0; + return OptionalByte.empty(); } @Override @@ -1304,7 +1307,7 @@ public class Byte2FloatOpenCustomHashMap extends AbstractByte2FloatMap implement } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -1321,18 +1324,18 @@ public class Byte2FloatOpenCustomHashMap extends AbstractByte2FloatMap implement } state = operator.applyAsFloat(state, values[i]); } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0F; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return OptionalFloat.empty(); + if(containsNull && filter.test(values[nullIndex])) return OptionalFloat.of(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], (byte)0) && filter.test(values[i])) return values[i]; + if(!strategy.equals(keys[i], (byte)0) && filter.test(values[i])) return OptionalFloat.of(values[i]); } - return 0F; + return OptionalFloat.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/bytes/maps/impl/customHash/Byte2IntLinkedOpenCustomHashMap.java b/src/main/java/speiger/src/collections/bytes/maps/impl/customHash/Byte2IntLinkedOpenCustomHashMap.java index 2ca960a..746b069 100644 --- a/src/main/java/speiger/src/collections/bytes/maps/impl/customHash/Byte2IntLinkedOpenCustomHashMap.java +++ b/src/main/java/speiger/src/collections/bytes/maps/impl/customHash/Byte2IntLinkedOpenCustomHashMap.java @@ -4,10 +4,12 @@ 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; import java.util.function.IntPredicate; +import java.util.OptionalInt; import speiger.src.collections.bytes.collections.ByteBidirectionalIterator; import speiger.src.collections.bytes.functions.ByteConsumer; @@ -15,6 +17,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectByteConsumer; import speiger.src.collections.ints.functions.consumer.IntByteConsumer; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntIntConsumer; +import speiger.src.collections.bytes.functions.OptionalByte; import speiger.src.collections.bytes.functions.function.BytePredicate; import speiger.src.collections.bytes.functions.consumer.ByteIntConsumer; import speiger.src.collections.bytes.functions.function.ByteByteUnaryOperator; @@ -671,6 +674,10 @@ public class Byte2IntLinkedOpenCustomHashMap extends Byte2IntOpenCustomHashMap i } private class MapEntrySet extends AbstractObjectSet implements Byte2IntOrderedMap.FastOrderedSet { + @Override + public void addFirst(Byte2IntMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Byte2IntMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Byte2IntMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -836,7 +843,7 @@ public class Byte2IntLinkedOpenCustomHashMap extends Byte2IntOpenCustomHashMap i } @Override - public Byte2IntMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Byte2IntMap.Entry state = null; boolean empty = true; @@ -851,21 +858,21 @@ public class Byte2IntLinkedOpenCustomHashMap extends Byte2IntOpenCustomHashMap i state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Byte2IntMap.Entry 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 @@ -944,10 +951,13 @@ public class Byte2IntLinkedOpenCustomHashMap extends Byte2IntOpenCustomHashMap i public boolean add(byte o) { throw new UnsupportedOperationException(); } - + + @Override + public void addFirst(byte o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(byte o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(byte o) { throw new UnsupportedOperationException(); } - @Override public boolean addAndMoveToLast(byte o) { throw new UnsupportedOperationException(); } @@ -1090,7 +1100,7 @@ public class Byte2IntLinkedOpenCustomHashMap extends Byte2IntOpenCustomHashMap i } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -1105,19 +1115,19 @@ public class Byte2IntLinkedOpenCustomHashMap extends Byte2IntOpenCustomHashMap i state = operator.applyAsByte(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (byte)0; + if(size() <= 0) return OptionalByte.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalByte.of(keys[index]); index = (int)links[index]; } - return (byte)0; + return OptionalByte.empty(); } @Override @@ -1252,7 +1262,7 @@ public class Byte2IntLinkedOpenCustomHashMap extends Byte2IntOpenCustomHashMap i } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -1267,19 +1277,19 @@ public class Byte2IntLinkedOpenCustomHashMap extends Byte2IntOpenCustomHashMap i state = operator.applyAsInt(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0; + if(size() <= 0) return OptionalInt.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalInt.of(values[index]); index = (int)links[index]; } - return 0; + return OptionalInt.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/bytes/maps/impl/customHash/Byte2IntOpenCustomHashMap.java b/src/main/java/speiger/src/collections/bytes/maps/impl/customHash/Byte2IntOpenCustomHashMap.java index b8ac58f..5a952b3 100644 --- a/src/main/java/speiger/src/collections/bytes/maps/impl/customHash/Byte2IntOpenCustomHashMap.java +++ b/src/main/java/speiger/src/collections/bytes/maps/impl/customHash/Byte2IntOpenCustomHashMap.java @@ -5,10 +5,12 @@ 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; import java.util.function.IntPredicate; +import java.util.OptionalInt; import speiger.src.collections.bytes.collections.ByteIterator; import speiger.src.collections.bytes.functions.ByteConsumer; @@ -20,6 +22,7 @@ import speiger.src.collections.bytes.functions.consumer.ByteIntConsumer; import speiger.src.collections.bytes.functions.function.Byte2IntFunction; import speiger.src.collections.bytes.functions.function.ByteIntUnaryOperator; import speiger.src.collections.bytes.functions.function.ByteByteUnaryOperator; +import speiger.src.collections.bytes.functions.OptionalByte; import speiger.src.collections.bytes.functions.function.BytePredicate; import speiger.src.collections.bytes.maps.abstracts.AbstractByte2IntMap; import speiger.src.collections.bytes.maps.interfaces.Byte2IntMap; @@ -952,7 +955,7 @@ public class Byte2IntOpenCustomHashMap extends AbstractByte2IntMap implements IT } @Override - public Byte2IntMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Byte2IntMap.Entry state = null; boolean empty = true; @@ -969,25 +972,25 @@ public class Byte2IntOpenCustomHashMap extends AbstractByte2IntMap implements IT } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Byte2IntMap.Entry 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], (byte)0)) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1161,7 +1164,7 @@ public class Byte2IntOpenCustomHashMap extends AbstractByte2IntMap implements IT } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -1178,18 +1181,18 @@ public class Byte2IntOpenCustomHashMap extends AbstractByte2IntMap implements IT } state = operator.applyAsByte(state, keys[i]); } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (byte)0; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OptionalByte.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OptionalByte.of(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], (byte)0) && filter.test(keys[i])) return keys[i]; + if(!strategy.equals(keys[i], (byte)0) && filter.test(keys[i])) return OptionalByte.of(keys[i]); } - return (byte)0; + return OptionalByte.empty(); } @Override @@ -1304,7 +1307,7 @@ public class Byte2IntOpenCustomHashMap extends AbstractByte2IntMap implements IT } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -1321,18 +1324,18 @@ public class Byte2IntOpenCustomHashMap extends AbstractByte2IntMap implements IT } state = operator.applyAsInt(state, values[i]); } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return OptionalInt.empty(); + if(containsNull && filter.test(values[nullIndex])) return OptionalInt.of(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], (byte)0) && filter.test(values[i])) return values[i]; + if(!strategy.equals(keys[i], (byte)0) && filter.test(values[i])) return OptionalInt.of(values[i]); } - return 0; + return OptionalInt.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/bytes/maps/impl/customHash/Byte2LongLinkedOpenCustomHashMap.java b/src/main/java/speiger/src/collections/bytes/maps/impl/customHash/Byte2LongLinkedOpenCustomHashMap.java index a05766e..839b07a 100644 --- a/src/main/java/speiger/src/collections/bytes/maps/impl/customHash/Byte2LongLinkedOpenCustomHashMap.java +++ b/src/main/java/speiger/src/collections/bytes/maps/impl/customHash/Byte2LongLinkedOpenCustomHashMap.java @@ -4,10 +4,12 @@ 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; import java.util.function.LongPredicate; +import java.util.OptionalLong; import speiger.src.collections.bytes.collections.ByteBidirectionalIterator; import speiger.src.collections.bytes.functions.ByteConsumer; @@ -15,6 +17,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectByteConsumer; import speiger.src.collections.ints.functions.consumer.IntByteConsumer; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntLongConsumer; +import speiger.src.collections.bytes.functions.OptionalByte; import speiger.src.collections.bytes.functions.function.BytePredicate; import speiger.src.collections.bytes.functions.consumer.ByteLongConsumer; import speiger.src.collections.bytes.functions.function.ByteByteUnaryOperator; @@ -671,6 +674,10 @@ public class Byte2LongLinkedOpenCustomHashMap extends Byte2LongOpenCustomHashMap } private class MapEntrySet extends AbstractObjectSet implements Byte2LongOrderedMap.FastOrderedSet { + @Override + public void addFirst(Byte2LongMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Byte2LongMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Byte2LongMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -836,7 +843,7 @@ public class Byte2LongLinkedOpenCustomHashMap extends Byte2LongOpenCustomHashMap } @Override - public Byte2LongMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Byte2LongMap.Entry state = null; boolean empty = true; @@ -851,21 +858,21 @@ public class Byte2LongLinkedOpenCustomHashMap extends Byte2LongOpenCustomHashMap state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Byte2LongMap.Entry 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 @@ -944,10 +951,13 @@ public class Byte2LongLinkedOpenCustomHashMap extends Byte2LongOpenCustomHashMap public boolean add(byte o) { throw new UnsupportedOperationException(); } - + + @Override + public void addFirst(byte o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(byte o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(byte o) { throw new UnsupportedOperationException(); } - @Override public boolean addAndMoveToLast(byte o) { throw new UnsupportedOperationException(); } @@ -1090,7 +1100,7 @@ public class Byte2LongLinkedOpenCustomHashMap extends Byte2LongOpenCustomHashMap } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -1105,19 +1115,19 @@ public class Byte2LongLinkedOpenCustomHashMap extends Byte2LongOpenCustomHashMap state = operator.applyAsByte(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (byte)0; + if(size() <= 0) return OptionalByte.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalByte.of(keys[index]); index = (int)links[index]; } - return (byte)0; + return OptionalByte.empty(); } @Override @@ -1252,7 +1262,7 @@ public class Byte2LongLinkedOpenCustomHashMap extends Byte2LongOpenCustomHashMap } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -1267,19 +1277,19 @@ public class Byte2LongLinkedOpenCustomHashMap extends Byte2LongOpenCustomHashMap state = operator.applyAsLong(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0L; + if(size() <= 0) return OptionalLong.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalLong.of(values[index]); index = (int)links[index]; } - return 0L; + return OptionalLong.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/bytes/maps/impl/customHash/Byte2LongOpenCustomHashMap.java b/src/main/java/speiger/src/collections/bytes/maps/impl/customHash/Byte2LongOpenCustomHashMap.java index 6b68b2d..12f0fbd 100644 --- a/src/main/java/speiger/src/collections/bytes/maps/impl/customHash/Byte2LongOpenCustomHashMap.java +++ b/src/main/java/speiger/src/collections/bytes/maps/impl/customHash/Byte2LongOpenCustomHashMap.java @@ -5,10 +5,12 @@ 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; import java.util.function.LongPredicate; +import java.util.OptionalLong; import speiger.src.collections.bytes.collections.ByteIterator; import speiger.src.collections.bytes.functions.ByteConsumer; @@ -20,6 +22,7 @@ import speiger.src.collections.bytes.functions.consumer.ByteLongConsumer; import speiger.src.collections.bytes.functions.function.Byte2LongFunction; import speiger.src.collections.bytes.functions.function.ByteLongUnaryOperator; import speiger.src.collections.bytes.functions.function.ByteByteUnaryOperator; +import speiger.src.collections.bytes.functions.OptionalByte; import speiger.src.collections.bytes.functions.function.BytePredicate; import speiger.src.collections.bytes.maps.abstracts.AbstractByte2LongMap; import speiger.src.collections.bytes.maps.interfaces.Byte2LongMap; @@ -952,7 +955,7 @@ public class Byte2LongOpenCustomHashMap extends AbstractByte2LongMap implements } @Override - public Byte2LongMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Byte2LongMap.Entry state = null; boolean empty = true; @@ -969,25 +972,25 @@ public class Byte2LongOpenCustomHashMap extends AbstractByte2LongMap implements } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Byte2LongMap.Entry 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], (byte)0)) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1161,7 +1164,7 @@ public class Byte2LongOpenCustomHashMap extends AbstractByte2LongMap implements } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -1178,18 +1181,18 @@ public class Byte2LongOpenCustomHashMap extends AbstractByte2LongMap implements } state = operator.applyAsByte(state, keys[i]); } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (byte)0; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OptionalByte.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OptionalByte.of(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], (byte)0) && filter.test(keys[i])) return keys[i]; + if(!strategy.equals(keys[i], (byte)0) && filter.test(keys[i])) return OptionalByte.of(keys[i]); } - return (byte)0; + return OptionalByte.empty(); } @Override @@ -1304,7 +1307,7 @@ public class Byte2LongOpenCustomHashMap extends AbstractByte2LongMap implements } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -1321,18 +1324,18 @@ public class Byte2LongOpenCustomHashMap extends AbstractByte2LongMap implements } state = operator.applyAsLong(state, values[i]); } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0L; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return OptionalLong.empty(); + if(containsNull && filter.test(values[nullIndex])) return OptionalLong.of(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], (byte)0) && filter.test(values[i])) return values[i]; + if(!strategy.equals(keys[i], (byte)0) && filter.test(values[i])) return OptionalLong.of(values[i]); } - return 0L; + return OptionalLong.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/bytes/maps/impl/customHash/Byte2ObjectLinkedOpenCustomHashMap.java b/src/main/java/speiger/src/collections/bytes/maps/impl/customHash/Byte2ObjectLinkedOpenCustomHashMap.java index cbd1321..616323e 100644 --- a/src/main/java/speiger/src/collections/bytes/maps/impl/customHash/Byte2ObjectLinkedOpenCustomHashMap.java +++ b/src/main/java/speiger/src/collections/bytes/maps/impl/customHash/Byte2ObjectLinkedOpenCustomHashMap.java @@ -4,6 +4,7 @@ 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; @@ -13,6 +14,7 @@ import speiger.src.collections.bytes.functions.ByteConsumer; import speiger.src.collections.objects.functions.consumer.ObjectByteConsumer; import speiger.src.collections.ints.functions.consumer.IntByteConsumer; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; +import speiger.src.collections.bytes.functions.OptionalByte; import speiger.src.collections.bytes.functions.function.BytePredicate; import speiger.src.collections.bytes.functions.consumer.ByteObjectConsumer; import speiger.src.collections.bytes.functions.function.ByteByteUnaryOperator; @@ -666,6 +668,10 @@ public class Byte2ObjectLinkedOpenCustomHashMap extends Byte2ObjectOpenCustom } private class MapEntrySet extends AbstractObjectSet> implements Byte2ObjectOrderedMap.FastOrderedSet { + @Override + public void addFirst(Byte2ObjectMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Byte2ObjectMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Byte2ObjectMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -831,7 +837,7 @@ public class Byte2ObjectLinkedOpenCustomHashMap extends Byte2ObjectOpenCustom } @Override - public Byte2ObjectMap.Entry reduce(ObjectObjectUnaryOperator, Byte2ObjectMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Byte2ObjectMap.Entry> operator) { Objects.requireNonNull(operator); Byte2ObjectMap.Entry state = null; boolean empty = true; @@ -846,21 +852,21 @@ public class Byte2ObjectLinkedOpenCustomHashMap extends Byte2ObjectOpenCustom state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Byte2ObjectMap.Entry 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 @@ -939,10 +945,13 @@ public class Byte2ObjectLinkedOpenCustomHashMap extends Byte2ObjectOpenCustom public boolean add(byte o) { throw new UnsupportedOperationException(); } - + + @Override + public void addFirst(byte o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(byte o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(byte o) { throw new UnsupportedOperationException(); } - @Override public boolean addAndMoveToLast(byte o) { throw new UnsupportedOperationException(); } @@ -1085,7 +1094,7 @@ public class Byte2ObjectLinkedOpenCustomHashMap extends Byte2ObjectOpenCustom } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -1100,19 +1109,19 @@ public class Byte2ObjectLinkedOpenCustomHashMap extends Byte2ObjectOpenCustom state = operator.applyAsByte(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (byte)0; + if(size() <= 0) return OptionalByte.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalByte.of(keys[index]); index = (int)links[index]; } - return (byte)0; + return OptionalByte.empty(); } @Override @@ -1248,7 +1257,7 @@ public class Byte2ObjectLinkedOpenCustomHashMap extends Byte2ObjectOpenCustom } @Override - public V reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); V state = null; boolean empty = true; @@ -1263,19 +1272,19 @@ public class Byte2ObjectLinkedOpenCustomHashMap extends Byte2ObjectOpenCustom state = operator.apply(state, values[index]); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public V findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return Optional.ofNullable(values[index]); index = (int)links[index]; } - return null; + return Optional.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/bytes/maps/impl/customHash/Byte2ObjectOpenCustomHashMap.java b/src/main/java/speiger/src/collections/bytes/maps/impl/customHash/Byte2ObjectOpenCustomHashMap.java index 0e3d8ab..366f49b 100644 --- a/src/main/java/speiger/src/collections/bytes/maps/impl/customHash/Byte2ObjectOpenCustomHashMap.java +++ b/src/main/java/speiger/src/collections/bytes/maps/impl/customHash/Byte2ObjectOpenCustomHashMap.java @@ -5,6 +5,7 @@ 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; @@ -18,6 +19,7 @@ import speiger.src.collections.bytes.functions.consumer.ByteObjectConsumer; import speiger.src.collections.bytes.functions.function.ByteFunction; import speiger.src.collections.bytes.functions.function.ByteObjectUnaryOperator; import speiger.src.collections.bytes.functions.function.ByteByteUnaryOperator; +import speiger.src.collections.bytes.functions.OptionalByte; import speiger.src.collections.bytes.functions.function.BytePredicate; import speiger.src.collections.bytes.maps.abstracts.AbstractByte2ObjectMap; import speiger.src.collections.bytes.maps.interfaces.Byte2ObjectMap; @@ -868,7 +870,7 @@ public class Byte2ObjectOpenCustomHashMap extends AbstractByte2ObjectMap i } @Override - public Byte2ObjectMap.Entry reduce(ObjectObjectUnaryOperator, Byte2ObjectMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Byte2ObjectMap.Entry> operator) { Objects.requireNonNull(operator); Byte2ObjectMap.Entry state = null; boolean empty = true; @@ -885,25 +887,25 @@ public class Byte2ObjectOpenCustomHashMap extends AbstractByte2ObjectMap i } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Byte2ObjectMap.Entry 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], (byte)0)) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1077,7 +1079,7 @@ public class Byte2ObjectOpenCustomHashMap extends AbstractByte2ObjectMap i } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -1094,18 +1096,18 @@ public class Byte2ObjectOpenCustomHashMap extends AbstractByte2ObjectMap i } state = operator.applyAsByte(state, keys[i]); } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (byte)0; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OptionalByte.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OptionalByte.of(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], (byte)0) && filter.test(keys[i])) return keys[i]; + if(!strategy.equals(keys[i], (byte)0) && filter.test(keys[i])) return OptionalByte.of(keys[i]); } - return (byte)0; + return OptionalByte.empty(); } @Override @@ -1220,7 +1222,7 @@ public class Byte2ObjectOpenCustomHashMap extends AbstractByte2ObjectMap i } @Override - public V reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); V state = null; boolean empty = true; @@ -1237,18 +1239,18 @@ public class Byte2ObjectOpenCustomHashMap extends AbstractByte2ObjectMap i } state = operator.apply(state, values[i]); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public V findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return Optional.empty(); + if(containsNull && filter.test(values[nullIndex])) return Optional.ofNullable(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], (byte)0) && filter.test(values[i])) return values[i]; + if(!strategy.equals(keys[i], (byte)0) && filter.test(values[i])) return Optional.ofNullable(values[i]); } - return null; + return Optional.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/bytes/maps/impl/customHash/Byte2ShortLinkedOpenCustomHashMap.java b/src/main/java/speiger/src/collections/bytes/maps/impl/customHash/Byte2ShortLinkedOpenCustomHashMap.java index f781472..4144439 100644 --- a/src/main/java/speiger/src/collections/bytes/maps/impl/customHash/Byte2ShortLinkedOpenCustomHashMap.java +++ b/src/main/java/speiger/src/collections/bytes/maps/impl/customHash/Byte2ShortLinkedOpenCustomHashMap.java @@ -4,6 +4,7 @@ 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; @@ -14,6 +15,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectByteConsumer; import speiger.src.collections.ints.functions.consumer.IntByteConsumer; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntShortConsumer; +import speiger.src.collections.bytes.functions.OptionalByte; import speiger.src.collections.bytes.functions.function.BytePredicate; import speiger.src.collections.bytes.functions.consumer.ByteShortConsumer; import speiger.src.collections.bytes.functions.function.ByteByteUnaryOperator; @@ -33,6 +35,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectShortConsumer; import speiger.src.collections.shorts.functions.function.ShortPredicate; +import speiger.src.collections.shorts.functions.OptionalShort; import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOperator; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.objects.lists.ObjectListIterator; @@ -671,6 +674,10 @@ public class Byte2ShortLinkedOpenCustomHashMap extends Byte2ShortOpenCustomHashM } private class MapEntrySet extends AbstractObjectSet implements Byte2ShortOrderedMap.FastOrderedSet { + @Override + public void addFirst(Byte2ShortMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Byte2ShortMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Byte2ShortMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -836,7 +843,7 @@ public class Byte2ShortLinkedOpenCustomHashMap extends Byte2ShortOpenCustomHashM } @Override - public Byte2ShortMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Byte2ShortMap.Entry state = null; boolean empty = true; @@ -851,21 +858,21 @@ public class Byte2ShortLinkedOpenCustomHashMap extends Byte2ShortOpenCustomHashM state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Byte2ShortMap.Entry 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 @@ -944,10 +951,13 @@ public class Byte2ShortLinkedOpenCustomHashMap extends Byte2ShortOpenCustomHashM public boolean add(byte o) { throw new UnsupportedOperationException(); } - + + @Override + public void addFirst(byte o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(byte o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(byte o) { throw new UnsupportedOperationException(); } - @Override public boolean addAndMoveToLast(byte o) { throw new UnsupportedOperationException(); } @@ -1090,7 +1100,7 @@ public class Byte2ShortLinkedOpenCustomHashMap extends Byte2ShortOpenCustomHashM } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -1105,19 +1115,19 @@ public class Byte2ShortLinkedOpenCustomHashMap extends Byte2ShortOpenCustomHashM state = operator.applyAsByte(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (byte)0; + if(size() <= 0) return OptionalByte.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalByte.of(keys[index]); index = (int)links[index]; } - return (byte)0; + return OptionalByte.empty(); } @Override @@ -1252,7 +1262,7 @@ public class Byte2ShortLinkedOpenCustomHashMap extends Byte2ShortOpenCustomHashM } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -1267,19 +1277,19 @@ public class Byte2ShortLinkedOpenCustomHashMap extends Byte2ShortOpenCustomHashM state = operator.applyAsShort(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (short)0; + if(size() <= 0) return OptionalShort.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalShort.of(values[index]); index = (int)links[index]; } - return (short)0; + return OptionalShort.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/bytes/maps/impl/customHash/Byte2ShortOpenCustomHashMap.java b/src/main/java/speiger/src/collections/bytes/maps/impl/customHash/Byte2ShortOpenCustomHashMap.java index d27135f..4c7e914 100644 --- a/src/main/java/speiger/src/collections/bytes/maps/impl/customHash/Byte2ShortOpenCustomHashMap.java +++ b/src/main/java/speiger/src/collections/bytes/maps/impl/customHash/Byte2ShortOpenCustomHashMap.java @@ -5,6 +5,7 @@ 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; @@ -19,6 +20,7 @@ import speiger.src.collections.bytes.functions.consumer.ByteShortConsumer; import speiger.src.collections.bytes.functions.function.Byte2ShortFunction; import speiger.src.collections.bytes.functions.function.ByteShortUnaryOperator; import speiger.src.collections.bytes.functions.function.ByteByteUnaryOperator; +import speiger.src.collections.bytes.functions.OptionalByte; import speiger.src.collections.bytes.functions.function.BytePredicate; import speiger.src.collections.bytes.maps.abstracts.AbstractByte2ShortMap; import speiger.src.collections.bytes.maps.interfaces.Byte2ShortMap; @@ -37,6 +39,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectShortConsumer; import speiger.src.collections.shorts.functions.function.ShortPredicate; +import speiger.src.collections.shorts.functions.OptionalShort; import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.sets.AbstractObjectSet; import speiger.src.collections.objects.sets.ObjectSet; @@ -952,7 +955,7 @@ public class Byte2ShortOpenCustomHashMap extends AbstractByte2ShortMap implement } @Override - public Byte2ShortMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Byte2ShortMap.Entry state = null; boolean empty = true; @@ -969,25 +972,25 @@ public class Byte2ShortOpenCustomHashMap extends AbstractByte2ShortMap implement } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Byte2ShortMap.Entry 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], (byte)0)) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1161,7 +1164,7 @@ public class Byte2ShortOpenCustomHashMap extends AbstractByte2ShortMap implement } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -1178,18 +1181,18 @@ public class Byte2ShortOpenCustomHashMap extends AbstractByte2ShortMap implement } state = operator.applyAsByte(state, keys[i]); } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (byte)0; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OptionalByte.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OptionalByte.of(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], (byte)0) && filter.test(keys[i])) return keys[i]; + if(!strategy.equals(keys[i], (byte)0) && filter.test(keys[i])) return OptionalByte.of(keys[i]); } - return (byte)0; + return OptionalByte.empty(); } @Override @@ -1304,7 +1307,7 @@ public class Byte2ShortOpenCustomHashMap extends AbstractByte2ShortMap implement } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -1321,18 +1324,18 @@ public class Byte2ShortOpenCustomHashMap extends AbstractByte2ShortMap implement } state = operator.applyAsShort(state, values[i]); } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (short)0; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return OptionalShort.empty(); + if(containsNull && filter.test(values[nullIndex])) return OptionalShort.of(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], (byte)0) && filter.test(values[i])) return values[i]; + if(!strategy.equals(keys[i], (byte)0) && filter.test(values[i])) return OptionalShort.of(values[i]); } - return (short)0; + return OptionalShort.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/bytes/maps/impl/hash/Byte2BooleanLinkedOpenHashMap.java b/src/main/java/speiger/src/collections/bytes/maps/impl/hash/Byte2BooleanLinkedOpenHashMap.java index bf550d1..54e359f 100644 --- a/src/main/java/speiger/src/collections/bytes/maps/impl/hash/Byte2BooleanLinkedOpenHashMap.java +++ b/src/main/java/speiger/src/collections/bytes/maps/impl/hash/Byte2BooleanLinkedOpenHashMap.java @@ -4,6 +4,7 @@ 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; @@ -14,6 +15,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectByteConsumer; import speiger.src.collections.ints.functions.consumer.IntByteConsumer; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntBooleanConsumer; +import speiger.src.collections.bytes.functions.OptionalByte; import speiger.src.collections.bytes.functions.function.BytePredicate; import speiger.src.collections.bytes.functions.consumer.ByteBooleanConsumer; import speiger.src.collections.bytes.functions.function.ByteByteUnaryOperator; @@ -32,6 +34,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectBooleanConsumer; import speiger.src.collections.booleans.functions.function.BooleanPredicate; +import speiger.src.collections.booleans.functions.OptionalBoolean; import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOperator; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.objects.lists.ObjectListIterator; @@ -669,6 +672,10 @@ public class Byte2BooleanLinkedOpenHashMap extends Byte2BooleanOpenHashMap imple } private class MapEntrySet extends AbstractObjectSet implements Byte2BooleanOrderedMap.FastOrderedSet { + @Override + public void addFirst(Byte2BooleanMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Byte2BooleanMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Byte2BooleanMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -834,7 +841,7 @@ public class Byte2BooleanLinkedOpenHashMap extends Byte2BooleanOpenHashMap imple } @Override - public Byte2BooleanMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Byte2BooleanMap.Entry state = null; boolean empty = true; @@ -849,21 +856,21 @@ public class Byte2BooleanLinkedOpenHashMap extends Byte2BooleanOpenHashMap imple state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Byte2BooleanMap.Entry 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 @@ -939,6 +946,12 @@ public class Byte2BooleanLinkedOpenHashMap extends Byte2BooleanOpenHashMap imple @Override public boolean add(byte o) { throw new UnsupportedOperationException(); } + + @Override + public void addFirst(byte o) { throw new UnsupportedOperationException(); } + + @Override + public void addLast(byte o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(byte o) { throw new UnsupportedOperationException(); } @@ -1085,7 +1098,7 @@ public class Byte2BooleanLinkedOpenHashMap extends Byte2BooleanOpenHashMap imple } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -1100,19 +1113,19 @@ public class Byte2BooleanLinkedOpenHashMap extends Byte2BooleanOpenHashMap imple state = operator.applyAsByte(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (byte)0; + if(size() <= 0) return OptionalByte.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalByte.of(keys[index]); index = (int)links[index]; } - return (byte)0; + return OptionalByte.empty(); } @Override @@ -1248,7 +1261,7 @@ public class Byte2BooleanLinkedOpenHashMap extends Byte2BooleanOpenHashMap imple } @Override - public boolean reduce(BooleanBooleanUnaryOperator operator) { + public OptionalBoolean reduce(BooleanBooleanUnaryOperator operator) { Objects.requireNonNull(operator); boolean state = false; boolean empty = true; @@ -1263,19 +1276,19 @@ public class Byte2BooleanLinkedOpenHashMap extends Byte2BooleanOpenHashMap imple state = operator.applyAsBoolean(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalBoolean.empty() : OptionalBoolean.of(state); } @Override - public boolean findFirst(BooleanPredicate filter) { + public OptionalBoolean findFirst(BooleanPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return false; + if(size() <= 0) return OptionalBoolean.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalBoolean.of(values[index]); index = (int)links[index]; } - return false; + return OptionalBoolean.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/bytes/maps/impl/hash/Byte2BooleanOpenHashMap.java b/src/main/java/speiger/src/collections/bytes/maps/impl/hash/Byte2BooleanOpenHashMap.java index ba5debd..e6af026 100644 --- a/src/main/java/speiger/src/collections/bytes/maps/impl/hash/Byte2BooleanOpenHashMap.java +++ b/src/main/java/speiger/src/collections/bytes/maps/impl/hash/Byte2BooleanOpenHashMap.java @@ -5,6 +5,7 @@ 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; @@ -19,6 +20,7 @@ import speiger.src.collections.bytes.functions.consumer.ByteBooleanConsumer; import speiger.src.collections.bytes.functions.function.BytePredicate; import speiger.src.collections.bytes.functions.function.ByteBooleanUnaryOperator; import speiger.src.collections.bytes.functions.function.ByteByteUnaryOperator; +import speiger.src.collections.bytes.functions.OptionalByte; import speiger.src.collections.bytes.maps.abstracts.AbstractByte2BooleanMap; import speiger.src.collections.bytes.maps.interfaces.Byte2BooleanMap; import speiger.src.collections.bytes.sets.AbstractByteSet; @@ -33,6 +35,7 @@ import speiger.src.collections.booleans.functions.BooleanConsumer; import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectBooleanConsumer; import speiger.src.collections.booleans.functions.function.BooleanPredicate; +import speiger.src.collections.booleans.functions.OptionalBoolean; import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.sets.AbstractObjectSet; import speiger.src.collections.objects.sets.ObjectSet; @@ -898,7 +901,7 @@ public class Byte2BooleanOpenHashMap extends AbstractByte2BooleanMap implements } @Override - public Byte2BooleanMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Byte2BooleanMap.Entry state = null; boolean empty = true; @@ -915,25 +918,25 @@ public class Byte2BooleanOpenHashMap extends AbstractByte2BooleanMap implements } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Byte2BooleanMap.Entry 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(keys[i] != (byte)0) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1105,7 +1108,7 @@ public class Byte2BooleanOpenHashMap extends AbstractByte2BooleanMap implements } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -1122,18 +1125,18 @@ public class Byte2BooleanOpenHashMap extends AbstractByte2BooleanMap implements } state = operator.applyAsByte(state, keys[i]); } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (byte)0; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OptionalByte.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OptionalByte.of(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(keys[i] != (byte)0 && filter.test(keys[i])) return keys[i]; + if(keys[i] != (byte)0 && filter.test(keys[i])) return OptionalByte.of(keys[i]); } - return (byte)0; + return OptionalByte.empty(); } @Override @@ -1248,7 +1251,7 @@ public class Byte2BooleanOpenHashMap extends AbstractByte2BooleanMap implements } @Override - public boolean reduce(BooleanBooleanUnaryOperator operator) { + public OptionalBoolean reduce(BooleanBooleanUnaryOperator operator) { Objects.requireNonNull(operator); boolean state = false; boolean empty = true; @@ -1265,18 +1268,18 @@ public class Byte2BooleanOpenHashMap extends AbstractByte2BooleanMap implements } state = operator.applyAsBoolean(state, values[i]); } - return state; + return empty ? OptionalBoolean.empty() : OptionalBoolean.of(state); } @Override - public boolean findFirst(BooleanPredicate filter) { + public OptionalBoolean findFirst(BooleanPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return false; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return OptionalBoolean.empty(); + if(containsNull && filter.test(values[nullIndex])) return OptionalBoolean.of(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(keys[i] != (byte)0 && filter.test(values[i])) return values[i]; + if(keys[i] != (byte)0 && filter.test(values[i])) return OptionalBoolean.of(values[i]); } - return false; + return OptionalBoolean.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/bytes/maps/impl/hash/Byte2ByteLinkedOpenHashMap.java b/src/main/java/speiger/src/collections/bytes/maps/impl/hash/Byte2ByteLinkedOpenHashMap.java index d4403b2..91f6532 100644 --- a/src/main/java/speiger/src/collections/bytes/maps/impl/hash/Byte2ByteLinkedOpenHashMap.java +++ b/src/main/java/speiger/src/collections/bytes/maps/impl/hash/Byte2ByteLinkedOpenHashMap.java @@ -4,6 +4,7 @@ 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; @@ -13,6 +14,7 @@ import speiger.src.collections.bytes.functions.ByteConsumer; import speiger.src.collections.objects.functions.consumer.ObjectByteConsumer; import speiger.src.collections.ints.functions.consumer.IntByteConsumer; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; +import speiger.src.collections.bytes.functions.OptionalByte; import speiger.src.collections.bytes.functions.function.BytePredicate; import speiger.src.collections.bytes.functions.consumer.ByteByteConsumer; import speiger.src.collections.bytes.functions.function.ByteByteUnaryOperator; @@ -662,6 +664,10 @@ public class Byte2ByteLinkedOpenHashMap extends Byte2ByteOpenHashMap implements } private class MapEntrySet extends AbstractObjectSet implements Byte2ByteOrderedMap.FastOrderedSet { + @Override + public void addFirst(Byte2ByteMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Byte2ByteMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Byte2ByteMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -827,7 +833,7 @@ public class Byte2ByteLinkedOpenHashMap extends Byte2ByteOpenHashMap implements } @Override - public Byte2ByteMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Byte2ByteMap.Entry state = null; boolean empty = true; @@ -842,21 +848,21 @@ public class Byte2ByteLinkedOpenHashMap extends Byte2ByteOpenHashMap implements state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Byte2ByteMap.Entry 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 @@ -932,6 +938,12 @@ public class Byte2ByteLinkedOpenHashMap extends Byte2ByteOpenHashMap implements @Override public boolean add(byte o) { throw new UnsupportedOperationException(); } + + @Override + public void addFirst(byte o) { throw new UnsupportedOperationException(); } + + @Override + public void addLast(byte o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(byte o) { throw new UnsupportedOperationException(); } @@ -1078,7 +1090,7 @@ public class Byte2ByteLinkedOpenHashMap extends Byte2ByteOpenHashMap implements } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -1093,19 +1105,19 @@ public class Byte2ByteLinkedOpenHashMap extends Byte2ByteOpenHashMap implements state = operator.applyAsByte(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (byte)0; + if(size() <= 0) return OptionalByte.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalByte.of(keys[index]); index = (int)links[index]; } - return (byte)0; + return OptionalByte.empty(); } @Override @@ -1241,7 +1253,7 @@ public class Byte2ByteLinkedOpenHashMap extends Byte2ByteOpenHashMap implements } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -1256,19 +1268,19 @@ public class Byte2ByteLinkedOpenHashMap extends Byte2ByteOpenHashMap implements state = operator.applyAsByte(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (byte)0; + if(size() <= 0) return OptionalByte.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalByte.of(values[index]); index = (int)links[index]; } - return (byte)0; + return OptionalByte.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/bytes/maps/impl/hash/Byte2ByteOpenHashMap.java b/src/main/java/speiger/src/collections/bytes/maps/impl/hash/Byte2ByteOpenHashMap.java index 90ff012..ae08bfb 100644 --- a/src/main/java/speiger/src/collections/bytes/maps/impl/hash/Byte2ByteOpenHashMap.java +++ b/src/main/java/speiger/src/collections/bytes/maps/impl/hash/Byte2ByteOpenHashMap.java @@ -5,6 +5,7 @@ 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; @@ -17,6 +18,7 @@ import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.bytes.functions.consumer.ByteByteConsumer; import speiger.src.collections.bytes.functions.function.ByteUnaryOperator; import speiger.src.collections.bytes.functions.function.ByteByteUnaryOperator; +import speiger.src.collections.bytes.functions.OptionalByte; import speiger.src.collections.bytes.functions.function.BytePredicate; import speiger.src.collections.bytes.maps.abstracts.AbstractByte2ByteMap; import speiger.src.collections.bytes.maps.interfaces.Byte2ByteMap; @@ -914,7 +916,7 @@ public class Byte2ByteOpenHashMap extends AbstractByte2ByteMap implements ITrimm } @Override - public Byte2ByteMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Byte2ByteMap.Entry state = null; boolean empty = true; @@ -931,25 +933,25 @@ public class Byte2ByteOpenHashMap extends AbstractByte2ByteMap implements ITrimm } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Byte2ByteMap.Entry 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(keys[i] != (byte)0) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1121,7 +1123,7 @@ public class Byte2ByteOpenHashMap extends AbstractByte2ByteMap implements ITrimm } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -1138,18 +1140,18 @@ public class Byte2ByteOpenHashMap extends AbstractByte2ByteMap implements ITrimm } state = operator.applyAsByte(state, keys[i]); } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (byte)0; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OptionalByte.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OptionalByte.of(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(keys[i] != (byte)0 && filter.test(keys[i])) return keys[i]; + if(keys[i] != (byte)0 && filter.test(keys[i])) return OptionalByte.of(keys[i]); } - return (byte)0; + return OptionalByte.empty(); } @Override @@ -1264,7 +1266,7 @@ public class Byte2ByteOpenHashMap extends AbstractByte2ByteMap implements ITrimm } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -1281,18 +1283,18 @@ public class Byte2ByteOpenHashMap extends AbstractByte2ByteMap implements ITrimm } state = operator.applyAsByte(state, values[i]); } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (byte)0; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return OptionalByte.empty(); + if(containsNull && filter.test(values[nullIndex])) return OptionalByte.of(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(keys[i] != (byte)0 && filter.test(values[i])) return values[i]; + if(keys[i] != (byte)0 && filter.test(values[i])) return OptionalByte.of(values[i]); } - return (byte)0; + return OptionalByte.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/bytes/maps/impl/hash/Byte2CharLinkedOpenHashMap.java b/src/main/java/speiger/src/collections/bytes/maps/impl/hash/Byte2CharLinkedOpenHashMap.java index be5580d..77b72f5 100644 --- a/src/main/java/speiger/src/collections/bytes/maps/impl/hash/Byte2CharLinkedOpenHashMap.java +++ b/src/main/java/speiger/src/collections/bytes/maps/impl/hash/Byte2CharLinkedOpenHashMap.java @@ -4,6 +4,7 @@ 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; @@ -14,6 +15,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectByteConsumer; import speiger.src.collections.ints.functions.consumer.IntByteConsumer; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntCharConsumer; +import speiger.src.collections.bytes.functions.OptionalByte; import speiger.src.collections.bytes.functions.function.BytePredicate; import speiger.src.collections.bytes.functions.consumer.ByteCharConsumer; import speiger.src.collections.bytes.functions.function.ByteByteUnaryOperator; @@ -32,6 +34,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectCharConsumer; import speiger.src.collections.chars.functions.function.CharPredicate; +import speiger.src.collections.chars.functions.OptionalChar; import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOperator; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.objects.lists.ObjectListIterator; @@ -669,6 +672,10 @@ public class Byte2CharLinkedOpenHashMap extends Byte2CharOpenHashMap implements } private class MapEntrySet extends AbstractObjectSet implements Byte2CharOrderedMap.FastOrderedSet { + @Override + public void addFirst(Byte2CharMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Byte2CharMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Byte2CharMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -834,7 +841,7 @@ public class Byte2CharLinkedOpenHashMap extends Byte2CharOpenHashMap implements } @Override - public Byte2CharMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Byte2CharMap.Entry state = null; boolean empty = true; @@ -849,21 +856,21 @@ public class Byte2CharLinkedOpenHashMap extends Byte2CharOpenHashMap implements state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Byte2CharMap.Entry 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 @@ -939,6 +946,12 @@ public class Byte2CharLinkedOpenHashMap extends Byte2CharOpenHashMap implements @Override public boolean add(byte o) { throw new UnsupportedOperationException(); } + + @Override + public void addFirst(byte o) { throw new UnsupportedOperationException(); } + + @Override + public void addLast(byte o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(byte o) { throw new UnsupportedOperationException(); } @@ -1085,7 +1098,7 @@ public class Byte2CharLinkedOpenHashMap extends Byte2CharOpenHashMap implements } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -1100,19 +1113,19 @@ public class Byte2CharLinkedOpenHashMap extends Byte2CharOpenHashMap implements state = operator.applyAsByte(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (byte)0; + if(size() <= 0) return OptionalByte.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalByte.of(keys[index]); index = (int)links[index]; } - return (byte)0; + return OptionalByte.empty(); } @Override @@ -1248,7 +1261,7 @@ public class Byte2CharLinkedOpenHashMap extends Byte2CharOpenHashMap implements } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -1263,19 +1276,19 @@ public class Byte2CharLinkedOpenHashMap extends Byte2CharOpenHashMap implements state = operator.applyAsChar(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (char)0; + if(size() <= 0) return OptionalChar.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalChar.of(values[index]); index = (int)links[index]; } - return (char)0; + return OptionalChar.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/bytes/maps/impl/hash/Byte2CharOpenHashMap.java b/src/main/java/speiger/src/collections/bytes/maps/impl/hash/Byte2CharOpenHashMap.java index bf4e02a..57e0c2a 100644 --- a/src/main/java/speiger/src/collections/bytes/maps/impl/hash/Byte2CharOpenHashMap.java +++ b/src/main/java/speiger/src/collections/bytes/maps/impl/hash/Byte2CharOpenHashMap.java @@ -5,6 +5,7 @@ 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; @@ -19,6 +20,7 @@ import speiger.src.collections.bytes.functions.consumer.ByteCharConsumer; import speiger.src.collections.bytes.functions.function.Byte2CharFunction; import speiger.src.collections.bytes.functions.function.ByteCharUnaryOperator; import speiger.src.collections.bytes.functions.function.ByteByteUnaryOperator; +import speiger.src.collections.bytes.functions.OptionalByte; import speiger.src.collections.bytes.functions.function.BytePredicate; import speiger.src.collections.bytes.maps.abstracts.AbstractByte2CharMap; import speiger.src.collections.bytes.maps.interfaces.Byte2CharMap; @@ -34,6 +36,7 @@ import speiger.src.collections.chars.functions.CharConsumer; import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectCharConsumer; import speiger.src.collections.chars.functions.function.CharPredicate; +import speiger.src.collections.chars.functions.OptionalChar; import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.sets.AbstractObjectSet; import speiger.src.collections.objects.sets.ObjectSet; @@ -920,7 +923,7 @@ public class Byte2CharOpenHashMap extends AbstractByte2CharMap implements ITrimm } @Override - public Byte2CharMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Byte2CharMap.Entry state = null; boolean empty = true; @@ -937,25 +940,25 @@ public class Byte2CharOpenHashMap extends AbstractByte2CharMap implements ITrimm } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Byte2CharMap.Entry 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(keys[i] != (byte)0) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1127,7 +1130,7 @@ public class Byte2CharOpenHashMap extends AbstractByte2CharMap implements ITrimm } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -1144,18 +1147,18 @@ public class Byte2CharOpenHashMap extends AbstractByte2CharMap implements ITrimm } state = operator.applyAsByte(state, keys[i]); } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (byte)0; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OptionalByte.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OptionalByte.of(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(keys[i] != (byte)0 && filter.test(keys[i])) return keys[i]; + if(keys[i] != (byte)0 && filter.test(keys[i])) return OptionalByte.of(keys[i]); } - return (byte)0; + return OptionalByte.empty(); } @Override @@ -1270,7 +1273,7 @@ public class Byte2CharOpenHashMap extends AbstractByte2CharMap implements ITrimm } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -1287,18 +1290,18 @@ public class Byte2CharOpenHashMap extends AbstractByte2CharMap implements ITrimm } state = operator.applyAsChar(state, values[i]); } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (char)0; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return OptionalChar.empty(); + if(containsNull && filter.test(values[nullIndex])) return OptionalChar.of(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(keys[i] != (byte)0 && filter.test(values[i])) return values[i]; + if(keys[i] != (byte)0 && filter.test(values[i])) return OptionalChar.of(values[i]); } - return (char)0; + return OptionalChar.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/bytes/maps/impl/hash/Byte2DoubleLinkedOpenHashMap.java b/src/main/java/speiger/src/collections/bytes/maps/impl/hash/Byte2DoubleLinkedOpenHashMap.java index bb568d1..3dcf8d7 100644 --- a/src/main/java/speiger/src/collections/bytes/maps/impl/hash/Byte2DoubleLinkedOpenHashMap.java +++ b/src/main/java/speiger/src/collections/bytes/maps/impl/hash/Byte2DoubleLinkedOpenHashMap.java @@ -4,10 +4,12 @@ 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; import java.util.function.DoublePredicate; +import java.util.OptionalDouble; import speiger.src.collections.bytes.collections.ByteBidirectionalIterator; import speiger.src.collections.bytes.functions.ByteConsumer; @@ -15,6 +17,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectByteConsumer; import speiger.src.collections.ints.functions.consumer.IntByteConsumer; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntDoubleConsumer; +import speiger.src.collections.bytes.functions.OptionalByte; import speiger.src.collections.bytes.functions.function.BytePredicate; import speiger.src.collections.bytes.functions.consumer.ByteDoubleConsumer; import speiger.src.collections.bytes.functions.function.ByteByteUnaryOperator; @@ -669,6 +672,10 @@ public class Byte2DoubleLinkedOpenHashMap extends Byte2DoubleOpenHashMap impleme } private class MapEntrySet extends AbstractObjectSet implements Byte2DoubleOrderedMap.FastOrderedSet { + @Override + public void addFirst(Byte2DoubleMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Byte2DoubleMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Byte2DoubleMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -834,7 +841,7 @@ public class Byte2DoubleLinkedOpenHashMap extends Byte2DoubleOpenHashMap impleme } @Override - public Byte2DoubleMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Byte2DoubleMap.Entry state = null; boolean empty = true; @@ -849,21 +856,21 @@ public class Byte2DoubleLinkedOpenHashMap extends Byte2DoubleOpenHashMap impleme state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Byte2DoubleMap.Entry 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 @@ -939,6 +946,12 @@ public class Byte2DoubleLinkedOpenHashMap extends Byte2DoubleOpenHashMap impleme @Override public boolean add(byte o) { throw new UnsupportedOperationException(); } + + @Override + public void addFirst(byte o) { throw new UnsupportedOperationException(); } + + @Override + public void addLast(byte o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(byte o) { throw new UnsupportedOperationException(); } @@ -1085,7 +1098,7 @@ public class Byte2DoubleLinkedOpenHashMap extends Byte2DoubleOpenHashMap impleme } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -1100,19 +1113,19 @@ public class Byte2DoubleLinkedOpenHashMap extends Byte2DoubleOpenHashMap impleme state = operator.applyAsByte(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (byte)0; + if(size() <= 0) return OptionalByte.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalByte.of(keys[index]); index = (int)links[index]; } - return (byte)0; + return OptionalByte.empty(); } @Override @@ -1248,7 +1261,7 @@ public class Byte2DoubleLinkedOpenHashMap extends Byte2DoubleOpenHashMap impleme } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -1263,19 +1276,19 @@ public class Byte2DoubleLinkedOpenHashMap extends Byte2DoubleOpenHashMap impleme state = operator.applyAsDouble(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0D; + if(size() <= 0) return OptionalDouble.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalDouble.of(values[index]); index = (int)links[index]; } - return 0D; + return OptionalDouble.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/bytes/maps/impl/hash/Byte2DoubleOpenHashMap.java b/src/main/java/speiger/src/collections/bytes/maps/impl/hash/Byte2DoubleOpenHashMap.java index 1d8a415..513dd52 100644 --- a/src/main/java/speiger/src/collections/bytes/maps/impl/hash/Byte2DoubleOpenHashMap.java +++ b/src/main/java/speiger/src/collections/bytes/maps/impl/hash/Byte2DoubleOpenHashMap.java @@ -5,10 +5,12 @@ 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; import java.util.function.DoublePredicate; +import java.util.OptionalDouble; import speiger.src.collections.bytes.collections.ByteIterator; import speiger.src.collections.bytes.functions.ByteConsumer; @@ -20,6 +22,7 @@ import speiger.src.collections.bytes.functions.consumer.ByteDoubleConsumer; import speiger.src.collections.bytes.functions.function.Byte2DoubleFunction; import speiger.src.collections.bytes.functions.function.ByteDoubleUnaryOperator; import speiger.src.collections.bytes.functions.function.ByteByteUnaryOperator; +import speiger.src.collections.bytes.functions.OptionalByte; import speiger.src.collections.bytes.functions.function.BytePredicate; import speiger.src.collections.bytes.maps.abstracts.AbstractByte2DoubleMap; import speiger.src.collections.bytes.maps.interfaces.Byte2DoubleMap; @@ -920,7 +923,7 @@ public class Byte2DoubleOpenHashMap extends AbstractByte2DoubleMap implements IT } @Override - public Byte2DoubleMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Byte2DoubleMap.Entry state = null; boolean empty = true; @@ -937,25 +940,25 @@ public class Byte2DoubleOpenHashMap extends AbstractByte2DoubleMap implements IT } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Byte2DoubleMap.Entry 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(keys[i] != (byte)0) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1127,7 +1130,7 @@ public class Byte2DoubleOpenHashMap extends AbstractByte2DoubleMap implements IT } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -1144,18 +1147,18 @@ public class Byte2DoubleOpenHashMap extends AbstractByte2DoubleMap implements IT } state = operator.applyAsByte(state, keys[i]); } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (byte)0; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OptionalByte.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OptionalByte.of(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(keys[i] != (byte)0 && filter.test(keys[i])) return keys[i]; + if(keys[i] != (byte)0 && filter.test(keys[i])) return OptionalByte.of(keys[i]); } - return (byte)0; + return OptionalByte.empty(); } @Override @@ -1270,7 +1273,7 @@ public class Byte2DoubleOpenHashMap extends AbstractByte2DoubleMap implements IT } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -1287,18 +1290,18 @@ public class Byte2DoubleOpenHashMap extends AbstractByte2DoubleMap implements IT } state = operator.applyAsDouble(state, values[i]); } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0D; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return OptionalDouble.empty(); + if(containsNull && filter.test(values[nullIndex])) return OptionalDouble.of(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(keys[i] != (byte)0 && filter.test(values[i])) return values[i]; + if(keys[i] != (byte)0 && filter.test(values[i])) return OptionalDouble.of(values[i]); } - return 0D; + return OptionalDouble.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/bytes/maps/impl/hash/Byte2FloatLinkedOpenHashMap.java b/src/main/java/speiger/src/collections/bytes/maps/impl/hash/Byte2FloatLinkedOpenHashMap.java index 5b05dd3..e4ec298 100644 --- a/src/main/java/speiger/src/collections/bytes/maps/impl/hash/Byte2FloatLinkedOpenHashMap.java +++ b/src/main/java/speiger/src/collections/bytes/maps/impl/hash/Byte2FloatLinkedOpenHashMap.java @@ -4,6 +4,7 @@ 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; @@ -14,6 +15,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectByteConsumer; import speiger.src.collections.ints.functions.consumer.IntByteConsumer; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntFloatConsumer; +import speiger.src.collections.bytes.functions.OptionalByte; import speiger.src.collections.bytes.functions.function.BytePredicate; import speiger.src.collections.bytes.functions.consumer.ByteFloatConsumer; import speiger.src.collections.bytes.functions.function.ByteByteUnaryOperator; @@ -32,6 +34,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectFloatConsumer; import speiger.src.collections.floats.functions.function.FloatPredicate; +import speiger.src.collections.floats.functions.OptionalFloat; import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOperator; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.objects.lists.ObjectListIterator; @@ -669,6 +672,10 @@ public class Byte2FloatLinkedOpenHashMap extends Byte2FloatOpenHashMap implement } private class MapEntrySet extends AbstractObjectSet implements Byte2FloatOrderedMap.FastOrderedSet { + @Override + public void addFirst(Byte2FloatMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Byte2FloatMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Byte2FloatMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -834,7 +841,7 @@ public class Byte2FloatLinkedOpenHashMap extends Byte2FloatOpenHashMap implement } @Override - public Byte2FloatMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Byte2FloatMap.Entry state = null; boolean empty = true; @@ -849,21 +856,21 @@ public class Byte2FloatLinkedOpenHashMap extends Byte2FloatOpenHashMap implement state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Byte2FloatMap.Entry 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 @@ -939,6 +946,12 @@ public class Byte2FloatLinkedOpenHashMap extends Byte2FloatOpenHashMap implement @Override public boolean add(byte o) { throw new UnsupportedOperationException(); } + + @Override + public void addFirst(byte o) { throw new UnsupportedOperationException(); } + + @Override + public void addLast(byte o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(byte o) { throw new UnsupportedOperationException(); } @@ -1085,7 +1098,7 @@ public class Byte2FloatLinkedOpenHashMap extends Byte2FloatOpenHashMap implement } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -1100,19 +1113,19 @@ public class Byte2FloatLinkedOpenHashMap extends Byte2FloatOpenHashMap implement state = operator.applyAsByte(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (byte)0; + if(size() <= 0) return OptionalByte.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalByte.of(keys[index]); index = (int)links[index]; } - return (byte)0; + return OptionalByte.empty(); } @Override @@ -1248,7 +1261,7 @@ public class Byte2FloatLinkedOpenHashMap extends Byte2FloatOpenHashMap implement } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -1263,19 +1276,19 @@ public class Byte2FloatLinkedOpenHashMap extends Byte2FloatOpenHashMap implement state = operator.applyAsFloat(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0F; + if(size() <= 0) return OptionalFloat.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalFloat.of(values[index]); index = (int)links[index]; } - return 0F; + return OptionalFloat.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/bytes/maps/impl/hash/Byte2FloatOpenHashMap.java b/src/main/java/speiger/src/collections/bytes/maps/impl/hash/Byte2FloatOpenHashMap.java index 4aae440..fd29c37 100644 --- a/src/main/java/speiger/src/collections/bytes/maps/impl/hash/Byte2FloatOpenHashMap.java +++ b/src/main/java/speiger/src/collections/bytes/maps/impl/hash/Byte2FloatOpenHashMap.java @@ -5,6 +5,7 @@ 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; @@ -19,6 +20,7 @@ import speiger.src.collections.bytes.functions.consumer.ByteFloatConsumer; import speiger.src.collections.bytes.functions.function.Byte2FloatFunction; import speiger.src.collections.bytes.functions.function.ByteFloatUnaryOperator; import speiger.src.collections.bytes.functions.function.ByteByteUnaryOperator; +import speiger.src.collections.bytes.functions.OptionalByte; import speiger.src.collections.bytes.functions.function.BytePredicate; import speiger.src.collections.bytes.maps.abstracts.AbstractByte2FloatMap; import speiger.src.collections.bytes.maps.interfaces.Byte2FloatMap; @@ -34,6 +36,7 @@ import speiger.src.collections.floats.functions.FloatConsumer; import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectFloatConsumer; import speiger.src.collections.floats.functions.function.FloatPredicate; +import speiger.src.collections.floats.functions.OptionalFloat; import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.sets.AbstractObjectSet; import speiger.src.collections.objects.sets.ObjectSet; @@ -920,7 +923,7 @@ public class Byte2FloatOpenHashMap extends AbstractByte2FloatMap implements ITri } @Override - public Byte2FloatMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Byte2FloatMap.Entry state = null; boolean empty = true; @@ -937,25 +940,25 @@ public class Byte2FloatOpenHashMap extends AbstractByte2FloatMap implements ITri } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Byte2FloatMap.Entry 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(keys[i] != (byte)0) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1127,7 +1130,7 @@ public class Byte2FloatOpenHashMap extends AbstractByte2FloatMap implements ITri } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -1144,18 +1147,18 @@ public class Byte2FloatOpenHashMap extends AbstractByte2FloatMap implements ITri } state = operator.applyAsByte(state, keys[i]); } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (byte)0; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OptionalByte.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OptionalByte.of(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(keys[i] != (byte)0 && filter.test(keys[i])) return keys[i]; + if(keys[i] != (byte)0 && filter.test(keys[i])) return OptionalByte.of(keys[i]); } - return (byte)0; + return OptionalByte.empty(); } @Override @@ -1270,7 +1273,7 @@ public class Byte2FloatOpenHashMap extends AbstractByte2FloatMap implements ITri } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -1287,18 +1290,18 @@ public class Byte2FloatOpenHashMap extends AbstractByte2FloatMap implements ITri } state = operator.applyAsFloat(state, values[i]); } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0F; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return OptionalFloat.empty(); + if(containsNull && filter.test(values[nullIndex])) return OptionalFloat.of(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(keys[i] != (byte)0 && filter.test(values[i])) return values[i]; + if(keys[i] != (byte)0 && filter.test(values[i])) return OptionalFloat.of(values[i]); } - return 0F; + return OptionalFloat.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/bytes/maps/impl/hash/Byte2IntLinkedOpenHashMap.java b/src/main/java/speiger/src/collections/bytes/maps/impl/hash/Byte2IntLinkedOpenHashMap.java index ff11287..e6bbd7e 100644 --- a/src/main/java/speiger/src/collections/bytes/maps/impl/hash/Byte2IntLinkedOpenHashMap.java +++ b/src/main/java/speiger/src/collections/bytes/maps/impl/hash/Byte2IntLinkedOpenHashMap.java @@ -4,10 +4,12 @@ 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; import java.util.function.IntPredicate; +import java.util.OptionalInt; import speiger.src.collections.bytes.collections.ByteBidirectionalIterator; import speiger.src.collections.bytes.functions.ByteConsumer; @@ -15,6 +17,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectByteConsumer; import speiger.src.collections.ints.functions.consumer.IntByteConsumer; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntIntConsumer; +import speiger.src.collections.bytes.functions.OptionalByte; import speiger.src.collections.bytes.functions.function.BytePredicate; import speiger.src.collections.bytes.functions.consumer.ByteIntConsumer; import speiger.src.collections.bytes.functions.function.ByteByteUnaryOperator; @@ -669,6 +672,10 @@ public class Byte2IntLinkedOpenHashMap extends Byte2IntOpenHashMap implements By } private class MapEntrySet extends AbstractObjectSet implements Byte2IntOrderedMap.FastOrderedSet { + @Override + public void addFirst(Byte2IntMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Byte2IntMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Byte2IntMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -834,7 +841,7 @@ public class Byte2IntLinkedOpenHashMap extends Byte2IntOpenHashMap implements By } @Override - public Byte2IntMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Byte2IntMap.Entry state = null; boolean empty = true; @@ -849,21 +856,21 @@ public class Byte2IntLinkedOpenHashMap extends Byte2IntOpenHashMap implements By state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Byte2IntMap.Entry 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 @@ -939,6 +946,12 @@ public class Byte2IntLinkedOpenHashMap extends Byte2IntOpenHashMap implements By @Override public boolean add(byte o) { throw new UnsupportedOperationException(); } + + @Override + public void addFirst(byte o) { throw new UnsupportedOperationException(); } + + @Override + public void addLast(byte o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(byte o) { throw new UnsupportedOperationException(); } @@ -1085,7 +1098,7 @@ public class Byte2IntLinkedOpenHashMap extends Byte2IntOpenHashMap implements By } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -1100,19 +1113,19 @@ public class Byte2IntLinkedOpenHashMap extends Byte2IntOpenHashMap implements By state = operator.applyAsByte(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (byte)0; + if(size() <= 0) return OptionalByte.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalByte.of(keys[index]); index = (int)links[index]; } - return (byte)0; + return OptionalByte.empty(); } @Override @@ -1248,7 +1261,7 @@ public class Byte2IntLinkedOpenHashMap extends Byte2IntOpenHashMap implements By } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -1263,19 +1276,19 @@ public class Byte2IntLinkedOpenHashMap extends Byte2IntOpenHashMap implements By state = operator.applyAsInt(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0; + if(size() <= 0) return OptionalInt.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalInt.of(values[index]); index = (int)links[index]; } - return 0; + return OptionalInt.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/bytes/maps/impl/hash/Byte2IntOpenHashMap.java b/src/main/java/speiger/src/collections/bytes/maps/impl/hash/Byte2IntOpenHashMap.java index 42b872a..810d640 100644 --- a/src/main/java/speiger/src/collections/bytes/maps/impl/hash/Byte2IntOpenHashMap.java +++ b/src/main/java/speiger/src/collections/bytes/maps/impl/hash/Byte2IntOpenHashMap.java @@ -5,10 +5,12 @@ 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; import java.util.function.IntPredicate; +import java.util.OptionalInt; import speiger.src.collections.bytes.collections.ByteIterator; import speiger.src.collections.bytes.functions.ByteConsumer; @@ -20,6 +22,7 @@ import speiger.src.collections.bytes.functions.consumer.ByteIntConsumer; import speiger.src.collections.bytes.functions.function.Byte2IntFunction; import speiger.src.collections.bytes.functions.function.ByteIntUnaryOperator; import speiger.src.collections.bytes.functions.function.ByteByteUnaryOperator; +import speiger.src.collections.bytes.functions.OptionalByte; import speiger.src.collections.bytes.functions.function.BytePredicate; import speiger.src.collections.bytes.maps.abstracts.AbstractByte2IntMap; import speiger.src.collections.bytes.maps.interfaces.Byte2IntMap; @@ -920,7 +923,7 @@ public class Byte2IntOpenHashMap extends AbstractByte2IntMap implements ITrimmab } @Override - public Byte2IntMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Byte2IntMap.Entry state = null; boolean empty = true; @@ -937,25 +940,25 @@ public class Byte2IntOpenHashMap extends AbstractByte2IntMap implements ITrimmab } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Byte2IntMap.Entry 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(keys[i] != (byte)0) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1127,7 +1130,7 @@ public class Byte2IntOpenHashMap extends AbstractByte2IntMap implements ITrimmab } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -1144,18 +1147,18 @@ public class Byte2IntOpenHashMap extends AbstractByte2IntMap implements ITrimmab } state = operator.applyAsByte(state, keys[i]); } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (byte)0; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OptionalByte.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OptionalByte.of(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(keys[i] != (byte)0 && filter.test(keys[i])) return keys[i]; + if(keys[i] != (byte)0 && filter.test(keys[i])) return OptionalByte.of(keys[i]); } - return (byte)0; + return OptionalByte.empty(); } @Override @@ -1270,7 +1273,7 @@ public class Byte2IntOpenHashMap extends AbstractByte2IntMap implements ITrimmab } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -1287,18 +1290,18 @@ public class Byte2IntOpenHashMap extends AbstractByte2IntMap implements ITrimmab } state = operator.applyAsInt(state, values[i]); } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return OptionalInt.empty(); + if(containsNull && filter.test(values[nullIndex])) return OptionalInt.of(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(keys[i] != (byte)0 && filter.test(values[i])) return values[i]; + if(keys[i] != (byte)0 && filter.test(values[i])) return OptionalInt.of(values[i]); } - return 0; + return OptionalInt.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/bytes/maps/impl/hash/Byte2LongLinkedOpenHashMap.java b/src/main/java/speiger/src/collections/bytes/maps/impl/hash/Byte2LongLinkedOpenHashMap.java index b476e6c..6ca052e 100644 --- a/src/main/java/speiger/src/collections/bytes/maps/impl/hash/Byte2LongLinkedOpenHashMap.java +++ b/src/main/java/speiger/src/collections/bytes/maps/impl/hash/Byte2LongLinkedOpenHashMap.java @@ -4,10 +4,12 @@ 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; import java.util.function.LongPredicate; +import java.util.OptionalLong; import speiger.src.collections.bytes.collections.ByteBidirectionalIterator; import speiger.src.collections.bytes.functions.ByteConsumer; @@ -15,6 +17,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectByteConsumer; import speiger.src.collections.ints.functions.consumer.IntByteConsumer; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntLongConsumer; +import speiger.src.collections.bytes.functions.OptionalByte; import speiger.src.collections.bytes.functions.function.BytePredicate; import speiger.src.collections.bytes.functions.consumer.ByteLongConsumer; import speiger.src.collections.bytes.functions.function.ByteByteUnaryOperator; @@ -669,6 +672,10 @@ public class Byte2LongLinkedOpenHashMap extends Byte2LongOpenHashMap implements } private class MapEntrySet extends AbstractObjectSet implements Byte2LongOrderedMap.FastOrderedSet { + @Override + public void addFirst(Byte2LongMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Byte2LongMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Byte2LongMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -834,7 +841,7 @@ public class Byte2LongLinkedOpenHashMap extends Byte2LongOpenHashMap implements } @Override - public Byte2LongMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Byte2LongMap.Entry state = null; boolean empty = true; @@ -849,21 +856,21 @@ public class Byte2LongLinkedOpenHashMap extends Byte2LongOpenHashMap implements state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Byte2LongMap.Entry 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 @@ -939,6 +946,12 @@ public class Byte2LongLinkedOpenHashMap extends Byte2LongOpenHashMap implements @Override public boolean add(byte o) { throw new UnsupportedOperationException(); } + + @Override + public void addFirst(byte o) { throw new UnsupportedOperationException(); } + + @Override + public void addLast(byte o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(byte o) { throw new UnsupportedOperationException(); } @@ -1085,7 +1098,7 @@ public class Byte2LongLinkedOpenHashMap extends Byte2LongOpenHashMap implements } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -1100,19 +1113,19 @@ public class Byte2LongLinkedOpenHashMap extends Byte2LongOpenHashMap implements state = operator.applyAsByte(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (byte)0; + if(size() <= 0) return OptionalByte.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalByte.of(keys[index]); index = (int)links[index]; } - return (byte)0; + return OptionalByte.empty(); } @Override @@ -1248,7 +1261,7 @@ public class Byte2LongLinkedOpenHashMap extends Byte2LongOpenHashMap implements } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -1263,19 +1276,19 @@ public class Byte2LongLinkedOpenHashMap extends Byte2LongOpenHashMap implements state = operator.applyAsLong(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0L; + if(size() <= 0) return OptionalLong.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalLong.of(values[index]); index = (int)links[index]; } - return 0L; + return OptionalLong.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/bytes/maps/impl/hash/Byte2LongOpenHashMap.java b/src/main/java/speiger/src/collections/bytes/maps/impl/hash/Byte2LongOpenHashMap.java index a3c5142..0084c50 100644 --- a/src/main/java/speiger/src/collections/bytes/maps/impl/hash/Byte2LongOpenHashMap.java +++ b/src/main/java/speiger/src/collections/bytes/maps/impl/hash/Byte2LongOpenHashMap.java @@ -5,10 +5,12 @@ 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; import java.util.function.LongPredicate; +import java.util.OptionalLong; import speiger.src.collections.bytes.collections.ByteIterator; import speiger.src.collections.bytes.functions.ByteConsumer; @@ -20,6 +22,7 @@ import speiger.src.collections.bytes.functions.consumer.ByteLongConsumer; import speiger.src.collections.bytes.functions.function.Byte2LongFunction; import speiger.src.collections.bytes.functions.function.ByteLongUnaryOperator; import speiger.src.collections.bytes.functions.function.ByteByteUnaryOperator; +import speiger.src.collections.bytes.functions.OptionalByte; import speiger.src.collections.bytes.functions.function.BytePredicate; import speiger.src.collections.bytes.maps.abstracts.AbstractByte2LongMap; import speiger.src.collections.bytes.maps.interfaces.Byte2LongMap; @@ -920,7 +923,7 @@ public class Byte2LongOpenHashMap extends AbstractByte2LongMap implements ITrimm } @Override - public Byte2LongMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Byte2LongMap.Entry state = null; boolean empty = true; @@ -937,25 +940,25 @@ public class Byte2LongOpenHashMap extends AbstractByte2LongMap implements ITrimm } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Byte2LongMap.Entry 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(keys[i] != (byte)0) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1127,7 +1130,7 @@ public class Byte2LongOpenHashMap extends AbstractByte2LongMap implements ITrimm } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -1144,18 +1147,18 @@ public class Byte2LongOpenHashMap extends AbstractByte2LongMap implements ITrimm } state = operator.applyAsByte(state, keys[i]); } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (byte)0; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OptionalByte.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OptionalByte.of(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(keys[i] != (byte)0 && filter.test(keys[i])) return keys[i]; + if(keys[i] != (byte)0 && filter.test(keys[i])) return OptionalByte.of(keys[i]); } - return (byte)0; + return OptionalByte.empty(); } @Override @@ -1270,7 +1273,7 @@ public class Byte2LongOpenHashMap extends AbstractByte2LongMap implements ITrimm } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -1287,18 +1290,18 @@ public class Byte2LongOpenHashMap extends AbstractByte2LongMap implements ITrimm } state = operator.applyAsLong(state, values[i]); } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0L; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return OptionalLong.empty(); + if(containsNull && filter.test(values[nullIndex])) return OptionalLong.of(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(keys[i] != (byte)0 && filter.test(values[i])) return values[i]; + if(keys[i] != (byte)0 && filter.test(values[i])) return OptionalLong.of(values[i]); } - return 0L; + return OptionalLong.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/bytes/maps/impl/hash/Byte2ObjectLinkedOpenHashMap.java b/src/main/java/speiger/src/collections/bytes/maps/impl/hash/Byte2ObjectLinkedOpenHashMap.java index d304acb..93bbf36 100644 --- a/src/main/java/speiger/src/collections/bytes/maps/impl/hash/Byte2ObjectLinkedOpenHashMap.java +++ b/src/main/java/speiger/src/collections/bytes/maps/impl/hash/Byte2ObjectLinkedOpenHashMap.java @@ -4,6 +4,7 @@ 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; @@ -13,6 +14,7 @@ import speiger.src.collections.bytes.functions.ByteConsumer; import speiger.src.collections.objects.functions.consumer.ObjectByteConsumer; import speiger.src.collections.ints.functions.consumer.IntByteConsumer; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; +import speiger.src.collections.bytes.functions.OptionalByte; import speiger.src.collections.bytes.functions.function.BytePredicate; import speiger.src.collections.bytes.functions.consumer.ByteObjectConsumer; import speiger.src.collections.bytes.functions.function.ByteByteUnaryOperator; @@ -653,6 +655,10 @@ public class Byte2ObjectLinkedOpenHashMap extends Byte2ObjectOpenHashMap i } private class MapEntrySet extends AbstractObjectSet> implements Byte2ObjectOrderedMap.FastOrderedSet { + @Override + public void addFirst(Byte2ObjectMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Byte2ObjectMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Byte2ObjectMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -818,7 +824,7 @@ public class Byte2ObjectLinkedOpenHashMap extends Byte2ObjectOpenHashMap i } @Override - public Byte2ObjectMap.Entry reduce(ObjectObjectUnaryOperator, Byte2ObjectMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Byte2ObjectMap.Entry> operator) { Objects.requireNonNull(operator); Byte2ObjectMap.Entry state = null; boolean empty = true; @@ -833,21 +839,21 @@ public class Byte2ObjectLinkedOpenHashMap extends Byte2ObjectOpenHashMap i state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Byte2ObjectMap.Entry 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 @@ -923,6 +929,12 @@ public class Byte2ObjectLinkedOpenHashMap extends Byte2ObjectOpenHashMap i @Override public boolean add(byte o) { throw new UnsupportedOperationException(); } + + @Override + public void addFirst(byte o) { throw new UnsupportedOperationException(); } + + @Override + public void addLast(byte o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(byte o) { throw new UnsupportedOperationException(); } @@ -1069,7 +1081,7 @@ public class Byte2ObjectLinkedOpenHashMap extends Byte2ObjectOpenHashMap i } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -1084,19 +1096,19 @@ public class Byte2ObjectLinkedOpenHashMap extends Byte2ObjectOpenHashMap i state = operator.applyAsByte(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (byte)0; + if(size() <= 0) return OptionalByte.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalByte.of(keys[index]); index = (int)links[index]; } - return (byte)0; + return OptionalByte.empty(); } @Override @@ -1233,7 +1245,7 @@ public class Byte2ObjectLinkedOpenHashMap extends Byte2ObjectOpenHashMap i } @Override - public V reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); V state = null; boolean empty = true; @@ -1248,19 +1260,19 @@ public class Byte2ObjectLinkedOpenHashMap extends Byte2ObjectOpenHashMap i state = operator.apply(state, values[index]); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public V findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return Optional.ofNullable(values[index]); index = (int)links[index]; } - return null; + return Optional.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/bytes/maps/impl/hash/Byte2ObjectOpenHashMap.java b/src/main/java/speiger/src/collections/bytes/maps/impl/hash/Byte2ObjectOpenHashMap.java index 2a26e1a..53af062 100644 --- a/src/main/java/speiger/src/collections/bytes/maps/impl/hash/Byte2ObjectOpenHashMap.java +++ b/src/main/java/speiger/src/collections/bytes/maps/impl/hash/Byte2ObjectOpenHashMap.java @@ -5,6 +5,7 @@ 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; @@ -18,6 +19,7 @@ import speiger.src.collections.bytes.functions.consumer.ByteObjectConsumer; import speiger.src.collections.bytes.functions.function.ByteFunction; import speiger.src.collections.bytes.functions.function.ByteObjectUnaryOperator; import speiger.src.collections.bytes.functions.function.ByteByteUnaryOperator; +import speiger.src.collections.bytes.functions.OptionalByte; import speiger.src.collections.bytes.functions.function.BytePredicate; import speiger.src.collections.bytes.maps.abstracts.AbstractByte2ObjectMap; import speiger.src.collections.bytes.maps.interfaces.Byte2ObjectMap; @@ -837,7 +839,7 @@ public class Byte2ObjectOpenHashMap extends AbstractByte2ObjectMap impleme } @Override - public Byte2ObjectMap.Entry reduce(ObjectObjectUnaryOperator, Byte2ObjectMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Byte2ObjectMap.Entry> operator) { Objects.requireNonNull(operator); Byte2ObjectMap.Entry state = null; boolean empty = true; @@ -854,25 +856,25 @@ public class Byte2ObjectOpenHashMap extends AbstractByte2ObjectMap impleme } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Byte2ObjectMap.Entry 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(keys[i] != (byte)0) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1044,7 +1046,7 @@ public class Byte2ObjectOpenHashMap extends AbstractByte2ObjectMap impleme } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -1061,18 +1063,18 @@ public class Byte2ObjectOpenHashMap extends AbstractByte2ObjectMap impleme } state = operator.applyAsByte(state, keys[i]); } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (byte)0; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OptionalByte.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OptionalByte.of(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(keys[i] != (byte)0 && filter.test(keys[i])) return keys[i]; + if(keys[i] != (byte)0 && filter.test(keys[i])) return OptionalByte.of(keys[i]); } - return (byte)0; + return OptionalByte.empty(); } @Override @@ -1187,7 +1189,7 @@ public class Byte2ObjectOpenHashMap extends AbstractByte2ObjectMap impleme } @Override - public V reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); V state = null; boolean empty = true; @@ -1204,18 +1206,18 @@ public class Byte2ObjectOpenHashMap extends AbstractByte2ObjectMap impleme } state = operator.apply(state, values[i]); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public V findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return Optional.empty(); + if(containsNull && filter.test(values[nullIndex])) return Optional.ofNullable(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(keys[i] != (byte)0 && filter.test(values[i])) return values[i]; + if(keys[i] != (byte)0 && filter.test(values[i])) return Optional.ofNullable(values[i]); } - return null; + return Optional.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/bytes/maps/impl/hash/Byte2ShortLinkedOpenHashMap.java b/src/main/java/speiger/src/collections/bytes/maps/impl/hash/Byte2ShortLinkedOpenHashMap.java index 9fcfd5c..d1c3fa0 100644 --- a/src/main/java/speiger/src/collections/bytes/maps/impl/hash/Byte2ShortLinkedOpenHashMap.java +++ b/src/main/java/speiger/src/collections/bytes/maps/impl/hash/Byte2ShortLinkedOpenHashMap.java @@ -4,6 +4,7 @@ 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; @@ -14,6 +15,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectByteConsumer; import speiger.src.collections.ints.functions.consumer.IntByteConsumer; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntShortConsumer; +import speiger.src.collections.bytes.functions.OptionalByte; import speiger.src.collections.bytes.functions.function.BytePredicate; import speiger.src.collections.bytes.functions.consumer.ByteShortConsumer; import speiger.src.collections.bytes.functions.function.ByteByteUnaryOperator; @@ -32,6 +34,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectShortConsumer; import speiger.src.collections.shorts.functions.function.ShortPredicate; +import speiger.src.collections.shorts.functions.OptionalShort; import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOperator; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.objects.lists.ObjectListIterator; @@ -669,6 +672,10 @@ public class Byte2ShortLinkedOpenHashMap extends Byte2ShortOpenHashMap implement } private class MapEntrySet extends AbstractObjectSet implements Byte2ShortOrderedMap.FastOrderedSet { + @Override + public void addFirst(Byte2ShortMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Byte2ShortMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Byte2ShortMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -834,7 +841,7 @@ public class Byte2ShortLinkedOpenHashMap extends Byte2ShortOpenHashMap implement } @Override - public Byte2ShortMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Byte2ShortMap.Entry state = null; boolean empty = true; @@ -849,21 +856,21 @@ public class Byte2ShortLinkedOpenHashMap extends Byte2ShortOpenHashMap implement state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Byte2ShortMap.Entry 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 @@ -939,6 +946,12 @@ public class Byte2ShortLinkedOpenHashMap extends Byte2ShortOpenHashMap implement @Override public boolean add(byte o) { throw new UnsupportedOperationException(); } + + @Override + public void addFirst(byte o) { throw new UnsupportedOperationException(); } + + @Override + public void addLast(byte o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(byte o) { throw new UnsupportedOperationException(); } @@ -1085,7 +1098,7 @@ public class Byte2ShortLinkedOpenHashMap extends Byte2ShortOpenHashMap implement } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -1100,19 +1113,19 @@ public class Byte2ShortLinkedOpenHashMap extends Byte2ShortOpenHashMap implement state = operator.applyAsByte(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (byte)0; + if(size() <= 0) return OptionalByte.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalByte.of(keys[index]); index = (int)links[index]; } - return (byte)0; + return OptionalByte.empty(); } @Override @@ -1248,7 +1261,7 @@ public class Byte2ShortLinkedOpenHashMap extends Byte2ShortOpenHashMap implement } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -1263,19 +1276,19 @@ public class Byte2ShortLinkedOpenHashMap extends Byte2ShortOpenHashMap implement state = operator.applyAsShort(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (short)0; + if(size() <= 0) return OptionalShort.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalShort.of(values[index]); index = (int)links[index]; } - return (short)0; + return OptionalShort.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/bytes/maps/impl/hash/Byte2ShortOpenHashMap.java b/src/main/java/speiger/src/collections/bytes/maps/impl/hash/Byte2ShortOpenHashMap.java index 261a7d0..7533471 100644 --- a/src/main/java/speiger/src/collections/bytes/maps/impl/hash/Byte2ShortOpenHashMap.java +++ b/src/main/java/speiger/src/collections/bytes/maps/impl/hash/Byte2ShortOpenHashMap.java @@ -5,6 +5,7 @@ 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; @@ -19,6 +20,7 @@ import speiger.src.collections.bytes.functions.consumer.ByteShortConsumer; import speiger.src.collections.bytes.functions.function.Byte2ShortFunction; import speiger.src.collections.bytes.functions.function.ByteShortUnaryOperator; import speiger.src.collections.bytes.functions.function.ByteByteUnaryOperator; +import speiger.src.collections.bytes.functions.OptionalByte; import speiger.src.collections.bytes.functions.function.BytePredicate; import speiger.src.collections.bytes.maps.abstracts.AbstractByte2ShortMap; import speiger.src.collections.bytes.maps.interfaces.Byte2ShortMap; @@ -34,6 +36,7 @@ import speiger.src.collections.shorts.functions.ShortConsumer; import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectShortConsumer; import speiger.src.collections.shorts.functions.function.ShortPredicate; +import speiger.src.collections.shorts.functions.OptionalShort; import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.sets.AbstractObjectSet; import speiger.src.collections.objects.sets.ObjectSet; @@ -920,7 +923,7 @@ public class Byte2ShortOpenHashMap extends AbstractByte2ShortMap implements ITri } @Override - public Byte2ShortMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Byte2ShortMap.Entry state = null; boolean empty = true; @@ -937,25 +940,25 @@ public class Byte2ShortOpenHashMap extends AbstractByte2ShortMap implements ITri } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Byte2ShortMap.Entry 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(keys[i] != (byte)0) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1127,7 +1130,7 @@ public class Byte2ShortOpenHashMap extends AbstractByte2ShortMap implements ITri } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -1144,18 +1147,18 @@ public class Byte2ShortOpenHashMap extends AbstractByte2ShortMap implements ITri } state = operator.applyAsByte(state, keys[i]); } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (byte)0; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OptionalByte.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OptionalByte.of(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(keys[i] != (byte)0 && filter.test(keys[i])) return keys[i]; + if(keys[i] != (byte)0 && filter.test(keys[i])) return OptionalByte.of(keys[i]); } - return (byte)0; + return OptionalByte.empty(); } @Override @@ -1270,7 +1273,7 @@ public class Byte2ShortOpenHashMap extends AbstractByte2ShortMap implements ITri } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -1287,18 +1290,18 @@ public class Byte2ShortOpenHashMap extends AbstractByte2ShortMap implements ITri } state = operator.applyAsShort(state, values[i]); } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (short)0; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return OptionalShort.empty(); + if(containsNull && filter.test(values[nullIndex])) return OptionalShort.of(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(keys[i] != (byte)0 && filter.test(values[i])) return values[i]; + if(keys[i] != (byte)0 && filter.test(values[i])) return OptionalShort.of(values[i]); } - return (short)0; + return OptionalShort.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/bytes/maps/impl/immutable/ImmutableByte2BooleanOpenHashMap.java b/src/main/java/speiger/src/collections/bytes/maps/impl/immutable/ImmutableByte2BooleanOpenHashMap.java index e2ee3f3..ad86aee 100644 --- a/src/main/java/speiger/src/collections/bytes/maps/impl/immutable/ImmutableByte2BooleanOpenHashMap.java +++ b/src/main/java/speiger/src/collections/bytes/maps/impl/immutable/ImmutableByte2BooleanOpenHashMap.java @@ -4,6 +4,7 @@ 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; @@ -14,6 +15,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectByteConsumer; import speiger.src.collections.ints.functions.consumer.IntByteConsumer; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntBooleanConsumer; +import speiger.src.collections.bytes.functions.OptionalByte; import speiger.src.collections.bytes.functions.consumer.ByteBooleanConsumer; import speiger.src.collections.bytes.functions.function.BytePredicate; import speiger.src.collections.bytes.functions.function.ByteBooleanUnaryOperator; @@ -28,6 +30,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectBooleanConsumer; import speiger.src.collections.booleans.functions.function.BooleanPredicate; +import speiger.src.collections.booleans.functions.OptionalBoolean; import speiger.src.collections.bytes.sets.AbstractByteSet; import speiger.src.collections.booleans.collections.AbstractBooleanCollection; import speiger.src.collections.booleans.collections.BooleanOrderedCollection; @@ -533,6 +536,10 @@ public class ImmutableByte2BooleanOpenHashMap extends AbstractByte2BooleanMap im } private class MapEntrySet extends AbstractObjectSet implements Byte2BooleanOrderedMap.FastOrderedSet { + @Override + public void addFirst(Byte2BooleanMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Byte2BooleanMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Byte2BooleanMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -686,7 +693,7 @@ public class ImmutableByte2BooleanOpenHashMap extends AbstractByte2BooleanMap im } @Override - public Byte2BooleanMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Byte2BooleanMap.Entry state = null; boolean empty = true; @@ -701,21 +708,21 @@ public class ImmutableByte2BooleanOpenHashMap extends AbstractByte2BooleanMap im state = operator.apply(state, new BasicEntry(keys[index], values[index])); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Byte2BooleanMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry entry = new BasicEntry(); 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 @@ -778,6 +785,12 @@ public class ImmutableByte2BooleanOpenHashMap extends AbstractByte2BooleanMap im throw new UnsupportedOperationException(); } + @Override + public void addFirst(byte o) { throw new UnsupportedOperationException(); } + + @Override + public void addLast(byte o) { throw new UnsupportedOperationException(); } + @Override public boolean addAndMoveToFirst(byte o) { throw new UnsupportedOperationException(); } @@ -913,7 +926,7 @@ public class ImmutableByte2BooleanOpenHashMap extends AbstractByte2BooleanMap im } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -928,19 +941,19 @@ public class ImmutableByte2BooleanOpenHashMap extends AbstractByte2BooleanMap im state = operator.applyAsByte(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (byte)0; + if(size() <= 0) return OptionalByte.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalByte.of(keys[index]); index = (int)links[index]; } - return (byte)0; + return OptionalByte.empty(); } @Override @@ -1067,7 +1080,7 @@ public class ImmutableByte2BooleanOpenHashMap extends AbstractByte2BooleanMap im } @Override - public boolean reduce(BooleanBooleanUnaryOperator operator) { + public OptionalBoolean reduce(BooleanBooleanUnaryOperator operator) { Objects.requireNonNull(operator); boolean state = false; boolean empty = true; @@ -1082,19 +1095,19 @@ public class ImmutableByte2BooleanOpenHashMap extends AbstractByte2BooleanMap im state = operator.applyAsBoolean(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalBoolean.empty() : OptionalBoolean.of(state); } @Override - public boolean findFirst(BooleanPredicate filter) { + public OptionalBoolean findFirst(BooleanPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return false; + if(size() <= 0) return OptionalBoolean.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalBoolean.of(values[index]); index = (int)links[index]; } - return false; + return OptionalBoolean.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/bytes/maps/impl/immutable/ImmutableByte2ByteOpenHashMap.java b/src/main/java/speiger/src/collections/bytes/maps/impl/immutable/ImmutableByte2ByteOpenHashMap.java index abbacbe..3b9da7a 100644 --- a/src/main/java/speiger/src/collections/bytes/maps/impl/immutable/ImmutableByte2ByteOpenHashMap.java +++ b/src/main/java/speiger/src/collections/bytes/maps/impl/immutable/ImmutableByte2ByteOpenHashMap.java @@ -4,6 +4,7 @@ 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; @@ -13,6 +14,7 @@ import speiger.src.collections.bytes.functions.ByteConsumer; import speiger.src.collections.objects.functions.consumer.ObjectByteConsumer; import speiger.src.collections.ints.functions.consumer.IntByteConsumer; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; +import speiger.src.collections.bytes.functions.OptionalByte; import speiger.src.collections.bytes.functions.function.BytePredicate; import speiger.src.collections.bytes.functions.consumer.ByteByteConsumer; import speiger.src.collections.bytes.functions.function.ByteUnaryOperator; @@ -529,6 +531,10 @@ public class ImmutableByte2ByteOpenHashMap extends AbstractByte2ByteMap implemen } private class MapEntrySet extends AbstractObjectSet implements Byte2ByteOrderedMap.FastOrderedSet { + @Override + public void addFirst(Byte2ByteMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Byte2ByteMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Byte2ByteMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -682,7 +688,7 @@ public class ImmutableByte2ByteOpenHashMap extends AbstractByte2ByteMap implemen } @Override - public Byte2ByteMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Byte2ByteMap.Entry state = null; boolean empty = true; @@ -697,21 +703,21 @@ public class ImmutableByte2ByteOpenHashMap extends AbstractByte2ByteMap implemen state = operator.apply(state, new BasicEntry(keys[index], values[index])); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Byte2ByteMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry entry = new BasicEntry(); 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 @@ -774,6 +780,12 @@ public class ImmutableByte2ByteOpenHashMap extends AbstractByte2ByteMap implemen throw new UnsupportedOperationException(); } + @Override + public void addFirst(byte o) { throw new UnsupportedOperationException(); } + + @Override + public void addLast(byte o) { throw new UnsupportedOperationException(); } + @Override public boolean addAndMoveToFirst(byte o) { throw new UnsupportedOperationException(); } @@ -909,7 +921,7 @@ public class ImmutableByte2ByteOpenHashMap extends AbstractByte2ByteMap implemen } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -924,19 +936,19 @@ public class ImmutableByte2ByteOpenHashMap extends AbstractByte2ByteMap implemen state = operator.applyAsByte(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (byte)0; + if(size() <= 0) return OptionalByte.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalByte.of(keys[index]); index = (int)links[index]; } - return (byte)0; + return OptionalByte.empty(); } @Override @@ -1063,7 +1075,7 @@ public class ImmutableByte2ByteOpenHashMap extends AbstractByte2ByteMap implemen } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -1078,19 +1090,19 @@ public class ImmutableByte2ByteOpenHashMap extends AbstractByte2ByteMap implemen state = operator.applyAsByte(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (byte)0; + if(size() <= 0) return OptionalByte.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalByte.of(values[index]); index = (int)links[index]; } - return (byte)0; + return OptionalByte.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/bytes/maps/impl/immutable/ImmutableByte2CharOpenHashMap.java b/src/main/java/speiger/src/collections/bytes/maps/impl/immutable/ImmutableByte2CharOpenHashMap.java index e6dc98c..d32d1c8 100644 --- a/src/main/java/speiger/src/collections/bytes/maps/impl/immutable/ImmutableByte2CharOpenHashMap.java +++ b/src/main/java/speiger/src/collections/bytes/maps/impl/immutable/ImmutableByte2CharOpenHashMap.java @@ -4,6 +4,7 @@ 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; @@ -14,6 +15,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectByteConsumer; import speiger.src.collections.ints.functions.consumer.IntByteConsumer; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntCharConsumer; +import speiger.src.collections.bytes.functions.OptionalByte; import speiger.src.collections.bytes.functions.function.BytePredicate; import speiger.src.collections.bytes.functions.consumer.ByteCharConsumer; import speiger.src.collections.bytes.functions.function.Byte2CharFunction; @@ -29,6 +31,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectCharConsumer; import speiger.src.collections.chars.functions.function.CharPredicate; +import speiger.src.collections.chars.functions.OptionalChar; import speiger.src.collections.bytes.sets.AbstractByteSet; import speiger.src.collections.chars.collections.AbstractCharCollection; import speiger.src.collections.chars.collections.CharOrderedCollection; @@ -538,6 +541,10 @@ public class ImmutableByte2CharOpenHashMap extends AbstractByte2CharMap implemen } private class MapEntrySet extends AbstractObjectSet implements Byte2CharOrderedMap.FastOrderedSet { + @Override + public void addFirst(Byte2CharMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Byte2CharMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Byte2CharMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -691,7 +698,7 @@ public class ImmutableByte2CharOpenHashMap extends AbstractByte2CharMap implemen } @Override - public Byte2CharMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Byte2CharMap.Entry state = null; boolean empty = true; @@ -706,21 +713,21 @@ public class ImmutableByte2CharOpenHashMap extends AbstractByte2CharMap implemen state = operator.apply(state, new BasicEntry(keys[index], values[index])); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Byte2CharMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry entry = new BasicEntry(); 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 @@ -783,6 +790,12 @@ public class ImmutableByte2CharOpenHashMap extends AbstractByte2CharMap implemen throw new UnsupportedOperationException(); } + @Override + public void addFirst(byte o) { throw new UnsupportedOperationException(); } + + @Override + public void addLast(byte o) { throw new UnsupportedOperationException(); } + @Override public boolean addAndMoveToFirst(byte o) { throw new UnsupportedOperationException(); } @@ -918,7 +931,7 @@ public class ImmutableByte2CharOpenHashMap extends AbstractByte2CharMap implemen } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -933,19 +946,19 @@ public class ImmutableByte2CharOpenHashMap extends AbstractByte2CharMap implemen state = operator.applyAsByte(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (byte)0; + if(size() <= 0) return OptionalByte.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalByte.of(keys[index]); index = (int)links[index]; } - return (byte)0; + return OptionalByte.empty(); } @Override @@ -1072,7 +1085,7 @@ public class ImmutableByte2CharOpenHashMap extends AbstractByte2CharMap implemen } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -1087,19 +1100,19 @@ public class ImmutableByte2CharOpenHashMap extends AbstractByte2CharMap implemen state = operator.applyAsChar(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (char)0; + if(size() <= 0) return OptionalChar.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalChar.of(values[index]); index = (int)links[index]; } - return (char)0; + return OptionalChar.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/bytes/maps/impl/immutable/ImmutableByte2DoubleOpenHashMap.java b/src/main/java/speiger/src/collections/bytes/maps/impl/immutable/ImmutableByte2DoubleOpenHashMap.java index 492af63..dbc7bc1 100644 --- a/src/main/java/speiger/src/collections/bytes/maps/impl/immutable/ImmutableByte2DoubleOpenHashMap.java +++ b/src/main/java/speiger/src/collections/bytes/maps/impl/immutable/ImmutableByte2DoubleOpenHashMap.java @@ -4,10 +4,12 @@ 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; import java.util.function.DoublePredicate; +import java.util.OptionalDouble; import speiger.src.collections.bytes.collections.ByteBidirectionalIterator; import speiger.src.collections.bytes.functions.ByteConsumer; @@ -15,6 +17,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectByteConsumer; import speiger.src.collections.ints.functions.consumer.IntByteConsumer; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntDoubleConsumer; +import speiger.src.collections.bytes.functions.OptionalByte; import speiger.src.collections.bytes.functions.function.BytePredicate; import speiger.src.collections.bytes.functions.consumer.ByteDoubleConsumer; import speiger.src.collections.bytes.functions.function.Byte2DoubleFunction; @@ -538,6 +541,10 @@ public class ImmutableByte2DoubleOpenHashMap extends AbstractByte2DoubleMap impl } private class MapEntrySet extends AbstractObjectSet implements Byte2DoubleOrderedMap.FastOrderedSet { + @Override + public void addFirst(Byte2DoubleMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Byte2DoubleMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Byte2DoubleMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -691,7 +698,7 @@ public class ImmutableByte2DoubleOpenHashMap extends AbstractByte2DoubleMap impl } @Override - public Byte2DoubleMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Byte2DoubleMap.Entry state = null; boolean empty = true; @@ -706,21 +713,21 @@ public class ImmutableByte2DoubleOpenHashMap extends AbstractByte2DoubleMap impl state = operator.apply(state, new BasicEntry(keys[index], values[index])); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Byte2DoubleMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry entry = new BasicEntry(); 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 @@ -783,6 +790,12 @@ public class ImmutableByte2DoubleOpenHashMap extends AbstractByte2DoubleMap impl throw new UnsupportedOperationException(); } + @Override + public void addFirst(byte o) { throw new UnsupportedOperationException(); } + + @Override + public void addLast(byte o) { throw new UnsupportedOperationException(); } + @Override public boolean addAndMoveToFirst(byte o) { throw new UnsupportedOperationException(); } @@ -918,7 +931,7 @@ public class ImmutableByte2DoubleOpenHashMap extends AbstractByte2DoubleMap impl } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -933,19 +946,19 @@ public class ImmutableByte2DoubleOpenHashMap extends AbstractByte2DoubleMap impl state = operator.applyAsByte(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (byte)0; + if(size() <= 0) return OptionalByte.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalByte.of(keys[index]); index = (int)links[index]; } - return (byte)0; + return OptionalByte.empty(); } @Override @@ -1072,7 +1085,7 @@ public class ImmutableByte2DoubleOpenHashMap extends AbstractByte2DoubleMap impl } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -1087,19 +1100,19 @@ public class ImmutableByte2DoubleOpenHashMap extends AbstractByte2DoubleMap impl state = operator.applyAsDouble(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0D; + if(size() <= 0) return OptionalDouble.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalDouble.of(values[index]); index = (int)links[index]; } - return 0D; + return OptionalDouble.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/bytes/maps/impl/immutable/ImmutableByte2FloatOpenHashMap.java b/src/main/java/speiger/src/collections/bytes/maps/impl/immutable/ImmutableByte2FloatOpenHashMap.java index 29ea36d..a8ce53a 100644 --- a/src/main/java/speiger/src/collections/bytes/maps/impl/immutable/ImmutableByte2FloatOpenHashMap.java +++ b/src/main/java/speiger/src/collections/bytes/maps/impl/immutable/ImmutableByte2FloatOpenHashMap.java @@ -4,6 +4,7 @@ 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; @@ -14,6 +15,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectByteConsumer; import speiger.src.collections.ints.functions.consumer.IntByteConsumer; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntFloatConsumer; +import speiger.src.collections.bytes.functions.OptionalByte; import speiger.src.collections.bytes.functions.function.BytePredicate; import speiger.src.collections.bytes.functions.consumer.ByteFloatConsumer; import speiger.src.collections.bytes.functions.function.Byte2FloatFunction; @@ -29,6 +31,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectFloatConsumer; import speiger.src.collections.floats.functions.function.FloatPredicate; +import speiger.src.collections.floats.functions.OptionalFloat; import speiger.src.collections.bytes.sets.AbstractByteSet; import speiger.src.collections.floats.collections.AbstractFloatCollection; import speiger.src.collections.floats.collections.FloatOrderedCollection; @@ -538,6 +541,10 @@ public class ImmutableByte2FloatOpenHashMap extends AbstractByte2FloatMap implem } private class MapEntrySet extends AbstractObjectSet implements Byte2FloatOrderedMap.FastOrderedSet { + @Override + public void addFirst(Byte2FloatMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Byte2FloatMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Byte2FloatMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -691,7 +698,7 @@ public class ImmutableByte2FloatOpenHashMap extends AbstractByte2FloatMap implem } @Override - public Byte2FloatMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Byte2FloatMap.Entry state = null; boolean empty = true; @@ -706,21 +713,21 @@ public class ImmutableByte2FloatOpenHashMap extends AbstractByte2FloatMap implem state = operator.apply(state, new BasicEntry(keys[index], values[index])); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Byte2FloatMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry entry = new BasicEntry(); 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 @@ -783,6 +790,12 @@ public class ImmutableByte2FloatOpenHashMap extends AbstractByte2FloatMap implem throw new UnsupportedOperationException(); } + @Override + public void addFirst(byte o) { throw new UnsupportedOperationException(); } + + @Override + public void addLast(byte o) { throw new UnsupportedOperationException(); } + @Override public boolean addAndMoveToFirst(byte o) { throw new UnsupportedOperationException(); } @@ -918,7 +931,7 @@ public class ImmutableByte2FloatOpenHashMap extends AbstractByte2FloatMap implem } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -933,19 +946,19 @@ public class ImmutableByte2FloatOpenHashMap extends AbstractByte2FloatMap implem state = operator.applyAsByte(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (byte)0; + if(size() <= 0) return OptionalByte.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalByte.of(keys[index]); index = (int)links[index]; } - return (byte)0; + return OptionalByte.empty(); } @Override @@ -1072,7 +1085,7 @@ public class ImmutableByte2FloatOpenHashMap extends AbstractByte2FloatMap implem } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -1087,19 +1100,19 @@ public class ImmutableByte2FloatOpenHashMap extends AbstractByte2FloatMap implem state = operator.applyAsFloat(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0F; + if(size() <= 0) return OptionalFloat.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalFloat.of(values[index]); index = (int)links[index]; } - return 0F; + return OptionalFloat.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/bytes/maps/impl/immutable/ImmutableByte2IntOpenHashMap.java b/src/main/java/speiger/src/collections/bytes/maps/impl/immutable/ImmutableByte2IntOpenHashMap.java index 3481ede..ad22d76 100644 --- a/src/main/java/speiger/src/collections/bytes/maps/impl/immutable/ImmutableByte2IntOpenHashMap.java +++ b/src/main/java/speiger/src/collections/bytes/maps/impl/immutable/ImmutableByte2IntOpenHashMap.java @@ -4,10 +4,12 @@ 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; import java.util.function.IntPredicate; +import java.util.OptionalInt; import speiger.src.collections.bytes.collections.ByteBidirectionalIterator; import speiger.src.collections.bytes.functions.ByteConsumer; @@ -15,6 +17,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectByteConsumer; import speiger.src.collections.ints.functions.consumer.IntByteConsumer; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntIntConsumer; +import speiger.src.collections.bytes.functions.OptionalByte; import speiger.src.collections.bytes.functions.function.BytePredicate; import speiger.src.collections.bytes.functions.consumer.ByteIntConsumer; import speiger.src.collections.bytes.functions.function.Byte2IntFunction; @@ -538,6 +541,10 @@ public class ImmutableByte2IntOpenHashMap extends AbstractByte2IntMap implements } private class MapEntrySet extends AbstractObjectSet implements Byte2IntOrderedMap.FastOrderedSet { + @Override + public void addFirst(Byte2IntMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Byte2IntMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Byte2IntMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -691,7 +698,7 @@ public class ImmutableByte2IntOpenHashMap extends AbstractByte2IntMap implements } @Override - public Byte2IntMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Byte2IntMap.Entry state = null; boolean empty = true; @@ -706,21 +713,21 @@ public class ImmutableByte2IntOpenHashMap extends AbstractByte2IntMap implements state = operator.apply(state, new BasicEntry(keys[index], values[index])); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Byte2IntMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry entry = new BasicEntry(); 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 @@ -783,6 +790,12 @@ public class ImmutableByte2IntOpenHashMap extends AbstractByte2IntMap implements throw new UnsupportedOperationException(); } + @Override + public void addFirst(byte o) { throw new UnsupportedOperationException(); } + + @Override + public void addLast(byte o) { throw new UnsupportedOperationException(); } + @Override public boolean addAndMoveToFirst(byte o) { throw new UnsupportedOperationException(); } @@ -918,7 +931,7 @@ public class ImmutableByte2IntOpenHashMap extends AbstractByte2IntMap implements } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -933,19 +946,19 @@ public class ImmutableByte2IntOpenHashMap extends AbstractByte2IntMap implements state = operator.applyAsByte(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (byte)0; + if(size() <= 0) return OptionalByte.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalByte.of(keys[index]); index = (int)links[index]; } - return (byte)0; + return OptionalByte.empty(); } @Override @@ -1072,7 +1085,7 @@ public class ImmutableByte2IntOpenHashMap extends AbstractByte2IntMap implements } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -1087,19 +1100,19 @@ public class ImmutableByte2IntOpenHashMap extends AbstractByte2IntMap implements state = operator.applyAsInt(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0; + if(size() <= 0) return OptionalInt.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalInt.of(values[index]); index = (int)links[index]; } - return 0; + return OptionalInt.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/bytes/maps/impl/immutable/ImmutableByte2LongOpenHashMap.java b/src/main/java/speiger/src/collections/bytes/maps/impl/immutable/ImmutableByte2LongOpenHashMap.java index 18bf351..74ca5bc 100644 --- a/src/main/java/speiger/src/collections/bytes/maps/impl/immutable/ImmutableByte2LongOpenHashMap.java +++ b/src/main/java/speiger/src/collections/bytes/maps/impl/immutable/ImmutableByte2LongOpenHashMap.java @@ -4,10 +4,12 @@ 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; import java.util.function.LongPredicate; +import java.util.OptionalLong; import speiger.src.collections.bytes.collections.ByteBidirectionalIterator; import speiger.src.collections.bytes.functions.ByteConsumer; @@ -15,6 +17,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectByteConsumer; import speiger.src.collections.ints.functions.consumer.IntByteConsumer; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntLongConsumer; +import speiger.src.collections.bytes.functions.OptionalByte; import speiger.src.collections.bytes.functions.function.BytePredicate; import speiger.src.collections.bytes.functions.consumer.ByteLongConsumer; import speiger.src.collections.bytes.functions.function.Byte2LongFunction; @@ -538,6 +541,10 @@ public class ImmutableByte2LongOpenHashMap extends AbstractByte2LongMap implemen } private class MapEntrySet extends AbstractObjectSet implements Byte2LongOrderedMap.FastOrderedSet { + @Override + public void addFirst(Byte2LongMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Byte2LongMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Byte2LongMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -691,7 +698,7 @@ public class ImmutableByte2LongOpenHashMap extends AbstractByte2LongMap implemen } @Override - public Byte2LongMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Byte2LongMap.Entry state = null; boolean empty = true; @@ -706,21 +713,21 @@ public class ImmutableByte2LongOpenHashMap extends AbstractByte2LongMap implemen state = operator.apply(state, new BasicEntry(keys[index], values[index])); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Byte2LongMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry entry = new BasicEntry(); 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 @@ -783,6 +790,12 @@ public class ImmutableByte2LongOpenHashMap extends AbstractByte2LongMap implemen throw new UnsupportedOperationException(); } + @Override + public void addFirst(byte o) { throw new UnsupportedOperationException(); } + + @Override + public void addLast(byte o) { throw new UnsupportedOperationException(); } + @Override public boolean addAndMoveToFirst(byte o) { throw new UnsupportedOperationException(); } @@ -918,7 +931,7 @@ public class ImmutableByte2LongOpenHashMap extends AbstractByte2LongMap implemen } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -933,19 +946,19 @@ public class ImmutableByte2LongOpenHashMap extends AbstractByte2LongMap implemen state = operator.applyAsByte(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (byte)0; + if(size() <= 0) return OptionalByte.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalByte.of(keys[index]); index = (int)links[index]; } - return (byte)0; + return OptionalByte.empty(); } @Override @@ -1072,7 +1085,7 @@ public class ImmutableByte2LongOpenHashMap extends AbstractByte2LongMap implemen } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -1087,19 +1100,19 @@ public class ImmutableByte2LongOpenHashMap extends AbstractByte2LongMap implemen state = operator.applyAsLong(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0L; + if(size() <= 0) return OptionalLong.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalLong.of(values[index]); index = (int)links[index]; } - return 0L; + return OptionalLong.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/bytes/maps/impl/immutable/ImmutableByte2ObjectOpenHashMap.java b/src/main/java/speiger/src/collections/bytes/maps/impl/immutable/ImmutableByte2ObjectOpenHashMap.java index 9e0fd8a..19ea5ed 100644 --- a/src/main/java/speiger/src/collections/bytes/maps/impl/immutable/ImmutableByte2ObjectOpenHashMap.java +++ b/src/main/java/speiger/src/collections/bytes/maps/impl/immutable/ImmutableByte2ObjectOpenHashMap.java @@ -4,6 +4,7 @@ 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; @@ -13,6 +14,7 @@ import speiger.src.collections.bytes.functions.ByteConsumer; import speiger.src.collections.objects.functions.consumer.ObjectByteConsumer; import speiger.src.collections.ints.functions.consumer.IntByteConsumer; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; +import speiger.src.collections.bytes.functions.OptionalByte; import speiger.src.collections.bytes.functions.function.BytePredicate; import speiger.src.collections.bytes.functions.consumer.ByteObjectConsumer; import speiger.src.collections.bytes.functions.function.ByteFunction; @@ -509,6 +511,10 @@ public class ImmutableByte2ObjectOpenHashMap extends AbstractByte2ObjectMap> implements Byte2ObjectOrderedMap.FastOrderedSet { + @Override + public void addFirst(Byte2ObjectMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Byte2ObjectMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Byte2ObjectMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -662,7 +668,7 @@ public class ImmutableByte2ObjectOpenHashMap extends AbstractByte2ObjectMap reduce(ObjectObjectUnaryOperator, Byte2ObjectMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Byte2ObjectMap.Entry> operator) { Objects.requireNonNull(operator); Byte2ObjectMap.Entry state = null; boolean empty = true; @@ -677,21 +683,21 @@ public class ImmutableByte2ObjectOpenHashMap extends AbstractByte2ObjectMap(keys[index], values[index])); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Byte2ObjectMap.Entry findFirst(Predicate> filter) { + public Optional> findFirst(Predicate> filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry entry = new BasicEntry<>(); 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 @@ -754,6 +760,12 @@ public class ImmutableByte2ObjectOpenHashMap extends AbstractByte2ObjectMap extends AbstractByte2ObjectMap extends AbstractByte2ObjectMap extends AbstractByte2ObjectMap operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); V state = null; boolean empty = true; @@ -1059,19 +1071,19 @@ public class ImmutableByte2ObjectOpenHashMap extends AbstractByte2ObjectMap filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return Optional.ofNullable(values[index]); index = (int)links[index]; } - return null; + return Optional.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/bytes/maps/impl/immutable/ImmutableByte2ShortOpenHashMap.java b/src/main/java/speiger/src/collections/bytes/maps/impl/immutable/ImmutableByte2ShortOpenHashMap.java index 0953ba8..794e7ce 100644 --- a/src/main/java/speiger/src/collections/bytes/maps/impl/immutable/ImmutableByte2ShortOpenHashMap.java +++ b/src/main/java/speiger/src/collections/bytes/maps/impl/immutable/ImmutableByte2ShortOpenHashMap.java @@ -4,6 +4,7 @@ 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; @@ -14,6 +15,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectByteConsumer; import speiger.src.collections.ints.functions.consumer.IntByteConsumer; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntShortConsumer; +import speiger.src.collections.bytes.functions.OptionalByte; import speiger.src.collections.bytes.functions.function.BytePredicate; import speiger.src.collections.bytes.functions.consumer.ByteShortConsumer; import speiger.src.collections.bytes.functions.function.Byte2ShortFunction; @@ -29,6 +31,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectShortConsumer; import speiger.src.collections.shorts.functions.function.ShortPredicate; +import speiger.src.collections.shorts.functions.OptionalShort; import speiger.src.collections.bytes.sets.AbstractByteSet; import speiger.src.collections.shorts.collections.AbstractShortCollection; import speiger.src.collections.shorts.collections.ShortOrderedCollection; @@ -538,6 +541,10 @@ public class ImmutableByte2ShortOpenHashMap extends AbstractByte2ShortMap implem } private class MapEntrySet extends AbstractObjectSet implements Byte2ShortOrderedMap.FastOrderedSet { + @Override + public void addFirst(Byte2ShortMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Byte2ShortMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Byte2ShortMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -691,7 +698,7 @@ public class ImmutableByte2ShortOpenHashMap extends AbstractByte2ShortMap implem } @Override - public Byte2ShortMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Byte2ShortMap.Entry state = null; boolean empty = true; @@ -706,21 +713,21 @@ public class ImmutableByte2ShortOpenHashMap extends AbstractByte2ShortMap implem state = operator.apply(state, new BasicEntry(keys[index], values[index])); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Byte2ShortMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry entry = new BasicEntry(); 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 @@ -783,6 +790,12 @@ public class ImmutableByte2ShortOpenHashMap extends AbstractByte2ShortMap implem throw new UnsupportedOperationException(); } + @Override + public void addFirst(byte o) { throw new UnsupportedOperationException(); } + + @Override + public void addLast(byte o) { throw new UnsupportedOperationException(); } + @Override public boolean addAndMoveToFirst(byte o) { throw new UnsupportedOperationException(); } @@ -918,7 +931,7 @@ public class ImmutableByte2ShortOpenHashMap extends AbstractByte2ShortMap implem } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -933,19 +946,19 @@ public class ImmutableByte2ShortOpenHashMap extends AbstractByte2ShortMap implem state = operator.applyAsByte(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (byte)0; + if(size() <= 0) return OptionalByte.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalByte.of(keys[index]); index = (int)links[index]; } - return (byte)0; + return OptionalByte.empty(); } @Override @@ -1072,7 +1085,7 @@ public class ImmutableByte2ShortOpenHashMap extends AbstractByte2ShortMap implem } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -1087,19 +1100,19 @@ public class ImmutableByte2ShortOpenHashMap extends AbstractByte2ShortMap implem state = operator.applyAsShort(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (short)0; + if(size() <= 0) return OptionalShort.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalShort.of(values[index]); index = (int)links[index]; } - return (short)0; + return OptionalShort.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/bytes/maps/impl/misc/Byte2BooleanArrayMap.java b/src/main/java/speiger/src/collections/bytes/maps/impl/misc/Byte2BooleanArrayMap.java index 5ddfb05..ee602b3 100644 --- a/src/main/java/speiger/src/collections/bytes/maps/impl/misc/Byte2BooleanArrayMap.java +++ b/src/main/java/speiger/src/collections/bytes/maps/impl/misc/Byte2BooleanArrayMap.java @@ -4,6 +4,7 @@ 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; @@ -14,6 +15,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectByteConsumer; import speiger.src.collections.ints.functions.consumer.IntByteConsumer; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntBooleanConsumer; +import speiger.src.collections.bytes.functions.OptionalByte; import speiger.src.collections.bytes.functions.consumer.ByteBooleanConsumer; import speiger.src.collections.bytes.functions.function.BytePredicate; import speiger.src.collections.bytes.functions.function.ByteBooleanUnaryOperator; @@ -34,6 +36,7 @@ import speiger.src.collections.booleans.lists.BooleanListIterator; import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.booleans.functions.function.BooleanPredicate; +import speiger.src.collections.booleans.functions.OptionalBoolean; import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOperator; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; @@ -728,6 +731,10 @@ public class Byte2BooleanArrayMap extends AbstractByte2BooleanMap implements Byt } private class MapEntrySet extends AbstractObjectSet implements Byte2BooleanOrderedMap.FastOrderedSet { + @Override + public void addFirst(Byte2BooleanMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Byte2BooleanMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Byte2BooleanMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -876,7 +883,7 @@ public class Byte2BooleanArrayMap extends AbstractByte2BooleanMap implements Byt } @Override - public Byte2BooleanMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Byte2BooleanMap.Entry state = null; boolean empty = true; @@ -888,19 +895,19 @@ public class Byte2BooleanArrayMap extends AbstractByte2BooleanMap implements Byt } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Byte2BooleanMap.Entry 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 implements Byte2ByteOrderedMap.FastOrderedSet { + @Override + public void addFirst(Byte2ByteMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Byte2ByteMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Byte2ByteMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -892,7 +898,7 @@ public class Byte2ByteArrayMap extends AbstractByte2ByteMap implements Byte2Byte } @Override - public Byte2ByteMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Byte2ByteMap.Entry state = null; boolean empty = true; @@ -904,19 +910,19 @@ public class Byte2ByteArrayMap extends AbstractByte2ByteMap implements Byte2Byte } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Byte2ByteMap.Entry 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 implements Byte2CharOrderedMap.FastOrderedSet { + @Override + public void addFirst(Byte2CharMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Byte2CharMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Byte2CharMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -899,7 +906,7 @@ public class Byte2CharArrayMap extends AbstractByte2CharMap implements Byte2Char } @Override - public Byte2CharMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Byte2CharMap.Entry state = null; boolean empty = true; @@ -911,19 +918,19 @@ public class Byte2CharArrayMap extends AbstractByte2CharMap implements Byte2Char } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Byte2CharMap.Entry 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 implements Byte2DoubleOrderedMap.FastOrderedSet { + @Override + public void addFirst(Byte2DoubleMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Byte2DoubleMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Byte2DoubleMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -899,7 +906,7 @@ public class Byte2DoubleArrayMap extends AbstractByte2DoubleMap implements Byte2 } @Override - public Byte2DoubleMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Byte2DoubleMap.Entry state = null; boolean empty = true; @@ -911,19 +918,19 @@ public class Byte2DoubleArrayMap extends AbstractByte2DoubleMap implements Byte2 } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Byte2DoubleMap.Entry 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 implements Byte2FloatOrderedMap.FastOrderedSet { + @Override + public void addFirst(Byte2FloatMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Byte2FloatMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Byte2FloatMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -899,7 +906,7 @@ public class Byte2FloatArrayMap extends AbstractByte2FloatMap implements Byte2Fl } @Override - public Byte2FloatMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Byte2FloatMap.Entry state = null; boolean empty = true; @@ -911,19 +918,19 @@ public class Byte2FloatArrayMap extends AbstractByte2FloatMap implements Byte2Fl } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Byte2FloatMap.Entry 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 implements Byte2IntOrderedMap.FastOrderedSet { + @Override + public void addFirst(Byte2IntMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Byte2IntMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Byte2IntMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -899,7 +906,7 @@ public class Byte2IntArrayMap extends AbstractByte2IntMap implements Byte2IntOrd } @Override - public Byte2IntMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Byte2IntMap.Entry state = null; boolean empty = true; @@ -911,19 +918,19 @@ public class Byte2IntArrayMap extends AbstractByte2IntMap implements Byte2IntOrd } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Byte2IntMap.Entry 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 implements Byte2LongOrderedMap.FastOrderedSet { + @Override + public void addFirst(Byte2LongMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Byte2LongMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Byte2LongMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -899,7 +906,7 @@ public class Byte2LongArrayMap extends AbstractByte2LongMap implements Byte2Long } @Override - public Byte2LongMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Byte2LongMap.Entry state = null; boolean empty = true; @@ -911,19 +918,19 @@ public class Byte2LongArrayMap extends AbstractByte2LongMap implements Byte2Long } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Byte2LongMap.Entry 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 extends AbstractByte2ObjectMap implements } private class MapEntrySet extends AbstractObjectSet> implements Byte2ObjectOrderedMap.FastOrderedSet { + @Override + public void addFirst(Byte2ObjectMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Byte2ObjectMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Byte2ObjectMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -812,7 +818,7 @@ public class Byte2ObjectArrayMap extends AbstractByte2ObjectMap implements } @Override - public Byte2ObjectMap.Entry reduce(ObjectObjectUnaryOperator, Byte2ObjectMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Byte2ObjectMap.Entry> operator) { Objects.requireNonNull(operator); Byte2ObjectMap.Entry state = null; boolean empty = true; @@ -824,19 +830,19 @@ public class Byte2ObjectArrayMap extends AbstractByte2ObjectMap implements } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Byte2ObjectMap.Entry 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 extends AbstractByte2ObjectMap implements @Override public boolean add(byte o) { throw new UnsupportedOperationException(); } @Override + public void addFirst(byte o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(byte o) { throw new UnsupportedOperationException(); } + @Override public boolean addAndMoveToFirst(byte o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToLast(byte o) { throw new UnsupportedOperationException(); } @@ -995,7 +1005,7 @@ public class Byte2ObjectArrayMap extends AbstractByte2ObjectMap implements } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -1007,16 +1017,16 @@ public class Byte2ObjectArrayMap extends AbstractByte2ObjectMap implements } state = operator.applyAsByte(state, keys[i]); } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); for(int i = 0;i extends AbstractByte2ObjectMap implements } @Override - public V reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); V state = null; boolean empty = true; @@ -1129,16 +1139,16 @@ public class Byte2ObjectArrayMap extends AbstractByte2ObjectMap implements } state = operator.apply(state, values[i]); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public V findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); for(int i = 0;i implements Byte2ShortOrderedMap.FastOrderedSet { + @Override + public void addFirst(Byte2ShortMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Byte2ShortMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Byte2ShortMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -899,7 +906,7 @@ public class Byte2ShortArrayMap extends AbstractByte2ShortMap implements Byte2Sh } @Override - public Byte2ShortMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Byte2ShortMap.Entry state = null; boolean empty = true; @@ -911,19 +918,19 @@ public class Byte2ShortArrayMap extends AbstractByte2ShortMap implements Byte2Sh } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Byte2ShortMap.Entry 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); Byte2BooleanMap.Entry state = null; boolean empty = true; @@ -1724,19 +1727,19 @@ public class Byte2BooleanAVLTreeMap extends AbstractByte2BooleanMap implements B } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Byte2BooleanMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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 @@ -1831,7 +1834,7 @@ public class Byte2BooleanAVLTreeMap extends AbstractByte2BooleanMap implements B } @Override - public boolean reduce(BooleanBooleanUnaryOperator operator) { + public OptionalBoolean reduce(BooleanBooleanUnaryOperator operator) { Objects.requireNonNull(operator); boolean state = false; boolean empty = true; @@ -1843,15 +1846,15 @@ public class Byte2BooleanAVLTreeMap extends AbstractByte2BooleanMap implements B } state = operator.applyAsBoolean(state, entry.value); } - return state; + return empty ? OptionalBoolean.empty() : OptionalBoolean.of(state); } @Override - public boolean findFirst(BooleanPredicate filter) { + public OptionalBoolean findFirst(BooleanPredicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return false; + if(filter.test(entry.value)) return OptionalBoolean.of(entry.value); + return OptionalBoolean.empty(); } @Override @@ -2167,7 +2170,7 @@ public class Byte2BooleanAVLTreeMap extends AbstractByte2BooleanMap implements B } @Override - public boolean reduce(BooleanBooleanUnaryOperator operator) { + public OptionalBoolean reduce(BooleanBooleanUnaryOperator operator) { Objects.requireNonNull(operator); boolean state = false; boolean empty = true; @@ -2179,15 +2182,15 @@ public class Byte2BooleanAVLTreeMap extends AbstractByte2BooleanMap implements B } state = operator.applyAsBoolean(state, entry.value); } - return state; + return empty ? OptionalBoolean.empty() : OptionalBoolean.of(state); } @Override - public boolean findFirst(BooleanPredicate filter) { + public OptionalBoolean findFirst(BooleanPredicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return false; + if(filter.test(entry.value)) return OptionalBoolean.of(entry.value); + return OptionalBoolean.empty(); } @Override @@ -2328,7 +2331,7 @@ public class Byte2BooleanAVLTreeMap extends AbstractByte2BooleanMap implements B } @Override - public Byte2BooleanMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Byte2BooleanMap.Entry state = null; boolean empty = true; @@ -2340,19 +2343,19 @@ public class Byte2BooleanAVLTreeMap extends AbstractByte2BooleanMap implements B } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Byte2BooleanMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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/main/java/speiger/src/collections/bytes/maps/impl/tree/Byte2BooleanRBTreeMap.java b/src/main/java/speiger/src/collections/bytes/maps/impl/tree/Byte2BooleanRBTreeMap.java index 5e9799e..bb5e0f6 100644 --- a/src/main/java/speiger/src/collections/bytes/maps/impl/tree/Byte2BooleanRBTreeMap.java +++ b/src/main/java/speiger/src/collections/bytes/maps/impl/tree/Byte2BooleanRBTreeMap.java @@ -3,6 +3,7 @@ package speiger.src.collections.bytes.maps.impl.tree; import java.util.Collections; import java.util.Map; import java.util.Objects; +import java.util.Optional; import java.util.NoSuchElementException; import java.util.function.Consumer; import java.util.function.BiFunction; @@ -17,6 +18,7 @@ import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntBooleanConsumer; import speiger.src.collections.bytes.functions.function.BytePredicate; import speiger.src.collections.bytes.functions.consumer.ByteBooleanConsumer; +import speiger.src.collections.bytes.functions.OptionalByte; import speiger.src.collections.bytes.functions.function.ByteBooleanUnaryOperator; import speiger.src.collections.bytes.functions.function.ByteByteUnaryOperator; import speiger.src.collections.bytes.maps.abstracts.AbstractByte2BooleanMap; @@ -35,6 +37,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectBooleanConsumer; import speiger.src.collections.booleans.functions.function.BooleanPredicate; +import speiger.src.collections.booleans.functions.OptionalBoolean; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOperator; @@ -1123,7 +1126,7 @@ public class Byte2BooleanRBTreeMap extends AbstractByte2BooleanMap implements By } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -1135,15 +1138,15 @@ public class Byte2BooleanRBTreeMap extends AbstractByte2BooleanMap implements By } state = operator.applyAsByte(state, entry.key); } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); for(Node entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.test(entry.key)) return entry.key; - return (byte)0; + if(filter.test(entry.key)) return OptionalByte.of(entry.key); + return OptionalByte.empty(); } @Override @@ -1775,7 +1778,7 @@ public class Byte2BooleanRBTreeMap extends AbstractByte2BooleanMap implements By } @Override - public Byte2BooleanMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Byte2BooleanMap.Entry state = null; boolean empty = true; @@ -1787,19 +1790,19 @@ public class Byte2BooleanRBTreeMap extends AbstractByte2BooleanMap implements By } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Byte2BooleanMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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 @@ -1894,7 +1897,7 @@ public class Byte2BooleanRBTreeMap extends AbstractByte2BooleanMap implements By } @Override - public boolean reduce(BooleanBooleanUnaryOperator operator) { + public OptionalBoolean reduce(BooleanBooleanUnaryOperator operator) { Objects.requireNonNull(operator); boolean state = false; boolean empty = true; @@ -1906,15 +1909,15 @@ public class Byte2BooleanRBTreeMap extends AbstractByte2BooleanMap implements By } state = operator.applyAsBoolean(state, entry.value); } - return state; + return empty ? OptionalBoolean.empty() : OptionalBoolean.of(state); } @Override - public boolean findFirst(BooleanPredicate filter) { + public OptionalBoolean findFirst(BooleanPredicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return false; + if(filter.test(entry.value)) return OptionalBoolean.of(entry.value); + return OptionalBoolean.empty(); } @Override @@ -2230,7 +2233,7 @@ public class Byte2BooleanRBTreeMap extends AbstractByte2BooleanMap implements By } @Override - public boolean reduce(BooleanBooleanUnaryOperator operator) { + public OptionalBoolean reduce(BooleanBooleanUnaryOperator operator) { Objects.requireNonNull(operator); boolean state = false; boolean empty = true; @@ -2242,15 +2245,15 @@ public class Byte2BooleanRBTreeMap extends AbstractByte2BooleanMap implements By } state = operator.applyAsBoolean(state, entry.value); } - return state; + return empty ? OptionalBoolean.empty() : OptionalBoolean.of(state); } @Override - public boolean findFirst(BooleanPredicate filter) { + public OptionalBoolean findFirst(BooleanPredicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return false; + if(filter.test(entry.value)) return OptionalBoolean.of(entry.value); + return OptionalBoolean.empty(); } @Override @@ -2391,7 +2394,7 @@ public class Byte2BooleanRBTreeMap extends AbstractByte2BooleanMap implements By } @Override - public Byte2BooleanMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Byte2BooleanMap.Entry state = null; boolean empty = true; @@ -2403,19 +2406,19 @@ public class Byte2BooleanRBTreeMap extends AbstractByte2BooleanMap implements By } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Byte2BooleanMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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/main/java/speiger/src/collections/bytes/maps/impl/tree/Byte2ByteAVLTreeMap.java b/src/main/java/speiger/src/collections/bytes/maps/impl/tree/Byte2ByteAVLTreeMap.java index b4fd998..7917ec2 100644 --- a/src/main/java/speiger/src/collections/bytes/maps/impl/tree/Byte2ByteAVLTreeMap.java +++ b/src/main/java/speiger/src/collections/bytes/maps/impl/tree/Byte2ByteAVLTreeMap.java @@ -3,6 +3,7 @@ package speiger.src.collections.bytes.maps.impl.tree; import java.util.Collections; import java.util.Map; import java.util.Objects; +import java.util.Optional; import java.util.NoSuchElementException; import java.util.function.Consumer; import java.util.function.BiFunction; @@ -17,6 +18,7 @@ import speiger.src.collections.ints.functions.consumer.IntByteConsumer; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.bytes.functions.function.ByteUnaryOperator; import speiger.src.collections.bytes.functions.consumer.ByteByteConsumer; +import speiger.src.collections.bytes.functions.OptionalByte; import speiger.src.collections.bytes.functions.function.BytePredicate; import speiger.src.collections.bytes.functions.function.ByteByteUnaryOperator; import speiger.src.collections.bytes.maps.abstracts.AbstractByte2ByteMap; @@ -1121,7 +1123,7 @@ public class Byte2ByteAVLTreeMap extends AbstractByte2ByteMap implements Byte2By } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -1133,15 +1135,15 @@ public class Byte2ByteAVLTreeMap extends AbstractByte2ByteMap implements Byte2By } state = operator.applyAsByte(state, entry.key); } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); for(Node entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.test(entry.key)) return entry.key; - return (byte)0; + if(filter.test(entry.key)) return OptionalByte.of(entry.key); + return OptionalByte.empty(); } @Override @@ -1775,7 +1777,7 @@ public class Byte2ByteAVLTreeMap extends AbstractByte2ByteMap implements Byte2By } @Override - public Byte2ByteMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Byte2ByteMap.Entry state = null; boolean empty = true; @@ -1787,19 +1789,19 @@ public class Byte2ByteAVLTreeMap extends AbstractByte2ByteMap implements Byte2By } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Byte2ByteMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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 @@ -1894,7 +1896,7 @@ public class Byte2ByteAVLTreeMap extends AbstractByte2ByteMap implements Byte2By } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -1906,15 +1908,15 @@ public class Byte2ByteAVLTreeMap extends AbstractByte2ByteMap implements Byte2By } state = operator.applyAsByte(state, entry.value); } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return (byte)0; + if(filter.test(entry.value)) return OptionalByte.of(entry.value); + return OptionalByte.empty(); } @Override @@ -2230,7 +2232,7 @@ public class Byte2ByteAVLTreeMap extends AbstractByte2ByteMap implements Byte2By } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -2242,15 +2244,15 @@ public class Byte2ByteAVLTreeMap extends AbstractByte2ByteMap implements Byte2By } state = operator.applyAsByte(state, entry.value); } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return (byte)0; + if(filter.test(entry.value)) return OptionalByte.of(entry.value); + return OptionalByte.empty(); } @Override @@ -2391,7 +2393,7 @@ public class Byte2ByteAVLTreeMap extends AbstractByte2ByteMap implements Byte2By } @Override - public Byte2ByteMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Byte2ByteMap.Entry state = null; boolean empty = true; @@ -2403,19 +2405,19 @@ public class Byte2ByteAVLTreeMap extends AbstractByte2ByteMap implements Byte2By } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Byte2ByteMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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/main/java/speiger/src/collections/bytes/maps/impl/tree/Byte2ByteRBTreeMap.java b/src/main/java/speiger/src/collections/bytes/maps/impl/tree/Byte2ByteRBTreeMap.java index 309d5ac..8127810 100644 --- a/src/main/java/speiger/src/collections/bytes/maps/impl/tree/Byte2ByteRBTreeMap.java +++ b/src/main/java/speiger/src/collections/bytes/maps/impl/tree/Byte2ByteRBTreeMap.java @@ -3,6 +3,7 @@ package speiger.src.collections.bytes.maps.impl.tree; import java.util.Collections; import java.util.Map; import java.util.Objects; +import java.util.Optional; import java.util.NoSuchElementException; import java.util.function.Consumer; import java.util.function.BiFunction; @@ -16,6 +17,7 @@ import speiger.src.collections.ints.functions.consumer.IntByteConsumer; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.bytes.functions.function.ByteUnaryOperator; import speiger.src.collections.bytes.functions.consumer.ByteByteConsumer; +import speiger.src.collections.bytes.functions.OptionalByte; import speiger.src.collections.bytes.functions.function.BytePredicate; import speiger.src.collections.bytes.functions.function.ByteByteUnaryOperator; import speiger.src.collections.bytes.maps.abstracts.AbstractByte2ByteMap; @@ -1174,7 +1176,7 @@ public class Byte2ByteRBTreeMap extends AbstractByte2ByteMap implements Byte2Byt } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -1186,15 +1188,15 @@ public class Byte2ByteRBTreeMap extends AbstractByte2ByteMap implements Byte2Byt } state = operator.applyAsByte(state, entry.key); } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); for(Node entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.test(entry.key)) return entry.key; - return (byte)0; + if(filter.test(entry.key)) return OptionalByte.of(entry.key); + return OptionalByte.empty(); } @Override @@ -1838,7 +1840,7 @@ public class Byte2ByteRBTreeMap extends AbstractByte2ByteMap implements Byte2Byt } @Override - public Byte2ByteMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Byte2ByteMap.Entry state = null; boolean empty = true; @@ -1850,19 +1852,19 @@ public class Byte2ByteRBTreeMap extends AbstractByte2ByteMap implements Byte2Byt } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Byte2ByteMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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 @@ -1957,7 +1959,7 @@ public class Byte2ByteRBTreeMap extends AbstractByte2ByteMap implements Byte2Byt } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -1969,15 +1971,15 @@ public class Byte2ByteRBTreeMap extends AbstractByte2ByteMap implements Byte2Byt } state = operator.applyAsByte(state, entry.value); } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return (byte)0; + if(filter.test(entry.value)) return OptionalByte.of(entry.value); + return OptionalByte.empty(); } @Override @@ -2293,7 +2295,7 @@ public class Byte2ByteRBTreeMap extends AbstractByte2ByteMap implements Byte2Byt } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -2305,15 +2307,15 @@ public class Byte2ByteRBTreeMap extends AbstractByte2ByteMap implements Byte2Byt } state = operator.applyAsByte(state, entry.value); } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return (byte)0; + if(filter.test(entry.value)) return OptionalByte.of(entry.value); + return OptionalByte.empty(); } @Override @@ -2454,7 +2456,7 @@ public class Byte2ByteRBTreeMap extends AbstractByte2ByteMap implements Byte2Byt } @Override - public Byte2ByteMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Byte2ByteMap.Entry state = null; boolean empty = true; @@ -2466,19 +2468,19 @@ public class Byte2ByteRBTreeMap extends AbstractByte2ByteMap implements Byte2Byt } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Byte2ByteMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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/main/java/speiger/src/collections/bytes/maps/impl/tree/Byte2CharAVLTreeMap.java b/src/main/java/speiger/src/collections/bytes/maps/impl/tree/Byte2CharAVLTreeMap.java index f7c4e65..900a2d5 100644 --- a/src/main/java/speiger/src/collections/bytes/maps/impl/tree/Byte2CharAVLTreeMap.java +++ b/src/main/java/speiger/src/collections/bytes/maps/impl/tree/Byte2CharAVLTreeMap.java @@ -3,6 +3,7 @@ package speiger.src.collections.bytes.maps.impl.tree; import java.util.Collections; import java.util.Map; import java.util.Objects; +import java.util.Optional; import java.util.NoSuchElementException; import java.util.function.Consumer; import java.util.function.BiFunction; @@ -18,6 +19,7 @@ import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntCharConsumer; import speiger.src.collections.bytes.functions.function.Byte2CharFunction; import speiger.src.collections.bytes.functions.consumer.ByteCharConsumer; +import speiger.src.collections.bytes.functions.OptionalByte; import speiger.src.collections.bytes.functions.function.BytePredicate; import speiger.src.collections.bytes.functions.function.ByteCharUnaryOperator; import speiger.src.collections.bytes.functions.function.ByteByteUnaryOperator; @@ -37,6 +39,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectCharConsumer; import speiger.src.collections.chars.functions.function.CharPredicate; +import speiger.src.collections.chars.functions.OptionalChar; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOperator; @@ -1129,7 +1132,7 @@ public class Byte2CharAVLTreeMap extends AbstractByte2CharMap implements Byte2Ch } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -1141,15 +1144,15 @@ public class Byte2CharAVLTreeMap extends AbstractByte2CharMap implements Byte2Ch } state = operator.applyAsByte(state, entry.key); } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); for(Node entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.test(entry.key)) return entry.key; - return (byte)0; + if(filter.test(entry.key)) return OptionalByte.of(entry.key); + return OptionalByte.empty(); } @Override @@ -1783,7 +1786,7 @@ public class Byte2CharAVLTreeMap extends AbstractByte2CharMap implements Byte2Ch } @Override - public Byte2CharMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Byte2CharMap.Entry state = null; boolean empty = true; @@ -1795,19 +1798,19 @@ public class Byte2CharAVLTreeMap extends AbstractByte2CharMap implements Byte2Ch } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Byte2CharMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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 @@ -1902,7 +1905,7 @@ public class Byte2CharAVLTreeMap extends AbstractByte2CharMap implements Byte2Ch } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -1914,15 +1917,15 @@ public class Byte2CharAVLTreeMap extends AbstractByte2CharMap implements Byte2Ch } state = operator.applyAsChar(state, entry.value); } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return (char)0; + if(filter.test(entry.value)) return OptionalChar.of(entry.value); + return OptionalChar.empty(); } @Override @@ -2238,7 +2241,7 @@ public class Byte2CharAVLTreeMap extends AbstractByte2CharMap implements Byte2Ch } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -2250,15 +2253,15 @@ public class Byte2CharAVLTreeMap extends AbstractByte2CharMap implements Byte2Ch } state = operator.applyAsChar(state, entry.value); } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return (char)0; + if(filter.test(entry.value)) return OptionalChar.of(entry.value); + return OptionalChar.empty(); } @Override @@ -2399,7 +2402,7 @@ public class Byte2CharAVLTreeMap extends AbstractByte2CharMap implements Byte2Ch } @Override - public Byte2CharMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Byte2CharMap.Entry state = null; boolean empty = true; @@ -2411,19 +2414,19 @@ public class Byte2CharAVLTreeMap extends AbstractByte2CharMap implements Byte2Ch } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Byte2CharMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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/main/java/speiger/src/collections/bytes/maps/impl/tree/Byte2CharRBTreeMap.java b/src/main/java/speiger/src/collections/bytes/maps/impl/tree/Byte2CharRBTreeMap.java index 11f0caa..aaa8176 100644 --- a/src/main/java/speiger/src/collections/bytes/maps/impl/tree/Byte2CharRBTreeMap.java +++ b/src/main/java/speiger/src/collections/bytes/maps/impl/tree/Byte2CharRBTreeMap.java @@ -3,6 +3,7 @@ package speiger.src.collections.bytes.maps.impl.tree; import java.util.Collections; import java.util.Map; import java.util.Objects; +import java.util.Optional; import java.util.NoSuchElementException; import java.util.function.Consumer; import java.util.function.BiFunction; @@ -17,6 +18,7 @@ import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntCharConsumer; import speiger.src.collections.bytes.functions.function.Byte2CharFunction; import speiger.src.collections.bytes.functions.consumer.ByteCharConsumer; +import speiger.src.collections.bytes.functions.OptionalByte; import speiger.src.collections.bytes.functions.function.BytePredicate; import speiger.src.collections.bytes.functions.function.ByteCharUnaryOperator; import speiger.src.collections.bytes.functions.function.ByteByteUnaryOperator; @@ -36,6 +38,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectCharConsumer; import speiger.src.collections.chars.functions.function.CharPredicate; +import speiger.src.collections.chars.functions.OptionalChar; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOperator; @@ -1182,7 +1185,7 @@ public class Byte2CharRBTreeMap extends AbstractByte2CharMap implements Byte2Cha } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -1194,15 +1197,15 @@ public class Byte2CharRBTreeMap extends AbstractByte2CharMap implements Byte2Cha } state = operator.applyAsByte(state, entry.key); } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); for(Node entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.test(entry.key)) return entry.key; - return (byte)0; + if(filter.test(entry.key)) return OptionalByte.of(entry.key); + return OptionalByte.empty(); } @Override @@ -1846,7 +1849,7 @@ public class Byte2CharRBTreeMap extends AbstractByte2CharMap implements Byte2Cha } @Override - public Byte2CharMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Byte2CharMap.Entry state = null; boolean empty = true; @@ -1858,19 +1861,19 @@ public class Byte2CharRBTreeMap extends AbstractByte2CharMap implements Byte2Cha } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Byte2CharMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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 @@ -1965,7 +1968,7 @@ public class Byte2CharRBTreeMap extends AbstractByte2CharMap implements Byte2Cha } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -1977,15 +1980,15 @@ public class Byte2CharRBTreeMap extends AbstractByte2CharMap implements Byte2Cha } state = operator.applyAsChar(state, entry.value); } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return (char)0; + if(filter.test(entry.value)) return OptionalChar.of(entry.value); + return OptionalChar.empty(); } @Override @@ -2301,7 +2304,7 @@ public class Byte2CharRBTreeMap extends AbstractByte2CharMap implements Byte2Cha } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -2313,15 +2316,15 @@ public class Byte2CharRBTreeMap extends AbstractByte2CharMap implements Byte2Cha } state = operator.applyAsChar(state, entry.value); } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return (char)0; + if(filter.test(entry.value)) return OptionalChar.of(entry.value); + return OptionalChar.empty(); } @Override @@ -2462,7 +2465,7 @@ public class Byte2CharRBTreeMap extends AbstractByte2CharMap implements Byte2Cha } @Override - public Byte2CharMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Byte2CharMap.Entry state = null; boolean empty = true; @@ -2474,19 +2477,19 @@ public class Byte2CharRBTreeMap extends AbstractByte2CharMap implements Byte2Cha } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Byte2CharMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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/main/java/speiger/src/collections/bytes/maps/impl/tree/Byte2DoubleAVLTreeMap.java b/src/main/java/speiger/src/collections/bytes/maps/impl/tree/Byte2DoubleAVLTreeMap.java index 438ec1f..d2d4d18 100644 --- a/src/main/java/speiger/src/collections/bytes/maps/impl/tree/Byte2DoubleAVLTreeMap.java +++ b/src/main/java/speiger/src/collections/bytes/maps/impl/tree/Byte2DoubleAVLTreeMap.java @@ -3,11 +3,13 @@ package speiger.src.collections.bytes.maps.impl.tree; import java.util.Collections; import java.util.Map; 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; import java.util.function.DoublePredicate; +import java.util.OptionalDouble; import speiger.src.collections.bytes.collections.ByteBidirectionalIterator; @@ -19,6 +21,7 @@ import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntDoubleConsumer; import speiger.src.collections.bytes.functions.function.Byte2DoubleFunction; import speiger.src.collections.bytes.functions.consumer.ByteDoubleConsumer; +import speiger.src.collections.bytes.functions.OptionalByte; import speiger.src.collections.bytes.functions.function.BytePredicate; import speiger.src.collections.bytes.functions.function.ByteDoubleUnaryOperator; import speiger.src.collections.bytes.functions.function.ByteByteUnaryOperator; @@ -1129,7 +1132,7 @@ public class Byte2DoubleAVLTreeMap extends AbstractByte2DoubleMap implements Byt } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -1141,15 +1144,15 @@ public class Byte2DoubleAVLTreeMap extends AbstractByte2DoubleMap implements Byt } state = operator.applyAsByte(state, entry.key); } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); for(Node entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.test(entry.key)) return entry.key; - return (byte)0; + if(filter.test(entry.key)) return OptionalByte.of(entry.key); + return OptionalByte.empty(); } @Override @@ -1783,7 +1786,7 @@ public class Byte2DoubleAVLTreeMap extends AbstractByte2DoubleMap implements Byt } @Override - public Byte2DoubleMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Byte2DoubleMap.Entry state = null; boolean empty = true; @@ -1795,19 +1798,19 @@ public class Byte2DoubleAVLTreeMap extends AbstractByte2DoubleMap implements Byt } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Byte2DoubleMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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 @@ -1902,7 +1905,7 @@ public class Byte2DoubleAVLTreeMap extends AbstractByte2DoubleMap implements Byt } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -1914,15 +1917,15 @@ public class Byte2DoubleAVLTreeMap extends AbstractByte2DoubleMap implements Byt } state = operator.applyAsDouble(state, entry.value); } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return 0D; + if(filter.test(entry.value)) return OptionalDouble.of(entry.value); + return OptionalDouble.empty(); } @Override @@ -2238,7 +2241,7 @@ public class Byte2DoubleAVLTreeMap extends AbstractByte2DoubleMap implements Byt } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -2250,15 +2253,15 @@ public class Byte2DoubleAVLTreeMap extends AbstractByte2DoubleMap implements Byt } state = operator.applyAsDouble(state, entry.value); } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return 0D; + if(filter.test(entry.value)) return OptionalDouble.of(entry.value); + return OptionalDouble.empty(); } @Override @@ -2399,7 +2402,7 @@ public class Byte2DoubleAVLTreeMap extends AbstractByte2DoubleMap implements Byt } @Override - public Byte2DoubleMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Byte2DoubleMap.Entry state = null; boolean empty = true; @@ -2411,19 +2414,19 @@ public class Byte2DoubleAVLTreeMap extends AbstractByte2DoubleMap implements Byt } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Byte2DoubleMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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/main/java/speiger/src/collections/bytes/maps/impl/tree/Byte2DoubleRBTreeMap.java b/src/main/java/speiger/src/collections/bytes/maps/impl/tree/Byte2DoubleRBTreeMap.java index c72f3a3..d5aab0e 100644 --- a/src/main/java/speiger/src/collections/bytes/maps/impl/tree/Byte2DoubleRBTreeMap.java +++ b/src/main/java/speiger/src/collections/bytes/maps/impl/tree/Byte2DoubleRBTreeMap.java @@ -3,11 +3,13 @@ package speiger.src.collections.bytes.maps.impl.tree; import java.util.Collections; import java.util.Map; 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; import java.util.function.DoublePredicate; +import java.util.OptionalDouble; import speiger.src.collections.bytes.collections.ByteBidirectionalIterator; import speiger.src.collections.bytes.functions.ByteComparator; @@ -18,6 +20,7 @@ import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntDoubleConsumer; import speiger.src.collections.bytes.functions.function.Byte2DoubleFunction; import speiger.src.collections.bytes.functions.consumer.ByteDoubleConsumer; +import speiger.src.collections.bytes.functions.OptionalByte; import speiger.src.collections.bytes.functions.function.BytePredicate; import speiger.src.collections.bytes.functions.function.ByteDoubleUnaryOperator; import speiger.src.collections.bytes.functions.function.ByteByteUnaryOperator; @@ -1182,7 +1185,7 @@ public class Byte2DoubleRBTreeMap extends AbstractByte2DoubleMap implements Byte } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -1194,15 +1197,15 @@ public class Byte2DoubleRBTreeMap extends AbstractByte2DoubleMap implements Byte } state = operator.applyAsByte(state, entry.key); } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); for(Node entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.test(entry.key)) return entry.key; - return (byte)0; + if(filter.test(entry.key)) return OptionalByte.of(entry.key); + return OptionalByte.empty(); } @Override @@ -1846,7 +1849,7 @@ public class Byte2DoubleRBTreeMap extends AbstractByte2DoubleMap implements Byte } @Override - public Byte2DoubleMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Byte2DoubleMap.Entry state = null; boolean empty = true; @@ -1858,19 +1861,19 @@ public class Byte2DoubleRBTreeMap extends AbstractByte2DoubleMap implements Byte } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Byte2DoubleMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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 @@ -1965,7 +1968,7 @@ public class Byte2DoubleRBTreeMap extends AbstractByte2DoubleMap implements Byte } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -1977,15 +1980,15 @@ public class Byte2DoubleRBTreeMap extends AbstractByte2DoubleMap implements Byte } state = operator.applyAsDouble(state, entry.value); } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return 0D; + if(filter.test(entry.value)) return OptionalDouble.of(entry.value); + return OptionalDouble.empty(); } @Override @@ -2301,7 +2304,7 @@ public class Byte2DoubleRBTreeMap extends AbstractByte2DoubleMap implements Byte } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -2313,15 +2316,15 @@ public class Byte2DoubleRBTreeMap extends AbstractByte2DoubleMap implements Byte } state = operator.applyAsDouble(state, entry.value); } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return 0D; + if(filter.test(entry.value)) return OptionalDouble.of(entry.value); + return OptionalDouble.empty(); } @Override @@ -2462,7 +2465,7 @@ public class Byte2DoubleRBTreeMap extends AbstractByte2DoubleMap implements Byte } @Override - public Byte2DoubleMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Byte2DoubleMap.Entry state = null; boolean empty = true; @@ -2474,19 +2477,19 @@ public class Byte2DoubleRBTreeMap extends AbstractByte2DoubleMap implements Byte } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Byte2DoubleMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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/main/java/speiger/src/collections/bytes/maps/impl/tree/Byte2FloatAVLTreeMap.java b/src/main/java/speiger/src/collections/bytes/maps/impl/tree/Byte2FloatAVLTreeMap.java index 22d69d4..896804b 100644 --- a/src/main/java/speiger/src/collections/bytes/maps/impl/tree/Byte2FloatAVLTreeMap.java +++ b/src/main/java/speiger/src/collections/bytes/maps/impl/tree/Byte2FloatAVLTreeMap.java @@ -3,6 +3,7 @@ package speiger.src.collections.bytes.maps.impl.tree; import java.util.Collections; import java.util.Map; import java.util.Objects; +import java.util.Optional; import java.util.NoSuchElementException; import java.util.function.Consumer; import java.util.function.BiFunction; @@ -18,6 +19,7 @@ import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntFloatConsumer; import speiger.src.collections.bytes.functions.function.Byte2FloatFunction; import speiger.src.collections.bytes.functions.consumer.ByteFloatConsumer; +import speiger.src.collections.bytes.functions.OptionalByte; import speiger.src.collections.bytes.functions.function.BytePredicate; import speiger.src.collections.bytes.functions.function.ByteFloatUnaryOperator; import speiger.src.collections.bytes.functions.function.ByteByteUnaryOperator; @@ -37,6 +39,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectFloatConsumer; import speiger.src.collections.floats.functions.function.FloatPredicate; +import speiger.src.collections.floats.functions.OptionalFloat; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOperator; @@ -1129,7 +1132,7 @@ public class Byte2FloatAVLTreeMap extends AbstractByte2FloatMap implements Byte2 } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -1141,15 +1144,15 @@ public class Byte2FloatAVLTreeMap extends AbstractByte2FloatMap implements Byte2 } state = operator.applyAsByte(state, entry.key); } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); for(Node entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.test(entry.key)) return entry.key; - return (byte)0; + if(filter.test(entry.key)) return OptionalByte.of(entry.key); + return OptionalByte.empty(); } @Override @@ -1783,7 +1786,7 @@ public class Byte2FloatAVLTreeMap extends AbstractByte2FloatMap implements Byte2 } @Override - public Byte2FloatMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Byte2FloatMap.Entry state = null; boolean empty = true; @@ -1795,19 +1798,19 @@ public class Byte2FloatAVLTreeMap extends AbstractByte2FloatMap implements Byte2 } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Byte2FloatMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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 @@ -1902,7 +1905,7 @@ public class Byte2FloatAVLTreeMap extends AbstractByte2FloatMap implements Byte2 } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -1914,15 +1917,15 @@ public class Byte2FloatAVLTreeMap extends AbstractByte2FloatMap implements Byte2 } state = operator.applyAsFloat(state, entry.value); } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return 0F; + if(filter.test(entry.value)) return OptionalFloat.of(entry.value); + return OptionalFloat.empty(); } @Override @@ -2238,7 +2241,7 @@ public class Byte2FloatAVLTreeMap extends AbstractByte2FloatMap implements Byte2 } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -2250,15 +2253,15 @@ public class Byte2FloatAVLTreeMap extends AbstractByte2FloatMap implements Byte2 } state = operator.applyAsFloat(state, entry.value); } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return 0F; + if(filter.test(entry.value)) return OptionalFloat.of(entry.value); + return OptionalFloat.empty(); } @Override @@ -2399,7 +2402,7 @@ public class Byte2FloatAVLTreeMap extends AbstractByte2FloatMap implements Byte2 } @Override - public Byte2FloatMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Byte2FloatMap.Entry state = null; boolean empty = true; @@ -2411,19 +2414,19 @@ public class Byte2FloatAVLTreeMap extends AbstractByte2FloatMap implements Byte2 } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Byte2FloatMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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/main/java/speiger/src/collections/bytes/maps/impl/tree/Byte2FloatRBTreeMap.java b/src/main/java/speiger/src/collections/bytes/maps/impl/tree/Byte2FloatRBTreeMap.java index 54ba4ea..1e465e6 100644 --- a/src/main/java/speiger/src/collections/bytes/maps/impl/tree/Byte2FloatRBTreeMap.java +++ b/src/main/java/speiger/src/collections/bytes/maps/impl/tree/Byte2FloatRBTreeMap.java @@ -3,6 +3,7 @@ package speiger.src.collections.bytes.maps.impl.tree; import java.util.Collections; import java.util.Map; import java.util.Objects; +import java.util.Optional; import java.util.NoSuchElementException; import java.util.function.Consumer; import java.util.function.BiFunction; @@ -17,6 +18,7 @@ import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntFloatConsumer; import speiger.src.collections.bytes.functions.function.Byte2FloatFunction; import speiger.src.collections.bytes.functions.consumer.ByteFloatConsumer; +import speiger.src.collections.bytes.functions.OptionalByte; import speiger.src.collections.bytes.functions.function.BytePredicate; import speiger.src.collections.bytes.functions.function.ByteFloatUnaryOperator; import speiger.src.collections.bytes.functions.function.ByteByteUnaryOperator; @@ -36,6 +38,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectFloatConsumer; import speiger.src.collections.floats.functions.function.FloatPredicate; +import speiger.src.collections.floats.functions.OptionalFloat; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOperator; @@ -1182,7 +1185,7 @@ public class Byte2FloatRBTreeMap extends AbstractByte2FloatMap implements Byte2F } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -1194,15 +1197,15 @@ public class Byte2FloatRBTreeMap extends AbstractByte2FloatMap implements Byte2F } state = operator.applyAsByte(state, entry.key); } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); for(Node entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.test(entry.key)) return entry.key; - return (byte)0; + if(filter.test(entry.key)) return OptionalByte.of(entry.key); + return OptionalByte.empty(); } @Override @@ -1846,7 +1849,7 @@ public class Byte2FloatRBTreeMap extends AbstractByte2FloatMap implements Byte2F } @Override - public Byte2FloatMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Byte2FloatMap.Entry state = null; boolean empty = true; @@ -1858,19 +1861,19 @@ public class Byte2FloatRBTreeMap extends AbstractByte2FloatMap implements Byte2F } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Byte2FloatMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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 @@ -1965,7 +1968,7 @@ public class Byte2FloatRBTreeMap extends AbstractByte2FloatMap implements Byte2F } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -1977,15 +1980,15 @@ public class Byte2FloatRBTreeMap extends AbstractByte2FloatMap implements Byte2F } state = operator.applyAsFloat(state, entry.value); } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return 0F; + if(filter.test(entry.value)) return OptionalFloat.of(entry.value); + return OptionalFloat.empty(); } @Override @@ -2301,7 +2304,7 @@ public class Byte2FloatRBTreeMap extends AbstractByte2FloatMap implements Byte2F } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -2313,15 +2316,15 @@ public class Byte2FloatRBTreeMap extends AbstractByte2FloatMap implements Byte2F } state = operator.applyAsFloat(state, entry.value); } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return 0F; + if(filter.test(entry.value)) return OptionalFloat.of(entry.value); + return OptionalFloat.empty(); } @Override @@ -2462,7 +2465,7 @@ public class Byte2FloatRBTreeMap extends AbstractByte2FloatMap implements Byte2F } @Override - public Byte2FloatMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Byte2FloatMap.Entry state = null; boolean empty = true; @@ -2474,19 +2477,19 @@ public class Byte2FloatRBTreeMap extends AbstractByte2FloatMap implements Byte2F } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Byte2FloatMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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/main/java/speiger/src/collections/bytes/maps/impl/tree/Byte2IntAVLTreeMap.java b/src/main/java/speiger/src/collections/bytes/maps/impl/tree/Byte2IntAVLTreeMap.java index 533b766..9e9784d 100644 --- a/src/main/java/speiger/src/collections/bytes/maps/impl/tree/Byte2IntAVLTreeMap.java +++ b/src/main/java/speiger/src/collections/bytes/maps/impl/tree/Byte2IntAVLTreeMap.java @@ -3,11 +3,13 @@ package speiger.src.collections.bytes.maps.impl.tree; import java.util.Collections; import java.util.Map; 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; import java.util.function.IntPredicate; +import java.util.OptionalInt; import speiger.src.collections.bytes.collections.ByteBidirectionalIterator; @@ -19,6 +21,7 @@ import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntIntConsumer; import speiger.src.collections.bytes.functions.function.Byte2IntFunction; import speiger.src.collections.bytes.functions.consumer.ByteIntConsumer; +import speiger.src.collections.bytes.functions.OptionalByte; import speiger.src.collections.bytes.functions.function.BytePredicate; import speiger.src.collections.bytes.functions.function.ByteIntUnaryOperator; import speiger.src.collections.bytes.functions.function.ByteByteUnaryOperator; @@ -1129,7 +1132,7 @@ public class Byte2IntAVLTreeMap extends AbstractByte2IntMap implements Byte2IntN } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -1141,15 +1144,15 @@ public class Byte2IntAVLTreeMap extends AbstractByte2IntMap implements Byte2IntN } state = operator.applyAsByte(state, entry.key); } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); for(Node entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.test(entry.key)) return entry.key; - return (byte)0; + if(filter.test(entry.key)) return OptionalByte.of(entry.key); + return OptionalByte.empty(); } @Override @@ -1783,7 +1786,7 @@ public class Byte2IntAVLTreeMap extends AbstractByte2IntMap implements Byte2IntN } @Override - public Byte2IntMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Byte2IntMap.Entry state = null; boolean empty = true; @@ -1795,19 +1798,19 @@ public class Byte2IntAVLTreeMap extends AbstractByte2IntMap implements Byte2IntN } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Byte2IntMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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 @@ -1902,7 +1905,7 @@ public class Byte2IntAVLTreeMap extends AbstractByte2IntMap implements Byte2IntN } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -1914,15 +1917,15 @@ public class Byte2IntAVLTreeMap extends AbstractByte2IntMap implements Byte2IntN } state = operator.applyAsInt(state, entry.value); } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return 0; + if(filter.test(entry.value)) return OptionalInt.of(entry.value); + return OptionalInt.empty(); } @Override @@ -2238,7 +2241,7 @@ public class Byte2IntAVLTreeMap extends AbstractByte2IntMap implements Byte2IntN } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -2250,15 +2253,15 @@ public class Byte2IntAVLTreeMap extends AbstractByte2IntMap implements Byte2IntN } state = operator.applyAsInt(state, entry.value); } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return 0; + if(filter.test(entry.value)) return OptionalInt.of(entry.value); + return OptionalInt.empty(); } @Override @@ -2399,7 +2402,7 @@ public class Byte2IntAVLTreeMap extends AbstractByte2IntMap implements Byte2IntN } @Override - public Byte2IntMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Byte2IntMap.Entry state = null; boolean empty = true; @@ -2411,19 +2414,19 @@ public class Byte2IntAVLTreeMap extends AbstractByte2IntMap implements Byte2IntN } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Byte2IntMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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/main/java/speiger/src/collections/bytes/maps/impl/tree/Byte2IntRBTreeMap.java b/src/main/java/speiger/src/collections/bytes/maps/impl/tree/Byte2IntRBTreeMap.java index 5cfbeda..7d063d9 100644 --- a/src/main/java/speiger/src/collections/bytes/maps/impl/tree/Byte2IntRBTreeMap.java +++ b/src/main/java/speiger/src/collections/bytes/maps/impl/tree/Byte2IntRBTreeMap.java @@ -3,11 +3,13 @@ package speiger.src.collections.bytes.maps.impl.tree; import java.util.Collections; import java.util.Map; 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; import java.util.function.IntPredicate; +import java.util.OptionalInt; import speiger.src.collections.bytes.collections.ByteBidirectionalIterator; import speiger.src.collections.bytes.functions.ByteComparator; @@ -18,6 +20,7 @@ import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntIntConsumer; import speiger.src.collections.bytes.functions.function.Byte2IntFunction; import speiger.src.collections.bytes.functions.consumer.ByteIntConsumer; +import speiger.src.collections.bytes.functions.OptionalByte; import speiger.src.collections.bytes.functions.function.BytePredicate; import speiger.src.collections.bytes.functions.function.ByteIntUnaryOperator; import speiger.src.collections.bytes.functions.function.ByteByteUnaryOperator; @@ -1182,7 +1185,7 @@ public class Byte2IntRBTreeMap extends AbstractByte2IntMap implements Byte2IntNa } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -1194,15 +1197,15 @@ public class Byte2IntRBTreeMap extends AbstractByte2IntMap implements Byte2IntNa } state = operator.applyAsByte(state, entry.key); } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); for(Node entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.test(entry.key)) return entry.key; - return (byte)0; + if(filter.test(entry.key)) return OptionalByte.of(entry.key); + return OptionalByte.empty(); } @Override @@ -1846,7 +1849,7 @@ public class Byte2IntRBTreeMap extends AbstractByte2IntMap implements Byte2IntNa } @Override - public Byte2IntMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Byte2IntMap.Entry state = null; boolean empty = true; @@ -1858,19 +1861,19 @@ public class Byte2IntRBTreeMap extends AbstractByte2IntMap implements Byte2IntNa } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Byte2IntMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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 @@ -1965,7 +1968,7 @@ public class Byte2IntRBTreeMap extends AbstractByte2IntMap implements Byte2IntNa } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -1977,15 +1980,15 @@ public class Byte2IntRBTreeMap extends AbstractByte2IntMap implements Byte2IntNa } state = operator.applyAsInt(state, entry.value); } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return 0; + if(filter.test(entry.value)) return OptionalInt.of(entry.value); + return OptionalInt.empty(); } @Override @@ -2301,7 +2304,7 @@ public class Byte2IntRBTreeMap extends AbstractByte2IntMap implements Byte2IntNa } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -2313,15 +2316,15 @@ public class Byte2IntRBTreeMap extends AbstractByte2IntMap implements Byte2IntNa } state = operator.applyAsInt(state, entry.value); } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return 0; + if(filter.test(entry.value)) return OptionalInt.of(entry.value); + return OptionalInt.empty(); } @Override @@ -2462,7 +2465,7 @@ public class Byte2IntRBTreeMap extends AbstractByte2IntMap implements Byte2IntNa } @Override - public Byte2IntMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Byte2IntMap.Entry state = null; boolean empty = true; @@ -2474,19 +2477,19 @@ public class Byte2IntRBTreeMap extends AbstractByte2IntMap implements Byte2IntNa } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Byte2IntMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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/main/java/speiger/src/collections/bytes/maps/impl/tree/Byte2LongAVLTreeMap.java b/src/main/java/speiger/src/collections/bytes/maps/impl/tree/Byte2LongAVLTreeMap.java index ee198d5..01d5e4d 100644 --- a/src/main/java/speiger/src/collections/bytes/maps/impl/tree/Byte2LongAVLTreeMap.java +++ b/src/main/java/speiger/src/collections/bytes/maps/impl/tree/Byte2LongAVLTreeMap.java @@ -3,11 +3,13 @@ package speiger.src.collections.bytes.maps.impl.tree; import java.util.Collections; import java.util.Map; 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; import java.util.function.LongPredicate; +import java.util.OptionalLong; import speiger.src.collections.bytes.collections.ByteBidirectionalIterator; @@ -19,6 +21,7 @@ import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntLongConsumer; import speiger.src.collections.bytes.functions.function.Byte2LongFunction; import speiger.src.collections.bytes.functions.consumer.ByteLongConsumer; +import speiger.src.collections.bytes.functions.OptionalByte; import speiger.src.collections.bytes.functions.function.BytePredicate; import speiger.src.collections.bytes.functions.function.ByteLongUnaryOperator; import speiger.src.collections.bytes.functions.function.ByteByteUnaryOperator; @@ -1129,7 +1132,7 @@ public class Byte2LongAVLTreeMap extends AbstractByte2LongMap implements Byte2Lo } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -1141,15 +1144,15 @@ public class Byte2LongAVLTreeMap extends AbstractByte2LongMap implements Byte2Lo } state = operator.applyAsByte(state, entry.key); } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); for(Node entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.test(entry.key)) return entry.key; - return (byte)0; + if(filter.test(entry.key)) return OptionalByte.of(entry.key); + return OptionalByte.empty(); } @Override @@ -1783,7 +1786,7 @@ public class Byte2LongAVLTreeMap extends AbstractByte2LongMap implements Byte2Lo } @Override - public Byte2LongMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Byte2LongMap.Entry state = null; boolean empty = true; @@ -1795,19 +1798,19 @@ public class Byte2LongAVLTreeMap extends AbstractByte2LongMap implements Byte2Lo } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Byte2LongMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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 @@ -1902,7 +1905,7 @@ public class Byte2LongAVLTreeMap extends AbstractByte2LongMap implements Byte2Lo } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -1914,15 +1917,15 @@ public class Byte2LongAVLTreeMap extends AbstractByte2LongMap implements Byte2Lo } state = operator.applyAsLong(state, entry.value); } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return 0L; + if(filter.test(entry.value)) return OptionalLong.of(entry.value); + return OptionalLong.empty(); } @Override @@ -2238,7 +2241,7 @@ public class Byte2LongAVLTreeMap extends AbstractByte2LongMap implements Byte2Lo } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -2250,15 +2253,15 @@ public class Byte2LongAVLTreeMap extends AbstractByte2LongMap implements Byte2Lo } state = operator.applyAsLong(state, entry.value); } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return 0L; + if(filter.test(entry.value)) return OptionalLong.of(entry.value); + return OptionalLong.empty(); } @Override @@ -2399,7 +2402,7 @@ public class Byte2LongAVLTreeMap extends AbstractByte2LongMap implements Byte2Lo } @Override - public Byte2LongMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Byte2LongMap.Entry state = null; boolean empty = true; @@ -2411,19 +2414,19 @@ public class Byte2LongAVLTreeMap extends AbstractByte2LongMap implements Byte2Lo } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Byte2LongMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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/main/java/speiger/src/collections/bytes/maps/impl/tree/Byte2LongRBTreeMap.java b/src/main/java/speiger/src/collections/bytes/maps/impl/tree/Byte2LongRBTreeMap.java index 7355d9c..dbf6045 100644 --- a/src/main/java/speiger/src/collections/bytes/maps/impl/tree/Byte2LongRBTreeMap.java +++ b/src/main/java/speiger/src/collections/bytes/maps/impl/tree/Byte2LongRBTreeMap.java @@ -3,11 +3,13 @@ package speiger.src.collections.bytes.maps.impl.tree; import java.util.Collections; import java.util.Map; 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; import java.util.function.LongPredicate; +import java.util.OptionalLong; import speiger.src.collections.bytes.collections.ByteBidirectionalIterator; import speiger.src.collections.bytes.functions.ByteComparator; @@ -18,6 +20,7 @@ import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntLongConsumer; import speiger.src.collections.bytes.functions.function.Byte2LongFunction; import speiger.src.collections.bytes.functions.consumer.ByteLongConsumer; +import speiger.src.collections.bytes.functions.OptionalByte; import speiger.src.collections.bytes.functions.function.BytePredicate; import speiger.src.collections.bytes.functions.function.ByteLongUnaryOperator; import speiger.src.collections.bytes.functions.function.ByteByteUnaryOperator; @@ -1182,7 +1185,7 @@ public class Byte2LongRBTreeMap extends AbstractByte2LongMap implements Byte2Lon } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -1194,15 +1197,15 @@ public class Byte2LongRBTreeMap extends AbstractByte2LongMap implements Byte2Lon } state = operator.applyAsByte(state, entry.key); } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); for(Node entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.test(entry.key)) return entry.key; - return (byte)0; + if(filter.test(entry.key)) return OptionalByte.of(entry.key); + return OptionalByte.empty(); } @Override @@ -1846,7 +1849,7 @@ public class Byte2LongRBTreeMap extends AbstractByte2LongMap implements Byte2Lon } @Override - public Byte2LongMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Byte2LongMap.Entry state = null; boolean empty = true; @@ -1858,19 +1861,19 @@ public class Byte2LongRBTreeMap extends AbstractByte2LongMap implements Byte2Lon } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Byte2LongMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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 @@ -1965,7 +1968,7 @@ public class Byte2LongRBTreeMap extends AbstractByte2LongMap implements Byte2Lon } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -1977,15 +1980,15 @@ public class Byte2LongRBTreeMap extends AbstractByte2LongMap implements Byte2Lon } state = operator.applyAsLong(state, entry.value); } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return 0L; + if(filter.test(entry.value)) return OptionalLong.of(entry.value); + return OptionalLong.empty(); } @Override @@ -2301,7 +2304,7 @@ public class Byte2LongRBTreeMap extends AbstractByte2LongMap implements Byte2Lon } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -2313,15 +2316,15 @@ public class Byte2LongRBTreeMap extends AbstractByte2LongMap implements Byte2Lon } state = operator.applyAsLong(state, entry.value); } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return 0L; + if(filter.test(entry.value)) return OptionalLong.of(entry.value); + return OptionalLong.empty(); } @Override @@ -2462,7 +2465,7 @@ public class Byte2LongRBTreeMap extends AbstractByte2LongMap implements Byte2Lon } @Override - public Byte2LongMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Byte2LongMap.Entry state = null; boolean empty = true; @@ -2474,19 +2477,19 @@ public class Byte2LongRBTreeMap extends AbstractByte2LongMap implements Byte2Lon } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Byte2LongMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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/main/java/speiger/src/collections/bytes/maps/impl/tree/Byte2ObjectAVLTreeMap.java b/src/main/java/speiger/src/collections/bytes/maps/impl/tree/Byte2ObjectAVLTreeMap.java index 2cd9c39..ffea1f2 100644 --- a/src/main/java/speiger/src/collections/bytes/maps/impl/tree/Byte2ObjectAVLTreeMap.java +++ b/src/main/java/speiger/src/collections/bytes/maps/impl/tree/Byte2ObjectAVLTreeMap.java @@ -3,6 +3,7 @@ package speiger.src.collections.bytes.maps.impl.tree; import java.util.Collections; import java.util.Map; import java.util.Objects; +import java.util.Optional; import java.util.NoSuchElementException; import java.util.function.Consumer; import java.util.function.BiFunction; @@ -17,6 +18,7 @@ import speiger.src.collections.ints.functions.consumer.IntByteConsumer; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.bytes.functions.function.ByteFunction; import speiger.src.collections.bytes.functions.consumer.ByteObjectConsumer; +import speiger.src.collections.bytes.functions.OptionalByte; import speiger.src.collections.bytes.functions.function.BytePredicate; import speiger.src.collections.bytes.functions.function.ByteObjectUnaryOperator; import speiger.src.collections.bytes.functions.function.ByteByteUnaryOperator; @@ -1017,7 +1019,7 @@ public class Byte2ObjectAVLTreeMap extends AbstractByte2ObjectMap implemen } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -1029,15 +1031,15 @@ public class Byte2ObjectAVLTreeMap extends AbstractByte2ObjectMap implemen } state = operator.applyAsByte(state, entry.key); } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); for(Node entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.test(entry.key)) return entry.key; - return (byte)0; + if(filter.test(entry.key)) return OptionalByte.of(entry.key); + return OptionalByte.empty(); } @Override @@ -1664,7 +1666,7 @@ public class Byte2ObjectAVLTreeMap extends AbstractByte2ObjectMap implemen } @Override - public Byte2ObjectMap.Entry reduce(ObjectObjectUnaryOperator, Byte2ObjectMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Byte2ObjectMap.Entry> operator) { Objects.requireNonNull(operator); Byte2ObjectMap.Entry state = null; boolean empty = true; @@ -1676,19 +1678,19 @@ public class Byte2ObjectAVLTreeMap extends AbstractByte2ObjectMap implemen } state = operator.apply(state, new BasicEntry<>(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Byte2ObjectMap.Entry findFirst(Predicate> filter) { + public Optional> findFirst(Predicate> filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry<>(); for(Node 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 @@ -1783,7 +1785,7 @@ public class Byte2ObjectAVLTreeMap extends AbstractByte2ObjectMap implemen } @Override - public V reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); V state = null; boolean empty = true; @@ -1795,15 +1797,15 @@ public class Byte2ObjectAVLTreeMap extends AbstractByte2ObjectMap implemen } state = operator.apply(state, entry.value); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public V findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return null; + if(filter.test(entry.value)) return Optional.ofNullable(entry.value); + return Optional.empty(); } @Override @@ -2119,7 +2121,7 @@ public class Byte2ObjectAVLTreeMap extends AbstractByte2ObjectMap implemen } @Override - public V reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); V state = null; boolean empty = true; @@ -2131,15 +2133,15 @@ public class Byte2ObjectAVLTreeMap extends AbstractByte2ObjectMap implemen } state = operator.apply(state, entry.value); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public V findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return null; + if(filter.test(entry.value)) return Optional.ofNullable(entry.value); + return Optional.empty(); } @Override @@ -2280,7 +2282,7 @@ public class Byte2ObjectAVLTreeMap extends AbstractByte2ObjectMap implemen } @Override - public Byte2ObjectMap.Entry reduce(ObjectObjectUnaryOperator, Byte2ObjectMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Byte2ObjectMap.Entry> operator) { Objects.requireNonNull(operator); Byte2ObjectMap.Entry state = null; boolean empty = true; @@ -2292,19 +2294,19 @@ public class Byte2ObjectAVLTreeMap extends AbstractByte2ObjectMap implemen } state = operator.apply(state, new BasicEntry<>(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Byte2ObjectMap.Entry findFirst(Predicate> filter) { + public Optional> findFirst(Predicate> filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry<>(); for(Node 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/main/java/speiger/src/collections/bytes/maps/impl/tree/Byte2ObjectRBTreeMap.java b/src/main/java/speiger/src/collections/bytes/maps/impl/tree/Byte2ObjectRBTreeMap.java index 62844db..9f7db96 100644 --- a/src/main/java/speiger/src/collections/bytes/maps/impl/tree/Byte2ObjectRBTreeMap.java +++ b/src/main/java/speiger/src/collections/bytes/maps/impl/tree/Byte2ObjectRBTreeMap.java @@ -3,6 +3,7 @@ package speiger.src.collections.bytes.maps.impl.tree; import java.util.Collections; import java.util.Map; import java.util.Objects; +import java.util.Optional; import java.util.NoSuchElementException; import java.util.function.Consumer; import java.util.function.BiFunction; @@ -16,6 +17,7 @@ import speiger.src.collections.ints.functions.consumer.IntByteConsumer; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.bytes.functions.function.ByteFunction; import speiger.src.collections.bytes.functions.consumer.ByteObjectConsumer; +import speiger.src.collections.bytes.functions.OptionalByte; import speiger.src.collections.bytes.functions.function.BytePredicate; import speiger.src.collections.bytes.functions.function.ByteObjectUnaryOperator; import speiger.src.collections.bytes.functions.function.ByteByteUnaryOperator; @@ -1070,7 +1072,7 @@ public class Byte2ObjectRBTreeMap extends AbstractByte2ObjectMap implement } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -1082,15 +1084,15 @@ public class Byte2ObjectRBTreeMap extends AbstractByte2ObjectMap implement } state = operator.applyAsByte(state, entry.key); } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); for(Node entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.test(entry.key)) return entry.key; - return (byte)0; + if(filter.test(entry.key)) return OptionalByte.of(entry.key); + return OptionalByte.empty(); } @Override @@ -1727,7 +1729,7 @@ public class Byte2ObjectRBTreeMap extends AbstractByte2ObjectMap implement } @Override - public Byte2ObjectMap.Entry reduce(ObjectObjectUnaryOperator, Byte2ObjectMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Byte2ObjectMap.Entry> operator) { Objects.requireNonNull(operator); Byte2ObjectMap.Entry state = null; boolean empty = true; @@ -1739,19 +1741,19 @@ public class Byte2ObjectRBTreeMap extends AbstractByte2ObjectMap implement } state = operator.apply(state, new BasicEntry<>(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Byte2ObjectMap.Entry findFirst(Predicate> filter) { + public Optional> findFirst(Predicate> filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry<>(); for(Node 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 @@ -1846,7 +1848,7 @@ public class Byte2ObjectRBTreeMap extends AbstractByte2ObjectMap implement } @Override - public V reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); V state = null; boolean empty = true; @@ -1858,15 +1860,15 @@ public class Byte2ObjectRBTreeMap extends AbstractByte2ObjectMap implement } state = operator.apply(state, entry.value); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public V findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return null; + if(filter.test(entry.value)) return Optional.ofNullable(entry.value); + return Optional.empty(); } @Override @@ -2182,7 +2184,7 @@ public class Byte2ObjectRBTreeMap extends AbstractByte2ObjectMap implement } @Override - public V reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); V state = null; boolean empty = true; @@ -2194,15 +2196,15 @@ public class Byte2ObjectRBTreeMap extends AbstractByte2ObjectMap implement } state = operator.apply(state, entry.value); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public V findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return null; + if(filter.test(entry.value)) return Optional.ofNullable(entry.value); + return Optional.empty(); } @Override @@ -2343,7 +2345,7 @@ public class Byte2ObjectRBTreeMap extends AbstractByte2ObjectMap implement } @Override - public Byte2ObjectMap.Entry reduce(ObjectObjectUnaryOperator, Byte2ObjectMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Byte2ObjectMap.Entry> operator) { Objects.requireNonNull(operator); Byte2ObjectMap.Entry state = null; boolean empty = true; @@ -2355,19 +2357,19 @@ public class Byte2ObjectRBTreeMap extends AbstractByte2ObjectMap implement } state = operator.apply(state, new BasicEntry<>(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Byte2ObjectMap.Entry findFirst(Predicate> filter) { + public Optional> findFirst(Predicate> filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry<>(); for(Node 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/main/java/speiger/src/collections/bytes/maps/impl/tree/Byte2ShortAVLTreeMap.java b/src/main/java/speiger/src/collections/bytes/maps/impl/tree/Byte2ShortAVLTreeMap.java index 6d262be..dfc810b 100644 --- a/src/main/java/speiger/src/collections/bytes/maps/impl/tree/Byte2ShortAVLTreeMap.java +++ b/src/main/java/speiger/src/collections/bytes/maps/impl/tree/Byte2ShortAVLTreeMap.java @@ -3,6 +3,7 @@ package speiger.src.collections.bytes.maps.impl.tree; import java.util.Collections; import java.util.Map; import java.util.Objects; +import java.util.Optional; import java.util.NoSuchElementException; import java.util.function.Consumer; import java.util.function.BiFunction; @@ -18,6 +19,7 @@ import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntShortConsumer; import speiger.src.collections.bytes.functions.function.Byte2ShortFunction; import speiger.src.collections.bytes.functions.consumer.ByteShortConsumer; +import speiger.src.collections.bytes.functions.OptionalByte; import speiger.src.collections.bytes.functions.function.BytePredicate; import speiger.src.collections.bytes.functions.function.ByteShortUnaryOperator; import speiger.src.collections.bytes.functions.function.ByteByteUnaryOperator; @@ -37,6 +39,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectShortConsumer; import speiger.src.collections.shorts.functions.function.ShortPredicate; +import speiger.src.collections.shorts.functions.OptionalShort; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOperator; @@ -1129,7 +1132,7 @@ public class Byte2ShortAVLTreeMap extends AbstractByte2ShortMap implements Byte2 } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -1141,15 +1144,15 @@ public class Byte2ShortAVLTreeMap extends AbstractByte2ShortMap implements Byte2 } state = operator.applyAsByte(state, entry.key); } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); for(Node entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.test(entry.key)) return entry.key; - return (byte)0; + if(filter.test(entry.key)) return OptionalByte.of(entry.key); + return OptionalByte.empty(); } @Override @@ -1783,7 +1786,7 @@ public class Byte2ShortAVLTreeMap extends AbstractByte2ShortMap implements Byte2 } @Override - public Byte2ShortMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Byte2ShortMap.Entry state = null; boolean empty = true; @@ -1795,19 +1798,19 @@ public class Byte2ShortAVLTreeMap extends AbstractByte2ShortMap implements Byte2 } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Byte2ShortMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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 @@ -1902,7 +1905,7 @@ public class Byte2ShortAVLTreeMap extends AbstractByte2ShortMap implements Byte2 } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -1914,15 +1917,15 @@ public class Byte2ShortAVLTreeMap extends AbstractByte2ShortMap implements Byte2 } state = operator.applyAsShort(state, entry.value); } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return (short)0; + if(filter.test(entry.value)) return OptionalShort.of(entry.value); + return OptionalShort.empty(); } @Override @@ -2238,7 +2241,7 @@ public class Byte2ShortAVLTreeMap extends AbstractByte2ShortMap implements Byte2 } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -2250,15 +2253,15 @@ public class Byte2ShortAVLTreeMap extends AbstractByte2ShortMap implements Byte2 } state = operator.applyAsShort(state, entry.value); } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return (short)0; + if(filter.test(entry.value)) return OptionalShort.of(entry.value); + return OptionalShort.empty(); } @Override @@ -2399,7 +2402,7 @@ public class Byte2ShortAVLTreeMap extends AbstractByte2ShortMap implements Byte2 } @Override - public Byte2ShortMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Byte2ShortMap.Entry state = null; boolean empty = true; @@ -2411,19 +2414,19 @@ public class Byte2ShortAVLTreeMap extends AbstractByte2ShortMap implements Byte2 } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Byte2ShortMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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/main/java/speiger/src/collections/bytes/maps/impl/tree/Byte2ShortRBTreeMap.java b/src/main/java/speiger/src/collections/bytes/maps/impl/tree/Byte2ShortRBTreeMap.java index 0321589..3fdf1cf 100644 --- a/src/main/java/speiger/src/collections/bytes/maps/impl/tree/Byte2ShortRBTreeMap.java +++ b/src/main/java/speiger/src/collections/bytes/maps/impl/tree/Byte2ShortRBTreeMap.java @@ -3,6 +3,7 @@ package speiger.src.collections.bytes.maps.impl.tree; import java.util.Collections; import java.util.Map; import java.util.Objects; +import java.util.Optional; import java.util.NoSuchElementException; import java.util.function.Consumer; import java.util.function.BiFunction; @@ -17,6 +18,7 @@ import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntShortConsumer; import speiger.src.collections.bytes.functions.function.Byte2ShortFunction; import speiger.src.collections.bytes.functions.consumer.ByteShortConsumer; +import speiger.src.collections.bytes.functions.OptionalByte; import speiger.src.collections.bytes.functions.function.BytePredicate; import speiger.src.collections.bytes.functions.function.ByteShortUnaryOperator; import speiger.src.collections.bytes.functions.function.ByteByteUnaryOperator; @@ -36,6 +38,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectShortConsumer; import speiger.src.collections.shorts.functions.function.ShortPredicate; +import speiger.src.collections.shorts.functions.OptionalShort; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOperator; @@ -1182,7 +1185,7 @@ public class Byte2ShortRBTreeMap extends AbstractByte2ShortMap implements Byte2S } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -1194,15 +1197,15 @@ public class Byte2ShortRBTreeMap extends AbstractByte2ShortMap implements Byte2S } state = operator.applyAsByte(state, entry.key); } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); for(Node entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.test(entry.key)) return entry.key; - return (byte)0; + if(filter.test(entry.key)) return OptionalByte.of(entry.key); + return OptionalByte.empty(); } @Override @@ -1846,7 +1849,7 @@ public class Byte2ShortRBTreeMap extends AbstractByte2ShortMap implements Byte2S } @Override - public Byte2ShortMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Byte2ShortMap.Entry state = null; boolean empty = true; @@ -1858,19 +1861,19 @@ public class Byte2ShortRBTreeMap extends AbstractByte2ShortMap implements Byte2S } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Byte2ShortMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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 @@ -1965,7 +1968,7 @@ public class Byte2ShortRBTreeMap extends AbstractByte2ShortMap implements Byte2S } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -1977,15 +1980,15 @@ public class Byte2ShortRBTreeMap extends AbstractByte2ShortMap implements Byte2S } state = operator.applyAsShort(state, entry.value); } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return (short)0; + if(filter.test(entry.value)) return OptionalShort.of(entry.value); + return OptionalShort.empty(); } @Override @@ -2301,7 +2304,7 @@ public class Byte2ShortRBTreeMap extends AbstractByte2ShortMap implements Byte2S } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -2313,15 +2316,15 @@ public class Byte2ShortRBTreeMap extends AbstractByte2ShortMap implements Byte2S } state = operator.applyAsShort(state, entry.value); } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return (short)0; + if(filter.test(entry.value)) return OptionalShort.of(entry.value); + return OptionalShort.empty(); } @Override @@ -2462,7 +2465,7 @@ public class Byte2ShortRBTreeMap extends AbstractByte2ShortMap implements Byte2S } @Override - public Byte2ShortMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Byte2ShortMap.Entry state = null; boolean empty = true; @@ -2474,19 +2477,19 @@ public class Byte2ShortRBTreeMap extends AbstractByte2ShortMap implements Byte2S } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Byte2ShortMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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/main/java/speiger/src/collections/bytes/queues/ByteArrayFIFOQueue.java b/src/main/java/speiger/src/collections/bytes/queues/ByteArrayFIFOQueue.java index 41d89be..d136740 100644 --- a/src/main/java/speiger/src/collections/bytes/queues/ByteArrayFIFOQueue.java +++ b/src/main/java/speiger/src/collections/bytes/queues/ByteArrayFIFOQueue.java @@ -5,6 +5,7 @@ import java.util.Objects; import java.util.NoSuchElementException; import speiger.src.collections.bytes.collections.ByteIterator; +import speiger.src.collections.bytes.functions.OptionalByte; import speiger.src.collections.bytes.functions.ByteComparator; import speiger.src.collections.bytes.functions.ByteConsumer; import speiger.src.collections.ints.functions.consumer.IntByteConsumer; @@ -278,17 +279,17 @@ public class ByteArrayFIFOQueue extends AbstractBytePriorityQueue implements Byt } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); for(int i = 0,m=size();i= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor)); + } + + @Override + public void addLast(byte o) { + if(strategy.equals(o, (byte)0)) { + if(containsNull) return; + containsNull = true; + onNodeAdded(nullIndex); + } + else { + int pos = HashUtil.mix(strategy.hashCode(o)) & mask; + while(!strategy.equals(keys[pos], (byte)0)) { + if(strategy.equals(keys[pos], o)) return; + pos = ++pos & mask; + } + keys[pos] = o; + onNodeAdded(pos); + } + if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor)); + } + @Override public boolean addAndMoveToFirst(byte o) { if(strategy.equals(o, (byte)0)) { @@ -618,7 +659,7 @@ public class ByteLinkedOpenCustomHashSet extends ByteOpenCustomHashSet implement } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -631,18 +672,18 @@ public class ByteLinkedOpenCustomHashSet extends ByteOpenCustomHashSet implement else state = operator.applyAsByte(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); int index = firstIndex; while(index != -1) { - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalByte.of(keys[index]); index = (int)links[index]; } - return (byte)0; + return OptionalByte.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/bytes/sets/ByteLinkedOpenHashSet.java b/src/main/java/speiger/src/collections/bytes/sets/ByteLinkedOpenHashSet.java index f37a4f9..05fcdef 100644 --- a/src/main/java/speiger/src/collections/bytes/sets/ByteLinkedOpenHashSet.java +++ b/src/main/java/speiger/src/collections/bytes/sets/ByteLinkedOpenHashSet.java @@ -10,6 +10,7 @@ import speiger.src.collections.bytes.collections.ByteCollection; import speiger.src.collections.bytes.collections.ByteIterator; import speiger.src.collections.bytes.collections.ByteBidirectionalIterator; import speiger.src.collections.bytes.functions.ByteConsumer; +import speiger.src.collections.bytes.functions.OptionalByte; import speiger.src.collections.ints.functions.consumer.IntByteConsumer; import speiger.src.collections.objects.functions.consumer.ObjectByteConsumer; import speiger.src.collections.bytes.functions.function.BytePredicate; @@ -183,6 +184,46 @@ public class ByteLinkedOpenHashSet extends ByteOpenHashSet implements ByteOrdere while(iterator.hasNext()) add(iterator.nextByte()); } + @Override + public void addFirst(byte o) { + if(o == (byte)0) { + if(containsNull) return; + containsNull = true; + onNodeAdded(nullIndex); + moveToFirstIndex(nullIndex); + } + else { + int pos = HashUtil.mix(Byte.hashCode(o)) & mask; + while(keys[pos] != (byte)0) { + if(keys[pos] == o) return; + pos = ++pos & mask; + } + keys[pos] = o; + onNodeAdded(pos); + moveToFirstIndex(pos); + } + if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor)); + } + + @Override + public void addLast(byte o) { + if(o == (byte)0) { + if(containsNull) return; + containsNull = true; + onNodeAdded(nullIndex); + } + else { + int pos = HashUtil.mix(Byte.hashCode(o)) & mask; + while(keys[pos] != (byte)0) { + if(keys[pos] == o) return; + pos = ++pos & mask; + } + keys[pos] = o; + onNodeAdded(pos); + } + if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor)); + } + @Override public boolean addAndMoveToFirst(byte o) { if(o == (byte)0) { @@ -471,7 +512,7 @@ public class ByteLinkedOpenHashSet extends ByteOpenHashSet implements ByteOrdere } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -484,18 +525,18 @@ public class ByteLinkedOpenHashSet extends ByteOpenHashSet implements ByteOrdere else state = operator.applyAsByte(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); int index = firstIndex; while(index != -1) { - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalByte.of(keys[index]); index = (int)links[index]; } - return (byte)0; + return OptionalByte.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/bytes/sets/ByteOpenCustomHashSet.java b/src/main/java/speiger/src/collections/bytes/sets/ByteOpenCustomHashSet.java index fffcfeb..265d547 100644 --- a/src/main/java/speiger/src/collections/bytes/sets/ByteOpenCustomHashSet.java +++ b/src/main/java/speiger/src/collections/bytes/sets/ByteOpenCustomHashSet.java @@ -9,6 +9,7 @@ import java.util.Objects; import speiger.src.collections.bytes.collections.ByteCollection; import speiger.src.collections.bytes.collections.ByteIterator; +import speiger.src.collections.bytes.functions.OptionalByte; import speiger.src.collections.bytes.utils.ByteIterators; import speiger.src.collections.bytes.functions.ByteConsumer; import speiger.src.collections.ints.functions.consumer.IntByteConsumer; @@ -546,7 +547,7 @@ public class ByteOpenCustomHashSet extends AbstractByteSet implements ITrimmable } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -563,18 +564,18 @@ public class ByteOpenCustomHashSet extends AbstractByteSet implements ITrimmable } state = operator.applyAsByte(state, keys[i]); } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (byte)0; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OptionalByte.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OptionalByte.of(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], (byte)0) && filter.test(keys[i])) return keys[i]; + if(!strategy.equals(keys[i], (byte)0) && filter.test(keys[i])) return OptionalByte.of(keys[i]); } - return (byte)0; + return OptionalByte.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/bytes/sets/ByteOpenHashSet.java b/src/main/java/speiger/src/collections/bytes/sets/ByteOpenHashSet.java index 7ecf7b6..d5f3f6b 100644 --- a/src/main/java/speiger/src/collections/bytes/sets/ByteOpenHashSet.java +++ b/src/main/java/speiger/src/collections/bytes/sets/ByteOpenHashSet.java @@ -11,6 +11,7 @@ import speiger.src.collections.bytes.collections.ByteCollection; import speiger.src.collections.bytes.collections.ByteIterator; import speiger.src.collections.bytes.utils.ByteIterators; import speiger.src.collections.bytes.functions.ByteConsumer; +import speiger.src.collections.bytes.functions.OptionalByte; import speiger.src.collections.ints.functions.consumer.IntByteConsumer; import speiger.src.collections.objects.functions.consumer.ObjectByteConsumer; import speiger.src.collections.bytes.functions.function.BytePredicate; @@ -428,7 +429,7 @@ public class ByteOpenHashSet extends AbstractByteSet implements ITrimmable } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -445,18 +446,18 @@ public class ByteOpenHashSet extends AbstractByteSet implements ITrimmable } state = operator.applyAsByte(state, keys[i]); } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (byte)0; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OptionalByte.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OptionalByte.of(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(keys[i] != (byte)0 && filter.test(keys[i])) return keys[i]; + if(keys[i] != (byte)0 && filter.test(keys[i])) return OptionalByte.of(keys[i]); } - return (byte)0; + return OptionalByte.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/bytes/sets/ByteOrderedSet.java b/src/main/java/speiger/src/collections/bytes/sets/ByteOrderedSet.java index 8e04175..fadc646 100644 --- a/src/main/java/speiger/src/collections/bytes/sets/ByteOrderedSet.java +++ b/src/main/java/speiger/src/collections/bytes/sets/ByteOrderedSet.java @@ -1,6 +1,7 @@ package speiger.src.collections.bytes.sets; import speiger.src.collections.bytes.collections.ByteBidirectionalIterator; +import speiger.src.collections.bytes.collections.ByteOrderedCollection; import speiger.src.collections.bytes.collections.ByteSplititerator; import speiger.src.collections.bytes.utils.ByteSplititerators; import speiger.src.collections.bytes.sets.AbstractByteSet.ReversedByteOrderedSet; @@ -13,7 +14,7 @@ import speiger.src.collections.bytes.utils.ByteSets; * This interface provides basic access to such functions while also providing some Sorted/NaivgableSet implementations that still fit into here. * */ -public interface ByteOrderedSet extends ByteSet +public interface ByteOrderedSet extends ByteSet, ByteOrderedCollection { /** * A customized add method that allows you to insert into the first index. diff --git a/src/main/java/speiger/src/collections/bytes/sets/ByteRBTreeSet.java b/src/main/java/speiger/src/collections/bytes/sets/ByteRBTreeSet.java index 8de9f19..b94647e 100644 --- a/src/main/java/speiger/src/collections/bytes/sets/ByteRBTreeSet.java +++ b/src/main/java/speiger/src/collections/bytes/sets/ByteRBTreeSet.java @@ -9,6 +9,7 @@ import java.util.Objects; import speiger.src.collections.bytes.collections.ByteBidirectionalIterator; import speiger.src.collections.bytes.functions.ByteComparator; import speiger.src.collections.bytes.functions.ByteConsumer; +import speiger.src.collections.bytes.functions.OptionalByte; import speiger.src.collections.ints.functions.consumer.IntByteConsumer; import speiger.src.collections.objects.functions.consumer.ObjectByteConsumer; import speiger.src.collections.bytes.functions.function.BytePredicate; @@ -334,7 +335,7 @@ public class ByteRBTreeSet extends AbstractByteSet implements ByteNavigableSet } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -346,16 +347,16 @@ public class ByteRBTreeSet extends AbstractByteSet implements ByteNavigableSet } state = operator.applyAsByte(state, entry.key); } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); for(Entry entry = first;entry != null;entry = entry.next()) { - if(filter.test(entry.key)) return entry.key; + if(filter.test(entry.key)) return OptionalByte.of(entry.key); } - return (byte)0; + return OptionalByte.empty(); } @Override @@ -1241,7 +1242,7 @@ public class ByteRBTreeSet extends AbstractByteSet implements ByteNavigableSet } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -1253,16 +1254,16 @@ public class ByteRBTreeSet extends AbstractByteSet implements ByteNavigableSet } state = operator.applyAsByte(state, entry.key); } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); for(Entry entry = start();entry != null && inRange(entry.key);entry = next(entry)) { - if(filter.test(entry.key)) return entry.key; + if(filter.test(entry.key)) return OptionalByte.of(entry.key); } - return (byte)0; + return OptionalByte.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/bytes/sets/ImmutableByteOpenHashSet.java b/src/main/java/speiger/src/collections/bytes/sets/ImmutableByteOpenHashSet.java index d503ef8..23f26d1 100644 --- a/src/main/java/speiger/src/collections/bytes/sets/ImmutableByteOpenHashSet.java +++ b/src/main/java/speiger/src/collections/bytes/sets/ImmutableByteOpenHashSet.java @@ -10,6 +10,7 @@ import speiger.src.collections.bytes.collections.ByteBidirectionalIterator; import speiger.src.collections.bytes.collections.ByteCollection; import speiger.src.collections.bytes.collections.ByteIterator; import speiger.src.collections.bytes.functions.ByteConsumer; +import speiger.src.collections.bytes.functions.OptionalByte; import speiger.src.collections.ints.functions.consumer.IntByteConsumer; import speiger.src.collections.objects.functions.consumer.ObjectByteConsumer; import speiger.src.collections.bytes.functions.function.BytePredicate; @@ -236,6 +237,10 @@ public class ImmutableByteOpenHashSet extends AbstractByteSet implements ByteOrd @Override public boolean addAll(ByteCollection c) { throw new UnsupportedOperationException(); } @Override + public void addFirst(byte o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(byte o) { throw new UnsupportedOperationException(); } + @Override public boolean addAndMoveToFirst(byte o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToLast(byte o) { throw new UnsupportedOperationException(); } @@ -371,7 +376,7 @@ public class ImmutableByteOpenHashSet extends AbstractByteSet implements ByteOrd } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -384,18 +389,18 @@ public class ImmutableByteOpenHashSet extends AbstractByteSet implements ByteOrd else state = operator.applyAsByte(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); int index = firstIndex; while(index != -1) { - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalByte.of(keys[index]); index = (int)links[index]; } - return (byte)0; + return OptionalByte.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/bytes/utils/ByteCollections.java b/src/main/java/speiger/src/collections/bytes/utils/ByteCollections.java index a2b327a..a8e5e5c 100644 --- a/src/main/java/speiger/src/collections/bytes/utils/ByteCollections.java +++ b/src/main/java/speiger/src/collections/bytes/utils/ByteCollections.java @@ -15,6 +15,7 @@ import speiger.src.collections.bytes.collections.ByteCollection; import speiger.src.collections.bytes.collections.ByteOrderedCollection; import speiger.src.collections.bytes.collections.ByteIterator; import speiger.src.collections.bytes.functions.ByteComparator; +import speiger.src.collections.bytes.functions.OptionalByte; import speiger.src.collections.objects.utils.ObjectArrays; import speiger.src.collections.bytes.functions.ByteConsumer; import speiger.src.collections.bytes.functions.function.BytePredicate; @@ -758,9 +759,9 @@ public class ByteCollections @Override public byte reduce(byte identity, ByteByteUnaryOperator operator) { synchronized(mutex) { return c.reduce(identity, operator); } } @Override - public byte reduce(ByteByteUnaryOperator operator) { synchronized(mutex) { return c.reduce(operator); } } + public OptionalByte reduce(ByteByteUnaryOperator operator) { synchronized(mutex) { return c.reduce(operator); } } @Override - public byte findFirst(BytePredicate filter) { synchronized(mutex) { return c.findFirst(filter); } } + public OptionalByte findFirst(BytePredicate filter) { synchronized(mutex) { return c.findFirst(filter); } } @Override public int count(BytePredicate filter) { synchronized(mutex) { return c.count(filter); } } } @@ -888,9 +889,9 @@ public class ByteCollections @Override public byte reduce(byte identity, ByteByteUnaryOperator operator) { return c.reduce(identity, operator); } @Override - public byte reduce(ByteByteUnaryOperator operator) { return c.reduce(operator); } + public OptionalByte reduce(ByteByteUnaryOperator operator) { return c.reduce(operator); } @Override - public byte findFirst(BytePredicate filter) { return c.findFirst(filter); } + public OptionalByte findFirst(BytePredicate filter) { return c.findFirst(filter); } @Override public int count(BytePredicate filter) { return c.count(filter); } } diff --git a/src/main/java/speiger/src/collections/bytes/utils/BytePriorityQueues.java b/src/main/java/speiger/src/collections/bytes/utils/BytePriorityQueues.java index 67d9603..6cd7e9b 100644 --- a/src/main/java/speiger/src/collections/bytes/utils/BytePriorityQueues.java +++ b/src/main/java/speiger/src/collections/bytes/utils/BytePriorityQueues.java @@ -1,13 +1,14 @@ package speiger.src.collections.bytes.utils; - import speiger.src.collections.bytes.collections.ByteIterator; import speiger.src.collections.bytes.collections.ByteCollection; import speiger.src.collections.bytes.functions.ByteComparator; +import speiger.src.collections.bytes.functions.OptionalByte; import speiger.src.collections.bytes.queues.BytePriorityDequeue; import speiger.src.collections.bytes.queues.BytePriorityQueue; import speiger.src.collections.bytes.functions.ByteConsumer; import speiger.src.collections.bytes.functions.function.BytePredicate; +import speiger.src.collections.bytes.functions.function.ByteByteUnaryOperator; import speiger.src.collections.objects.functions.consumer.ObjectByteConsumer; /** @@ -113,7 +114,11 @@ public class BytePriorityQueues @Override public boolean matchesAll(BytePredicate filter) { synchronized(mutex) { return queue.matchesAll(filter); } } @Override - public byte findFirst(BytePredicate filter) { synchronized(mutex) { return queue.findFirst(filter); } } + public OptionalByte findFirst(BytePredicate filter) { synchronized(mutex) { return queue.findFirst(filter); } } + @Override + public byte reduce(byte identity, ByteByteUnaryOperator operator) { synchronized(mutex) { return queue.reduce(identity, operator); } } + @Override + public OptionalByte reduce(ByteByteUnaryOperator operator) { synchronized(mutex) { return queue.reduce(operator); } } @Override public int count(BytePredicate filter) { synchronized(mutex) { return queue.count(filter); } } } diff --git a/src/main/java/speiger/src/collections/bytes/utils/ByteSets.java b/src/main/java/speiger/src/collections/bytes/utils/ByteSets.java index 53d3055..66a8a36 100644 --- a/src/main/java/speiger/src/collections/bytes/utils/ByteSets.java +++ b/src/main/java/speiger/src/collections/bytes/utils/ByteSets.java @@ -301,6 +301,10 @@ public class ByteSets s = c; } + @Override + public void addFirst(byte o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(byte o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(byte o) { throw new UnsupportedOperationException(); } @Override @@ -573,7 +577,11 @@ public class ByteSets super(c, mutex); s = c; } - + + @Override + public void addFirst(byte o) { synchronized(mutex) { s.addFirst(o); } } + @Override + public void addLast(byte o) { synchronized(mutex) { s.addLast(o); } } @Override public boolean addAndMoveToFirst(byte o) { synchronized(mutex) { return s.addAndMoveToFirst(o); } } @Override diff --git a/src/main/java/speiger/src/collections/bytes/utils/maps/Byte2BooleanMaps.java b/src/main/java/speiger/src/collections/bytes/utils/maps/Byte2BooleanMaps.java index 16ecbf4..2be90d1 100644 --- a/src/main/java/speiger/src/collections/bytes/utils/maps/Byte2BooleanMaps.java +++ b/src/main/java/speiger/src/collections/bytes/utils/maps/Byte2BooleanMaps.java @@ -608,7 +608,11 @@ public class Byte2BooleanMaps super(c); set = c; } - + + @Override + public void addFirst(Byte2BooleanMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Byte2BooleanMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Byte2BooleanMap.Entry o) { throw new UnsupportedOperationException(); } @Override diff --git a/src/main/java/speiger/src/collections/bytes/utils/maps/Byte2ByteMaps.java b/src/main/java/speiger/src/collections/bytes/utils/maps/Byte2ByteMaps.java index 06e0f8d..e21ed76 100644 --- a/src/main/java/speiger/src/collections/bytes/utils/maps/Byte2ByteMaps.java +++ b/src/main/java/speiger/src/collections/bytes/utils/maps/Byte2ByteMaps.java @@ -619,7 +619,11 @@ public class Byte2ByteMaps super(c); set = c; } - + + @Override + public void addFirst(Byte2ByteMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Byte2ByteMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Byte2ByteMap.Entry o) { throw new UnsupportedOperationException(); } @Override diff --git a/src/main/java/speiger/src/collections/bytes/utils/maps/Byte2CharMaps.java b/src/main/java/speiger/src/collections/bytes/utils/maps/Byte2CharMaps.java index 8f3817d..2e2f6e7 100644 --- a/src/main/java/speiger/src/collections/bytes/utils/maps/Byte2CharMaps.java +++ b/src/main/java/speiger/src/collections/bytes/utils/maps/Byte2CharMaps.java @@ -620,7 +620,11 @@ public class Byte2CharMaps super(c); set = c; } - + + @Override + public void addFirst(Byte2CharMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Byte2CharMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Byte2CharMap.Entry o) { throw new UnsupportedOperationException(); } @Override diff --git a/src/main/java/speiger/src/collections/bytes/utils/maps/Byte2DoubleMaps.java b/src/main/java/speiger/src/collections/bytes/utils/maps/Byte2DoubleMaps.java index f8bdbc5..04b2127 100644 --- a/src/main/java/speiger/src/collections/bytes/utils/maps/Byte2DoubleMaps.java +++ b/src/main/java/speiger/src/collections/bytes/utils/maps/Byte2DoubleMaps.java @@ -620,7 +620,11 @@ public class Byte2DoubleMaps super(c); set = c; } - + + @Override + public void addFirst(Byte2DoubleMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Byte2DoubleMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Byte2DoubleMap.Entry o) { throw new UnsupportedOperationException(); } @Override diff --git a/src/main/java/speiger/src/collections/bytes/utils/maps/Byte2FloatMaps.java b/src/main/java/speiger/src/collections/bytes/utils/maps/Byte2FloatMaps.java index aab5bd7..5d5f50a 100644 --- a/src/main/java/speiger/src/collections/bytes/utils/maps/Byte2FloatMaps.java +++ b/src/main/java/speiger/src/collections/bytes/utils/maps/Byte2FloatMaps.java @@ -620,7 +620,11 @@ public class Byte2FloatMaps super(c); set = c; } - + + @Override + public void addFirst(Byte2FloatMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Byte2FloatMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Byte2FloatMap.Entry o) { throw new UnsupportedOperationException(); } @Override diff --git a/src/main/java/speiger/src/collections/bytes/utils/maps/Byte2IntMaps.java b/src/main/java/speiger/src/collections/bytes/utils/maps/Byte2IntMaps.java index 2a6c866..9abff6d 100644 --- a/src/main/java/speiger/src/collections/bytes/utils/maps/Byte2IntMaps.java +++ b/src/main/java/speiger/src/collections/bytes/utils/maps/Byte2IntMaps.java @@ -620,7 +620,11 @@ public class Byte2IntMaps super(c); set = c; } - + + @Override + public void addFirst(Byte2IntMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Byte2IntMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Byte2IntMap.Entry o) { throw new UnsupportedOperationException(); } @Override diff --git a/src/main/java/speiger/src/collections/bytes/utils/maps/Byte2LongMaps.java b/src/main/java/speiger/src/collections/bytes/utils/maps/Byte2LongMaps.java index 8af841c..2f2216c 100644 --- a/src/main/java/speiger/src/collections/bytes/utils/maps/Byte2LongMaps.java +++ b/src/main/java/speiger/src/collections/bytes/utils/maps/Byte2LongMaps.java @@ -620,7 +620,11 @@ public class Byte2LongMaps super(c); set = c; } - + + @Override + public void addFirst(Byte2LongMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Byte2LongMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Byte2LongMap.Entry o) { throw new UnsupportedOperationException(); } @Override diff --git a/src/main/java/speiger/src/collections/bytes/utils/maps/Byte2ObjectMaps.java b/src/main/java/speiger/src/collections/bytes/utils/maps/Byte2ObjectMaps.java index d8e5c22..c2a3e93 100644 --- a/src/main/java/speiger/src/collections/bytes/utils/maps/Byte2ObjectMaps.java +++ b/src/main/java/speiger/src/collections/bytes/utils/maps/Byte2ObjectMaps.java @@ -611,7 +611,11 @@ public class Byte2ObjectMaps super(c); set = c; } - + + @Override + public void addFirst(Byte2ObjectMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Byte2ObjectMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Byte2ObjectMap.Entry o) { throw new UnsupportedOperationException(); } @Override diff --git a/src/main/java/speiger/src/collections/bytes/utils/maps/Byte2ShortMaps.java b/src/main/java/speiger/src/collections/bytes/utils/maps/Byte2ShortMaps.java index c9ef49a..0022355 100644 --- a/src/main/java/speiger/src/collections/bytes/utils/maps/Byte2ShortMaps.java +++ b/src/main/java/speiger/src/collections/bytes/utils/maps/Byte2ShortMaps.java @@ -620,7 +620,11 @@ public class Byte2ShortMaps super(c); set = c; } - + + @Override + public void addFirst(Byte2ShortMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Byte2ShortMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Byte2ShortMap.Entry o) { throw new UnsupportedOperationException(); } @Override diff --git a/src/main/java/speiger/src/collections/chars/collections/CharIterable.java b/src/main/java/speiger/src/collections/chars/collections/CharIterable.java index c0e6a53..31dd470 100644 --- a/src/main/java/speiger/src/collections/chars/collections/CharIterable.java +++ b/src/main/java/speiger/src/collections/chars/collections/CharIterable.java @@ -7,6 +7,7 @@ import java.util.function.Consumer; import speiger.src.collections.chars.functions.CharConsumer; import speiger.src.collections.chars.functions.CharComparator; import speiger.src.collections.objects.collections.ObjectIterable; +import speiger.src.collections.chars.functions.OptionalChar; import speiger.src.collections.chars.functions.function.CharFunction; import speiger.src.collections.ints.functions.consumer.IntCharConsumer; import speiger.src.collections.objects.functions.consumer.ObjectCharConsumer; @@ -281,13 +282,13 @@ public interface CharIterable extends Iterable * @param filter that should be applied * @return the found value or the null equivalent variant. */ - default char findFirst(CharPredicate filter) { + default OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); for(CharIterator iter = iterator();iter.hasNext();) { char entry = iter.nextChar(); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return OptionalChar.of(entry); } - return (char)0; + return OptionalChar.empty(); } /** @@ -312,7 +313,7 @@ public interface CharIterable extends Iterable * @param operator the operation that should be applied * @return the reduction result, returns null value if nothing was found */ - default char reduce(CharCharUnaryOperator operator) { + default OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -324,7 +325,7 @@ public interface CharIterable extends Iterable } state = operator.applyAsChar(state, iter.nextChar()); } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } /** diff --git a/src/main/java/speiger/src/collections/chars/lists/CharArrayList.java b/src/main/java/speiger/src/collections/chars/lists/CharArrayList.java index 8f72b07..7c10f51 100644 --- a/src/main/java/speiger/src/collections/chars/lists/CharArrayList.java +++ b/src/main/java/speiger/src/collections/chars/lists/CharArrayList.java @@ -12,6 +12,7 @@ import java.nio.CharBuffer; import speiger.src.collections.chars.collections.CharCollection; import speiger.src.collections.chars.collections.CharStack; import speiger.src.collections.chars.collections.CharIterator; +import speiger.src.collections.chars.functions.OptionalChar; import speiger.src.collections.chars.functions.CharComparator; import speiger.src.collections.chars.functions.CharConsumer; import speiger.src.collections.objects.functions.consumer.ObjectCharConsumer; @@ -519,12 +520,12 @@ public class CharArrayList extends AbstractCharList implements ICharArray, CharS } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); for(int i = 0;i operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Char2BooleanMap.Entry state = null; boolean empty = true; @@ -779,11 +782,11 @@ public class Char2BooleanConcurrentOpenHashMap extends AbstractChar2BooleanMap i seg.unlockRead(stamp); } } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Char2BooleanMap.Entry 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); Char2ByteMap.Entry state = null; boolean empty = true; @@ -792,11 +795,11 @@ public class Char2ByteConcurrentOpenHashMap extends AbstractChar2ByteMap impleme seg.unlockRead(stamp); } } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Char2ByteMap.Entry 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); Char2CharMap.Entry state = null; boolean empty = true; @@ -785,11 +787,11 @@ public class Char2CharConcurrentOpenHashMap extends AbstractChar2CharMap impleme seg.unlockRead(stamp); } } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Char2CharMap.Entry 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); Char2DoubleMap.Entry state = null; boolean empty = true; @@ -792,11 +795,11 @@ public class Char2DoubleConcurrentOpenHashMap extends AbstractChar2DoubleMap imp seg.unlockRead(stamp); } } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Char2DoubleMap.Entry 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); Char2FloatMap.Entry state = null; boolean empty = true; @@ -792,11 +795,11 @@ public class Char2FloatConcurrentOpenHashMap extends AbstractChar2FloatMap imple seg.unlockRead(stamp); } } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Char2FloatMap.Entry 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); Char2IntMap.Entry state = null; boolean empty = true; @@ -792,11 +795,11 @@ public class Char2IntConcurrentOpenHashMap extends AbstractChar2IntMap implement seg.unlockRead(stamp); } } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Char2IntMap.Entry 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); Char2LongMap.Entry state = null; boolean empty = true; @@ -792,11 +795,11 @@ public class Char2LongConcurrentOpenHashMap extends AbstractChar2LongMap impleme seg.unlockRead(stamp); } } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Char2LongMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); MapEntry entry = new MapEntry(); for(int i = 0,m=segments.length;i extends AbstractChar2ObjectMap< } @Override - public Char2ObjectMap.Entry reduce(ObjectObjectUnaryOperator, Char2ObjectMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Char2ObjectMap.Entry> operator) { Objects.requireNonNull(operator); Char2ObjectMap.Entry state = null; boolean empty = true; @@ -739,11 +741,11 @@ public class Char2ObjectConcurrentOpenHashMap extends AbstractChar2ObjectMap< seg.unlockRead(stamp); } } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Char2ObjectMap.Entry findFirst(Predicate> filter) { + public Optional> findFirst(Predicate> filter) { Objects.requireNonNull(filter); MapEntry entry = new MapEntry(); for(int i = 0,m=segments.length;i extends AbstractChar2ObjectMap< try { while(index != -1) { entry.set(index, i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); index = (int)seg.links[index]; } } @@ -761,7 +763,7 @@ public class Char2ObjectConcurrentOpenHashMap extends AbstractChar2ObjectMap< seg.unlockRead(stamp); } } - return null; + return Optional.empty(); } @Override @@ -1022,7 +1024,7 @@ public class Char2ObjectConcurrentOpenHashMap extends AbstractChar2ObjectMap< } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -1046,11 +1048,11 @@ public class Char2ObjectConcurrentOpenHashMap extends AbstractChar2ObjectMap< seg.unlockRead(stamp); } } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); for(int i = 0,m=segments.length;i seg = segments[i]; @@ -1058,7 +1060,7 @@ public class Char2ObjectConcurrentOpenHashMap extends AbstractChar2ObjectMap< try { int index = seg.firstIndex; while(index != -1){ - if(filter.test(seg.keys[index])) return seg.keys[index]; + if(filter.test(seg.keys[index])) return OptionalChar.of(seg.keys[index]); index = (int)seg.links[index]; } } @@ -1066,7 +1068,7 @@ public class Char2ObjectConcurrentOpenHashMap extends AbstractChar2ObjectMap< seg.unlockRead(stamp); } } - return (char)0; + return OptionalChar.empty(); } @Override @@ -1258,7 +1260,7 @@ public class Char2ObjectConcurrentOpenHashMap extends AbstractChar2ObjectMap< } @Override - public V reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); V state = null; boolean empty = true; @@ -1282,20 +1284,20 @@ public class Char2ObjectConcurrentOpenHashMap extends AbstractChar2ObjectMap< seg.unlockRead(stamp); } } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public V findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); for(int i = 0,m=segments.length;i seg = segments[i]; long stamp = seg.readLock(); try { int index = seg.firstIndex; while(index != -1){ - if(filter.test(seg.values[index])) return seg.values[index]; + if(filter.test(seg.values[index])) return Optional.ofNullable(seg.values[index]); index = (int)seg.links[index]; } } @@ -1303,7 +1305,7 @@ public class Char2ObjectConcurrentOpenHashMap extends AbstractChar2ObjectMap< seg.unlockRead(stamp); } } - return null; + return Optional.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/chars/maps/impl/concurrent/Char2ShortConcurrentOpenHashMap.java b/src/main/java/speiger/src/collections/chars/maps/impl/concurrent/Char2ShortConcurrentOpenHashMap.java index 0742c01..5c9985f 100644 --- a/src/main/java/speiger/src/collections/chars/maps/impl/concurrent/Char2ShortConcurrentOpenHashMap.java +++ b/src/main/java/speiger/src/collections/chars/maps/impl/concurrent/Char2ShortConcurrentOpenHashMap.java @@ -4,6 +4,7 @@ import java.util.Arrays; import java.util.Map; import java.util.NoSuchElementException; import java.util.Objects; +import java.util.Optional; import java.util.concurrent.locks.StampedLock; import java.util.function.Consumer; import java.util.function.Predicate; @@ -19,6 +20,7 @@ import speiger.src.collections.chars.functions.consumer.CharShortConsumer; import speiger.src.collections.chars.functions.function.Char2ShortFunction; import speiger.src.collections.chars.functions.function.CharShortUnaryOperator; import speiger.src.collections.chars.functions.function.CharCharUnaryOperator; +import speiger.src.collections.chars.functions.OptionalChar; import speiger.src.collections.chars.functions.function.CharPredicate; import speiger.src.collections.chars.maps.abstracts.AbstractChar2ShortMap; import speiger.src.collections.chars.maps.interfaces.Char2ShortMap; @@ -37,6 +39,7 @@ import speiger.src.collections.shorts.functions.ShortConsumer; import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectShortConsumer; import speiger.src.collections.shorts.functions.function.ShortPredicate; +import speiger.src.collections.shorts.functions.OptionalShort; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.objects.sets.AbstractObjectSet; import speiger.src.collections.objects.sets.ObjectSet; @@ -768,7 +771,7 @@ public class Char2ShortConcurrentOpenHashMap extends AbstractChar2ShortMap imple } @Override - public Char2ShortMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Char2ShortMap.Entry state = null; boolean empty = true; @@ -792,11 +795,11 @@ public class Char2ShortConcurrentOpenHashMap extends AbstractChar2ShortMap imple seg.unlockRead(stamp); } } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Char2ShortMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); MapEntry entry = new MapEntry(); for(int i = 0,m=segments.length;i implements Char2BooleanOrderedMap.FastOrderedSet { + @Override + public void addFirst(Char2BooleanMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Char2BooleanMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Char2BooleanMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -836,7 +843,7 @@ public class Char2BooleanLinkedOpenCustomHashMap extends Char2BooleanOpenCustomH } @Override - public Char2BooleanMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Char2BooleanMap.Entry state = null; boolean empty = true; @@ -851,21 +858,21 @@ public class Char2BooleanLinkedOpenCustomHashMap extends Char2BooleanOpenCustomH state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Char2BooleanMap.Entry 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 @@ -944,10 +951,13 @@ public class Char2BooleanLinkedOpenCustomHashMap extends Char2BooleanOpenCustomH public boolean add(char o) { throw new UnsupportedOperationException(); } - + + @Override + public void addFirst(char o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(char o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(char o) { throw new UnsupportedOperationException(); } - @Override public boolean addAndMoveToLast(char o) { throw new UnsupportedOperationException(); } @@ -1090,7 +1100,7 @@ public class Char2BooleanLinkedOpenCustomHashMap extends Char2BooleanOpenCustomH } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -1105,19 +1115,19 @@ public class Char2BooleanLinkedOpenCustomHashMap extends Char2BooleanOpenCustomH state = operator.applyAsChar(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (char)0; + if(size() <= 0) return OptionalChar.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalChar.of(keys[index]); index = (int)links[index]; } - return (char)0; + return OptionalChar.empty(); } @Override @@ -1252,7 +1262,7 @@ public class Char2BooleanLinkedOpenCustomHashMap extends Char2BooleanOpenCustomH } @Override - public boolean reduce(BooleanBooleanUnaryOperator operator) { + public OptionalBoolean reduce(BooleanBooleanUnaryOperator operator) { Objects.requireNonNull(operator); boolean state = false; boolean empty = true; @@ -1267,19 +1277,19 @@ public class Char2BooleanLinkedOpenCustomHashMap extends Char2BooleanOpenCustomH state = operator.applyAsBoolean(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalBoolean.empty() : OptionalBoolean.of(state); } @Override - public boolean findFirst(BooleanPredicate filter) { + public OptionalBoolean findFirst(BooleanPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return false; + if(size() <= 0) return OptionalBoolean.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalBoolean.of(values[index]); index = (int)links[index]; } - return false; + return OptionalBoolean.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/chars/maps/impl/customHash/Char2BooleanOpenCustomHashMap.java b/src/main/java/speiger/src/collections/chars/maps/impl/customHash/Char2BooleanOpenCustomHashMap.java index 33a755c..1f78106 100644 --- a/src/main/java/speiger/src/collections/chars/maps/impl/customHash/Char2BooleanOpenCustomHashMap.java +++ b/src/main/java/speiger/src/collections/chars/maps/impl/customHash/Char2BooleanOpenCustomHashMap.java @@ -5,6 +5,7 @@ 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; @@ -19,6 +20,7 @@ import speiger.src.collections.chars.functions.consumer.CharBooleanConsumer; import speiger.src.collections.chars.functions.function.CharPredicate; import speiger.src.collections.chars.functions.function.CharBooleanUnaryOperator; import speiger.src.collections.chars.functions.function.CharCharUnaryOperator; +import speiger.src.collections.chars.functions.OptionalChar; import speiger.src.collections.chars.maps.abstracts.AbstractChar2BooleanMap; import speiger.src.collections.chars.maps.interfaces.Char2BooleanMap; import speiger.src.collections.chars.sets.AbstractCharSet; @@ -36,6 +38,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectBooleanConsumer; import speiger.src.collections.booleans.functions.function.BooleanPredicate; +import speiger.src.collections.booleans.functions.OptionalBoolean; import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.sets.AbstractObjectSet; import speiger.src.collections.objects.sets.ObjectSet; @@ -930,7 +933,7 @@ public class Char2BooleanOpenCustomHashMap extends AbstractChar2BooleanMap imple } @Override - public Char2BooleanMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Char2BooleanMap.Entry state = null; boolean empty = true; @@ -947,25 +950,25 @@ public class Char2BooleanOpenCustomHashMap extends AbstractChar2BooleanMap imple } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Char2BooleanMap.Entry 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], (char)0)) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1139,7 +1142,7 @@ public class Char2BooleanOpenCustomHashMap extends AbstractChar2BooleanMap imple } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -1156,18 +1159,18 @@ public class Char2BooleanOpenCustomHashMap extends AbstractChar2BooleanMap imple } state = operator.applyAsChar(state, keys[i]); } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (char)0; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OptionalChar.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OptionalChar.of(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], (char)0) && filter.test(keys[i])) return keys[i]; + if(!strategy.equals(keys[i], (char)0) && filter.test(keys[i])) return OptionalChar.of(keys[i]); } - return (char)0; + return OptionalChar.empty(); } @Override @@ -1282,7 +1285,7 @@ public class Char2BooleanOpenCustomHashMap extends AbstractChar2BooleanMap imple } @Override - public boolean reduce(BooleanBooleanUnaryOperator operator) { + public OptionalBoolean reduce(BooleanBooleanUnaryOperator operator) { Objects.requireNonNull(operator); boolean state = false; boolean empty = true; @@ -1299,18 +1302,18 @@ public class Char2BooleanOpenCustomHashMap extends AbstractChar2BooleanMap imple } state = operator.applyAsBoolean(state, values[i]); } - return state; + return empty ? OptionalBoolean.empty() : OptionalBoolean.of(state); } @Override - public boolean findFirst(BooleanPredicate filter) { + public OptionalBoolean findFirst(BooleanPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return false; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return OptionalBoolean.empty(); + if(containsNull && filter.test(values[nullIndex])) return OptionalBoolean.of(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], (char)0) && filter.test(values[i])) return values[i]; + if(!strategy.equals(keys[i], (char)0) && filter.test(values[i])) return OptionalBoolean.of(values[i]); } - return false; + return OptionalBoolean.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/chars/maps/impl/customHash/Char2ByteLinkedOpenCustomHashMap.java b/src/main/java/speiger/src/collections/chars/maps/impl/customHash/Char2ByteLinkedOpenCustomHashMap.java index ed2799d..79c0324 100644 --- a/src/main/java/speiger/src/collections/chars/maps/impl/customHash/Char2ByteLinkedOpenCustomHashMap.java +++ b/src/main/java/speiger/src/collections/chars/maps/impl/customHash/Char2ByteLinkedOpenCustomHashMap.java @@ -4,6 +4,7 @@ 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; @@ -14,6 +15,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectCharConsumer; import speiger.src.collections.ints.functions.consumer.IntCharConsumer; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntByteConsumer; +import speiger.src.collections.chars.functions.OptionalChar; import speiger.src.collections.chars.functions.function.CharPredicate; import speiger.src.collections.chars.functions.consumer.CharByteConsumer; import speiger.src.collections.chars.functions.function.CharCharUnaryOperator; @@ -33,6 +35,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectByteConsumer; import speiger.src.collections.bytes.functions.function.BytePredicate; +import speiger.src.collections.bytes.functions.OptionalByte; import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOperator; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.objects.lists.ObjectListIterator; @@ -671,6 +674,10 @@ public class Char2ByteLinkedOpenCustomHashMap extends Char2ByteOpenCustomHashMap } private class MapEntrySet extends AbstractObjectSet implements Char2ByteOrderedMap.FastOrderedSet { + @Override + public void addFirst(Char2ByteMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Char2ByteMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Char2ByteMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -836,7 +843,7 @@ public class Char2ByteLinkedOpenCustomHashMap extends Char2ByteOpenCustomHashMap } @Override - public Char2ByteMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Char2ByteMap.Entry state = null; boolean empty = true; @@ -851,21 +858,21 @@ public class Char2ByteLinkedOpenCustomHashMap extends Char2ByteOpenCustomHashMap state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Char2ByteMap.Entry 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 @@ -944,10 +951,13 @@ public class Char2ByteLinkedOpenCustomHashMap extends Char2ByteOpenCustomHashMap public boolean add(char o) { throw new UnsupportedOperationException(); } - + + @Override + public void addFirst(char o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(char o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(char o) { throw new UnsupportedOperationException(); } - @Override public boolean addAndMoveToLast(char o) { throw new UnsupportedOperationException(); } @@ -1090,7 +1100,7 @@ public class Char2ByteLinkedOpenCustomHashMap extends Char2ByteOpenCustomHashMap } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -1105,19 +1115,19 @@ public class Char2ByteLinkedOpenCustomHashMap extends Char2ByteOpenCustomHashMap state = operator.applyAsChar(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (char)0; + if(size() <= 0) return OptionalChar.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalChar.of(keys[index]); index = (int)links[index]; } - return (char)0; + return OptionalChar.empty(); } @Override @@ -1252,7 +1262,7 @@ public class Char2ByteLinkedOpenCustomHashMap extends Char2ByteOpenCustomHashMap } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -1267,19 +1277,19 @@ public class Char2ByteLinkedOpenCustomHashMap extends Char2ByteOpenCustomHashMap state = operator.applyAsByte(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (byte)0; + if(size() <= 0) return OptionalByte.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalByte.of(values[index]); index = (int)links[index]; } - return (byte)0; + return OptionalByte.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/chars/maps/impl/customHash/Char2ByteOpenCustomHashMap.java b/src/main/java/speiger/src/collections/chars/maps/impl/customHash/Char2ByteOpenCustomHashMap.java index d24f11d..8a9e24e 100644 --- a/src/main/java/speiger/src/collections/chars/maps/impl/customHash/Char2ByteOpenCustomHashMap.java +++ b/src/main/java/speiger/src/collections/chars/maps/impl/customHash/Char2ByteOpenCustomHashMap.java @@ -5,6 +5,7 @@ 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; @@ -19,6 +20,7 @@ import speiger.src.collections.chars.functions.consumer.CharByteConsumer; import speiger.src.collections.chars.functions.function.Char2ByteFunction; import speiger.src.collections.chars.functions.function.CharByteUnaryOperator; import speiger.src.collections.chars.functions.function.CharCharUnaryOperator; +import speiger.src.collections.chars.functions.OptionalChar; import speiger.src.collections.chars.functions.function.CharPredicate; import speiger.src.collections.chars.maps.abstracts.AbstractChar2ByteMap; import speiger.src.collections.chars.maps.interfaces.Char2ByteMap; @@ -37,6 +39,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectByteConsumer; import speiger.src.collections.bytes.functions.function.BytePredicate; +import speiger.src.collections.bytes.functions.OptionalByte; import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.sets.AbstractObjectSet; import speiger.src.collections.objects.sets.ObjectSet; @@ -952,7 +955,7 @@ public class Char2ByteOpenCustomHashMap extends AbstractChar2ByteMap implements } @Override - public Char2ByteMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Char2ByteMap.Entry state = null; boolean empty = true; @@ -969,25 +972,25 @@ public class Char2ByteOpenCustomHashMap extends AbstractChar2ByteMap implements } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Char2ByteMap.Entry 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], (char)0)) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1161,7 +1164,7 @@ public class Char2ByteOpenCustomHashMap extends AbstractChar2ByteMap implements } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -1178,18 +1181,18 @@ public class Char2ByteOpenCustomHashMap extends AbstractChar2ByteMap implements } state = operator.applyAsChar(state, keys[i]); } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (char)0; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OptionalChar.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OptionalChar.of(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], (char)0) && filter.test(keys[i])) return keys[i]; + if(!strategy.equals(keys[i], (char)0) && filter.test(keys[i])) return OptionalChar.of(keys[i]); } - return (char)0; + return OptionalChar.empty(); } @Override @@ -1304,7 +1307,7 @@ public class Char2ByteOpenCustomHashMap extends AbstractChar2ByteMap implements } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -1321,18 +1324,18 @@ public class Char2ByteOpenCustomHashMap extends AbstractChar2ByteMap implements } state = operator.applyAsByte(state, values[i]); } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (byte)0; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return OptionalByte.empty(); + if(containsNull && filter.test(values[nullIndex])) return OptionalByte.of(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], (char)0) && filter.test(values[i])) return values[i]; + if(!strategy.equals(keys[i], (char)0) && filter.test(values[i])) return OptionalByte.of(values[i]); } - return (byte)0; + return OptionalByte.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/chars/maps/impl/customHash/Char2CharLinkedOpenCustomHashMap.java b/src/main/java/speiger/src/collections/chars/maps/impl/customHash/Char2CharLinkedOpenCustomHashMap.java index cd92f99..6e29c60 100644 --- a/src/main/java/speiger/src/collections/chars/maps/impl/customHash/Char2CharLinkedOpenCustomHashMap.java +++ b/src/main/java/speiger/src/collections/chars/maps/impl/customHash/Char2CharLinkedOpenCustomHashMap.java @@ -4,6 +4,7 @@ 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; @@ -13,6 +14,7 @@ import speiger.src.collections.chars.functions.CharConsumer; import speiger.src.collections.objects.functions.consumer.ObjectCharConsumer; import speiger.src.collections.ints.functions.consumer.IntCharConsumer; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; +import speiger.src.collections.chars.functions.OptionalChar; import speiger.src.collections.chars.functions.function.CharPredicate; import speiger.src.collections.chars.functions.consumer.CharCharConsumer; import speiger.src.collections.chars.functions.function.CharCharUnaryOperator; @@ -664,6 +666,10 @@ public class Char2CharLinkedOpenCustomHashMap extends Char2CharOpenCustomHashMap } private class MapEntrySet extends AbstractObjectSet implements Char2CharOrderedMap.FastOrderedSet { + @Override + public void addFirst(Char2CharMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Char2CharMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Char2CharMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -829,7 +835,7 @@ public class Char2CharLinkedOpenCustomHashMap extends Char2CharOpenCustomHashMap } @Override - public Char2CharMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Char2CharMap.Entry state = null; boolean empty = true; @@ -844,21 +850,21 @@ public class Char2CharLinkedOpenCustomHashMap extends Char2CharOpenCustomHashMap state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Char2CharMap.Entry 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 @@ -937,10 +943,13 @@ public class Char2CharLinkedOpenCustomHashMap extends Char2CharOpenCustomHashMap public boolean add(char o) { throw new UnsupportedOperationException(); } - + + @Override + public void addFirst(char o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(char o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(char o) { throw new UnsupportedOperationException(); } - @Override public boolean addAndMoveToLast(char o) { throw new UnsupportedOperationException(); } @@ -1083,7 +1092,7 @@ public class Char2CharLinkedOpenCustomHashMap extends Char2CharOpenCustomHashMap } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -1098,19 +1107,19 @@ public class Char2CharLinkedOpenCustomHashMap extends Char2CharOpenCustomHashMap state = operator.applyAsChar(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (char)0; + if(size() <= 0) return OptionalChar.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalChar.of(keys[index]); index = (int)links[index]; } - return (char)0; + return OptionalChar.empty(); } @Override @@ -1245,7 +1254,7 @@ public class Char2CharLinkedOpenCustomHashMap extends Char2CharOpenCustomHashMap } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -1260,19 +1269,19 @@ public class Char2CharLinkedOpenCustomHashMap extends Char2CharOpenCustomHashMap state = operator.applyAsChar(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (char)0; + if(size() <= 0) return OptionalChar.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalChar.of(values[index]); index = (int)links[index]; } - return (char)0; + return OptionalChar.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/chars/maps/impl/customHash/Char2CharOpenCustomHashMap.java b/src/main/java/speiger/src/collections/chars/maps/impl/customHash/Char2CharOpenCustomHashMap.java index 202a695..2eab821 100644 --- a/src/main/java/speiger/src/collections/chars/maps/impl/customHash/Char2CharOpenCustomHashMap.java +++ b/src/main/java/speiger/src/collections/chars/maps/impl/customHash/Char2CharOpenCustomHashMap.java @@ -5,6 +5,7 @@ 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; @@ -17,6 +18,7 @@ import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.chars.functions.consumer.CharCharConsumer; import speiger.src.collections.chars.functions.function.CharUnaryOperator; import speiger.src.collections.chars.functions.function.CharCharUnaryOperator; +import speiger.src.collections.chars.functions.OptionalChar; import speiger.src.collections.chars.functions.function.CharPredicate; import speiger.src.collections.chars.maps.abstracts.AbstractChar2CharMap; import speiger.src.collections.chars.maps.interfaces.Char2CharMap; @@ -944,7 +946,7 @@ public class Char2CharOpenCustomHashMap extends AbstractChar2CharMap implements } @Override - public Char2CharMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Char2CharMap.Entry state = null; boolean empty = true; @@ -961,25 +963,25 @@ public class Char2CharOpenCustomHashMap extends AbstractChar2CharMap implements } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Char2CharMap.Entry 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], (char)0)) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1153,7 +1155,7 @@ public class Char2CharOpenCustomHashMap extends AbstractChar2CharMap implements } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -1170,18 +1172,18 @@ public class Char2CharOpenCustomHashMap extends AbstractChar2CharMap implements } state = operator.applyAsChar(state, keys[i]); } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (char)0; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OptionalChar.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OptionalChar.of(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], (char)0) && filter.test(keys[i])) return keys[i]; + if(!strategy.equals(keys[i], (char)0) && filter.test(keys[i])) return OptionalChar.of(keys[i]); } - return (char)0; + return OptionalChar.empty(); } @Override @@ -1296,7 +1298,7 @@ public class Char2CharOpenCustomHashMap extends AbstractChar2CharMap implements } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -1313,18 +1315,18 @@ public class Char2CharOpenCustomHashMap extends AbstractChar2CharMap implements } state = operator.applyAsChar(state, values[i]); } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (char)0; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return OptionalChar.empty(); + if(containsNull && filter.test(values[nullIndex])) return OptionalChar.of(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], (char)0) && filter.test(values[i])) return values[i]; + if(!strategy.equals(keys[i], (char)0) && filter.test(values[i])) return OptionalChar.of(values[i]); } - return (char)0; + return OptionalChar.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/chars/maps/impl/customHash/Char2DoubleLinkedOpenCustomHashMap.java b/src/main/java/speiger/src/collections/chars/maps/impl/customHash/Char2DoubleLinkedOpenCustomHashMap.java index 78abcc2..cfbe6f9 100644 --- a/src/main/java/speiger/src/collections/chars/maps/impl/customHash/Char2DoubleLinkedOpenCustomHashMap.java +++ b/src/main/java/speiger/src/collections/chars/maps/impl/customHash/Char2DoubleLinkedOpenCustomHashMap.java @@ -4,10 +4,12 @@ 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; import java.util.function.DoublePredicate; +import java.util.OptionalDouble; import speiger.src.collections.chars.collections.CharBidirectionalIterator; import speiger.src.collections.chars.functions.CharConsumer; @@ -15,6 +17,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectCharConsumer; import speiger.src.collections.ints.functions.consumer.IntCharConsumer; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntDoubleConsumer; +import speiger.src.collections.chars.functions.OptionalChar; import speiger.src.collections.chars.functions.function.CharPredicate; import speiger.src.collections.chars.functions.consumer.CharDoubleConsumer; import speiger.src.collections.chars.functions.function.CharCharUnaryOperator; @@ -671,6 +674,10 @@ public class Char2DoubleLinkedOpenCustomHashMap extends Char2DoubleOpenCustomHas } private class MapEntrySet extends AbstractObjectSet implements Char2DoubleOrderedMap.FastOrderedSet { + @Override + public void addFirst(Char2DoubleMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Char2DoubleMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Char2DoubleMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -836,7 +843,7 @@ public class Char2DoubleLinkedOpenCustomHashMap extends Char2DoubleOpenCustomHas } @Override - public Char2DoubleMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Char2DoubleMap.Entry state = null; boolean empty = true; @@ -851,21 +858,21 @@ public class Char2DoubleLinkedOpenCustomHashMap extends Char2DoubleOpenCustomHas state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Char2DoubleMap.Entry 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 @@ -944,10 +951,13 @@ public class Char2DoubleLinkedOpenCustomHashMap extends Char2DoubleOpenCustomHas public boolean add(char o) { throw new UnsupportedOperationException(); } - + + @Override + public void addFirst(char o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(char o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(char o) { throw new UnsupportedOperationException(); } - @Override public boolean addAndMoveToLast(char o) { throw new UnsupportedOperationException(); } @@ -1090,7 +1100,7 @@ public class Char2DoubleLinkedOpenCustomHashMap extends Char2DoubleOpenCustomHas } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -1105,19 +1115,19 @@ public class Char2DoubleLinkedOpenCustomHashMap extends Char2DoubleOpenCustomHas state = operator.applyAsChar(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (char)0; + if(size() <= 0) return OptionalChar.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalChar.of(keys[index]); index = (int)links[index]; } - return (char)0; + return OptionalChar.empty(); } @Override @@ -1252,7 +1262,7 @@ public class Char2DoubleLinkedOpenCustomHashMap extends Char2DoubleOpenCustomHas } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -1267,19 +1277,19 @@ public class Char2DoubleLinkedOpenCustomHashMap extends Char2DoubleOpenCustomHas state = operator.applyAsDouble(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0D; + if(size() <= 0) return OptionalDouble.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalDouble.of(values[index]); index = (int)links[index]; } - return 0D; + return OptionalDouble.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/chars/maps/impl/customHash/Char2DoubleOpenCustomHashMap.java b/src/main/java/speiger/src/collections/chars/maps/impl/customHash/Char2DoubleOpenCustomHashMap.java index 2827a37..90b8b63 100644 --- a/src/main/java/speiger/src/collections/chars/maps/impl/customHash/Char2DoubleOpenCustomHashMap.java +++ b/src/main/java/speiger/src/collections/chars/maps/impl/customHash/Char2DoubleOpenCustomHashMap.java @@ -5,10 +5,12 @@ 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; import java.util.function.DoublePredicate; +import java.util.OptionalDouble; import speiger.src.collections.chars.collections.CharIterator; import speiger.src.collections.chars.functions.CharConsumer; @@ -20,6 +22,7 @@ import speiger.src.collections.chars.functions.consumer.CharDoubleConsumer; import speiger.src.collections.chars.functions.function.Char2DoubleFunction; import speiger.src.collections.chars.functions.function.CharDoubleUnaryOperator; import speiger.src.collections.chars.functions.function.CharCharUnaryOperator; +import speiger.src.collections.chars.functions.OptionalChar; import speiger.src.collections.chars.functions.function.CharPredicate; import speiger.src.collections.chars.maps.abstracts.AbstractChar2DoubleMap; import speiger.src.collections.chars.maps.interfaces.Char2DoubleMap; @@ -952,7 +955,7 @@ public class Char2DoubleOpenCustomHashMap extends AbstractChar2DoubleMap impleme } @Override - public Char2DoubleMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Char2DoubleMap.Entry state = null; boolean empty = true; @@ -969,25 +972,25 @@ public class Char2DoubleOpenCustomHashMap extends AbstractChar2DoubleMap impleme } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Char2DoubleMap.Entry 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], (char)0)) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1161,7 +1164,7 @@ public class Char2DoubleOpenCustomHashMap extends AbstractChar2DoubleMap impleme } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -1178,18 +1181,18 @@ public class Char2DoubleOpenCustomHashMap extends AbstractChar2DoubleMap impleme } state = operator.applyAsChar(state, keys[i]); } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (char)0; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OptionalChar.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OptionalChar.of(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], (char)0) && filter.test(keys[i])) return keys[i]; + if(!strategy.equals(keys[i], (char)0) && filter.test(keys[i])) return OptionalChar.of(keys[i]); } - return (char)0; + return OptionalChar.empty(); } @Override @@ -1304,7 +1307,7 @@ public class Char2DoubleOpenCustomHashMap extends AbstractChar2DoubleMap impleme } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -1321,18 +1324,18 @@ public class Char2DoubleOpenCustomHashMap extends AbstractChar2DoubleMap impleme } state = operator.applyAsDouble(state, values[i]); } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0D; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return OptionalDouble.empty(); + if(containsNull && filter.test(values[nullIndex])) return OptionalDouble.of(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], (char)0) && filter.test(values[i])) return values[i]; + if(!strategy.equals(keys[i], (char)0) && filter.test(values[i])) return OptionalDouble.of(values[i]); } - return 0D; + return OptionalDouble.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/chars/maps/impl/customHash/Char2FloatLinkedOpenCustomHashMap.java b/src/main/java/speiger/src/collections/chars/maps/impl/customHash/Char2FloatLinkedOpenCustomHashMap.java index 488c7d3..cca19cc 100644 --- a/src/main/java/speiger/src/collections/chars/maps/impl/customHash/Char2FloatLinkedOpenCustomHashMap.java +++ b/src/main/java/speiger/src/collections/chars/maps/impl/customHash/Char2FloatLinkedOpenCustomHashMap.java @@ -4,6 +4,7 @@ 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; @@ -14,6 +15,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectCharConsumer; import speiger.src.collections.ints.functions.consumer.IntCharConsumer; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntFloatConsumer; +import speiger.src.collections.chars.functions.OptionalChar; import speiger.src.collections.chars.functions.function.CharPredicate; import speiger.src.collections.chars.functions.consumer.CharFloatConsumer; import speiger.src.collections.chars.functions.function.CharCharUnaryOperator; @@ -33,6 +35,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectFloatConsumer; import speiger.src.collections.floats.functions.function.FloatPredicate; +import speiger.src.collections.floats.functions.OptionalFloat; import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOperator; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.objects.lists.ObjectListIterator; @@ -671,6 +674,10 @@ public class Char2FloatLinkedOpenCustomHashMap extends Char2FloatOpenCustomHashM } private class MapEntrySet extends AbstractObjectSet implements Char2FloatOrderedMap.FastOrderedSet { + @Override + public void addFirst(Char2FloatMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Char2FloatMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Char2FloatMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -836,7 +843,7 @@ public class Char2FloatLinkedOpenCustomHashMap extends Char2FloatOpenCustomHashM } @Override - public Char2FloatMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Char2FloatMap.Entry state = null; boolean empty = true; @@ -851,21 +858,21 @@ public class Char2FloatLinkedOpenCustomHashMap extends Char2FloatOpenCustomHashM state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Char2FloatMap.Entry 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 @@ -944,10 +951,13 @@ public class Char2FloatLinkedOpenCustomHashMap extends Char2FloatOpenCustomHashM public boolean add(char o) { throw new UnsupportedOperationException(); } - + + @Override + public void addFirst(char o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(char o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(char o) { throw new UnsupportedOperationException(); } - @Override public boolean addAndMoveToLast(char o) { throw new UnsupportedOperationException(); } @@ -1090,7 +1100,7 @@ public class Char2FloatLinkedOpenCustomHashMap extends Char2FloatOpenCustomHashM } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -1105,19 +1115,19 @@ public class Char2FloatLinkedOpenCustomHashMap extends Char2FloatOpenCustomHashM state = operator.applyAsChar(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (char)0; + if(size() <= 0) return OptionalChar.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalChar.of(keys[index]); index = (int)links[index]; } - return (char)0; + return OptionalChar.empty(); } @Override @@ -1252,7 +1262,7 @@ public class Char2FloatLinkedOpenCustomHashMap extends Char2FloatOpenCustomHashM } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -1267,19 +1277,19 @@ public class Char2FloatLinkedOpenCustomHashMap extends Char2FloatOpenCustomHashM state = operator.applyAsFloat(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0F; + if(size() <= 0) return OptionalFloat.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalFloat.of(values[index]); index = (int)links[index]; } - return 0F; + return OptionalFloat.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/chars/maps/impl/customHash/Char2FloatOpenCustomHashMap.java b/src/main/java/speiger/src/collections/chars/maps/impl/customHash/Char2FloatOpenCustomHashMap.java index fefed7d..09f9d1e 100644 --- a/src/main/java/speiger/src/collections/chars/maps/impl/customHash/Char2FloatOpenCustomHashMap.java +++ b/src/main/java/speiger/src/collections/chars/maps/impl/customHash/Char2FloatOpenCustomHashMap.java @@ -5,6 +5,7 @@ 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; @@ -19,6 +20,7 @@ import speiger.src.collections.chars.functions.consumer.CharFloatConsumer; import speiger.src.collections.chars.functions.function.Char2FloatFunction; import speiger.src.collections.chars.functions.function.CharFloatUnaryOperator; import speiger.src.collections.chars.functions.function.CharCharUnaryOperator; +import speiger.src.collections.chars.functions.OptionalChar; import speiger.src.collections.chars.functions.function.CharPredicate; import speiger.src.collections.chars.maps.abstracts.AbstractChar2FloatMap; import speiger.src.collections.chars.maps.interfaces.Char2FloatMap; @@ -37,6 +39,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectFloatConsumer; import speiger.src.collections.floats.functions.function.FloatPredicate; +import speiger.src.collections.floats.functions.OptionalFloat; import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.sets.AbstractObjectSet; import speiger.src.collections.objects.sets.ObjectSet; @@ -952,7 +955,7 @@ public class Char2FloatOpenCustomHashMap extends AbstractChar2FloatMap implement } @Override - public Char2FloatMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Char2FloatMap.Entry state = null; boolean empty = true; @@ -969,25 +972,25 @@ public class Char2FloatOpenCustomHashMap extends AbstractChar2FloatMap implement } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Char2FloatMap.Entry 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], (char)0)) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1161,7 +1164,7 @@ public class Char2FloatOpenCustomHashMap extends AbstractChar2FloatMap implement } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -1178,18 +1181,18 @@ public class Char2FloatOpenCustomHashMap extends AbstractChar2FloatMap implement } state = operator.applyAsChar(state, keys[i]); } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (char)0; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OptionalChar.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OptionalChar.of(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], (char)0) && filter.test(keys[i])) return keys[i]; + if(!strategy.equals(keys[i], (char)0) && filter.test(keys[i])) return OptionalChar.of(keys[i]); } - return (char)0; + return OptionalChar.empty(); } @Override @@ -1304,7 +1307,7 @@ public class Char2FloatOpenCustomHashMap extends AbstractChar2FloatMap implement } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -1321,18 +1324,18 @@ public class Char2FloatOpenCustomHashMap extends AbstractChar2FloatMap implement } state = operator.applyAsFloat(state, values[i]); } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0F; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return OptionalFloat.empty(); + if(containsNull && filter.test(values[nullIndex])) return OptionalFloat.of(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], (char)0) && filter.test(values[i])) return values[i]; + if(!strategy.equals(keys[i], (char)0) && filter.test(values[i])) return OptionalFloat.of(values[i]); } - return 0F; + return OptionalFloat.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/chars/maps/impl/customHash/Char2IntLinkedOpenCustomHashMap.java b/src/main/java/speiger/src/collections/chars/maps/impl/customHash/Char2IntLinkedOpenCustomHashMap.java index 9b40a08..c5e5beb 100644 --- a/src/main/java/speiger/src/collections/chars/maps/impl/customHash/Char2IntLinkedOpenCustomHashMap.java +++ b/src/main/java/speiger/src/collections/chars/maps/impl/customHash/Char2IntLinkedOpenCustomHashMap.java @@ -4,10 +4,12 @@ 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; import java.util.function.IntPredicate; +import java.util.OptionalInt; import speiger.src.collections.chars.collections.CharBidirectionalIterator; import speiger.src.collections.chars.functions.CharConsumer; @@ -15,6 +17,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectCharConsumer; import speiger.src.collections.ints.functions.consumer.IntCharConsumer; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntIntConsumer; +import speiger.src.collections.chars.functions.OptionalChar; import speiger.src.collections.chars.functions.function.CharPredicate; import speiger.src.collections.chars.functions.consumer.CharIntConsumer; import speiger.src.collections.chars.functions.function.CharCharUnaryOperator; @@ -671,6 +674,10 @@ public class Char2IntLinkedOpenCustomHashMap extends Char2IntOpenCustomHashMap i } private class MapEntrySet extends AbstractObjectSet implements Char2IntOrderedMap.FastOrderedSet { + @Override + public void addFirst(Char2IntMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Char2IntMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Char2IntMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -836,7 +843,7 @@ public class Char2IntLinkedOpenCustomHashMap extends Char2IntOpenCustomHashMap i } @Override - public Char2IntMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Char2IntMap.Entry state = null; boolean empty = true; @@ -851,21 +858,21 @@ public class Char2IntLinkedOpenCustomHashMap extends Char2IntOpenCustomHashMap i state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Char2IntMap.Entry 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 @@ -944,10 +951,13 @@ public class Char2IntLinkedOpenCustomHashMap extends Char2IntOpenCustomHashMap i public boolean add(char o) { throw new UnsupportedOperationException(); } - + + @Override + public void addFirst(char o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(char o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(char o) { throw new UnsupportedOperationException(); } - @Override public boolean addAndMoveToLast(char o) { throw new UnsupportedOperationException(); } @@ -1090,7 +1100,7 @@ public class Char2IntLinkedOpenCustomHashMap extends Char2IntOpenCustomHashMap i } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -1105,19 +1115,19 @@ public class Char2IntLinkedOpenCustomHashMap extends Char2IntOpenCustomHashMap i state = operator.applyAsChar(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (char)0; + if(size() <= 0) return OptionalChar.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalChar.of(keys[index]); index = (int)links[index]; } - return (char)0; + return OptionalChar.empty(); } @Override @@ -1252,7 +1262,7 @@ public class Char2IntLinkedOpenCustomHashMap extends Char2IntOpenCustomHashMap i } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -1267,19 +1277,19 @@ public class Char2IntLinkedOpenCustomHashMap extends Char2IntOpenCustomHashMap i state = operator.applyAsInt(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0; + if(size() <= 0) return OptionalInt.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalInt.of(values[index]); index = (int)links[index]; } - return 0; + return OptionalInt.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/chars/maps/impl/customHash/Char2IntOpenCustomHashMap.java b/src/main/java/speiger/src/collections/chars/maps/impl/customHash/Char2IntOpenCustomHashMap.java index 4f483fe..284cd25 100644 --- a/src/main/java/speiger/src/collections/chars/maps/impl/customHash/Char2IntOpenCustomHashMap.java +++ b/src/main/java/speiger/src/collections/chars/maps/impl/customHash/Char2IntOpenCustomHashMap.java @@ -5,10 +5,12 @@ 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; import java.util.function.IntPredicate; +import java.util.OptionalInt; import speiger.src.collections.chars.collections.CharIterator; import speiger.src.collections.chars.functions.CharConsumer; @@ -20,6 +22,7 @@ import speiger.src.collections.chars.functions.consumer.CharIntConsumer; import speiger.src.collections.chars.functions.function.Char2IntFunction; import speiger.src.collections.chars.functions.function.CharIntUnaryOperator; import speiger.src.collections.chars.functions.function.CharCharUnaryOperator; +import speiger.src.collections.chars.functions.OptionalChar; import speiger.src.collections.chars.functions.function.CharPredicate; import speiger.src.collections.chars.maps.abstracts.AbstractChar2IntMap; import speiger.src.collections.chars.maps.interfaces.Char2IntMap; @@ -952,7 +955,7 @@ public class Char2IntOpenCustomHashMap extends AbstractChar2IntMap implements IT } @Override - public Char2IntMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Char2IntMap.Entry state = null; boolean empty = true; @@ -969,25 +972,25 @@ public class Char2IntOpenCustomHashMap extends AbstractChar2IntMap implements IT } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Char2IntMap.Entry 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], (char)0)) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1161,7 +1164,7 @@ public class Char2IntOpenCustomHashMap extends AbstractChar2IntMap implements IT } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -1178,18 +1181,18 @@ public class Char2IntOpenCustomHashMap extends AbstractChar2IntMap implements IT } state = operator.applyAsChar(state, keys[i]); } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (char)0; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OptionalChar.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OptionalChar.of(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], (char)0) && filter.test(keys[i])) return keys[i]; + if(!strategy.equals(keys[i], (char)0) && filter.test(keys[i])) return OptionalChar.of(keys[i]); } - return (char)0; + return OptionalChar.empty(); } @Override @@ -1304,7 +1307,7 @@ public class Char2IntOpenCustomHashMap extends AbstractChar2IntMap implements IT } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -1321,18 +1324,18 @@ public class Char2IntOpenCustomHashMap extends AbstractChar2IntMap implements IT } state = operator.applyAsInt(state, values[i]); } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return OptionalInt.empty(); + if(containsNull && filter.test(values[nullIndex])) return OptionalInt.of(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], (char)0) && filter.test(values[i])) return values[i]; + if(!strategy.equals(keys[i], (char)0) && filter.test(values[i])) return OptionalInt.of(values[i]); } - return 0; + return OptionalInt.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/chars/maps/impl/customHash/Char2LongLinkedOpenCustomHashMap.java b/src/main/java/speiger/src/collections/chars/maps/impl/customHash/Char2LongLinkedOpenCustomHashMap.java index 3253b35..9578ab1 100644 --- a/src/main/java/speiger/src/collections/chars/maps/impl/customHash/Char2LongLinkedOpenCustomHashMap.java +++ b/src/main/java/speiger/src/collections/chars/maps/impl/customHash/Char2LongLinkedOpenCustomHashMap.java @@ -4,10 +4,12 @@ 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; import java.util.function.LongPredicate; +import java.util.OptionalLong; import speiger.src.collections.chars.collections.CharBidirectionalIterator; import speiger.src.collections.chars.functions.CharConsumer; @@ -15,6 +17,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectCharConsumer; import speiger.src.collections.ints.functions.consumer.IntCharConsumer; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntLongConsumer; +import speiger.src.collections.chars.functions.OptionalChar; import speiger.src.collections.chars.functions.function.CharPredicate; import speiger.src.collections.chars.functions.consumer.CharLongConsumer; import speiger.src.collections.chars.functions.function.CharCharUnaryOperator; @@ -671,6 +674,10 @@ public class Char2LongLinkedOpenCustomHashMap extends Char2LongOpenCustomHashMap } private class MapEntrySet extends AbstractObjectSet implements Char2LongOrderedMap.FastOrderedSet { + @Override + public void addFirst(Char2LongMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Char2LongMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Char2LongMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -836,7 +843,7 @@ public class Char2LongLinkedOpenCustomHashMap extends Char2LongOpenCustomHashMap } @Override - public Char2LongMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Char2LongMap.Entry state = null; boolean empty = true; @@ -851,21 +858,21 @@ public class Char2LongLinkedOpenCustomHashMap extends Char2LongOpenCustomHashMap state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Char2LongMap.Entry 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 @@ -944,10 +951,13 @@ public class Char2LongLinkedOpenCustomHashMap extends Char2LongOpenCustomHashMap public boolean add(char o) { throw new UnsupportedOperationException(); } - + + @Override + public void addFirst(char o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(char o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(char o) { throw new UnsupportedOperationException(); } - @Override public boolean addAndMoveToLast(char o) { throw new UnsupportedOperationException(); } @@ -1090,7 +1100,7 @@ public class Char2LongLinkedOpenCustomHashMap extends Char2LongOpenCustomHashMap } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -1105,19 +1115,19 @@ public class Char2LongLinkedOpenCustomHashMap extends Char2LongOpenCustomHashMap state = operator.applyAsChar(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (char)0; + if(size() <= 0) return OptionalChar.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalChar.of(keys[index]); index = (int)links[index]; } - return (char)0; + return OptionalChar.empty(); } @Override @@ -1252,7 +1262,7 @@ public class Char2LongLinkedOpenCustomHashMap extends Char2LongOpenCustomHashMap } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -1267,19 +1277,19 @@ public class Char2LongLinkedOpenCustomHashMap extends Char2LongOpenCustomHashMap state = operator.applyAsLong(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0L; + if(size() <= 0) return OptionalLong.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalLong.of(values[index]); index = (int)links[index]; } - return 0L; + return OptionalLong.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/chars/maps/impl/customHash/Char2LongOpenCustomHashMap.java b/src/main/java/speiger/src/collections/chars/maps/impl/customHash/Char2LongOpenCustomHashMap.java index 3a531d9..9b56ce4 100644 --- a/src/main/java/speiger/src/collections/chars/maps/impl/customHash/Char2LongOpenCustomHashMap.java +++ b/src/main/java/speiger/src/collections/chars/maps/impl/customHash/Char2LongOpenCustomHashMap.java @@ -5,10 +5,12 @@ 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; import java.util.function.LongPredicate; +import java.util.OptionalLong; import speiger.src.collections.chars.collections.CharIterator; import speiger.src.collections.chars.functions.CharConsumer; @@ -20,6 +22,7 @@ import speiger.src.collections.chars.functions.consumer.CharLongConsumer; import speiger.src.collections.chars.functions.function.Char2LongFunction; import speiger.src.collections.chars.functions.function.CharLongUnaryOperator; import speiger.src.collections.chars.functions.function.CharCharUnaryOperator; +import speiger.src.collections.chars.functions.OptionalChar; import speiger.src.collections.chars.functions.function.CharPredicate; import speiger.src.collections.chars.maps.abstracts.AbstractChar2LongMap; import speiger.src.collections.chars.maps.interfaces.Char2LongMap; @@ -952,7 +955,7 @@ public class Char2LongOpenCustomHashMap extends AbstractChar2LongMap implements } @Override - public Char2LongMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Char2LongMap.Entry state = null; boolean empty = true; @@ -969,25 +972,25 @@ public class Char2LongOpenCustomHashMap extends AbstractChar2LongMap implements } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Char2LongMap.Entry 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], (char)0)) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1161,7 +1164,7 @@ public class Char2LongOpenCustomHashMap extends AbstractChar2LongMap implements } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -1178,18 +1181,18 @@ public class Char2LongOpenCustomHashMap extends AbstractChar2LongMap implements } state = operator.applyAsChar(state, keys[i]); } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (char)0; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OptionalChar.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OptionalChar.of(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], (char)0) && filter.test(keys[i])) return keys[i]; + if(!strategy.equals(keys[i], (char)0) && filter.test(keys[i])) return OptionalChar.of(keys[i]); } - return (char)0; + return OptionalChar.empty(); } @Override @@ -1304,7 +1307,7 @@ public class Char2LongOpenCustomHashMap extends AbstractChar2LongMap implements } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -1321,18 +1324,18 @@ public class Char2LongOpenCustomHashMap extends AbstractChar2LongMap implements } state = operator.applyAsLong(state, values[i]); } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0L; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return OptionalLong.empty(); + if(containsNull && filter.test(values[nullIndex])) return OptionalLong.of(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], (char)0) && filter.test(values[i])) return values[i]; + if(!strategy.equals(keys[i], (char)0) && filter.test(values[i])) return OptionalLong.of(values[i]); } - return 0L; + return OptionalLong.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/chars/maps/impl/customHash/Char2ObjectLinkedOpenCustomHashMap.java b/src/main/java/speiger/src/collections/chars/maps/impl/customHash/Char2ObjectLinkedOpenCustomHashMap.java index 2724a94..5a3d8b9 100644 --- a/src/main/java/speiger/src/collections/chars/maps/impl/customHash/Char2ObjectLinkedOpenCustomHashMap.java +++ b/src/main/java/speiger/src/collections/chars/maps/impl/customHash/Char2ObjectLinkedOpenCustomHashMap.java @@ -4,6 +4,7 @@ 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; @@ -13,6 +14,7 @@ import speiger.src.collections.chars.functions.CharConsumer; import speiger.src.collections.objects.functions.consumer.ObjectCharConsumer; import speiger.src.collections.ints.functions.consumer.IntCharConsumer; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; +import speiger.src.collections.chars.functions.OptionalChar; import speiger.src.collections.chars.functions.function.CharPredicate; import speiger.src.collections.chars.functions.consumer.CharObjectConsumer; import speiger.src.collections.chars.functions.function.CharCharUnaryOperator; @@ -666,6 +668,10 @@ public class Char2ObjectLinkedOpenCustomHashMap extends Char2ObjectOpenCustom } private class MapEntrySet extends AbstractObjectSet> implements Char2ObjectOrderedMap.FastOrderedSet { + @Override + public void addFirst(Char2ObjectMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Char2ObjectMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Char2ObjectMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -831,7 +837,7 @@ public class Char2ObjectLinkedOpenCustomHashMap extends Char2ObjectOpenCustom } @Override - public Char2ObjectMap.Entry reduce(ObjectObjectUnaryOperator, Char2ObjectMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Char2ObjectMap.Entry> operator) { Objects.requireNonNull(operator); Char2ObjectMap.Entry state = null; boolean empty = true; @@ -846,21 +852,21 @@ public class Char2ObjectLinkedOpenCustomHashMap extends Char2ObjectOpenCustom state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Char2ObjectMap.Entry 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 @@ -939,10 +945,13 @@ public class Char2ObjectLinkedOpenCustomHashMap extends Char2ObjectOpenCustom public boolean add(char o) { throw new UnsupportedOperationException(); } - + + @Override + public void addFirst(char o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(char o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(char o) { throw new UnsupportedOperationException(); } - @Override public boolean addAndMoveToLast(char o) { throw new UnsupportedOperationException(); } @@ -1085,7 +1094,7 @@ public class Char2ObjectLinkedOpenCustomHashMap extends Char2ObjectOpenCustom } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -1100,19 +1109,19 @@ public class Char2ObjectLinkedOpenCustomHashMap extends Char2ObjectOpenCustom state = operator.applyAsChar(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (char)0; + if(size() <= 0) return OptionalChar.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalChar.of(keys[index]); index = (int)links[index]; } - return (char)0; + return OptionalChar.empty(); } @Override @@ -1248,7 +1257,7 @@ public class Char2ObjectLinkedOpenCustomHashMap extends Char2ObjectOpenCustom } @Override - public V reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); V state = null; boolean empty = true; @@ -1263,19 +1272,19 @@ public class Char2ObjectLinkedOpenCustomHashMap extends Char2ObjectOpenCustom state = operator.apply(state, values[index]); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public V findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return Optional.ofNullable(values[index]); index = (int)links[index]; } - return null; + return Optional.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/chars/maps/impl/customHash/Char2ObjectOpenCustomHashMap.java b/src/main/java/speiger/src/collections/chars/maps/impl/customHash/Char2ObjectOpenCustomHashMap.java index b72f150..c487b6b 100644 --- a/src/main/java/speiger/src/collections/chars/maps/impl/customHash/Char2ObjectOpenCustomHashMap.java +++ b/src/main/java/speiger/src/collections/chars/maps/impl/customHash/Char2ObjectOpenCustomHashMap.java @@ -5,6 +5,7 @@ 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; @@ -18,6 +19,7 @@ import speiger.src.collections.chars.functions.consumer.CharObjectConsumer; import speiger.src.collections.chars.functions.function.CharFunction; import speiger.src.collections.chars.functions.function.CharObjectUnaryOperator; import speiger.src.collections.chars.functions.function.CharCharUnaryOperator; +import speiger.src.collections.chars.functions.OptionalChar; import speiger.src.collections.chars.functions.function.CharPredicate; import speiger.src.collections.chars.maps.abstracts.AbstractChar2ObjectMap; import speiger.src.collections.chars.maps.interfaces.Char2ObjectMap; @@ -868,7 +870,7 @@ public class Char2ObjectOpenCustomHashMap extends AbstractChar2ObjectMap i } @Override - public Char2ObjectMap.Entry reduce(ObjectObjectUnaryOperator, Char2ObjectMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Char2ObjectMap.Entry> operator) { Objects.requireNonNull(operator); Char2ObjectMap.Entry state = null; boolean empty = true; @@ -885,25 +887,25 @@ public class Char2ObjectOpenCustomHashMap extends AbstractChar2ObjectMap i } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Char2ObjectMap.Entry 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], (char)0)) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1077,7 +1079,7 @@ public class Char2ObjectOpenCustomHashMap extends AbstractChar2ObjectMap i } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -1094,18 +1096,18 @@ public class Char2ObjectOpenCustomHashMap extends AbstractChar2ObjectMap i } state = operator.applyAsChar(state, keys[i]); } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (char)0; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OptionalChar.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OptionalChar.of(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], (char)0) && filter.test(keys[i])) return keys[i]; + if(!strategy.equals(keys[i], (char)0) && filter.test(keys[i])) return OptionalChar.of(keys[i]); } - return (char)0; + return OptionalChar.empty(); } @Override @@ -1220,7 +1222,7 @@ public class Char2ObjectOpenCustomHashMap extends AbstractChar2ObjectMap i } @Override - public V reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); V state = null; boolean empty = true; @@ -1237,18 +1239,18 @@ public class Char2ObjectOpenCustomHashMap extends AbstractChar2ObjectMap i } state = operator.apply(state, values[i]); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public V findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return Optional.empty(); + if(containsNull && filter.test(values[nullIndex])) return Optional.ofNullable(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], (char)0) && filter.test(values[i])) return values[i]; + if(!strategy.equals(keys[i], (char)0) && filter.test(values[i])) return Optional.ofNullable(values[i]); } - return null; + return Optional.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/chars/maps/impl/customHash/Char2ShortLinkedOpenCustomHashMap.java b/src/main/java/speiger/src/collections/chars/maps/impl/customHash/Char2ShortLinkedOpenCustomHashMap.java index dd6be69..ccf3a24 100644 --- a/src/main/java/speiger/src/collections/chars/maps/impl/customHash/Char2ShortLinkedOpenCustomHashMap.java +++ b/src/main/java/speiger/src/collections/chars/maps/impl/customHash/Char2ShortLinkedOpenCustomHashMap.java @@ -4,6 +4,7 @@ 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; @@ -14,6 +15,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectCharConsumer; import speiger.src.collections.ints.functions.consumer.IntCharConsumer; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntShortConsumer; +import speiger.src.collections.chars.functions.OptionalChar; import speiger.src.collections.chars.functions.function.CharPredicate; import speiger.src.collections.chars.functions.consumer.CharShortConsumer; import speiger.src.collections.chars.functions.function.CharCharUnaryOperator; @@ -33,6 +35,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectShortConsumer; import speiger.src.collections.shorts.functions.function.ShortPredicate; +import speiger.src.collections.shorts.functions.OptionalShort; import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOperator; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.objects.lists.ObjectListIterator; @@ -671,6 +674,10 @@ public class Char2ShortLinkedOpenCustomHashMap extends Char2ShortOpenCustomHashM } private class MapEntrySet extends AbstractObjectSet implements Char2ShortOrderedMap.FastOrderedSet { + @Override + public void addFirst(Char2ShortMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Char2ShortMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Char2ShortMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -836,7 +843,7 @@ public class Char2ShortLinkedOpenCustomHashMap extends Char2ShortOpenCustomHashM } @Override - public Char2ShortMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Char2ShortMap.Entry state = null; boolean empty = true; @@ -851,21 +858,21 @@ public class Char2ShortLinkedOpenCustomHashMap extends Char2ShortOpenCustomHashM state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Char2ShortMap.Entry 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 @@ -944,10 +951,13 @@ public class Char2ShortLinkedOpenCustomHashMap extends Char2ShortOpenCustomHashM public boolean add(char o) { throw new UnsupportedOperationException(); } - + + @Override + public void addFirst(char o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(char o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(char o) { throw new UnsupportedOperationException(); } - @Override public boolean addAndMoveToLast(char o) { throw new UnsupportedOperationException(); } @@ -1090,7 +1100,7 @@ public class Char2ShortLinkedOpenCustomHashMap extends Char2ShortOpenCustomHashM } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -1105,19 +1115,19 @@ public class Char2ShortLinkedOpenCustomHashMap extends Char2ShortOpenCustomHashM state = operator.applyAsChar(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (char)0; + if(size() <= 0) return OptionalChar.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalChar.of(keys[index]); index = (int)links[index]; } - return (char)0; + return OptionalChar.empty(); } @Override @@ -1252,7 +1262,7 @@ public class Char2ShortLinkedOpenCustomHashMap extends Char2ShortOpenCustomHashM } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -1267,19 +1277,19 @@ public class Char2ShortLinkedOpenCustomHashMap extends Char2ShortOpenCustomHashM state = operator.applyAsShort(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (short)0; + if(size() <= 0) return OptionalShort.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalShort.of(values[index]); index = (int)links[index]; } - return (short)0; + return OptionalShort.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/chars/maps/impl/customHash/Char2ShortOpenCustomHashMap.java b/src/main/java/speiger/src/collections/chars/maps/impl/customHash/Char2ShortOpenCustomHashMap.java index 0b1bc66..099e72a 100644 --- a/src/main/java/speiger/src/collections/chars/maps/impl/customHash/Char2ShortOpenCustomHashMap.java +++ b/src/main/java/speiger/src/collections/chars/maps/impl/customHash/Char2ShortOpenCustomHashMap.java @@ -5,6 +5,7 @@ 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; @@ -19,6 +20,7 @@ import speiger.src.collections.chars.functions.consumer.CharShortConsumer; import speiger.src.collections.chars.functions.function.Char2ShortFunction; import speiger.src.collections.chars.functions.function.CharShortUnaryOperator; import speiger.src.collections.chars.functions.function.CharCharUnaryOperator; +import speiger.src.collections.chars.functions.OptionalChar; import speiger.src.collections.chars.functions.function.CharPredicate; import speiger.src.collections.chars.maps.abstracts.AbstractChar2ShortMap; import speiger.src.collections.chars.maps.interfaces.Char2ShortMap; @@ -37,6 +39,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectShortConsumer; import speiger.src.collections.shorts.functions.function.ShortPredicate; +import speiger.src.collections.shorts.functions.OptionalShort; import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.sets.AbstractObjectSet; import speiger.src.collections.objects.sets.ObjectSet; @@ -952,7 +955,7 @@ public class Char2ShortOpenCustomHashMap extends AbstractChar2ShortMap implement } @Override - public Char2ShortMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Char2ShortMap.Entry state = null; boolean empty = true; @@ -969,25 +972,25 @@ public class Char2ShortOpenCustomHashMap extends AbstractChar2ShortMap implement } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Char2ShortMap.Entry 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], (char)0)) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1161,7 +1164,7 @@ public class Char2ShortOpenCustomHashMap extends AbstractChar2ShortMap implement } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -1178,18 +1181,18 @@ public class Char2ShortOpenCustomHashMap extends AbstractChar2ShortMap implement } state = operator.applyAsChar(state, keys[i]); } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (char)0; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OptionalChar.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OptionalChar.of(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], (char)0) && filter.test(keys[i])) return keys[i]; + if(!strategy.equals(keys[i], (char)0) && filter.test(keys[i])) return OptionalChar.of(keys[i]); } - return (char)0; + return OptionalChar.empty(); } @Override @@ -1304,7 +1307,7 @@ public class Char2ShortOpenCustomHashMap extends AbstractChar2ShortMap implement } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -1321,18 +1324,18 @@ public class Char2ShortOpenCustomHashMap extends AbstractChar2ShortMap implement } state = operator.applyAsShort(state, values[i]); } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (short)0; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return OptionalShort.empty(); + if(containsNull && filter.test(values[nullIndex])) return OptionalShort.of(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], (char)0) && filter.test(values[i])) return values[i]; + if(!strategy.equals(keys[i], (char)0) && filter.test(values[i])) return OptionalShort.of(values[i]); } - return (short)0; + return OptionalShort.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/chars/maps/impl/hash/Char2BooleanLinkedOpenHashMap.java b/src/main/java/speiger/src/collections/chars/maps/impl/hash/Char2BooleanLinkedOpenHashMap.java index 02a3e93..f6ed4b9 100644 --- a/src/main/java/speiger/src/collections/chars/maps/impl/hash/Char2BooleanLinkedOpenHashMap.java +++ b/src/main/java/speiger/src/collections/chars/maps/impl/hash/Char2BooleanLinkedOpenHashMap.java @@ -4,6 +4,7 @@ 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; @@ -14,6 +15,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectCharConsumer; import speiger.src.collections.ints.functions.consumer.IntCharConsumer; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntBooleanConsumer; +import speiger.src.collections.chars.functions.OptionalChar; import speiger.src.collections.chars.functions.function.CharPredicate; import speiger.src.collections.chars.functions.consumer.CharBooleanConsumer; import speiger.src.collections.chars.functions.function.CharCharUnaryOperator; @@ -32,6 +34,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectBooleanConsumer; import speiger.src.collections.booleans.functions.function.BooleanPredicate; +import speiger.src.collections.booleans.functions.OptionalBoolean; import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOperator; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.objects.lists.ObjectListIterator; @@ -669,6 +672,10 @@ public class Char2BooleanLinkedOpenHashMap extends Char2BooleanOpenHashMap imple } private class MapEntrySet extends AbstractObjectSet implements Char2BooleanOrderedMap.FastOrderedSet { + @Override + public void addFirst(Char2BooleanMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Char2BooleanMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Char2BooleanMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -834,7 +841,7 @@ public class Char2BooleanLinkedOpenHashMap extends Char2BooleanOpenHashMap imple } @Override - public Char2BooleanMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Char2BooleanMap.Entry state = null; boolean empty = true; @@ -849,21 +856,21 @@ public class Char2BooleanLinkedOpenHashMap extends Char2BooleanOpenHashMap imple state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Char2BooleanMap.Entry 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 @@ -939,6 +946,12 @@ public class Char2BooleanLinkedOpenHashMap extends Char2BooleanOpenHashMap imple @Override public boolean add(char o) { throw new UnsupportedOperationException(); } + + @Override + public void addFirst(char o) { throw new UnsupportedOperationException(); } + + @Override + public void addLast(char o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(char o) { throw new UnsupportedOperationException(); } @@ -1085,7 +1098,7 @@ public class Char2BooleanLinkedOpenHashMap extends Char2BooleanOpenHashMap imple } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -1100,19 +1113,19 @@ public class Char2BooleanLinkedOpenHashMap extends Char2BooleanOpenHashMap imple state = operator.applyAsChar(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (char)0; + if(size() <= 0) return OptionalChar.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalChar.of(keys[index]); index = (int)links[index]; } - return (char)0; + return OptionalChar.empty(); } @Override @@ -1248,7 +1261,7 @@ public class Char2BooleanLinkedOpenHashMap extends Char2BooleanOpenHashMap imple } @Override - public boolean reduce(BooleanBooleanUnaryOperator operator) { + public OptionalBoolean reduce(BooleanBooleanUnaryOperator operator) { Objects.requireNonNull(operator); boolean state = false; boolean empty = true; @@ -1263,19 +1276,19 @@ public class Char2BooleanLinkedOpenHashMap extends Char2BooleanOpenHashMap imple state = operator.applyAsBoolean(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalBoolean.empty() : OptionalBoolean.of(state); } @Override - public boolean findFirst(BooleanPredicate filter) { + public OptionalBoolean findFirst(BooleanPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return false; + if(size() <= 0) return OptionalBoolean.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalBoolean.of(values[index]); index = (int)links[index]; } - return false; + return OptionalBoolean.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/chars/maps/impl/hash/Char2BooleanOpenHashMap.java b/src/main/java/speiger/src/collections/chars/maps/impl/hash/Char2BooleanOpenHashMap.java index 2c2a74c..3958ea1 100644 --- a/src/main/java/speiger/src/collections/chars/maps/impl/hash/Char2BooleanOpenHashMap.java +++ b/src/main/java/speiger/src/collections/chars/maps/impl/hash/Char2BooleanOpenHashMap.java @@ -5,6 +5,7 @@ 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; @@ -19,6 +20,7 @@ import speiger.src.collections.chars.functions.consumer.CharBooleanConsumer; import speiger.src.collections.chars.functions.function.CharPredicate; import speiger.src.collections.chars.functions.function.CharBooleanUnaryOperator; import speiger.src.collections.chars.functions.function.CharCharUnaryOperator; +import speiger.src.collections.chars.functions.OptionalChar; import speiger.src.collections.chars.maps.abstracts.AbstractChar2BooleanMap; import speiger.src.collections.chars.maps.interfaces.Char2BooleanMap; import speiger.src.collections.chars.sets.AbstractCharSet; @@ -33,6 +35,7 @@ import speiger.src.collections.booleans.functions.BooleanConsumer; import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectBooleanConsumer; import speiger.src.collections.booleans.functions.function.BooleanPredicate; +import speiger.src.collections.booleans.functions.OptionalBoolean; import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.sets.AbstractObjectSet; import speiger.src.collections.objects.sets.ObjectSet; @@ -898,7 +901,7 @@ public class Char2BooleanOpenHashMap extends AbstractChar2BooleanMap implements } @Override - public Char2BooleanMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Char2BooleanMap.Entry state = null; boolean empty = true; @@ -915,25 +918,25 @@ public class Char2BooleanOpenHashMap extends AbstractChar2BooleanMap implements } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Char2BooleanMap.Entry 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(keys[i] != (char)0) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1105,7 +1108,7 @@ public class Char2BooleanOpenHashMap extends AbstractChar2BooleanMap implements } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -1122,18 +1125,18 @@ public class Char2BooleanOpenHashMap extends AbstractChar2BooleanMap implements } state = operator.applyAsChar(state, keys[i]); } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (char)0; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OptionalChar.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OptionalChar.of(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(keys[i] != (char)0 && filter.test(keys[i])) return keys[i]; + if(keys[i] != (char)0 && filter.test(keys[i])) return OptionalChar.of(keys[i]); } - return (char)0; + return OptionalChar.empty(); } @Override @@ -1248,7 +1251,7 @@ public class Char2BooleanOpenHashMap extends AbstractChar2BooleanMap implements } @Override - public boolean reduce(BooleanBooleanUnaryOperator operator) { + public OptionalBoolean reduce(BooleanBooleanUnaryOperator operator) { Objects.requireNonNull(operator); boolean state = false; boolean empty = true; @@ -1265,18 +1268,18 @@ public class Char2BooleanOpenHashMap extends AbstractChar2BooleanMap implements } state = operator.applyAsBoolean(state, values[i]); } - return state; + return empty ? OptionalBoolean.empty() : OptionalBoolean.of(state); } @Override - public boolean findFirst(BooleanPredicate filter) { + public OptionalBoolean findFirst(BooleanPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return false; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return OptionalBoolean.empty(); + if(containsNull && filter.test(values[nullIndex])) return OptionalBoolean.of(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(keys[i] != (char)0 && filter.test(values[i])) return values[i]; + if(keys[i] != (char)0 && filter.test(values[i])) return OptionalBoolean.of(values[i]); } - return false; + return OptionalBoolean.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/chars/maps/impl/hash/Char2ByteLinkedOpenHashMap.java b/src/main/java/speiger/src/collections/chars/maps/impl/hash/Char2ByteLinkedOpenHashMap.java index 3a8560e..002dce4 100644 --- a/src/main/java/speiger/src/collections/chars/maps/impl/hash/Char2ByteLinkedOpenHashMap.java +++ b/src/main/java/speiger/src/collections/chars/maps/impl/hash/Char2ByteLinkedOpenHashMap.java @@ -4,6 +4,7 @@ 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; @@ -14,6 +15,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectCharConsumer; import speiger.src.collections.ints.functions.consumer.IntCharConsumer; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntByteConsumer; +import speiger.src.collections.chars.functions.OptionalChar; import speiger.src.collections.chars.functions.function.CharPredicate; import speiger.src.collections.chars.functions.consumer.CharByteConsumer; import speiger.src.collections.chars.functions.function.CharCharUnaryOperator; @@ -32,6 +34,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectByteConsumer; import speiger.src.collections.bytes.functions.function.BytePredicate; +import speiger.src.collections.bytes.functions.OptionalByte; import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOperator; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.objects.lists.ObjectListIterator; @@ -669,6 +672,10 @@ public class Char2ByteLinkedOpenHashMap extends Char2ByteOpenHashMap implements } private class MapEntrySet extends AbstractObjectSet implements Char2ByteOrderedMap.FastOrderedSet { + @Override + public void addFirst(Char2ByteMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Char2ByteMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Char2ByteMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -834,7 +841,7 @@ public class Char2ByteLinkedOpenHashMap extends Char2ByteOpenHashMap implements } @Override - public Char2ByteMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Char2ByteMap.Entry state = null; boolean empty = true; @@ -849,21 +856,21 @@ public class Char2ByteLinkedOpenHashMap extends Char2ByteOpenHashMap implements state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Char2ByteMap.Entry 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 @@ -939,6 +946,12 @@ public class Char2ByteLinkedOpenHashMap extends Char2ByteOpenHashMap implements @Override public boolean add(char o) { throw new UnsupportedOperationException(); } + + @Override + public void addFirst(char o) { throw new UnsupportedOperationException(); } + + @Override + public void addLast(char o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(char o) { throw new UnsupportedOperationException(); } @@ -1085,7 +1098,7 @@ public class Char2ByteLinkedOpenHashMap extends Char2ByteOpenHashMap implements } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -1100,19 +1113,19 @@ public class Char2ByteLinkedOpenHashMap extends Char2ByteOpenHashMap implements state = operator.applyAsChar(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (char)0; + if(size() <= 0) return OptionalChar.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalChar.of(keys[index]); index = (int)links[index]; } - return (char)0; + return OptionalChar.empty(); } @Override @@ -1248,7 +1261,7 @@ public class Char2ByteLinkedOpenHashMap extends Char2ByteOpenHashMap implements } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -1263,19 +1276,19 @@ public class Char2ByteLinkedOpenHashMap extends Char2ByteOpenHashMap implements state = operator.applyAsByte(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (byte)0; + if(size() <= 0) return OptionalByte.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalByte.of(values[index]); index = (int)links[index]; } - return (byte)0; + return OptionalByte.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/chars/maps/impl/hash/Char2ByteOpenHashMap.java b/src/main/java/speiger/src/collections/chars/maps/impl/hash/Char2ByteOpenHashMap.java index 0dc979b..3758e7a 100644 --- a/src/main/java/speiger/src/collections/chars/maps/impl/hash/Char2ByteOpenHashMap.java +++ b/src/main/java/speiger/src/collections/chars/maps/impl/hash/Char2ByteOpenHashMap.java @@ -5,6 +5,7 @@ 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; @@ -19,6 +20,7 @@ import speiger.src.collections.chars.functions.consumer.CharByteConsumer; import speiger.src.collections.chars.functions.function.Char2ByteFunction; import speiger.src.collections.chars.functions.function.CharByteUnaryOperator; import speiger.src.collections.chars.functions.function.CharCharUnaryOperator; +import speiger.src.collections.chars.functions.OptionalChar; import speiger.src.collections.chars.functions.function.CharPredicate; import speiger.src.collections.chars.maps.abstracts.AbstractChar2ByteMap; import speiger.src.collections.chars.maps.interfaces.Char2ByteMap; @@ -34,6 +36,7 @@ import speiger.src.collections.bytes.functions.ByteConsumer; import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectByteConsumer; import speiger.src.collections.bytes.functions.function.BytePredicate; +import speiger.src.collections.bytes.functions.OptionalByte; import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.sets.AbstractObjectSet; import speiger.src.collections.objects.sets.ObjectSet; @@ -920,7 +923,7 @@ public class Char2ByteOpenHashMap extends AbstractChar2ByteMap implements ITrimm } @Override - public Char2ByteMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Char2ByteMap.Entry state = null; boolean empty = true; @@ -937,25 +940,25 @@ public class Char2ByteOpenHashMap extends AbstractChar2ByteMap implements ITrimm } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Char2ByteMap.Entry 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(keys[i] != (char)0) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1127,7 +1130,7 @@ public class Char2ByteOpenHashMap extends AbstractChar2ByteMap implements ITrimm } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -1144,18 +1147,18 @@ public class Char2ByteOpenHashMap extends AbstractChar2ByteMap implements ITrimm } state = operator.applyAsChar(state, keys[i]); } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (char)0; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OptionalChar.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OptionalChar.of(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(keys[i] != (char)0 && filter.test(keys[i])) return keys[i]; + if(keys[i] != (char)0 && filter.test(keys[i])) return OptionalChar.of(keys[i]); } - return (char)0; + return OptionalChar.empty(); } @Override @@ -1270,7 +1273,7 @@ public class Char2ByteOpenHashMap extends AbstractChar2ByteMap implements ITrimm } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -1287,18 +1290,18 @@ public class Char2ByteOpenHashMap extends AbstractChar2ByteMap implements ITrimm } state = operator.applyAsByte(state, values[i]); } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (byte)0; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return OptionalByte.empty(); + if(containsNull && filter.test(values[nullIndex])) return OptionalByte.of(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(keys[i] != (char)0 && filter.test(values[i])) return values[i]; + if(keys[i] != (char)0 && filter.test(values[i])) return OptionalByte.of(values[i]); } - return (byte)0; + return OptionalByte.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/chars/maps/impl/hash/Char2CharLinkedOpenHashMap.java b/src/main/java/speiger/src/collections/chars/maps/impl/hash/Char2CharLinkedOpenHashMap.java index cac51a3..fb84e4a 100644 --- a/src/main/java/speiger/src/collections/chars/maps/impl/hash/Char2CharLinkedOpenHashMap.java +++ b/src/main/java/speiger/src/collections/chars/maps/impl/hash/Char2CharLinkedOpenHashMap.java @@ -4,6 +4,7 @@ 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; @@ -13,6 +14,7 @@ import speiger.src.collections.chars.functions.CharConsumer; import speiger.src.collections.objects.functions.consumer.ObjectCharConsumer; import speiger.src.collections.ints.functions.consumer.IntCharConsumer; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; +import speiger.src.collections.chars.functions.OptionalChar; import speiger.src.collections.chars.functions.function.CharPredicate; import speiger.src.collections.chars.functions.consumer.CharCharConsumer; import speiger.src.collections.chars.functions.function.CharCharUnaryOperator; @@ -662,6 +664,10 @@ public class Char2CharLinkedOpenHashMap extends Char2CharOpenHashMap implements } private class MapEntrySet extends AbstractObjectSet implements Char2CharOrderedMap.FastOrderedSet { + @Override + public void addFirst(Char2CharMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Char2CharMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Char2CharMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -827,7 +833,7 @@ public class Char2CharLinkedOpenHashMap extends Char2CharOpenHashMap implements } @Override - public Char2CharMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Char2CharMap.Entry state = null; boolean empty = true; @@ -842,21 +848,21 @@ public class Char2CharLinkedOpenHashMap extends Char2CharOpenHashMap implements state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Char2CharMap.Entry 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 @@ -932,6 +938,12 @@ public class Char2CharLinkedOpenHashMap extends Char2CharOpenHashMap implements @Override public boolean add(char o) { throw new UnsupportedOperationException(); } + + @Override + public void addFirst(char o) { throw new UnsupportedOperationException(); } + + @Override + public void addLast(char o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(char o) { throw new UnsupportedOperationException(); } @@ -1078,7 +1090,7 @@ public class Char2CharLinkedOpenHashMap extends Char2CharOpenHashMap implements } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -1093,19 +1105,19 @@ public class Char2CharLinkedOpenHashMap extends Char2CharOpenHashMap implements state = operator.applyAsChar(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (char)0; + if(size() <= 0) return OptionalChar.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalChar.of(keys[index]); index = (int)links[index]; } - return (char)0; + return OptionalChar.empty(); } @Override @@ -1241,7 +1253,7 @@ public class Char2CharLinkedOpenHashMap extends Char2CharOpenHashMap implements } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -1256,19 +1268,19 @@ public class Char2CharLinkedOpenHashMap extends Char2CharOpenHashMap implements state = operator.applyAsChar(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (char)0; + if(size() <= 0) return OptionalChar.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalChar.of(values[index]); index = (int)links[index]; } - return (char)0; + return OptionalChar.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/chars/maps/impl/hash/Char2CharOpenHashMap.java b/src/main/java/speiger/src/collections/chars/maps/impl/hash/Char2CharOpenHashMap.java index 0420262..d291c63 100644 --- a/src/main/java/speiger/src/collections/chars/maps/impl/hash/Char2CharOpenHashMap.java +++ b/src/main/java/speiger/src/collections/chars/maps/impl/hash/Char2CharOpenHashMap.java @@ -5,6 +5,7 @@ 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; @@ -17,6 +18,7 @@ import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.chars.functions.consumer.CharCharConsumer; import speiger.src.collections.chars.functions.function.CharUnaryOperator; import speiger.src.collections.chars.functions.function.CharCharUnaryOperator; +import speiger.src.collections.chars.functions.OptionalChar; import speiger.src.collections.chars.functions.function.CharPredicate; import speiger.src.collections.chars.maps.abstracts.AbstractChar2CharMap; import speiger.src.collections.chars.maps.interfaces.Char2CharMap; @@ -914,7 +916,7 @@ public class Char2CharOpenHashMap extends AbstractChar2CharMap implements ITrimm } @Override - public Char2CharMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Char2CharMap.Entry state = null; boolean empty = true; @@ -931,25 +933,25 @@ public class Char2CharOpenHashMap extends AbstractChar2CharMap implements ITrimm } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Char2CharMap.Entry 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(keys[i] != (char)0) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1121,7 +1123,7 @@ public class Char2CharOpenHashMap extends AbstractChar2CharMap implements ITrimm } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -1138,18 +1140,18 @@ public class Char2CharOpenHashMap extends AbstractChar2CharMap implements ITrimm } state = operator.applyAsChar(state, keys[i]); } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (char)0; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OptionalChar.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OptionalChar.of(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(keys[i] != (char)0 && filter.test(keys[i])) return keys[i]; + if(keys[i] != (char)0 && filter.test(keys[i])) return OptionalChar.of(keys[i]); } - return (char)0; + return OptionalChar.empty(); } @Override @@ -1264,7 +1266,7 @@ public class Char2CharOpenHashMap extends AbstractChar2CharMap implements ITrimm } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -1281,18 +1283,18 @@ public class Char2CharOpenHashMap extends AbstractChar2CharMap implements ITrimm } state = operator.applyAsChar(state, values[i]); } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (char)0; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return OptionalChar.empty(); + if(containsNull && filter.test(values[nullIndex])) return OptionalChar.of(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(keys[i] != (char)0 && filter.test(values[i])) return values[i]; + if(keys[i] != (char)0 && filter.test(values[i])) return OptionalChar.of(values[i]); } - return (char)0; + return OptionalChar.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/chars/maps/impl/hash/Char2DoubleLinkedOpenHashMap.java b/src/main/java/speiger/src/collections/chars/maps/impl/hash/Char2DoubleLinkedOpenHashMap.java index 5efd2bd..0619c44 100644 --- a/src/main/java/speiger/src/collections/chars/maps/impl/hash/Char2DoubleLinkedOpenHashMap.java +++ b/src/main/java/speiger/src/collections/chars/maps/impl/hash/Char2DoubleLinkedOpenHashMap.java @@ -4,10 +4,12 @@ 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; import java.util.function.DoublePredicate; +import java.util.OptionalDouble; import speiger.src.collections.chars.collections.CharBidirectionalIterator; import speiger.src.collections.chars.functions.CharConsumer; @@ -15,6 +17,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectCharConsumer; import speiger.src.collections.ints.functions.consumer.IntCharConsumer; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntDoubleConsumer; +import speiger.src.collections.chars.functions.OptionalChar; import speiger.src.collections.chars.functions.function.CharPredicate; import speiger.src.collections.chars.functions.consumer.CharDoubleConsumer; import speiger.src.collections.chars.functions.function.CharCharUnaryOperator; @@ -669,6 +672,10 @@ public class Char2DoubleLinkedOpenHashMap extends Char2DoubleOpenHashMap impleme } private class MapEntrySet extends AbstractObjectSet implements Char2DoubleOrderedMap.FastOrderedSet { + @Override + public void addFirst(Char2DoubleMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Char2DoubleMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Char2DoubleMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -834,7 +841,7 @@ public class Char2DoubleLinkedOpenHashMap extends Char2DoubleOpenHashMap impleme } @Override - public Char2DoubleMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Char2DoubleMap.Entry state = null; boolean empty = true; @@ -849,21 +856,21 @@ public class Char2DoubleLinkedOpenHashMap extends Char2DoubleOpenHashMap impleme state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Char2DoubleMap.Entry 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 @@ -939,6 +946,12 @@ public class Char2DoubleLinkedOpenHashMap extends Char2DoubleOpenHashMap impleme @Override public boolean add(char o) { throw new UnsupportedOperationException(); } + + @Override + public void addFirst(char o) { throw new UnsupportedOperationException(); } + + @Override + public void addLast(char o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(char o) { throw new UnsupportedOperationException(); } @@ -1085,7 +1098,7 @@ public class Char2DoubleLinkedOpenHashMap extends Char2DoubleOpenHashMap impleme } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -1100,19 +1113,19 @@ public class Char2DoubleLinkedOpenHashMap extends Char2DoubleOpenHashMap impleme state = operator.applyAsChar(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (char)0; + if(size() <= 0) return OptionalChar.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalChar.of(keys[index]); index = (int)links[index]; } - return (char)0; + return OptionalChar.empty(); } @Override @@ -1248,7 +1261,7 @@ public class Char2DoubleLinkedOpenHashMap extends Char2DoubleOpenHashMap impleme } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -1263,19 +1276,19 @@ public class Char2DoubleLinkedOpenHashMap extends Char2DoubleOpenHashMap impleme state = operator.applyAsDouble(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0D; + if(size() <= 0) return OptionalDouble.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalDouble.of(values[index]); index = (int)links[index]; } - return 0D; + return OptionalDouble.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/chars/maps/impl/hash/Char2DoubleOpenHashMap.java b/src/main/java/speiger/src/collections/chars/maps/impl/hash/Char2DoubleOpenHashMap.java index 7020e0f..00e2432 100644 --- a/src/main/java/speiger/src/collections/chars/maps/impl/hash/Char2DoubleOpenHashMap.java +++ b/src/main/java/speiger/src/collections/chars/maps/impl/hash/Char2DoubleOpenHashMap.java @@ -5,10 +5,12 @@ 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; import java.util.function.DoublePredicate; +import java.util.OptionalDouble; import speiger.src.collections.chars.collections.CharIterator; import speiger.src.collections.chars.functions.CharConsumer; @@ -20,6 +22,7 @@ import speiger.src.collections.chars.functions.consumer.CharDoubleConsumer; import speiger.src.collections.chars.functions.function.Char2DoubleFunction; import speiger.src.collections.chars.functions.function.CharDoubleUnaryOperator; import speiger.src.collections.chars.functions.function.CharCharUnaryOperator; +import speiger.src.collections.chars.functions.OptionalChar; import speiger.src.collections.chars.functions.function.CharPredicate; import speiger.src.collections.chars.maps.abstracts.AbstractChar2DoubleMap; import speiger.src.collections.chars.maps.interfaces.Char2DoubleMap; @@ -920,7 +923,7 @@ public class Char2DoubleOpenHashMap extends AbstractChar2DoubleMap implements IT } @Override - public Char2DoubleMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Char2DoubleMap.Entry state = null; boolean empty = true; @@ -937,25 +940,25 @@ public class Char2DoubleOpenHashMap extends AbstractChar2DoubleMap implements IT } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Char2DoubleMap.Entry 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(keys[i] != (char)0) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1127,7 +1130,7 @@ public class Char2DoubleOpenHashMap extends AbstractChar2DoubleMap implements IT } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -1144,18 +1147,18 @@ public class Char2DoubleOpenHashMap extends AbstractChar2DoubleMap implements IT } state = operator.applyAsChar(state, keys[i]); } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (char)0; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OptionalChar.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OptionalChar.of(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(keys[i] != (char)0 && filter.test(keys[i])) return keys[i]; + if(keys[i] != (char)0 && filter.test(keys[i])) return OptionalChar.of(keys[i]); } - return (char)0; + return OptionalChar.empty(); } @Override @@ -1270,7 +1273,7 @@ public class Char2DoubleOpenHashMap extends AbstractChar2DoubleMap implements IT } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -1287,18 +1290,18 @@ public class Char2DoubleOpenHashMap extends AbstractChar2DoubleMap implements IT } state = operator.applyAsDouble(state, values[i]); } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0D; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return OptionalDouble.empty(); + if(containsNull && filter.test(values[nullIndex])) return OptionalDouble.of(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(keys[i] != (char)0 && filter.test(values[i])) return values[i]; + if(keys[i] != (char)0 && filter.test(values[i])) return OptionalDouble.of(values[i]); } - return 0D; + return OptionalDouble.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/chars/maps/impl/hash/Char2FloatLinkedOpenHashMap.java b/src/main/java/speiger/src/collections/chars/maps/impl/hash/Char2FloatLinkedOpenHashMap.java index 9310199..97cc475 100644 --- a/src/main/java/speiger/src/collections/chars/maps/impl/hash/Char2FloatLinkedOpenHashMap.java +++ b/src/main/java/speiger/src/collections/chars/maps/impl/hash/Char2FloatLinkedOpenHashMap.java @@ -4,6 +4,7 @@ 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; @@ -14,6 +15,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectCharConsumer; import speiger.src.collections.ints.functions.consumer.IntCharConsumer; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntFloatConsumer; +import speiger.src.collections.chars.functions.OptionalChar; import speiger.src.collections.chars.functions.function.CharPredicate; import speiger.src.collections.chars.functions.consumer.CharFloatConsumer; import speiger.src.collections.chars.functions.function.CharCharUnaryOperator; @@ -32,6 +34,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectFloatConsumer; import speiger.src.collections.floats.functions.function.FloatPredicate; +import speiger.src.collections.floats.functions.OptionalFloat; import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOperator; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.objects.lists.ObjectListIterator; @@ -669,6 +672,10 @@ public class Char2FloatLinkedOpenHashMap extends Char2FloatOpenHashMap implement } private class MapEntrySet extends AbstractObjectSet implements Char2FloatOrderedMap.FastOrderedSet { + @Override + public void addFirst(Char2FloatMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Char2FloatMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Char2FloatMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -834,7 +841,7 @@ public class Char2FloatLinkedOpenHashMap extends Char2FloatOpenHashMap implement } @Override - public Char2FloatMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Char2FloatMap.Entry state = null; boolean empty = true; @@ -849,21 +856,21 @@ public class Char2FloatLinkedOpenHashMap extends Char2FloatOpenHashMap implement state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Char2FloatMap.Entry 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 @@ -939,6 +946,12 @@ public class Char2FloatLinkedOpenHashMap extends Char2FloatOpenHashMap implement @Override public boolean add(char o) { throw new UnsupportedOperationException(); } + + @Override + public void addFirst(char o) { throw new UnsupportedOperationException(); } + + @Override + public void addLast(char o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(char o) { throw new UnsupportedOperationException(); } @@ -1085,7 +1098,7 @@ public class Char2FloatLinkedOpenHashMap extends Char2FloatOpenHashMap implement } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -1100,19 +1113,19 @@ public class Char2FloatLinkedOpenHashMap extends Char2FloatOpenHashMap implement state = operator.applyAsChar(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (char)0; + if(size() <= 0) return OptionalChar.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalChar.of(keys[index]); index = (int)links[index]; } - return (char)0; + return OptionalChar.empty(); } @Override @@ -1248,7 +1261,7 @@ public class Char2FloatLinkedOpenHashMap extends Char2FloatOpenHashMap implement } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -1263,19 +1276,19 @@ public class Char2FloatLinkedOpenHashMap extends Char2FloatOpenHashMap implement state = operator.applyAsFloat(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0F; + if(size() <= 0) return OptionalFloat.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalFloat.of(values[index]); index = (int)links[index]; } - return 0F; + return OptionalFloat.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/chars/maps/impl/hash/Char2FloatOpenHashMap.java b/src/main/java/speiger/src/collections/chars/maps/impl/hash/Char2FloatOpenHashMap.java index 49b036d..abfa7f4 100644 --- a/src/main/java/speiger/src/collections/chars/maps/impl/hash/Char2FloatOpenHashMap.java +++ b/src/main/java/speiger/src/collections/chars/maps/impl/hash/Char2FloatOpenHashMap.java @@ -5,6 +5,7 @@ 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; @@ -19,6 +20,7 @@ import speiger.src.collections.chars.functions.consumer.CharFloatConsumer; import speiger.src.collections.chars.functions.function.Char2FloatFunction; import speiger.src.collections.chars.functions.function.CharFloatUnaryOperator; import speiger.src.collections.chars.functions.function.CharCharUnaryOperator; +import speiger.src.collections.chars.functions.OptionalChar; import speiger.src.collections.chars.functions.function.CharPredicate; import speiger.src.collections.chars.maps.abstracts.AbstractChar2FloatMap; import speiger.src.collections.chars.maps.interfaces.Char2FloatMap; @@ -34,6 +36,7 @@ import speiger.src.collections.floats.functions.FloatConsumer; import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectFloatConsumer; import speiger.src.collections.floats.functions.function.FloatPredicate; +import speiger.src.collections.floats.functions.OptionalFloat; import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.sets.AbstractObjectSet; import speiger.src.collections.objects.sets.ObjectSet; @@ -920,7 +923,7 @@ public class Char2FloatOpenHashMap extends AbstractChar2FloatMap implements ITri } @Override - public Char2FloatMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Char2FloatMap.Entry state = null; boolean empty = true; @@ -937,25 +940,25 @@ public class Char2FloatOpenHashMap extends AbstractChar2FloatMap implements ITri } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Char2FloatMap.Entry 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(keys[i] != (char)0) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1127,7 +1130,7 @@ public class Char2FloatOpenHashMap extends AbstractChar2FloatMap implements ITri } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -1144,18 +1147,18 @@ public class Char2FloatOpenHashMap extends AbstractChar2FloatMap implements ITri } state = operator.applyAsChar(state, keys[i]); } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (char)0; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OptionalChar.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OptionalChar.of(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(keys[i] != (char)0 && filter.test(keys[i])) return keys[i]; + if(keys[i] != (char)0 && filter.test(keys[i])) return OptionalChar.of(keys[i]); } - return (char)0; + return OptionalChar.empty(); } @Override @@ -1270,7 +1273,7 @@ public class Char2FloatOpenHashMap extends AbstractChar2FloatMap implements ITri } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -1287,18 +1290,18 @@ public class Char2FloatOpenHashMap extends AbstractChar2FloatMap implements ITri } state = operator.applyAsFloat(state, values[i]); } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0F; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return OptionalFloat.empty(); + if(containsNull && filter.test(values[nullIndex])) return OptionalFloat.of(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(keys[i] != (char)0 && filter.test(values[i])) return values[i]; + if(keys[i] != (char)0 && filter.test(values[i])) return OptionalFloat.of(values[i]); } - return 0F; + return OptionalFloat.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/chars/maps/impl/hash/Char2IntLinkedOpenHashMap.java b/src/main/java/speiger/src/collections/chars/maps/impl/hash/Char2IntLinkedOpenHashMap.java index 6552218..1938d3d 100644 --- a/src/main/java/speiger/src/collections/chars/maps/impl/hash/Char2IntLinkedOpenHashMap.java +++ b/src/main/java/speiger/src/collections/chars/maps/impl/hash/Char2IntLinkedOpenHashMap.java @@ -4,10 +4,12 @@ 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; import java.util.function.IntPredicate; +import java.util.OptionalInt; import speiger.src.collections.chars.collections.CharBidirectionalIterator; import speiger.src.collections.chars.functions.CharConsumer; @@ -15,6 +17,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectCharConsumer; import speiger.src.collections.ints.functions.consumer.IntCharConsumer; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntIntConsumer; +import speiger.src.collections.chars.functions.OptionalChar; import speiger.src.collections.chars.functions.function.CharPredicate; import speiger.src.collections.chars.functions.consumer.CharIntConsumer; import speiger.src.collections.chars.functions.function.CharCharUnaryOperator; @@ -669,6 +672,10 @@ public class Char2IntLinkedOpenHashMap extends Char2IntOpenHashMap implements Ch } private class MapEntrySet extends AbstractObjectSet implements Char2IntOrderedMap.FastOrderedSet { + @Override + public void addFirst(Char2IntMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Char2IntMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Char2IntMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -834,7 +841,7 @@ public class Char2IntLinkedOpenHashMap extends Char2IntOpenHashMap implements Ch } @Override - public Char2IntMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Char2IntMap.Entry state = null; boolean empty = true; @@ -849,21 +856,21 @@ public class Char2IntLinkedOpenHashMap extends Char2IntOpenHashMap implements Ch state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Char2IntMap.Entry 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 @@ -939,6 +946,12 @@ public class Char2IntLinkedOpenHashMap extends Char2IntOpenHashMap implements Ch @Override public boolean add(char o) { throw new UnsupportedOperationException(); } + + @Override + public void addFirst(char o) { throw new UnsupportedOperationException(); } + + @Override + public void addLast(char o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(char o) { throw new UnsupportedOperationException(); } @@ -1085,7 +1098,7 @@ public class Char2IntLinkedOpenHashMap extends Char2IntOpenHashMap implements Ch } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -1100,19 +1113,19 @@ public class Char2IntLinkedOpenHashMap extends Char2IntOpenHashMap implements Ch state = operator.applyAsChar(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (char)0; + if(size() <= 0) return OptionalChar.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalChar.of(keys[index]); index = (int)links[index]; } - return (char)0; + return OptionalChar.empty(); } @Override @@ -1248,7 +1261,7 @@ public class Char2IntLinkedOpenHashMap extends Char2IntOpenHashMap implements Ch } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -1263,19 +1276,19 @@ public class Char2IntLinkedOpenHashMap extends Char2IntOpenHashMap implements Ch state = operator.applyAsInt(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0; + if(size() <= 0) return OptionalInt.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalInt.of(values[index]); index = (int)links[index]; } - return 0; + return OptionalInt.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/chars/maps/impl/hash/Char2IntOpenHashMap.java b/src/main/java/speiger/src/collections/chars/maps/impl/hash/Char2IntOpenHashMap.java index eea889c..828315e 100644 --- a/src/main/java/speiger/src/collections/chars/maps/impl/hash/Char2IntOpenHashMap.java +++ b/src/main/java/speiger/src/collections/chars/maps/impl/hash/Char2IntOpenHashMap.java @@ -5,10 +5,12 @@ 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; import java.util.function.IntPredicate; +import java.util.OptionalInt; import speiger.src.collections.chars.collections.CharIterator; import speiger.src.collections.chars.functions.CharConsumer; @@ -20,6 +22,7 @@ import speiger.src.collections.chars.functions.consumer.CharIntConsumer; import speiger.src.collections.chars.functions.function.Char2IntFunction; import speiger.src.collections.chars.functions.function.CharIntUnaryOperator; import speiger.src.collections.chars.functions.function.CharCharUnaryOperator; +import speiger.src.collections.chars.functions.OptionalChar; import speiger.src.collections.chars.functions.function.CharPredicate; import speiger.src.collections.chars.maps.abstracts.AbstractChar2IntMap; import speiger.src.collections.chars.maps.interfaces.Char2IntMap; @@ -920,7 +923,7 @@ public class Char2IntOpenHashMap extends AbstractChar2IntMap implements ITrimmab } @Override - public Char2IntMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Char2IntMap.Entry state = null; boolean empty = true; @@ -937,25 +940,25 @@ public class Char2IntOpenHashMap extends AbstractChar2IntMap implements ITrimmab } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Char2IntMap.Entry 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(keys[i] != (char)0) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1127,7 +1130,7 @@ public class Char2IntOpenHashMap extends AbstractChar2IntMap implements ITrimmab } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -1144,18 +1147,18 @@ public class Char2IntOpenHashMap extends AbstractChar2IntMap implements ITrimmab } state = operator.applyAsChar(state, keys[i]); } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (char)0; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OptionalChar.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OptionalChar.of(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(keys[i] != (char)0 && filter.test(keys[i])) return keys[i]; + if(keys[i] != (char)0 && filter.test(keys[i])) return OptionalChar.of(keys[i]); } - return (char)0; + return OptionalChar.empty(); } @Override @@ -1270,7 +1273,7 @@ public class Char2IntOpenHashMap extends AbstractChar2IntMap implements ITrimmab } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -1287,18 +1290,18 @@ public class Char2IntOpenHashMap extends AbstractChar2IntMap implements ITrimmab } state = operator.applyAsInt(state, values[i]); } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return OptionalInt.empty(); + if(containsNull && filter.test(values[nullIndex])) return OptionalInt.of(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(keys[i] != (char)0 && filter.test(values[i])) return values[i]; + if(keys[i] != (char)0 && filter.test(values[i])) return OptionalInt.of(values[i]); } - return 0; + return OptionalInt.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/chars/maps/impl/hash/Char2LongLinkedOpenHashMap.java b/src/main/java/speiger/src/collections/chars/maps/impl/hash/Char2LongLinkedOpenHashMap.java index 361aef6..a842c96 100644 --- a/src/main/java/speiger/src/collections/chars/maps/impl/hash/Char2LongLinkedOpenHashMap.java +++ b/src/main/java/speiger/src/collections/chars/maps/impl/hash/Char2LongLinkedOpenHashMap.java @@ -4,10 +4,12 @@ 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; import java.util.function.LongPredicate; +import java.util.OptionalLong; import speiger.src.collections.chars.collections.CharBidirectionalIterator; import speiger.src.collections.chars.functions.CharConsumer; @@ -15,6 +17,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectCharConsumer; import speiger.src.collections.ints.functions.consumer.IntCharConsumer; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntLongConsumer; +import speiger.src.collections.chars.functions.OptionalChar; import speiger.src.collections.chars.functions.function.CharPredicate; import speiger.src.collections.chars.functions.consumer.CharLongConsumer; import speiger.src.collections.chars.functions.function.CharCharUnaryOperator; @@ -669,6 +672,10 @@ public class Char2LongLinkedOpenHashMap extends Char2LongOpenHashMap implements } private class MapEntrySet extends AbstractObjectSet implements Char2LongOrderedMap.FastOrderedSet { + @Override + public void addFirst(Char2LongMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Char2LongMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Char2LongMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -834,7 +841,7 @@ public class Char2LongLinkedOpenHashMap extends Char2LongOpenHashMap implements } @Override - public Char2LongMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Char2LongMap.Entry state = null; boolean empty = true; @@ -849,21 +856,21 @@ public class Char2LongLinkedOpenHashMap extends Char2LongOpenHashMap implements state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Char2LongMap.Entry 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 @@ -939,6 +946,12 @@ public class Char2LongLinkedOpenHashMap extends Char2LongOpenHashMap implements @Override public boolean add(char o) { throw new UnsupportedOperationException(); } + + @Override + public void addFirst(char o) { throw new UnsupportedOperationException(); } + + @Override + public void addLast(char o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(char o) { throw new UnsupportedOperationException(); } @@ -1085,7 +1098,7 @@ public class Char2LongLinkedOpenHashMap extends Char2LongOpenHashMap implements } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -1100,19 +1113,19 @@ public class Char2LongLinkedOpenHashMap extends Char2LongOpenHashMap implements state = operator.applyAsChar(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (char)0; + if(size() <= 0) return OptionalChar.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalChar.of(keys[index]); index = (int)links[index]; } - return (char)0; + return OptionalChar.empty(); } @Override @@ -1248,7 +1261,7 @@ public class Char2LongLinkedOpenHashMap extends Char2LongOpenHashMap implements } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -1263,19 +1276,19 @@ public class Char2LongLinkedOpenHashMap extends Char2LongOpenHashMap implements state = operator.applyAsLong(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0L; + if(size() <= 0) return OptionalLong.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalLong.of(values[index]); index = (int)links[index]; } - return 0L; + return OptionalLong.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/chars/maps/impl/hash/Char2LongOpenHashMap.java b/src/main/java/speiger/src/collections/chars/maps/impl/hash/Char2LongOpenHashMap.java index 52348d1..7f3229f 100644 --- a/src/main/java/speiger/src/collections/chars/maps/impl/hash/Char2LongOpenHashMap.java +++ b/src/main/java/speiger/src/collections/chars/maps/impl/hash/Char2LongOpenHashMap.java @@ -5,10 +5,12 @@ 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; import java.util.function.LongPredicate; +import java.util.OptionalLong; import speiger.src.collections.chars.collections.CharIterator; import speiger.src.collections.chars.functions.CharConsumer; @@ -20,6 +22,7 @@ import speiger.src.collections.chars.functions.consumer.CharLongConsumer; import speiger.src.collections.chars.functions.function.Char2LongFunction; import speiger.src.collections.chars.functions.function.CharLongUnaryOperator; import speiger.src.collections.chars.functions.function.CharCharUnaryOperator; +import speiger.src.collections.chars.functions.OptionalChar; import speiger.src.collections.chars.functions.function.CharPredicate; import speiger.src.collections.chars.maps.abstracts.AbstractChar2LongMap; import speiger.src.collections.chars.maps.interfaces.Char2LongMap; @@ -920,7 +923,7 @@ public class Char2LongOpenHashMap extends AbstractChar2LongMap implements ITrimm } @Override - public Char2LongMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Char2LongMap.Entry state = null; boolean empty = true; @@ -937,25 +940,25 @@ public class Char2LongOpenHashMap extends AbstractChar2LongMap implements ITrimm } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Char2LongMap.Entry 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(keys[i] != (char)0) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1127,7 +1130,7 @@ public class Char2LongOpenHashMap extends AbstractChar2LongMap implements ITrimm } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -1144,18 +1147,18 @@ public class Char2LongOpenHashMap extends AbstractChar2LongMap implements ITrimm } state = operator.applyAsChar(state, keys[i]); } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (char)0; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OptionalChar.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OptionalChar.of(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(keys[i] != (char)0 && filter.test(keys[i])) return keys[i]; + if(keys[i] != (char)0 && filter.test(keys[i])) return OptionalChar.of(keys[i]); } - return (char)0; + return OptionalChar.empty(); } @Override @@ -1270,7 +1273,7 @@ public class Char2LongOpenHashMap extends AbstractChar2LongMap implements ITrimm } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -1287,18 +1290,18 @@ public class Char2LongOpenHashMap extends AbstractChar2LongMap implements ITrimm } state = operator.applyAsLong(state, values[i]); } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0L; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return OptionalLong.empty(); + if(containsNull && filter.test(values[nullIndex])) return OptionalLong.of(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(keys[i] != (char)0 && filter.test(values[i])) return values[i]; + if(keys[i] != (char)0 && filter.test(values[i])) return OptionalLong.of(values[i]); } - return 0L; + return OptionalLong.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/chars/maps/impl/hash/Char2ObjectLinkedOpenHashMap.java b/src/main/java/speiger/src/collections/chars/maps/impl/hash/Char2ObjectLinkedOpenHashMap.java index 9c3fba1..050286d 100644 --- a/src/main/java/speiger/src/collections/chars/maps/impl/hash/Char2ObjectLinkedOpenHashMap.java +++ b/src/main/java/speiger/src/collections/chars/maps/impl/hash/Char2ObjectLinkedOpenHashMap.java @@ -4,6 +4,7 @@ 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; @@ -13,6 +14,7 @@ import speiger.src.collections.chars.functions.CharConsumer; import speiger.src.collections.objects.functions.consumer.ObjectCharConsumer; import speiger.src.collections.ints.functions.consumer.IntCharConsumer; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; +import speiger.src.collections.chars.functions.OptionalChar; import speiger.src.collections.chars.functions.function.CharPredicate; import speiger.src.collections.chars.functions.consumer.CharObjectConsumer; import speiger.src.collections.chars.functions.function.CharCharUnaryOperator; @@ -653,6 +655,10 @@ public class Char2ObjectLinkedOpenHashMap extends Char2ObjectOpenHashMap i } private class MapEntrySet extends AbstractObjectSet> implements Char2ObjectOrderedMap.FastOrderedSet { + @Override + public void addFirst(Char2ObjectMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Char2ObjectMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Char2ObjectMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -818,7 +824,7 @@ public class Char2ObjectLinkedOpenHashMap extends Char2ObjectOpenHashMap i } @Override - public Char2ObjectMap.Entry reduce(ObjectObjectUnaryOperator, Char2ObjectMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Char2ObjectMap.Entry> operator) { Objects.requireNonNull(operator); Char2ObjectMap.Entry state = null; boolean empty = true; @@ -833,21 +839,21 @@ public class Char2ObjectLinkedOpenHashMap extends Char2ObjectOpenHashMap i state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Char2ObjectMap.Entry 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 @@ -923,6 +929,12 @@ public class Char2ObjectLinkedOpenHashMap extends Char2ObjectOpenHashMap i @Override public boolean add(char o) { throw new UnsupportedOperationException(); } + + @Override + public void addFirst(char o) { throw new UnsupportedOperationException(); } + + @Override + public void addLast(char o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(char o) { throw new UnsupportedOperationException(); } @@ -1069,7 +1081,7 @@ public class Char2ObjectLinkedOpenHashMap extends Char2ObjectOpenHashMap i } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -1084,19 +1096,19 @@ public class Char2ObjectLinkedOpenHashMap extends Char2ObjectOpenHashMap i state = operator.applyAsChar(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (char)0; + if(size() <= 0) return OptionalChar.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalChar.of(keys[index]); index = (int)links[index]; } - return (char)0; + return OptionalChar.empty(); } @Override @@ -1233,7 +1245,7 @@ public class Char2ObjectLinkedOpenHashMap extends Char2ObjectOpenHashMap i } @Override - public V reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); V state = null; boolean empty = true; @@ -1248,19 +1260,19 @@ public class Char2ObjectLinkedOpenHashMap extends Char2ObjectOpenHashMap i state = operator.apply(state, values[index]); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public V findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return Optional.ofNullable(values[index]); index = (int)links[index]; } - return null; + return Optional.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/chars/maps/impl/hash/Char2ObjectOpenHashMap.java b/src/main/java/speiger/src/collections/chars/maps/impl/hash/Char2ObjectOpenHashMap.java index 5b2ea01..42c176a 100644 --- a/src/main/java/speiger/src/collections/chars/maps/impl/hash/Char2ObjectOpenHashMap.java +++ b/src/main/java/speiger/src/collections/chars/maps/impl/hash/Char2ObjectOpenHashMap.java @@ -5,6 +5,7 @@ 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; @@ -18,6 +19,7 @@ import speiger.src.collections.chars.functions.consumer.CharObjectConsumer; import speiger.src.collections.chars.functions.function.CharFunction; import speiger.src.collections.chars.functions.function.CharObjectUnaryOperator; import speiger.src.collections.chars.functions.function.CharCharUnaryOperator; +import speiger.src.collections.chars.functions.OptionalChar; import speiger.src.collections.chars.functions.function.CharPredicate; import speiger.src.collections.chars.maps.abstracts.AbstractChar2ObjectMap; import speiger.src.collections.chars.maps.interfaces.Char2ObjectMap; @@ -837,7 +839,7 @@ public class Char2ObjectOpenHashMap extends AbstractChar2ObjectMap impleme } @Override - public Char2ObjectMap.Entry reduce(ObjectObjectUnaryOperator, Char2ObjectMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Char2ObjectMap.Entry> operator) { Objects.requireNonNull(operator); Char2ObjectMap.Entry state = null; boolean empty = true; @@ -854,25 +856,25 @@ public class Char2ObjectOpenHashMap extends AbstractChar2ObjectMap impleme } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Char2ObjectMap.Entry 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(keys[i] != (char)0) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1044,7 +1046,7 @@ public class Char2ObjectOpenHashMap extends AbstractChar2ObjectMap impleme } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -1061,18 +1063,18 @@ public class Char2ObjectOpenHashMap extends AbstractChar2ObjectMap impleme } state = operator.applyAsChar(state, keys[i]); } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (char)0; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OptionalChar.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OptionalChar.of(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(keys[i] != (char)0 && filter.test(keys[i])) return keys[i]; + if(keys[i] != (char)0 && filter.test(keys[i])) return OptionalChar.of(keys[i]); } - return (char)0; + return OptionalChar.empty(); } @Override @@ -1187,7 +1189,7 @@ public class Char2ObjectOpenHashMap extends AbstractChar2ObjectMap impleme } @Override - public V reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); V state = null; boolean empty = true; @@ -1204,18 +1206,18 @@ public class Char2ObjectOpenHashMap extends AbstractChar2ObjectMap impleme } state = operator.apply(state, values[i]); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public V findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return Optional.empty(); + if(containsNull && filter.test(values[nullIndex])) return Optional.ofNullable(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(keys[i] != (char)0 && filter.test(values[i])) return values[i]; + if(keys[i] != (char)0 && filter.test(values[i])) return Optional.ofNullable(values[i]); } - return null; + return Optional.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/chars/maps/impl/hash/Char2ShortLinkedOpenHashMap.java b/src/main/java/speiger/src/collections/chars/maps/impl/hash/Char2ShortLinkedOpenHashMap.java index 5ddde8b..bd3dfa2 100644 --- a/src/main/java/speiger/src/collections/chars/maps/impl/hash/Char2ShortLinkedOpenHashMap.java +++ b/src/main/java/speiger/src/collections/chars/maps/impl/hash/Char2ShortLinkedOpenHashMap.java @@ -4,6 +4,7 @@ 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; @@ -14,6 +15,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectCharConsumer; import speiger.src.collections.ints.functions.consumer.IntCharConsumer; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntShortConsumer; +import speiger.src.collections.chars.functions.OptionalChar; import speiger.src.collections.chars.functions.function.CharPredicate; import speiger.src.collections.chars.functions.consumer.CharShortConsumer; import speiger.src.collections.chars.functions.function.CharCharUnaryOperator; @@ -32,6 +34,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectShortConsumer; import speiger.src.collections.shorts.functions.function.ShortPredicate; +import speiger.src.collections.shorts.functions.OptionalShort; import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOperator; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.objects.lists.ObjectListIterator; @@ -669,6 +672,10 @@ public class Char2ShortLinkedOpenHashMap extends Char2ShortOpenHashMap implement } private class MapEntrySet extends AbstractObjectSet implements Char2ShortOrderedMap.FastOrderedSet { + @Override + public void addFirst(Char2ShortMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Char2ShortMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Char2ShortMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -834,7 +841,7 @@ public class Char2ShortLinkedOpenHashMap extends Char2ShortOpenHashMap implement } @Override - public Char2ShortMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Char2ShortMap.Entry state = null; boolean empty = true; @@ -849,21 +856,21 @@ public class Char2ShortLinkedOpenHashMap extends Char2ShortOpenHashMap implement state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Char2ShortMap.Entry 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 @@ -939,6 +946,12 @@ public class Char2ShortLinkedOpenHashMap extends Char2ShortOpenHashMap implement @Override public boolean add(char o) { throw new UnsupportedOperationException(); } + + @Override + public void addFirst(char o) { throw new UnsupportedOperationException(); } + + @Override + public void addLast(char o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(char o) { throw new UnsupportedOperationException(); } @@ -1085,7 +1098,7 @@ public class Char2ShortLinkedOpenHashMap extends Char2ShortOpenHashMap implement } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -1100,19 +1113,19 @@ public class Char2ShortLinkedOpenHashMap extends Char2ShortOpenHashMap implement state = operator.applyAsChar(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (char)0; + if(size() <= 0) return OptionalChar.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalChar.of(keys[index]); index = (int)links[index]; } - return (char)0; + return OptionalChar.empty(); } @Override @@ -1248,7 +1261,7 @@ public class Char2ShortLinkedOpenHashMap extends Char2ShortOpenHashMap implement } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -1263,19 +1276,19 @@ public class Char2ShortLinkedOpenHashMap extends Char2ShortOpenHashMap implement state = operator.applyAsShort(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (short)0; + if(size() <= 0) return OptionalShort.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalShort.of(values[index]); index = (int)links[index]; } - return (short)0; + return OptionalShort.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/chars/maps/impl/hash/Char2ShortOpenHashMap.java b/src/main/java/speiger/src/collections/chars/maps/impl/hash/Char2ShortOpenHashMap.java index 2667446..57d2cda 100644 --- a/src/main/java/speiger/src/collections/chars/maps/impl/hash/Char2ShortOpenHashMap.java +++ b/src/main/java/speiger/src/collections/chars/maps/impl/hash/Char2ShortOpenHashMap.java @@ -5,6 +5,7 @@ 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; @@ -19,6 +20,7 @@ import speiger.src.collections.chars.functions.consumer.CharShortConsumer; import speiger.src.collections.chars.functions.function.Char2ShortFunction; import speiger.src.collections.chars.functions.function.CharShortUnaryOperator; import speiger.src.collections.chars.functions.function.CharCharUnaryOperator; +import speiger.src.collections.chars.functions.OptionalChar; import speiger.src.collections.chars.functions.function.CharPredicate; import speiger.src.collections.chars.maps.abstracts.AbstractChar2ShortMap; import speiger.src.collections.chars.maps.interfaces.Char2ShortMap; @@ -34,6 +36,7 @@ import speiger.src.collections.shorts.functions.ShortConsumer; import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectShortConsumer; import speiger.src.collections.shorts.functions.function.ShortPredicate; +import speiger.src.collections.shorts.functions.OptionalShort; import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.sets.AbstractObjectSet; import speiger.src.collections.objects.sets.ObjectSet; @@ -920,7 +923,7 @@ public class Char2ShortOpenHashMap extends AbstractChar2ShortMap implements ITri } @Override - public Char2ShortMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Char2ShortMap.Entry state = null; boolean empty = true; @@ -937,25 +940,25 @@ public class Char2ShortOpenHashMap extends AbstractChar2ShortMap implements ITri } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Char2ShortMap.Entry 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(keys[i] != (char)0) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1127,7 +1130,7 @@ public class Char2ShortOpenHashMap extends AbstractChar2ShortMap implements ITri } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -1144,18 +1147,18 @@ public class Char2ShortOpenHashMap extends AbstractChar2ShortMap implements ITri } state = operator.applyAsChar(state, keys[i]); } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (char)0; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OptionalChar.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OptionalChar.of(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(keys[i] != (char)0 && filter.test(keys[i])) return keys[i]; + if(keys[i] != (char)0 && filter.test(keys[i])) return OptionalChar.of(keys[i]); } - return (char)0; + return OptionalChar.empty(); } @Override @@ -1270,7 +1273,7 @@ public class Char2ShortOpenHashMap extends AbstractChar2ShortMap implements ITri } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -1287,18 +1290,18 @@ public class Char2ShortOpenHashMap extends AbstractChar2ShortMap implements ITri } state = operator.applyAsShort(state, values[i]); } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (short)0; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return OptionalShort.empty(); + if(containsNull && filter.test(values[nullIndex])) return OptionalShort.of(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(keys[i] != (char)0 && filter.test(values[i])) return values[i]; + if(keys[i] != (char)0 && filter.test(values[i])) return OptionalShort.of(values[i]); } - return (short)0; + return OptionalShort.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/chars/maps/impl/immutable/ImmutableChar2BooleanOpenHashMap.java b/src/main/java/speiger/src/collections/chars/maps/impl/immutable/ImmutableChar2BooleanOpenHashMap.java index 1f06c0f..c555044 100644 --- a/src/main/java/speiger/src/collections/chars/maps/impl/immutable/ImmutableChar2BooleanOpenHashMap.java +++ b/src/main/java/speiger/src/collections/chars/maps/impl/immutable/ImmutableChar2BooleanOpenHashMap.java @@ -4,6 +4,7 @@ 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; @@ -14,6 +15,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectCharConsumer; import speiger.src.collections.ints.functions.consumer.IntCharConsumer; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntBooleanConsumer; +import speiger.src.collections.chars.functions.OptionalChar; import speiger.src.collections.chars.functions.consumer.CharBooleanConsumer; import speiger.src.collections.chars.functions.function.CharPredicate; import speiger.src.collections.chars.functions.function.CharBooleanUnaryOperator; @@ -28,6 +30,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectBooleanConsumer; import speiger.src.collections.booleans.functions.function.BooleanPredicate; +import speiger.src.collections.booleans.functions.OptionalBoolean; import speiger.src.collections.chars.sets.AbstractCharSet; import speiger.src.collections.booleans.collections.AbstractBooleanCollection; import speiger.src.collections.booleans.collections.BooleanOrderedCollection; @@ -533,6 +536,10 @@ public class ImmutableChar2BooleanOpenHashMap extends AbstractChar2BooleanMap im } private class MapEntrySet extends AbstractObjectSet implements Char2BooleanOrderedMap.FastOrderedSet { + @Override + public void addFirst(Char2BooleanMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Char2BooleanMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Char2BooleanMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -686,7 +693,7 @@ public class ImmutableChar2BooleanOpenHashMap extends AbstractChar2BooleanMap im } @Override - public Char2BooleanMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Char2BooleanMap.Entry state = null; boolean empty = true; @@ -701,21 +708,21 @@ public class ImmutableChar2BooleanOpenHashMap extends AbstractChar2BooleanMap im state = operator.apply(state, new BasicEntry(keys[index], values[index])); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Char2BooleanMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry entry = new BasicEntry(); 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 @@ -778,6 +785,12 @@ public class ImmutableChar2BooleanOpenHashMap extends AbstractChar2BooleanMap im throw new UnsupportedOperationException(); } + @Override + public void addFirst(char o) { throw new UnsupportedOperationException(); } + + @Override + public void addLast(char o) { throw new UnsupportedOperationException(); } + @Override public boolean addAndMoveToFirst(char o) { throw new UnsupportedOperationException(); } @@ -913,7 +926,7 @@ public class ImmutableChar2BooleanOpenHashMap extends AbstractChar2BooleanMap im } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -928,19 +941,19 @@ public class ImmutableChar2BooleanOpenHashMap extends AbstractChar2BooleanMap im state = operator.applyAsChar(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (char)0; + if(size() <= 0) return OptionalChar.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalChar.of(keys[index]); index = (int)links[index]; } - return (char)0; + return OptionalChar.empty(); } @Override @@ -1067,7 +1080,7 @@ public class ImmutableChar2BooleanOpenHashMap extends AbstractChar2BooleanMap im } @Override - public boolean reduce(BooleanBooleanUnaryOperator operator) { + public OptionalBoolean reduce(BooleanBooleanUnaryOperator operator) { Objects.requireNonNull(operator); boolean state = false; boolean empty = true; @@ -1082,19 +1095,19 @@ public class ImmutableChar2BooleanOpenHashMap extends AbstractChar2BooleanMap im state = operator.applyAsBoolean(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalBoolean.empty() : OptionalBoolean.of(state); } @Override - public boolean findFirst(BooleanPredicate filter) { + public OptionalBoolean findFirst(BooleanPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return false; + if(size() <= 0) return OptionalBoolean.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalBoolean.of(values[index]); index = (int)links[index]; } - return false; + return OptionalBoolean.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/chars/maps/impl/immutable/ImmutableChar2ByteOpenHashMap.java b/src/main/java/speiger/src/collections/chars/maps/impl/immutable/ImmutableChar2ByteOpenHashMap.java index 8f04fa8..eff3e19 100644 --- a/src/main/java/speiger/src/collections/chars/maps/impl/immutable/ImmutableChar2ByteOpenHashMap.java +++ b/src/main/java/speiger/src/collections/chars/maps/impl/immutable/ImmutableChar2ByteOpenHashMap.java @@ -4,6 +4,7 @@ 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; @@ -14,6 +15,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectCharConsumer; import speiger.src.collections.ints.functions.consumer.IntCharConsumer; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntByteConsumer; +import speiger.src.collections.chars.functions.OptionalChar; import speiger.src.collections.chars.functions.function.CharPredicate; import speiger.src.collections.chars.functions.consumer.CharByteConsumer; import speiger.src.collections.chars.functions.function.Char2ByteFunction; @@ -29,6 +31,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectByteConsumer; import speiger.src.collections.bytes.functions.function.BytePredicate; +import speiger.src.collections.bytes.functions.OptionalByte; import speiger.src.collections.chars.sets.AbstractCharSet; import speiger.src.collections.bytes.collections.AbstractByteCollection; import speiger.src.collections.bytes.collections.ByteOrderedCollection; @@ -538,6 +541,10 @@ public class ImmutableChar2ByteOpenHashMap extends AbstractChar2ByteMap implemen } private class MapEntrySet extends AbstractObjectSet implements Char2ByteOrderedMap.FastOrderedSet { + @Override + public void addFirst(Char2ByteMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Char2ByteMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Char2ByteMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -691,7 +698,7 @@ public class ImmutableChar2ByteOpenHashMap extends AbstractChar2ByteMap implemen } @Override - public Char2ByteMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Char2ByteMap.Entry state = null; boolean empty = true; @@ -706,21 +713,21 @@ public class ImmutableChar2ByteOpenHashMap extends AbstractChar2ByteMap implemen state = operator.apply(state, new BasicEntry(keys[index], values[index])); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Char2ByteMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry entry = new BasicEntry(); 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 @@ -783,6 +790,12 @@ public class ImmutableChar2ByteOpenHashMap extends AbstractChar2ByteMap implemen throw new UnsupportedOperationException(); } + @Override + public void addFirst(char o) { throw new UnsupportedOperationException(); } + + @Override + public void addLast(char o) { throw new UnsupportedOperationException(); } + @Override public boolean addAndMoveToFirst(char o) { throw new UnsupportedOperationException(); } @@ -918,7 +931,7 @@ public class ImmutableChar2ByteOpenHashMap extends AbstractChar2ByteMap implemen } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -933,19 +946,19 @@ public class ImmutableChar2ByteOpenHashMap extends AbstractChar2ByteMap implemen state = operator.applyAsChar(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (char)0; + if(size() <= 0) return OptionalChar.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalChar.of(keys[index]); index = (int)links[index]; } - return (char)0; + return OptionalChar.empty(); } @Override @@ -1072,7 +1085,7 @@ public class ImmutableChar2ByteOpenHashMap extends AbstractChar2ByteMap implemen } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -1087,19 +1100,19 @@ public class ImmutableChar2ByteOpenHashMap extends AbstractChar2ByteMap implemen state = operator.applyAsByte(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (byte)0; + if(size() <= 0) return OptionalByte.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalByte.of(values[index]); index = (int)links[index]; } - return (byte)0; + return OptionalByte.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/chars/maps/impl/immutable/ImmutableChar2CharOpenHashMap.java b/src/main/java/speiger/src/collections/chars/maps/impl/immutable/ImmutableChar2CharOpenHashMap.java index 31dd939..cc24729 100644 --- a/src/main/java/speiger/src/collections/chars/maps/impl/immutable/ImmutableChar2CharOpenHashMap.java +++ b/src/main/java/speiger/src/collections/chars/maps/impl/immutable/ImmutableChar2CharOpenHashMap.java @@ -4,6 +4,7 @@ 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; @@ -13,6 +14,7 @@ import speiger.src.collections.chars.functions.CharConsumer; import speiger.src.collections.objects.functions.consumer.ObjectCharConsumer; import speiger.src.collections.ints.functions.consumer.IntCharConsumer; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; +import speiger.src.collections.chars.functions.OptionalChar; import speiger.src.collections.chars.functions.function.CharPredicate; import speiger.src.collections.chars.functions.consumer.CharCharConsumer; import speiger.src.collections.chars.functions.function.CharUnaryOperator; @@ -529,6 +531,10 @@ public class ImmutableChar2CharOpenHashMap extends AbstractChar2CharMap implemen } private class MapEntrySet extends AbstractObjectSet implements Char2CharOrderedMap.FastOrderedSet { + @Override + public void addFirst(Char2CharMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Char2CharMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Char2CharMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -682,7 +688,7 @@ public class ImmutableChar2CharOpenHashMap extends AbstractChar2CharMap implemen } @Override - public Char2CharMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Char2CharMap.Entry state = null; boolean empty = true; @@ -697,21 +703,21 @@ public class ImmutableChar2CharOpenHashMap extends AbstractChar2CharMap implemen state = operator.apply(state, new BasicEntry(keys[index], values[index])); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Char2CharMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry entry = new BasicEntry(); 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 @@ -774,6 +780,12 @@ public class ImmutableChar2CharOpenHashMap extends AbstractChar2CharMap implemen throw new UnsupportedOperationException(); } + @Override + public void addFirst(char o) { throw new UnsupportedOperationException(); } + + @Override + public void addLast(char o) { throw new UnsupportedOperationException(); } + @Override public boolean addAndMoveToFirst(char o) { throw new UnsupportedOperationException(); } @@ -909,7 +921,7 @@ public class ImmutableChar2CharOpenHashMap extends AbstractChar2CharMap implemen } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -924,19 +936,19 @@ public class ImmutableChar2CharOpenHashMap extends AbstractChar2CharMap implemen state = operator.applyAsChar(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (char)0; + if(size() <= 0) return OptionalChar.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalChar.of(keys[index]); index = (int)links[index]; } - return (char)0; + return OptionalChar.empty(); } @Override @@ -1063,7 +1075,7 @@ public class ImmutableChar2CharOpenHashMap extends AbstractChar2CharMap implemen } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -1078,19 +1090,19 @@ public class ImmutableChar2CharOpenHashMap extends AbstractChar2CharMap implemen state = operator.applyAsChar(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (char)0; + if(size() <= 0) return OptionalChar.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalChar.of(values[index]); index = (int)links[index]; } - return (char)0; + return OptionalChar.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/chars/maps/impl/immutable/ImmutableChar2DoubleOpenHashMap.java b/src/main/java/speiger/src/collections/chars/maps/impl/immutable/ImmutableChar2DoubleOpenHashMap.java index 53c6719..71ba4d7 100644 --- a/src/main/java/speiger/src/collections/chars/maps/impl/immutable/ImmutableChar2DoubleOpenHashMap.java +++ b/src/main/java/speiger/src/collections/chars/maps/impl/immutable/ImmutableChar2DoubleOpenHashMap.java @@ -4,10 +4,12 @@ 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; import java.util.function.DoublePredicate; +import java.util.OptionalDouble; import speiger.src.collections.chars.collections.CharBidirectionalIterator; import speiger.src.collections.chars.functions.CharConsumer; @@ -15,6 +17,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectCharConsumer; import speiger.src.collections.ints.functions.consumer.IntCharConsumer; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntDoubleConsumer; +import speiger.src.collections.chars.functions.OptionalChar; import speiger.src.collections.chars.functions.function.CharPredicate; import speiger.src.collections.chars.functions.consumer.CharDoubleConsumer; import speiger.src.collections.chars.functions.function.Char2DoubleFunction; @@ -538,6 +541,10 @@ public class ImmutableChar2DoubleOpenHashMap extends AbstractChar2DoubleMap impl } private class MapEntrySet extends AbstractObjectSet implements Char2DoubleOrderedMap.FastOrderedSet { + @Override + public void addFirst(Char2DoubleMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Char2DoubleMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Char2DoubleMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -691,7 +698,7 @@ public class ImmutableChar2DoubleOpenHashMap extends AbstractChar2DoubleMap impl } @Override - public Char2DoubleMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Char2DoubleMap.Entry state = null; boolean empty = true; @@ -706,21 +713,21 @@ public class ImmutableChar2DoubleOpenHashMap extends AbstractChar2DoubleMap impl state = operator.apply(state, new BasicEntry(keys[index], values[index])); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Char2DoubleMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry entry = new BasicEntry(); 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 @@ -783,6 +790,12 @@ public class ImmutableChar2DoubleOpenHashMap extends AbstractChar2DoubleMap impl throw new UnsupportedOperationException(); } + @Override + public void addFirst(char o) { throw new UnsupportedOperationException(); } + + @Override + public void addLast(char o) { throw new UnsupportedOperationException(); } + @Override public boolean addAndMoveToFirst(char o) { throw new UnsupportedOperationException(); } @@ -918,7 +931,7 @@ public class ImmutableChar2DoubleOpenHashMap extends AbstractChar2DoubleMap impl } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -933,19 +946,19 @@ public class ImmutableChar2DoubleOpenHashMap extends AbstractChar2DoubleMap impl state = operator.applyAsChar(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (char)0; + if(size() <= 0) return OptionalChar.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalChar.of(keys[index]); index = (int)links[index]; } - return (char)0; + return OptionalChar.empty(); } @Override @@ -1072,7 +1085,7 @@ public class ImmutableChar2DoubleOpenHashMap extends AbstractChar2DoubleMap impl } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -1087,19 +1100,19 @@ public class ImmutableChar2DoubleOpenHashMap extends AbstractChar2DoubleMap impl state = operator.applyAsDouble(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0D; + if(size() <= 0) return OptionalDouble.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalDouble.of(values[index]); index = (int)links[index]; } - return 0D; + return OptionalDouble.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/chars/maps/impl/immutable/ImmutableChar2FloatOpenHashMap.java b/src/main/java/speiger/src/collections/chars/maps/impl/immutable/ImmutableChar2FloatOpenHashMap.java index 70d04f6..7355d14 100644 --- a/src/main/java/speiger/src/collections/chars/maps/impl/immutable/ImmutableChar2FloatOpenHashMap.java +++ b/src/main/java/speiger/src/collections/chars/maps/impl/immutable/ImmutableChar2FloatOpenHashMap.java @@ -4,6 +4,7 @@ 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; @@ -14,6 +15,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectCharConsumer; import speiger.src.collections.ints.functions.consumer.IntCharConsumer; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntFloatConsumer; +import speiger.src.collections.chars.functions.OptionalChar; import speiger.src.collections.chars.functions.function.CharPredicate; import speiger.src.collections.chars.functions.consumer.CharFloatConsumer; import speiger.src.collections.chars.functions.function.Char2FloatFunction; @@ -29,6 +31,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectFloatConsumer; import speiger.src.collections.floats.functions.function.FloatPredicate; +import speiger.src.collections.floats.functions.OptionalFloat; import speiger.src.collections.chars.sets.AbstractCharSet; import speiger.src.collections.floats.collections.AbstractFloatCollection; import speiger.src.collections.floats.collections.FloatOrderedCollection; @@ -538,6 +541,10 @@ public class ImmutableChar2FloatOpenHashMap extends AbstractChar2FloatMap implem } private class MapEntrySet extends AbstractObjectSet implements Char2FloatOrderedMap.FastOrderedSet { + @Override + public void addFirst(Char2FloatMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Char2FloatMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Char2FloatMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -691,7 +698,7 @@ public class ImmutableChar2FloatOpenHashMap extends AbstractChar2FloatMap implem } @Override - public Char2FloatMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Char2FloatMap.Entry state = null; boolean empty = true; @@ -706,21 +713,21 @@ public class ImmutableChar2FloatOpenHashMap extends AbstractChar2FloatMap implem state = operator.apply(state, new BasicEntry(keys[index], values[index])); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Char2FloatMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry entry = new BasicEntry(); 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 @@ -783,6 +790,12 @@ public class ImmutableChar2FloatOpenHashMap extends AbstractChar2FloatMap implem throw new UnsupportedOperationException(); } + @Override + public void addFirst(char o) { throw new UnsupportedOperationException(); } + + @Override + public void addLast(char o) { throw new UnsupportedOperationException(); } + @Override public boolean addAndMoveToFirst(char o) { throw new UnsupportedOperationException(); } @@ -918,7 +931,7 @@ public class ImmutableChar2FloatOpenHashMap extends AbstractChar2FloatMap implem } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -933,19 +946,19 @@ public class ImmutableChar2FloatOpenHashMap extends AbstractChar2FloatMap implem state = operator.applyAsChar(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (char)0; + if(size() <= 0) return OptionalChar.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalChar.of(keys[index]); index = (int)links[index]; } - return (char)0; + return OptionalChar.empty(); } @Override @@ -1072,7 +1085,7 @@ public class ImmutableChar2FloatOpenHashMap extends AbstractChar2FloatMap implem } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -1087,19 +1100,19 @@ public class ImmutableChar2FloatOpenHashMap extends AbstractChar2FloatMap implem state = operator.applyAsFloat(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0F; + if(size() <= 0) return OptionalFloat.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalFloat.of(values[index]); index = (int)links[index]; } - return 0F; + return OptionalFloat.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/chars/maps/impl/immutable/ImmutableChar2IntOpenHashMap.java b/src/main/java/speiger/src/collections/chars/maps/impl/immutable/ImmutableChar2IntOpenHashMap.java index 869c7e9..e539b2a 100644 --- a/src/main/java/speiger/src/collections/chars/maps/impl/immutable/ImmutableChar2IntOpenHashMap.java +++ b/src/main/java/speiger/src/collections/chars/maps/impl/immutable/ImmutableChar2IntOpenHashMap.java @@ -4,10 +4,12 @@ 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; import java.util.function.IntPredicate; +import java.util.OptionalInt; import speiger.src.collections.chars.collections.CharBidirectionalIterator; import speiger.src.collections.chars.functions.CharConsumer; @@ -15,6 +17,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectCharConsumer; import speiger.src.collections.ints.functions.consumer.IntCharConsumer; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntIntConsumer; +import speiger.src.collections.chars.functions.OptionalChar; import speiger.src.collections.chars.functions.function.CharPredicate; import speiger.src.collections.chars.functions.consumer.CharIntConsumer; import speiger.src.collections.chars.functions.function.Char2IntFunction; @@ -538,6 +541,10 @@ public class ImmutableChar2IntOpenHashMap extends AbstractChar2IntMap implements } private class MapEntrySet extends AbstractObjectSet implements Char2IntOrderedMap.FastOrderedSet { + @Override + public void addFirst(Char2IntMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Char2IntMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Char2IntMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -691,7 +698,7 @@ public class ImmutableChar2IntOpenHashMap extends AbstractChar2IntMap implements } @Override - public Char2IntMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Char2IntMap.Entry state = null; boolean empty = true; @@ -706,21 +713,21 @@ public class ImmutableChar2IntOpenHashMap extends AbstractChar2IntMap implements state = operator.apply(state, new BasicEntry(keys[index], values[index])); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Char2IntMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry entry = new BasicEntry(); 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 @@ -783,6 +790,12 @@ public class ImmutableChar2IntOpenHashMap extends AbstractChar2IntMap implements throw new UnsupportedOperationException(); } + @Override + public void addFirst(char o) { throw new UnsupportedOperationException(); } + + @Override + public void addLast(char o) { throw new UnsupportedOperationException(); } + @Override public boolean addAndMoveToFirst(char o) { throw new UnsupportedOperationException(); } @@ -918,7 +931,7 @@ public class ImmutableChar2IntOpenHashMap extends AbstractChar2IntMap implements } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -933,19 +946,19 @@ public class ImmutableChar2IntOpenHashMap extends AbstractChar2IntMap implements state = operator.applyAsChar(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (char)0; + if(size() <= 0) return OptionalChar.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalChar.of(keys[index]); index = (int)links[index]; } - return (char)0; + return OptionalChar.empty(); } @Override @@ -1072,7 +1085,7 @@ public class ImmutableChar2IntOpenHashMap extends AbstractChar2IntMap implements } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -1087,19 +1100,19 @@ public class ImmutableChar2IntOpenHashMap extends AbstractChar2IntMap implements state = operator.applyAsInt(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0; + if(size() <= 0) return OptionalInt.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalInt.of(values[index]); index = (int)links[index]; } - return 0; + return OptionalInt.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/chars/maps/impl/immutable/ImmutableChar2LongOpenHashMap.java b/src/main/java/speiger/src/collections/chars/maps/impl/immutable/ImmutableChar2LongOpenHashMap.java index 33c0aee..cdaccab 100644 --- a/src/main/java/speiger/src/collections/chars/maps/impl/immutable/ImmutableChar2LongOpenHashMap.java +++ b/src/main/java/speiger/src/collections/chars/maps/impl/immutable/ImmutableChar2LongOpenHashMap.java @@ -4,10 +4,12 @@ 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; import java.util.function.LongPredicate; +import java.util.OptionalLong; import speiger.src.collections.chars.collections.CharBidirectionalIterator; import speiger.src.collections.chars.functions.CharConsumer; @@ -15,6 +17,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectCharConsumer; import speiger.src.collections.ints.functions.consumer.IntCharConsumer; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntLongConsumer; +import speiger.src.collections.chars.functions.OptionalChar; import speiger.src.collections.chars.functions.function.CharPredicate; import speiger.src.collections.chars.functions.consumer.CharLongConsumer; import speiger.src.collections.chars.functions.function.Char2LongFunction; @@ -538,6 +541,10 @@ public class ImmutableChar2LongOpenHashMap extends AbstractChar2LongMap implemen } private class MapEntrySet extends AbstractObjectSet implements Char2LongOrderedMap.FastOrderedSet { + @Override + public void addFirst(Char2LongMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Char2LongMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Char2LongMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -691,7 +698,7 @@ public class ImmutableChar2LongOpenHashMap extends AbstractChar2LongMap implemen } @Override - public Char2LongMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Char2LongMap.Entry state = null; boolean empty = true; @@ -706,21 +713,21 @@ public class ImmutableChar2LongOpenHashMap extends AbstractChar2LongMap implemen state = operator.apply(state, new BasicEntry(keys[index], values[index])); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Char2LongMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry entry = new BasicEntry(); 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 @@ -783,6 +790,12 @@ public class ImmutableChar2LongOpenHashMap extends AbstractChar2LongMap implemen throw new UnsupportedOperationException(); } + @Override + public void addFirst(char o) { throw new UnsupportedOperationException(); } + + @Override + public void addLast(char o) { throw new UnsupportedOperationException(); } + @Override public boolean addAndMoveToFirst(char o) { throw new UnsupportedOperationException(); } @@ -918,7 +931,7 @@ public class ImmutableChar2LongOpenHashMap extends AbstractChar2LongMap implemen } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -933,19 +946,19 @@ public class ImmutableChar2LongOpenHashMap extends AbstractChar2LongMap implemen state = operator.applyAsChar(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (char)0; + if(size() <= 0) return OptionalChar.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalChar.of(keys[index]); index = (int)links[index]; } - return (char)0; + return OptionalChar.empty(); } @Override @@ -1072,7 +1085,7 @@ public class ImmutableChar2LongOpenHashMap extends AbstractChar2LongMap implemen } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -1087,19 +1100,19 @@ public class ImmutableChar2LongOpenHashMap extends AbstractChar2LongMap implemen state = operator.applyAsLong(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0L; + if(size() <= 0) return OptionalLong.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalLong.of(values[index]); index = (int)links[index]; } - return 0L; + return OptionalLong.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/chars/maps/impl/immutable/ImmutableChar2ObjectOpenHashMap.java b/src/main/java/speiger/src/collections/chars/maps/impl/immutable/ImmutableChar2ObjectOpenHashMap.java index 301fd39..d436039 100644 --- a/src/main/java/speiger/src/collections/chars/maps/impl/immutable/ImmutableChar2ObjectOpenHashMap.java +++ b/src/main/java/speiger/src/collections/chars/maps/impl/immutable/ImmutableChar2ObjectOpenHashMap.java @@ -4,6 +4,7 @@ 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; @@ -13,6 +14,7 @@ import speiger.src.collections.chars.functions.CharConsumer; import speiger.src.collections.objects.functions.consumer.ObjectCharConsumer; import speiger.src.collections.ints.functions.consumer.IntCharConsumer; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; +import speiger.src.collections.chars.functions.OptionalChar; import speiger.src.collections.chars.functions.function.CharPredicate; import speiger.src.collections.chars.functions.consumer.CharObjectConsumer; import speiger.src.collections.chars.functions.function.CharFunction; @@ -509,6 +511,10 @@ public class ImmutableChar2ObjectOpenHashMap extends AbstractChar2ObjectMap> implements Char2ObjectOrderedMap.FastOrderedSet { + @Override + public void addFirst(Char2ObjectMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Char2ObjectMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Char2ObjectMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -662,7 +668,7 @@ public class ImmutableChar2ObjectOpenHashMap extends AbstractChar2ObjectMap reduce(ObjectObjectUnaryOperator, Char2ObjectMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Char2ObjectMap.Entry> operator) { Objects.requireNonNull(operator); Char2ObjectMap.Entry state = null; boolean empty = true; @@ -677,21 +683,21 @@ public class ImmutableChar2ObjectOpenHashMap extends AbstractChar2ObjectMap(keys[index], values[index])); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Char2ObjectMap.Entry findFirst(Predicate> filter) { + public Optional> findFirst(Predicate> filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry entry = new BasicEntry<>(); 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 @@ -754,6 +760,12 @@ public class ImmutableChar2ObjectOpenHashMap extends AbstractChar2ObjectMap extends AbstractChar2ObjectMap extends AbstractChar2ObjectMap extends AbstractChar2ObjectMap operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); V state = null; boolean empty = true; @@ -1059,19 +1071,19 @@ public class ImmutableChar2ObjectOpenHashMap extends AbstractChar2ObjectMap filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return Optional.ofNullable(values[index]); index = (int)links[index]; } - return null; + return Optional.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/chars/maps/impl/immutable/ImmutableChar2ShortOpenHashMap.java b/src/main/java/speiger/src/collections/chars/maps/impl/immutable/ImmutableChar2ShortOpenHashMap.java index 384e775..71e6c39 100644 --- a/src/main/java/speiger/src/collections/chars/maps/impl/immutable/ImmutableChar2ShortOpenHashMap.java +++ b/src/main/java/speiger/src/collections/chars/maps/impl/immutable/ImmutableChar2ShortOpenHashMap.java @@ -4,6 +4,7 @@ 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; @@ -14,6 +15,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectCharConsumer; import speiger.src.collections.ints.functions.consumer.IntCharConsumer; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntShortConsumer; +import speiger.src.collections.chars.functions.OptionalChar; import speiger.src.collections.chars.functions.function.CharPredicate; import speiger.src.collections.chars.functions.consumer.CharShortConsumer; import speiger.src.collections.chars.functions.function.Char2ShortFunction; @@ -29,6 +31,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectShortConsumer; import speiger.src.collections.shorts.functions.function.ShortPredicate; +import speiger.src.collections.shorts.functions.OptionalShort; import speiger.src.collections.chars.sets.AbstractCharSet; import speiger.src.collections.shorts.collections.AbstractShortCollection; import speiger.src.collections.shorts.collections.ShortOrderedCollection; @@ -538,6 +541,10 @@ public class ImmutableChar2ShortOpenHashMap extends AbstractChar2ShortMap implem } private class MapEntrySet extends AbstractObjectSet implements Char2ShortOrderedMap.FastOrderedSet { + @Override + public void addFirst(Char2ShortMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Char2ShortMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Char2ShortMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -691,7 +698,7 @@ public class ImmutableChar2ShortOpenHashMap extends AbstractChar2ShortMap implem } @Override - public Char2ShortMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Char2ShortMap.Entry state = null; boolean empty = true; @@ -706,21 +713,21 @@ public class ImmutableChar2ShortOpenHashMap extends AbstractChar2ShortMap implem state = operator.apply(state, new BasicEntry(keys[index], values[index])); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Char2ShortMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry entry = new BasicEntry(); 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 @@ -783,6 +790,12 @@ public class ImmutableChar2ShortOpenHashMap extends AbstractChar2ShortMap implem throw new UnsupportedOperationException(); } + @Override + public void addFirst(char o) { throw new UnsupportedOperationException(); } + + @Override + public void addLast(char o) { throw new UnsupportedOperationException(); } + @Override public boolean addAndMoveToFirst(char o) { throw new UnsupportedOperationException(); } @@ -918,7 +931,7 @@ public class ImmutableChar2ShortOpenHashMap extends AbstractChar2ShortMap implem } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -933,19 +946,19 @@ public class ImmutableChar2ShortOpenHashMap extends AbstractChar2ShortMap implem state = operator.applyAsChar(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (char)0; + if(size() <= 0) return OptionalChar.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalChar.of(keys[index]); index = (int)links[index]; } - return (char)0; + return OptionalChar.empty(); } @Override @@ -1072,7 +1085,7 @@ public class ImmutableChar2ShortOpenHashMap extends AbstractChar2ShortMap implem } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -1087,19 +1100,19 @@ public class ImmutableChar2ShortOpenHashMap extends AbstractChar2ShortMap implem state = operator.applyAsShort(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (short)0; + if(size() <= 0) return OptionalShort.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalShort.of(values[index]); index = (int)links[index]; } - return (short)0; + return OptionalShort.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/chars/maps/impl/misc/Char2BooleanArrayMap.java b/src/main/java/speiger/src/collections/chars/maps/impl/misc/Char2BooleanArrayMap.java index 4495a6c..855bbf3 100644 --- a/src/main/java/speiger/src/collections/chars/maps/impl/misc/Char2BooleanArrayMap.java +++ b/src/main/java/speiger/src/collections/chars/maps/impl/misc/Char2BooleanArrayMap.java @@ -4,6 +4,7 @@ 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; @@ -14,6 +15,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectCharConsumer; import speiger.src.collections.ints.functions.consumer.IntCharConsumer; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntBooleanConsumer; +import speiger.src.collections.chars.functions.OptionalChar; import speiger.src.collections.chars.functions.consumer.CharBooleanConsumer; import speiger.src.collections.chars.functions.function.CharPredicate; import speiger.src.collections.chars.functions.function.CharBooleanUnaryOperator; @@ -34,6 +36,7 @@ import speiger.src.collections.booleans.lists.BooleanListIterator; import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.booleans.functions.function.BooleanPredicate; +import speiger.src.collections.booleans.functions.OptionalBoolean; import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOperator; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; @@ -728,6 +731,10 @@ public class Char2BooleanArrayMap extends AbstractChar2BooleanMap implements Cha } private class MapEntrySet extends AbstractObjectSet implements Char2BooleanOrderedMap.FastOrderedSet { + @Override + public void addFirst(Char2BooleanMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Char2BooleanMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Char2BooleanMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -876,7 +883,7 @@ public class Char2BooleanArrayMap extends AbstractChar2BooleanMap implements Cha } @Override - public Char2BooleanMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Char2BooleanMap.Entry state = null; boolean empty = true; @@ -888,19 +895,19 @@ public class Char2BooleanArrayMap extends AbstractChar2BooleanMap implements Cha } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Char2BooleanMap.Entry 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 implements Char2ByteOrderedMap.FastOrderedSet { + @Override + public void addFirst(Char2ByteMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Char2ByteMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Char2ByteMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -899,7 +906,7 @@ public class Char2ByteArrayMap extends AbstractChar2ByteMap implements Char2Byte } @Override - public Char2ByteMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Char2ByteMap.Entry state = null; boolean empty = true; @@ -911,19 +918,19 @@ public class Char2ByteArrayMap extends AbstractChar2ByteMap implements Char2Byte } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Char2ByteMap.Entry 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 implements Char2CharOrderedMap.FastOrderedSet { + @Override + public void addFirst(Char2CharMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Char2CharMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Char2CharMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -892,7 +898,7 @@ public class Char2CharArrayMap extends AbstractChar2CharMap implements Char2Char } @Override - public Char2CharMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Char2CharMap.Entry state = null; boolean empty = true; @@ -904,19 +910,19 @@ public class Char2CharArrayMap extends AbstractChar2CharMap implements Char2Char } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Char2CharMap.Entry 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 implements Char2DoubleOrderedMap.FastOrderedSet { + @Override + public void addFirst(Char2DoubleMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Char2DoubleMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Char2DoubleMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -899,7 +906,7 @@ public class Char2DoubleArrayMap extends AbstractChar2DoubleMap implements Char2 } @Override - public Char2DoubleMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Char2DoubleMap.Entry state = null; boolean empty = true; @@ -911,19 +918,19 @@ public class Char2DoubleArrayMap extends AbstractChar2DoubleMap implements Char2 } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Char2DoubleMap.Entry 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 implements Char2FloatOrderedMap.FastOrderedSet { + @Override + public void addFirst(Char2FloatMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Char2FloatMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Char2FloatMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -899,7 +906,7 @@ public class Char2FloatArrayMap extends AbstractChar2FloatMap implements Char2Fl } @Override - public Char2FloatMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Char2FloatMap.Entry state = null; boolean empty = true; @@ -911,19 +918,19 @@ public class Char2FloatArrayMap extends AbstractChar2FloatMap implements Char2Fl } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Char2FloatMap.Entry 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 implements Char2IntOrderedMap.FastOrderedSet { + @Override + public void addFirst(Char2IntMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Char2IntMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Char2IntMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -899,7 +906,7 @@ public class Char2IntArrayMap extends AbstractChar2IntMap implements Char2IntOrd } @Override - public Char2IntMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Char2IntMap.Entry state = null; boolean empty = true; @@ -911,19 +918,19 @@ public class Char2IntArrayMap extends AbstractChar2IntMap implements Char2IntOrd } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Char2IntMap.Entry 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 implements Char2LongOrderedMap.FastOrderedSet { + @Override + public void addFirst(Char2LongMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Char2LongMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Char2LongMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -899,7 +906,7 @@ public class Char2LongArrayMap extends AbstractChar2LongMap implements Char2Long } @Override - public Char2LongMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Char2LongMap.Entry state = null; boolean empty = true; @@ -911,19 +918,19 @@ public class Char2LongArrayMap extends AbstractChar2LongMap implements Char2Long } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Char2LongMap.Entry 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 extends AbstractChar2ObjectMap implements } private class MapEntrySet extends AbstractObjectSet> implements Char2ObjectOrderedMap.FastOrderedSet { + @Override + public void addFirst(Char2ObjectMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Char2ObjectMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Char2ObjectMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -812,7 +818,7 @@ public class Char2ObjectArrayMap extends AbstractChar2ObjectMap implements } @Override - public Char2ObjectMap.Entry reduce(ObjectObjectUnaryOperator, Char2ObjectMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Char2ObjectMap.Entry> operator) { Objects.requireNonNull(operator); Char2ObjectMap.Entry state = null; boolean empty = true; @@ -824,19 +830,19 @@ public class Char2ObjectArrayMap extends AbstractChar2ObjectMap implements } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Char2ObjectMap.Entry 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 extends AbstractChar2ObjectMap implements @Override public boolean add(char o) { throw new UnsupportedOperationException(); } @Override + public void addFirst(char o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(char o) { throw new UnsupportedOperationException(); } + @Override public boolean addAndMoveToFirst(char o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToLast(char o) { throw new UnsupportedOperationException(); } @@ -995,7 +1005,7 @@ public class Char2ObjectArrayMap extends AbstractChar2ObjectMap implements } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -1007,16 +1017,16 @@ public class Char2ObjectArrayMap extends AbstractChar2ObjectMap implements } state = operator.applyAsChar(state, keys[i]); } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); for(int i = 0;i extends AbstractChar2ObjectMap implements } @Override - public V reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); V state = null; boolean empty = true; @@ -1129,16 +1139,16 @@ public class Char2ObjectArrayMap extends AbstractChar2ObjectMap implements } state = operator.apply(state, values[i]); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public V findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); for(int i = 0;i implements Char2ShortOrderedMap.FastOrderedSet { + @Override + public void addFirst(Char2ShortMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Char2ShortMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Char2ShortMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -899,7 +906,7 @@ public class Char2ShortArrayMap extends AbstractChar2ShortMap implements Char2Sh } @Override - public Char2ShortMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Char2ShortMap.Entry state = null; boolean empty = true; @@ -911,19 +918,19 @@ public class Char2ShortArrayMap extends AbstractChar2ShortMap implements Char2Sh } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Char2ShortMap.Entry 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); Char2BooleanMap.Entry state = null; boolean empty = true; @@ -1724,19 +1727,19 @@ public class Char2BooleanAVLTreeMap extends AbstractChar2BooleanMap implements C } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Char2BooleanMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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 @@ -1831,7 +1834,7 @@ public class Char2BooleanAVLTreeMap extends AbstractChar2BooleanMap implements C } @Override - public boolean reduce(BooleanBooleanUnaryOperator operator) { + public OptionalBoolean reduce(BooleanBooleanUnaryOperator operator) { Objects.requireNonNull(operator); boolean state = false; boolean empty = true; @@ -1843,15 +1846,15 @@ public class Char2BooleanAVLTreeMap extends AbstractChar2BooleanMap implements C } state = operator.applyAsBoolean(state, entry.value); } - return state; + return empty ? OptionalBoolean.empty() : OptionalBoolean.of(state); } @Override - public boolean findFirst(BooleanPredicate filter) { + public OptionalBoolean findFirst(BooleanPredicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return false; + if(filter.test(entry.value)) return OptionalBoolean.of(entry.value); + return OptionalBoolean.empty(); } @Override @@ -2167,7 +2170,7 @@ public class Char2BooleanAVLTreeMap extends AbstractChar2BooleanMap implements C } @Override - public boolean reduce(BooleanBooleanUnaryOperator operator) { + public OptionalBoolean reduce(BooleanBooleanUnaryOperator operator) { Objects.requireNonNull(operator); boolean state = false; boolean empty = true; @@ -2179,15 +2182,15 @@ public class Char2BooleanAVLTreeMap extends AbstractChar2BooleanMap implements C } state = operator.applyAsBoolean(state, entry.value); } - return state; + return empty ? OptionalBoolean.empty() : OptionalBoolean.of(state); } @Override - public boolean findFirst(BooleanPredicate filter) { + public OptionalBoolean findFirst(BooleanPredicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return false; + if(filter.test(entry.value)) return OptionalBoolean.of(entry.value); + return OptionalBoolean.empty(); } @Override @@ -2328,7 +2331,7 @@ public class Char2BooleanAVLTreeMap extends AbstractChar2BooleanMap implements C } @Override - public Char2BooleanMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Char2BooleanMap.Entry state = null; boolean empty = true; @@ -2340,19 +2343,19 @@ public class Char2BooleanAVLTreeMap extends AbstractChar2BooleanMap implements C } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Char2BooleanMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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/main/java/speiger/src/collections/chars/maps/impl/tree/Char2BooleanRBTreeMap.java b/src/main/java/speiger/src/collections/chars/maps/impl/tree/Char2BooleanRBTreeMap.java index 4621948..f99e48b 100644 --- a/src/main/java/speiger/src/collections/chars/maps/impl/tree/Char2BooleanRBTreeMap.java +++ b/src/main/java/speiger/src/collections/chars/maps/impl/tree/Char2BooleanRBTreeMap.java @@ -3,6 +3,7 @@ package speiger.src.collections.chars.maps.impl.tree; import java.util.Collections; import java.util.Map; import java.util.Objects; +import java.util.Optional; import java.util.NoSuchElementException; import java.util.function.Consumer; import java.util.function.BiFunction; @@ -17,6 +18,7 @@ import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntBooleanConsumer; import speiger.src.collections.chars.functions.function.CharPredicate; import speiger.src.collections.chars.functions.consumer.CharBooleanConsumer; +import speiger.src.collections.chars.functions.OptionalChar; import speiger.src.collections.chars.functions.function.CharBooleanUnaryOperator; import speiger.src.collections.chars.functions.function.CharCharUnaryOperator; import speiger.src.collections.chars.maps.abstracts.AbstractChar2BooleanMap; @@ -35,6 +37,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectBooleanConsumer; import speiger.src.collections.booleans.functions.function.BooleanPredicate; +import speiger.src.collections.booleans.functions.OptionalBoolean; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOperator; @@ -1123,7 +1126,7 @@ public class Char2BooleanRBTreeMap extends AbstractChar2BooleanMap implements Ch } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -1135,15 +1138,15 @@ public class Char2BooleanRBTreeMap extends AbstractChar2BooleanMap implements Ch } state = operator.applyAsChar(state, entry.key); } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); for(Node entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.test(entry.key)) return entry.key; - return (char)0; + if(filter.test(entry.key)) return OptionalChar.of(entry.key); + return OptionalChar.empty(); } @Override @@ -1775,7 +1778,7 @@ public class Char2BooleanRBTreeMap extends AbstractChar2BooleanMap implements Ch } @Override - public Char2BooleanMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Char2BooleanMap.Entry state = null; boolean empty = true; @@ -1787,19 +1790,19 @@ public class Char2BooleanRBTreeMap extends AbstractChar2BooleanMap implements Ch } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Char2BooleanMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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 @@ -1894,7 +1897,7 @@ public class Char2BooleanRBTreeMap extends AbstractChar2BooleanMap implements Ch } @Override - public boolean reduce(BooleanBooleanUnaryOperator operator) { + public OptionalBoolean reduce(BooleanBooleanUnaryOperator operator) { Objects.requireNonNull(operator); boolean state = false; boolean empty = true; @@ -1906,15 +1909,15 @@ public class Char2BooleanRBTreeMap extends AbstractChar2BooleanMap implements Ch } state = operator.applyAsBoolean(state, entry.value); } - return state; + return empty ? OptionalBoolean.empty() : OptionalBoolean.of(state); } @Override - public boolean findFirst(BooleanPredicate filter) { + public OptionalBoolean findFirst(BooleanPredicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return false; + if(filter.test(entry.value)) return OptionalBoolean.of(entry.value); + return OptionalBoolean.empty(); } @Override @@ -2230,7 +2233,7 @@ public class Char2BooleanRBTreeMap extends AbstractChar2BooleanMap implements Ch } @Override - public boolean reduce(BooleanBooleanUnaryOperator operator) { + public OptionalBoolean reduce(BooleanBooleanUnaryOperator operator) { Objects.requireNonNull(operator); boolean state = false; boolean empty = true; @@ -2242,15 +2245,15 @@ public class Char2BooleanRBTreeMap extends AbstractChar2BooleanMap implements Ch } state = operator.applyAsBoolean(state, entry.value); } - return state; + return empty ? OptionalBoolean.empty() : OptionalBoolean.of(state); } @Override - public boolean findFirst(BooleanPredicate filter) { + public OptionalBoolean findFirst(BooleanPredicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return false; + if(filter.test(entry.value)) return OptionalBoolean.of(entry.value); + return OptionalBoolean.empty(); } @Override @@ -2391,7 +2394,7 @@ public class Char2BooleanRBTreeMap extends AbstractChar2BooleanMap implements Ch } @Override - public Char2BooleanMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Char2BooleanMap.Entry state = null; boolean empty = true; @@ -2403,19 +2406,19 @@ public class Char2BooleanRBTreeMap extends AbstractChar2BooleanMap implements Ch } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Char2BooleanMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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/main/java/speiger/src/collections/chars/maps/impl/tree/Char2ByteAVLTreeMap.java b/src/main/java/speiger/src/collections/chars/maps/impl/tree/Char2ByteAVLTreeMap.java index c1ffbf4..b309acd 100644 --- a/src/main/java/speiger/src/collections/chars/maps/impl/tree/Char2ByteAVLTreeMap.java +++ b/src/main/java/speiger/src/collections/chars/maps/impl/tree/Char2ByteAVLTreeMap.java @@ -3,6 +3,7 @@ package speiger.src.collections.chars.maps.impl.tree; import java.util.Collections; import java.util.Map; import java.util.Objects; +import java.util.Optional; import java.util.NoSuchElementException; import java.util.function.Consumer; import java.util.function.BiFunction; @@ -18,6 +19,7 @@ import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntByteConsumer; import speiger.src.collections.chars.functions.function.Char2ByteFunction; import speiger.src.collections.chars.functions.consumer.CharByteConsumer; +import speiger.src.collections.chars.functions.OptionalChar; import speiger.src.collections.chars.functions.function.CharPredicate; import speiger.src.collections.chars.functions.function.CharByteUnaryOperator; import speiger.src.collections.chars.functions.function.CharCharUnaryOperator; @@ -37,6 +39,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectByteConsumer; import speiger.src.collections.bytes.functions.function.BytePredicate; +import speiger.src.collections.bytes.functions.OptionalByte; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOperator; @@ -1129,7 +1132,7 @@ public class Char2ByteAVLTreeMap extends AbstractChar2ByteMap implements Char2By } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -1141,15 +1144,15 @@ public class Char2ByteAVLTreeMap extends AbstractChar2ByteMap implements Char2By } state = operator.applyAsChar(state, entry.key); } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); for(Node entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.test(entry.key)) return entry.key; - return (char)0; + if(filter.test(entry.key)) return OptionalChar.of(entry.key); + return OptionalChar.empty(); } @Override @@ -1783,7 +1786,7 @@ public class Char2ByteAVLTreeMap extends AbstractChar2ByteMap implements Char2By } @Override - public Char2ByteMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Char2ByteMap.Entry state = null; boolean empty = true; @@ -1795,19 +1798,19 @@ public class Char2ByteAVLTreeMap extends AbstractChar2ByteMap implements Char2By } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Char2ByteMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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 @@ -1902,7 +1905,7 @@ public class Char2ByteAVLTreeMap extends AbstractChar2ByteMap implements Char2By } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -1914,15 +1917,15 @@ public class Char2ByteAVLTreeMap extends AbstractChar2ByteMap implements Char2By } state = operator.applyAsByte(state, entry.value); } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return (byte)0; + if(filter.test(entry.value)) return OptionalByte.of(entry.value); + return OptionalByte.empty(); } @Override @@ -2238,7 +2241,7 @@ public class Char2ByteAVLTreeMap extends AbstractChar2ByteMap implements Char2By } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -2250,15 +2253,15 @@ public class Char2ByteAVLTreeMap extends AbstractChar2ByteMap implements Char2By } state = operator.applyAsByte(state, entry.value); } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return (byte)0; + if(filter.test(entry.value)) return OptionalByte.of(entry.value); + return OptionalByte.empty(); } @Override @@ -2399,7 +2402,7 @@ public class Char2ByteAVLTreeMap extends AbstractChar2ByteMap implements Char2By } @Override - public Char2ByteMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Char2ByteMap.Entry state = null; boolean empty = true; @@ -2411,19 +2414,19 @@ public class Char2ByteAVLTreeMap extends AbstractChar2ByteMap implements Char2By } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Char2ByteMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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/main/java/speiger/src/collections/chars/maps/impl/tree/Char2ByteRBTreeMap.java b/src/main/java/speiger/src/collections/chars/maps/impl/tree/Char2ByteRBTreeMap.java index b2abf89..7c79016 100644 --- a/src/main/java/speiger/src/collections/chars/maps/impl/tree/Char2ByteRBTreeMap.java +++ b/src/main/java/speiger/src/collections/chars/maps/impl/tree/Char2ByteRBTreeMap.java @@ -3,6 +3,7 @@ package speiger.src.collections.chars.maps.impl.tree; import java.util.Collections; import java.util.Map; import java.util.Objects; +import java.util.Optional; import java.util.NoSuchElementException; import java.util.function.Consumer; import java.util.function.BiFunction; @@ -17,6 +18,7 @@ import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntByteConsumer; import speiger.src.collections.chars.functions.function.Char2ByteFunction; import speiger.src.collections.chars.functions.consumer.CharByteConsumer; +import speiger.src.collections.chars.functions.OptionalChar; import speiger.src.collections.chars.functions.function.CharPredicate; import speiger.src.collections.chars.functions.function.CharByteUnaryOperator; import speiger.src.collections.chars.functions.function.CharCharUnaryOperator; @@ -36,6 +38,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectByteConsumer; import speiger.src.collections.bytes.functions.function.BytePredicate; +import speiger.src.collections.bytes.functions.OptionalByte; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOperator; @@ -1182,7 +1185,7 @@ public class Char2ByteRBTreeMap extends AbstractChar2ByteMap implements Char2Byt } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -1194,15 +1197,15 @@ public class Char2ByteRBTreeMap extends AbstractChar2ByteMap implements Char2Byt } state = operator.applyAsChar(state, entry.key); } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); for(Node entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.test(entry.key)) return entry.key; - return (char)0; + if(filter.test(entry.key)) return OptionalChar.of(entry.key); + return OptionalChar.empty(); } @Override @@ -1846,7 +1849,7 @@ public class Char2ByteRBTreeMap extends AbstractChar2ByteMap implements Char2Byt } @Override - public Char2ByteMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Char2ByteMap.Entry state = null; boolean empty = true; @@ -1858,19 +1861,19 @@ public class Char2ByteRBTreeMap extends AbstractChar2ByteMap implements Char2Byt } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Char2ByteMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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 @@ -1965,7 +1968,7 @@ public class Char2ByteRBTreeMap extends AbstractChar2ByteMap implements Char2Byt } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -1977,15 +1980,15 @@ public class Char2ByteRBTreeMap extends AbstractChar2ByteMap implements Char2Byt } state = operator.applyAsByte(state, entry.value); } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return (byte)0; + if(filter.test(entry.value)) return OptionalByte.of(entry.value); + return OptionalByte.empty(); } @Override @@ -2301,7 +2304,7 @@ public class Char2ByteRBTreeMap extends AbstractChar2ByteMap implements Char2Byt } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -2313,15 +2316,15 @@ public class Char2ByteRBTreeMap extends AbstractChar2ByteMap implements Char2Byt } state = operator.applyAsByte(state, entry.value); } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return (byte)0; + if(filter.test(entry.value)) return OptionalByte.of(entry.value); + return OptionalByte.empty(); } @Override @@ -2462,7 +2465,7 @@ public class Char2ByteRBTreeMap extends AbstractChar2ByteMap implements Char2Byt } @Override - public Char2ByteMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Char2ByteMap.Entry state = null; boolean empty = true; @@ -2474,19 +2477,19 @@ public class Char2ByteRBTreeMap extends AbstractChar2ByteMap implements Char2Byt } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Char2ByteMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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/main/java/speiger/src/collections/chars/maps/impl/tree/Char2CharAVLTreeMap.java b/src/main/java/speiger/src/collections/chars/maps/impl/tree/Char2CharAVLTreeMap.java index ce036f2..ccee734 100644 --- a/src/main/java/speiger/src/collections/chars/maps/impl/tree/Char2CharAVLTreeMap.java +++ b/src/main/java/speiger/src/collections/chars/maps/impl/tree/Char2CharAVLTreeMap.java @@ -3,6 +3,7 @@ package speiger.src.collections.chars.maps.impl.tree; import java.util.Collections; import java.util.Map; import java.util.Objects; +import java.util.Optional; import java.util.NoSuchElementException; import java.util.function.Consumer; import java.util.function.BiFunction; @@ -17,6 +18,7 @@ import speiger.src.collections.ints.functions.consumer.IntCharConsumer; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.chars.functions.function.CharUnaryOperator; import speiger.src.collections.chars.functions.consumer.CharCharConsumer; +import speiger.src.collections.chars.functions.OptionalChar; import speiger.src.collections.chars.functions.function.CharPredicate; import speiger.src.collections.chars.functions.function.CharCharUnaryOperator; import speiger.src.collections.chars.maps.abstracts.AbstractChar2CharMap; @@ -1121,7 +1123,7 @@ public class Char2CharAVLTreeMap extends AbstractChar2CharMap implements Char2Ch } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -1133,15 +1135,15 @@ public class Char2CharAVLTreeMap extends AbstractChar2CharMap implements Char2Ch } state = operator.applyAsChar(state, entry.key); } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); for(Node entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.test(entry.key)) return entry.key; - return (char)0; + if(filter.test(entry.key)) return OptionalChar.of(entry.key); + return OptionalChar.empty(); } @Override @@ -1775,7 +1777,7 @@ public class Char2CharAVLTreeMap extends AbstractChar2CharMap implements Char2Ch } @Override - public Char2CharMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Char2CharMap.Entry state = null; boolean empty = true; @@ -1787,19 +1789,19 @@ public class Char2CharAVLTreeMap extends AbstractChar2CharMap implements Char2Ch } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Char2CharMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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 @@ -1894,7 +1896,7 @@ public class Char2CharAVLTreeMap extends AbstractChar2CharMap implements Char2Ch } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -1906,15 +1908,15 @@ public class Char2CharAVLTreeMap extends AbstractChar2CharMap implements Char2Ch } state = operator.applyAsChar(state, entry.value); } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return (char)0; + if(filter.test(entry.value)) return OptionalChar.of(entry.value); + return OptionalChar.empty(); } @Override @@ -2230,7 +2232,7 @@ public class Char2CharAVLTreeMap extends AbstractChar2CharMap implements Char2Ch } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -2242,15 +2244,15 @@ public class Char2CharAVLTreeMap extends AbstractChar2CharMap implements Char2Ch } state = operator.applyAsChar(state, entry.value); } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return (char)0; + if(filter.test(entry.value)) return OptionalChar.of(entry.value); + return OptionalChar.empty(); } @Override @@ -2391,7 +2393,7 @@ public class Char2CharAVLTreeMap extends AbstractChar2CharMap implements Char2Ch } @Override - public Char2CharMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Char2CharMap.Entry state = null; boolean empty = true; @@ -2403,19 +2405,19 @@ public class Char2CharAVLTreeMap extends AbstractChar2CharMap implements Char2Ch } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Char2CharMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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/main/java/speiger/src/collections/chars/maps/impl/tree/Char2CharRBTreeMap.java b/src/main/java/speiger/src/collections/chars/maps/impl/tree/Char2CharRBTreeMap.java index d24412e..c903b32 100644 --- a/src/main/java/speiger/src/collections/chars/maps/impl/tree/Char2CharRBTreeMap.java +++ b/src/main/java/speiger/src/collections/chars/maps/impl/tree/Char2CharRBTreeMap.java @@ -3,6 +3,7 @@ package speiger.src.collections.chars.maps.impl.tree; import java.util.Collections; import java.util.Map; import java.util.Objects; +import java.util.Optional; import java.util.NoSuchElementException; import java.util.function.Consumer; import java.util.function.BiFunction; @@ -16,6 +17,7 @@ import speiger.src.collections.ints.functions.consumer.IntCharConsumer; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.chars.functions.function.CharUnaryOperator; import speiger.src.collections.chars.functions.consumer.CharCharConsumer; +import speiger.src.collections.chars.functions.OptionalChar; import speiger.src.collections.chars.functions.function.CharPredicate; import speiger.src.collections.chars.functions.function.CharCharUnaryOperator; import speiger.src.collections.chars.maps.abstracts.AbstractChar2CharMap; @@ -1174,7 +1176,7 @@ public class Char2CharRBTreeMap extends AbstractChar2CharMap implements Char2Cha } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -1186,15 +1188,15 @@ public class Char2CharRBTreeMap extends AbstractChar2CharMap implements Char2Cha } state = operator.applyAsChar(state, entry.key); } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); for(Node entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.test(entry.key)) return entry.key; - return (char)0; + if(filter.test(entry.key)) return OptionalChar.of(entry.key); + return OptionalChar.empty(); } @Override @@ -1838,7 +1840,7 @@ public class Char2CharRBTreeMap extends AbstractChar2CharMap implements Char2Cha } @Override - public Char2CharMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Char2CharMap.Entry state = null; boolean empty = true; @@ -1850,19 +1852,19 @@ public class Char2CharRBTreeMap extends AbstractChar2CharMap implements Char2Cha } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Char2CharMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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 @@ -1957,7 +1959,7 @@ public class Char2CharRBTreeMap extends AbstractChar2CharMap implements Char2Cha } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -1969,15 +1971,15 @@ public class Char2CharRBTreeMap extends AbstractChar2CharMap implements Char2Cha } state = operator.applyAsChar(state, entry.value); } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return (char)0; + if(filter.test(entry.value)) return OptionalChar.of(entry.value); + return OptionalChar.empty(); } @Override @@ -2293,7 +2295,7 @@ public class Char2CharRBTreeMap extends AbstractChar2CharMap implements Char2Cha } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -2305,15 +2307,15 @@ public class Char2CharRBTreeMap extends AbstractChar2CharMap implements Char2Cha } state = operator.applyAsChar(state, entry.value); } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return (char)0; + if(filter.test(entry.value)) return OptionalChar.of(entry.value); + return OptionalChar.empty(); } @Override @@ -2454,7 +2456,7 @@ public class Char2CharRBTreeMap extends AbstractChar2CharMap implements Char2Cha } @Override - public Char2CharMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Char2CharMap.Entry state = null; boolean empty = true; @@ -2466,19 +2468,19 @@ public class Char2CharRBTreeMap extends AbstractChar2CharMap implements Char2Cha } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Char2CharMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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/main/java/speiger/src/collections/chars/maps/impl/tree/Char2DoubleAVLTreeMap.java b/src/main/java/speiger/src/collections/chars/maps/impl/tree/Char2DoubleAVLTreeMap.java index 200f4df..a262d23 100644 --- a/src/main/java/speiger/src/collections/chars/maps/impl/tree/Char2DoubleAVLTreeMap.java +++ b/src/main/java/speiger/src/collections/chars/maps/impl/tree/Char2DoubleAVLTreeMap.java @@ -3,11 +3,13 @@ package speiger.src.collections.chars.maps.impl.tree; import java.util.Collections; import java.util.Map; 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; import java.util.function.DoublePredicate; +import java.util.OptionalDouble; import speiger.src.collections.chars.collections.CharBidirectionalIterator; @@ -19,6 +21,7 @@ import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntDoubleConsumer; import speiger.src.collections.chars.functions.function.Char2DoubleFunction; import speiger.src.collections.chars.functions.consumer.CharDoubleConsumer; +import speiger.src.collections.chars.functions.OptionalChar; import speiger.src.collections.chars.functions.function.CharPredicate; import speiger.src.collections.chars.functions.function.CharDoubleUnaryOperator; import speiger.src.collections.chars.functions.function.CharCharUnaryOperator; @@ -1129,7 +1132,7 @@ public class Char2DoubleAVLTreeMap extends AbstractChar2DoubleMap implements Cha } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -1141,15 +1144,15 @@ public class Char2DoubleAVLTreeMap extends AbstractChar2DoubleMap implements Cha } state = operator.applyAsChar(state, entry.key); } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); for(Node entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.test(entry.key)) return entry.key; - return (char)0; + if(filter.test(entry.key)) return OptionalChar.of(entry.key); + return OptionalChar.empty(); } @Override @@ -1783,7 +1786,7 @@ public class Char2DoubleAVLTreeMap extends AbstractChar2DoubleMap implements Cha } @Override - public Char2DoubleMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Char2DoubleMap.Entry state = null; boolean empty = true; @@ -1795,19 +1798,19 @@ public class Char2DoubleAVLTreeMap extends AbstractChar2DoubleMap implements Cha } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Char2DoubleMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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 @@ -1902,7 +1905,7 @@ public class Char2DoubleAVLTreeMap extends AbstractChar2DoubleMap implements Cha } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -1914,15 +1917,15 @@ public class Char2DoubleAVLTreeMap extends AbstractChar2DoubleMap implements Cha } state = operator.applyAsDouble(state, entry.value); } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return 0D; + if(filter.test(entry.value)) return OptionalDouble.of(entry.value); + return OptionalDouble.empty(); } @Override @@ -2238,7 +2241,7 @@ public class Char2DoubleAVLTreeMap extends AbstractChar2DoubleMap implements Cha } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -2250,15 +2253,15 @@ public class Char2DoubleAVLTreeMap extends AbstractChar2DoubleMap implements Cha } state = operator.applyAsDouble(state, entry.value); } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return 0D; + if(filter.test(entry.value)) return OptionalDouble.of(entry.value); + return OptionalDouble.empty(); } @Override @@ -2399,7 +2402,7 @@ public class Char2DoubleAVLTreeMap extends AbstractChar2DoubleMap implements Cha } @Override - public Char2DoubleMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Char2DoubleMap.Entry state = null; boolean empty = true; @@ -2411,19 +2414,19 @@ public class Char2DoubleAVLTreeMap extends AbstractChar2DoubleMap implements Cha } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Char2DoubleMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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/main/java/speiger/src/collections/chars/maps/impl/tree/Char2DoubleRBTreeMap.java b/src/main/java/speiger/src/collections/chars/maps/impl/tree/Char2DoubleRBTreeMap.java index d62da56..6e3bc3d 100644 --- a/src/main/java/speiger/src/collections/chars/maps/impl/tree/Char2DoubleRBTreeMap.java +++ b/src/main/java/speiger/src/collections/chars/maps/impl/tree/Char2DoubleRBTreeMap.java @@ -3,11 +3,13 @@ package speiger.src.collections.chars.maps.impl.tree; import java.util.Collections; import java.util.Map; 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; import java.util.function.DoublePredicate; +import java.util.OptionalDouble; import speiger.src.collections.chars.collections.CharBidirectionalIterator; import speiger.src.collections.chars.functions.CharComparator; @@ -18,6 +20,7 @@ import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntDoubleConsumer; import speiger.src.collections.chars.functions.function.Char2DoubleFunction; import speiger.src.collections.chars.functions.consumer.CharDoubleConsumer; +import speiger.src.collections.chars.functions.OptionalChar; import speiger.src.collections.chars.functions.function.CharPredicate; import speiger.src.collections.chars.functions.function.CharDoubleUnaryOperator; import speiger.src.collections.chars.functions.function.CharCharUnaryOperator; @@ -1182,7 +1185,7 @@ public class Char2DoubleRBTreeMap extends AbstractChar2DoubleMap implements Char } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -1194,15 +1197,15 @@ public class Char2DoubleRBTreeMap extends AbstractChar2DoubleMap implements Char } state = operator.applyAsChar(state, entry.key); } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); for(Node entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.test(entry.key)) return entry.key; - return (char)0; + if(filter.test(entry.key)) return OptionalChar.of(entry.key); + return OptionalChar.empty(); } @Override @@ -1846,7 +1849,7 @@ public class Char2DoubleRBTreeMap extends AbstractChar2DoubleMap implements Char } @Override - public Char2DoubleMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Char2DoubleMap.Entry state = null; boolean empty = true; @@ -1858,19 +1861,19 @@ public class Char2DoubleRBTreeMap extends AbstractChar2DoubleMap implements Char } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Char2DoubleMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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 @@ -1965,7 +1968,7 @@ public class Char2DoubleRBTreeMap extends AbstractChar2DoubleMap implements Char } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -1977,15 +1980,15 @@ public class Char2DoubleRBTreeMap extends AbstractChar2DoubleMap implements Char } state = operator.applyAsDouble(state, entry.value); } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return 0D; + if(filter.test(entry.value)) return OptionalDouble.of(entry.value); + return OptionalDouble.empty(); } @Override @@ -2301,7 +2304,7 @@ public class Char2DoubleRBTreeMap extends AbstractChar2DoubleMap implements Char } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -2313,15 +2316,15 @@ public class Char2DoubleRBTreeMap extends AbstractChar2DoubleMap implements Char } state = operator.applyAsDouble(state, entry.value); } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return 0D; + if(filter.test(entry.value)) return OptionalDouble.of(entry.value); + return OptionalDouble.empty(); } @Override @@ -2462,7 +2465,7 @@ public class Char2DoubleRBTreeMap extends AbstractChar2DoubleMap implements Char } @Override - public Char2DoubleMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Char2DoubleMap.Entry state = null; boolean empty = true; @@ -2474,19 +2477,19 @@ public class Char2DoubleRBTreeMap extends AbstractChar2DoubleMap implements Char } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Char2DoubleMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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/main/java/speiger/src/collections/chars/maps/impl/tree/Char2FloatAVLTreeMap.java b/src/main/java/speiger/src/collections/chars/maps/impl/tree/Char2FloatAVLTreeMap.java index fba16b8..448110e 100644 --- a/src/main/java/speiger/src/collections/chars/maps/impl/tree/Char2FloatAVLTreeMap.java +++ b/src/main/java/speiger/src/collections/chars/maps/impl/tree/Char2FloatAVLTreeMap.java @@ -3,6 +3,7 @@ package speiger.src.collections.chars.maps.impl.tree; import java.util.Collections; import java.util.Map; import java.util.Objects; +import java.util.Optional; import java.util.NoSuchElementException; import java.util.function.Consumer; import java.util.function.BiFunction; @@ -18,6 +19,7 @@ import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntFloatConsumer; import speiger.src.collections.chars.functions.function.Char2FloatFunction; import speiger.src.collections.chars.functions.consumer.CharFloatConsumer; +import speiger.src.collections.chars.functions.OptionalChar; import speiger.src.collections.chars.functions.function.CharPredicate; import speiger.src.collections.chars.functions.function.CharFloatUnaryOperator; import speiger.src.collections.chars.functions.function.CharCharUnaryOperator; @@ -37,6 +39,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectFloatConsumer; import speiger.src.collections.floats.functions.function.FloatPredicate; +import speiger.src.collections.floats.functions.OptionalFloat; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOperator; @@ -1129,7 +1132,7 @@ public class Char2FloatAVLTreeMap extends AbstractChar2FloatMap implements Char2 } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -1141,15 +1144,15 @@ public class Char2FloatAVLTreeMap extends AbstractChar2FloatMap implements Char2 } state = operator.applyAsChar(state, entry.key); } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); for(Node entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.test(entry.key)) return entry.key; - return (char)0; + if(filter.test(entry.key)) return OptionalChar.of(entry.key); + return OptionalChar.empty(); } @Override @@ -1783,7 +1786,7 @@ public class Char2FloatAVLTreeMap extends AbstractChar2FloatMap implements Char2 } @Override - public Char2FloatMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Char2FloatMap.Entry state = null; boolean empty = true; @@ -1795,19 +1798,19 @@ public class Char2FloatAVLTreeMap extends AbstractChar2FloatMap implements Char2 } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Char2FloatMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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 @@ -1902,7 +1905,7 @@ public class Char2FloatAVLTreeMap extends AbstractChar2FloatMap implements Char2 } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -1914,15 +1917,15 @@ public class Char2FloatAVLTreeMap extends AbstractChar2FloatMap implements Char2 } state = operator.applyAsFloat(state, entry.value); } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return 0F; + if(filter.test(entry.value)) return OptionalFloat.of(entry.value); + return OptionalFloat.empty(); } @Override @@ -2238,7 +2241,7 @@ public class Char2FloatAVLTreeMap extends AbstractChar2FloatMap implements Char2 } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -2250,15 +2253,15 @@ public class Char2FloatAVLTreeMap extends AbstractChar2FloatMap implements Char2 } state = operator.applyAsFloat(state, entry.value); } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return 0F; + if(filter.test(entry.value)) return OptionalFloat.of(entry.value); + return OptionalFloat.empty(); } @Override @@ -2399,7 +2402,7 @@ public class Char2FloatAVLTreeMap extends AbstractChar2FloatMap implements Char2 } @Override - public Char2FloatMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Char2FloatMap.Entry state = null; boolean empty = true; @@ -2411,19 +2414,19 @@ public class Char2FloatAVLTreeMap extends AbstractChar2FloatMap implements Char2 } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Char2FloatMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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/main/java/speiger/src/collections/chars/maps/impl/tree/Char2FloatRBTreeMap.java b/src/main/java/speiger/src/collections/chars/maps/impl/tree/Char2FloatRBTreeMap.java index b439296..a16919a 100644 --- a/src/main/java/speiger/src/collections/chars/maps/impl/tree/Char2FloatRBTreeMap.java +++ b/src/main/java/speiger/src/collections/chars/maps/impl/tree/Char2FloatRBTreeMap.java @@ -3,6 +3,7 @@ package speiger.src.collections.chars.maps.impl.tree; import java.util.Collections; import java.util.Map; import java.util.Objects; +import java.util.Optional; import java.util.NoSuchElementException; import java.util.function.Consumer; import java.util.function.BiFunction; @@ -17,6 +18,7 @@ import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntFloatConsumer; import speiger.src.collections.chars.functions.function.Char2FloatFunction; import speiger.src.collections.chars.functions.consumer.CharFloatConsumer; +import speiger.src.collections.chars.functions.OptionalChar; import speiger.src.collections.chars.functions.function.CharPredicate; import speiger.src.collections.chars.functions.function.CharFloatUnaryOperator; import speiger.src.collections.chars.functions.function.CharCharUnaryOperator; @@ -36,6 +38,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectFloatConsumer; import speiger.src.collections.floats.functions.function.FloatPredicate; +import speiger.src.collections.floats.functions.OptionalFloat; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOperator; @@ -1182,7 +1185,7 @@ public class Char2FloatRBTreeMap extends AbstractChar2FloatMap implements Char2F } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -1194,15 +1197,15 @@ public class Char2FloatRBTreeMap extends AbstractChar2FloatMap implements Char2F } state = operator.applyAsChar(state, entry.key); } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); for(Node entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.test(entry.key)) return entry.key; - return (char)0; + if(filter.test(entry.key)) return OptionalChar.of(entry.key); + return OptionalChar.empty(); } @Override @@ -1846,7 +1849,7 @@ public class Char2FloatRBTreeMap extends AbstractChar2FloatMap implements Char2F } @Override - public Char2FloatMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Char2FloatMap.Entry state = null; boolean empty = true; @@ -1858,19 +1861,19 @@ public class Char2FloatRBTreeMap extends AbstractChar2FloatMap implements Char2F } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Char2FloatMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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 @@ -1965,7 +1968,7 @@ public class Char2FloatRBTreeMap extends AbstractChar2FloatMap implements Char2F } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -1977,15 +1980,15 @@ public class Char2FloatRBTreeMap extends AbstractChar2FloatMap implements Char2F } state = operator.applyAsFloat(state, entry.value); } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return 0F; + if(filter.test(entry.value)) return OptionalFloat.of(entry.value); + return OptionalFloat.empty(); } @Override @@ -2301,7 +2304,7 @@ public class Char2FloatRBTreeMap extends AbstractChar2FloatMap implements Char2F } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -2313,15 +2316,15 @@ public class Char2FloatRBTreeMap extends AbstractChar2FloatMap implements Char2F } state = operator.applyAsFloat(state, entry.value); } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return 0F; + if(filter.test(entry.value)) return OptionalFloat.of(entry.value); + return OptionalFloat.empty(); } @Override @@ -2462,7 +2465,7 @@ public class Char2FloatRBTreeMap extends AbstractChar2FloatMap implements Char2F } @Override - public Char2FloatMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Char2FloatMap.Entry state = null; boolean empty = true; @@ -2474,19 +2477,19 @@ public class Char2FloatRBTreeMap extends AbstractChar2FloatMap implements Char2F } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Char2FloatMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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/main/java/speiger/src/collections/chars/maps/impl/tree/Char2IntAVLTreeMap.java b/src/main/java/speiger/src/collections/chars/maps/impl/tree/Char2IntAVLTreeMap.java index 0e4c487..09f1df0 100644 --- a/src/main/java/speiger/src/collections/chars/maps/impl/tree/Char2IntAVLTreeMap.java +++ b/src/main/java/speiger/src/collections/chars/maps/impl/tree/Char2IntAVLTreeMap.java @@ -3,11 +3,13 @@ package speiger.src.collections.chars.maps.impl.tree; import java.util.Collections; import java.util.Map; 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; import java.util.function.IntPredicate; +import java.util.OptionalInt; import speiger.src.collections.chars.collections.CharBidirectionalIterator; @@ -19,6 +21,7 @@ import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntIntConsumer; import speiger.src.collections.chars.functions.function.Char2IntFunction; import speiger.src.collections.chars.functions.consumer.CharIntConsumer; +import speiger.src.collections.chars.functions.OptionalChar; import speiger.src.collections.chars.functions.function.CharPredicate; import speiger.src.collections.chars.functions.function.CharIntUnaryOperator; import speiger.src.collections.chars.functions.function.CharCharUnaryOperator; @@ -1129,7 +1132,7 @@ public class Char2IntAVLTreeMap extends AbstractChar2IntMap implements Char2IntN } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -1141,15 +1144,15 @@ public class Char2IntAVLTreeMap extends AbstractChar2IntMap implements Char2IntN } state = operator.applyAsChar(state, entry.key); } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); for(Node entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.test(entry.key)) return entry.key; - return (char)0; + if(filter.test(entry.key)) return OptionalChar.of(entry.key); + return OptionalChar.empty(); } @Override @@ -1783,7 +1786,7 @@ public class Char2IntAVLTreeMap extends AbstractChar2IntMap implements Char2IntN } @Override - public Char2IntMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Char2IntMap.Entry state = null; boolean empty = true; @@ -1795,19 +1798,19 @@ public class Char2IntAVLTreeMap extends AbstractChar2IntMap implements Char2IntN } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Char2IntMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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 @@ -1902,7 +1905,7 @@ public class Char2IntAVLTreeMap extends AbstractChar2IntMap implements Char2IntN } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -1914,15 +1917,15 @@ public class Char2IntAVLTreeMap extends AbstractChar2IntMap implements Char2IntN } state = operator.applyAsInt(state, entry.value); } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return 0; + if(filter.test(entry.value)) return OptionalInt.of(entry.value); + return OptionalInt.empty(); } @Override @@ -2238,7 +2241,7 @@ public class Char2IntAVLTreeMap extends AbstractChar2IntMap implements Char2IntN } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -2250,15 +2253,15 @@ public class Char2IntAVLTreeMap extends AbstractChar2IntMap implements Char2IntN } state = operator.applyAsInt(state, entry.value); } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return 0; + if(filter.test(entry.value)) return OptionalInt.of(entry.value); + return OptionalInt.empty(); } @Override @@ -2399,7 +2402,7 @@ public class Char2IntAVLTreeMap extends AbstractChar2IntMap implements Char2IntN } @Override - public Char2IntMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Char2IntMap.Entry state = null; boolean empty = true; @@ -2411,19 +2414,19 @@ public class Char2IntAVLTreeMap extends AbstractChar2IntMap implements Char2IntN } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Char2IntMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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/main/java/speiger/src/collections/chars/maps/impl/tree/Char2IntRBTreeMap.java b/src/main/java/speiger/src/collections/chars/maps/impl/tree/Char2IntRBTreeMap.java index c5581d6..919f79b 100644 --- a/src/main/java/speiger/src/collections/chars/maps/impl/tree/Char2IntRBTreeMap.java +++ b/src/main/java/speiger/src/collections/chars/maps/impl/tree/Char2IntRBTreeMap.java @@ -3,11 +3,13 @@ package speiger.src.collections.chars.maps.impl.tree; import java.util.Collections; import java.util.Map; 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; import java.util.function.IntPredicate; +import java.util.OptionalInt; import speiger.src.collections.chars.collections.CharBidirectionalIterator; import speiger.src.collections.chars.functions.CharComparator; @@ -18,6 +20,7 @@ import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntIntConsumer; import speiger.src.collections.chars.functions.function.Char2IntFunction; import speiger.src.collections.chars.functions.consumer.CharIntConsumer; +import speiger.src.collections.chars.functions.OptionalChar; import speiger.src.collections.chars.functions.function.CharPredicate; import speiger.src.collections.chars.functions.function.CharIntUnaryOperator; import speiger.src.collections.chars.functions.function.CharCharUnaryOperator; @@ -1182,7 +1185,7 @@ public class Char2IntRBTreeMap extends AbstractChar2IntMap implements Char2IntNa } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -1194,15 +1197,15 @@ public class Char2IntRBTreeMap extends AbstractChar2IntMap implements Char2IntNa } state = operator.applyAsChar(state, entry.key); } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); for(Node entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.test(entry.key)) return entry.key; - return (char)0; + if(filter.test(entry.key)) return OptionalChar.of(entry.key); + return OptionalChar.empty(); } @Override @@ -1846,7 +1849,7 @@ public class Char2IntRBTreeMap extends AbstractChar2IntMap implements Char2IntNa } @Override - public Char2IntMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Char2IntMap.Entry state = null; boolean empty = true; @@ -1858,19 +1861,19 @@ public class Char2IntRBTreeMap extends AbstractChar2IntMap implements Char2IntNa } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Char2IntMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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 @@ -1965,7 +1968,7 @@ public class Char2IntRBTreeMap extends AbstractChar2IntMap implements Char2IntNa } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -1977,15 +1980,15 @@ public class Char2IntRBTreeMap extends AbstractChar2IntMap implements Char2IntNa } state = operator.applyAsInt(state, entry.value); } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return 0; + if(filter.test(entry.value)) return OptionalInt.of(entry.value); + return OptionalInt.empty(); } @Override @@ -2301,7 +2304,7 @@ public class Char2IntRBTreeMap extends AbstractChar2IntMap implements Char2IntNa } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -2313,15 +2316,15 @@ public class Char2IntRBTreeMap extends AbstractChar2IntMap implements Char2IntNa } state = operator.applyAsInt(state, entry.value); } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return 0; + if(filter.test(entry.value)) return OptionalInt.of(entry.value); + return OptionalInt.empty(); } @Override @@ -2462,7 +2465,7 @@ public class Char2IntRBTreeMap extends AbstractChar2IntMap implements Char2IntNa } @Override - public Char2IntMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Char2IntMap.Entry state = null; boolean empty = true; @@ -2474,19 +2477,19 @@ public class Char2IntRBTreeMap extends AbstractChar2IntMap implements Char2IntNa } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Char2IntMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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/main/java/speiger/src/collections/chars/maps/impl/tree/Char2LongAVLTreeMap.java b/src/main/java/speiger/src/collections/chars/maps/impl/tree/Char2LongAVLTreeMap.java index 233d69f..67ee2c9 100644 --- a/src/main/java/speiger/src/collections/chars/maps/impl/tree/Char2LongAVLTreeMap.java +++ b/src/main/java/speiger/src/collections/chars/maps/impl/tree/Char2LongAVLTreeMap.java @@ -3,11 +3,13 @@ package speiger.src.collections.chars.maps.impl.tree; import java.util.Collections; import java.util.Map; 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; import java.util.function.LongPredicate; +import java.util.OptionalLong; import speiger.src.collections.chars.collections.CharBidirectionalIterator; @@ -19,6 +21,7 @@ import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntLongConsumer; import speiger.src.collections.chars.functions.function.Char2LongFunction; import speiger.src.collections.chars.functions.consumer.CharLongConsumer; +import speiger.src.collections.chars.functions.OptionalChar; import speiger.src.collections.chars.functions.function.CharPredicate; import speiger.src.collections.chars.functions.function.CharLongUnaryOperator; import speiger.src.collections.chars.functions.function.CharCharUnaryOperator; @@ -1129,7 +1132,7 @@ public class Char2LongAVLTreeMap extends AbstractChar2LongMap implements Char2Lo } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -1141,15 +1144,15 @@ public class Char2LongAVLTreeMap extends AbstractChar2LongMap implements Char2Lo } state = operator.applyAsChar(state, entry.key); } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); for(Node entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.test(entry.key)) return entry.key; - return (char)0; + if(filter.test(entry.key)) return OptionalChar.of(entry.key); + return OptionalChar.empty(); } @Override @@ -1783,7 +1786,7 @@ public class Char2LongAVLTreeMap extends AbstractChar2LongMap implements Char2Lo } @Override - public Char2LongMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Char2LongMap.Entry state = null; boolean empty = true; @@ -1795,19 +1798,19 @@ public class Char2LongAVLTreeMap extends AbstractChar2LongMap implements Char2Lo } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Char2LongMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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 @@ -1902,7 +1905,7 @@ public class Char2LongAVLTreeMap extends AbstractChar2LongMap implements Char2Lo } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -1914,15 +1917,15 @@ public class Char2LongAVLTreeMap extends AbstractChar2LongMap implements Char2Lo } state = operator.applyAsLong(state, entry.value); } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return 0L; + if(filter.test(entry.value)) return OptionalLong.of(entry.value); + return OptionalLong.empty(); } @Override @@ -2238,7 +2241,7 @@ public class Char2LongAVLTreeMap extends AbstractChar2LongMap implements Char2Lo } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -2250,15 +2253,15 @@ public class Char2LongAVLTreeMap extends AbstractChar2LongMap implements Char2Lo } state = operator.applyAsLong(state, entry.value); } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return 0L; + if(filter.test(entry.value)) return OptionalLong.of(entry.value); + return OptionalLong.empty(); } @Override @@ -2399,7 +2402,7 @@ public class Char2LongAVLTreeMap extends AbstractChar2LongMap implements Char2Lo } @Override - public Char2LongMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Char2LongMap.Entry state = null; boolean empty = true; @@ -2411,19 +2414,19 @@ public class Char2LongAVLTreeMap extends AbstractChar2LongMap implements Char2Lo } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Char2LongMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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/main/java/speiger/src/collections/chars/maps/impl/tree/Char2LongRBTreeMap.java b/src/main/java/speiger/src/collections/chars/maps/impl/tree/Char2LongRBTreeMap.java index f2cd6e6..492c0b8 100644 --- a/src/main/java/speiger/src/collections/chars/maps/impl/tree/Char2LongRBTreeMap.java +++ b/src/main/java/speiger/src/collections/chars/maps/impl/tree/Char2LongRBTreeMap.java @@ -3,11 +3,13 @@ package speiger.src.collections.chars.maps.impl.tree; import java.util.Collections; import java.util.Map; 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; import java.util.function.LongPredicate; +import java.util.OptionalLong; import speiger.src.collections.chars.collections.CharBidirectionalIterator; import speiger.src.collections.chars.functions.CharComparator; @@ -18,6 +20,7 @@ import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntLongConsumer; import speiger.src.collections.chars.functions.function.Char2LongFunction; import speiger.src.collections.chars.functions.consumer.CharLongConsumer; +import speiger.src.collections.chars.functions.OptionalChar; import speiger.src.collections.chars.functions.function.CharPredicate; import speiger.src.collections.chars.functions.function.CharLongUnaryOperator; import speiger.src.collections.chars.functions.function.CharCharUnaryOperator; @@ -1182,7 +1185,7 @@ public class Char2LongRBTreeMap extends AbstractChar2LongMap implements Char2Lon } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -1194,15 +1197,15 @@ public class Char2LongRBTreeMap extends AbstractChar2LongMap implements Char2Lon } state = operator.applyAsChar(state, entry.key); } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); for(Node entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.test(entry.key)) return entry.key; - return (char)0; + if(filter.test(entry.key)) return OptionalChar.of(entry.key); + return OptionalChar.empty(); } @Override @@ -1846,7 +1849,7 @@ public class Char2LongRBTreeMap extends AbstractChar2LongMap implements Char2Lon } @Override - public Char2LongMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Char2LongMap.Entry state = null; boolean empty = true; @@ -1858,19 +1861,19 @@ public class Char2LongRBTreeMap extends AbstractChar2LongMap implements Char2Lon } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Char2LongMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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 @@ -1965,7 +1968,7 @@ public class Char2LongRBTreeMap extends AbstractChar2LongMap implements Char2Lon } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -1977,15 +1980,15 @@ public class Char2LongRBTreeMap extends AbstractChar2LongMap implements Char2Lon } state = operator.applyAsLong(state, entry.value); } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return 0L; + if(filter.test(entry.value)) return OptionalLong.of(entry.value); + return OptionalLong.empty(); } @Override @@ -2301,7 +2304,7 @@ public class Char2LongRBTreeMap extends AbstractChar2LongMap implements Char2Lon } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -2313,15 +2316,15 @@ public class Char2LongRBTreeMap extends AbstractChar2LongMap implements Char2Lon } state = operator.applyAsLong(state, entry.value); } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return 0L; + if(filter.test(entry.value)) return OptionalLong.of(entry.value); + return OptionalLong.empty(); } @Override @@ -2462,7 +2465,7 @@ public class Char2LongRBTreeMap extends AbstractChar2LongMap implements Char2Lon } @Override - public Char2LongMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Char2LongMap.Entry state = null; boolean empty = true; @@ -2474,19 +2477,19 @@ public class Char2LongRBTreeMap extends AbstractChar2LongMap implements Char2Lon } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Char2LongMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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/main/java/speiger/src/collections/chars/maps/impl/tree/Char2ObjectAVLTreeMap.java b/src/main/java/speiger/src/collections/chars/maps/impl/tree/Char2ObjectAVLTreeMap.java index d6ec056..60c4167 100644 --- a/src/main/java/speiger/src/collections/chars/maps/impl/tree/Char2ObjectAVLTreeMap.java +++ b/src/main/java/speiger/src/collections/chars/maps/impl/tree/Char2ObjectAVLTreeMap.java @@ -3,6 +3,7 @@ package speiger.src.collections.chars.maps.impl.tree; import java.util.Collections; import java.util.Map; import java.util.Objects; +import java.util.Optional; import java.util.NoSuchElementException; import java.util.function.Consumer; import java.util.function.BiFunction; @@ -17,6 +18,7 @@ import speiger.src.collections.ints.functions.consumer.IntCharConsumer; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.chars.functions.function.CharFunction; import speiger.src.collections.chars.functions.consumer.CharObjectConsumer; +import speiger.src.collections.chars.functions.OptionalChar; import speiger.src.collections.chars.functions.function.CharPredicate; import speiger.src.collections.chars.functions.function.CharObjectUnaryOperator; import speiger.src.collections.chars.functions.function.CharCharUnaryOperator; @@ -1017,7 +1019,7 @@ public class Char2ObjectAVLTreeMap extends AbstractChar2ObjectMap implemen } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -1029,15 +1031,15 @@ public class Char2ObjectAVLTreeMap extends AbstractChar2ObjectMap implemen } state = operator.applyAsChar(state, entry.key); } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); for(Node entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.test(entry.key)) return entry.key; - return (char)0; + if(filter.test(entry.key)) return OptionalChar.of(entry.key); + return OptionalChar.empty(); } @Override @@ -1664,7 +1666,7 @@ public class Char2ObjectAVLTreeMap extends AbstractChar2ObjectMap implemen } @Override - public Char2ObjectMap.Entry reduce(ObjectObjectUnaryOperator, Char2ObjectMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Char2ObjectMap.Entry> operator) { Objects.requireNonNull(operator); Char2ObjectMap.Entry state = null; boolean empty = true; @@ -1676,19 +1678,19 @@ public class Char2ObjectAVLTreeMap extends AbstractChar2ObjectMap implemen } state = operator.apply(state, new BasicEntry<>(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Char2ObjectMap.Entry findFirst(Predicate> filter) { + public Optional> findFirst(Predicate> filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry<>(); for(Node 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 @@ -1783,7 +1785,7 @@ public class Char2ObjectAVLTreeMap extends AbstractChar2ObjectMap implemen } @Override - public V reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); V state = null; boolean empty = true; @@ -1795,15 +1797,15 @@ public class Char2ObjectAVLTreeMap extends AbstractChar2ObjectMap implemen } state = operator.apply(state, entry.value); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public V findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return null; + if(filter.test(entry.value)) return Optional.ofNullable(entry.value); + return Optional.empty(); } @Override @@ -2119,7 +2121,7 @@ public class Char2ObjectAVLTreeMap extends AbstractChar2ObjectMap implemen } @Override - public V reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); V state = null; boolean empty = true; @@ -2131,15 +2133,15 @@ public class Char2ObjectAVLTreeMap extends AbstractChar2ObjectMap implemen } state = operator.apply(state, entry.value); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public V findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return null; + if(filter.test(entry.value)) return Optional.ofNullable(entry.value); + return Optional.empty(); } @Override @@ -2280,7 +2282,7 @@ public class Char2ObjectAVLTreeMap extends AbstractChar2ObjectMap implemen } @Override - public Char2ObjectMap.Entry reduce(ObjectObjectUnaryOperator, Char2ObjectMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Char2ObjectMap.Entry> operator) { Objects.requireNonNull(operator); Char2ObjectMap.Entry state = null; boolean empty = true; @@ -2292,19 +2294,19 @@ public class Char2ObjectAVLTreeMap extends AbstractChar2ObjectMap implemen } state = operator.apply(state, new BasicEntry<>(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Char2ObjectMap.Entry findFirst(Predicate> filter) { + public Optional> findFirst(Predicate> filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry<>(); for(Node 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/main/java/speiger/src/collections/chars/maps/impl/tree/Char2ObjectRBTreeMap.java b/src/main/java/speiger/src/collections/chars/maps/impl/tree/Char2ObjectRBTreeMap.java index c4d94cf..80bfed4 100644 --- a/src/main/java/speiger/src/collections/chars/maps/impl/tree/Char2ObjectRBTreeMap.java +++ b/src/main/java/speiger/src/collections/chars/maps/impl/tree/Char2ObjectRBTreeMap.java @@ -3,6 +3,7 @@ package speiger.src.collections.chars.maps.impl.tree; import java.util.Collections; import java.util.Map; import java.util.Objects; +import java.util.Optional; import java.util.NoSuchElementException; import java.util.function.Consumer; import java.util.function.BiFunction; @@ -16,6 +17,7 @@ import speiger.src.collections.ints.functions.consumer.IntCharConsumer; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.chars.functions.function.CharFunction; import speiger.src.collections.chars.functions.consumer.CharObjectConsumer; +import speiger.src.collections.chars.functions.OptionalChar; import speiger.src.collections.chars.functions.function.CharPredicate; import speiger.src.collections.chars.functions.function.CharObjectUnaryOperator; import speiger.src.collections.chars.functions.function.CharCharUnaryOperator; @@ -1070,7 +1072,7 @@ public class Char2ObjectRBTreeMap extends AbstractChar2ObjectMap implement } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -1082,15 +1084,15 @@ public class Char2ObjectRBTreeMap extends AbstractChar2ObjectMap implement } state = operator.applyAsChar(state, entry.key); } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); for(Node entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.test(entry.key)) return entry.key; - return (char)0; + if(filter.test(entry.key)) return OptionalChar.of(entry.key); + return OptionalChar.empty(); } @Override @@ -1727,7 +1729,7 @@ public class Char2ObjectRBTreeMap extends AbstractChar2ObjectMap implement } @Override - public Char2ObjectMap.Entry reduce(ObjectObjectUnaryOperator, Char2ObjectMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Char2ObjectMap.Entry> operator) { Objects.requireNonNull(operator); Char2ObjectMap.Entry state = null; boolean empty = true; @@ -1739,19 +1741,19 @@ public class Char2ObjectRBTreeMap extends AbstractChar2ObjectMap implement } state = operator.apply(state, new BasicEntry<>(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Char2ObjectMap.Entry findFirst(Predicate> filter) { + public Optional> findFirst(Predicate> filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry<>(); for(Node 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 @@ -1846,7 +1848,7 @@ public class Char2ObjectRBTreeMap extends AbstractChar2ObjectMap implement } @Override - public V reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); V state = null; boolean empty = true; @@ -1858,15 +1860,15 @@ public class Char2ObjectRBTreeMap extends AbstractChar2ObjectMap implement } state = operator.apply(state, entry.value); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public V findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return null; + if(filter.test(entry.value)) return Optional.ofNullable(entry.value); + return Optional.empty(); } @Override @@ -2182,7 +2184,7 @@ public class Char2ObjectRBTreeMap extends AbstractChar2ObjectMap implement } @Override - public V reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); V state = null; boolean empty = true; @@ -2194,15 +2196,15 @@ public class Char2ObjectRBTreeMap extends AbstractChar2ObjectMap implement } state = operator.apply(state, entry.value); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public V findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return null; + if(filter.test(entry.value)) return Optional.ofNullable(entry.value); + return Optional.empty(); } @Override @@ -2343,7 +2345,7 @@ public class Char2ObjectRBTreeMap extends AbstractChar2ObjectMap implement } @Override - public Char2ObjectMap.Entry reduce(ObjectObjectUnaryOperator, Char2ObjectMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Char2ObjectMap.Entry> operator) { Objects.requireNonNull(operator); Char2ObjectMap.Entry state = null; boolean empty = true; @@ -2355,19 +2357,19 @@ public class Char2ObjectRBTreeMap extends AbstractChar2ObjectMap implement } state = operator.apply(state, new BasicEntry<>(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Char2ObjectMap.Entry findFirst(Predicate> filter) { + public Optional> findFirst(Predicate> filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry<>(); for(Node 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/main/java/speiger/src/collections/chars/maps/impl/tree/Char2ShortAVLTreeMap.java b/src/main/java/speiger/src/collections/chars/maps/impl/tree/Char2ShortAVLTreeMap.java index 4ad1975..e1ef6c2 100644 --- a/src/main/java/speiger/src/collections/chars/maps/impl/tree/Char2ShortAVLTreeMap.java +++ b/src/main/java/speiger/src/collections/chars/maps/impl/tree/Char2ShortAVLTreeMap.java @@ -3,6 +3,7 @@ package speiger.src.collections.chars.maps.impl.tree; import java.util.Collections; import java.util.Map; import java.util.Objects; +import java.util.Optional; import java.util.NoSuchElementException; import java.util.function.Consumer; import java.util.function.BiFunction; @@ -18,6 +19,7 @@ import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntShortConsumer; import speiger.src.collections.chars.functions.function.Char2ShortFunction; import speiger.src.collections.chars.functions.consumer.CharShortConsumer; +import speiger.src.collections.chars.functions.OptionalChar; import speiger.src.collections.chars.functions.function.CharPredicate; import speiger.src.collections.chars.functions.function.CharShortUnaryOperator; import speiger.src.collections.chars.functions.function.CharCharUnaryOperator; @@ -37,6 +39,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectShortConsumer; import speiger.src.collections.shorts.functions.function.ShortPredicate; +import speiger.src.collections.shorts.functions.OptionalShort; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOperator; @@ -1129,7 +1132,7 @@ public class Char2ShortAVLTreeMap extends AbstractChar2ShortMap implements Char2 } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -1141,15 +1144,15 @@ public class Char2ShortAVLTreeMap extends AbstractChar2ShortMap implements Char2 } state = operator.applyAsChar(state, entry.key); } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); for(Node entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.test(entry.key)) return entry.key; - return (char)0; + if(filter.test(entry.key)) return OptionalChar.of(entry.key); + return OptionalChar.empty(); } @Override @@ -1783,7 +1786,7 @@ public class Char2ShortAVLTreeMap extends AbstractChar2ShortMap implements Char2 } @Override - public Char2ShortMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Char2ShortMap.Entry state = null; boolean empty = true; @@ -1795,19 +1798,19 @@ public class Char2ShortAVLTreeMap extends AbstractChar2ShortMap implements Char2 } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Char2ShortMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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 @@ -1902,7 +1905,7 @@ public class Char2ShortAVLTreeMap extends AbstractChar2ShortMap implements Char2 } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -1914,15 +1917,15 @@ public class Char2ShortAVLTreeMap extends AbstractChar2ShortMap implements Char2 } state = operator.applyAsShort(state, entry.value); } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return (short)0; + if(filter.test(entry.value)) return OptionalShort.of(entry.value); + return OptionalShort.empty(); } @Override @@ -2238,7 +2241,7 @@ public class Char2ShortAVLTreeMap extends AbstractChar2ShortMap implements Char2 } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -2250,15 +2253,15 @@ public class Char2ShortAVLTreeMap extends AbstractChar2ShortMap implements Char2 } state = operator.applyAsShort(state, entry.value); } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return (short)0; + if(filter.test(entry.value)) return OptionalShort.of(entry.value); + return OptionalShort.empty(); } @Override @@ -2399,7 +2402,7 @@ public class Char2ShortAVLTreeMap extends AbstractChar2ShortMap implements Char2 } @Override - public Char2ShortMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Char2ShortMap.Entry state = null; boolean empty = true; @@ -2411,19 +2414,19 @@ public class Char2ShortAVLTreeMap extends AbstractChar2ShortMap implements Char2 } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Char2ShortMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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/main/java/speiger/src/collections/chars/maps/impl/tree/Char2ShortRBTreeMap.java b/src/main/java/speiger/src/collections/chars/maps/impl/tree/Char2ShortRBTreeMap.java index 7edd274..a70d3e1 100644 --- a/src/main/java/speiger/src/collections/chars/maps/impl/tree/Char2ShortRBTreeMap.java +++ b/src/main/java/speiger/src/collections/chars/maps/impl/tree/Char2ShortRBTreeMap.java @@ -3,6 +3,7 @@ package speiger.src.collections.chars.maps.impl.tree; import java.util.Collections; import java.util.Map; import java.util.Objects; +import java.util.Optional; import java.util.NoSuchElementException; import java.util.function.Consumer; import java.util.function.BiFunction; @@ -17,6 +18,7 @@ import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntShortConsumer; import speiger.src.collections.chars.functions.function.Char2ShortFunction; import speiger.src.collections.chars.functions.consumer.CharShortConsumer; +import speiger.src.collections.chars.functions.OptionalChar; import speiger.src.collections.chars.functions.function.CharPredicate; import speiger.src.collections.chars.functions.function.CharShortUnaryOperator; import speiger.src.collections.chars.functions.function.CharCharUnaryOperator; @@ -36,6 +38,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectShortConsumer; import speiger.src.collections.shorts.functions.function.ShortPredicate; +import speiger.src.collections.shorts.functions.OptionalShort; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOperator; @@ -1182,7 +1185,7 @@ public class Char2ShortRBTreeMap extends AbstractChar2ShortMap implements Char2S } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -1194,15 +1197,15 @@ public class Char2ShortRBTreeMap extends AbstractChar2ShortMap implements Char2S } state = operator.applyAsChar(state, entry.key); } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); for(Node entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.test(entry.key)) return entry.key; - return (char)0; + if(filter.test(entry.key)) return OptionalChar.of(entry.key); + return OptionalChar.empty(); } @Override @@ -1846,7 +1849,7 @@ public class Char2ShortRBTreeMap extends AbstractChar2ShortMap implements Char2S } @Override - public Char2ShortMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Char2ShortMap.Entry state = null; boolean empty = true; @@ -1858,19 +1861,19 @@ public class Char2ShortRBTreeMap extends AbstractChar2ShortMap implements Char2S } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Char2ShortMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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 @@ -1965,7 +1968,7 @@ public class Char2ShortRBTreeMap extends AbstractChar2ShortMap implements Char2S } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -1977,15 +1980,15 @@ public class Char2ShortRBTreeMap extends AbstractChar2ShortMap implements Char2S } state = operator.applyAsShort(state, entry.value); } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return (short)0; + if(filter.test(entry.value)) return OptionalShort.of(entry.value); + return OptionalShort.empty(); } @Override @@ -2301,7 +2304,7 @@ public class Char2ShortRBTreeMap extends AbstractChar2ShortMap implements Char2S } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -2313,15 +2316,15 @@ public class Char2ShortRBTreeMap extends AbstractChar2ShortMap implements Char2S } state = operator.applyAsShort(state, entry.value); } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return (short)0; + if(filter.test(entry.value)) return OptionalShort.of(entry.value); + return OptionalShort.empty(); } @Override @@ -2462,7 +2465,7 @@ public class Char2ShortRBTreeMap extends AbstractChar2ShortMap implements Char2S } @Override - public Char2ShortMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Char2ShortMap.Entry state = null; boolean empty = true; @@ -2474,19 +2477,19 @@ public class Char2ShortRBTreeMap extends AbstractChar2ShortMap implements Char2S } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Char2ShortMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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/main/java/speiger/src/collections/chars/queues/CharArrayFIFOQueue.java b/src/main/java/speiger/src/collections/chars/queues/CharArrayFIFOQueue.java index fbd78a4..282c2d7 100644 --- a/src/main/java/speiger/src/collections/chars/queues/CharArrayFIFOQueue.java +++ b/src/main/java/speiger/src/collections/chars/queues/CharArrayFIFOQueue.java @@ -5,6 +5,7 @@ import java.util.Objects; import java.util.NoSuchElementException; import speiger.src.collections.chars.collections.CharIterator; +import speiger.src.collections.chars.functions.OptionalChar; import speiger.src.collections.chars.functions.CharComparator; import speiger.src.collections.chars.functions.CharConsumer; import speiger.src.collections.ints.functions.consumer.IntCharConsumer; @@ -278,17 +279,17 @@ public class CharArrayFIFOQueue extends AbstractCharPriorityQueue implements Cha } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); for(int i = 0,m=size();i= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor)); + } + + @Override + public void addLast(char o) { + if(strategy.equals(o, (char)0)) { + if(containsNull) return; + containsNull = true; + onNodeAdded(nullIndex); + } + else { + int pos = HashUtil.mix(strategy.hashCode(o)) & mask; + while(!strategy.equals(keys[pos], (char)0)) { + if(strategy.equals(keys[pos], o)) return; + pos = ++pos & mask; + } + keys[pos] = o; + onNodeAdded(pos); + } + if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor)); + } + @Override public boolean addAndMoveToFirst(char o) { if(strategy.equals(o, (char)0)) { @@ -618,7 +659,7 @@ public class CharLinkedOpenCustomHashSet extends CharOpenCustomHashSet implement } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -631,18 +672,18 @@ public class CharLinkedOpenCustomHashSet extends CharOpenCustomHashSet implement else state = operator.applyAsChar(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); int index = firstIndex; while(index != -1) { - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalChar.of(keys[index]); index = (int)links[index]; } - return (char)0; + return OptionalChar.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/chars/sets/CharLinkedOpenHashSet.java b/src/main/java/speiger/src/collections/chars/sets/CharLinkedOpenHashSet.java index 327a90d..7c6d365 100644 --- a/src/main/java/speiger/src/collections/chars/sets/CharLinkedOpenHashSet.java +++ b/src/main/java/speiger/src/collections/chars/sets/CharLinkedOpenHashSet.java @@ -10,6 +10,7 @@ import speiger.src.collections.chars.collections.CharCollection; import speiger.src.collections.chars.collections.CharIterator; import speiger.src.collections.chars.collections.CharBidirectionalIterator; import speiger.src.collections.chars.functions.CharConsumer; +import speiger.src.collections.chars.functions.OptionalChar; import speiger.src.collections.ints.functions.consumer.IntCharConsumer; import speiger.src.collections.objects.functions.consumer.ObjectCharConsumer; import speiger.src.collections.chars.functions.function.CharPredicate; @@ -183,6 +184,46 @@ public class CharLinkedOpenHashSet extends CharOpenHashSet implements CharOrdere while(iterator.hasNext()) add(iterator.nextChar()); } + @Override + public void addFirst(char o) { + if(o == (char)0) { + if(containsNull) return; + containsNull = true; + onNodeAdded(nullIndex); + moveToFirstIndex(nullIndex); + } + else { + int pos = HashUtil.mix(Character.hashCode(o)) & mask; + while(keys[pos] != (char)0) { + if(keys[pos] == o) return; + pos = ++pos & mask; + } + keys[pos] = o; + onNodeAdded(pos); + moveToFirstIndex(pos); + } + if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor)); + } + + @Override + public void addLast(char o) { + if(o == (char)0) { + if(containsNull) return; + containsNull = true; + onNodeAdded(nullIndex); + } + else { + int pos = HashUtil.mix(Character.hashCode(o)) & mask; + while(keys[pos] != (char)0) { + if(keys[pos] == o) return; + pos = ++pos & mask; + } + keys[pos] = o; + onNodeAdded(pos); + } + if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor)); + } + @Override public boolean addAndMoveToFirst(char o) { if(o == (char)0) { @@ -471,7 +512,7 @@ public class CharLinkedOpenHashSet extends CharOpenHashSet implements CharOrdere } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -484,18 +525,18 @@ public class CharLinkedOpenHashSet extends CharOpenHashSet implements CharOrdere else state = operator.applyAsChar(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); int index = firstIndex; while(index != -1) { - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalChar.of(keys[index]); index = (int)links[index]; } - return (char)0; + return OptionalChar.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/chars/sets/CharOpenCustomHashSet.java b/src/main/java/speiger/src/collections/chars/sets/CharOpenCustomHashSet.java index 3cc1e78..cd241d4 100644 --- a/src/main/java/speiger/src/collections/chars/sets/CharOpenCustomHashSet.java +++ b/src/main/java/speiger/src/collections/chars/sets/CharOpenCustomHashSet.java @@ -9,6 +9,7 @@ import java.util.Objects; import speiger.src.collections.chars.collections.CharCollection; import speiger.src.collections.chars.collections.CharIterator; +import speiger.src.collections.chars.functions.OptionalChar; import speiger.src.collections.chars.utils.CharIterators; import speiger.src.collections.chars.functions.CharConsumer; import speiger.src.collections.ints.functions.consumer.IntCharConsumer; @@ -546,7 +547,7 @@ public class CharOpenCustomHashSet extends AbstractCharSet implements ITrimmable } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -563,18 +564,18 @@ public class CharOpenCustomHashSet extends AbstractCharSet implements ITrimmable } state = operator.applyAsChar(state, keys[i]); } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (char)0; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OptionalChar.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OptionalChar.of(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], (char)0) && filter.test(keys[i])) return keys[i]; + if(!strategy.equals(keys[i], (char)0) && filter.test(keys[i])) return OptionalChar.of(keys[i]); } - return (char)0; + return OptionalChar.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/chars/sets/CharOpenHashSet.java b/src/main/java/speiger/src/collections/chars/sets/CharOpenHashSet.java index a16e117..6a65b33 100644 --- a/src/main/java/speiger/src/collections/chars/sets/CharOpenHashSet.java +++ b/src/main/java/speiger/src/collections/chars/sets/CharOpenHashSet.java @@ -11,6 +11,7 @@ import speiger.src.collections.chars.collections.CharCollection; import speiger.src.collections.chars.collections.CharIterator; import speiger.src.collections.chars.utils.CharIterators; import speiger.src.collections.chars.functions.CharConsumer; +import speiger.src.collections.chars.functions.OptionalChar; import speiger.src.collections.ints.functions.consumer.IntCharConsumer; import speiger.src.collections.objects.functions.consumer.ObjectCharConsumer; import speiger.src.collections.chars.functions.function.CharPredicate; @@ -428,7 +429,7 @@ public class CharOpenHashSet extends AbstractCharSet implements ITrimmable } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -445,18 +446,18 @@ public class CharOpenHashSet extends AbstractCharSet implements ITrimmable } state = operator.applyAsChar(state, keys[i]); } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (char)0; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OptionalChar.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OptionalChar.of(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(keys[i] != (char)0 && filter.test(keys[i])) return keys[i]; + if(keys[i] != (char)0 && filter.test(keys[i])) return OptionalChar.of(keys[i]); } - return (char)0; + return OptionalChar.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/chars/sets/CharOrderedSet.java b/src/main/java/speiger/src/collections/chars/sets/CharOrderedSet.java index 3c49f85..f11e5d2 100644 --- a/src/main/java/speiger/src/collections/chars/sets/CharOrderedSet.java +++ b/src/main/java/speiger/src/collections/chars/sets/CharOrderedSet.java @@ -1,6 +1,7 @@ package speiger.src.collections.chars.sets; import speiger.src.collections.chars.collections.CharBidirectionalIterator; +import speiger.src.collections.chars.collections.CharOrderedCollection; import speiger.src.collections.chars.collections.CharSplititerator; import speiger.src.collections.chars.utils.CharSplititerators; import speiger.src.collections.chars.sets.AbstractCharSet.ReversedCharOrderedSet; @@ -13,7 +14,7 @@ import speiger.src.collections.chars.utils.CharSets; * This interface provides basic access to such functions while also providing some Sorted/NaivgableSet implementations that still fit into here. * */ -public interface CharOrderedSet extends CharSet +public interface CharOrderedSet extends CharSet, CharOrderedCollection { /** * A customized add method that allows you to insert into the first index. diff --git a/src/main/java/speiger/src/collections/chars/sets/CharRBTreeSet.java b/src/main/java/speiger/src/collections/chars/sets/CharRBTreeSet.java index 7ff693a..d08e20b 100644 --- a/src/main/java/speiger/src/collections/chars/sets/CharRBTreeSet.java +++ b/src/main/java/speiger/src/collections/chars/sets/CharRBTreeSet.java @@ -9,6 +9,7 @@ import java.util.Objects; import speiger.src.collections.chars.collections.CharBidirectionalIterator; import speiger.src.collections.chars.functions.CharComparator; import speiger.src.collections.chars.functions.CharConsumer; +import speiger.src.collections.chars.functions.OptionalChar; import speiger.src.collections.ints.functions.consumer.IntCharConsumer; import speiger.src.collections.objects.functions.consumer.ObjectCharConsumer; import speiger.src.collections.chars.functions.function.CharPredicate; @@ -334,7 +335,7 @@ public class CharRBTreeSet extends AbstractCharSet implements CharNavigableSet } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -346,16 +347,16 @@ public class CharRBTreeSet extends AbstractCharSet implements CharNavigableSet } state = operator.applyAsChar(state, entry.key); } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); for(Entry entry = first;entry != null;entry = entry.next()) { - if(filter.test(entry.key)) return entry.key; + if(filter.test(entry.key)) return OptionalChar.of(entry.key); } - return (char)0; + return OptionalChar.empty(); } @Override @@ -1241,7 +1242,7 @@ public class CharRBTreeSet extends AbstractCharSet implements CharNavigableSet } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -1253,16 +1254,16 @@ public class CharRBTreeSet extends AbstractCharSet implements CharNavigableSet } state = operator.applyAsChar(state, entry.key); } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); for(Entry entry = start();entry != null && inRange(entry.key);entry = next(entry)) { - if(filter.test(entry.key)) return entry.key; + if(filter.test(entry.key)) return OptionalChar.of(entry.key); } - return (char)0; + return OptionalChar.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/chars/sets/ImmutableCharOpenHashSet.java b/src/main/java/speiger/src/collections/chars/sets/ImmutableCharOpenHashSet.java index 6fdfaf7..fc125d4 100644 --- a/src/main/java/speiger/src/collections/chars/sets/ImmutableCharOpenHashSet.java +++ b/src/main/java/speiger/src/collections/chars/sets/ImmutableCharOpenHashSet.java @@ -10,6 +10,7 @@ import speiger.src.collections.chars.collections.CharBidirectionalIterator; import speiger.src.collections.chars.collections.CharCollection; import speiger.src.collections.chars.collections.CharIterator; import speiger.src.collections.chars.functions.CharConsumer; +import speiger.src.collections.chars.functions.OptionalChar; import speiger.src.collections.ints.functions.consumer.IntCharConsumer; import speiger.src.collections.objects.functions.consumer.ObjectCharConsumer; import speiger.src.collections.chars.functions.function.CharPredicate; @@ -236,6 +237,10 @@ public class ImmutableCharOpenHashSet extends AbstractCharSet implements CharOrd @Override public boolean addAll(CharCollection c) { throw new UnsupportedOperationException(); } @Override + public void addFirst(char o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(char o) { throw new UnsupportedOperationException(); } + @Override public boolean addAndMoveToFirst(char o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToLast(char o) { throw new UnsupportedOperationException(); } @@ -371,7 +376,7 @@ public class ImmutableCharOpenHashSet extends AbstractCharSet implements CharOrd } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -384,18 +389,18 @@ public class ImmutableCharOpenHashSet extends AbstractCharSet implements CharOrd else state = operator.applyAsChar(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); int index = firstIndex; while(index != -1) { - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalChar.of(keys[index]); index = (int)links[index]; } - return (char)0; + return OptionalChar.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/chars/utils/CharCollections.java b/src/main/java/speiger/src/collections/chars/utils/CharCollections.java index 8120ff0..4f9158f 100644 --- a/src/main/java/speiger/src/collections/chars/utils/CharCollections.java +++ b/src/main/java/speiger/src/collections/chars/utils/CharCollections.java @@ -15,6 +15,7 @@ import speiger.src.collections.chars.collections.CharCollection; import speiger.src.collections.chars.collections.CharOrderedCollection; import speiger.src.collections.chars.collections.CharIterator; import speiger.src.collections.chars.functions.CharComparator; +import speiger.src.collections.chars.functions.OptionalChar; import speiger.src.collections.objects.utils.ObjectArrays; import speiger.src.collections.chars.functions.CharConsumer; import speiger.src.collections.chars.functions.function.CharPredicate; @@ -758,9 +759,9 @@ public class CharCollections @Override public char reduce(char identity, CharCharUnaryOperator operator) { synchronized(mutex) { return c.reduce(identity, operator); } } @Override - public char reduce(CharCharUnaryOperator operator) { synchronized(mutex) { return c.reduce(operator); } } + public OptionalChar reduce(CharCharUnaryOperator operator) { synchronized(mutex) { return c.reduce(operator); } } @Override - public char findFirst(CharPredicate filter) { synchronized(mutex) { return c.findFirst(filter); } } + public OptionalChar findFirst(CharPredicate filter) { synchronized(mutex) { return c.findFirst(filter); } } @Override public int count(CharPredicate filter) { synchronized(mutex) { return c.count(filter); } } } @@ -888,9 +889,9 @@ public class CharCollections @Override public char reduce(char identity, CharCharUnaryOperator operator) { return c.reduce(identity, operator); } @Override - public char reduce(CharCharUnaryOperator operator) { return c.reduce(operator); } + public OptionalChar reduce(CharCharUnaryOperator operator) { return c.reduce(operator); } @Override - public char findFirst(CharPredicate filter) { return c.findFirst(filter); } + public OptionalChar findFirst(CharPredicate filter) { return c.findFirst(filter); } @Override public int count(CharPredicate filter) { return c.count(filter); } } diff --git a/src/main/java/speiger/src/collections/chars/utils/CharPriorityQueues.java b/src/main/java/speiger/src/collections/chars/utils/CharPriorityQueues.java index f3bfa68..8e21928 100644 --- a/src/main/java/speiger/src/collections/chars/utils/CharPriorityQueues.java +++ b/src/main/java/speiger/src/collections/chars/utils/CharPriorityQueues.java @@ -1,13 +1,14 @@ package speiger.src.collections.chars.utils; - import speiger.src.collections.chars.collections.CharIterator; import speiger.src.collections.chars.collections.CharCollection; import speiger.src.collections.chars.functions.CharComparator; +import speiger.src.collections.chars.functions.OptionalChar; import speiger.src.collections.chars.queues.CharPriorityDequeue; import speiger.src.collections.chars.queues.CharPriorityQueue; import speiger.src.collections.chars.functions.CharConsumer; import speiger.src.collections.chars.functions.function.CharPredicate; +import speiger.src.collections.chars.functions.function.CharCharUnaryOperator; import speiger.src.collections.objects.functions.consumer.ObjectCharConsumer; /** @@ -113,7 +114,11 @@ public class CharPriorityQueues @Override public boolean matchesAll(CharPredicate filter) { synchronized(mutex) { return queue.matchesAll(filter); } } @Override - public char findFirst(CharPredicate filter) { synchronized(mutex) { return queue.findFirst(filter); } } + public OptionalChar findFirst(CharPredicate filter) { synchronized(mutex) { return queue.findFirst(filter); } } + @Override + public char reduce(char identity, CharCharUnaryOperator operator) { synchronized(mutex) { return queue.reduce(identity, operator); } } + @Override + public OptionalChar reduce(CharCharUnaryOperator operator) { synchronized(mutex) { return queue.reduce(operator); } } @Override public int count(CharPredicate filter) { synchronized(mutex) { return queue.count(filter); } } } diff --git a/src/main/java/speiger/src/collections/chars/utils/CharSets.java b/src/main/java/speiger/src/collections/chars/utils/CharSets.java index 051468f..be27951 100644 --- a/src/main/java/speiger/src/collections/chars/utils/CharSets.java +++ b/src/main/java/speiger/src/collections/chars/utils/CharSets.java @@ -301,6 +301,10 @@ public class CharSets s = c; } + @Override + public void addFirst(char o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(char o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(char o) { throw new UnsupportedOperationException(); } @Override @@ -573,7 +577,11 @@ public class CharSets super(c, mutex); s = c; } - + + @Override + public void addFirst(char o) { synchronized(mutex) { s.addFirst(o); } } + @Override + public void addLast(char o) { synchronized(mutex) { s.addLast(o); } } @Override public boolean addAndMoveToFirst(char o) { synchronized(mutex) { return s.addAndMoveToFirst(o); } } @Override diff --git a/src/main/java/speiger/src/collections/chars/utils/maps/Char2BooleanMaps.java b/src/main/java/speiger/src/collections/chars/utils/maps/Char2BooleanMaps.java index 2bcd743..f8bb3f5 100644 --- a/src/main/java/speiger/src/collections/chars/utils/maps/Char2BooleanMaps.java +++ b/src/main/java/speiger/src/collections/chars/utils/maps/Char2BooleanMaps.java @@ -608,7 +608,11 @@ public class Char2BooleanMaps super(c); set = c; } - + + @Override + public void addFirst(Char2BooleanMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Char2BooleanMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Char2BooleanMap.Entry o) { throw new UnsupportedOperationException(); } @Override diff --git a/src/main/java/speiger/src/collections/chars/utils/maps/Char2ByteMaps.java b/src/main/java/speiger/src/collections/chars/utils/maps/Char2ByteMaps.java index 87bbac9..c1dbc9c 100644 --- a/src/main/java/speiger/src/collections/chars/utils/maps/Char2ByteMaps.java +++ b/src/main/java/speiger/src/collections/chars/utils/maps/Char2ByteMaps.java @@ -620,7 +620,11 @@ public class Char2ByteMaps super(c); set = c; } - + + @Override + public void addFirst(Char2ByteMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Char2ByteMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Char2ByteMap.Entry o) { throw new UnsupportedOperationException(); } @Override diff --git a/src/main/java/speiger/src/collections/chars/utils/maps/Char2CharMaps.java b/src/main/java/speiger/src/collections/chars/utils/maps/Char2CharMaps.java index 93171c0..14dd0c0 100644 --- a/src/main/java/speiger/src/collections/chars/utils/maps/Char2CharMaps.java +++ b/src/main/java/speiger/src/collections/chars/utils/maps/Char2CharMaps.java @@ -619,7 +619,11 @@ public class Char2CharMaps super(c); set = c; } - + + @Override + public void addFirst(Char2CharMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Char2CharMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Char2CharMap.Entry o) { throw new UnsupportedOperationException(); } @Override diff --git a/src/main/java/speiger/src/collections/chars/utils/maps/Char2DoubleMaps.java b/src/main/java/speiger/src/collections/chars/utils/maps/Char2DoubleMaps.java index 4d1dc69..45404f0 100644 --- a/src/main/java/speiger/src/collections/chars/utils/maps/Char2DoubleMaps.java +++ b/src/main/java/speiger/src/collections/chars/utils/maps/Char2DoubleMaps.java @@ -620,7 +620,11 @@ public class Char2DoubleMaps super(c); set = c; } - + + @Override + public void addFirst(Char2DoubleMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Char2DoubleMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Char2DoubleMap.Entry o) { throw new UnsupportedOperationException(); } @Override diff --git a/src/main/java/speiger/src/collections/chars/utils/maps/Char2FloatMaps.java b/src/main/java/speiger/src/collections/chars/utils/maps/Char2FloatMaps.java index 51cd505..e12d1fb 100644 --- a/src/main/java/speiger/src/collections/chars/utils/maps/Char2FloatMaps.java +++ b/src/main/java/speiger/src/collections/chars/utils/maps/Char2FloatMaps.java @@ -620,7 +620,11 @@ public class Char2FloatMaps super(c); set = c; } - + + @Override + public void addFirst(Char2FloatMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Char2FloatMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Char2FloatMap.Entry o) { throw new UnsupportedOperationException(); } @Override diff --git a/src/main/java/speiger/src/collections/chars/utils/maps/Char2IntMaps.java b/src/main/java/speiger/src/collections/chars/utils/maps/Char2IntMaps.java index bf8ef5d..7506288 100644 --- a/src/main/java/speiger/src/collections/chars/utils/maps/Char2IntMaps.java +++ b/src/main/java/speiger/src/collections/chars/utils/maps/Char2IntMaps.java @@ -620,7 +620,11 @@ public class Char2IntMaps super(c); set = c; } - + + @Override + public void addFirst(Char2IntMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Char2IntMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Char2IntMap.Entry o) { throw new UnsupportedOperationException(); } @Override diff --git a/src/main/java/speiger/src/collections/chars/utils/maps/Char2LongMaps.java b/src/main/java/speiger/src/collections/chars/utils/maps/Char2LongMaps.java index bbc0665..9f5eabf 100644 --- a/src/main/java/speiger/src/collections/chars/utils/maps/Char2LongMaps.java +++ b/src/main/java/speiger/src/collections/chars/utils/maps/Char2LongMaps.java @@ -620,7 +620,11 @@ public class Char2LongMaps super(c); set = c; } - + + @Override + public void addFirst(Char2LongMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Char2LongMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Char2LongMap.Entry o) { throw new UnsupportedOperationException(); } @Override diff --git a/src/main/java/speiger/src/collections/chars/utils/maps/Char2ObjectMaps.java b/src/main/java/speiger/src/collections/chars/utils/maps/Char2ObjectMaps.java index 0067efc..939f34b 100644 --- a/src/main/java/speiger/src/collections/chars/utils/maps/Char2ObjectMaps.java +++ b/src/main/java/speiger/src/collections/chars/utils/maps/Char2ObjectMaps.java @@ -611,7 +611,11 @@ public class Char2ObjectMaps super(c); set = c; } - + + @Override + public void addFirst(Char2ObjectMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Char2ObjectMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Char2ObjectMap.Entry o) { throw new UnsupportedOperationException(); } @Override diff --git a/src/main/java/speiger/src/collections/chars/utils/maps/Char2ShortMaps.java b/src/main/java/speiger/src/collections/chars/utils/maps/Char2ShortMaps.java index 49569cf..c8a198b 100644 --- a/src/main/java/speiger/src/collections/chars/utils/maps/Char2ShortMaps.java +++ b/src/main/java/speiger/src/collections/chars/utils/maps/Char2ShortMaps.java @@ -620,7 +620,11 @@ public class Char2ShortMaps super(c); set = c; } - + + @Override + public void addFirst(Char2ShortMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Char2ShortMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Char2ShortMap.Entry o) { throw new UnsupportedOperationException(); } @Override diff --git a/src/main/java/speiger/src/collections/doubles/collections/DoubleIterable.java b/src/main/java/speiger/src/collections/doubles/collections/DoubleIterable.java index 5d3b358..8f05e2a 100644 --- a/src/main/java/speiger/src/collections/doubles/collections/DoubleIterable.java +++ b/src/main/java/speiger/src/collections/doubles/collections/DoubleIterable.java @@ -3,6 +3,7 @@ package speiger.src.collections.doubles.collections; import java.util.Objects; import java.util.function.Consumer; import java.util.function.DoublePredicate; +import java.util.OptionalDouble; import speiger.src.collections.doubles.functions.DoubleConsumer; @@ -281,13 +282,13 @@ public interface DoubleIterable extends Iterable * @param filter that should be applied * @return the found value or the null equivalent variant. */ - default double findFirst(DoublePredicate filter) { + default OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); for(DoubleIterator iter = iterator();iter.hasNext();) { double entry = iter.nextDouble(); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return OptionalDouble.of(entry); } - return 0D; + return OptionalDouble.empty(); } /** @@ -312,7 +313,7 @@ public interface DoubleIterable extends Iterable * @param operator the operation that should be applied * @return the reduction result, returns null value if nothing was found */ - default double reduce(DoubleDoubleUnaryOperator operator) { + default OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -324,7 +325,7 @@ public interface DoubleIterable extends Iterable } state = operator.applyAsDouble(state, iter.nextDouble()); } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } /** diff --git a/src/main/java/speiger/src/collections/doubles/lists/CopyOnWriteDoubleArrayList.java b/src/main/java/speiger/src/collections/doubles/lists/CopyOnWriteDoubleArrayList.java index bc02976..d218470 100644 --- a/src/main/java/speiger/src/collections/doubles/lists/CopyOnWriteDoubleArrayList.java +++ b/src/main/java/speiger/src/collections/doubles/lists/CopyOnWriteDoubleArrayList.java @@ -5,6 +5,7 @@ import java.util.Collection; import java.util.Iterator; import java.util.NoSuchElementException; import java.util.Objects; +import java.util.OptionalDouble; import java.util.concurrent.locks.ReentrantLock; import java.util.function.Consumer; import java.util.function.Supplier; @@ -654,13 +655,13 @@ public class CopyOnWriteDoubleArrayList extends AbstractDoubleList implements IT } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); double[] data = this.data; for(int i = 0,m=data.length;i operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Double2BooleanMap.Entry state = null; boolean empty = true; @@ -779,11 +782,11 @@ public class Double2BooleanConcurrentOpenHashMap extends AbstractDouble2BooleanM seg.unlockRead(stamp); } } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Double2BooleanMap.Entry 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); Double2ByteMap.Entry state = null; boolean empty = true; @@ -792,11 +795,11 @@ public class Double2ByteConcurrentOpenHashMap extends AbstractDouble2ByteMap imp seg.unlockRead(stamp); } } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Double2ByteMap.Entry 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); Double2CharMap.Entry state = null; boolean empty = true; @@ -792,11 +795,11 @@ public class Double2CharConcurrentOpenHashMap extends AbstractDouble2CharMap imp seg.unlockRead(stamp); } } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Double2CharMap.Entry 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); Double2DoubleMap.Entry state = null; boolean empty = true; @@ -785,11 +787,11 @@ public class Double2DoubleConcurrentOpenHashMap extends AbstractDouble2DoubleMap seg.unlockRead(stamp); } } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Double2DoubleMap.Entry 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); Double2FloatMap.Entry state = null; boolean empty = true; @@ -792,11 +795,11 @@ public class Double2FloatConcurrentOpenHashMap extends AbstractDouble2FloatMap i seg.unlockRead(stamp); } } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Double2FloatMap.Entry 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); Double2IntMap.Entry state = null; boolean empty = true; @@ -792,11 +795,11 @@ public class Double2IntConcurrentOpenHashMap extends AbstractDouble2IntMap imple seg.unlockRead(stamp); } } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Double2IntMap.Entry 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); Double2LongMap.Entry state = null; boolean empty = true; @@ -792,11 +795,11 @@ public class Double2LongConcurrentOpenHashMap extends AbstractDouble2LongMap imp seg.unlockRead(stamp); } } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Double2LongMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); MapEntry entry = new MapEntry(); for(int i = 0,m=segments.length;i extends AbstractDouble2Object } @Override - public Double2ObjectMap.Entry reduce(ObjectObjectUnaryOperator, Double2ObjectMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Double2ObjectMap.Entry> operator) { Objects.requireNonNull(operator); Double2ObjectMap.Entry state = null; boolean empty = true; @@ -739,11 +741,11 @@ public class Double2ObjectConcurrentOpenHashMap extends AbstractDouble2Object seg.unlockRead(stamp); } } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Double2ObjectMap.Entry findFirst(Predicate> filter) { + public Optional> findFirst(Predicate> filter) { Objects.requireNonNull(filter); MapEntry entry = new MapEntry(); for(int i = 0,m=segments.length;i extends AbstractDouble2Object try { while(index != -1) { entry.set(index, i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); index = (int)seg.links[index]; } } @@ -761,7 +763,7 @@ public class Double2ObjectConcurrentOpenHashMap extends AbstractDouble2Object seg.unlockRead(stamp); } } - return null; + return Optional.empty(); } @Override @@ -1022,7 +1024,7 @@ public class Double2ObjectConcurrentOpenHashMap extends AbstractDouble2Object } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -1046,11 +1048,11 @@ public class Double2ObjectConcurrentOpenHashMap extends AbstractDouble2Object seg.unlockRead(stamp); } } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); for(int i = 0,m=segments.length;i seg = segments[i]; @@ -1058,7 +1060,7 @@ public class Double2ObjectConcurrentOpenHashMap extends AbstractDouble2Object try { int index = seg.firstIndex; while(index != -1){ - if(filter.test(seg.keys[index])) return seg.keys[index]; + if(filter.test(seg.keys[index])) return OptionalDouble.of(seg.keys[index]); index = (int)seg.links[index]; } } @@ -1066,7 +1068,7 @@ public class Double2ObjectConcurrentOpenHashMap extends AbstractDouble2Object seg.unlockRead(stamp); } } - return 0D; + return OptionalDouble.empty(); } @Override @@ -1258,7 +1260,7 @@ public class Double2ObjectConcurrentOpenHashMap extends AbstractDouble2Object } @Override - public V reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); V state = null; boolean empty = true; @@ -1282,20 +1284,20 @@ public class Double2ObjectConcurrentOpenHashMap extends AbstractDouble2Object seg.unlockRead(stamp); } } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public V findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); for(int i = 0,m=segments.length;i seg = segments[i]; long stamp = seg.readLock(); try { int index = seg.firstIndex; while(index != -1){ - if(filter.test(seg.values[index])) return seg.values[index]; + if(filter.test(seg.values[index])) return Optional.ofNullable(seg.values[index]); index = (int)seg.links[index]; } } @@ -1303,7 +1305,7 @@ public class Double2ObjectConcurrentOpenHashMap extends AbstractDouble2Object seg.unlockRead(stamp); } } - return null; + return Optional.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/doubles/maps/impl/concurrent/Double2ShortConcurrentOpenHashMap.java b/src/main/java/speiger/src/collections/doubles/maps/impl/concurrent/Double2ShortConcurrentOpenHashMap.java index 745f24f..5ffb8e0 100644 --- a/src/main/java/speiger/src/collections/doubles/maps/impl/concurrent/Double2ShortConcurrentOpenHashMap.java +++ b/src/main/java/speiger/src/collections/doubles/maps/impl/concurrent/Double2ShortConcurrentOpenHashMap.java @@ -4,11 +4,13 @@ import java.util.Arrays; import java.util.Map; import java.util.NoSuchElementException; import java.util.Objects; +import java.util.Optional; import java.util.concurrent.locks.StampedLock; import java.util.function.Consumer; import java.util.function.Predicate; import java.util.function.BiFunction; import java.util.function.DoublePredicate; +import java.util.OptionalDouble; import speiger.src.collections.doubles.collections.DoubleBidirectionalIterator; import speiger.src.collections.doubles.functions.DoubleConsumer; @@ -37,6 +39,7 @@ import speiger.src.collections.shorts.functions.ShortConsumer; import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectShortConsumer; import speiger.src.collections.shorts.functions.function.ShortPredicate; +import speiger.src.collections.shorts.functions.OptionalShort; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.objects.sets.AbstractObjectSet; import speiger.src.collections.objects.sets.ObjectSet; @@ -768,7 +771,7 @@ public class Double2ShortConcurrentOpenHashMap extends AbstractDouble2ShortMap i } @Override - public Double2ShortMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Double2ShortMap.Entry state = null; boolean empty = true; @@ -792,11 +795,11 @@ public class Double2ShortConcurrentOpenHashMap extends AbstractDouble2ShortMap i seg.unlockRead(stamp); } } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Double2ShortMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); MapEntry entry = new MapEntry(); for(int i = 0,m=segments.length;i implements Double2BooleanOrderedMap.FastOrderedSet { + @Override + public void addFirst(Double2BooleanMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Double2BooleanMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Double2BooleanMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -836,7 +843,7 @@ public class Double2BooleanLinkedOpenCustomHashMap extends Double2BooleanOpenCus } @Override - public Double2BooleanMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Double2BooleanMap.Entry state = null; boolean empty = true; @@ -851,21 +858,21 @@ public class Double2BooleanLinkedOpenCustomHashMap extends Double2BooleanOpenCus state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Double2BooleanMap.Entry 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 @@ -944,10 +951,13 @@ public class Double2BooleanLinkedOpenCustomHashMap extends Double2BooleanOpenCus public boolean add(double o) { throw new UnsupportedOperationException(); } - + + @Override + public void addFirst(double o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(double o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(double o) { throw new UnsupportedOperationException(); } - @Override public boolean addAndMoveToLast(double o) { throw new UnsupportedOperationException(); } @@ -1090,7 +1100,7 @@ public class Double2BooleanLinkedOpenCustomHashMap extends Double2BooleanOpenCus } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -1105,19 +1115,19 @@ public class Double2BooleanLinkedOpenCustomHashMap extends Double2BooleanOpenCus state = operator.applyAsDouble(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0D; + if(size() <= 0) return OptionalDouble.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalDouble.of(keys[index]); index = (int)links[index]; } - return 0D; + return OptionalDouble.empty(); } @Override @@ -1252,7 +1262,7 @@ public class Double2BooleanLinkedOpenCustomHashMap extends Double2BooleanOpenCus } @Override - public boolean reduce(BooleanBooleanUnaryOperator operator) { + public OptionalBoolean reduce(BooleanBooleanUnaryOperator operator) { Objects.requireNonNull(operator); boolean state = false; boolean empty = true; @@ -1267,19 +1277,19 @@ public class Double2BooleanLinkedOpenCustomHashMap extends Double2BooleanOpenCus state = operator.applyAsBoolean(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalBoolean.empty() : OptionalBoolean.of(state); } @Override - public boolean findFirst(BooleanPredicate filter) { + public OptionalBoolean findFirst(BooleanPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return false; + if(size() <= 0) return OptionalBoolean.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalBoolean.of(values[index]); index = (int)links[index]; } - return false; + return OptionalBoolean.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/doubles/maps/impl/customHash/Double2BooleanOpenCustomHashMap.java b/src/main/java/speiger/src/collections/doubles/maps/impl/customHash/Double2BooleanOpenCustomHashMap.java index 07cd964..e7de61e 100644 --- a/src/main/java/speiger/src/collections/doubles/maps/impl/customHash/Double2BooleanOpenCustomHashMap.java +++ b/src/main/java/speiger/src/collections/doubles/maps/impl/customHash/Double2BooleanOpenCustomHashMap.java @@ -5,10 +5,12 @@ 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; import java.util.function.DoublePredicate; +import java.util.OptionalDouble; import speiger.src.collections.doubles.collections.DoubleIterator; import speiger.src.collections.doubles.functions.DoubleConsumer; @@ -36,6 +38,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectBooleanConsumer; import speiger.src.collections.booleans.functions.function.BooleanPredicate; +import speiger.src.collections.booleans.functions.OptionalBoolean; import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.sets.AbstractObjectSet; import speiger.src.collections.objects.sets.ObjectSet; @@ -930,7 +933,7 @@ public class Double2BooleanOpenCustomHashMap extends AbstractDouble2BooleanMap i } @Override - public Double2BooleanMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Double2BooleanMap.Entry state = null; boolean empty = true; @@ -947,25 +950,25 @@ public class Double2BooleanOpenCustomHashMap extends AbstractDouble2BooleanMap i } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Double2BooleanMap.Entry 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], 0D)) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1139,7 +1142,7 @@ public class Double2BooleanOpenCustomHashMap extends AbstractDouble2BooleanMap i } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -1156,18 +1159,18 @@ public class Double2BooleanOpenCustomHashMap extends AbstractDouble2BooleanMap i } state = operator.applyAsDouble(state, keys[i]); } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0D; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OptionalDouble.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OptionalDouble.of(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], 0D) && filter.test(keys[i])) return keys[i]; + if(!strategy.equals(keys[i], 0D) && filter.test(keys[i])) return OptionalDouble.of(keys[i]); } - return 0D; + return OptionalDouble.empty(); } @Override @@ -1282,7 +1285,7 @@ public class Double2BooleanOpenCustomHashMap extends AbstractDouble2BooleanMap i } @Override - public boolean reduce(BooleanBooleanUnaryOperator operator) { + public OptionalBoolean reduce(BooleanBooleanUnaryOperator operator) { Objects.requireNonNull(operator); boolean state = false; boolean empty = true; @@ -1299,18 +1302,18 @@ public class Double2BooleanOpenCustomHashMap extends AbstractDouble2BooleanMap i } state = operator.applyAsBoolean(state, values[i]); } - return state; + return empty ? OptionalBoolean.empty() : OptionalBoolean.of(state); } @Override - public boolean findFirst(BooleanPredicate filter) { + public OptionalBoolean findFirst(BooleanPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return false; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return OptionalBoolean.empty(); + if(containsNull && filter.test(values[nullIndex])) return OptionalBoolean.of(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], 0D) && filter.test(values[i])) return values[i]; + if(!strategy.equals(keys[i], 0D) && filter.test(values[i])) return OptionalBoolean.of(values[i]); } - return false; + return OptionalBoolean.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/doubles/maps/impl/customHash/Double2ByteLinkedOpenCustomHashMap.java b/src/main/java/speiger/src/collections/doubles/maps/impl/customHash/Double2ByteLinkedOpenCustomHashMap.java index 9cc51ee..0665f15 100644 --- a/src/main/java/speiger/src/collections/doubles/maps/impl/customHash/Double2ByteLinkedOpenCustomHashMap.java +++ b/src/main/java/speiger/src/collections/doubles/maps/impl/customHash/Double2ByteLinkedOpenCustomHashMap.java @@ -4,10 +4,12 @@ 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; import java.util.function.DoublePredicate; +import java.util.OptionalDouble; import speiger.src.collections.doubles.collections.DoubleBidirectionalIterator; import speiger.src.collections.doubles.functions.DoubleConsumer; @@ -33,6 +35,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectByteConsumer; import speiger.src.collections.bytes.functions.function.BytePredicate; +import speiger.src.collections.bytes.functions.OptionalByte; import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOperator; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.objects.lists.ObjectListIterator; @@ -671,6 +674,10 @@ public class Double2ByteLinkedOpenCustomHashMap extends Double2ByteOpenCustomHas } private class MapEntrySet extends AbstractObjectSet implements Double2ByteOrderedMap.FastOrderedSet { + @Override + public void addFirst(Double2ByteMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Double2ByteMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Double2ByteMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -836,7 +843,7 @@ public class Double2ByteLinkedOpenCustomHashMap extends Double2ByteOpenCustomHas } @Override - public Double2ByteMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Double2ByteMap.Entry state = null; boolean empty = true; @@ -851,21 +858,21 @@ public class Double2ByteLinkedOpenCustomHashMap extends Double2ByteOpenCustomHas state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Double2ByteMap.Entry 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 @@ -944,10 +951,13 @@ public class Double2ByteLinkedOpenCustomHashMap extends Double2ByteOpenCustomHas public boolean add(double o) { throw new UnsupportedOperationException(); } - + + @Override + public void addFirst(double o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(double o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(double o) { throw new UnsupportedOperationException(); } - @Override public boolean addAndMoveToLast(double o) { throw new UnsupportedOperationException(); } @@ -1090,7 +1100,7 @@ public class Double2ByteLinkedOpenCustomHashMap extends Double2ByteOpenCustomHas } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -1105,19 +1115,19 @@ public class Double2ByteLinkedOpenCustomHashMap extends Double2ByteOpenCustomHas state = operator.applyAsDouble(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0D; + if(size() <= 0) return OptionalDouble.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalDouble.of(keys[index]); index = (int)links[index]; } - return 0D; + return OptionalDouble.empty(); } @Override @@ -1252,7 +1262,7 @@ public class Double2ByteLinkedOpenCustomHashMap extends Double2ByteOpenCustomHas } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -1267,19 +1277,19 @@ public class Double2ByteLinkedOpenCustomHashMap extends Double2ByteOpenCustomHas state = operator.applyAsByte(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (byte)0; + if(size() <= 0) return OptionalByte.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalByte.of(values[index]); index = (int)links[index]; } - return (byte)0; + return OptionalByte.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/doubles/maps/impl/customHash/Double2ByteOpenCustomHashMap.java b/src/main/java/speiger/src/collections/doubles/maps/impl/customHash/Double2ByteOpenCustomHashMap.java index 0d94dd4..435224c 100644 --- a/src/main/java/speiger/src/collections/doubles/maps/impl/customHash/Double2ByteOpenCustomHashMap.java +++ b/src/main/java/speiger/src/collections/doubles/maps/impl/customHash/Double2ByteOpenCustomHashMap.java @@ -5,10 +5,12 @@ 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; import java.util.function.DoublePredicate; +import java.util.OptionalDouble; import speiger.src.collections.doubles.collections.DoubleIterator; import speiger.src.collections.doubles.functions.DoubleConsumer; @@ -37,6 +39,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectByteConsumer; import speiger.src.collections.bytes.functions.function.BytePredicate; +import speiger.src.collections.bytes.functions.OptionalByte; import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.sets.AbstractObjectSet; import speiger.src.collections.objects.sets.ObjectSet; @@ -952,7 +955,7 @@ public class Double2ByteOpenCustomHashMap extends AbstractDouble2ByteMap impleme } @Override - public Double2ByteMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Double2ByteMap.Entry state = null; boolean empty = true; @@ -969,25 +972,25 @@ public class Double2ByteOpenCustomHashMap extends AbstractDouble2ByteMap impleme } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Double2ByteMap.Entry 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], 0D)) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1161,7 +1164,7 @@ public class Double2ByteOpenCustomHashMap extends AbstractDouble2ByteMap impleme } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -1178,18 +1181,18 @@ public class Double2ByteOpenCustomHashMap extends AbstractDouble2ByteMap impleme } state = operator.applyAsDouble(state, keys[i]); } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0D; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OptionalDouble.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OptionalDouble.of(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], 0D) && filter.test(keys[i])) return keys[i]; + if(!strategy.equals(keys[i], 0D) && filter.test(keys[i])) return OptionalDouble.of(keys[i]); } - return 0D; + return OptionalDouble.empty(); } @Override @@ -1304,7 +1307,7 @@ public class Double2ByteOpenCustomHashMap extends AbstractDouble2ByteMap impleme } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -1321,18 +1324,18 @@ public class Double2ByteOpenCustomHashMap extends AbstractDouble2ByteMap impleme } state = operator.applyAsByte(state, values[i]); } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (byte)0; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return OptionalByte.empty(); + if(containsNull && filter.test(values[nullIndex])) return OptionalByte.of(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], 0D) && filter.test(values[i])) return values[i]; + if(!strategy.equals(keys[i], 0D) && filter.test(values[i])) return OptionalByte.of(values[i]); } - return (byte)0; + return OptionalByte.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/doubles/maps/impl/customHash/Double2CharLinkedOpenCustomHashMap.java b/src/main/java/speiger/src/collections/doubles/maps/impl/customHash/Double2CharLinkedOpenCustomHashMap.java index aa8bfd4..5c958da 100644 --- a/src/main/java/speiger/src/collections/doubles/maps/impl/customHash/Double2CharLinkedOpenCustomHashMap.java +++ b/src/main/java/speiger/src/collections/doubles/maps/impl/customHash/Double2CharLinkedOpenCustomHashMap.java @@ -4,10 +4,12 @@ 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; import java.util.function.DoublePredicate; +import java.util.OptionalDouble; import speiger.src.collections.doubles.collections.DoubleBidirectionalIterator; import speiger.src.collections.doubles.functions.DoubleConsumer; @@ -33,6 +35,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectCharConsumer; import speiger.src.collections.chars.functions.function.CharPredicate; +import speiger.src.collections.chars.functions.OptionalChar; import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOperator; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.objects.lists.ObjectListIterator; @@ -671,6 +674,10 @@ public class Double2CharLinkedOpenCustomHashMap extends Double2CharOpenCustomHas } private class MapEntrySet extends AbstractObjectSet implements Double2CharOrderedMap.FastOrderedSet { + @Override + public void addFirst(Double2CharMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Double2CharMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Double2CharMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -836,7 +843,7 @@ public class Double2CharLinkedOpenCustomHashMap extends Double2CharOpenCustomHas } @Override - public Double2CharMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Double2CharMap.Entry state = null; boolean empty = true; @@ -851,21 +858,21 @@ public class Double2CharLinkedOpenCustomHashMap extends Double2CharOpenCustomHas state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Double2CharMap.Entry 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 @@ -944,10 +951,13 @@ public class Double2CharLinkedOpenCustomHashMap extends Double2CharOpenCustomHas public boolean add(double o) { throw new UnsupportedOperationException(); } - + + @Override + public void addFirst(double o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(double o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(double o) { throw new UnsupportedOperationException(); } - @Override public boolean addAndMoveToLast(double o) { throw new UnsupportedOperationException(); } @@ -1090,7 +1100,7 @@ public class Double2CharLinkedOpenCustomHashMap extends Double2CharOpenCustomHas } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -1105,19 +1115,19 @@ public class Double2CharLinkedOpenCustomHashMap extends Double2CharOpenCustomHas state = operator.applyAsDouble(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0D; + if(size() <= 0) return OptionalDouble.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalDouble.of(keys[index]); index = (int)links[index]; } - return 0D; + return OptionalDouble.empty(); } @Override @@ -1252,7 +1262,7 @@ public class Double2CharLinkedOpenCustomHashMap extends Double2CharOpenCustomHas } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -1267,19 +1277,19 @@ public class Double2CharLinkedOpenCustomHashMap extends Double2CharOpenCustomHas state = operator.applyAsChar(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (char)0; + if(size() <= 0) return OptionalChar.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalChar.of(values[index]); index = (int)links[index]; } - return (char)0; + return OptionalChar.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/doubles/maps/impl/customHash/Double2CharOpenCustomHashMap.java b/src/main/java/speiger/src/collections/doubles/maps/impl/customHash/Double2CharOpenCustomHashMap.java index abd4083..7e5ae78 100644 --- a/src/main/java/speiger/src/collections/doubles/maps/impl/customHash/Double2CharOpenCustomHashMap.java +++ b/src/main/java/speiger/src/collections/doubles/maps/impl/customHash/Double2CharOpenCustomHashMap.java @@ -5,10 +5,12 @@ 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; import java.util.function.DoublePredicate; +import java.util.OptionalDouble; import speiger.src.collections.doubles.collections.DoubleIterator; import speiger.src.collections.doubles.functions.DoubleConsumer; @@ -37,6 +39,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectCharConsumer; import speiger.src.collections.chars.functions.function.CharPredicate; +import speiger.src.collections.chars.functions.OptionalChar; import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.sets.AbstractObjectSet; import speiger.src.collections.objects.sets.ObjectSet; @@ -952,7 +955,7 @@ public class Double2CharOpenCustomHashMap extends AbstractDouble2CharMap impleme } @Override - public Double2CharMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Double2CharMap.Entry state = null; boolean empty = true; @@ -969,25 +972,25 @@ public class Double2CharOpenCustomHashMap extends AbstractDouble2CharMap impleme } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Double2CharMap.Entry 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], 0D)) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1161,7 +1164,7 @@ public class Double2CharOpenCustomHashMap extends AbstractDouble2CharMap impleme } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -1178,18 +1181,18 @@ public class Double2CharOpenCustomHashMap extends AbstractDouble2CharMap impleme } state = operator.applyAsDouble(state, keys[i]); } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0D; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OptionalDouble.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OptionalDouble.of(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], 0D) && filter.test(keys[i])) return keys[i]; + if(!strategy.equals(keys[i], 0D) && filter.test(keys[i])) return OptionalDouble.of(keys[i]); } - return 0D; + return OptionalDouble.empty(); } @Override @@ -1304,7 +1307,7 @@ public class Double2CharOpenCustomHashMap extends AbstractDouble2CharMap impleme } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -1321,18 +1324,18 @@ public class Double2CharOpenCustomHashMap extends AbstractDouble2CharMap impleme } state = operator.applyAsChar(state, values[i]); } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (char)0; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return OptionalChar.empty(); + if(containsNull && filter.test(values[nullIndex])) return OptionalChar.of(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], 0D) && filter.test(values[i])) return values[i]; + if(!strategy.equals(keys[i], 0D) && filter.test(values[i])) return OptionalChar.of(values[i]); } - return (char)0; + return OptionalChar.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/doubles/maps/impl/customHash/Double2DoubleLinkedOpenCustomHashMap.java b/src/main/java/speiger/src/collections/doubles/maps/impl/customHash/Double2DoubleLinkedOpenCustomHashMap.java index 64a9e3a..3372a3b 100644 --- a/src/main/java/speiger/src/collections/doubles/maps/impl/customHash/Double2DoubleLinkedOpenCustomHashMap.java +++ b/src/main/java/speiger/src/collections/doubles/maps/impl/customHash/Double2DoubleLinkedOpenCustomHashMap.java @@ -4,10 +4,12 @@ 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; import java.util.function.DoublePredicate; +import java.util.OptionalDouble; import speiger.src.collections.doubles.collections.DoubleBidirectionalIterator; import speiger.src.collections.doubles.functions.DoubleConsumer; @@ -664,6 +666,10 @@ public class Double2DoubleLinkedOpenCustomHashMap extends Double2DoubleOpenCusto } private class MapEntrySet extends AbstractObjectSet implements Double2DoubleOrderedMap.FastOrderedSet { + @Override + public void addFirst(Double2DoubleMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Double2DoubleMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Double2DoubleMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -829,7 +835,7 @@ public class Double2DoubleLinkedOpenCustomHashMap extends Double2DoubleOpenCusto } @Override - public Double2DoubleMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Double2DoubleMap.Entry state = null; boolean empty = true; @@ -844,21 +850,21 @@ public class Double2DoubleLinkedOpenCustomHashMap extends Double2DoubleOpenCusto state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Double2DoubleMap.Entry 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 @@ -937,10 +943,13 @@ public class Double2DoubleLinkedOpenCustomHashMap extends Double2DoubleOpenCusto public boolean add(double o) { throw new UnsupportedOperationException(); } - + + @Override + public void addFirst(double o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(double o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(double o) { throw new UnsupportedOperationException(); } - @Override public boolean addAndMoveToLast(double o) { throw new UnsupportedOperationException(); } @@ -1083,7 +1092,7 @@ public class Double2DoubleLinkedOpenCustomHashMap extends Double2DoubleOpenCusto } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -1098,19 +1107,19 @@ public class Double2DoubleLinkedOpenCustomHashMap extends Double2DoubleOpenCusto state = operator.applyAsDouble(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0D; + if(size() <= 0) return OptionalDouble.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalDouble.of(keys[index]); index = (int)links[index]; } - return 0D; + return OptionalDouble.empty(); } @Override @@ -1245,7 +1254,7 @@ public class Double2DoubleLinkedOpenCustomHashMap extends Double2DoubleOpenCusto } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -1260,19 +1269,19 @@ public class Double2DoubleLinkedOpenCustomHashMap extends Double2DoubleOpenCusto state = operator.applyAsDouble(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0D; + if(size() <= 0) return OptionalDouble.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalDouble.of(values[index]); index = (int)links[index]; } - return 0D; + return OptionalDouble.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/doubles/maps/impl/customHash/Double2DoubleOpenCustomHashMap.java b/src/main/java/speiger/src/collections/doubles/maps/impl/customHash/Double2DoubleOpenCustomHashMap.java index e127a3c..4203724 100644 --- a/src/main/java/speiger/src/collections/doubles/maps/impl/customHash/Double2DoubleOpenCustomHashMap.java +++ b/src/main/java/speiger/src/collections/doubles/maps/impl/customHash/Double2DoubleOpenCustomHashMap.java @@ -5,10 +5,12 @@ 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; import java.util.function.DoublePredicate; +import java.util.OptionalDouble; import speiger.src.collections.doubles.collections.DoubleIterator; import speiger.src.collections.doubles.functions.DoubleConsumer; @@ -944,7 +946,7 @@ public class Double2DoubleOpenCustomHashMap extends AbstractDouble2DoubleMap imp } @Override - public Double2DoubleMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Double2DoubleMap.Entry state = null; boolean empty = true; @@ -961,25 +963,25 @@ public class Double2DoubleOpenCustomHashMap extends AbstractDouble2DoubleMap imp } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Double2DoubleMap.Entry 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], 0D)) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1153,7 +1155,7 @@ public class Double2DoubleOpenCustomHashMap extends AbstractDouble2DoubleMap imp } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -1170,18 +1172,18 @@ public class Double2DoubleOpenCustomHashMap extends AbstractDouble2DoubleMap imp } state = operator.applyAsDouble(state, keys[i]); } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0D; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OptionalDouble.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OptionalDouble.of(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], 0D) && filter.test(keys[i])) return keys[i]; + if(!strategy.equals(keys[i], 0D) && filter.test(keys[i])) return OptionalDouble.of(keys[i]); } - return 0D; + return OptionalDouble.empty(); } @Override @@ -1296,7 +1298,7 @@ public class Double2DoubleOpenCustomHashMap extends AbstractDouble2DoubleMap imp } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -1313,18 +1315,18 @@ public class Double2DoubleOpenCustomHashMap extends AbstractDouble2DoubleMap imp } state = operator.applyAsDouble(state, values[i]); } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0D; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return OptionalDouble.empty(); + if(containsNull && filter.test(values[nullIndex])) return OptionalDouble.of(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], 0D) && filter.test(values[i])) return values[i]; + if(!strategy.equals(keys[i], 0D) && filter.test(values[i])) return OptionalDouble.of(values[i]); } - return 0D; + return OptionalDouble.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/doubles/maps/impl/customHash/Double2FloatLinkedOpenCustomHashMap.java b/src/main/java/speiger/src/collections/doubles/maps/impl/customHash/Double2FloatLinkedOpenCustomHashMap.java index a77d317..403af42 100644 --- a/src/main/java/speiger/src/collections/doubles/maps/impl/customHash/Double2FloatLinkedOpenCustomHashMap.java +++ b/src/main/java/speiger/src/collections/doubles/maps/impl/customHash/Double2FloatLinkedOpenCustomHashMap.java @@ -4,10 +4,12 @@ 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; import java.util.function.DoublePredicate; +import java.util.OptionalDouble; import speiger.src.collections.doubles.collections.DoubleBidirectionalIterator; import speiger.src.collections.doubles.functions.DoubleConsumer; @@ -33,6 +35,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectFloatConsumer; import speiger.src.collections.floats.functions.function.FloatPredicate; +import speiger.src.collections.floats.functions.OptionalFloat; import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOperator; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.objects.lists.ObjectListIterator; @@ -671,6 +674,10 @@ public class Double2FloatLinkedOpenCustomHashMap extends Double2FloatOpenCustomH } private class MapEntrySet extends AbstractObjectSet implements Double2FloatOrderedMap.FastOrderedSet { + @Override + public void addFirst(Double2FloatMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Double2FloatMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Double2FloatMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -836,7 +843,7 @@ public class Double2FloatLinkedOpenCustomHashMap extends Double2FloatOpenCustomH } @Override - public Double2FloatMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Double2FloatMap.Entry state = null; boolean empty = true; @@ -851,21 +858,21 @@ public class Double2FloatLinkedOpenCustomHashMap extends Double2FloatOpenCustomH state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Double2FloatMap.Entry 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 @@ -944,10 +951,13 @@ public class Double2FloatLinkedOpenCustomHashMap extends Double2FloatOpenCustomH public boolean add(double o) { throw new UnsupportedOperationException(); } - + + @Override + public void addFirst(double o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(double o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(double o) { throw new UnsupportedOperationException(); } - @Override public boolean addAndMoveToLast(double o) { throw new UnsupportedOperationException(); } @@ -1090,7 +1100,7 @@ public class Double2FloatLinkedOpenCustomHashMap extends Double2FloatOpenCustomH } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -1105,19 +1115,19 @@ public class Double2FloatLinkedOpenCustomHashMap extends Double2FloatOpenCustomH state = operator.applyAsDouble(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0D; + if(size() <= 0) return OptionalDouble.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalDouble.of(keys[index]); index = (int)links[index]; } - return 0D; + return OptionalDouble.empty(); } @Override @@ -1252,7 +1262,7 @@ public class Double2FloatLinkedOpenCustomHashMap extends Double2FloatOpenCustomH } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -1267,19 +1277,19 @@ public class Double2FloatLinkedOpenCustomHashMap extends Double2FloatOpenCustomH state = operator.applyAsFloat(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0F; + if(size() <= 0) return OptionalFloat.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalFloat.of(values[index]); index = (int)links[index]; } - return 0F; + return OptionalFloat.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/doubles/maps/impl/customHash/Double2FloatOpenCustomHashMap.java b/src/main/java/speiger/src/collections/doubles/maps/impl/customHash/Double2FloatOpenCustomHashMap.java index b1a1f2a..cb689d0 100644 --- a/src/main/java/speiger/src/collections/doubles/maps/impl/customHash/Double2FloatOpenCustomHashMap.java +++ b/src/main/java/speiger/src/collections/doubles/maps/impl/customHash/Double2FloatOpenCustomHashMap.java @@ -5,10 +5,12 @@ 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; import java.util.function.DoublePredicate; +import java.util.OptionalDouble; import speiger.src.collections.doubles.collections.DoubleIterator; import speiger.src.collections.doubles.functions.DoubleConsumer; @@ -37,6 +39,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectFloatConsumer; import speiger.src.collections.floats.functions.function.FloatPredicate; +import speiger.src.collections.floats.functions.OptionalFloat; import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.sets.AbstractObjectSet; import speiger.src.collections.objects.sets.ObjectSet; @@ -952,7 +955,7 @@ public class Double2FloatOpenCustomHashMap extends AbstractDouble2FloatMap imple } @Override - public Double2FloatMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Double2FloatMap.Entry state = null; boolean empty = true; @@ -969,25 +972,25 @@ public class Double2FloatOpenCustomHashMap extends AbstractDouble2FloatMap imple } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Double2FloatMap.Entry 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], 0D)) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1161,7 +1164,7 @@ public class Double2FloatOpenCustomHashMap extends AbstractDouble2FloatMap imple } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -1178,18 +1181,18 @@ public class Double2FloatOpenCustomHashMap extends AbstractDouble2FloatMap imple } state = operator.applyAsDouble(state, keys[i]); } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0D; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OptionalDouble.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OptionalDouble.of(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], 0D) && filter.test(keys[i])) return keys[i]; + if(!strategy.equals(keys[i], 0D) && filter.test(keys[i])) return OptionalDouble.of(keys[i]); } - return 0D; + return OptionalDouble.empty(); } @Override @@ -1304,7 +1307,7 @@ public class Double2FloatOpenCustomHashMap extends AbstractDouble2FloatMap imple } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -1321,18 +1324,18 @@ public class Double2FloatOpenCustomHashMap extends AbstractDouble2FloatMap imple } state = operator.applyAsFloat(state, values[i]); } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0F; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return OptionalFloat.empty(); + if(containsNull && filter.test(values[nullIndex])) return OptionalFloat.of(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], 0D) && filter.test(values[i])) return values[i]; + if(!strategy.equals(keys[i], 0D) && filter.test(values[i])) return OptionalFloat.of(values[i]); } - return 0F; + return OptionalFloat.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/doubles/maps/impl/customHash/Double2IntLinkedOpenCustomHashMap.java b/src/main/java/speiger/src/collections/doubles/maps/impl/customHash/Double2IntLinkedOpenCustomHashMap.java index 2466271..c6505d2 100644 --- a/src/main/java/speiger/src/collections/doubles/maps/impl/customHash/Double2IntLinkedOpenCustomHashMap.java +++ b/src/main/java/speiger/src/collections/doubles/maps/impl/customHash/Double2IntLinkedOpenCustomHashMap.java @@ -4,11 +4,14 @@ 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; import java.util.function.DoublePredicate; +import java.util.OptionalDouble; import java.util.function.IntPredicate; +import java.util.OptionalInt; import speiger.src.collections.doubles.collections.DoubleBidirectionalIterator; import speiger.src.collections.doubles.functions.DoubleConsumer; @@ -671,6 +674,10 @@ public class Double2IntLinkedOpenCustomHashMap extends Double2IntOpenCustomHashM } private class MapEntrySet extends AbstractObjectSet implements Double2IntOrderedMap.FastOrderedSet { + @Override + public void addFirst(Double2IntMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Double2IntMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Double2IntMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -836,7 +843,7 @@ public class Double2IntLinkedOpenCustomHashMap extends Double2IntOpenCustomHashM } @Override - public Double2IntMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Double2IntMap.Entry state = null; boolean empty = true; @@ -851,21 +858,21 @@ public class Double2IntLinkedOpenCustomHashMap extends Double2IntOpenCustomHashM state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Double2IntMap.Entry 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 @@ -944,10 +951,13 @@ public class Double2IntLinkedOpenCustomHashMap extends Double2IntOpenCustomHashM public boolean add(double o) { throw new UnsupportedOperationException(); } - + + @Override + public void addFirst(double o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(double o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(double o) { throw new UnsupportedOperationException(); } - @Override public boolean addAndMoveToLast(double o) { throw new UnsupportedOperationException(); } @@ -1090,7 +1100,7 @@ public class Double2IntLinkedOpenCustomHashMap extends Double2IntOpenCustomHashM } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -1105,19 +1115,19 @@ public class Double2IntLinkedOpenCustomHashMap extends Double2IntOpenCustomHashM state = operator.applyAsDouble(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0D; + if(size() <= 0) return OptionalDouble.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalDouble.of(keys[index]); index = (int)links[index]; } - return 0D; + return OptionalDouble.empty(); } @Override @@ -1252,7 +1262,7 @@ public class Double2IntLinkedOpenCustomHashMap extends Double2IntOpenCustomHashM } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -1267,19 +1277,19 @@ public class Double2IntLinkedOpenCustomHashMap extends Double2IntOpenCustomHashM state = operator.applyAsInt(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0; + if(size() <= 0) return OptionalInt.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalInt.of(values[index]); index = (int)links[index]; } - return 0; + return OptionalInt.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/doubles/maps/impl/customHash/Double2IntOpenCustomHashMap.java b/src/main/java/speiger/src/collections/doubles/maps/impl/customHash/Double2IntOpenCustomHashMap.java index c05505e..49e1b6c 100644 --- a/src/main/java/speiger/src/collections/doubles/maps/impl/customHash/Double2IntOpenCustomHashMap.java +++ b/src/main/java/speiger/src/collections/doubles/maps/impl/customHash/Double2IntOpenCustomHashMap.java @@ -5,11 +5,14 @@ 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; import java.util.function.DoublePredicate; +import java.util.OptionalDouble; import java.util.function.IntPredicate; +import java.util.OptionalInt; import speiger.src.collections.doubles.collections.DoubleIterator; import speiger.src.collections.doubles.functions.DoubleConsumer; @@ -952,7 +955,7 @@ public class Double2IntOpenCustomHashMap extends AbstractDouble2IntMap implement } @Override - public Double2IntMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Double2IntMap.Entry state = null; boolean empty = true; @@ -969,25 +972,25 @@ public class Double2IntOpenCustomHashMap extends AbstractDouble2IntMap implement } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Double2IntMap.Entry 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], 0D)) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1161,7 +1164,7 @@ public class Double2IntOpenCustomHashMap extends AbstractDouble2IntMap implement } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -1178,18 +1181,18 @@ public class Double2IntOpenCustomHashMap extends AbstractDouble2IntMap implement } state = operator.applyAsDouble(state, keys[i]); } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0D; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OptionalDouble.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OptionalDouble.of(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], 0D) && filter.test(keys[i])) return keys[i]; + if(!strategy.equals(keys[i], 0D) && filter.test(keys[i])) return OptionalDouble.of(keys[i]); } - return 0D; + return OptionalDouble.empty(); } @Override @@ -1304,7 +1307,7 @@ public class Double2IntOpenCustomHashMap extends AbstractDouble2IntMap implement } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -1321,18 +1324,18 @@ public class Double2IntOpenCustomHashMap extends AbstractDouble2IntMap implement } state = operator.applyAsInt(state, values[i]); } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return OptionalInt.empty(); + if(containsNull && filter.test(values[nullIndex])) return OptionalInt.of(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], 0D) && filter.test(values[i])) return values[i]; + if(!strategy.equals(keys[i], 0D) && filter.test(values[i])) return OptionalInt.of(values[i]); } - return 0; + return OptionalInt.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/doubles/maps/impl/customHash/Double2LongLinkedOpenCustomHashMap.java b/src/main/java/speiger/src/collections/doubles/maps/impl/customHash/Double2LongLinkedOpenCustomHashMap.java index 49e4054..ee8e523 100644 --- a/src/main/java/speiger/src/collections/doubles/maps/impl/customHash/Double2LongLinkedOpenCustomHashMap.java +++ b/src/main/java/speiger/src/collections/doubles/maps/impl/customHash/Double2LongLinkedOpenCustomHashMap.java @@ -4,11 +4,14 @@ 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; import java.util.function.DoublePredicate; +import java.util.OptionalDouble; import java.util.function.LongPredicate; +import java.util.OptionalLong; import speiger.src.collections.doubles.collections.DoubleBidirectionalIterator; import speiger.src.collections.doubles.functions.DoubleConsumer; @@ -671,6 +674,10 @@ public class Double2LongLinkedOpenCustomHashMap extends Double2LongOpenCustomHas } private class MapEntrySet extends AbstractObjectSet implements Double2LongOrderedMap.FastOrderedSet { + @Override + public void addFirst(Double2LongMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Double2LongMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Double2LongMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -836,7 +843,7 @@ public class Double2LongLinkedOpenCustomHashMap extends Double2LongOpenCustomHas } @Override - public Double2LongMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Double2LongMap.Entry state = null; boolean empty = true; @@ -851,21 +858,21 @@ public class Double2LongLinkedOpenCustomHashMap extends Double2LongOpenCustomHas state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Double2LongMap.Entry 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 @@ -944,10 +951,13 @@ public class Double2LongLinkedOpenCustomHashMap extends Double2LongOpenCustomHas public boolean add(double o) { throw new UnsupportedOperationException(); } - + + @Override + public void addFirst(double o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(double o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(double o) { throw new UnsupportedOperationException(); } - @Override public boolean addAndMoveToLast(double o) { throw new UnsupportedOperationException(); } @@ -1090,7 +1100,7 @@ public class Double2LongLinkedOpenCustomHashMap extends Double2LongOpenCustomHas } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -1105,19 +1115,19 @@ public class Double2LongLinkedOpenCustomHashMap extends Double2LongOpenCustomHas state = operator.applyAsDouble(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0D; + if(size() <= 0) return OptionalDouble.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalDouble.of(keys[index]); index = (int)links[index]; } - return 0D; + return OptionalDouble.empty(); } @Override @@ -1252,7 +1262,7 @@ public class Double2LongLinkedOpenCustomHashMap extends Double2LongOpenCustomHas } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -1267,19 +1277,19 @@ public class Double2LongLinkedOpenCustomHashMap extends Double2LongOpenCustomHas state = operator.applyAsLong(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0L; + if(size() <= 0) return OptionalLong.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalLong.of(values[index]); index = (int)links[index]; } - return 0L; + return OptionalLong.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/doubles/maps/impl/customHash/Double2LongOpenCustomHashMap.java b/src/main/java/speiger/src/collections/doubles/maps/impl/customHash/Double2LongOpenCustomHashMap.java index 993e742..174ba65 100644 --- a/src/main/java/speiger/src/collections/doubles/maps/impl/customHash/Double2LongOpenCustomHashMap.java +++ b/src/main/java/speiger/src/collections/doubles/maps/impl/customHash/Double2LongOpenCustomHashMap.java @@ -5,11 +5,14 @@ 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; import java.util.function.DoublePredicate; +import java.util.OptionalDouble; import java.util.function.LongPredicate; +import java.util.OptionalLong; import speiger.src.collections.doubles.collections.DoubleIterator; import speiger.src.collections.doubles.functions.DoubleConsumer; @@ -952,7 +955,7 @@ public class Double2LongOpenCustomHashMap extends AbstractDouble2LongMap impleme } @Override - public Double2LongMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Double2LongMap.Entry state = null; boolean empty = true; @@ -969,25 +972,25 @@ public class Double2LongOpenCustomHashMap extends AbstractDouble2LongMap impleme } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Double2LongMap.Entry 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], 0D)) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1161,7 +1164,7 @@ public class Double2LongOpenCustomHashMap extends AbstractDouble2LongMap impleme } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -1178,18 +1181,18 @@ public class Double2LongOpenCustomHashMap extends AbstractDouble2LongMap impleme } state = operator.applyAsDouble(state, keys[i]); } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0D; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OptionalDouble.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OptionalDouble.of(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], 0D) && filter.test(keys[i])) return keys[i]; + if(!strategy.equals(keys[i], 0D) && filter.test(keys[i])) return OptionalDouble.of(keys[i]); } - return 0D; + return OptionalDouble.empty(); } @Override @@ -1304,7 +1307,7 @@ public class Double2LongOpenCustomHashMap extends AbstractDouble2LongMap impleme } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -1321,18 +1324,18 @@ public class Double2LongOpenCustomHashMap extends AbstractDouble2LongMap impleme } state = operator.applyAsLong(state, values[i]); } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0L; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return OptionalLong.empty(); + if(containsNull && filter.test(values[nullIndex])) return OptionalLong.of(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], 0D) && filter.test(values[i])) return values[i]; + if(!strategy.equals(keys[i], 0D) && filter.test(values[i])) return OptionalLong.of(values[i]); } - return 0L; + return OptionalLong.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/doubles/maps/impl/customHash/Double2ObjectLinkedOpenCustomHashMap.java b/src/main/java/speiger/src/collections/doubles/maps/impl/customHash/Double2ObjectLinkedOpenCustomHashMap.java index 7d1debb..349de59 100644 --- a/src/main/java/speiger/src/collections/doubles/maps/impl/customHash/Double2ObjectLinkedOpenCustomHashMap.java +++ b/src/main/java/speiger/src/collections/doubles/maps/impl/customHash/Double2ObjectLinkedOpenCustomHashMap.java @@ -4,10 +4,12 @@ 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; import java.util.function.DoublePredicate; +import java.util.OptionalDouble; import speiger.src.collections.doubles.collections.DoubleBidirectionalIterator; import speiger.src.collections.doubles.functions.DoubleConsumer; @@ -666,6 +668,10 @@ public class Double2ObjectLinkedOpenCustomHashMap extends Double2ObjectOpenCu } private class MapEntrySet extends AbstractObjectSet> implements Double2ObjectOrderedMap.FastOrderedSet { + @Override + public void addFirst(Double2ObjectMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Double2ObjectMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Double2ObjectMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -831,7 +837,7 @@ public class Double2ObjectLinkedOpenCustomHashMap extends Double2ObjectOpenCu } @Override - public Double2ObjectMap.Entry reduce(ObjectObjectUnaryOperator, Double2ObjectMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Double2ObjectMap.Entry> operator) { Objects.requireNonNull(operator); Double2ObjectMap.Entry state = null; boolean empty = true; @@ -846,21 +852,21 @@ public class Double2ObjectLinkedOpenCustomHashMap extends Double2ObjectOpenCu state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Double2ObjectMap.Entry 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 @@ -939,10 +945,13 @@ public class Double2ObjectLinkedOpenCustomHashMap extends Double2ObjectOpenCu public boolean add(double o) { throw new UnsupportedOperationException(); } - + + @Override + public void addFirst(double o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(double o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(double o) { throw new UnsupportedOperationException(); } - @Override public boolean addAndMoveToLast(double o) { throw new UnsupportedOperationException(); } @@ -1085,7 +1094,7 @@ public class Double2ObjectLinkedOpenCustomHashMap extends Double2ObjectOpenCu } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -1100,19 +1109,19 @@ public class Double2ObjectLinkedOpenCustomHashMap extends Double2ObjectOpenCu state = operator.applyAsDouble(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0D; + if(size() <= 0) return OptionalDouble.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalDouble.of(keys[index]); index = (int)links[index]; } - return 0D; + return OptionalDouble.empty(); } @Override @@ -1248,7 +1257,7 @@ public class Double2ObjectLinkedOpenCustomHashMap extends Double2ObjectOpenCu } @Override - public V reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); V state = null; boolean empty = true; @@ -1263,19 +1272,19 @@ public class Double2ObjectLinkedOpenCustomHashMap extends Double2ObjectOpenCu state = operator.apply(state, values[index]); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public V findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return Optional.ofNullable(values[index]); index = (int)links[index]; } - return null; + return Optional.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/doubles/maps/impl/customHash/Double2ObjectOpenCustomHashMap.java b/src/main/java/speiger/src/collections/doubles/maps/impl/customHash/Double2ObjectOpenCustomHashMap.java index 0b5d7e7..851cc68 100644 --- a/src/main/java/speiger/src/collections/doubles/maps/impl/customHash/Double2ObjectOpenCustomHashMap.java +++ b/src/main/java/speiger/src/collections/doubles/maps/impl/customHash/Double2ObjectOpenCustomHashMap.java @@ -5,10 +5,12 @@ 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; import java.util.function.DoublePredicate; +import java.util.OptionalDouble; import speiger.src.collections.doubles.collections.DoubleIterator; import speiger.src.collections.doubles.functions.DoubleConsumer; @@ -868,7 +870,7 @@ public class Double2ObjectOpenCustomHashMap extends AbstractDouble2ObjectMap< } @Override - public Double2ObjectMap.Entry reduce(ObjectObjectUnaryOperator, Double2ObjectMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Double2ObjectMap.Entry> operator) { Objects.requireNonNull(operator); Double2ObjectMap.Entry state = null; boolean empty = true; @@ -885,25 +887,25 @@ public class Double2ObjectOpenCustomHashMap extends AbstractDouble2ObjectMap< } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Double2ObjectMap.Entry 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], 0D)) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1077,7 +1079,7 @@ public class Double2ObjectOpenCustomHashMap extends AbstractDouble2ObjectMap< } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -1094,18 +1096,18 @@ public class Double2ObjectOpenCustomHashMap extends AbstractDouble2ObjectMap< } state = operator.applyAsDouble(state, keys[i]); } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0D; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OptionalDouble.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OptionalDouble.of(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], 0D) && filter.test(keys[i])) return keys[i]; + if(!strategy.equals(keys[i], 0D) && filter.test(keys[i])) return OptionalDouble.of(keys[i]); } - return 0D; + return OptionalDouble.empty(); } @Override @@ -1220,7 +1222,7 @@ public class Double2ObjectOpenCustomHashMap extends AbstractDouble2ObjectMap< } @Override - public V reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); V state = null; boolean empty = true; @@ -1237,18 +1239,18 @@ public class Double2ObjectOpenCustomHashMap extends AbstractDouble2ObjectMap< } state = operator.apply(state, values[i]); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public V findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return Optional.empty(); + if(containsNull && filter.test(values[nullIndex])) return Optional.ofNullable(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], 0D) && filter.test(values[i])) return values[i]; + if(!strategy.equals(keys[i], 0D) && filter.test(values[i])) return Optional.ofNullable(values[i]); } - return null; + return Optional.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/doubles/maps/impl/customHash/Double2ShortLinkedOpenCustomHashMap.java b/src/main/java/speiger/src/collections/doubles/maps/impl/customHash/Double2ShortLinkedOpenCustomHashMap.java index 26bdfab..9c7f1aa 100644 --- a/src/main/java/speiger/src/collections/doubles/maps/impl/customHash/Double2ShortLinkedOpenCustomHashMap.java +++ b/src/main/java/speiger/src/collections/doubles/maps/impl/customHash/Double2ShortLinkedOpenCustomHashMap.java @@ -4,10 +4,12 @@ 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; import java.util.function.DoublePredicate; +import java.util.OptionalDouble; import speiger.src.collections.doubles.collections.DoubleBidirectionalIterator; import speiger.src.collections.doubles.functions.DoubleConsumer; @@ -33,6 +35,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectShortConsumer; import speiger.src.collections.shorts.functions.function.ShortPredicate; +import speiger.src.collections.shorts.functions.OptionalShort; import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOperator; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.objects.lists.ObjectListIterator; @@ -671,6 +674,10 @@ public class Double2ShortLinkedOpenCustomHashMap extends Double2ShortOpenCustomH } private class MapEntrySet extends AbstractObjectSet implements Double2ShortOrderedMap.FastOrderedSet { + @Override + public void addFirst(Double2ShortMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Double2ShortMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Double2ShortMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -836,7 +843,7 @@ public class Double2ShortLinkedOpenCustomHashMap extends Double2ShortOpenCustomH } @Override - public Double2ShortMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Double2ShortMap.Entry state = null; boolean empty = true; @@ -851,21 +858,21 @@ public class Double2ShortLinkedOpenCustomHashMap extends Double2ShortOpenCustomH state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Double2ShortMap.Entry 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 @@ -944,10 +951,13 @@ public class Double2ShortLinkedOpenCustomHashMap extends Double2ShortOpenCustomH public boolean add(double o) { throw new UnsupportedOperationException(); } - + + @Override + public void addFirst(double o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(double o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(double o) { throw new UnsupportedOperationException(); } - @Override public boolean addAndMoveToLast(double o) { throw new UnsupportedOperationException(); } @@ -1090,7 +1100,7 @@ public class Double2ShortLinkedOpenCustomHashMap extends Double2ShortOpenCustomH } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -1105,19 +1115,19 @@ public class Double2ShortLinkedOpenCustomHashMap extends Double2ShortOpenCustomH state = operator.applyAsDouble(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0D; + if(size() <= 0) return OptionalDouble.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalDouble.of(keys[index]); index = (int)links[index]; } - return 0D; + return OptionalDouble.empty(); } @Override @@ -1252,7 +1262,7 @@ public class Double2ShortLinkedOpenCustomHashMap extends Double2ShortOpenCustomH } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -1267,19 +1277,19 @@ public class Double2ShortLinkedOpenCustomHashMap extends Double2ShortOpenCustomH state = operator.applyAsShort(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (short)0; + if(size() <= 0) return OptionalShort.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalShort.of(values[index]); index = (int)links[index]; } - return (short)0; + return OptionalShort.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/doubles/maps/impl/customHash/Double2ShortOpenCustomHashMap.java b/src/main/java/speiger/src/collections/doubles/maps/impl/customHash/Double2ShortOpenCustomHashMap.java index de38908..cc2b174 100644 --- a/src/main/java/speiger/src/collections/doubles/maps/impl/customHash/Double2ShortOpenCustomHashMap.java +++ b/src/main/java/speiger/src/collections/doubles/maps/impl/customHash/Double2ShortOpenCustomHashMap.java @@ -5,10 +5,12 @@ 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; import java.util.function.DoublePredicate; +import java.util.OptionalDouble; import speiger.src.collections.doubles.collections.DoubleIterator; import speiger.src.collections.doubles.functions.DoubleConsumer; @@ -37,6 +39,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectShortConsumer; import speiger.src.collections.shorts.functions.function.ShortPredicate; +import speiger.src.collections.shorts.functions.OptionalShort; import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.sets.AbstractObjectSet; import speiger.src.collections.objects.sets.ObjectSet; @@ -952,7 +955,7 @@ public class Double2ShortOpenCustomHashMap extends AbstractDouble2ShortMap imple } @Override - public Double2ShortMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Double2ShortMap.Entry state = null; boolean empty = true; @@ -969,25 +972,25 @@ public class Double2ShortOpenCustomHashMap extends AbstractDouble2ShortMap imple } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Double2ShortMap.Entry 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], 0D)) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1161,7 +1164,7 @@ public class Double2ShortOpenCustomHashMap extends AbstractDouble2ShortMap imple } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -1178,18 +1181,18 @@ public class Double2ShortOpenCustomHashMap extends AbstractDouble2ShortMap imple } state = operator.applyAsDouble(state, keys[i]); } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0D; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OptionalDouble.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OptionalDouble.of(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], 0D) && filter.test(keys[i])) return keys[i]; + if(!strategy.equals(keys[i], 0D) && filter.test(keys[i])) return OptionalDouble.of(keys[i]); } - return 0D; + return OptionalDouble.empty(); } @Override @@ -1304,7 +1307,7 @@ public class Double2ShortOpenCustomHashMap extends AbstractDouble2ShortMap imple } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -1321,18 +1324,18 @@ public class Double2ShortOpenCustomHashMap extends AbstractDouble2ShortMap imple } state = operator.applyAsShort(state, values[i]); } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (short)0; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return OptionalShort.empty(); + if(containsNull && filter.test(values[nullIndex])) return OptionalShort.of(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], 0D) && filter.test(values[i])) return values[i]; + if(!strategy.equals(keys[i], 0D) && filter.test(values[i])) return OptionalShort.of(values[i]); } - return (short)0; + return OptionalShort.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/doubles/maps/impl/hash/Double2BooleanLinkedOpenHashMap.java b/src/main/java/speiger/src/collections/doubles/maps/impl/hash/Double2BooleanLinkedOpenHashMap.java index e9c5649..51a98c7 100644 --- a/src/main/java/speiger/src/collections/doubles/maps/impl/hash/Double2BooleanLinkedOpenHashMap.java +++ b/src/main/java/speiger/src/collections/doubles/maps/impl/hash/Double2BooleanLinkedOpenHashMap.java @@ -4,10 +4,12 @@ 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; import java.util.function.DoublePredicate; +import java.util.OptionalDouble; import speiger.src.collections.doubles.collections.DoubleBidirectionalIterator; import speiger.src.collections.doubles.functions.DoubleConsumer; @@ -32,6 +34,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectBooleanConsumer; import speiger.src.collections.booleans.functions.function.BooleanPredicate; +import speiger.src.collections.booleans.functions.OptionalBoolean; import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOperator; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.objects.lists.ObjectListIterator; @@ -669,6 +672,10 @@ public class Double2BooleanLinkedOpenHashMap extends Double2BooleanOpenHashMap i } private class MapEntrySet extends AbstractObjectSet implements Double2BooleanOrderedMap.FastOrderedSet { + @Override + public void addFirst(Double2BooleanMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Double2BooleanMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Double2BooleanMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -834,7 +841,7 @@ public class Double2BooleanLinkedOpenHashMap extends Double2BooleanOpenHashMap i } @Override - public Double2BooleanMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Double2BooleanMap.Entry state = null; boolean empty = true; @@ -849,21 +856,21 @@ public class Double2BooleanLinkedOpenHashMap extends Double2BooleanOpenHashMap i state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Double2BooleanMap.Entry 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 @@ -939,6 +946,12 @@ public class Double2BooleanLinkedOpenHashMap extends Double2BooleanOpenHashMap i @Override public boolean add(double o) { throw new UnsupportedOperationException(); } + + @Override + public void addFirst(double o) { throw new UnsupportedOperationException(); } + + @Override + public void addLast(double o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(double o) { throw new UnsupportedOperationException(); } @@ -1085,7 +1098,7 @@ public class Double2BooleanLinkedOpenHashMap extends Double2BooleanOpenHashMap i } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -1100,19 +1113,19 @@ public class Double2BooleanLinkedOpenHashMap extends Double2BooleanOpenHashMap i state = operator.applyAsDouble(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0D; + if(size() <= 0) return OptionalDouble.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalDouble.of(keys[index]); index = (int)links[index]; } - return 0D; + return OptionalDouble.empty(); } @Override @@ -1248,7 +1261,7 @@ public class Double2BooleanLinkedOpenHashMap extends Double2BooleanOpenHashMap i } @Override - public boolean reduce(BooleanBooleanUnaryOperator operator) { + public OptionalBoolean reduce(BooleanBooleanUnaryOperator operator) { Objects.requireNonNull(operator); boolean state = false; boolean empty = true; @@ -1263,19 +1276,19 @@ public class Double2BooleanLinkedOpenHashMap extends Double2BooleanOpenHashMap i state = operator.applyAsBoolean(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalBoolean.empty() : OptionalBoolean.of(state); } @Override - public boolean findFirst(BooleanPredicate filter) { + public OptionalBoolean findFirst(BooleanPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return false; + if(size() <= 0) return OptionalBoolean.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalBoolean.of(values[index]); index = (int)links[index]; } - return false; + return OptionalBoolean.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/doubles/maps/impl/hash/Double2BooleanOpenHashMap.java b/src/main/java/speiger/src/collections/doubles/maps/impl/hash/Double2BooleanOpenHashMap.java index 9bb6588..cea4cb2 100644 --- a/src/main/java/speiger/src/collections/doubles/maps/impl/hash/Double2BooleanOpenHashMap.java +++ b/src/main/java/speiger/src/collections/doubles/maps/impl/hash/Double2BooleanOpenHashMap.java @@ -5,10 +5,12 @@ 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; import java.util.function.DoublePredicate; +import java.util.OptionalDouble; import speiger.src.collections.doubles.collections.DoubleIterator; import speiger.src.collections.doubles.functions.DoubleConsumer; @@ -33,6 +35,7 @@ import speiger.src.collections.booleans.functions.BooleanConsumer; import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectBooleanConsumer; import speiger.src.collections.booleans.functions.function.BooleanPredicate; +import speiger.src.collections.booleans.functions.OptionalBoolean; import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.sets.AbstractObjectSet; import speiger.src.collections.objects.sets.ObjectSet; @@ -898,7 +901,7 @@ public class Double2BooleanOpenHashMap extends AbstractDouble2BooleanMap impleme } @Override - public Double2BooleanMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Double2BooleanMap.Entry state = null; boolean empty = true; @@ -915,25 +918,25 @@ public class Double2BooleanOpenHashMap extends AbstractDouble2BooleanMap impleme } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Double2BooleanMap.Entry 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(Double.doubleToLongBits(keys[i]) != 0) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1105,7 +1108,7 @@ public class Double2BooleanOpenHashMap extends AbstractDouble2BooleanMap impleme } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -1122,18 +1125,18 @@ public class Double2BooleanOpenHashMap extends AbstractDouble2BooleanMap impleme } state = operator.applyAsDouble(state, keys[i]); } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0D; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OptionalDouble.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OptionalDouble.of(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(Double.doubleToLongBits(keys[i]) != 0 && filter.test(keys[i])) return keys[i]; + if(Double.doubleToLongBits(keys[i]) != 0 && filter.test(keys[i])) return OptionalDouble.of(keys[i]); } - return 0D; + return OptionalDouble.empty(); } @Override @@ -1248,7 +1251,7 @@ public class Double2BooleanOpenHashMap extends AbstractDouble2BooleanMap impleme } @Override - public boolean reduce(BooleanBooleanUnaryOperator operator) { + public OptionalBoolean reduce(BooleanBooleanUnaryOperator operator) { Objects.requireNonNull(operator); boolean state = false; boolean empty = true; @@ -1265,18 +1268,18 @@ public class Double2BooleanOpenHashMap extends AbstractDouble2BooleanMap impleme } state = operator.applyAsBoolean(state, values[i]); } - return state; + return empty ? OptionalBoolean.empty() : OptionalBoolean.of(state); } @Override - public boolean findFirst(BooleanPredicate filter) { + public OptionalBoolean findFirst(BooleanPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return false; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return OptionalBoolean.empty(); + if(containsNull && filter.test(values[nullIndex])) return OptionalBoolean.of(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(Double.doubleToLongBits(keys[i]) != 0 && filter.test(values[i])) return values[i]; + if(Double.doubleToLongBits(keys[i]) != 0 && filter.test(values[i])) return OptionalBoolean.of(values[i]); } - return false; + return OptionalBoolean.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/doubles/maps/impl/hash/Double2ByteLinkedOpenHashMap.java b/src/main/java/speiger/src/collections/doubles/maps/impl/hash/Double2ByteLinkedOpenHashMap.java index b855e56..4fe85b3 100644 --- a/src/main/java/speiger/src/collections/doubles/maps/impl/hash/Double2ByteLinkedOpenHashMap.java +++ b/src/main/java/speiger/src/collections/doubles/maps/impl/hash/Double2ByteLinkedOpenHashMap.java @@ -4,10 +4,12 @@ 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; import java.util.function.DoublePredicate; +import java.util.OptionalDouble; import speiger.src.collections.doubles.collections.DoubleBidirectionalIterator; import speiger.src.collections.doubles.functions.DoubleConsumer; @@ -32,6 +34,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectByteConsumer; import speiger.src.collections.bytes.functions.function.BytePredicate; +import speiger.src.collections.bytes.functions.OptionalByte; import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOperator; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.objects.lists.ObjectListIterator; @@ -669,6 +672,10 @@ public class Double2ByteLinkedOpenHashMap extends Double2ByteOpenHashMap impleme } private class MapEntrySet extends AbstractObjectSet implements Double2ByteOrderedMap.FastOrderedSet { + @Override + public void addFirst(Double2ByteMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Double2ByteMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Double2ByteMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -834,7 +841,7 @@ public class Double2ByteLinkedOpenHashMap extends Double2ByteOpenHashMap impleme } @Override - public Double2ByteMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Double2ByteMap.Entry state = null; boolean empty = true; @@ -849,21 +856,21 @@ public class Double2ByteLinkedOpenHashMap extends Double2ByteOpenHashMap impleme state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Double2ByteMap.Entry 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 @@ -939,6 +946,12 @@ public class Double2ByteLinkedOpenHashMap extends Double2ByteOpenHashMap impleme @Override public boolean add(double o) { throw new UnsupportedOperationException(); } + + @Override + public void addFirst(double o) { throw new UnsupportedOperationException(); } + + @Override + public void addLast(double o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(double o) { throw new UnsupportedOperationException(); } @@ -1085,7 +1098,7 @@ public class Double2ByteLinkedOpenHashMap extends Double2ByteOpenHashMap impleme } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -1100,19 +1113,19 @@ public class Double2ByteLinkedOpenHashMap extends Double2ByteOpenHashMap impleme state = operator.applyAsDouble(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0D; + if(size() <= 0) return OptionalDouble.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalDouble.of(keys[index]); index = (int)links[index]; } - return 0D; + return OptionalDouble.empty(); } @Override @@ -1248,7 +1261,7 @@ public class Double2ByteLinkedOpenHashMap extends Double2ByteOpenHashMap impleme } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -1263,19 +1276,19 @@ public class Double2ByteLinkedOpenHashMap extends Double2ByteOpenHashMap impleme state = operator.applyAsByte(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (byte)0; + if(size() <= 0) return OptionalByte.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalByte.of(values[index]); index = (int)links[index]; } - return (byte)0; + return OptionalByte.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/doubles/maps/impl/hash/Double2ByteOpenHashMap.java b/src/main/java/speiger/src/collections/doubles/maps/impl/hash/Double2ByteOpenHashMap.java index 2d8c579..7e08c5d 100644 --- a/src/main/java/speiger/src/collections/doubles/maps/impl/hash/Double2ByteOpenHashMap.java +++ b/src/main/java/speiger/src/collections/doubles/maps/impl/hash/Double2ByteOpenHashMap.java @@ -5,10 +5,12 @@ 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; import java.util.function.DoublePredicate; +import java.util.OptionalDouble; import speiger.src.collections.doubles.collections.DoubleIterator; import speiger.src.collections.doubles.functions.DoubleConsumer; @@ -34,6 +36,7 @@ import speiger.src.collections.bytes.functions.ByteConsumer; import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectByteConsumer; import speiger.src.collections.bytes.functions.function.BytePredicate; +import speiger.src.collections.bytes.functions.OptionalByte; import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.sets.AbstractObjectSet; import speiger.src.collections.objects.sets.ObjectSet; @@ -920,7 +923,7 @@ public class Double2ByteOpenHashMap extends AbstractDouble2ByteMap implements IT } @Override - public Double2ByteMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Double2ByteMap.Entry state = null; boolean empty = true; @@ -937,25 +940,25 @@ public class Double2ByteOpenHashMap extends AbstractDouble2ByteMap implements IT } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Double2ByteMap.Entry 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(Double.doubleToLongBits(keys[i]) != 0) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1127,7 +1130,7 @@ public class Double2ByteOpenHashMap extends AbstractDouble2ByteMap implements IT } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -1144,18 +1147,18 @@ public class Double2ByteOpenHashMap extends AbstractDouble2ByteMap implements IT } state = operator.applyAsDouble(state, keys[i]); } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0D; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OptionalDouble.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OptionalDouble.of(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(Double.doubleToLongBits(keys[i]) != 0 && filter.test(keys[i])) return keys[i]; + if(Double.doubleToLongBits(keys[i]) != 0 && filter.test(keys[i])) return OptionalDouble.of(keys[i]); } - return 0D; + return OptionalDouble.empty(); } @Override @@ -1270,7 +1273,7 @@ public class Double2ByteOpenHashMap extends AbstractDouble2ByteMap implements IT } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -1287,18 +1290,18 @@ public class Double2ByteOpenHashMap extends AbstractDouble2ByteMap implements IT } state = operator.applyAsByte(state, values[i]); } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (byte)0; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return OptionalByte.empty(); + if(containsNull && filter.test(values[nullIndex])) return OptionalByte.of(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(Double.doubleToLongBits(keys[i]) != 0 && filter.test(values[i])) return values[i]; + if(Double.doubleToLongBits(keys[i]) != 0 && filter.test(values[i])) return OptionalByte.of(values[i]); } - return (byte)0; + return OptionalByte.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/doubles/maps/impl/hash/Double2CharLinkedOpenHashMap.java b/src/main/java/speiger/src/collections/doubles/maps/impl/hash/Double2CharLinkedOpenHashMap.java index 0595e93..808b786 100644 --- a/src/main/java/speiger/src/collections/doubles/maps/impl/hash/Double2CharLinkedOpenHashMap.java +++ b/src/main/java/speiger/src/collections/doubles/maps/impl/hash/Double2CharLinkedOpenHashMap.java @@ -4,10 +4,12 @@ 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; import java.util.function.DoublePredicate; +import java.util.OptionalDouble; import speiger.src.collections.doubles.collections.DoubleBidirectionalIterator; import speiger.src.collections.doubles.functions.DoubleConsumer; @@ -32,6 +34,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectCharConsumer; import speiger.src.collections.chars.functions.function.CharPredicate; +import speiger.src.collections.chars.functions.OptionalChar; import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOperator; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.objects.lists.ObjectListIterator; @@ -669,6 +672,10 @@ public class Double2CharLinkedOpenHashMap extends Double2CharOpenHashMap impleme } private class MapEntrySet extends AbstractObjectSet implements Double2CharOrderedMap.FastOrderedSet { + @Override + public void addFirst(Double2CharMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Double2CharMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Double2CharMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -834,7 +841,7 @@ public class Double2CharLinkedOpenHashMap extends Double2CharOpenHashMap impleme } @Override - public Double2CharMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Double2CharMap.Entry state = null; boolean empty = true; @@ -849,21 +856,21 @@ public class Double2CharLinkedOpenHashMap extends Double2CharOpenHashMap impleme state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Double2CharMap.Entry 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 @@ -939,6 +946,12 @@ public class Double2CharLinkedOpenHashMap extends Double2CharOpenHashMap impleme @Override public boolean add(double o) { throw new UnsupportedOperationException(); } + + @Override + public void addFirst(double o) { throw new UnsupportedOperationException(); } + + @Override + public void addLast(double o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(double o) { throw new UnsupportedOperationException(); } @@ -1085,7 +1098,7 @@ public class Double2CharLinkedOpenHashMap extends Double2CharOpenHashMap impleme } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -1100,19 +1113,19 @@ public class Double2CharLinkedOpenHashMap extends Double2CharOpenHashMap impleme state = operator.applyAsDouble(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0D; + if(size() <= 0) return OptionalDouble.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalDouble.of(keys[index]); index = (int)links[index]; } - return 0D; + return OptionalDouble.empty(); } @Override @@ -1248,7 +1261,7 @@ public class Double2CharLinkedOpenHashMap extends Double2CharOpenHashMap impleme } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -1263,19 +1276,19 @@ public class Double2CharLinkedOpenHashMap extends Double2CharOpenHashMap impleme state = operator.applyAsChar(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (char)0; + if(size() <= 0) return OptionalChar.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalChar.of(values[index]); index = (int)links[index]; } - return (char)0; + return OptionalChar.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/doubles/maps/impl/hash/Double2CharOpenHashMap.java b/src/main/java/speiger/src/collections/doubles/maps/impl/hash/Double2CharOpenHashMap.java index fb8d46b..ee4aae2 100644 --- a/src/main/java/speiger/src/collections/doubles/maps/impl/hash/Double2CharOpenHashMap.java +++ b/src/main/java/speiger/src/collections/doubles/maps/impl/hash/Double2CharOpenHashMap.java @@ -5,10 +5,12 @@ 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; import java.util.function.DoublePredicate; +import java.util.OptionalDouble; import speiger.src.collections.doubles.collections.DoubleIterator; import speiger.src.collections.doubles.functions.DoubleConsumer; @@ -34,6 +36,7 @@ import speiger.src.collections.chars.functions.CharConsumer; import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectCharConsumer; import speiger.src.collections.chars.functions.function.CharPredicate; +import speiger.src.collections.chars.functions.OptionalChar; import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.sets.AbstractObjectSet; import speiger.src.collections.objects.sets.ObjectSet; @@ -920,7 +923,7 @@ public class Double2CharOpenHashMap extends AbstractDouble2CharMap implements IT } @Override - public Double2CharMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Double2CharMap.Entry state = null; boolean empty = true; @@ -937,25 +940,25 @@ public class Double2CharOpenHashMap extends AbstractDouble2CharMap implements IT } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Double2CharMap.Entry 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(Double.doubleToLongBits(keys[i]) != 0) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1127,7 +1130,7 @@ public class Double2CharOpenHashMap extends AbstractDouble2CharMap implements IT } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -1144,18 +1147,18 @@ public class Double2CharOpenHashMap extends AbstractDouble2CharMap implements IT } state = operator.applyAsDouble(state, keys[i]); } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0D; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OptionalDouble.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OptionalDouble.of(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(Double.doubleToLongBits(keys[i]) != 0 && filter.test(keys[i])) return keys[i]; + if(Double.doubleToLongBits(keys[i]) != 0 && filter.test(keys[i])) return OptionalDouble.of(keys[i]); } - return 0D; + return OptionalDouble.empty(); } @Override @@ -1270,7 +1273,7 @@ public class Double2CharOpenHashMap extends AbstractDouble2CharMap implements IT } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -1287,18 +1290,18 @@ public class Double2CharOpenHashMap extends AbstractDouble2CharMap implements IT } state = operator.applyAsChar(state, values[i]); } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (char)0; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return OptionalChar.empty(); + if(containsNull && filter.test(values[nullIndex])) return OptionalChar.of(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(Double.doubleToLongBits(keys[i]) != 0 && filter.test(values[i])) return values[i]; + if(Double.doubleToLongBits(keys[i]) != 0 && filter.test(values[i])) return OptionalChar.of(values[i]); } - return (char)0; + return OptionalChar.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/doubles/maps/impl/hash/Double2DoubleLinkedOpenHashMap.java b/src/main/java/speiger/src/collections/doubles/maps/impl/hash/Double2DoubleLinkedOpenHashMap.java index 0a31e86..e7b4de4 100644 --- a/src/main/java/speiger/src/collections/doubles/maps/impl/hash/Double2DoubleLinkedOpenHashMap.java +++ b/src/main/java/speiger/src/collections/doubles/maps/impl/hash/Double2DoubleLinkedOpenHashMap.java @@ -4,10 +4,12 @@ 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; import java.util.function.DoublePredicate; +import java.util.OptionalDouble; import speiger.src.collections.doubles.collections.DoubleBidirectionalIterator; import speiger.src.collections.doubles.functions.DoubleConsumer; @@ -662,6 +664,10 @@ public class Double2DoubleLinkedOpenHashMap extends Double2DoubleOpenHashMap imp } private class MapEntrySet extends AbstractObjectSet implements Double2DoubleOrderedMap.FastOrderedSet { + @Override + public void addFirst(Double2DoubleMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Double2DoubleMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Double2DoubleMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -827,7 +833,7 @@ public class Double2DoubleLinkedOpenHashMap extends Double2DoubleOpenHashMap imp } @Override - public Double2DoubleMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Double2DoubleMap.Entry state = null; boolean empty = true; @@ -842,21 +848,21 @@ public class Double2DoubleLinkedOpenHashMap extends Double2DoubleOpenHashMap imp state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Double2DoubleMap.Entry 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 @@ -932,6 +938,12 @@ public class Double2DoubleLinkedOpenHashMap extends Double2DoubleOpenHashMap imp @Override public boolean add(double o) { throw new UnsupportedOperationException(); } + + @Override + public void addFirst(double o) { throw new UnsupportedOperationException(); } + + @Override + public void addLast(double o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(double o) { throw new UnsupportedOperationException(); } @@ -1078,7 +1090,7 @@ public class Double2DoubleLinkedOpenHashMap extends Double2DoubleOpenHashMap imp } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -1093,19 +1105,19 @@ public class Double2DoubleLinkedOpenHashMap extends Double2DoubleOpenHashMap imp state = operator.applyAsDouble(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0D; + if(size() <= 0) return OptionalDouble.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalDouble.of(keys[index]); index = (int)links[index]; } - return 0D; + return OptionalDouble.empty(); } @Override @@ -1241,7 +1253,7 @@ public class Double2DoubleLinkedOpenHashMap extends Double2DoubleOpenHashMap imp } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -1256,19 +1268,19 @@ public class Double2DoubleLinkedOpenHashMap extends Double2DoubleOpenHashMap imp state = operator.applyAsDouble(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0D; + if(size() <= 0) return OptionalDouble.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalDouble.of(values[index]); index = (int)links[index]; } - return 0D; + return OptionalDouble.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/doubles/maps/impl/hash/Double2DoubleOpenHashMap.java b/src/main/java/speiger/src/collections/doubles/maps/impl/hash/Double2DoubleOpenHashMap.java index d84466d..0a07b77 100644 --- a/src/main/java/speiger/src/collections/doubles/maps/impl/hash/Double2DoubleOpenHashMap.java +++ b/src/main/java/speiger/src/collections/doubles/maps/impl/hash/Double2DoubleOpenHashMap.java @@ -5,10 +5,12 @@ 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; import java.util.function.DoublePredicate; +import java.util.OptionalDouble; import speiger.src.collections.doubles.collections.DoubleIterator; import speiger.src.collections.doubles.functions.DoubleConsumer; @@ -914,7 +916,7 @@ public class Double2DoubleOpenHashMap extends AbstractDouble2DoubleMap implement } @Override - public Double2DoubleMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Double2DoubleMap.Entry state = null; boolean empty = true; @@ -931,25 +933,25 @@ public class Double2DoubleOpenHashMap extends AbstractDouble2DoubleMap implement } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Double2DoubleMap.Entry 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(Double.doubleToLongBits(keys[i]) != 0) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1121,7 +1123,7 @@ public class Double2DoubleOpenHashMap extends AbstractDouble2DoubleMap implement } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -1138,18 +1140,18 @@ public class Double2DoubleOpenHashMap extends AbstractDouble2DoubleMap implement } state = operator.applyAsDouble(state, keys[i]); } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0D; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OptionalDouble.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OptionalDouble.of(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(Double.doubleToLongBits(keys[i]) != 0 && filter.test(keys[i])) return keys[i]; + if(Double.doubleToLongBits(keys[i]) != 0 && filter.test(keys[i])) return OptionalDouble.of(keys[i]); } - return 0D; + return OptionalDouble.empty(); } @Override @@ -1264,7 +1266,7 @@ public class Double2DoubleOpenHashMap extends AbstractDouble2DoubleMap implement } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -1281,18 +1283,18 @@ public class Double2DoubleOpenHashMap extends AbstractDouble2DoubleMap implement } state = operator.applyAsDouble(state, values[i]); } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0D; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return OptionalDouble.empty(); + if(containsNull && filter.test(values[nullIndex])) return OptionalDouble.of(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(Double.doubleToLongBits(keys[i]) != 0 && filter.test(values[i])) return values[i]; + if(Double.doubleToLongBits(keys[i]) != 0 && filter.test(values[i])) return OptionalDouble.of(values[i]); } - return 0D; + return OptionalDouble.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/doubles/maps/impl/hash/Double2FloatLinkedOpenHashMap.java b/src/main/java/speiger/src/collections/doubles/maps/impl/hash/Double2FloatLinkedOpenHashMap.java index 9e5104e..8eea882 100644 --- a/src/main/java/speiger/src/collections/doubles/maps/impl/hash/Double2FloatLinkedOpenHashMap.java +++ b/src/main/java/speiger/src/collections/doubles/maps/impl/hash/Double2FloatLinkedOpenHashMap.java @@ -4,10 +4,12 @@ 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; import java.util.function.DoublePredicate; +import java.util.OptionalDouble; import speiger.src.collections.doubles.collections.DoubleBidirectionalIterator; import speiger.src.collections.doubles.functions.DoubleConsumer; @@ -32,6 +34,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectFloatConsumer; import speiger.src.collections.floats.functions.function.FloatPredicate; +import speiger.src.collections.floats.functions.OptionalFloat; import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOperator; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.objects.lists.ObjectListIterator; @@ -669,6 +672,10 @@ public class Double2FloatLinkedOpenHashMap extends Double2FloatOpenHashMap imple } private class MapEntrySet extends AbstractObjectSet implements Double2FloatOrderedMap.FastOrderedSet { + @Override + public void addFirst(Double2FloatMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Double2FloatMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Double2FloatMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -834,7 +841,7 @@ public class Double2FloatLinkedOpenHashMap extends Double2FloatOpenHashMap imple } @Override - public Double2FloatMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Double2FloatMap.Entry state = null; boolean empty = true; @@ -849,21 +856,21 @@ public class Double2FloatLinkedOpenHashMap extends Double2FloatOpenHashMap imple state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Double2FloatMap.Entry 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 @@ -939,6 +946,12 @@ public class Double2FloatLinkedOpenHashMap extends Double2FloatOpenHashMap imple @Override public boolean add(double o) { throw new UnsupportedOperationException(); } + + @Override + public void addFirst(double o) { throw new UnsupportedOperationException(); } + + @Override + public void addLast(double o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(double o) { throw new UnsupportedOperationException(); } @@ -1085,7 +1098,7 @@ public class Double2FloatLinkedOpenHashMap extends Double2FloatOpenHashMap imple } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -1100,19 +1113,19 @@ public class Double2FloatLinkedOpenHashMap extends Double2FloatOpenHashMap imple state = operator.applyAsDouble(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0D; + if(size() <= 0) return OptionalDouble.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalDouble.of(keys[index]); index = (int)links[index]; } - return 0D; + return OptionalDouble.empty(); } @Override @@ -1248,7 +1261,7 @@ public class Double2FloatLinkedOpenHashMap extends Double2FloatOpenHashMap imple } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -1263,19 +1276,19 @@ public class Double2FloatLinkedOpenHashMap extends Double2FloatOpenHashMap imple state = operator.applyAsFloat(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0F; + if(size() <= 0) return OptionalFloat.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalFloat.of(values[index]); index = (int)links[index]; } - return 0F; + return OptionalFloat.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/doubles/maps/impl/hash/Double2FloatOpenHashMap.java b/src/main/java/speiger/src/collections/doubles/maps/impl/hash/Double2FloatOpenHashMap.java index 481ba54..74c7843 100644 --- a/src/main/java/speiger/src/collections/doubles/maps/impl/hash/Double2FloatOpenHashMap.java +++ b/src/main/java/speiger/src/collections/doubles/maps/impl/hash/Double2FloatOpenHashMap.java @@ -5,10 +5,12 @@ 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; import java.util.function.DoublePredicate; +import java.util.OptionalDouble; import speiger.src.collections.doubles.collections.DoubleIterator; import speiger.src.collections.doubles.functions.DoubleConsumer; @@ -34,6 +36,7 @@ import speiger.src.collections.floats.functions.FloatConsumer; import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectFloatConsumer; import speiger.src.collections.floats.functions.function.FloatPredicate; +import speiger.src.collections.floats.functions.OptionalFloat; import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.sets.AbstractObjectSet; import speiger.src.collections.objects.sets.ObjectSet; @@ -920,7 +923,7 @@ public class Double2FloatOpenHashMap extends AbstractDouble2FloatMap implements } @Override - public Double2FloatMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Double2FloatMap.Entry state = null; boolean empty = true; @@ -937,25 +940,25 @@ public class Double2FloatOpenHashMap extends AbstractDouble2FloatMap implements } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Double2FloatMap.Entry 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(Double.doubleToLongBits(keys[i]) != 0) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1127,7 +1130,7 @@ public class Double2FloatOpenHashMap extends AbstractDouble2FloatMap implements } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -1144,18 +1147,18 @@ public class Double2FloatOpenHashMap extends AbstractDouble2FloatMap implements } state = operator.applyAsDouble(state, keys[i]); } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0D; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OptionalDouble.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OptionalDouble.of(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(Double.doubleToLongBits(keys[i]) != 0 && filter.test(keys[i])) return keys[i]; + if(Double.doubleToLongBits(keys[i]) != 0 && filter.test(keys[i])) return OptionalDouble.of(keys[i]); } - return 0D; + return OptionalDouble.empty(); } @Override @@ -1270,7 +1273,7 @@ public class Double2FloatOpenHashMap extends AbstractDouble2FloatMap implements } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -1287,18 +1290,18 @@ public class Double2FloatOpenHashMap extends AbstractDouble2FloatMap implements } state = operator.applyAsFloat(state, values[i]); } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0F; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return OptionalFloat.empty(); + if(containsNull && filter.test(values[nullIndex])) return OptionalFloat.of(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(Double.doubleToLongBits(keys[i]) != 0 && filter.test(values[i])) return values[i]; + if(Double.doubleToLongBits(keys[i]) != 0 && filter.test(values[i])) return OptionalFloat.of(values[i]); } - return 0F; + return OptionalFloat.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/doubles/maps/impl/hash/Double2IntLinkedOpenHashMap.java b/src/main/java/speiger/src/collections/doubles/maps/impl/hash/Double2IntLinkedOpenHashMap.java index 127ace2..a9020a6 100644 --- a/src/main/java/speiger/src/collections/doubles/maps/impl/hash/Double2IntLinkedOpenHashMap.java +++ b/src/main/java/speiger/src/collections/doubles/maps/impl/hash/Double2IntLinkedOpenHashMap.java @@ -4,11 +4,14 @@ 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; import java.util.function.DoublePredicate; +import java.util.OptionalDouble; import java.util.function.IntPredicate; +import java.util.OptionalInt; import speiger.src.collections.doubles.collections.DoubleBidirectionalIterator; import speiger.src.collections.doubles.functions.DoubleConsumer; @@ -669,6 +672,10 @@ public class Double2IntLinkedOpenHashMap extends Double2IntOpenHashMap implement } private class MapEntrySet extends AbstractObjectSet implements Double2IntOrderedMap.FastOrderedSet { + @Override + public void addFirst(Double2IntMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Double2IntMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Double2IntMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -834,7 +841,7 @@ public class Double2IntLinkedOpenHashMap extends Double2IntOpenHashMap implement } @Override - public Double2IntMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Double2IntMap.Entry state = null; boolean empty = true; @@ -849,21 +856,21 @@ public class Double2IntLinkedOpenHashMap extends Double2IntOpenHashMap implement state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Double2IntMap.Entry 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 @@ -939,6 +946,12 @@ public class Double2IntLinkedOpenHashMap extends Double2IntOpenHashMap implement @Override public boolean add(double o) { throw new UnsupportedOperationException(); } + + @Override + public void addFirst(double o) { throw new UnsupportedOperationException(); } + + @Override + public void addLast(double o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(double o) { throw new UnsupportedOperationException(); } @@ -1085,7 +1098,7 @@ public class Double2IntLinkedOpenHashMap extends Double2IntOpenHashMap implement } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -1100,19 +1113,19 @@ public class Double2IntLinkedOpenHashMap extends Double2IntOpenHashMap implement state = operator.applyAsDouble(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0D; + if(size() <= 0) return OptionalDouble.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalDouble.of(keys[index]); index = (int)links[index]; } - return 0D; + return OptionalDouble.empty(); } @Override @@ -1248,7 +1261,7 @@ public class Double2IntLinkedOpenHashMap extends Double2IntOpenHashMap implement } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -1263,19 +1276,19 @@ public class Double2IntLinkedOpenHashMap extends Double2IntOpenHashMap implement state = operator.applyAsInt(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0; + if(size() <= 0) return OptionalInt.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalInt.of(values[index]); index = (int)links[index]; } - return 0; + return OptionalInt.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/doubles/maps/impl/hash/Double2IntOpenHashMap.java b/src/main/java/speiger/src/collections/doubles/maps/impl/hash/Double2IntOpenHashMap.java index 4678e7b..596eef4 100644 --- a/src/main/java/speiger/src/collections/doubles/maps/impl/hash/Double2IntOpenHashMap.java +++ b/src/main/java/speiger/src/collections/doubles/maps/impl/hash/Double2IntOpenHashMap.java @@ -5,11 +5,14 @@ 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; import java.util.function.DoublePredicate; +import java.util.OptionalDouble; import java.util.function.IntPredicate; +import java.util.OptionalInt; import speiger.src.collections.doubles.collections.DoubleIterator; import speiger.src.collections.doubles.functions.DoubleConsumer; @@ -920,7 +923,7 @@ public class Double2IntOpenHashMap extends AbstractDouble2IntMap implements ITri } @Override - public Double2IntMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Double2IntMap.Entry state = null; boolean empty = true; @@ -937,25 +940,25 @@ public class Double2IntOpenHashMap extends AbstractDouble2IntMap implements ITri } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Double2IntMap.Entry 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(Double.doubleToLongBits(keys[i]) != 0) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1127,7 +1130,7 @@ public class Double2IntOpenHashMap extends AbstractDouble2IntMap implements ITri } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -1144,18 +1147,18 @@ public class Double2IntOpenHashMap extends AbstractDouble2IntMap implements ITri } state = operator.applyAsDouble(state, keys[i]); } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0D; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OptionalDouble.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OptionalDouble.of(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(Double.doubleToLongBits(keys[i]) != 0 && filter.test(keys[i])) return keys[i]; + if(Double.doubleToLongBits(keys[i]) != 0 && filter.test(keys[i])) return OptionalDouble.of(keys[i]); } - return 0D; + return OptionalDouble.empty(); } @Override @@ -1270,7 +1273,7 @@ public class Double2IntOpenHashMap extends AbstractDouble2IntMap implements ITri } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -1287,18 +1290,18 @@ public class Double2IntOpenHashMap extends AbstractDouble2IntMap implements ITri } state = operator.applyAsInt(state, values[i]); } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return OptionalInt.empty(); + if(containsNull && filter.test(values[nullIndex])) return OptionalInt.of(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(Double.doubleToLongBits(keys[i]) != 0 && filter.test(values[i])) return values[i]; + if(Double.doubleToLongBits(keys[i]) != 0 && filter.test(values[i])) return OptionalInt.of(values[i]); } - return 0; + return OptionalInt.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/doubles/maps/impl/hash/Double2LongLinkedOpenHashMap.java b/src/main/java/speiger/src/collections/doubles/maps/impl/hash/Double2LongLinkedOpenHashMap.java index 223edec..a789d89 100644 --- a/src/main/java/speiger/src/collections/doubles/maps/impl/hash/Double2LongLinkedOpenHashMap.java +++ b/src/main/java/speiger/src/collections/doubles/maps/impl/hash/Double2LongLinkedOpenHashMap.java @@ -4,11 +4,14 @@ 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; import java.util.function.DoublePredicate; +import java.util.OptionalDouble; import java.util.function.LongPredicate; +import java.util.OptionalLong; import speiger.src.collections.doubles.collections.DoubleBidirectionalIterator; import speiger.src.collections.doubles.functions.DoubleConsumer; @@ -669,6 +672,10 @@ public class Double2LongLinkedOpenHashMap extends Double2LongOpenHashMap impleme } private class MapEntrySet extends AbstractObjectSet implements Double2LongOrderedMap.FastOrderedSet { + @Override + public void addFirst(Double2LongMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Double2LongMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Double2LongMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -834,7 +841,7 @@ public class Double2LongLinkedOpenHashMap extends Double2LongOpenHashMap impleme } @Override - public Double2LongMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Double2LongMap.Entry state = null; boolean empty = true; @@ -849,21 +856,21 @@ public class Double2LongLinkedOpenHashMap extends Double2LongOpenHashMap impleme state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Double2LongMap.Entry 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 @@ -939,6 +946,12 @@ public class Double2LongLinkedOpenHashMap extends Double2LongOpenHashMap impleme @Override public boolean add(double o) { throw new UnsupportedOperationException(); } + + @Override + public void addFirst(double o) { throw new UnsupportedOperationException(); } + + @Override + public void addLast(double o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(double o) { throw new UnsupportedOperationException(); } @@ -1085,7 +1098,7 @@ public class Double2LongLinkedOpenHashMap extends Double2LongOpenHashMap impleme } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -1100,19 +1113,19 @@ public class Double2LongLinkedOpenHashMap extends Double2LongOpenHashMap impleme state = operator.applyAsDouble(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0D; + if(size() <= 0) return OptionalDouble.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalDouble.of(keys[index]); index = (int)links[index]; } - return 0D; + return OptionalDouble.empty(); } @Override @@ -1248,7 +1261,7 @@ public class Double2LongLinkedOpenHashMap extends Double2LongOpenHashMap impleme } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -1263,19 +1276,19 @@ public class Double2LongLinkedOpenHashMap extends Double2LongOpenHashMap impleme state = operator.applyAsLong(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0L; + if(size() <= 0) return OptionalLong.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalLong.of(values[index]); index = (int)links[index]; } - return 0L; + return OptionalLong.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/doubles/maps/impl/hash/Double2LongOpenHashMap.java b/src/main/java/speiger/src/collections/doubles/maps/impl/hash/Double2LongOpenHashMap.java index 5b3cbaa..15ed370 100644 --- a/src/main/java/speiger/src/collections/doubles/maps/impl/hash/Double2LongOpenHashMap.java +++ b/src/main/java/speiger/src/collections/doubles/maps/impl/hash/Double2LongOpenHashMap.java @@ -5,11 +5,14 @@ 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; import java.util.function.DoublePredicate; +import java.util.OptionalDouble; import java.util.function.LongPredicate; +import java.util.OptionalLong; import speiger.src.collections.doubles.collections.DoubleIterator; import speiger.src.collections.doubles.functions.DoubleConsumer; @@ -920,7 +923,7 @@ public class Double2LongOpenHashMap extends AbstractDouble2LongMap implements IT } @Override - public Double2LongMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Double2LongMap.Entry state = null; boolean empty = true; @@ -937,25 +940,25 @@ public class Double2LongOpenHashMap extends AbstractDouble2LongMap implements IT } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Double2LongMap.Entry 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(Double.doubleToLongBits(keys[i]) != 0) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1127,7 +1130,7 @@ public class Double2LongOpenHashMap extends AbstractDouble2LongMap implements IT } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -1144,18 +1147,18 @@ public class Double2LongOpenHashMap extends AbstractDouble2LongMap implements IT } state = operator.applyAsDouble(state, keys[i]); } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0D; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OptionalDouble.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OptionalDouble.of(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(Double.doubleToLongBits(keys[i]) != 0 && filter.test(keys[i])) return keys[i]; + if(Double.doubleToLongBits(keys[i]) != 0 && filter.test(keys[i])) return OptionalDouble.of(keys[i]); } - return 0D; + return OptionalDouble.empty(); } @Override @@ -1270,7 +1273,7 @@ public class Double2LongOpenHashMap extends AbstractDouble2LongMap implements IT } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -1287,18 +1290,18 @@ public class Double2LongOpenHashMap extends AbstractDouble2LongMap implements IT } state = operator.applyAsLong(state, values[i]); } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0L; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return OptionalLong.empty(); + if(containsNull && filter.test(values[nullIndex])) return OptionalLong.of(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(Double.doubleToLongBits(keys[i]) != 0 && filter.test(values[i])) return values[i]; + if(Double.doubleToLongBits(keys[i]) != 0 && filter.test(values[i])) return OptionalLong.of(values[i]); } - return 0L; + return OptionalLong.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/doubles/maps/impl/hash/Double2ObjectLinkedOpenHashMap.java b/src/main/java/speiger/src/collections/doubles/maps/impl/hash/Double2ObjectLinkedOpenHashMap.java index 7799d25..074d734 100644 --- a/src/main/java/speiger/src/collections/doubles/maps/impl/hash/Double2ObjectLinkedOpenHashMap.java +++ b/src/main/java/speiger/src/collections/doubles/maps/impl/hash/Double2ObjectLinkedOpenHashMap.java @@ -4,10 +4,12 @@ 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; import java.util.function.DoublePredicate; +import java.util.OptionalDouble; import speiger.src.collections.doubles.collections.DoubleBidirectionalIterator; import speiger.src.collections.doubles.functions.DoubleConsumer; @@ -653,6 +655,10 @@ public class Double2ObjectLinkedOpenHashMap extends Double2ObjectOpenHashMap< } private class MapEntrySet extends AbstractObjectSet> implements Double2ObjectOrderedMap.FastOrderedSet { + @Override + public void addFirst(Double2ObjectMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Double2ObjectMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Double2ObjectMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -818,7 +824,7 @@ public class Double2ObjectLinkedOpenHashMap extends Double2ObjectOpenHashMap< } @Override - public Double2ObjectMap.Entry reduce(ObjectObjectUnaryOperator, Double2ObjectMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Double2ObjectMap.Entry> operator) { Objects.requireNonNull(operator); Double2ObjectMap.Entry state = null; boolean empty = true; @@ -833,21 +839,21 @@ public class Double2ObjectLinkedOpenHashMap extends Double2ObjectOpenHashMap< state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Double2ObjectMap.Entry 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 @@ -923,6 +929,12 @@ public class Double2ObjectLinkedOpenHashMap extends Double2ObjectOpenHashMap< @Override public boolean add(double o) { throw new UnsupportedOperationException(); } + + @Override + public void addFirst(double o) { throw new UnsupportedOperationException(); } + + @Override + public void addLast(double o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(double o) { throw new UnsupportedOperationException(); } @@ -1069,7 +1081,7 @@ public class Double2ObjectLinkedOpenHashMap extends Double2ObjectOpenHashMap< } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -1084,19 +1096,19 @@ public class Double2ObjectLinkedOpenHashMap extends Double2ObjectOpenHashMap< state = operator.applyAsDouble(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0D; + if(size() <= 0) return OptionalDouble.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalDouble.of(keys[index]); index = (int)links[index]; } - return 0D; + return OptionalDouble.empty(); } @Override @@ -1233,7 +1245,7 @@ public class Double2ObjectLinkedOpenHashMap extends Double2ObjectOpenHashMap< } @Override - public V reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); V state = null; boolean empty = true; @@ -1248,19 +1260,19 @@ public class Double2ObjectLinkedOpenHashMap extends Double2ObjectOpenHashMap< state = operator.apply(state, values[index]); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public V findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return Optional.ofNullable(values[index]); index = (int)links[index]; } - return null; + return Optional.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/doubles/maps/impl/hash/Double2ObjectOpenHashMap.java b/src/main/java/speiger/src/collections/doubles/maps/impl/hash/Double2ObjectOpenHashMap.java index 5fd9897..dc23986 100644 --- a/src/main/java/speiger/src/collections/doubles/maps/impl/hash/Double2ObjectOpenHashMap.java +++ b/src/main/java/speiger/src/collections/doubles/maps/impl/hash/Double2ObjectOpenHashMap.java @@ -5,10 +5,12 @@ 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; import java.util.function.DoublePredicate; +import java.util.OptionalDouble; import speiger.src.collections.doubles.collections.DoubleIterator; import speiger.src.collections.doubles.functions.DoubleConsumer; @@ -837,7 +839,7 @@ public class Double2ObjectOpenHashMap extends AbstractDouble2ObjectMap imp } @Override - public Double2ObjectMap.Entry reduce(ObjectObjectUnaryOperator, Double2ObjectMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Double2ObjectMap.Entry> operator) { Objects.requireNonNull(operator); Double2ObjectMap.Entry state = null; boolean empty = true; @@ -854,25 +856,25 @@ public class Double2ObjectOpenHashMap extends AbstractDouble2ObjectMap imp } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Double2ObjectMap.Entry 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(Double.doubleToLongBits(keys[i]) != 0) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1044,7 +1046,7 @@ public class Double2ObjectOpenHashMap extends AbstractDouble2ObjectMap imp } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -1061,18 +1063,18 @@ public class Double2ObjectOpenHashMap extends AbstractDouble2ObjectMap imp } state = operator.applyAsDouble(state, keys[i]); } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0D; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OptionalDouble.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OptionalDouble.of(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(Double.doubleToLongBits(keys[i]) != 0 && filter.test(keys[i])) return keys[i]; + if(Double.doubleToLongBits(keys[i]) != 0 && filter.test(keys[i])) return OptionalDouble.of(keys[i]); } - return 0D; + return OptionalDouble.empty(); } @Override @@ -1187,7 +1189,7 @@ public class Double2ObjectOpenHashMap extends AbstractDouble2ObjectMap imp } @Override - public V reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); V state = null; boolean empty = true; @@ -1204,18 +1206,18 @@ public class Double2ObjectOpenHashMap extends AbstractDouble2ObjectMap imp } state = operator.apply(state, values[i]); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public V findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return Optional.empty(); + if(containsNull && filter.test(values[nullIndex])) return Optional.ofNullable(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(Double.doubleToLongBits(keys[i]) != 0 && filter.test(values[i])) return values[i]; + if(Double.doubleToLongBits(keys[i]) != 0 && filter.test(values[i])) return Optional.ofNullable(values[i]); } - return null; + return Optional.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/doubles/maps/impl/hash/Double2ShortLinkedOpenHashMap.java b/src/main/java/speiger/src/collections/doubles/maps/impl/hash/Double2ShortLinkedOpenHashMap.java index 81f2e66..7584844 100644 --- a/src/main/java/speiger/src/collections/doubles/maps/impl/hash/Double2ShortLinkedOpenHashMap.java +++ b/src/main/java/speiger/src/collections/doubles/maps/impl/hash/Double2ShortLinkedOpenHashMap.java @@ -4,10 +4,12 @@ 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; import java.util.function.DoublePredicate; +import java.util.OptionalDouble; import speiger.src.collections.doubles.collections.DoubleBidirectionalIterator; import speiger.src.collections.doubles.functions.DoubleConsumer; @@ -32,6 +34,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectShortConsumer; import speiger.src.collections.shorts.functions.function.ShortPredicate; +import speiger.src.collections.shorts.functions.OptionalShort; import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOperator; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.objects.lists.ObjectListIterator; @@ -669,6 +672,10 @@ public class Double2ShortLinkedOpenHashMap extends Double2ShortOpenHashMap imple } private class MapEntrySet extends AbstractObjectSet implements Double2ShortOrderedMap.FastOrderedSet { + @Override + public void addFirst(Double2ShortMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Double2ShortMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Double2ShortMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -834,7 +841,7 @@ public class Double2ShortLinkedOpenHashMap extends Double2ShortOpenHashMap imple } @Override - public Double2ShortMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Double2ShortMap.Entry state = null; boolean empty = true; @@ -849,21 +856,21 @@ public class Double2ShortLinkedOpenHashMap extends Double2ShortOpenHashMap imple state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Double2ShortMap.Entry 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 @@ -939,6 +946,12 @@ public class Double2ShortLinkedOpenHashMap extends Double2ShortOpenHashMap imple @Override public boolean add(double o) { throw new UnsupportedOperationException(); } + + @Override + public void addFirst(double o) { throw new UnsupportedOperationException(); } + + @Override + public void addLast(double o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(double o) { throw new UnsupportedOperationException(); } @@ -1085,7 +1098,7 @@ public class Double2ShortLinkedOpenHashMap extends Double2ShortOpenHashMap imple } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -1100,19 +1113,19 @@ public class Double2ShortLinkedOpenHashMap extends Double2ShortOpenHashMap imple state = operator.applyAsDouble(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0D; + if(size() <= 0) return OptionalDouble.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalDouble.of(keys[index]); index = (int)links[index]; } - return 0D; + return OptionalDouble.empty(); } @Override @@ -1248,7 +1261,7 @@ public class Double2ShortLinkedOpenHashMap extends Double2ShortOpenHashMap imple } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -1263,19 +1276,19 @@ public class Double2ShortLinkedOpenHashMap extends Double2ShortOpenHashMap imple state = operator.applyAsShort(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (short)0; + if(size() <= 0) return OptionalShort.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalShort.of(values[index]); index = (int)links[index]; } - return (short)0; + return OptionalShort.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/doubles/maps/impl/hash/Double2ShortOpenHashMap.java b/src/main/java/speiger/src/collections/doubles/maps/impl/hash/Double2ShortOpenHashMap.java index 321957e..0b7ca7c 100644 --- a/src/main/java/speiger/src/collections/doubles/maps/impl/hash/Double2ShortOpenHashMap.java +++ b/src/main/java/speiger/src/collections/doubles/maps/impl/hash/Double2ShortOpenHashMap.java @@ -5,10 +5,12 @@ 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; import java.util.function.DoublePredicate; +import java.util.OptionalDouble; import speiger.src.collections.doubles.collections.DoubleIterator; import speiger.src.collections.doubles.functions.DoubleConsumer; @@ -34,6 +36,7 @@ import speiger.src.collections.shorts.functions.ShortConsumer; import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectShortConsumer; import speiger.src.collections.shorts.functions.function.ShortPredicate; +import speiger.src.collections.shorts.functions.OptionalShort; import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.sets.AbstractObjectSet; import speiger.src.collections.objects.sets.ObjectSet; @@ -920,7 +923,7 @@ public class Double2ShortOpenHashMap extends AbstractDouble2ShortMap implements } @Override - public Double2ShortMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Double2ShortMap.Entry state = null; boolean empty = true; @@ -937,25 +940,25 @@ public class Double2ShortOpenHashMap extends AbstractDouble2ShortMap implements } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Double2ShortMap.Entry 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(Double.doubleToLongBits(keys[i]) != 0) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1127,7 +1130,7 @@ public class Double2ShortOpenHashMap extends AbstractDouble2ShortMap implements } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -1144,18 +1147,18 @@ public class Double2ShortOpenHashMap extends AbstractDouble2ShortMap implements } state = operator.applyAsDouble(state, keys[i]); } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0D; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OptionalDouble.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OptionalDouble.of(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(Double.doubleToLongBits(keys[i]) != 0 && filter.test(keys[i])) return keys[i]; + if(Double.doubleToLongBits(keys[i]) != 0 && filter.test(keys[i])) return OptionalDouble.of(keys[i]); } - return 0D; + return OptionalDouble.empty(); } @Override @@ -1270,7 +1273,7 @@ public class Double2ShortOpenHashMap extends AbstractDouble2ShortMap implements } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -1287,18 +1290,18 @@ public class Double2ShortOpenHashMap extends AbstractDouble2ShortMap implements } state = operator.applyAsShort(state, values[i]); } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (short)0; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return OptionalShort.empty(); + if(containsNull && filter.test(values[nullIndex])) return OptionalShort.of(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(Double.doubleToLongBits(keys[i]) != 0 && filter.test(values[i])) return values[i]; + if(Double.doubleToLongBits(keys[i]) != 0 && filter.test(values[i])) return OptionalShort.of(values[i]); } - return (short)0; + return OptionalShort.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/doubles/maps/impl/immutable/ImmutableDouble2BooleanOpenHashMap.java b/src/main/java/speiger/src/collections/doubles/maps/impl/immutable/ImmutableDouble2BooleanOpenHashMap.java index ba63b85..6862154 100644 --- a/src/main/java/speiger/src/collections/doubles/maps/impl/immutable/ImmutableDouble2BooleanOpenHashMap.java +++ b/src/main/java/speiger/src/collections/doubles/maps/impl/immutable/ImmutableDouble2BooleanOpenHashMap.java @@ -4,10 +4,12 @@ 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; import java.util.function.DoublePredicate; +import java.util.OptionalDouble; import speiger.src.collections.doubles.collections.DoubleBidirectionalIterator; import speiger.src.collections.doubles.functions.DoubleConsumer; @@ -28,6 +30,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectBooleanConsumer; import speiger.src.collections.booleans.functions.function.BooleanPredicate; +import speiger.src.collections.booleans.functions.OptionalBoolean; import speiger.src.collections.doubles.sets.AbstractDoubleSet; import speiger.src.collections.booleans.collections.AbstractBooleanCollection; import speiger.src.collections.booleans.collections.BooleanOrderedCollection; @@ -533,6 +536,10 @@ public class ImmutableDouble2BooleanOpenHashMap extends AbstractDouble2BooleanMa } private class MapEntrySet extends AbstractObjectSet implements Double2BooleanOrderedMap.FastOrderedSet { + @Override + public void addFirst(Double2BooleanMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Double2BooleanMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Double2BooleanMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -686,7 +693,7 @@ public class ImmutableDouble2BooleanOpenHashMap extends AbstractDouble2BooleanMa } @Override - public Double2BooleanMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Double2BooleanMap.Entry state = null; boolean empty = true; @@ -701,21 +708,21 @@ public class ImmutableDouble2BooleanOpenHashMap extends AbstractDouble2BooleanMa state = operator.apply(state, new BasicEntry(keys[index], values[index])); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Double2BooleanMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry entry = new BasicEntry(); 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 @@ -778,6 +785,12 @@ public class ImmutableDouble2BooleanOpenHashMap extends AbstractDouble2BooleanMa throw new UnsupportedOperationException(); } + @Override + public void addFirst(double o) { throw new UnsupportedOperationException(); } + + @Override + public void addLast(double o) { throw new UnsupportedOperationException(); } + @Override public boolean addAndMoveToFirst(double o) { throw new UnsupportedOperationException(); } @@ -913,7 +926,7 @@ public class ImmutableDouble2BooleanOpenHashMap extends AbstractDouble2BooleanMa } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -928,19 +941,19 @@ public class ImmutableDouble2BooleanOpenHashMap extends AbstractDouble2BooleanMa state = operator.applyAsDouble(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0D; + if(size() <= 0) return OptionalDouble.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalDouble.of(keys[index]); index = (int)links[index]; } - return 0D; + return OptionalDouble.empty(); } @Override @@ -1067,7 +1080,7 @@ public class ImmutableDouble2BooleanOpenHashMap extends AbstractDouble2BooleanMa } @Override - public boolean reduce(BooleanBooleanUnaryOperator operator) { + public OptionalBoolean reduce(BooleanBooleanUnaryOperator operator) { Objects.requireNonNull(operator); boolean state = false; boolean empty = true; @@ -1082,19 +1095,19 @@ public class ImmutableDouble2BooleanOpenHashMap extends AbstractDouble2BooleanMa state = operator.applyAsBoolean(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalBoolean.empty() : OptionalBoolean.of(state); } @Override - public boolean findFirst(BooleanPredicate filter) { + public OptionalBoolean findFirst(BooleanPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return false; + if(size() <= 0) return OptionalBoolean.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalBoolean.of(values[index]); index = (int)links[index]; } - return false; + return OptionalBoolean.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/doubles/maps/impl/immutable/ImmutableDouble2ByteOpenHashMap.java b/src/main/java/speiger/src/collections/doubles/maps/impl/immutable/ImmutableDouble2ByteOpenHashMap.java index fda8ce6..29a53a7 100644 --- a/src/main/java/speiger/src/collections/doubles/maps/impl/immutable/ImmutableDouble2ByteOpenHashMap.java +++ b/src/main/java/speiger/src/collections/doubles/maps/impl/immutable/ImmutableDouble2ByteOpenHashMap.java @@ -4,10 +4,12 @@ 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; import java.util.function.DoublePredicate; +import java.util.OptionalDouble; import speiger.src.collections.doubles.collections.DoubleBidirectionalIterator; import speiger.src.collections.doubles.functions.DoubleConsumer; @@ -29,6 +31,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectByteConsumer; import speiger.src.collections.bytes.functions.function.BytePredicate; +import speiger.src.collections.bytes.functions.OptionalByte; import speiger.src.collections.doubles.sets.AbstractDoubleSet; import speiger.src.collections.bytes.collections.AbstractByteCollection; import speiger.src.collections.bytes.collections.ByteOrderedCollection; @@ -538,6 +541,10 @@ public class ImmutableDouble2ByteOpenHashMap extends AbstractDouble2ByteMap impl } private class MapEntrySet extends AbstractObjectSet implements Double2ByteOrderedMap.FastOrderedSet { + @Override + public void addFirst(Double2ByteMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Double2ByteMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Double2ByteMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -691,7 +698,7 @@ public class ImmutableDouble2ByteOpenHashMap extends AbstractDouble2ByteMap impl } @Override - public Double2ByteMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Double2ByteMap.Entry state = null; boolean empty = true; @@ -706,21 +713,21 @@ public class ImmutableDouble2ByteOpenHashMap extends AbstractDouble2ByteMap impl state = operator.apply(state, new BasicEntry(keys[index], values[index])); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Double2ByteMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry entry = new BasicEntry(); 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 @@ -783,6 +790,12 @@ public class ImmutableDouble2ByteOpenHashMap extends AbstractDouble2ByteMap impl throw new UnsupportedOperationException(); } + @Override + public void addFirst(double o) { throw new UnsupportedOperationException(); } + + @Override + public void addLast(double o) { throw new UnsupportedOperationException(); } + @Override public boolean addAndMoveToFirst(double o) { throw new UnsupportedOperationException(); } @@ -918,7 +931,7 @@ public class ImmutableDouble2ByteOpenHashMap extends AbstractDouble2ByteMap impl } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -933,19 +946,19 @@ public class ImmutableDouble2ByteOpenHashMap extends AbstractDouble2ByteMap impl state = operator.applyAsDouble(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0D; + if(size() <= 0) return OptionalDouble.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalDouble.of(keys[index]); index = (int)links[index]; } - return 0D; + return OptionalDouble.empty(); } @Override @@ -1072,7 +1085,7 @@ public class ImmutableDouble2ByteOpenHashMap extends AbstractDouble2ByteMap impl } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -1087,19 +1100,19 @@ public class ImmutableDouble2ByteOpenHashMap extends AbstractDouble2ByteMap impl state = operator.applyAsByte(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (byte)0; + if(size() <= 0) return OptionalByte.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalByte.of(values[index]); index = (int)links[index]; } - return (byte)0; + return OptionalByte.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/doubles/maps/impl/immutable/ImmutableDouble2CharOpenHashMap.java b/src/main/java/speiger/src/collections/doubles/maps/impl/immutable/ImmutableDouble2CharOpenHashMap.java index 371c271..fddd0bc 100644 --- a/src/main/java/speiger/src/collections/doubles/maps/impl/immutable/ImmutableDouble2CharOpenHashMap.java +++ b/src/main/java/speiger/src/collections/doubles/maps/impl/immutable/ImmutableDouble2CharOpenHashMap.java @@ -4,10 +4,12 @@ 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; import java.util.function.DoublePredicate; +import java.util.OptionalDouble; import speiger.src.collections.doubles.collections.DoubleBidirectionalIterator; import speiger.src.collections.doubles.functions.DoubleConsumer; @@ -29,6 +31,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectCharConsumer; import speiger.src.collections.chars.functions.function.CharPredicate; +import speiger.src.collections.chars.functions.OptionalChar; import speiger.src.collections.doubles.sets.AbstractDoubleSet; import speiger.src.collections.chars.collections.AbstractCharCollection; import speiger.src.collections.chars.collections.CharOrderedCollection; @@ -538,6 +541,10 @@ public class ImmutableDouble2CharOpenHashMap extends AbstractDouble2CharMap impl } private class MapEntrySet extends AbstractObjectSet implements Double2CharOrderedMap.FastOrderedSet { + @Override + public void addFirst(Double2CharMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Double2CharMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Double2CharMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -691,7 +698,7 @@ public class ImmutableDouble2CharOpenHashMap extends AbstractDouble2CharMap impl } @Override - public Double2CharMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Double2CharMap.Entry state = null; boolean empty = true; @@ -706,21 +713,21 @@ public class ImmutableDouble2CharOpenHashMap extends AbstractDouble2CharMap impl state = operator.apply(state, new BasicEntry(keys[index], values[index])); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Double2CharMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry entry = new BasicEntry(); 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 @@ -783,6 +790,12 @@ public class ImmutableDouble2CharOpenHashMap extends AbstractDouble2CharMap impl throw new UnsupportedOperationException(); } + @Override + public void addFirst(double o) { throw new UnsupportedOperationException(); } + + @Override + public void addLast(double o) { throw new UnsupportedOperationException(); } + @Override public boolean addAndMoveToFirst(double o) { throw new UnsupportedOperationException(); } @@ -918,7 +931,7 @@ public class ImmutableDouble2CharOpenHashMap extends AbstractDouble2CharMap impl } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -933,19 +946,19 @@ public class ImmutableDouble2CharOpenHashMap extends AbstractDouble2CharMap impl state = operator.applyAsDouble(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0D; + if(size() <= 0) return OptionalDouble.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalDouble.of(keys[index]); index = (int)links[index]; } - return 0D; + return OptionalDouble.empty(); } @Override @@ -1072,7 +1085,7 @@ public class ImmutableDouble2CharOpenHashMap extends AbstractDouble2CharMap impl } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -1087,19 +1100,19 @@ public class ImmutableDouble2CharOpenHashMap extends AbstractDouble2CharMap impl state = operator.applyAsChar(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (char)0; + if(size() <= 0) return OptionalChar.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalChar.of(values[index]); index = (int)links[index]; } - return (char)0; + return OptionalChar.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/doubles/maps/impl/immutable/ImmutableDouble2DoubleOpenHashMap.java b/src/main/java/speiger/src/collections/doubles/maps/impl/immutable/ImmutableDouble2DoubleOpenHashMap.java index d7e04ab..b171320 100644 --- a/src/main/java/speiger/src/collections/doubles/maps/impl/immutable/ImmutableDouble2DoubleOpenHashMap.java +++ b/src/main/java/speiger/src/collections/doubles/maps/impl/immutable/ImmutableDouble2DoubleOpenHashMap.java @@ -4,10 +4,12 @@ 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; import java.util.function.DoublePredicate; +import java.util.OptionalDouble; import speiger.src.collections.doubles.collections.DoubleBidirectionalIterator; import speiger.src.collections.doubles.functions.DoubleConsumer; @@ -529,6 +531,10 @@ public class ImmutableDouble2DoubleOpenHashMap extends AbstractDouble2DoubleMap } private class MapEntrySet extends AbstractObjectSet implements Double2DoubleOrderedMap.FastOrderedSet { + @Override + public void addFirst(Double2DoubleMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Double2DoubleMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Double2DoubleMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -682,7 +688,7 @@ public class ImmutableDouble2DoubleOpenHashMap extends AbstractDouble2DoubleMap } @Override - public Double2DoubleMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Double2DoubleMap.Entry state = null; boolean empty = true; @@ -697,21 +703,21 @@ public class ImmutableDouble2DoubleOpenHashMap extends AbstractDouble2DoubleMap state = operator.apply(state, new BasicEntry(keys[index], values[index])); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Double2DoubleMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry entry = new BasicEntry(); 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 @@ -774,6 +780,12 @@ public class ImmutableDouble2DoubleOpenHashMap extends AbstractDouble2DoubleMap throw new UnsupportedOperationException(); } + @Override + public void addFirst(double o) { throw new UnsupportedOperationException(); } + + @Override + public void addLast(double o) { throw new UnsupportedOperationException(); } + @Override public boolean addAndMoveToFirst(double o) { throw new UnsupportedOperationException(); } @@ -909,7 +921,7 @@ public class ImmutableDouble2DoubleOpenHashMap extends AbstractDouble2DoubleMap } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -924,19 +936,19 @@ public class ImmutableDouble2DoubleOpenHashMap extends AbstractDouble2DoubleMap state = operator.applyAsDouble(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0D; + if(size() <= 0) return OptionalDouble.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalDouble.of(keys[index]); index = (int)links[index]; } - return 0D; + return OptionalDouble.empty(); } @Override @@ -1063,7 +1075,7 @@ public class ImmutableDouble2DoubleOpenHashMap extends AbstractDouble2DoubleMap } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -1078,19 +1090,19 @@ public class ImmutableDouble2DoubleOpenHashMap extends AbstractDouble2DoubleMap state = operator.applyAsDouble(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0D; + if(size() <= 0) return OptionalDouble.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalDouble.of(values[index]); index = (int)links[index]; } - return 0D; + return OptionalDouble.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/doubles/maps/impl/immutable/ImmutableDouble2FloatOpenHashMap.java b/src/main/java/speiger/src/collections/doubles/maps/impl/immutable/ImmutableDouble2FloatOpenHashMap.java index e1e8c8d..45792fa 100644 --- a/src/main/java/speiger/src/collections/doubles/maps/impl/immutable/ImmutableDouble2FloatOpenHashMap.java +++ b/src/main/java/speiger/src/collections/doubles/maps/impl/immutable/ImmutableDouble2FloatOpenHashMap.java @@ -4,10 +4,12 @@ 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; import java.util.function.DoublePredicate; +import java.util.OptionalDouble; import speiger.src.collections.doubles.collections.DoubleBidirectionalIterator; import speiger.src.collections.doubles.functions.DoubleConsumer; @@ -29,6 +31,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectFloatConsumer; import speiger.src.collections.floats.functions.function.FloatPredicate; +import speiger.src.collections.floats.functions.OptionalFloat; import speiger.src.collections.doubles.sets.AbstractDoubleSet; import speiger.src.collections.floats.collections.AbstractFloatCollection; import speiger.src.collections.floats.collections.FloatOrderedCollection; @@ -538,6 +541,10 @@ public class ImmutableDouble2FloatOpenHashMap extends AbstractDouble2FloatMap im } private class MapEntrySet extends AbstractObjectSet implements Double2FloatOrderedMap.FastOrderedSet { + @Override + public void addFirst(Double2FloatMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Double2FloatMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Double2FloatMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -691,7 +698,7 @@ public class ImmutableDouble2FloatOpenHashMap extends AbstractDouble2FloatMap im } @Override - public Double2FloatMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Double2FloatMap.Entry state = null; boolean empty = true; @@ -706,21 +713,21 @@ public class ImmutableDouble2FloatOpenHashMap extends AbstractDouble2FloatMap im state = operator.apply(state, new BasicEntry(keys[index], values[index])); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Double2FloatMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry entry = new BasicEntry(); 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 @@ -783,6 +790,12 @@ public class ImmutableDouble2FloatOpenHashMap extends AbstractDouble2FloatMap im throw new UnsupportedOperationException(); } + @Override + public void addFirst(double o) { throw new UnsupportedOperationException(); } + + @Override + public void addLast(double o) { throw new UnsupportedOperationException(); } + @Override public boolean addAndMoveToFirst(double o) { throw new UnsupportedOperationException(); } @@ -918,7 +931,7 @@ public class ImmutableDouble2FloatOpenHashMap extends AbstractDouble2FloatMap im } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -933,19 +946,19 @@ public class ImmutableDouble2FloatOpenHashMap extends AbstractDouble2FloatMap im state = operator.applyAsDouble(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0D; + if(size() <= 0) return OptionalDouble.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalDouble.of(keys[index]); index = (int)links[index]; } - return 0D; + return OptionalDouble.empty(); } @Override @@ -1072,7 +1085,7 @@ public class ImmutableDouble2FloatOpenHashMap extends AbstractDouble2FloatMap im } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -1087,19 +1100,19 @@ public class ImmutableDouble2FloatOpenHashMap extends AbstractDouble2FloatMap im state = operator.applyAsFloat(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0F; + if(size() <= 0) return OptionalFloat.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalFloat.of(values[index]); index = (int)links[index]; } - return 0F; + return OptionalFloat.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/doubles/maps/impl/immutable/ImmutableDouble2IntOpenHashMap.java b/src/main/java/speiger/src/collections/doubles/maps/impl/immutable/ImmutableDouble2IntOpenHashMap.java index e5de9d2..776dfb4 100644 --- a/src/main/java/speiger/src/collections/doubles/maps/impl/immutable/ImmutableDouble2IntOpenHashMap.java +++ b/src/main/java/speiger/src/collections/doubles/maps/impl/immutable/ImmutableDouble2IntOpenHashMap.java @@ -4,11 +4,14 @@ 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; import java.util.function.DoublePredicate; +import java.util.OptionalDouble; import java.util.function.IntPredicate; +import java.util.OptionalInt; import speiger.src.collections.doubles.collections.DoubleBidirectionalIterator; import speiger.src.collections.doubles.functions.DoubleConsumer; @@ -538,6 +541,10 @@ public class ImmutableDouble2IntOpenHashMap extends AbstractDouble2IntMap implem } private class MapEntrySet extends AbstractObjectSet implements Double2IntOrderedMap.FastOrderedSet { + @Override + public void addFirst(Double2IntMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Double2IntMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Double2IntMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -691,7 +698,7 @@ public class ImmutableDouble2IntOpenHashMap extends AbstractDouble2IntMap implem } @Override - public Double2IntMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Double2IntMap.Entry state = null; boolean empty = true; @@ -706,21 +713,21 @@ public class ImmutableDouble2IntOpenHashMap extends AbstractDouble2IntMap implem state = operator.apply(state, new BasicEntry(keys[index], values[index])); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Double2IntMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry entry = new BasicEntry(); 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 @@ -783,6 +790,12 @@ public class ImmutableDouble2IntOpenHashMap extends AbstractDouble2IntMap implem throw new UnsupportedOperationException(); } + @Override + public void addFirst(double o) { throw new UnsupportedOperationException(); } + + @Override + public void addLast(double o) { throw new UnsupportedOperationException(); } + @Override public boolean addAndMoveToFirst(double o) { throw new UnsupportedOperationException(); } @@ -918,7 +931,7 @@ public class ImmutableDouble2IntOpenHashMap extends AbstractDouble2IntMap implem } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -933,19 +946,19 @@ public class ImmutableDouble2IntOpenHashMap extends AbstractDouble2IntMap implem state = operator.applyAsDouble(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0D; + if(size() <= 0) return OptionalDouble.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalDouble.of(keys[index]); index = (int)links[index]; } - return 0D; + return OptionalDouble.empty(); } @Override @@ -1072,7 +1085,7 @@ public class ImmutableDouble2IntOpenHashMap extends AbstractDouble2IntMap implem } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -1087,19 +1100,19 @@ public class ImmutableDouble2IntOpenHashMap extends AbstractDouble2IntMap implem state = operator.applyAsInt(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0; + if(size() <= 0) return OptionalInt.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalInt.of(values[index]); index = (int)links[index]; } - return 0; + return OptionalInt.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/doubles/maps/impl/immutable/ImmutableDouble2LongOpenHashMap.java b/src/main/java/speiger/src/collections/doubles/maps/impl/immutable/ImmutableDouble2LongOpenHashMap.java index 44e451a..16364bf 100644 --- a/src/main/java/speiger/src/collections/doubles/maps/impl/immutable/ImmutableDouble2LongOpenHashMap.java +++ b/src/main/java/speiger/src/collections/doubles/maps/impl/immutable/ImmutableDouble2LongOpenHashMap.java @@ -4,11 +4,14 @@ 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; import java.util.function.DoublePredicate; +import java.util.OptionalDouble; import java.util.function.LongPredicate; +import java.util.OptionalLong; import speiger.src.collections.doubles.collections.DoubleBidirectionalIterator; import speiger.src.collections.doubles.functions.DoubleConsumer; @@ -538,6 +541,10 @@ public class ImmutableDouble2LongOpenHashMap extends AbstractDouble2LongMap impl } private class MapEntrySet extends AbstractObjectSet implements Double2LongOrderedMap.FastOrderedSet { + @Override + public void addFirst(Double2LongMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Double2LongMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Double2LongMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -691,7 +698,7 @@ public class ImmutableDouble2LongOpenHashMap extends AbstractDouble2LongMap impl } @Override - public Double2LongMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Double2LongMap.Entry state = null; boolean empty = true; @@ -706,21 +713,21 @@ public class ImmutableDouble2LongOpenHashMap extends AbstractDouble2LongMap impl state = operator.apply(state, new BasicEntry(keys[index], values[index])); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Double2LongMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry entry = new BasicEntry(); 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 @@ -783,6 +790,12 @@ public class ImmutableDouble2LongOpenHashMap extends AbstractDouble2LongMap impl throw new UnsupportedOperationException(); } + @Override + public void addFirst(double o) { throw new UnsupportedOperationException(); } + + @Override + public void addLast(double o) { throw new UnsupportedOperationException(); } + @Override public boolean addAndMoveToFirst(double o) { throw new UnsupportedOperationException(); } @@ -918,7 +931,7 @@ public class ImmutableDouble2LongOpenHashMap extends AbstractDouble2LongMap impl } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -933,19 +946,19 @@ public class ImmutableDouble2LongOpenHashMap extends AbstractDouble2LongMap impl state = operator.applyAsDouble(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0D; + if(size() <= 0) return OptionalDouble.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalDouble.of(keys[index]); index = (int)links[index]; } - return 0D; + return OptionalDouble.empty(); } @Override @@ -1072,7 +1085,7 @@ public class ImmutableDouble2LongOpenHashMap extends AbstractDouble2LongMap impl } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -1087,19 +1100,19 @@ public class ImmutableDouble2LongOpenHashMap extends AbstractDouble2LongMap impl state = operator.applyAsLong(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0L; + if(size() <= 0) return OptionalLong.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalLong.of(values[index]); index = (int)links[index]; } - return 0L; + return OptionalLong.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/doubles/maps/impl/immutable/ImmutableDouble2ObjectOpenHashMap.java b/src/main/java/speiger/src/collections/doubles/maps/impl/immutable/ImmutableDouble2ObjectOpenHashMap.java index f48bb9c..ab20aa6 100644 --- a/src/main/java/speiger/src/collections/doubles/maps/impl/immutable/ImmutableDouble2ObjectOpenHashMap.java +++ b/src/main/java/speiger/src/collections/doubles/maps/impl/immutable/ImmutableDouble2ObjectOpenHashMap.java @@ -4,10 +4,12 @@ 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; import java.util.function.DoublePredicate; +import java.util.OptionalDouble; import speiger.src.collections.doubles.collections.DoubleBidirectionalIterator; import speiger.src.collections.doubles.functions.DoubleConsumer; @@ -509,6 +511,10 @@ public class ImmutableDouble2ObjectOpenHashMap extends AbstractDouble2ObjectM } private class MapEntrySet extends AbstractObjectSet> implements Double2ObjectOrderedMap.FastOrderedSet { + @Override + public void addFirst(Double2ObjectMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Double2ObjectMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Double2ObjectMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -662,7 +668,7 @@ public class ImmutableDouble2ObjectOpenHashMap extends AbstractDouble2ObjectM } @Override - public Double2ObjectMap.Entry reduce(ObjectObjectUnaryOperator, Double2ObjectMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Double2ObjectMap.Entry> operator) { Objects.requireNonNull(operator); Double2ObjectMap.Entry state = null; boolean empty = true; @@ -677,21 +683,21 @@ public class ImmutableDouble2ObjectOpenHashMap extends AbstractDouble2ObjectM state = operator.apply(state, new BasicEntry<>(keys[index], values[index])); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Double2ObjectMap.Entry findFirst(Predicate> filter) { + public Optional> findFirst(Predicate> filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry entry = new BasicEntry<>(); 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 @@ -754,6 +760,12 @@ public class ImmutableDouble2ObjectOpenHashMap extends AbstractDouble2ObjectM throw new UnsupportedOperationException(); } + @Override + public void addFirst(double o) { throw new UnsupportedOperationException(); } + + @Override + public void addLast(double o) { throw new UnsupportedOperationException(); } + @Override public boolean addAndMoveToFirst(double o) { throw new UnsupportedOperationException(); } @@ -889,7 +901,7 @@ public class ImmutableDouble2ObjectOpenHashMap extends AbstractDouble2ObjectM } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -904,19 +916,19 @@ public class ImmutableDouble2ObjectOpenHashMap extends AbstractDouble2ObjectM state = operator.applyAsDouble(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0D; + if(size() <= 0) return OptionalDouble.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalDouble.of(keys[index]); index = (int)links[index]; } - return 0D; + return OptionalDouble.empty(); } @Override @@ -1044,7 +1056,7 @@ public class ImmutableDouble2ObjectOpenHashMap extends AbstractDouble2ObjectM } @Override - public V reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); V state = null; boolean empty = true; @@ -1059,19 +1071,19 @@ public class ImmutableDouble2ObjectOpenHashMap extends AbstractDouble2ObjectM state = operator.apply(state, values[index]); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public V findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return Optional.ofNullable(values[index]); index = (int)links[index]; } - return null; + return Optional.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/doubles/maps/impl/immutable/ImmutableDouble2ShortOpenHashMap.java b/src/main/java/speiger/src/collections/doubles/maps/impl/immutable/ImmutableDouble2ShortOpenHashMap.java index 66f686b..5d6b174 100644 --- a/src/main/java/speiger/src/collections/doubles/maps/impl/immutable/ImmutableDouble2ShortOpenHashMap.java +++ b/src/main/java/speiger/src/collections/doubles/maps/impl/immutable/ImmutableDouble2ShortOpenHashMap.java @@ -4,10 +4,12 @@ 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; import java.util.function.DoublePredicate; +import java.util.OptionalDouble; import speiger.src.collections.doubles.collections.DoubleBidirectionalIterator; import speiger.src.collections.doubles.functions.DoubleConsumer; @@ -29,6 +31,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectShortConsumer; import speiger.src.collections.shorts.functions.function.ShortPredicate; +import speiger.src.collections.shorts.functions.OptionalShort; import speiger.src.collections.doubles.sets.AbstractDoubleSet; import speiger.src.collections.shorts.collections.AbstractShortCollection; import speiger.src.collections.shorts.collections.ShortOrderedCollection; @@ -538,6 +541,10 @@ public class ImmutableDouble2ShortOpenHashMap extends AbstractDouble2ShortMap im } private class MapEntrySet extends AbstractObjectSet implements Double2ShortOrderedMap.FastOrderedSet { + @Override + public void addFirst(Double2ShortMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Double2ShortMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Double2ShortMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -691,7 +698,7 @@ public class ImmutableDouble2ShortOpenHashMap extends AbstractDouble2ShortMap im } @Override - public Double2ShortMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Double2ShortMap.Entry state = null; boolean empty = true; @@ -706,21 +713,21 @@ public class ImmutableDouble2ShortOpenHashMap extends AbstractDouble2ShortMap im state = operator.apply(state, new BasicEntry(keys[index], values[index])); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Double2ShortMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry entry = new BasicEntry(); 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 @@ -783,6 +790,12 @@ public class ImmutableDouble2ShortOpenHashMap extends AbstractDouble2ShortMap im throw new UnsupportedOperationException(); } + @Override + public void addFirst(double o) { throw new UnsupportedOperationException(); } + + @Override + public void addLast(double o) { throw new UnsupportedOperationException(); } + @Override public boolean addAndMoveToFirst(double o) { throw new UnsupportedOperationException(); } @@ -918,7 +931,7 @@ public class ImmutableDouble2ShortOpenHashMap extends AbstractDouble2ShortMap im } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -933,19 +946,19 @@ public class ImmutableDouble2ShortOpenHashMap extends AbstractDouble2ShortMap im state = operator.applyAsDouble(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0D; + if(size() <= 0) return OptionalDouble.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalDouble.of(keys[index]); index = (int)links[index]; } - return 0D; + return OptionalDouble.empty(); } @Override @@ -1072,7 +1085,7 @@ public class ImmutableDouble2ShortOpenHashMap extends AbstractDouble2ShortMap im } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -1087,19 +1100,19 @@ public class ImmutableDouble2ShortOpenHashMap extends AbstractDouble2ShortMap im state = operator.applyAsShort(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (short)0; + if(size() <= 0) return OptionalShort.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalShort.of(values[index]); index = (int)links[index]; } - return (short)0; + return OptionalShort.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/doubles/maps/impl/misc/Double2BooleanArrayMap.java b/src/main/java/speiger/src/collections/doubles/maps/impl/misc/Double2BooleanArrayMap.java index 3b91ce4..3e8d88e 100644 --- a/src/main/java/speiger/src/collections/doubles/maps/impl/misc/Double2BooleanArrayMap.java +++ b/src/main/java/speiger/src/collections/doubles/maps/impl/misc/Double2BooleanArrayMap.java @@ -4,10 +4,12 @@ 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; import java.util.function.DoublePredicate; +import java.util.OptionalDouble; import speiger.src.collections.doubles.collections.DoubleBidirectionalIterator; import speiger.src.collections.doubles.functions.DoubleConsumer; @@ -34,6 +36,7 @@ import speiger.src.collections.booleans.lists.BooleanListIterator; import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.booleans.functions.function.BooleanPredicate; +import speiger.src.collections.booleans.functions.OptionalBoolean; import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOperator; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; @@ -728,6 +731,10 @@ public class Double2BooleanArrayMap extends AbstractDouble2BooleanMap implements } private class MapEntrySet extends AbstractObjectSet implements Double2BooleanOrderedMap.FastOrderedSet { + @Override + public void addFirst(Double2BooleanMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Double2BooleanMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Double2BooleanMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -876,7 +883,7 @@ public class Double2BooleanArrayMap extends AbstractDouble2BooleanMap implements } @Override - public Double2BooleanMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Double2BooleanMap.Entry state = null; boolean empty = true; @@ -888,19 +895,19 @@ public class Double2BooleanArrayMap extends AbstractDouble2BooleanMap implements } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Double2BooleanMap.Entry 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 implements Double2ByteOrderedMap.FastOrderedSet { + @Override + public void addFirst(Double2ByteMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Double2ByteMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Double2ByteMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -899,7 +906,7 @@ public class Double2ByteArrayMap extends AbstractDouble2ByteMap implements Doubl } @Override - public Double2ByteMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Double2ByteMap.Entry state = null; boolean empty = true; @@ -911,19 +918,19 @@ public class Double2ByteArrayMap extends AbstractDouble2ByteMap implements Doubl } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Double2ByteMap.Entry 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 implements Double2CharOrderedMap.FastOrderedSet { + @Override + public void addFirst(Double2CharMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Double2CharMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Double2CharMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -899,7 +906,7 @@ public class Double2CharArrayMap extends AbstractDouble2CharMap implements Doubl } @Override - public Double2CharMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Double2CharMap.Entry state = null; boolean empty = true; @@ -911,19 +918,19 @@ public class Double2CharArrayMap extends AbstractDouble2CharMap implements Doubl } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Double2CharMap.Entry 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 implements Double2DoubleOrderedMap.FastOrderedSet { + @Override + public void addFirst(Double2DoubleMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Double2DoubleMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Double2DoubleMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -892,7 +898,7 @@ public class Double2DoubleArrayMap extends AbstractDouble2DoubleMap implements D } @Override - public Double2DoubleMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Double2DoubleMap.Entry state = null; boolean empty = true; @@ -904,19 +910,19 @@ public class Double2DoubleArrayMap extends AbstractDouble2DoubleMap implements D } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Double2DoubleMap.Entry 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 implements Double2FloatOrderedMap.FastOrderedSet { + @Override + public void addFirst(Double2FloatMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Double2FloatMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Double2FloatMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -899,7 +906,7 @@ public class Double2FloatArrayMap extends AbstractDouble2FloatMap implements Dou } @Override - public Double2FloatMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Double2FloatMap.Entry state = null; boolean empty = true; @@ -911,19 +918,19 @@ public class Double2FloatArrayMap extends AbstractDouble2FloatMap implements Dou } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Double2FloatMap.Entry 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 implements Double2IntOrderedMap.FastOrderedSet { + @Override + public void addFirst(Double2IntMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Double2IntMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Double2IntMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -899,7 +906,7 @@ public class Double2IntArrayMap extends AbstractDouble2IntMap implements Double2 } @Override - public Double2IntMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Double2IntMap.Entry state = null; boolean empty = true; @@ -911,19 +918,19 @@ public class Double2IntArrayMap extends AbstractDouble2IntMap implements Double2 } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Double2IntMap.Entry 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 implements Double2LongOrderedMap.FastOrderedSet { + @Override + public void addFirst(Double2LongMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Double2LongMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Double2LongMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -899,7 +906,7 @@ public class Double2LongArrayMap extends AbstractDouble2LongMap implements Doubl } @Override - public Double2LongMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Double2LongMap.Entry state = null; boolean empty = true; @@ -911,19 +918,19 @@ public class Double2LongArrayMap extends AbstractDouble2LongMap implements Doubl } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Double2LongMap.Entry 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 extends AbstractDouble2ObjectMap implem } private class MapEntrySet extends AbstractObjectSet> implements Double2ObjectOrderedMap.FastOrderedSet { + @Override + public void addFirst(Double2ObjectMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Double2ObjectMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Double2ObjectMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -812,7 +818,7 @@ public class Double2ObjectArrayMap extends AbstractDouble2ObjectMap implem } @Override - public Double2ObjectMap.Entry reduce(ObjectObjectUnaryOperator, Double2ObjectMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Double2ObjectMap.Entry> operator) { Objects.requireNonNull(operator); Double2ObjectMap.Entry state = null; boolean empty = true; @@ -824,19 +830,19 @@ public class Double2ObjectArrayMap extends AbstractDouble2ObjectMap implem } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Double2ObjectMap.Entry 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 extends AbstractDouble2ObjectMap implem @Override public boolean add(double o) { throw new UnsupportedOperationException(); } @Override + public void addFirst(double o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(double o) { throw new UnsupportedOperationException(); } + @Override public boolean addAndMoveToFirst(double o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToLast(double o) { throw new UnsupportedOperationException(); } @@ -995,7 +1005,7 @@ public class Double2ObjectArrayMap extends AbstractDouble2ObjectMap implem } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -1007,16 +1017,16 @@ public class Double2ObjectArrayMap extends AbstractDouble2ObjectMap implem } state = operator.applyAsDouble(state, keys[i]); } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); for(int i = 0;i extends AbstractDouble2ObjectMap implem } @Override - public V reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); V state = null; boolean empty = true; @@ -1129,16 +1139,16 @@ public class Double2ObjectArrayMap extends AbstractDouble2ObjectMap implem } state = operator.apply(state, values[i]); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public V findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); for(int i = 0;i implements Double2ShortOrderedMap.FastOrderedSet { + @Override + public void addFirst(Double2ShortMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Double2ShortMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Double2ShortMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -899,7 +906,7 @@ public class Double2ShortArrayMap extends AbstractDouble2ShortMap implements Dou } @Override - public Double2ShortMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Double2ShortMap.Entry state = null; boolean empty = true; @@ -911,19 +918,19 @@ public class Double2ShortArrayMap extends AbstractDouble2ShortMap implements Dou } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Double2ShortMap.Entry 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); Double2BooleanMap.Entry state = null; boolean empty = true; @@ -1724,19 +1727,19 @@ public class Double2BooleanAVLTreeMap extends AbstractDouble2BooleanMap implemen } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Double2BooleanMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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 @@ -1831,7 +1834,7 @@ public class Double2BooleanAVLTreeMap extends AbstractDouble2BooleanMap implemen } @Override - public boolean reduce(BooleanBooleanUnaryOperator operator) { + public OptionalBoolean reduce(BooleanBooleanUnaryOperator operator) { Objects.requireNonNull(operator); boolean state = false; boolean empty = true; @@ -1843,15 +1846,15 @@ public class Double2BooleanAVLTreeMap extends AbstractDouble2BooleanMap implemen } state = operator.applyAsBoolean(state, entry.value); } - return state; + return empty ? OptionalBoolean.empty() : OptionalBoolean.of(state); } @Override - public boolean findFirst(BooleanPredicate filter) { + public OptionalBoolean findFirst(BooleanPredicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return false; + if(filter.test(entry.value)) return OptionalBoolean.of(entry.value); + return OptionalBoolean.empty(); } @Override @@ -2167,7 +2170,7 @@ public class Double2BooleanAVLTreeMap extends AbstractDouble2BooleanMap implemen } @Override - public boolean reduce(BooleanBooleanUnaryOperator operator) { + public OptionalBoolean reduce(BooleanBooleanUnaryOperator operator) { Objects.requireNonNull(operator); boolean state = false; boolean empty = true; @@ -2179,15 +2182,15 @@ public class Double2BooleanAVLTreeMap extends AbstractDouble2BooleanMap implemen } state = operator.applyAsBoolean(state, entry.value); } - return state; + return empty ? OptionalBoolean.empty() : OptionalBoolean.of(state); } @Override - public boolean findFirst(BooleanPredicate filter) { + public OptionalBoolean findFirst(BooleanPredicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return false; + if(filter.test(entry.value)) return OptionalBoolean.of(entry.value); + return OptionalBoolean.empty(); } @Override @@ -2328,7 +2331,7 @@ public class Double2BooleanAVLTreeMap extends AbstractDouble2BooleanMap implemen } @Override - public Double2BooleanMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Double2BooleanMap.Entry state = null; boolean empty = true; @@ -2340,19 +2343,19 @@ public class Double2BooleanAVLTreeMap extends AbstractDouble2BooleanMap implemen } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Double2BooleanMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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/main/java/speiger/src/collections/doubles/maps/impl/tree/Double2BooleanRBTreeMap.java b/src/main/java/speiger/src/collections/doubles/maps/impl/tree/Double2BooleanRBTreeMap.java index 4142677..2bbd0c2 100644 --- a/src/main/java/speiger/src/collections/doubles/maps/impl/tree/Double2BooleanRBTreeMap.java +++ b/src/main/java/speiger/src/collections/doubles/maps/impl/tree/Double2BooleanRBTreeMap.java @@ -3,11 +3,13 @@ package speiger.src.collections.doubles.maps.impl.tree; import java.util.Collections; import java.util.Map; 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; import java.util.function.DoublePredicate; +import java.util.OptionalDouble; import speiger.src.collections.doubles.collections.DoubleBidirectionalIterator; import speiger.src.collections.doubles.functions.DoubleComparator; @@ -35,6 +37,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectBooleanConsumer; import speiger.src.collections.booleans.functions.function.BooleanPredicate; +import speiger.src.collections.booleans.functions.OptionalBoolean; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOperator; @@ -1123,7 +1126,7 @@ public class Double2BooleanRBTreeMap extends AbstractDouble2BooleanMap implement } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -1135,15 +1138,15 @@ public class Double2BooleanRBTreeMap extends AbstractDouble2BooleanMap implement } state = operator.applyAsDouble(state, entry.key); } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); for(Node entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.test(entry.key)) return entry.key; - return 0D; + if(filter.test(entry.key)) return OptionalDouble.of(entry.key); + return OptionalDouble.empty(); } @Override @@ -1775,7 +1778,7 @@ public class Double2BooleanRBTreeMap extends AbstractDouble2BooleanMap implement } @Override - public Double2BooleanMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Double2BooleanMap.Entry state = null; boolean empty = true; @@ -1787,19 +1790,19 @@ public class Double2BooleanRBTreeMap extends AbstractDouble2BooleanMap implement } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Double2BooleanMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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 @@ -1894,7 +1897,7 @@ public class Double2BooleanRBTreeMap extends AbstractDouble2BooleanMap implement } @Override - public boolean reduce(BooleanBooleanUnaryOperator operator) { + public OptionalBoolean reduce(BooleanBooleanUnaryOperator operator) { Objects.requireNonNull(operator); boolean state = false; boolean empty = true; @@ -1906,15 +1909,15 @@ public class Double2BooleanRBTreeMap extends AbstractDouble2BooleanMap implement } state = operator.applyAsBoolean(state, entry.value); } - return state; + return empty ? OptionalBoolean.empty() : OptionalBoolean.of(state); } @Override - public boolean findFirst(BooleanPredicate filter) { + public OptionalBoolean findFirst(BooleanPredicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return false; + if(filter.test(entry.value)) return OptionalBoolean.of(entry.value); + return OptionalBoolean.empty(); } @Override @@ -2230,7 +2233,7 @@ public class Double2BooleanRBTreeMap extends AbstractDouble2BooleanMap implement } @Override - public boolean reduce(BooleanBooleanUnaryOperator operator) { + public OptionalBoolean reduce(BooleanBooleanUnaryOperator operator) { Objects.requireNonNull(operator); boolean state = false; boolean empty = true; @@ -2242,15 +2245,15 @@ public class Double2BooleanRBTreeMap extends AbstractDouble2BooleanMap implement } state = operator.applyAsBoolean(state, entry.value); } - return state; + return empty ? OptionalBoolean.empty() : OptionalBoolean.of(state); } @Override - public boolean findFirst(BooleanPredicate filter) { + public OptionalBoolean findFirst(BooleanPredicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return false; + if(filter.test(entry.value)) return OptionalBoolean.of(entry.value); + return OptionalBoolean.empty(); } @Override @@ -2391,7 +2394,7 @@ public class Double2BooleanRBTreeMap extends AbstractDouble2BooleanMap implement } @Override - public Double2BooleanMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Double2BooleanMap.Entry state = null; boolean empty = true; @@ -2403,19 +2406,19 @@ public class Double2BooleanRBTreeMap extends AbstractDouble2BooleanMap implement } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Double2BooleanMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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/main/java/speiger/src/collections/doubles/maps/impl/tree/Double2ByteAVLTreeMap.java b/src/main/java/speiger/src/collections/doubles/maps/impl/tree/Double2ByteAVLTreeMap.java index bd4f3e8..bb1e147 100644 --- a/src/main/java/speiger/src/collections/doubles/maps/impl/tree/Double2ByteAVLTreeMap.java +++ b/src/main/java/speiger/src/collections/doubles/maps/impl/tree/Double2ByteAVLTreeMap.java @@ -3,11 +3,13 @@ package speiger.src.collections.doubles.maps.impl.tree; import java.util.Collections; import java.util.Map; 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; import java.util.function.DoublePredicate; +import java.util.OptionalDouble; import speiger.src.collections.doubles.collections.DoubleBidirectionalIterator; @@ -37,6 +39,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectByteConsumer; import speiger.src.collections.bytes.functions.function.BytePredicate; +import speiger.src.collections.bytes.functions.OptionalByte; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOperator; @@ -1129,7 +1132,7 @@ public class Double2ByteAVLTreeMap extends AbstractDouble2ByteMap implements Dou } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -1141,15 +1144,15 @@ public class Double2ByteAVLTreeMap extends AbstractDouble2ByteMap implements Dou } state = operator.applyAsDouble(state, entry.key); } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); for(Node entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.test(entry.key)) return entry.key; - return 0D; + if(filter.test(entry.key)) return OptionalDouble.of(entry.key); + return OptionalDouble.empty(); } @Override @@ -1783,7 +1786,7 @@ public class Double2ByteAVLTreeMap extends AbstractDouble2ByteMap implements Dou } @Override - public Double2ByteMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Double2ByteMap.Entry state = null; boolean empty = true; @@ -1795,19 +1798,19 @@ public class Double2ByteAVLTreeMap extends AbstractDouble2ByteMap implements Dou } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Double2ByteMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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 @@ -1902,7 +1905,7 @@ public class Double2ByteAVLTreeMap extends AbstractDouble2ByteMap implements Dou } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -1914,15 +1917,15 @@ public class Double2ByteAVLTreeMap extends AbstractDouble2ByteMap implements Dou } state = operator.applyAsByte(state, entry.value); } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return (byte)0; + if(filter.test(entry.value)) return OptionalByte.of(entry.value); + return OptionalByte.empty(); } @Override @@ -2238,7 +2241,7 @@ public class Double2ByteAVLTreeMap extends AbstractDouble2ByteMap implements Dou } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -2250,15 +2253,15 @@ public class Double2ByteAVLTreeMap extends AbstractDouble2ByteMap implements Dou } state = operator.applyAsByte(state, entry.value); } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return (byte)0; + if(filter.test(entry.value)) return OptionalByte.of(entry.value); + return OptionalByte.empty(); } @Override @@ -2399,7 +2402,7 @@ public class Double2ByteAVLTreeMap extends AbstractDouble2ByteMap implements Dou } @Override - public Double2ByteMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Double2ByteMap.Entry state = null; boolean empty = true; @@ -2411,19 +2414,19 @@ public class Double2ByteAVLTreeMap extends AbstractDouble2ByteMap implements Dou } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Double2ByteMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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/main/java/speiger/src/collections/doubles/maps/impl/tree/Double2ByteRBTreeMap.java b/src/main/java/speiger/src/collections/doubles/maps/impl/tree/Double2ByteRBTreeMap.java index 9af6023..1721b9c 100644 --- a/src/main/java/speiger/src/collections/doubles/maps/impl/tree/Double2ByteRBTreeMap.java +++ b/src/main/java/speiger/src/collections/doubles/maps/impl/tree/Double2ByteRBTreeMap.java @@ -3,11 +3,13 @@ package speiger.src.collections.doubles.maps.impl.tree; import java.util.Collections; import java.util.Map; 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; import java.util.function.DoublePredicate; +import java.util.OptionalDouble; import speiger.src.collections.doubles.collections.DoubleBidirectionalIterator; import speiger.src.collections.doubles.functions.DoubleComparator; @@ -36,6 +38,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectByteConsumer; import speiger.src.collections.bytes.functions.function.BytePredicate; +import speiger.src.collections.bytes.functions.OptionalByte; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOperator; @@ -1182,7 +1185,7 @@ public class Double2ByteRBTreeMap extends AbstractDouble2ByteMap implements Doub } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -1194,15 +1197,15 @@ public class Double2ByteRBTreeMap extends AbstractDouble2ByteMap implements Doub } state = operator.applyAsDouble(state, entry.key); } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); for(Node entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.test(entry.key)) return entry.key; - return 0D; + if(filter.test(entry.key)) return OptionalDouble.of(entry.key); + return OptionalDouble.empty(); } @Override @@ -1846,7 +1849,7 @@ public class Double2ByteRBTreeMap extends AbstractDouble2ByteMap implements Doub } @Override - public Double2ByteMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Double2ByteMap.Entry state = null; boolean empty = true; @@ -1858,19 +1861,19 @@ public class Double2ByteRBTreeMap extends AbstractDouble2ByteMap implements Doub } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Double2ByteMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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 @@ -1965,7 +1968,7 @@ public class Double2ByteRBTreeMap extends AbstractDouble2ByteMap implements Doub } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -1977,15 +1980,15 @@ public class Double2ByteRBTreeMap extends AbstractDouble2ByteMap implements Doub } state = operator.applyAsByte(state, entry.value); } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return (byte)0; + if(filter.test(entry.value)) return OptionalByte.of(entry.value); + return OptionalByte.empty(); } @Override @@ -2301,7 +2304,7 @@ public class Double2ByteRBTreeMap extends AbstractDouble2ByteMap implements Doub } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -2313,15 +2316,15 @@ public class Double2ByteRBTreeMap extends AbstractDouble2ByteMap implements Doub } state = operator.applyAsByte(state, entry.value); } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return (byte)0; + if(filter.test(entry.value)) return OptionalByte.of(entry.value); + return OptionalByte.empty(); } @Override @@ -2462,7 +2465,7 @@ public class Double2ByteRBTreeMap extends AbstractDouble2ByteMap implements Doub } @Override - public Double2ByteMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Double2ByteMap.Entry state = null; boolean empty = true; @@ -2474,19 +2477,19 @@ public class Double2ByteRBTreeMap extends AbstractDouble2ByteMap implements Doub } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Double2ByteMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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/main/java/speiger/src/collections/doubles/maps/impl/tree/Double2CharAVLTreeMap.java b/src/main/java/speiger/src/collections/doubles/maps/impl/tree/Double2CharAVLTreeMap.java index af5a99c..493a4e0 100644 --- a/src/main/java/speiger/src/collections/doubles/maps/impl/tree/Double2CharAVLTreeMap.java +++ b/src/main/java/speiger/src/collections/doubles/maps/impl/tree/Double2CharAVLTreeMap.java @@ -3,11 +3,13 @@ package speiger.src.collections.doubles.maps.impl.tree; import java.util.Collections; import java.util.Map; 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; import java.util.function.DoublePredicate; +import java.util.OptionalDouble; import speiger.src.collections.doubles.collections.DoubleBidirectionalIterator; @@ -37,6 +39,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectCharConsumer; import speiger.src.collections.chars.functions.function.CharPredicate; +import speiger.src.collections.chars.functions.OptionalChar; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOperator; @@ -1129,7 +1132,7 @@ public class Double2CharAVLTreeMap extends AbstractDouble2CharMap implements Dou } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -1141,15 +1144,15 @@ public class Double2CharAVLTreeMap extends AbstractDouble2CharMap implements Dou } state = operator.applyAsDouble(state, entry.key); } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); for(Node entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.test(entry.key)) return entry.key; - return 0D; + if(filter.test(entry.key)) return OptionalDouble.of(entry.key); + return OptionalDouble.empty(); } @Override @@ -1783,7 +1786,7 @@ public class Double2CharAVLTreeMap extends AbstractDouble2CharMap implements Dou } @Override - public Double2CharMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Double2CharMap.Entry state = null; boolean empty = true; @@ -1795,19 +1798,19 @@ public class Double2CharAVLTreeMap extends AbstractDouble2CharMap implements Dou } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Double2CharMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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 @@ -1902,7 +1905,7 @@ public class Double2CharAVLTreeMap extends AbstractDouble2CharMap implements Dou } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -1914,15 +1917,15 @@ public class Double2CharAVLTreeMap extends AbstractDouble2CharMap implements Dou } state = operator.applyAsChar(state, entry.value); } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return (char)0; + if(filter.test(entry.value)) return OptionalChar.of(entry.value); + return OptionalChar.empty(); } @Override @@ -2238,7 +2241,7 @@ public class Double2CharAVLTreeMap extends AbstractDouble2CharMap implements Dou } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -2250,15 +2253,15 @@ public class Double2CharAVLTreeMap extends AbstractDouble2CharMap implements Dou } state = operator.applyAsChar(state, entry.value); } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return (char)0; + if(filter.test(entry.value)) return OptionalChar.of(entry.value); + return OptionalChar.empty(); } @Override @@ -2399,7 +2402,7 @@ public class Double2CharAVLTreeMap extends AbstractDouble2CharMap implements Dou } @Override - public Double2CharMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Double2CharMap.Entry state = null; boolean empty = true; @@ -2411,19 +2414,19 @@ public class Double2CharAVLTreeMap extends AbstractDouble2CharMap implements Dou } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Double2CharMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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/main/java/speiger/src/collections/doubles/maps/impl/tree/Double2CharRBTreeMap.java b/src/main/java/speiger/src/collections/doubles/maps/impl/tree/Double2CharRBTreeMap.java index 905f2bf..2df5532 100644 --- a/src/main/java/speiger/src/collections/doubles/maps/impl/tree/Double2CharRBTreeMap.java +++ b/src/main/java/speiger/src/collections/doubles/maps/impl/tree/Double2CharRBTreeMap.java @@ -3,11 +3,13 @@ package speiger.src.collections.doubles.maps.impl.tree; import java.util.Collections; import java.util.Map; 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; import java.util.function.DoublePredicate; +import java.util.OptionalDouble; import speiger.src.collections.doubles.collections.DoubleBidirectionalIterator; import speiger.src.collections.doubles.functions.DoubleComparator; @@ -36,6 +38,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectCharConsumer; import speiger.src.collections.chars.functions.function.CharPredicate; +import speiger.src.collections.chars.functions.OptionalChar; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOperator; @@ -1182,7 +1185,7 @@ public class Double2CharRBTreeMap extends AbstractDouble2CharMap implements Doub } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -1194,15 +1197,15 @@ public class Double2CharRBTreeMap extends AbstractDouble2CharMap implements Doub } state = operator.applyAsDouble(state, entry.key); } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); for(Node entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.test(entry.key)) return entry.key; - return 0D; + if(filter.test(entry.key)) return OptionalDouble.of(entry.key); + return OptionalDouble.empty(); } @Override @@ -1846,7 +1849,7 @@ public class Double2CharRBTreeMap extends AbstractDouble2CharMap implements Doub } @Override - public Double2CharMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Double2CharMap.Entry state = null; boolean empty = true; @@ -1858,19 +1861,19 @@ public class Double2CharRBTreeMap extends AbstractDouble2CharMap implements Doub } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Double2CharMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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 @@ -1965,7 +1968,7 @@ public class Double2CharRBTreeMap extends AbstractDouble2CharMap implements Doub } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -1977,15 +1980,15 @@ public class Double2CharRBTreeMap extends AbstractDouble2CharMap implements Doub } state = operator.applyAsChar(state, entry.value); } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return (char)0; + if(filter.test(entry.value)) return OptionalChar.of(entry.value); + return OptionalChar.empty(); } @Override @@ -2301,7 +2304,7 @@ public class Double2CharRBTreeMap extends AbstractDouble2CharMap implements Doub } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -2313,15 +2316,15 @@ public class Double2CharRBTreeMap extends AbstractDouble2CharMap implements Doub } state = operator.applyAsChar(state, entry.value); } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return (char)0; + if(filter.test(entry.value)) return OptionalChar.of(entry.value); + return OptionalChar.empty(); } @Override @@ -2462,7 +2465,7 @@ public class Double2CharRBTreeMap extends AbstractDouble2CharMap implements Doub } @Override - public Double2CharMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Double2CharMap.Entry state = null; boolean empty = true; @@ -2474,19 +2477,19 @@ public class Double2CharRBTreeMap extends AbstractDouble2CharMap implements Doub } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Double2CharMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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/main/java/speiger/src/collections/doubles/maps/impl/tree/Double2DoubleAVLTreeMap.java b/src/main/java/speiger/src/collections/doubles/maps/impl/tree/Double2DoubleAVLTreeMap.java index 2c312fe..4c421fd 100644 --- a/src/main/java/speiger/src/collections/doubles/maps/impl/tree/Double2DoubleAVLTreeMap.java +++ b/src/main/java/speiger/src/collections/doubles/maps/impl/tree/Double2DoubleAVLTreeMap.java @@ -3,11 +3,13 @@ package speiger.src.collections.doubles.maps.impl.tree; import java.util.Collections; import java.util.Map; 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; import java.util.function.DoublePredicate; +import java.util.OptionalDouble; import speiger.src.collections.doubles.collections.DoubleBidirectionalIterator; @@ -1121,7 +1123,7 @@ public class Double2DoubleAVLTreeMap extends AbstractDouble2DoubleMap implements } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -1133,15 +1135,15 @@ public class Double2DoubleAVLTreeMap extends AbstractDouble2DoubleMap implements } state = operator.applyAsDouble(state, entry.key); } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); for(Node entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.test(entry.key)) return entry.key; - return 0D; + if(filter.test(entry.key)) return OptionalDouble.of(entry.key); + return OptionalDouble.empty(); } @Override @@ -1775,7 +1777,7 @@ public class Double2DoubleAVLTreeMap extends AbstractDouble2DoubleMap implements } @Override - public Double2DoubleMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Double2DoubleMap.Entry state = null; boolean empty = true; @@ -1787,19 +1789,19 @@ public class Double2DoubleAVLTreeMap extends AbstractDouble2DoubleMap implements } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Double2DoubleMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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 @@ -1894,7 +1896,7 @@ public class Double2DoubleAVLTreeMap extends AbstractDouble2DoubleMap implements } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -1906,15 +1908,15 @@ public class Double2DoubleAVLTreeMap extends AbstractDouble2DoubleMap implements } state = operator.applyAsDouble(state, entry.value); } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return 0D; + if(filter.test(entry.value)) return OptionalDouble.of(entry.value); + return OptionalDouble.empty(); } @Override @@ -2230,7 +2232,7 @@ public class Double2DoubleAVLTreeMap extends AbstractDouble2DoubleMap implements } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -2242,15 +2244,15 @@ public class Double2DoubleAVLTreeMap extends AbstractDouble2DoubleMap implements } state = operator.applyAsDouble(state, entry.value); } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return 0D; + if(filter.test(entry.value)) return OptionalDouble.of(entry.value); + return OptionalDouble.empty(); } @Override @@ -2391,7 +2393,7 @@ public class Double2DoubleAVLTreeMap extends AbstractDouble2DoubleMap implements } @Override - public Double2DoubleMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Double2DoubleMap.Entry state = null; boolean empty = true; @@ -2403,19 +2405,19 @@ public class Double2DoubleAVLTreeMap extends AbstractDouble2DoubleMap implements } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Double2DoubleMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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/main/java/speiger/src/collections/doubles/maps/impl/tree/Double2DoubleRBTreeMap.java b/src/main/java/speiger/src/collections/doubles/maps/impl/tree/Double2DoubleRBTreeMap.java index 676ce58..eb3a8f4 100644 --- a/src/main/java/speiger/src/collections/doubles/maps/impl/tree/Double2DoubleRBTreeMap.java +++ b/src/main/java/speiger/src/collections/doubles/maps/impl/tree/Double2DoubleRBTreeMap.java @@ -3,11 +3,13 @@ package speiger.src.collections.doubles.maps.impl.tree; import java.util.Collections; import java.util.Map; 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; import java.util.function.DoublePredicate; +import java.util.OptionalDouble; import speiger.src.collections.doubles.collections.DoubleBidirectionalIterator; import speiger.src.collections.doubles.functions.DoubleComparator; @@ -1174,7 +1176,7 @@ public class Double2DoubleRBTreeMap extends AbstractDouble2DoubleMap implements } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -1186,15 +1188,15 @@ public class Double2DoubleRBTreeMap extends AbstractDouble2DoubleMap implements } state = operator.applyAsDouble(state, entry.key); } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); for(Node entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.test(entry.key)) return entry.key; - return 0D; + if(filter.test(entry.key)) return OptionalDouble.of(entry.key); + return OptionalDouble.empty(); } @Override @@ -1838,7 +1840,7 @@ public class Double2DoubleRBTreeMap extends AbstractDouble2DoubleMap implements } @Override - public Double2DoubleMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Double2DoubleMap.Entry state = null; boolean empty = true; @@ -1850,19 +1852,19 @@ public class Double2DoubleRBTreeMap extends AbstractDouble2DoubleMap implements } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Double2DoubleMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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 @@ -1957,7 +1959,7 @@ public class Double2DoubleRBTreeMap extends AbstractDouble2DoubleMap implements } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -1969,15 +1971,15 @@ public class Double2DoubleRBTreeMap extends AbstractDouble2DoubleMap implements } state = operator.applyAsDouble(state, entry.value); } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return 0D; + if(filter.test(entry.value)) return OptionalDouble.of(entry.value); + return OptionalDouble.empty(); } @Override @@ -2293,7 +2295,7 @@ public class Double2DoubleRBTreeMap extends AbstractDouble2DoubleMap implements } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -2305,15 +2307,15 @@ public class Double2DoubleRBTreeMap extends AbstractDouble2DoubleMap implements } state = operator.applyAsDouble(state, entry.value); } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return 0D; + if(filter.test(entry.value)) return OptionalDouble.of(entry.value); + return OptionalDouble.empty(); } @Override @@ -2454,7 +2456,7 @@ public class Double2DoubleRBTreeMap extends AbstractDouble2DoubleMap implements } @Override - public Double2DoubleMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Double2DoubleMap.Entry state = null; boolean empty = true; @@ -2466,19 +2468,19 @@ public class Double2DoubleRBTreeMap extends AbstractDouble2DoubleMap implements } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Double2DoubleMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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/main/java/speiger/src/collections/doubles/maps/impl/tree/Double2FloatAVLTreeMap.java b/src/main/java/speiger/src/collections/doubles/maps/impl/tree/Double2FloatAVLTreeMap.java index 80c41b0..23c78b3 100644 --- a/src/main/java/speiger/src/collections/doubles/maps/impl/tree/Double2FloatAVLTreeMap.java +++ b/src/main/java/speiger/src/collections/doubles/maps/impl/tree/Double2FloatAVLTreeMap.java @@ -3,11 +3,13 @@ package speiger.src.collections.doubles.maps.impl.tree; import java.util.Collections; import java.util.Map; 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; import java.util.function.DoublePredicate; +import java.util.OptionalDouble; import speiger.src.collections.doubles.collections.DoubleBidirectionalIterator; @@ -37,6 +39,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectFloatConsumer; import speiger.src.collections.floats.functions.function.FloatPredicate; +import speiger.src.collections.floats.functions.OptionalFloat; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOperator; @@ -1129,7 +1132,7 @@ public class Double2FloatAVLTreeMap extends AbstractDouble2FloatMap implements D } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -1141,15 +1144,15 @@ public class Double2FloatAVLTreeMap extends AbstractDouble2FloatMap implements D } state = operator.applyAsDouble(state, entry.key); } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); for(Node entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.test(entry.key)) return entry.key; - return 0D; + if(filter.test(entry.key)) return OptionalDouble.of(entry.key); + return OptionalDouble.empty(); } @Override @@ -1783,7 +1786,7 @@ public class Double2FloatAVLTreeMap extends AbstractDouble2FloatMap implements D } @Override - public Double2FloatMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Double2FloatMap.Entry state = null; boolean empty = true; @@ -1795,19 +1798,19 @@ public class Double2FloatAVLTreeMap extends AbstractDouble2FloatMap implements D } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Double2FloatMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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 @@ -1902,7 +1905,7 @@ public class Double2FloatAVLTreeMap extends AbstractDouble2FloatMap implements D } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -1914,15 +1917,15 @@ public class Double2FloatAVLTreeMap extends AbstractDouble2FloatMap implements D } state = operator.applyAsFloat(state, entry.value); } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return 0F; + if(filter.test(entry.value)) return OptionalFloat.of(entry.value); + return OptionalFloat.empty(); } @Override @@ -2238,7 +2241,7 @@ public class Double2FloatAVLTreeMap extends AbstractDouble2FloatMap implements D } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -2250,15 +2253,15 @@ public class Double2FloatAVLTreeMap extends AbstractDouble2FloatMap implements D } state = operator.applyAsFloat(state, entry.value); } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return 0F; + if(filter.test(entry.value)) return OptionalFloat.of(entry.value); + return OptionalFloat.empty(); } @Override @@ -2399,7 +2402,7 @@ public class Double2FloatAVLTreeMap extends AbstractDouble2FloatMap implements D } @Override - public Double2FloatMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Double2FloatMap.Entry state = null; boolean empty = true; @@ -2411,19 +2414,19 @@ public class Double2FloatAVLTreeMap extends AbstractDouble2FloatMap implements D } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Double2FloatMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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/main/java/speiger/src/collections/doubles/maps/impl/tree/Double2FloatRBTreeMap.java b/src/main/java/speiger/src/collections/doubles/maps/impl/tree/Double2FloatRBTreeMap.java index a21dc4f..8047b41 100644 --- a/src/main/java/speiger/src/collections/doubles/maps/impl/tree/Double2FloatRBTreeMap.java +++ b/src/main/java/speiger/src/collections/doubles/maps/impl/tree/Double2FloatRBTreeMap.java @@ -3,11 +3,13 @@ package speiger.src.collections.doubles.maps.impl.tree; import java.util.Collections; import java.util.Map; 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; import java.util.function.DoublePredicate; +import java.util.OptionalDouble; import speiger.src.collections.doubles.collections.DoubleBidirectionalIterator; import speiger.src.collections.doubles.functions.DoubleComparator; @@ -36,6 +38,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectFloatConsumer; import speiger.src.collections.floats.functions.function.FloatPredicate; +import speiger.src.collections.floats.functions.OptionalFloat; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOperator; @@ -1182,7 +1185,7 @@ public class Double2FloatRBTreeMap extends AbstractDouble2FloatMap implements Do } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -1194,15 +1197,15 @@ public class Double2FloatRBTreeMap extends AbstractDouble2FloatMap implements Do } state = operator.applyAsDouble(state, entry.key); } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); for(Node entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.test(entry.key)) return entry.key; - return 0D; + if(filter.test(entry.key)) return OptionalDouble.of(entry.key); + return OptionalDouble.empty(); } @Override @@ -1846,7 +1849,7 @@ public class Double2FloatRBTreeMap extends AbstractDouble2FloatMap implements Do } @Override - public Double2FloatMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Double2FloatMap.Entry state = null; boolean empty = true; @@ -1858,19 +1861,19 @@ public class Double2FloatRBTreeMap extends AbstractDouble2FloatMap implements Do } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Double2FloatMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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 @@ -1965,7 +1968,7 @@ public class Double2FloatRBTreeMap extends AbstractDouble2FloatMap implements Do } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -1977,15 +1980,15 @@ public class Double2FloatRBTreeMap extends AbstractDouble2FloatMap implements Do } state = operator.applyAsFloat(state, entry.value); } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return 0F; + if(filter.test(entry.value)) return OptionalFloat.of(entry.value); + return OptionalFloat.empty(); } @Override @@ -2301,7 +2304,7 @@ public class Double2FloatRBTreeMap extends AbstractDouble2FloatMap implements Do } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -2313,15 +2316,15 @@ public class Double2FloatRBTreeMap extends AbstractDouble2FloatMap implements Do } state = operator.applyAsFloat(state, entry.value); } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return 0F; + if(filter.test(entry.value)) return OptionalFloat.of(entry.value); + return OptionalFloat.empty(); } @Override @@ -2462,7 +2465,7 @@ public class Double2FloatRBTreeMap extends AbstractDouble2FloatMap implements Do } @Override - public Double2FloatMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Double2FloatMap.Entry state = null; boolean empty = true; @@ -2474,19 +2477,19 @@ public class Double2FloatRBTreeMap extends AbstractDouble2FloatMap implements Do } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Double2FloatMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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/main/java/speiger/src/collections/doubles/maps/impl/tree/Double2IntAVLTreeMap.java b/src/main/java/speiger/src/collections/doubles/maps/impl/tree/Double2IntAVLTreeMap.java index 5e34797..8b0a35c 100644 --- a/src/main/java/speiger/src/collections/doubles/maps/impl/tree/Double2IntAVLTreeMap.java +++ b/src/main/java/speiger/src/collections/doubles/maps/impl/tree/Double2IntAVLTreeMap.java @@ -3,12 +3,15 @@ package speiger.src.collections.doubles.maps.impl.tree; import java.util.Collections; import java.util.Map; 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; import java.util.function.DoublePredicate; +import java.util.OptionalDouble; import java.util.function.IntPredicate; +import java.util.OptionalInt; import speiger.src.collections.doubles.collections.DoubleBidirectionalIterator; @@ -1129,7 +1132,7 @@ public class Double2IntAVLTreeMap extends AbstractDouble2IntMap implements Doubl } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -1141,15 +1144,15 @@ public class Double2IntAVLTreeMap extends AbstractDouble2IntMap implements Doubl } state = operator.applyAsDouble(state, entry.key); } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); for(Node entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.test(entry.key)) return entry.key; - return 0D; + if(filter.test(entry.key)) return OptionalDouble.of(entry.key); + return OptionalDouble.empty(); } @Override @@ -1783,7 +1786,7 @@ public class Double2IntAVLTreeMap extends AbstractDouble2IntMap implements Doubl } @Override - public Double2IntMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Double2IntMap.Entry state = null; boolean empty = true; @@ -1795,19 +1798,19 @@ public class Double2IntAVLTreeMap extends AbstractDouble2IntMap implements Doubl } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Double2IntMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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 @@ -1902,7 +1905,7 @@ public class Double2IntAVLTreeMap extends AbstractDouble2IntMap implements Doubl } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -1914,15 +1917,15 @@ public class Double2IntAVLTreeMap extends AbstractDouble2IntMap implements Doubl } state = operator.applyAsInt(state, entry.value); } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return 0; + if(filter.test(entry.value)) return OptionalInt.of(entry.value); + return OptionalInt.empty(); } @Override @@ -2238,7 +2241,7 @@ public class Double2IntAVLTreeMap extends AbstractDouble2IntMap implements Doubl } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -2250,15 +2253,15 @@ public class Double2IntAVLTreeMap extends AbstractDouble2IntMap implements Doubl } state = operator.applyAsInt(state, entry.value); } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return 0; + if(filter.test(entry.value)) return OptionalInt.of(entry.value); + return OptionalInt.empty(); } @Override @@ -2399,7 +2402,7 @@ public class Double2IntAVLTreeMap extends AbstractDouble2IntMap implements Doubl } @Override - public Double2IntMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Double2IntMap.Entry state = null; boolean empty = true; @@ -2411,19 +2414,19 @@ public class Double2IntAVLTreeMap extends AbstractDouble2IntMap implements Doubl } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Double2IntMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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/main/java/speiger/src/collections/doubles/maps/impl/tree/Double2IntRBTreeMap.java b/src/main/java/speiger/src/collections/doubles/maps/impl/tree/Double2IntRBTreeMap.java index 6897e52..4c49677 100644 --- a/src/main/java/speiger/src/collections/doubles/maps/impl/tree/Double2IntRBTreeMap.java +++ b/src/main/java/speiger/src/collections/doubles/maps/impl/tree/Double2IntRBTreeMap.java @@ -3,12 +3,15 @@ package speiger.src.collections.doubles.maps.impl.tree; import java.util.Collections; import java.util.Map; 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; import java.util.function.DoublePredicate; +import java.util.OptionalDouble; import java.util.function.IntPredicate; +import java.util.OptionalInt; import speiger.src.collections.doubles.collections.DoubleBidirectionalIterator; import speiger.src.collections.doubles.functions.DoubleComparator; @@ -1182,7 +1185,7 @@ public class Double2IntRBTreeMap extends AbstractDouble2IntMap implements Double } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -1194,15 +1197,15 @@ public class Double2IntRBTreeMap extends AbstractDouble2IntMap implements Double } state = operator.applyAsDouble(state, entry.key); } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); for(Node entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.test(entry.key)) return entry.key; - return 0D; + if(filter.test(entry.key)) return OptionalDouble.of(entry.key); + return OptionalDouble.empty(); } @Override @@ -1846,7 +1849,7 @@ public class Double2IntRBTreeMap extends AbstractDouble2IntMap implements Double } @Override - public Double2IntMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Double2IntMap.Entry state = null; boolean empty = true; @@ -1858,19 +1861,19 @@ public class Double2IntRBTreeMap extends AbstractDouble2IntMap implements Double } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Double2IntMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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 @@ -1965,7 +1968,7 @@ public class Double2IntRBTreeMap extends AbstractDouble2IntMap implements Double } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -1977,15 +1980,15 @@ public class Double2IntRBTreeMap extends AbstractDouble2IntMap implements Double } state = operator.applyAsInt(state, entry.value); } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return 0; + if(filter.test(entry.value)) return OptionalInt.of(entry.value); + return OptionalInt.empty(); } @Override @@ -2301,7 +2304,7 @@ public class Double2IntRBTreeMap extends AbstractDouble2IntMap implements Double } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -2313,15 +2316,15 @@ public class Double2IntRBTreeMap extends AbstractDouble2IntMap implements Double } state = operator.applyAsInt(state, entry.value); } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return 0; + if(filter.test(entry.value)) return OptionalInt.of(entry.value); + return OptionalInt.empty(); } @Override @@ -2462,7 +2465,7 @@ public class Double2IntRBTreeMap extends AbstractDouble2IntMap implements Double } @Override - public Double2IntMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Double2IntMap.Entry state = null; boolean empty = true; @@ -2474,19 +2477,19 @@ public class Double2IntRBTreeMap extends AbstractDouble2IntMap implements Double } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Double2IntMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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/main/java/speiger/src/collections/doubles/maps/impl/tree/Double2LongAVLTreeMap.java b/src/main/java/speiger/src/collections/doubles/maps/impl/tree/Double2LongAVLTreeMap.java index 96e966f..0160603 100644 --- a/src/main/java/speiger/src/collections/doubles/maps/impl/tree/Double2LongAVLTreeMap.java +++ b/src/main/java/speiger/src/collections/doubles/maps/impl/tree/Double2LongAVLTreeMap.java @@ -3,12 +3,15 @@ package speiger.src.collections.doubles.maps.impl.tree; import java.util.Collections; import java.util.Map; 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; import java.util.function.DoublePredicate; +import java.util.OptionalDouble; import java.util.function.LongPredicate; +import java.util.OptionalLong; import speiger.src.collections.doubles.collections.DoubleBidirectionalIterator; @@ -1129,7 +1132,7 @@ public class Double2LongAVLTreeMap extends AbstractDouble2LongMap implements Dou } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -1141,15 +1144,15 @@ public class Double2LongAVLTreeMap extends AbstractDouble2LongMap implements Dou } state = operator.applyAsDouble(state, entry.key); } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); for(Node entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.test(entry.key)) return entry.key; - return 0D; + if(filter.test(entry.key)) return OptionalDouble.of(entry.key); + return OptionalDouble.empty(); } @Override @@ -1783,7 +1786,7 @@ public class Double2LongAVLTreeMap extends AbstractDouble2LongMap implements Dou } @Override - public Double2LongMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Double2LongMap.Entry state = null; boolean empty = true; @@ -1795,19 +1798,19 @@ public class Double2LongAVLTreeMap extends AbstractDouble2LongMap implements Dou } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Double2LongMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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 @@ -1902,7 +1905,7 @@ public class Double2LongAVLTreeMap extends AbstractDouble2LongMap implements Dou } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -1914,15 +1917,15 @@ public class Double2LongAVLTreeMap extends AbstractDouble2LongMap implements Dou } state = operator.applyAsLong(state, entry.value); } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return 0L; + if(filter.test(entry.value)) return OptionalLong.of(entry.value); + return OptionalLong.empty(); } @Override @@ -2238,7 +2241,7 @@ public class Double2LongAVLTreeMap extends AbstractDouble2LongMap implements Dou } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -2250,15 +2253,15 @@ public class Double2LongAVLTreeMap extends AbstractDouble2LongMap implements Dou } state = operator.applyAsLong(state, entry.value); } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return 0L; + if(filter.test(entry.value)) return OptionalLong.of(entry.value); + return OptionalLong.empty(); } @Override @@ -2399,7 +2402,7 @@ public class Double2LongAVLTreeMap extends AbstractDouble2LongMap implements Dou } @Override - public Double2LongMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Double2LongMap.Entry state = null; boolean empty = true; @@ -2411,19 +2414,19 @@ public class Double2LongAVLTreeMap extends AbstractDouble2LongMap implements Dou } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Double2LongMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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/main/java/speiger/src/collections/doubles/maps/impl/tree/Double2LongRBTreeMap.java b/src/main/java/speiger/src/collections/doubles/maps/impl/tree/Double2LongRBTreeMap.java index 3ee5cd0..bd6023c 100644 --- a/src/main/java/speiger/src/collections/doubles/maps/impl/tree/Double2LongRBTreeMap.java +++ b/src/main/java/speiger/src/collections/doubles/maps/impl/tree/Double2LongRBTreeMap.java @@ -3,12 +3,15 @@ package speiger.src.collections.doubles.maps.impl.tree; import java.util.Collections; import java.util.Map; 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; import java.util.function.DoublePredicate; +import java.util.OptionalDouble; import java.util.function.LongPredicate; +import java.util.OptionalLong; import speiger.src.collections.doubles.collections.DoubleBidirectionalIterator; import speiger.src.collections.doubles.functions.DoubleComparator; @@ -1182,7 +1185,7 @@ public class Double2LongRBTreeMap extends AbstractDouble2LongMap implements Doub } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -1194,15 +1197,15 @@ public class Double2LongRBTreeMap extends AbstractDouble2LongMap implements Doub } state = operator.applyAsDouble(state, entry.key); } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); for(Node entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.test(entry.key)) return entry.key; - return 0D; + if(filter.test(entry.key)) return OptionalDouble.of(entry.key); + return OptionalDouble.empty(); } @Override @@ -1846,7 +1849,7 @@ public class Double2LongRBTreeMap extends AbstractDouble2LongMap implements Doub } @Override - public Double2LongMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Double2LongMap.Entry state = null; boolean empty = true; @@ -1858,19 +1861,19 @@ public class Double2LongRBTreeMap extends AbstractDouble2LongMap implements Doub } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Double2LongMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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 @@ -1965,7 +1968,7 @@ public class Double2LongRBTreeMap extends AbstractDouble2LongMap implements Doub } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -1977,15 +1980,15 @@ public class Double2LongRBTreeMap extends AbstractDouble2LongMap implements Doub } state = operator.applyAsLong(state, entry.value); } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return 0L; + if(filter.test(entry.value)) return OptionalLong.of(entry.value); + return OptionalLong.empty(); } @Override @@ -2301,7 +2304,7 @@ public class Double2LongRBTreeMap extends AbstractDouble2LongMap implements Doub } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -2313,15 +2316,15 @@ public class Double2LongRBTreeMap extends AbstractDouble2LongMap implements Doub } state = operator.applyAsLong(state, entry.value); } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return 0L; + if(filter.test(entry.value)) return OptionalLong.of(entry.value); + return OptionalLong.empty(); } @Override @@ -2462,7 +2465,7 @@ public class Double2LongRBTreeMap extends AbstractDouble2LongMap implements Doub } @Override - public Double2LongMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Double2LongMap.Entry state = null; boolean empty = true; @@ -2474,19 +2477,19 @@ public class Double2LongRBTreeMap extends AbstractDouble2LongMap implements Doub } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Double2LongMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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/main/java/speiger/src/collections/doubles/maps/impl/tree/Double2ObjectAVLTreeMap.java b/src/main/java/speiger/src/collections/doubles/maps/impl/tree/Double2ObjectAVLTreeMap.java index ec5151d..0ad716f 100644 --- a/src/main/java/speiger/src/collections/doubles/maps/impl/tree/Double2ObjectAVLTreeMap.java +++ b/src/main/java/speiger/src/collections/doubles/maps/impl/tree/Double2ObjectAVLTreeMap.java @@ -3,11 +3,13 @@ package speiger.src.collections.doubles.maps.impl.tree; import java.util.Collections; import java.util.Map; 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; import java.util.function.DoublePredicate; +import java.util.OptionalDouble; import speiger.src.collections.doubles.collections.DoubleBidirectionalIterator; @@ -1017,7 +1019,7 @@ public class Double2ObjectAVLTreeMap extends AbstractDouble2ObjectMap impl } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -1029,15 +1031,15 @@ public class Double2ObjectAVLTreeMap extends AbstractDouble2ObjectMap impl } state = operator.applyAsDouble(state, entry.key); } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); for(Node entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.test(entry.key)) return entry.key; - return 0D; + if(filter.test(entry.key)) return OptionalDouble.of(entry.key); + return OptionalDouble.empty(); } @Override @@ -1664,7 +1666,7 @@ public class Double2ObjectAVLTreeMap extends AbstractDouble2ObjectMap impl } @Override - public Double2ObjectMap.Entry reduce(ObjectObjectUnaryOperator, Double2ObjectMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Double2ObjectMap.Entry> operator) { Objects.requireNonNull(operator); Double2ObjectMap.Entry state = null; boolean empty = true; @@ -1676,19 +1678,19 @@ public class Double2ObjectAVLTreeMap extends AbstractDouble2ObjectMap impl } state = operator.apply(state, new BasicEntry<>(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Double2ObjectMap.Entry findFirst(Predicate> filter) { + public Optional> findFirst(Predicate> filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry<>(); for(Node 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 @@ -1783,7 +1785,7 @@ public class Double2ObjectAVLTreeMap extends AbstractDouble2ObjectMap impl } @Override - public V reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); V state = null; boolean empty = true; @@ -1795,15 +1797,15 @@ public class Double2ObjectAVLTreeMap extends AbstractDouble2ObjectMap impl } state = operator.apply(state, entry.value); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public V findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return null; + if(filter.test(entry.value)) return Optional.ofNullable(entry.value); + return Optional.empty(); } @Override @@ -2119,7 +2121,7 @@ public class Double2ObjectAVLTreeMap extends AbstractDouble2ObjectMap impl } @Override - public V reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); V state = null; boolean empty = true; @@ -2131,15 +2133,15 @@ public class Double2ObjectAVLTreeMap extends AbstractDouble2ObjectMap impl } state = operator.apply(state, entry.value); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public V findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return null; + if(filter.test(entry.value)) return Optional.ofNullable(entry.value); + return Optional.empty(); } @Override @@ -2280,7 +2282,7 @@ public class Double2ObjectAVLTreeMap extends AbstractDouble2ObjectMap impl } @Override - public Double2ObjectMap.Entry reduce(ObjectObjectUnaryOperator, Double2ObjectMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Double2ObjectMap.Entry> operator) { Objects.requireNonNull(operator); Double2ObjectMap.Entry state = null; boolean empty = true; @@ -2292,19 +2294,19 @@ public class Double2ObjectAVLTreeMap extends AbstractDouble2ObjectMap impl } state = operator.apply(state, new BasicEntry<>(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Double2ObjectMap.Entry findFirst(Predicate> filter) { + public Optional> findFirst(Predicate> filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry<>(); for(Node 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/main/java/speiger/src/collections/doubles/maps/impl/tree/Double2ObjectRBTreeMap.java b/src/main/java/speiger/src/collections/doubles/maps/impl/tree/Double2ObjectRBTreeMap.java index bd1b93b..b202ed2 100644 --- a/src/main/java/speiger/src/collections/doubles/maps/impl/tree/Double2ObjectRBTreeMap.java +++ b/src/main/java/speiger/src/collections/doubles/maps/impl/tree/Double2ObjectRBTreeMap.java @@ -3,11 +3,13 @@ package speiger.src.collections.doubles.maps.impl.tree; import java.util.Collections; import java.util.Map; 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; import java.util.function.DoublePredicate; +import java.util.OptionalDouble; import speiger.src.collections.doubles.collections.DoubleBidirectionalIterator; import speiger.src.collections.doubles.functions.DoubleComparator; @@ -1070,7 +1072,7 @@ public class Double2ObjectRBTreeMap extends AbstractDouble2ObjectMap imple } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -1082,15 +1084,15 @@ public class Double2ObjectRBTreeMap extends AbstractDouble2ObjectMap imple } state = operator.applyAsDouble(state, entry.key); } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); for(Node entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.test(entry.key)) return entry.key; - return 0D; + if(filter.test(entry.key)) return OptionalDouble.of(entry.key); + return OptionalDouble.empty(); } @Override @@ -1727,7 +1729,7 @@ public class Double2ObjectRBTreeMap extends AbstractDouble2ObjectMap imple } @Override - public Double2ObjectMap.Entry reduce(ObjectObjectUnaryOperator, Double2ObjectMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Double2ObjectMap.Entry> operator) { Objects.requireNonNull(operator); Double2ObjectMap.Entry state = null; boolean empty = true; @@ -1739,19 +1741,19 @@ public class Double2ObjectRBTreeMap extends AbstractDouble2ObjectMap imple } state = operator.apply(state, new BasicEntry<>(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Double2ObjectMap.Entry findFirst(Predicate> filter) { + public Optional> findFirst(Predicate> filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry<>(); for(Node 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 @@ -1846,7 +1848,7 @@ public class Double2ObjectRBTreeMap extends AbstractDouble2ObjectMap imple } @Override - public V reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); V state = null; boolean empty = true; @@ -1858,15 +1860,15 @@ public class Double2ObjectRBTreeMap extends AbstractDouble2ObjectMap imple } state = operator.apply(state, entry.value); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public V findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return null; + if(filter.test(entry.value)) return Optional.ofNullable(entry.value); + return Optional.empty(); } @Override @@ -2182,7 +2184,7 @@ public class Double2ObjectRBTreeMap extends AbstractDouble2ObjectMap imple } @Override - public V reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); V state = null; boolean empty = true; @@ -2194,15 +2196,15 @@ public class Double2ObjectRBTreeMap extends AbstractDouble2ObjectMap imple } state = operator.apply(state, entry.value); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public V findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return null; + if(filter.test(entry.value)) return Optional.ofNullable(entry.value); + return Optional.empty(); } @Override @@ -2343,7 +2345,7 @@ public class Double2ObjectRBTreeMap extends AbstractDouble2ObjectMap imple } @Override - public Double2ObjectMap.Entry reduce(ObjectObjectUnaryOperator, Double2ObjectMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Double2ObjectMap.Entry> operator) { Objects.requireNonNull(operator); Double2ObjectMap.Entry state = null; boolean empty = true; @@ -2355,19 +2357,19 @@ public class Double2ObjectRBTreeMap extends AbstractDouble2ObjectMap imple } state = operator.apply(state, new BasicEntry<>(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Double2ObjectMap.Entry findFirst(Predicate> filter) { + public Optional> findFirst(Predicate> filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry<>(); for(Node 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/main/java/speiger/src/collections/doubles/maps/impl/tree/Double2ShortAVLTreeMap.java b/src/main/java/speiger/src/collections/doubles/maps/impl/tree/Double2ShortAVLTreeMap.java index f58e437..e4aecf0 100644 --- a/src/main/java/speiger/src/collections/doubles/maps/impl/tree/Double2ShortAVLTreeMap.java +++ b/src/main/java/speiger/src/collections/doubles/maps/impl/tree/Double2ShortAVLTreeMap.java @@ -3,11 +3,13 @@ package speiger.src.collections.doubles.maps.impl.tree; import java.util.Collections; import java.util.Map; 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; import java.util.function.DoublePredicate; +import java.util.OptionalDouble; import speiger.src.collections.doubles.collections.DoubleBidirectionalIterator; @@ -37,6 +39,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectShortConsumer; import speiger.src.collections.shorts.functions.function.ShortPredicate; +import speiger.src.collections.shorts.functions.OptionalShort; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOperator; @@ -1129,7 +1132,7 @@ public class Double2ShortAVLTreeMap extends AbstractDouble2ShortMap implements D } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -1141,15 +1144,15 @@ public class Double2ShortAVLTreeMap extends AbstractDouble2ShortMap implements D } state = operator.applyAsDouble(state, entry.key); } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); for(Node entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.test(entry.key)) return entry.key; - return 0D; + if(filter.test(entry.key)) return OptionalDouble.of(entry.key); + return OptionalDouble.empty(); } @Override @@ -1783,7 +1786,7 @@ public class Double2ShortAVLTreeMap extends AbstractDouble2ShortMap implements D } @Override - public Double2ShortMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Double2ShortMap.Entry state = null; boolean empty = true; @@ -1795,19 +1798,19 @@ public class Double2ShortAVLTreeMap extends AbstractDouble2ShortMap implements D } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Double2ShortMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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 @@ -1902,7 +1905,7 @@ public class Double2ShortAVLTreeMap extends AbstractDouble2ShortMap implements D } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -1914,15 +1917,15 @@ public class Double2ShortAVLTreeMap extends AbstractDouble2ShortMap implements D } state = operator.applyAsShort(state, entry.value); } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return (short)0; + if(filter.test(entry.value)) return OptionalShort.of(entry.value); + return OptionalShort.empty(); } @Override @@ -2238,7 +2241,7 @@ public class Double2ShortAVLTreeMap extends AbstractDouble2ShortMap implements D } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -2250,15 +2253,15 @@ public class Double2ShortAVLTreeMap extends AbstractDouble2ShortMap implements D } state = operator.applyAsShort(state, entry.value); } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return (short)0; + if(filter.test(entry.value)) return OptionalShort.of(entry.value); + return OptionalShort.empty(); } @Override @@ -2399,7 +2402,7 @@ public class Double2ShortAVLTreeMap extends AbstractDouble2ShortMap implements D } @Override - public Double2ShortMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Double2ShortMap.Entry state = null; boolean empty = true; @@ -2411,19 +2414,19 @@ public class Double2ShortAVLTreeMap extends AbstractDouble2ShortMap implements D } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Double2ShortMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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/main/java/speiger/src/collections/doubles/maps/impl/tree/Double2ShortRBTreeMap.java b/src/main/java/speiger/src/collections/doubles/maps/impl/tree/Double2ShortRBTreeMap.java index 419f430..7189ac7 100644 --- a/src/main/java/speiger/src/collections/doubles/maps/impl/tree/Double2ShortRBTreeMap.java +++ b/src/main/java/speiger/src/collections/doubles/maps/impl/tree/Double2ShortRBTreeMap.java @@ -3,11 +3,13 @@ package speiger.src.collections.doubles.maps.impl.tree; import java.util.Collections; import java.util.Map; 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; import java.util.function.DoublePredicate; +import java.util.OptionalDouble; import speiger.src.collections.doubles.collections.DoubleBidirectionalIterator; import speiger.src.collections.doubles.functions.DoubleComparator; @@ -36,6 +38,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectShortConsumer; import speiger.src.collections.shorts.functions.function.ShortPredicate; +import speiger.src.collections.shorts.functions.OptionalShort; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOperator; @@ -1182,7 +1185,7 @@ public class Double2ShortRBTreeMap extends AbstractDouble2ShortMap implements Do } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -1194,15 +1197,15 @@ public class Double2ShortRBTreeMap extends AbstractDouble2ShortMap implements Do } state = operator.applyAsDouble(state, entry.key); } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); for(Node entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.test(entry.key)) return entry.key; - return 0D; + if(filter.test(entry.key)) return OptionalDouble.of(entry.key); + return OptionalDouble.empty(); } @Override @@ -1846,7 +1849,7 @@ public class Double2ShortRBTreeMap extends AbstractDouble2ShortMap implements Do } @Override - public Double2ShortMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Double2ShortMap.Entry state = null; boolean empty = true; @@ -1858,19 +1861,19 @@ public class Double2ShortRBTreeMap extends AbstractDouble2ShortMap implements Do } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Double2ShortMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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 @@ -1965,7 +1968,7 @@ public class Double2ShortRBTreeMap extends AbstractDouble2ShortMap implements Do } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -1977,15 +1980,15 @@ public class Double2ShortRBTreeMap extends AbstractDouble2ShortMap implements Do } state = operator.applyAsShort(state, entry.value); } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return (short)0; + if(filter.test(entry.value)) return OptionalShort.of(entry.value); + return OptionalShort.empty(); } @Override @@ -2301,7 +2304,7 @@ public class Double2ShortRBTreeMap extends AbstractDouble2ShortMap implements Do } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -2313,15 +2316,15 @@ public class Double2ShortRBTreeMap extends AbstractDouble2ShortMap implements Do } state = operator.applyAsShort(state, entry.value); } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return (short)0; + if(filter.test(entry.value)) return OptionalShort.of(entry.value); + return OptionalShort.empty(); } @Override @@ -2462,7 +2465,7 @@ public class Double2ShortRBTreeMap extends AbstractDouble2ShortMap implements Do } @Override - public Double2ShortMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Double2ShortMap.Entry state = null; boolean empty = true; @@ -2474,19 +2477,19 @@ public class Double2ShortRBTreeMap extends AbstractDouble2ShortMap implements Do } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Double2ShortMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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/main/java/speiger/src/collections/doubles/queues/DoubleArrayFIFOQueue.java b/src/main/java/speiger/src/collections/doubles/queues/DoubleArrayFIFOQueue.java index f9ba1b3..97d82a6 100644 --- a/src/main/java/speiger/src/collections/doubles/queues/DoubleArrayFIFOQueue.java +++ b/src/main/java/speiger/src/collections/doubles/queues/DoubleArrayFIFOQueue.java @@ -2,6 +2,7 @@ package speiger.src.collections.doubles.queues; import java.util.Arrays; import java.util.Objects; +import java.util.OptionalDouble; import java.util.NoSuchElementException; import java.util.function.DoublePredicate; @@ -278,17 +279,17 @@ public class DoubleArrayFIFOQueue extends AbstractDoublePriorityQueue implements } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); for(int i = 0,m=size();i= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor)); + } + + @Override + public void addLast(double o) { + if(strategy.equals(o, 0D)) { + if(containsNull) return; + containsNull = true; + onNodeAdded(nullIndex); + } + else { + int pos = HashUtil.mix(strategy.hashCode(o)) & mask; + while(!strategy.equals(keys[pos], 0D)) { + if(strategy.equals(keys[pos], o)) return; + pos = ++pos & mask; + } + keys[pos] = o; + onNodeAdded(pos); + } + if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor)); + } + @Override public boolean addAndMoveToFirst(double o) { if(strategy.equals(o, 0D)) { @@ -618,7 +659,7 @@ public class DoubleLinkedOpenCustomHashSet extends DoubleOpenCustomHashSet imple } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -631,18 +672,18 @@ public class DoubleLinkedOpenCustomHashSet extends DoubleOpenCustomHashSet imple else state = operator.applyAsDouble(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); int index = firstIndex; while(index != -1) { - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalDouble.of(keys[index]); index = (int)links[index]; } - return 0D; + return OptionalDouble.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/doubles/sets/DoubleLinkedOpenHashSet.java b/src/main/java/speiger/src/collections/doubles/sets/DoubleLinkedOpenHashSet.java index 36c374b..93fa1b1 100644 --- a/src/main/java/speiger/src/collections/doubles/sets/DoubleLinkedOpenHashSet.java +++ b/src/main/java/speiger/src/collections/doubles/sets/DoubleLinkedOpenHashSet.java @@ -5,6 +5,7 @@ import java.util.Collection; import java.util.Iterator; import java.util.NoSuchElementException; import java.util.Objects; +import java.util.OptionalDouble; import java.util.function.DoublePredicate; import speiger.src.collections.doubles.collections.DoubleCollection; @@ -183,6 +184,46 @@ public class DoubleLinkedOpenHashSet extends DoubleOpenHashSet implements Double while(iterator.hasNext()) add(iterator.nextDouble()); } + @Override + public void addFirst(double o) { + if(Double.doubleToLongBits(o) == 0) { + if(containsNull) return; + containsNull = true; + onNodeAdded(nullIndex); + moveToFirstIndex(nullIndex); + } + else { + int pos = HashUtil.mix(Double.hashCode(o)) & mask; + while(Double.doubleToLongBits(keys[pos]) != 0) { + if(Double.doubleToLongBits(keys[pos]) == Double.doubleToLongBits(o)) return; + pos = ++pos & mask; + } + keys[pos] = o; + onNodeAdded(pos); + moveToFirstIndex(pos); + } + if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor)); + } + + @Override + public void addLast(double o) { + if(Double.doubleToLongBits(o) == 0) { + if(containsNull) return; + containsNull = true; + onNodeAdded(nullIndex); + } + else { + int pos = HashUtil.mix(Double.hashCode(o)) & mask; + while(Double.doubleToLongBits(keys[pos]) != 0) { + if(Double.doubleToLongBits(keys[pos]) == Double.doubleToLongBits(o)) return; + pos = ++pos & mask; + } + keys[pos] = o; + onNodeAdded(pos); + } + if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor)); + } + @Override public boolean addAndMoveToFirst(double o) { if(Double.doubleToLongBits(o) == 0) { @@ -471,7 +512,7 @@ public class DoubleLinkedOpenHashSet extends DoubleOpenHashSet implements Double } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -484,18 +525,18 @@ public class DoubleLinkedOpenHashSet extends DoubleOpenHashSet implements Double else state = operator.applyAsDouble(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); int index = firstIndex; while(index != -1) { - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalDouble.of(keys[index]); index = (int)links[index]; } - return 0D; + return OptionalDouble.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/doubles/sets/DoubleOpenCustomHashSet.java b/src/main/java/speiger/src/collections/doubles/sets/DoubleOpenCustomHashSet.java index 7eb7f30..b49fa4a 100644 --- a/src/main/java/speiger/src/collections/doubles/sets/DoubleOpenCustomHashSet.java +++ b/src/main/java/speiger/src/collections/doubles/sets/DoubleOpenCustomHashSet.java @@ -6,6 +6,7 @@ import java.util.ConcurrentModificationException; import java.util.Iterator; import java.util.NoSuchElementException; import java.util.Objects; +import java.util.OptionalDouble; import java.util.function.DoublePredicate; import speiger.src.collections.doubles.collections.DoubleCollection; @@ -546,7 +547,7 @@ public class DoubleOpenCustomHashSet extends AbstractDoubleSet implements ITrimm } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -563,18 +564,18 @@ public class DoubleOpenCustomHashSet extends AbstractDoubleSet implements ITrimm } state = operator.applyAsDouble(state, keys[i]); } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0D; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OptionalDouble.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OptionalDouble.of(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], 0D) && filter.test(keys[i])) return keys[i]; + if(!strategy.equals(keys[i], 0D) && filter.test(keys[i])) return OptionalDouble.of(keys[i]); } - return 0D; + return OptionalDouble.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/doubles/sets/DoubleOpenHashSet.java b/src/main/java/speiger/src/collections/doubles/sets/DoubleOpenHashSet.java index dfa2304..838551f 100644 --- a/src/main/java/speiger/src/collections/doubles/sets/DoubleOpenHashSet.java +++ b/src/main/java/speiger/src/collections/doubles/sets/DoubleOpenHashSet.java @@ -6,6 +6,7 @@ import java.util.ConcurrentModificationException; import java.util.Iterator; import java.util.NoSuchElementException; import java.util.Objects; +import java.util.OptionalDouble; import java.util.function.DoublePredicate; import speiger.src.collections.doubles.collections.DoubleCollection; @@ -428,7 +429,7 @@ public class DoubleOpenHashSet extends AbstractDoubleSet implements ITrimmable } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -445,18 +446,18 @@ public class DoubleOpenHashSet extends AbstractDoubleSet implements ITrimmable } state = operator.applyAsDouble(state, keys[i]); } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0D; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OptionalDouble.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OptionalDouble.of(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(Double.doubleToLongBits(keys[i]) != 0 && filter.test(keys[i])) return keys[i]; + if(Double.doubleToLongBits(keys[i]) != 0 && filter.test(keys[i])) return OptionalDouble.of(keys[i]); } - return 0D; + return OptionalDouble.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/doubles/sets/DoubleOrderedSet.java b/src/main/java/speiger/src/collections/doubles/sets/DoubleOrderedSet.java index 91d7c6b..b69c620 100644 --- a/src/main/java/speiger/src/collections/doubles/sets/DoubleOrderedSet.java +++ b/src/main/java/speiger/src/collections/doubles/sets/DoubleOrderedSet.java @@ -1,6 +1,7 @@ package speiger.src.collections.doubles.sets; import speiger.src.collections.doubles.collections.DoubleBidirectionalIterator; +import speiger.src.collections.doubles.collections.DoubleOrderedCollection; import speiger.src.collections.doubles.collections.DoubleSplititerator; import speiger.src.collections.doubles.utils.DoubleSplititerators; import speiger.src.collections.doubles.sets.AbstractDoubleSet.ReversedDoubleOrderedSet; @@ -13,7 +14,7 @@ import speiger.src.collections.doubles.utils.DoubleSets; * This interface provides basic access to such functions while also providing some Sorted/NaivgableSet implementations that still fit into here. * */ -public interface DoubleOrderedSet extends DoubleSet +public interface DoubleOrderedSet extends DoubleSet, DoubleOrderedCollection { /** * A customized add method that allows you to insert into the first index. diff --git a/src/main/java/speiger/src/collections/doubles/sets/DoubleRBTreeSet.java b/src/main/java/speiger/src/collections/doubles/sets/DoubleRBTreeSet.java index e55201f..4d2f642 100644 --- a/src/main/java/speiger/src/collections/doubles/sets/DoubleRBTreeSet.java +++ b/src/main/java/speiger/src/collections/doubles/sets/DoubleRBTreeSet.java @@ -2,6 +2,7 @@ package speiger.src.collections.doubles.sets; import java.util.Collection; import java.util.Collections; +import java.util.OptionalDouble; import java.util.Iterator; import java.util.NoSuchElementException; import java.util.Objects; @@ -334,7 +335,7 @@ public class DoubleRBTreeSet extends AbstractDoubleSet implements DoubleNavigabl } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -346,16 +347,16 @@ public class DoubleRBTreeSet extends AbstractDoubleSet implements DoubleNavigabl } state = operator.applyAsDouble(state, entry.key); } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); for(Entry entry = first;entry != null;entry = entry.next()) { - if(filter.test(entry.key)) return entry.key; + if(filter.test(entry.key)) return OptionalDouble.of(entry.key); } - return 0D; + return OptionalDouble.empty(); } @Override @@ -1241,7 +1242,7 @@ public class DoubleRBTreeSet extends AbstractDoubleSet implements DoubleNavigabl } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -1253,16 +1254,16 @@ public class DoubleRBTreeSet extends AbstractDoubleSet implements DoubleNavigabl } state = operator.applyAsDouble(state, entry.key); } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); for(Entry entry = start();entry != null && inRange(entry.key);entry = next(entry)) { - if(filter.test(entry.key)) return entry.key; + if(filter.test(entry.key)) return OptionalDouble.of(entry.key); } - return 0D; + return OptionalDouble.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/doubles/sets/ImmutableDoubleOpenHashSet.java b/src/main/java/speiger/src/collections/doubles/sets/ImmutableDoubleOpenHashSet.java index cca2eb8..9f9b005 100644 --- a/src/main/java/speiger/src/collections/doubles/sets/ImmutableDoubleOpenHashSet.java +++ b/src/main/java/speiger/src/collections/doubles/sets/ImmutableDoubleOpenHashSet.java @@ -5,6 +5,7 @@ import java.util.Collection; import java.util.Iterator; import java.util.NoSuchElementException; import java.util.Objects; +import java.util.OptionalDouble; import java.util.function.DoublePredicate; import speiger.src.collections.doubles.collections.DoubleBidirectionalIterator; @@ -236,6 +237,10 @@ public class ImmutableDoubleOpenHashSet extends AbstractDoubleSet implements Dou @Override public boolean addAll(DoubleCollection c) { throw new UnsupportedOperationException(); } @Override + public void addFirst(double o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(double o) { throw new UnsupportedOperationException(); } + @Override public boolean addAndMoveToFirst(double o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToLast(double o) { throw new UnsupportedOperationException(); } @@ -371,7 +376,7 @@ public class ImmutableDoubleOpenHashSet extends AbstractDoubleSet implements Dou } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -384,18 +389,18 @@ public class ImmutableDoubleOpenHashSet extends AbstractDoubleSet implements Dou else state = operator.applyAsDouble(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); int index = firstIndex; while(index != -1) { - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalDouble.of(keys[index]); index = (int)links[index]; } - return 0D; + return OptionalDouble.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/doubles/utils/DoubleCollections.java b/src/main/java/speiger/src/collections/doubles/utils/DoubleCollections.java index b2a5af8..5b1185a 100644 --- a/src/main/java/speiger/src/collections/doubles/utils/DoubleCollections.java +++ b/src/main/java/speiger/src/collections/doubles/utils/DoubleCollections.java @@ -6,6 +6,7 @@ import java.util.ConcurrentModificationException; import java.util.Iterator; import java.util.NoSuchElementException; import java.util.Objects; +import java.util.OptionalDouble; import java.util.function.Predicate; import java.util.function.DoublePredicate; import java.util.function.Consumer; @@ -757,9 +758,9 @@ public class DoubleCollections @Override public double reduce(double identity, DoubleDoubleUnaryOperator operator) { synchronized(mutex) { return c.reduce(identity, operator); } } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { synchronized(mutex) { return c.reduce(operator); } } + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { synchronized(mutex) { return c.reduce(operator); } } @Override - public double findFirst(DoublePredicate filter) { synchronized(mutex) { return c.findFirst(filter); } } + public OptionalDouble findFirst(DoublePredicate filter) { synchronized(mutex) { return c.findFirst(filter); } } @Override public int count(DoublePredicate filter) { synchronized(mutex) { return c.count(filter); } } } @@ -887,9 +888,9 @@ public class DoubleCollections @Override public double reduce(double identity, DoubleDoubleUnaryOperator operator) { return c.reduce(identity, operator); } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { return c.reduce(operator); } + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { return c.reduce(operator); } @Override - public double findFirst(DoublePredicate filter) { return c.findFirst(filter); } + public OptionalDouble findFirst(DoublePredicate filter) { return c.findFirst(filter); } @Override public int count(DoublePredicate filter) { return c.count(filter); } } diff --git a/src/main/java/speiger/src/collections/doubles/utils/DoublePriorityQueues.java b/src/main/java/speiger/src/collections/doubles/utils/DoublePriorityQueues.java index 3bdba4b..c0bf905 100644 --- a/src/main/java/speiger/src/collections/doubles/utils/DoublePriorityQueues.java +++ b/src/main/java/speiger/src/collections/doubles/utils/DoublePriorityQueues.java @@ -1,13 +1,14 @@ package speiger.src.collections.doubles.utils; import java.util.function.DoublePredicate; - +import java.util.OptionalDouble; import speiger.src.collections.doubles.collections.DoubleIterator; import speiger.src.collections.doubles.collections.DoubleCollection; import speiger.src.collections.doubles.functions.DoubleComparator; import speiger.src.collections.doubles.queues.DoublePriorityDequeue; import speiger.src.collections.doubles.queues.DoublePriorityQueue; import speiger.src.collections.doubles.functions.DoubleConsumer; +import speiger.src.collections.doubles.functions.function.DoubleDoubleUnaryOperator; import speiger.src.collections.objects.functions.consumer.ObjectDoubleConsumer; /** @@ -113,7 +114,11 @@ public class DoublePriorityQueues @Override public boolean matchesAll(DoublePredicate filter) { synchronized(mutex) { return queue.matchesAll(filter); } } @Override - public double findFirst(DoublePredicate filter) { synchronized(mutex) { return queue.findFirst(filter); } } + public OptionalDouble findFirst(DoublePredicate filter) { synchronized(mutex) { return queue.findFirst(filter); } } + @Override + public double reduce(double identity, DoubleDoubleUnaryOperator operator) { synchronized(mutex) { return queue.reduce(identity, operator); } } + @Override + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { synchronized(mutex) { return queue.reduce(operator); } } @Override public int count(DoublePredicate filter) { synchronized(mutex) { return queue.count(filter); } } } diff --git a/src/main/java/speiger/src/collections/doubles/utils/DoubleSets.java b/src/main/java/speiger/src/collections/doubles/utils/DoubleSets.java index 23d6949..c67df6a 100644 --- a/src/main/java/speiger/src/collections/doubles/utils/DoubleSets.java +++ b/src/main/java/speiger/src/collections/doubles/utils/DoubleSets.java @@ -301,6 +301,10 @@ public class DoubleSets s = c; } + @Override + public void addFirst(double o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(double o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(double o) { throw new UnsupportedOperationException(); } @Override @@ -573,7 +577,11 @@ public class DoubleSets super(c, mutex); s = c; } - + + @Override + public void addFirst(double o) { synchronized(mutex) { s.addFirst(o); } } + @Override + public void addLast(double o) { synchronized(mutex) { s.addLast(o); } } @Override public boolean addAndMoveToFirst(double o) { synchronized(mutex) { return s.addAndMoveToFirst(o); } } @Override diff --git a/src/main/java/speiger/src/collections/doubles/utils/maps/Double2BooleanMaps.java b/src/main/java/speiger/src/collections/doubles/utils/maps/Double2BooleanMaps.java index 39ee34f..904d530 100644 --- a/src/main/java/speiger/src/collections/doubles/utils/maps/Double2BooleanMaps.java +++ b/src/main/java/speiger/src/collections/doubles/utils/maps/Double2BooleanMaps.java @@ -608,7 +608,11 @@ public class Double2BooleanMaps super(c); set = c; } - + + @Override + public void addFirst(Double2BooleanMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Double2BooleanMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Double2BooleanMap.Entry o) { throw new UnsupportedOperationException(); } @Override diff --git a/src/main/java/speiger/src/collections/doubles/utils/maps/Double2ByteMaps.java b/src/main/java/speiger/src/collections/doubles/utils/maps/Double2ByteMaps.java index 3801fd5..c3b94ae 100644 --- a/src/main/java/speiger/src/collections/doubles/utils/maps/Double2ByteMaps.java +++ b/src/main/java/speiger/src/collections/doubles/utils/maps/Double2ByteMaps.java @@ -620,7 +620,11 @@ public class Double2ByteMaps super(c); set = c; } - + + @Override + public void addFirst(Double2ByteMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Double2ByteMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Double2ByteMap.Entry o) { throw new UnsupportedOperationException(); } @Override diff --git a/src/main/java/speiger/src/collections/doubles/utils/maps/Double2CharMaps.java b/src/main/java/speiger/src/collections/doubles/utils/maps/Double2CharMaps.java index 8b1ffa9..c187987 100644 --- a/src/main/java/speiger/src/collections/doubles/utils/maps/Double2CharMaps.java +++ b/src/main/java/speiger/src/collections/doubles/utils/maps/Double2CharMaps.java @@ -620,7 +620,11 @@ public class Double2CharMaps super(c); set = c; } - + + @Override + public void addFirst(Double2CharMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Double2CharMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Double2CharMap.Entry o) { throw new UnsupportedOperationException(); } @Override diff --git a/src/main/java/speiger/src/collections/doubles/utils/maps/Double2DoubleMaps.java b/src/main/java/speiger/src/collections/doubles/utils/maps/Double2DoubleMaps.java index 4eb5169..c6b6f2e 100644 --- a/src/main/java/speiger/src/collections/doubles/utils/maps/Double2DoubleMaps.java +++ b/src/main/java/speiger/src/collections/doubles/utils/maps/Double2DoubleMaps.java @@ -619,7 +619,11 @@ public class Double2DoubleMaps super(c); set = c; } - + + @Override + public void addFirst(Double2DoubleMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Double2DoubleMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Double2DoubleMap.Entry o) { throw new UnsupportedOperationException(); } @Override diff --git a/src/main/java/speiger/src/collections/doubles/utils/maps/Double2FloatMaps.java b/src/main/java/speiger/src/collections/doubles/utils/maps/Double2FloatMaps.java index ccb7eb0..8f18b71 100644 --- a/src/main/java/speiger/src/collections/doubles/utils/maps/Double2FloatMaps.java +++ b/src/main/java/speiger/src/collections/doubles/utils/maps/Double2FloatMaps.java @@ -620,7 +620,11 @@ public class Double2FloatMaps super(c); set = c; } - + + @Override + public void addFirst(Double2FloatMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Double2FloatMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Double2FloatMap.Entry o) { throw new UnsupportedOperationException(); } @Override diff --git a/src/main/java/speiger/src/collections/doubles/utils/maps/Double2IntMaps.java b/src/main/java/speiger/src/collections/doubles/utils/maps/Double2IntMaps.java index fa9a894..35724ae 100644 --- a/src/main/java/speiger/src/collections/doubles/utils/maps/Double2IntMaps.java +++ b/src/main/java/speiger/src/collections/doubles/utils/maps/Double2IntMaps.java @@ -620,7 +620,11 @@ public class Double2IntMaps super(c); set = c; } - + + @Override + public void addFirst(Double2IntMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Double2IntMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Double2IntMap.Entry o) { throw new UnsupportedOperationException(); } @Override diff --git a/src/main/java/speiger/src/collections/doubles/utils/maps/Double2LongMaps.java b/src/main/java/speiger/src/collections/doubles/utils/maps/Double2LongMaps.java index 6367af0..5be485b 100644 --- a/src/main/java/speiger/src/collections/doubles/utils/maps/Double2LongMaps.java +++ b/src/main/java/speiger/src/collections/doubles/utils/maps/Double2LongMaps.java @@ -620,7 +620,11 @@ public class Double2LongMaps super(c); set = c; } - + + @Override + public void addFirst(Double2LongMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Double2LongMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Double2LongMap.Entry o) { throw new UnsupportedOperationException(); } @Override diff --git a/src/main/java/speiger/src/collections/doubles/utils/maps/Double2ObjectMaps.java b/src/main/java/speiger/src/collections/doubles/utils/maps/Double2ObjectMaps.java index a87992f..c7cf47b 100644 --- a/src/main/java/speiger/src/collections/doubles/utils/maps/Double2ObjectMaps.java +++ b/src/main/java/speiger/src/collections/doubles/utils/maps/Double2ObjectMaps.java @@ -611,7 +611,11 @@ public class Double2ObjectMaps super(c); set = c; } - + + @Override + public void addFirst(Double2ObjectMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Double2ObjectMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Double2ObjectMap.Entry o) { throw new UnsupportedOperationException(); } @Override diff --git a/src/main/java/speiger/src/collections/doubles/utils/maps/Double2ShortMaps.java b/src/main/java/speiger/src/collections/doubles/utils/maps/Double2ShortMaps.java index 3416be8..d937971 100644 --- a/src/main/java/speiger/src/collections/doubles/utils/maps/Double2ShortMaps.java +++ b/src/main/java/speiger/src/collections/doubles/utils/maps/Double2ShortMaps.java @@ -620,7 +620,11 @@ public class Double2ShortMaps super(c); set = c; } - + + @Override + public void addFirst(Double2ShortMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Double2ShortMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Double2ShortMap.Entry o) { throw new UnsupportedOperationException(); } @Override diff --git a/src/main/java/speiger/src/collections/floats/collections/FloatIterable.java b/src/main/java/speiger/src/collections/floats/collections/FloatIterable.java index df80beb..daa31b9 100644 --- a/src/main/java/speiger/src/collections/floats/collections/FloatIterable.java +++ b/src/main/java/speiger/src/collections/floats/collections/FloatIterable.java @@ -7,6 +7,7 @@ import java.util.function.Consumer; import speiger.src.collections.floats.functions.FloatConsumer; import speiger.src.collections.floats.functions.FloatComparator; import speiger.src.collections.objects.collections.ObjectIterable; +import speiger.src.collections.floats.functions.OptionalFloat; import speiger.src.collections.floats.functions.function.FloatFunction; import speiger.src.collections.ints.functions.consumer.IntFloatConsumer; import speiger.src.collections.objects.functions.consumer.ObjectFloatConsumer; @@ -281,13 +282,13 @@ public interface FloatIterable extends Iterable * @param filter that should be applied * @return the found value or the null equivalent variant. */ - default float findFirst(FloatPredicate filter) { + default OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); for(FloatIterator iter = iterator();iter.hasNext();) { float entry = iter.nextFloat(); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return OptionalFloat.of(entry); } - return 0F; + return OptionalFloat.empty(); } /** @@ -312,7 +313,7 @@ public interface FloatIterable extends Iterable * @param operator the operation that should be applied * @return the reduction result, returns null value if nothing was found */ - default float reduce(FloatFloatUnaryOperator operator) { + default OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -324,7 +325,7 @@ public interface FloatIterable extends Iterable } state = operator.applyAsFloat(state, iter.nextFloat()); } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } /** diff --git a/src/main/java/speiger/src/collections/floats/lists/CopyOnWriteFloatArrayList.java b/src/main/java/speiger/src/collections/floats/lists/CopyOnWriteFloatArrayList.java index af658a8..4f9ea5c 100644 --- a/src/main/java/speiger/src/collections/floats/lists/CopyOnWriteFloatArrayList.java +++ b/src/main/java/speiger/src/collections/floats/lists/CopyOnWriteFloatArrayList.java @@ -16,6 +16,7 @@ import java.nio.FloatBuffer; import speiger.src.collections.floats.collections.FloatCollection; import speiger.src.collections.floats.collections.FloatStack; import speiger.src.collections.floats.collections.FloatIterator; +import speiger.src.collections.floats.functions.OptionalFloat; import speiger.src.collections.floats.functions.FloatComparator; import speiger.src.collections.floats.functions.FloatConsumer; import speiger.src.collections.objects.functions.consumer.ObjectFloatConsumer; @@ -654,13 +655,13 @@ public class CopyOnWriteFloatArrayList extends AbstractFloatList implements ITri } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); float[] data = this.data; for(int i = 0,m=data.length;i operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Float2BooleanMap.Entry state = null; boolean empty = true; @@ -779,11 +782,11 @@ public class Float2BooleanConcurrentOpenHashMap extends AbstractFloat2BooleanMap seg.unlockRead(stamp); } } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Float2BooleanMap.Entry 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); Float2ByteMap.Entry state = null; boolean empty = true; @@ -792,11 +795,11 @@ public class Float2ByteConcurrentOpenHashMap extends AbstractFloat2ByteMap imple seg.unlockRead(stamp); } } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Float2ByteMap.Entry 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); Float2CharMap.Entry state = null; boolean empty = true; @@ -792,11 +795,11 @@ public class Float2CharConcurrentOpenHashMap extends AbstractFloat2CharMap imple seg.unlockRead(stamp); } } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Float2CharMap.Entry 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); Float2DoubleMap.Entry state = null; boolean empty = true; @@ -792,11 +795,11 @@ public class Float2DoubleConcurrentOpenHashMap extends AbstractFloat2DoubleMap i seg.unlockRead(stamp); } } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Float2DoubleMap.Entry 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); Float2FloatMap.Entry state = null; boolean empty = true; @@ -785,11 +787,11 @@ public class Float2FloatConcurrentOpenHashMap extends AbstractFloat2FloatMap imp seg.unlockRead(stamp); } } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Float2FloatMap.Entry 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); Float2IntMap.Entry state = null; boolean empty = true; @@ -792,11 +795,11 @@ public class Float2IntConcurrentOpenHashMap extends AbstractFloat2IntMap impleme seg.unlockRead(stamp); } } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Float2IntMap.Entry 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); Float2LongMap.Entry state = null; boolean empty = true; @@ -792,11 +795,11 @@ public class Float2LongConcurrentOpenHashMap extends AbstractFloat2LongMap imple seg.unlockRead(stamp); } } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Float2LongMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); MapEntry entry = new MapEntry(); for(int i = 0,m=segments.length;i extends AbstractFloat2ObjectMa } @Override - public Float2ObjectMap.Entry reduce(ObjectObjectUnaryOperator, Float2ObjectMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Float2ObjectMap.Entry> operator) { Objects.requireNonNull(operator); Float2ObjectMap.Entry state = null; boolean empty = true; @@ -739,11 +741,11 @@ public class Float2ObjectConcurrentOpenHashMap extends AbstractFloat2ObjectMa seg.unlockRead(stamp); } } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Float2ObjectMap.Entry findFirst(Predicate> filter) { + public Optional> findFirst(Predicate> filter) { Objects.requireNonNull(filter); MapEntry entry = new MapEntry(); for(int i = 0,m=segments.length;i extends AbstractFloat2ObjectMa try { while(index != -1) { entry.set(index, i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); index = (int)seg.links[index]; } } @@ -761,7 +763,7 @@ public class Float2ObjectConcurrentOpenHashMap extends AbstractFloat2ObjectMa seg.unlockRead(stamp); } } - return null; + return Optional.empty(); } @Override @@ -1022,7 +1024,7 @@ public class Float2ObjectConcurrentOpenHashMap extends AbstractFloat2ObjectMa } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -1046,11 +1048,11 @@ public class Float2ObjectConcurrentOpenHashMap extends AbstractFloat2ObjectMa seg.unlockRead(stamp); } } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); for(int i = 0,m=segments.length;i seg = segments[i]; @@ -1058,7 +1060,7 @@ public class Float2ObjectConcurrentOpenHashMap extends AbstractFloat2ObjectMa try { int index = seg.firstIndex; while(index != -1){ - if(filter.test(seg.keys[index])) return seg.keys[index]; + if(filter.test(seg.keys[index])) return OptionalFloat.of(seg.keys[index]); index = (int)seg.links[index]; } } @@ -1066,7 +1068,7 @@ public class Float2ObjectConcurrentOpenHashMap extends AbstractFloat2ObjectMa seg.unlockRead(stamp); } } - return 0F; + return OptionalFloat.empty(); } @Override @@ -1258,7 +1260,7 @@ public class Float2ObjectConcurrentOpenHashMap extends AbstractFloat2ObjectMa } @Override - public V reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); V state = null; boolean empty = true; @@ -1282,20 +1284,20 @@ public class Float2ObjectConcurrentOpenHashMap extends AbstractFloat2ObjectMa seg.unlockRead(stamp); } } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public V findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); for(int i = 0,m=segments.length;i seg = segments[i]; long stamp = seg.readLock(); try { int index = seg.firstIndex; while(index != -1){ - if(filter.test(seg.values[index])) return seg.values[index]; + if(filter.test(seg.values[index])) return Optional.ofNullable(seg.values[index]); index = (int)seg.links[index]; } } @@ -1303,7 +1305,7 @@ public class Float2ObjectConcurrentOpenHashMap extends AbstractFloat2ObjectMa seg.unlockRead(stamp); } } - return null; + return Optional.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/floats/maps/impl/concurrent/Float2ShortConcurrentOpenHashMap.java b/src/main/java/speiger/src/collections/floats/maps/impl/concurrent/Float2ShortConcurrentOpenHashMap.java index ce27739..e923f42 100644 --- a/src/main/java/speiger/src/collections/floats/maps/impl/concurrent/Float2ShortConcurrentOpenHashMap.java +++ b/src/main/java/speiger/src/collections/floats/maps/impl/concurrent/Float2ShortConcurrentOpenHashMap.java @@ -4,6 +4,7 @@ import java.util.Arrays; import java.util.Map; import java.util.NoSuchElementException; import java.util.Objects; +import java.util.Optional; import java.util.concurrent.locks.StampedLock; import java.util.function.Consumer; import java.util.function.Predicate; @@ -19,6 +20,7 @@ import speiger.src.collections.floats.functions.consumer.FloatShortConsumer; import speiger.src.collections.floats.functions.function.Float2ShortFunction; import speiger.src.collections.floats.functions.function.FloatShortUnaryOperator; import speiger.src.collections.floats.functions.function.FloatFloatUnaryOperator; +import speiger.src.collections.floats.functions.OptionalFloat; import speiger.src.collections.floats.functions.function.FloatPredicate; import speiger.src.collections.floats.maps.abstracts.AbstractFloat2ShortMap; import speiger.src.collections.floats.maps.interfaces.Float2ShortMap; @@ -37,6 +39,7 @@ import speiger.src.collections.shorts.functions.ShortConsumer; import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectShortConsumer; import speiger.src.collections.shorts.functions.function.ShortPredicate; +import speiger.src.collections.shorts.functions.OptionalShort; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.objects.sets.AbstractObjectSet; import speiger.src.collections.objects.sets.ObjectSet; @@ -768,7 +771,7 @@ public class Float2ShortConcurrentOpenHashMap extends AbstractFloat2ShortMap imp } @Override - public Float2ShortMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Float2ShortMap.Entry state = null; boolean empty = true; @@ -792,11 +795,11 @@ public class Float2ShortConcurrentOpenHashMap extends AbstractFloat2ShortMap imp seg.unlockRead(stamp); } } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Float2ShortMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); MapEntry entry = new MapEntry(); for(int i = 0,m=segments.length;i implements Float2BooleanOrderedMap.FastOrderedSet { + @Override + public void addFirst(Float2BooleanMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Float2BooleanMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Float2BooleanMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -836,7 +843,7 @@ public class Float2BooleanLinkedOpenCustomHashMap extends Float2BooleanOpenCusto } @Override - public Float2BooleanMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Float2BooleanMap.Entry state = null; boolean empty = true; @@ -851,21 +858,21 @@ public class Float2BooleanLinkedOpenCustomHashMap extends Float2BooleanOpenCusto state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Float2BooleanMap.Entry 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 @@ -944,10 +951,13 @@ public class Float2BooleanLinkedOpenCustomHashMap extends Float2BooleanOpenCusto public boolean add(float o) { throw new UnsupportedOperationException(); } - + + @Override + public void addFirst(float o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(float o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(float o) { throw new UnsupportedOperationException(); } - @Override public boolean addAndMoveToLast(float o) { throw new UnsupportedOperationException(); } @@ -1090,7 +1100,7 @@ public class Float2BooleanLinkedOpenCustomHashMap extends Float2BooleanOpenCusto } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -1105,19 +1115,19 @@ public class Float2BooleanLinkedOpenCustomHashMap extends Float2BooleanOpenCusto state = operator.applyAsFloat(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0F; + if(size() <= 0) return OptionalFloat.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalFloat.of(keys[index]); index = (int)links[index]; } - return 0F; + return OptionalFloat.empty(); } @Override @@ -1252,7 +1262,7 @@ public class Float2BooleanLinkedOpenCustomHashMap extends Float2BooleanOpenCusto } @Override - public boolean reduce(BooleanBooleanUnaryOperator operator) { + public OptionalBoolean reduce(BooleanBooleanUnaryOperator operator) { Objects.requireNonNull(operator); boolean state = false; boolean empty = true; @@ -1267,19 +1277,19 @@ public class Float2BooleanLinkedOpenCustomHashMap extends Float2BooleanOpenCusto state = operator.applyAsBoolean(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalBoolean.empty() : OptionalBoolean.of(state); } @Override - public boolean findFirst(BooleanPredicate filter) { + public OptionalBoolean findFirst(BooleanPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return false; + if(size() <= 0) return OptionalBoolean.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalBoolean.of(values[index]); index = (int)links[index]; } - return false; + return OptionalBoolean.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/floats/maps/impl/customHash/Float2BooleanOpenCustomHashMap.java b/src/main/java/speiger/src/collections/floats/maps/impl/customHash/Float2BooleanOpenCustomHashMap.java index fcb2d01..f9d3d00 100644 --- a/src/main/java/speiger/src/collections/floats/maps/impl/customHash/Float2BooleanOpenCustomHashMap.java +++ b/src/main/java/speiger/src/collections/floats/maps/impl/customHash/Float2BooleanOpenCustomHashMap.java @@ -5,6 +5,7 @@ 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; @@ -19,6 +20,7 @@ import speiger.src.collections.floats.functions.consumer.FloatBooleanConsumer; import speiger.src.collections.floats.functions.function.FloatPredicate; import speiger.src.collections.floats.functions.function.FloatBooleanUnaryOperator; import speiger.src.collections.floats.functions.function.FloatFloatUnaryOperator; +import speiger.src.collections.floats.functions.OptionalFloat; import speiger.src.collections.floats.maps.abstracts.AbstractFloat2BooleanMap; import speiger.src.collections.floats.maps.interfaces.Float2BooleanMap; import speiger.src.collections.floats.sets.AbstractFloatSet; @@ -36,6 +38,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectBooleanConsumer; import speiger.src.collections.booleans.functions.function.BooleanPredicate; +import speiger.src.collections.booleans.functions.OptionalBoolean; import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.sets.AbstractObjectSet; import speiger.src.collections.objects.sets.ObjectSet; @@ -930,7 +933,7 @@ public class Float2BooleanOpenCustomHashMap extends AbstractFloat2BooleanMap imp } @Override - public Float2BooleanMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Float2BooleanMap.Entry state = null; boolean empty = true; @@ -947,25 +950,25 @@ public class Float2BooleanOpenCustomHashMap extends AbstractFloat2BooleanMap imp } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Float2BooleanMap.Entry 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], 0F)) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1139,7 +1142,7 @@ public class Float2BooleanOpenCustomHashMap extends AbstractFloat2BooleanMap imp } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -1156,18 +1159,18 @@ public class Float2BooleanOpenCustomHashMap extends AbstractFloat2BooleanMap imp } state = operator.applyAsFloat(state, keys[i]); } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0F; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OptionalFloat.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OptionalFloat.of(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], 0F) && filter.test(keys[i])) return keys[i]; + if(!strategy.equals(keys[i], 0F) && filter.test(keys[i])) return OptionalFloat.of(keys[i]); } - return 0F; + return OptionalFloat.empty(); } @Override @@ -1282,7 +1285,7 @@ public class Float2BooleanOpenCustomHashMap extends AbstractFloat2BooleanMap imp } @Override - public boolean reduce(BooleanBooleanUnaryOperator operator) { + public OptionalBoolean reduce(BooleanBooleanUnaryOperator operator) { Objects.requireNonNull(operator); boolean state = false; boolean empty = true; @@ -1299,18 +1302,18 @@ public class Float2BooleanOpenCustomHashMap extends AbstractFloat2BooleanMap imp } state = operator.applyAsBoolean(state, values[i]); } - return state; + return empty ? OptionalBoolean.empty() : OptionalBoolean.of(state); } @Override - public boolean findFirst(BooleanPredicate filter) { + public OptionalBoolean findFirst(BooleanPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return false; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return OptionalBoolean.empty(); + if(containsNull && filter.test(values[nullIndex])) return OptionalBoolean.of(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], 0F) && filter.test(values[i])) return values[i]; + if(!strategy.equals(keys[i], 0F) && filter.test(values[i])) return OptionalBoolean.of(values[i]); } - return false; + return OptionalBoolean.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/floats/maps/impl/customHash/Float2ByteLinkedOpenCustomHashMap.java b/src/main/java/speiger/src/collections/floats/maps/impl/customHash/Float2ByteLinkedOpenCustomHashMap.java index 8d30bd7..311deea 100644 --- a/src/main/java/speiger/src/collections/floats/maps/impl/customHash/Float2ByteLinkedOpenCustomHashMap.java +++ b/src/main/java/speiger/src/collections/floats/maps/impl/customHash/Float2ByteLinkedOpenCustomHashMap.java @@ -4,6 +4,7 @@ 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; @@ -14,6 +15,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectFloatConsumer; import speiger.src.collections.ints.functions.consumer.IntFloatConsumer; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntByteConsumer; +import speiger.src.collections.floats.functions.OptionalFloat; import speiger.src.collections.floats.functions.function.FloatPredicate; import speiger.src.collections.floats.functions.consumer.FloatByteConsumer; import speiger.src.collections.floats.functions.function.FloatFloatUnaryOperator; @@ -33,6 +35,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectByteConsumer; import speiger.src.collections.bytes.functions.function.BytePredicate; +import speiger.src.collections.bytes.functions.OptionalByte; import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOperator; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.objects.lists.ObjectListIterator; @@ -671,6 +674,10 @@ public class Float2ByteLinkedOpenCustomHashMap extends Float2ByteOpenCustomHashM } private class MapEntrySet extends AbstractObjectSet implements Float2ByteOrderedMap.FastOrderedSet { + @Override + public void addFirst(Float2ByteMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Float2ByteMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Float2ByteMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -836,7 +843,7 @@ public class Float2ByteLinkedOpenCustomHashMap extends Float2ByteOpenCustomHashM } @Override - public Float2ByteMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Float2ByteMap.Entry state = null; boolean empty = true; @@ -851,21 +858,21 @@ public class Float2ByteLinkedOpenCustomHashMap extends Float2ByteOpenCustomHashM state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Float2ByteMap.Entry 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 @@ -944,10 +951,13 @@ public class Float2ByteLinkedOpenCustomHashMap extends Float2ByteOpenCustomHashM public boolean add(float o) { throw new UnsupportedOperationException(); } - + + @Override + public void addFirst(float o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(float o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(float o) { throw new UnsupportedOperationException(); } - @Override public boolean addAndMoveToLast(float o) { throw new UnsupportedOperationException(); } @@ -1090,7 +1100,7 @@ public class Float2ByteLinkedOpenCustomHashMap extends Float2ByteOpenCustomHashM } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -1105,19 +1115,19 @@ public class Float2ByteLinkedOpenCustomHashMap extends Float2ByteOpenCustomHashM state = operator.applyAsFloat(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0F; + if(size() <= 0) return OptionalFloat.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalFloat.of(keys[index]); index = (int)links[index]; } - return 0F; + return OptionalFloat.empty(); } @Override @@ -1252,7 +1262,7 @@ public class Float2ByteLinkedOpenCustomHashMap extends Float2ByteOpenCustomHashM } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -1267,19 +1277,19 @@ public class Float2ByteLinkedOpenCustomHashMap extends Float2ByteOpenCustomHashM state = operator.applyAsByte(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (byte)0; + if(size() <= 0) return OptionalByte.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalByte.of(values[index]); index = (int)links[index]; } - return (byte)0; + return OptionalByte.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/floats/maps/impl/customHash/Float2ByteOpenCustomHashMap.java b/src/main/java/speiger/src/collections/floats/maps/impl/customHash/Float2ByteOpenCustomHashMap.java index f0c1bdc..81f237f 100644 --- a/src/main/java/speiger/src/collections/floats/maps/impl/customHash/Float2ByteOpenCustomHashMap.java +++ b/src/main/java/speiger/src/collections/floats/maps/impl/customHash/Float2ByteOpenCustomHashMap.java @@ -5,6 +5,7 @@ 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; @@ -19,6 +20,7 @@ import speiger.src.collections.floats.functions.consumer.FloatByteConsumer; import speiger.src.collections.floats.functions.function.Float2ByteFunction; import speiger.src.collections.floats.functions.function.FloatByteUnaryOperator; import speiger.src.collections.floats.functions.function.FloatFloatUnaryOperator; +import speiger.src.collections.floats.functions.OptionalFloat; import speiger.src.collections.floats.functions.function.FloatPredicate; import speiger.src.collections.floats.maps.abstracts.AbstractFloat2ByteMap; import speiger.src.collections.floats.maps.interfaces.Float2ByteMap; @@ -37,6 +39,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectByteConsumer; import speiger.src.collections.bytes.functions.function.BytePredicate; +import speiger.src.collections.bytes.functions.OptionalByte; import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.sets.AbstractObjectSet; import speiger.src.collections.objects.sets.ObjectSet; @@ -952,7 +955,7 @@ public class Float2ByteOpenCustomHashMap extends AbstractFloat2ByteMap implement } @Override - public Float2ByteMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Float2ByteMap.Entry state = null; boolean empty = true; @@ -969,25 +972,25 @@ public class Float2ByteOpenCustomHashMap extends AbstractFloat2ByteMap implement } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Float2ByteMap.Entry 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], 0F)) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1161,7 +1164,7 @@ public class Float2ByteOpenCustomHashMap extends AbstractFloat2ByteMap implement } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -1178,18 +1181,18 @@ public class Float2ByteOpenCustomHashMap extends AbstractFloat2ByteMap implement } state = operator.applyAsFloat(state, keys[i]); } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0F; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OptionalFloat.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OptionalFloat.of(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], 0F) && filter.test(keys[i])) return keys[i]; + if(!strategy.equals(keys[i], 0F) && filter.test(keys[i])) return OptionalFloat.of(keys[i]); } - return 0F; + return OptionalFloat.empty(); } @Override @@ -1304,7 +1307,7 @@ public class Float2ByteOpenCustomHashMap extends AbstractFloat2ByteMap implement } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -1321,18 +1324,18 @@ public class Float2ByteOpenCustomHashMap extends AbstractFloat2ByteMap implement } state = operator.applyAsByte(state, values[i]); } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (byte)0; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return OptionalByte.empty(); + if(containsNull && filter.test(values[nullIndex])) return OptionalByte.of(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], 0F) && filter.test(values[i])) return values[i]; + if(!strategy.equals(keys[i], 0F) && filter.test(values[i])) return OptionalByte.of(values[i]); } - return (byte)0; + return OptionalByte.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/floats/maps/impl/customHash/Float2CharLinkedOpenCustomHashMap.java b/src/main/java/speiger/src/collections/floats/maps/impl/customHash/Float2CharLinkedOpenCustomHashMap.java index a4c58a4..37e629f 100644 --- a/src/main/java/speiger/src/collections/floats/maps/impl/customHash/Float2CharLinkedOpenCustomHashMap.java +++ b/src/main/java/speiger/src/collections/floats/maps/impl/customHash/Float2CharLinkedOpenCustomHashMap.java @@ -4,6 +4,7 @@ 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; @@ -14,6 +15,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectFloatConsumer; import speiger.src.collections.ints.functions.consumer.IntFloatConsumer; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntCharConsumer; +import speiger.src.collections.floats.functions.OptionalFloat; import speiger.src.collections.floats.functions.function.FloatPredicate; import speiger.src.collections.floats.functions.consumer.FloatCharConsumer; import speiger.src.collections.floats.functions.function.FloatFloatUnaryOperator; @@ -33,6 +35,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectCharConsumer; import speiger.src.collections.chars.functions.function.CharPredicate; +import speiger.src.collections.chars.functions.OptionalChar; import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOperator; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.objects.lists.ObjectListIterator; @@ -671,6 +674,10 @@ public class Float2CharLinkedOpenCustomHashMap extends Float2CharOpenCustomHashM } private class MapEntrySet extends AbstractObjectSet implements Float2CharOrderedMap.FastOrderedSet { + @Override + public void addFirst(Float2CharMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Float2CharMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Float2CharMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -836,7 +843,7 @@ public class Float2CharLinkedOpenCustomHashMap extends Float2CharOpenCustomHashM } @Override - public Float2CharMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Float2CharMap.Entry state = null; boolean empty = true; @@ -851,21 +858,21 @@ public class Float2CharLinkedOpenCustomHashMap extends Float2CharOpenCustomHashM state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Float2CharMap.Entry 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 @@ -944,10 +951,13 @@ public class Float2CharLinkedOpenCustomHashMap extends Float2CharOpenCustomHashM public boolean add(float o) { throw new UnsupportedOperationException(); } - + + @Override + public void addFirst(float o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(float o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(float o) { throw new UnsupportedOperationException(); } - @Override public boolean addAndMoveToLast(float o) { throw new UnsupportedOperationException(); } @@ -1090,7 +1100,7 @@ public class Float2CharLinkedOpenCustomHashMap extends Float2CharOpenCustomHashM } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -1105,19 +1115,19 @@ public class Float2CharLinkedOpenCustomHashMap extends Float2CharOpenCustomHashM state = operator.applyAsFloat(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0F; + if(size() <= 0) return OptionalFloat.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalFloat.of(keys[index]); index = (int)links[index]; } - return 0F; + return OptionalFloat.empty(); } @Override @@ -1252,7 +1262,7 @@ public class Float2CharLinkedOpenCustomHashMap extends Float2CharOpenCustomHashM } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -1267,19 +1277,19 @@ public class Float2CharLinkedOpenCustomHashMap extends Float2CharOpenCustomHashM state = operator.applyAsChar(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (char)0; + if(size() <= 0) return OptionalChar.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalChar.of(values[index]); index = (int)links[index]; } - return (char)0; + return OptionalChar.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/floats/maps/impl/customHash/Float2CharOpenCustomHashMap.java b/src/main/java/speiger/src/collections/floats/maps/impl/customHash/Float2CharOpenCustomHashMap.java index a84890e..3a934f3 100644 --- a/src/main/java/speiger/src/collections/floats/maps/impl/customHash/Float2CharOpenCustomHashMap.java +++ b/src/main/java/speiger/src/collections/floats/maps/impl/customHash/Float2CharOpenCustomHashMap.java @@ -5,6 +5,7 @@ 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; @@ -19,6 +20,7 @@ import speiger.src.collections.floats.functions.consumer.FloatCharConsumer; import speiger.src.collections.floats.functions.function.Float2CharFunction; import speiger.src.collections.floats.functions.function.FloatCharUnaryOperator; import speiger.src.collections.floats.functions.function.FloatFloatUnaryOperator; +import speiger.src.collections.floats.functions.OptionalFloat; import speiger.src.collections.floats.functions.function.FloatPredicate; import speiger.src.collections.floats.maps.abstracts.AbstractFloat2CharMap; import speiger.src.collections.floats.maps.interfaces.Float2CharMap; @@ -37,6 +39,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectCharConsumer; import speiger.src.collections.chars.functions.function.CharPredicate; +import speiger.src.collections.chars.functions.OptionalChar; import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.sets.AbstractObjectSet; import speiger.src.collections.objects.sets.ObjectSet; @@ -952,7 +955,7 @@ public class Float2CharOpenCustomHashMap extends AbstractFloat2CharMap implement } @Override - public Float2CharMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Float2CharMap.Entry state = null; boolean empty = true; @@ -969,25 +972,25 @@ public class Float2CharOpenCustomHashMap extends AbstractFloat2CharMap implement } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Float2CharMap.Entry 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], 0F)) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1161,7 +1164,7 @@ public class Float2CharOpenCustomHashMap extends AbstractFloat2CharMap implement } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -1178,18 +1181,18 @@ public class Float2CharOpenCustomHashMap extends AbstractFloat2CharMap implement } state = operator.applyAsFloat(state, keys[i]); } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0F; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OptionalFloat.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OptionalFloat.of(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], 0F) && filter.test(keys[i])) return keys[i]; + if(!strategy.equals(keys[i], 0F) && filter.test(keys[i])) return OptionalFloat.of(keys[i]); } - return 0F; + return OptionalFloat.empty(); } @Override @@ -1304,7 +1307,7 @@ public class Float2CharOpenCustomHashMap extends AbstractFloat2CharMap implement } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -1321,18 +1324,18 @@ public class Float2CharOpenCustomHashMap extends AbstractFloat2CharMap implement } state = operator.applyAsChar(state, values[i]); } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (char)0; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return OptionalChar.empty(); + if(containsNull && filter.test(values[nullIndex])) return OptionalChar.of(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], 0F) && filter.test(values[i])) return values[i]; + if(!strategy.equals(keys[i], 0F) && filter.test(values[i])) return OptionalChar.of(values[i]); } - return (char)0; + return OptionalChar.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/floats/maps/impl/customHash/Float2DoubleLinkedOpenCustomHashMap.java b/src/main/java/speiger/src/collections/floats/maps/impl/customHash/Float2DoubleLinkedOpenCustomHashMap.java index 717f9ac..5ce21e8 100644 --- a/src/main/java/speiger/src/collections/floats/maps/impl/customHash/Float2DoubleLinkedOpenCustomHashMap.java +++ b/src/main/java/speiger/src/collections/floats/maps/impl/customHash/Float2DoubleLinkedOpenCustomHashMap.java @@ -4,10 +4,12 @@ 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; import java.util.function.DoublePredicate; +import java.util.OptionalDouble; import speiger.src.collections.floats.collections.FloatBidirectionalIterator; import speiger.src.collections.floats.functions.FloatConsumer; @@ -15,6 +17,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectFloatConsumer; import speiger.src.collections.ints.functions.consumer.IntFloatConsumer; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntDoubleConsumer; +import speiger.src.collections.floats.functions.OptionalFloat; import speiger.src.collections.floats.functions.function.FloatPredicate; import speiger.src.collections.floats.functions.consumer.FloatDoubleConsumer; import speiger.src.collections.floats.functions.function.FloatFloatUnaryOperator; @@ -671,6 +674,10 @@ public class Float2DoubleLinkedOpenCustomHashMap extends Float2DoubleOpenCustomH } private class MapEntrySet extends AbstractObjectSet implements Float2DoubleOrderedMap.FastOrderedSet { + @Override + public void addFirst(Float2DoubleMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Float2DoubleMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Float2DoubleMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -836,7 +843,7 @@ public class Float2DoubleLinkedOpenCustomHashMap extends Float2DoubleOpenCustomH } @Override - public Float2DoubleMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Float2DoubleMap.Entry state = null; boolean empty = true; @@ -851,21 +858,21 @@ public class Float2DoubleLinkedOpenCustomHashMap extends Float2DoubleOpenCustomH state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Float2DoubleMap.Entry 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 @@ -944,10 +951,13 @@ public class Float2DoubleLinkedOpenCustomHashMap extends Float2DoubleOpenCustomH public boolean add(float o) { throw new UnsupportedOperationException(); } - + + @Override + public void addFirst(float o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(float o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(float o) { throw new UnsupportedOperationException(); } - @Override public boolean addAndMoveToLast(float o) { throw new UnsupportedOperationException(); } @@ -1090,7 +1100,7 @@ public class Float2DoubleLinkedOpenCustomHashMap extends Float2DoubleOpenCustomH } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -1105,19 +1115,19 @@ public class Float2DoubleLinkedOpenCustomHashMap extends Float2DoubleOpenCustomH state = operator.applyAsFloat(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0F; + if(size() <= 0) return OptionalFloat.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalFloat.of(keys[index]); index = (int)links[index]; } - return 0F; + return OptionalFloat.empty(); } @Override @@ -1252,7 +1262,7 @@ public class Float2DoubleLinkedOpenCustomHashMap extends Float2DoubleOpenCustomH } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -1267,19 +1277,19 @@ public class Float2DoubleLinkedOpenCustomHashMap extends Float2DoubleOpenCustomH state = operator.applyAsDouble(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0D; + if(size() <= 0) return OptionalDouble.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalDouble.of(values[index]); index = (int)links[index]; } - return 0D; + return OptionalDouble.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/floats/maps/impl/customHash/Float2DoubleOpenCustomHashMap.java b/src/main/java/speiger/src/collections/floats/maps/impl/customHash/Float2DoubleOpenCustomHashMap.java index 8879ace..50c118c 100644 --- a/src/main/java/speiger/src/collections/floats/maps/impl/customHash/Float2DoubleOpenCustomHashMap.java +++ b/src/main/java/speiger/src/collections/floats/maps/impl/customHash/Float2DoubleOpenCustomHashMap.java @@ -5,10 +5,12 @@ 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; import java.util.function.DoublePredicate; +import java.util.OptionalDouble; import speiger.src.collections.floats.collections.FloatIterator; import speiger.src.collections.floats.functions.FloatConsumer; @@ -20,6 +22,7 @@ import speiger.src.collections.floats.functions.consumer.FloatDoubleConsumer; import speiger.src.collections.floats.functions.function.Float2DoubleFunction; import speiger.src.collections.floats.functions.function.FloatDoubleUnaryOperator; import speiger.src.collections.floats.functions.function.FloatFloatUnaryOperator; +import speiger.src.collections.floats.functions.OptionalFloat; import speiger.src.collections.floats.functions.function.FloatPredicate; import speiger.src.collections.floats.maps.abstracts.AbstractFloat2DoubleMap; import speiger.src.collections.floats.maps.interfaces.Float2DoubleMap; @@ -952,7 +955,7 @@ public class Float2DoubleOpenCustomHashMap extends AbstractFloat2DoubleMap imple } @Override - public Float2DoubleMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Float2DoubleMap.Entry state = null; boolean empty = true; @@ -969,25 +972,25 @@ public class Float2DoubleOpenCustomHashMap extends AbstractFloat2DoubleMap imple } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Float2DoubleMap.Entry 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], 0F)) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1161,7 +1164,7 @@ public class Float2DoubleOpenCustomHashMap extends AbstractFloat2DoubleMap imple } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -1178,18 +1181,18 @@ public class Float2DoubleOpenCustomHashMap extends AbstractFloat2DoubleMap imple } state = operator.applyAsFloat(state, keys[i]); } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0F; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OptionalFloat.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OptionalFloat.of(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], 0F) && filter.test(keys[i])) return keys[i]; + if(!strategy.equals(keys[i], 0F) && filter.test(keys[i])) return OptionalFloat.of(keys[i]); } - return 0F; + return OptionalFloat.empty(); } @Override @@ -1304,7 +1307,7 @@ public class Float2DoubleOpenCustomHashMap extends AbstractFloat2DoubleMap imple } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -1321,18 +1324,18 @@ public class Float2DoubleOpenCustomHashMap extends AbstractFloat2DoubleMap imple } state = operator.applyAsDouble(state, values[i]); } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0D; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return OptionalDouble.empty(); + if(containsNull && filter.test(values[nullIndex])) return OptionalDouble.of(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], 0F) && filter.test(values[i])) return values[i]; + if(!strategy.equals(keys[i], 0F) && filter.test(values[i])) return OptionalDouble.of(values[i]); } - return 0D; + return OptionalDouble.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/floats/maps/impl/customHash/Float2FloatLinkedOpenCustomHashMap.java b/src/main/java/speiger/src/collections/floats/maps/impl/customHash/Float2FloatLinkedOpenCustomHashMap.java index 5db9928..17ac779 100644 --- a/src/main/java/speiger/src/collections/floats/maps/impl/customHash/Float2FloatLinkedOpenCustomHashMap.java +++ b/src/main/java/speiger/src/collections/floats/maps/impl/customHash/Float2FloatLinkedOpenCustomHashMap.java @@ -4,6 +4,7 @@ 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; @@ -13,6 +14,7 @@ import speiger.src.collections.floats.functions.FloatConsumer; import speiger.src.collections.objects.functions.consumer.ObjectFloatConsumer; import speiger.src.collections.ints.functions.consumer.IntFloatConsumer; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; +import speiger.src.collections.floats.functions.OptionalFloat; import speiger.src.collections.floats.functions.function.FloatPredicate; import speiger.src.collections.floats.functions.consumer.FloatFloatConsumer; import speiger.src.collections.floats.functions.function.FloatFloatUnaryOperator; @@ -664,6 +666,10 @@ public class Float2FloatLinkedOpenCustomHashMap extends Float2FloatOpenCustomHas } private class MapEntrySet extends AbstractObjectSet implements Float2FloatOrderedMap.FastOrderedSet { + @Override + public void addFirst(Float2FloatMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Float2FloatMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Float2FloatMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -829,7 +835,7 @@ public class Float2FloatLinkedOpenCustomHashMap extends Float2FloatOpenCustomHas } @Override - public Float2FloatMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Float2FloatMap.Entry state = null; boolean empty = true; @@ -844,21 +850,21 @@ public class Float2FloatLinkedOpenCustomHashMap extends Float2FloatOpenCustomHas state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Float2FloatMap.Entry 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 @@ -937,10 +943,13 @@ public class Float2FloatLinkedOpenCustomHashMap extends Float2FloatOpenCustomHas public boolean add(float o) { throw new UnsupportedOperationException(); } - + + @Override + public void addFirst(float o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(float o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(float o) { throw new UnsupportedOperationException(); } - @Override public boolean addAndMoveToLast(float o) { throw new UnsupportedOperationException(); } @@ -1083,7 +1092,7 @@ public class Float2FloatLinkedOpenCustomHashMap extends Float2FloatOpenCustomHas } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -1098,19 +1107,19 @@ public class Float2FloatLinkedOpenCustomHashMap extends Float2FloatOpenCustomHas state = operator.applyAsFloat(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0F; + if(size() <= 0) return OptionalFloat.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalFloat.of(keys[index]); index = (int)links[index]; } - return 0F; + return OptionalFloat.empty(); } @Override @@ -1245,7 +1254,7 @@ public class Float2FloatLinkedOpenCustomHashMap extends Float2FloatOpenCustomHas } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -1260,19 +1269,19 @@ public class Float2FloatLinkedOpenCustomHashMap extends Float2FloatOpenCustomHas state = operator.applyAsFloat(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0F; + if(size() <= 0) return OptionalFloat.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalFloat.of(values[index]); index = (int)links[index]; } - return 0F; + return OptionalFloat.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/floats/maps/impl/customHash/Float2FloatOpenCustomHashMap.java b/src/main/java/speiger/src/collections/floats/maps/impl/customHash/Float2FloatOpenCustomHashMap.java index 36ab9ed..9eb0a2a 100644 --- a/src/main/java/speiger/src/collections/floats/maps/impl/customHash/Float2FloatOpenCustomHashMap.java +++ b/src/main/java/speiger/src/collections/floats/maps/impl/customHash/Float2FloatOpenCustomHashMap.java @@ -5,6 +5,7 @@ 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; @@ -17,6 +18,7 @@ import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.floats.functions.consumer.FloatFloatConsumer; import speiger.src.collections.floats.functions.function.FloatUnaryOperator; import speiger.src.collections.floats.functions.function.FloatFloatUnaryOperator; +import speiger.src.collections.floats.functions.OptionalFloat; import speiger.src.collections.floats.functions.function.FloatPredicate; import speiger.src.collections.floats.maps.abstracts.AbstractFloat2FloatMap; import speiger.src.collections.floats.maps.interfaces.Float2FloatMap; @@ -944,7 +946,7 @@ public class Float2FloatOpenCustomHashMap extends AbstractFloat2FloatMap impleme } @Override - public Float2FloatMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Float2FloatMap.Entry state = null; boolean empty = true; @@ -961,25 +963,25 @@ public class Float2FloatOpenCustomHashMap extends AbstractFloat2FloatMap impleme } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Float2FloatMap.Entry 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], 0F)) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1153,7 +1155,7 @@ public class Float2FloatOpenCustomHashMap extends AbstractFloat2FloatMap impleme } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -1170,18 +1172,18 @@ public class Float2FloatOpenCustomHashMap extends AbstractFloat2FloatMap impleme } state = operator.applyAsFloat(state, keys[i]); } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0F; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OptionalFloat.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OptionalFloat.of(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], 0F) && filter.test(keys[i])) return keys[i]; + if(!strategy.equals(keys[i], 0F) && filter.test(keys[i])) return OptionalFloat.of(keys[i]); } - return 0F; + return OptionalFloat.empty(); } @Override @@ -1296,7 +1298,7 @@ public class Float2FloatOpenCustomHashMap extends AbstractFloat2FloatMap impleme } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -1313,18 +1315,18 @@ public class Float2FloatOpenCustomHashMap extends AbstractFloat2FloatMap impleme } state = operator.applyAsFloat(state, values[i]); } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0F; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return OptionalFloat.empty(); + if(containsNull && filter.test(values[nullIndex])) return OptionalFloat.of(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], 0F) && filter.test(values[i])) return values[i]; + if(!strategy.equals(keys[i], 0F) && filter.test(values[i])) return OptionalFloat.of(values[i]); } - return 0F; + return OptionalFloat.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/floats/maps/impl/customHash/Float2IntLinkedOpenCustomHashMap.java b/src/main/java/speiger/src/collections/floats/maps/impl/customHash/Float2IntLinkedOpenCustomHashMap.java index 85131f9..6788e1d 100644 --- a/src/main/java/speiger/src/collections/floats/maps/impl/customHash/Float2IntLinkedOpenCustomHashMap.java +++ b/src/main/java/speiger/src/collections/floats/maps/impl/customHash/Float2IntLinkedOpenCustomHashMap.java @@ -4,10 +4,12 @@ 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; import java.util.function.IntPredicate; +import java.util.OptionalInt; import speiger.src.collections.floats.collections.FloatBidirectionalIterator; import speiger.src.collections.floats.functions.FloatConsumer; @@ -15,6 +17,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectFloatConsumer; import speiger.src.collections.ints.functions.consumer.IntFloatConsumer; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntIntConsumer; +import speiger.src.collections.floats.functions.OptionalFloat; import speiger.src.collections.floats.functions.function.FloatPredicate; import speiger.src.collections.floats.functions.consumer.FloatIntConsumer; import speiger.src.collections.floats.functions.function.FloatFloatUnaryOperator; @@ -671,6 +674,10 @@ public class Float2IntLinkedOpenCustomHashMap extends Float2IntOpenCustomHashMap } private class MapEntrySet extends AbstractObjectSet implements Float2IntOrderedMap.FastOrderedSet { + @Override + public void addFirst(Float2IntMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Float2IntMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Float2IntMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -836,7 +843,7 @@ public class Float2IntLinkedOpenCustomHashMap extends Float2IntOpenCustomHashMap } @Override - public Float2IntMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Float2IntMap.Entry state = null; boolean empty = true; @@ -851,21 +858,21 @@ public class Float2IntLinkedOpenCustomHashMap extends Float2IntOpenCustomHashMap state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Float2IntMap.Entry 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 @@ -944,10 +951,13 @@ public class Float2IntLinkedOpenCustomHashMap extends Float2IntOpenCustomHashMap public boolean add(float o) { throw new UnsupportedOperationException(); } - + + @Override + public void addFirst(float o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(float o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(float o) { throw new UnsupportedOperationException(); } - @Override public boolean addAndMoveToLast(float o) { throw new UnsupportedOperationException(); } @@ -1090,7 +1100,7 @@ public class Float2IntLinkedOpenCustomHashMap extends Float2IntOpenCustomHashMap } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -1105,19 +1115,19 @@ public class Float2IntLinkedOpenCustomHashMap extends Float2IntOpenCustomHashMap state = operator.applyAsFloat(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0F; + if(size() <= 0) return OptionalFloat.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalFloat.of(keys[index]); index = (int)links[index]; } - return 0F; + return OptionalFloat.empty(); } @Override @@ -1252,7 +1262,7 @@ public class Float2IntLinkedOpenCustomHashMap extends Float2IntOpenCustomHashMap } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -1267,19 +1277,19 @@ public class Float2IntLinkedOpenCustomHashMap extends Float2IntOpenCustomHashMap state = operator.applyAsInt(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0; + if(size() <= 0) return OptionalInt.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalInt.of(values[index]); index = (int)links[index]; } - return 0; + return OptionalInt.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/floats/maps/impl/customHash/Float2IntOpenCustomHashMap.java b/src/main/java/speiger/src/collections/floats/maps/impl/customHash/Float2IntOpenCustomHashMap.java index e149b2e..9ee5d9b 100644 --- a/src/main/java/speiger/src/collections/floats/maps/impl/customHash/Float2IntOpenCustomHashMap.java +++ b/src/main/java/speiger/src/collections/floats/maps/impl/customHash/Float2IntOpenCustomHashMap.java @@ -5,10 +5,12 @@ 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; import java.util.function.IntPredicate; +import java.util.OptionalInt; import speiger.src.collections.floats.collections.FloatIterator; import speiger.src.collections.floats.functions.FloatConsumer; @@ -20,6 +22,7 @@ import speiger.src.collections.floats.functions.consumer.FloatIntConsumer; import speiger.src.collections.floats.functions.function.Float2IntFunction; import speiger.src.collections.floats.functions.function.FloatIntUnaryOperator; import speiger.src.collections.floats.functions.function.FloatFloatUnaryOperator; +import speiger.src.collections.floats.functions.OptionalFloat; import speiger.src.collections.floats.functions.function.FloatPredicate; import speiger.src.collections.floats.maps.abstracts.AbstractFloat2IntMap; import speiger.src.collections.floats.maps.interfaces.Float2IntMap; @@ -952,7 +955,7 @@ public class Float2IntOpenCustomHashMap extends AbstractFloat2IntMap implements } @Override - public Float2IntMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Float2IntMap.Entry state = null; boolean empty = true; @@ -969,25 +972,25 @@ public class Float2IntOpenCustomHashMap extends AbstractFloat2IntMap implements } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Float2IntMap.Entry 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], 0F)) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1161,7 +1164,7 @@ public class Float2IntOpenCustomHashMap extends AbstractFloat2IntMap implements } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -1178,18 +1181,18 @@ public class Float2IntOpenCustomHashMap extends AbstractFloat2IntMap implements } state = operator.applyAsFloat(state, keys[i]); } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0F; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OptionalFloat.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OptionalFloat.of(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], 0F) && filter.test(keys[i])) return keys[i]; + if(!strategy.equals(keys[i], 0F) && filter.test(keys[i])) return OptionalFloat.of(keys[i]); } - return 0F; + return OptionalFloat.empty(); } @Override @@ -1304,7 +1307,7 @@ public class Float2IntOpenCustomHashMap extends AbstractFloat2IntMap implements } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -1321,18 +1324,18 @@ public class Float2IntOpenCustomHashMap extends AbstractFloat2IntMap implements } state = operator.applyAsInt(state, values[i]); } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return OptionalInt.empty(); + if(containsNull && filter.test(values[nullIndex])) return OptionalInt.of(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], 0F) && filter.test(values[i])) return values[i]; + if(!strategy.equals(keys[i], 0F) && filter.test(values[i])) return OptionalInt.of(values[i]); } - return 0; + return OptionalInt.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/floats/maps/impl/customHash/Float2LongLinkedOpenCustomHashMap.java b/src/main/java/speiger/src/collections/floats/maps/impl/customHash/Float2LongLinkedOpenCustomHashMap.java index 9913ec3..f722798 100644 --- a/src/main/java/speiger/src/collections/floats/maps/impl/customHash/Float2LongLinkedOpenCustomHashMap.java +++ b/src/main/java/speiger/src/collections/floats/maps/impl/customHash/Float2LongLinkedOpenCustomHashMap.java @@ -4,10 +4,12 @@ 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; import java.util.function.LongPredicate; +import java.util.OptionalLong; import speiger.src.collections.floats.collections.FloatBidirectionalIterator; import speiger.src.collections.floats.functions.FloatConsumer; @@ -15,6 +17,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectFloatConsumer; import speiger.src.collections.ints.functions.consumer.IntFloatConsumer; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntLongConsumer; +import speiger.src.collections.floats.functions.OptionalFloat; import speiger.src.collections.floats.functions.function.FloatPredicate; import speiger.src.collections.floats.functions.consumer.FloatLongConsumer; import speiger.src.collections.floats.functions.function.FloatFloatUnaryOperator; @@ -671,6 +674,10 @@ public class Float2LongLinkedOpenCustomHashMap extends Float2LongOpenCustomHashM } private class MapEntrySet extends AbstractObjectSet implements Float2LongOrderedMap.FastOrderedSet { + @Override + public void addFirst(Float2LongMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Float2LongMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Float2LongMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -836,7 +843,7 @@ public class Float2LongLinkedOpenCustomHashMap extends Float2LongOpenCustomHashM } @Override - public Float2LongMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Float2LongMap.Entry state = null; boolean empty = true; @@ -851,21 +858,21 @@ public class Float2LongLinkedOpenCustomHashMap extends Float2LongOpenCustomHashM state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Float2LongMap.Entry 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 @@ -944,10 +951,13 @@ public class Float2LongLinkedOpenCustomHashMap extends Float2LongOpenCustomHashM public boolean add(float o) { throw new UnsupportedOperationException(); } - + + @Override + public void addFirst(float o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(float o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(float o) { throw new UnsupportedOperationException(); } - @Override public boolean addAndMoveToLast(float o) { throw new UnsupportedOperationException(); } @@ -1090,7 +1100,7 @@ public class Float2LongLinkedOpenCustomHashMap extends Float2LongOpenCustomHashM } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -1105,19 +1115,19 @@ public class Float2LongLinkedOpenCustomHashMap extends Float2LongOpenCustomHashM state = operator.applyAsFloat(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0F; + if(size() <= 0) return OptionalFloat.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalFloat.of(keys[index]); index = (int)links[index]; } - return 0F; + return OptionalFloat.empty(); } @Override @@ -1252,7 +1262,7 @@ public class Float2LongLinkedOpenCustomHashMap extends Float2LongOpenCustomHashM } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -1267,19 +1277,19 @@ public class Float2LongLinkedOpenCustomHashMap extends Float2LongOpenCustomHashM state = operator.applyAsLong(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0L; + if(size() <= 0) return OptionalLong.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalLong.of(values[index]); index = (int)links[index]; } - return 0L; + return OptionalLong.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/floats/maps/impl/customHash/Float2LongOpenCustomHashMap.java b/src/main/java/speiger/src/collections/floats/maps/impl/customHash/Float2LongOpenCustomHashMap.java index 1ade7ff..9eabb3e 100644 --- a/src/main/java/speiger/src/collections/floats/maps/impl/customHash/Float2LongOpenCustomHashMap.java +++ b/src/main/java/speiger/src/collections/floats/maps/impl/customHash/Float2LongOpenCustomHashMap.java @@ -5,10 +5,12 @@ 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; import java.util.function.LongPredicate; +import java.util.OptionalLong; import speiger.src.collections.floats.collections.FloatIterator; import speiger.src.collections.floats.functions.FloatConsumer; @@ -20,6 +22,7 @@ import speiger.src.collections.floats.functions.consumer.FloatLongConsumer; import speiger.src.collections.floats.functions.function.Float2LongFunction; import speiger.src.collections.floats.functions.function.FloatLongUnaryOperator; import speiger.src.collections.floats.functions.function.FloatFloatUnaryOperator; +import speiger.src.collections.floats.functions.OptionalFloat; import speiger.src.collections.floats.functions.function.FloatPredicate; import speiger.src.collections.floats.maps.abstracts.AbstractFloat2LongMap; import speiger.src.collections.floats.maps.interfaces.Float2LongMap; @@ -952,7 +955,7 @@ public class Float2LongOpenCustomHashMap extends AbstractFloat2LongMap implement } @Override - public Float2LongMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Float2LongMap.Entry state = null; boolean empty = true; @@ -969,25 +972,25 @@ public class Float2LongOpenCustomHashMap extends AbstractFloat2LongMap implement } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Float2LongMap.Entry 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], 0F)) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1161,7 +1164,7 @@ public class Float2LongOpenCustomHashMap extends AbstractFloat2LongMap implement } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -1178,18 +1181,18 @@ public class Float2LongOpenCustomHashMap extends AbstractFloat2LongMap implement } state = operator.applyAsFloat(state, keys[i]); } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0F; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OptionalFloat.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OptionalFloat.of(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], 0F) && filter.test(keys[i])) return keys[i]; + if(!strategy.equals(keys[i], 0F) && filter.test(keys[i])) return OptionalFloat.of(keys[i]); } - return 0F; + return OptionalFloat.empty(); } @Override @@ -1304,7 +1307,7 @@ public class Float2LongOpenCustomHashMap extends AbstractFloat2LongMap implement } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -1321,18 +1324,18 @@ public class Float2LongOpenCustomHashMap extends AbstractFloat2LongMap implement } state = operator.applyAsLong(state, values[i]); } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0L; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return OptionalLong.empty(); + if(containsNull && filter.test(values[nullIndex])) return OptionalLong.of(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], 0F) && filter.test(values[i])) return values[i]; + if(!strategy.equals(keys[i], 0F) && filter.test(values[i])) return OptionalLong.of(values[i]); } - return 0L; + return OptionalLong.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/floats/maps/impl/customHash/Float2ObjectLinkedOpenCustomHashMap.java b/src/main/java/speiger/src/collections/floats/maps/impl/customHash/Float2ObjectLinkedOpenCustomHashMap.java index 751d00c..fc480b5 100644 --- a/src/main/java/speiger/src/collections/floats/maps/impl/customHash/Float2ObjectLinkedOpenCustomHashMap.java +++ b/src/main/java/speiger/src/collections/floats/maps/impl/customHash/Float2ObjectLinkedOpenCustomHashMap.java @@ -4,6 +4,7 @@ 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; @@ -13,6 +14,7 @@ import speiger.src.collections.floats.functions.FloatConsumer; import speiger.src.collections.objects.functions.consumer.ObjectFloatConsumer; import speiger.src.collections.ints.functions.consumer.IntFloatConsumer; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; +import speiger.src.collections.floats.functions.OptionalFloat; import speiger.src.collections.floats.functions.function.FloatPredicate; import speiger.src.collections.floats.functions.consumer.FloatObjectConsumer; import speiger.src.collections.floats.functions.function.FloatFloatUnaryOperator; @@ -666,6 +668,10 @@ public class Float2ObjectLinkedOpenCustomHashMap extends Float2ObjectOpenCust } private class MapEntrySet extends AbstractObjectSet> implements Float2ObjectOrderedMap.FastOrderedSet { + @Override + public void addFirst(Float2ObjectMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Float2ObjectMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Float2ObjectMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -831,7 +837,7 @@ public class Float2ObjectLinkedOpenCustomHashMap extends Float2ObjectOpenCust } @Override - public Float2ObjectMap.Entry reduce(ObjectObjectUnaryOperator, Float2ObjectMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Float2ObjectMap.Entry> operator) { Objects.requireNonNull(operator); Float2ObjectMap.Entry state = null; boolean empty = true; @@ -846,21 +852,21 @@ public class Float2ObjectLinkedOpenCustomHashMap extends Float2ObjectOpenCust state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Float2ObjectMap.Entry 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 @@ -939,10 +945,13 @@ public class Float2ObjectLinkedOpenCustomHashMap extends Float2ObjectOpenCust public boolean add(float o) { throw new UnsupportedOperationException(); } - + + @Override + public void addFirst(float o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(float o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(float o) { throw new UnsupportedOperationException(); } - @Override public boolean addAndMoveToLast(float o) { throw new UnsupportedOperationException(); } @@ -1085,7 +1094,7 @@ public class Float2ObjectLinkedOpenCustomHashMap extends Float2ObjectOpenCust } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -1100,19 +1109,19 @@ public class Float2ObjectLinkedOpenCustomHashMap extends Float2ObjectOpenCust state = operator.applyAsFloat(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0F; + if(size() <= 0) return OptionalFloat.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalFloat.of(keys[index]); index = (int)links[index]; } - return 0F; + return OptionalFloat.empty(); } @Override @@ -1248,7 +1257,7 @@ public class Float2ObjectLinkedOpenCustomHashMap extends Float2ObjectOpenCust } @Override - public V reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); V state = null; boolean empty = true; @@ -1263,19 +1272,19 @@ public class Float2ObjectLinkedOpenCustomHashMap extends Float2ObjectOpenCust state = operator.apply(state, values[index]); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public V findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return Optional.ofNullable(values[index]); index = (int)links[index]; } - return null; + return Optional.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/floats/maps/impl/customHash/Float2ObjectOpenCustomHashMap.java b/src/main/java/speiger/src/collections/floats/maps/impl/customHash/Float2ObjectOpenCustomHashMap.java index 3abe68e..2b65717 100644 --- a/src/main/java/speiger/src/collections/floats/maps/impl/customHash/Float2ObjectOpenCustomHashMap.java +++ b/src/main/java/speiger/src/collections/floats/maps/impl/customHash/Float2ObjectOpenCustomHashMap.java @@ -5,6 +5,7 @@ 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; @@ -18,6 +19,7 @@ import speiger.src.collections.floats.functions.consumer.FloatObjectConsumer; import speiger.src.collections.floats.functions.function.FloatFunction; import speiger.src.collections.floats.functions.function.FloatObjectUnaryOperator; import speiger.src.collections.floats.functions.function.FloatFloatUnaryOperator; +import speiger.src.collections.floats.functions.OptionalFloat; import speiger.src.collections.floats.functions.function.FloatPredicate; import speiger.src.collections.floats.maps.abstracts.AbstractFloat2ObjectMap; import speiger.src.collections.floats.maps.interfaces.Float2ObjectMap; @@ -868,7 +870,7 @@ public class Float2ObjectOpenCustomHashMap extends AbstractFloat2ObjectMap } @Override - public Float2ObjectMap.Entry reduce(ObjectObjectUnaryOperator, Float2ObjectMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Float2ObjectMap.Entry> operator) { Objects.requireNonNull(operator); Float2ObjectMap.Entry state = null; boolean empty = true; @@ -885,25 +887,25 @@ public class Float2ObjectOpenCustomHashMap extends AbstractFloat2ObjectMap } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Float2ObjectMap.Entry 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], 0F)) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1077,7 +1079,7 @@ public class Float2ObjectOpenCustomHashMap extends AbstractFloat2ObjectMap } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -1094,18 +1096,18 @@ public class Float2ObjectOpenCustomHashMap extends AbstractFloat2ObjectMap } state = operator.applyAsFloat(state, keys[i]); } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0F; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OptionalFloat.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OptionalFloat.of(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], 0F) && filter.test(keys[i])) return keys[i]; + if(!strategy.equals(keys[i], 0F) && filter.test(keys[i])) return OptionalFloat.of(keys[i]); } - return 0F; + return OptionalFloat.empty(); } @Override @@ -1220,7 +1222,7 @@ public class Float2ObjectOpenCustomHashMap extends AbstractFloat2ObjectMap } @Override - public V reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); V state = null; boolean empty = true; @@ -1237,18 +1239,18 @@ public class Float2ObjectOpenCustomHashMap extends AbstractFloat2ObjectMap } state = operator.apply(state, values[i]); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public V findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return Optional.empty(); + if(containsNull && filter.test(values[nullIndex])) return Optional.ofNullable(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], 0F) && filter.test(values[i])) return values[i]; + if(!strategy.equals(keys[i], 0F) && filter.test(values[i])) return Optional.ofNullable(values[i]); } - return null; + return Optional.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/floats/maps/impl/customHash/Float2ShortLinkedOpenCustomHashMap.java b/src/main/java/speiger/src/collections/floats/maps/impl/customHash/Float2ShortLinkedOpenCustomHashMap.java index e0ec186..edc2daa 100644 --- a/src/main/java/speiger/src/collections/floats/maps/impl/customHash/Float2ShortLinkedOpenCustomHashMap.java +++ b/src/main/java/speiger/src/collections/floats/maps/impl/customHash/Float2ShortLinkedOpenCustomHashMap.java @@ -4,6 +4,7 @@ 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; @@ -14,6 +15,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectFloatConsumer; import speiger.src.collections.ints.functions.consumer.IntFloatConsumer; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntShortConsumer; +import speiger.src.collections.floats.functions.OptionalFloat; import speiger.src.collections.floats.functions.function.FloatPredicate; import speiger.src.collections.floats.functions.consumer.FloatShortConsumer; import speiger.src.collections.floats.functions.function.FloatFloatUnaryOperator; @@ -33,6 +35,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectShortConsumer; import speiger.src.collections.shorts.functions.function.ShortPredicate; +import speiger.src.collections.shorts.functions.OptionalShort; import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOperator; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.objects.lists.ObjectListIterator; @@ -671,6 +674,10 @@ public class Float2ShortLinkedOpenCustomHashMap extends Float2ShortOpenCustomHas } private class MapEntrySet extends AbstractObjectSet implements Float2ShortOrderedMap.FastOrderedSet { + @Override + public void addFirst(Float2ShortMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Float2ShortMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Float2ShortMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -836,7 +843,7 @@ public class Float2ShortLinkedOpenCustomHashMap extends Float2ShortOpenCustomHas } @Override - public Float2ShortMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Float2ShortMap.Entry state = null; boolean empty = true; @@ -851,21 +858,21 @@ public class Float2ShortLinkedOpenCustomHashMap extends Float2ShortOpenCustomHas state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Float2ShortMap.Entry 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 @@ -944,10 +951,13 @@ public class Float2ShortLinkedOpenCustomHashMap extends Float2ShortOpenCustomHas public boolean add(float o) { throw new UnsupportedOperationException(); } - + + @Override + public void addFirst(float o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(float o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(float o) { throw new UnsupportedOperationException(); } - @Override public boolean addAndMoveToLast(float o) { throw new UnsupportedOperationException(); } @@ -1090,7 +1100,7 @@ public class Float2ShortLinkedOpenCustomHashMap extends Float2ShortOpenCustomHas } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -1105,19 +1115,19 @@ public class Float2ShortLinkedOpenCustomHashMap extends Float2ShortOpenCustomHas state = operator.applyAsFloat(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0F; + if(size() <= 0) return OptionalFloat.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalFloat.of(keys[index]); index = (int)links[index]; } - return 0F; + return OptionalFloat.empty(); } @Override @@ -1252,7 +1262,7 @@ public class Float2ShortLinkedOpenCustomHashMap extends Float2ShortOpenCustomHas } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -1267,19 +1277,19 @@ public class Float2ShortLinkedOpenCustomHashMap extends Float2ShortOpenCustomHas state = operator.applyAsShort(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (short)0; + if(size() <= 0) return OptionalShort.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalShort.of(values[index]); index = (int)links[index]; } - return (short)0; + return OptionalShort.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/floats/maps/impl/customHash/Float2ShortOpenCustomHashMap.java b/src/main/java/speiger/src/collections/floats/maps/impl/customHash/Float2ShortOpenCustomHashMap.java index 5272af1..21a5fc9 100644 --- a/src/main/java/speiger/src/collections/floats/maps/impl/customHash/Float2ShortOpenCustomHashMap.java +++ b/src/main/java/speiger/src/collections/floats/maps/impl/customHash/Float2ShortOpenCustomHashMap.java @@ -5,6 +5,7 @@ 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; @@ -19,6 +20,7 @@ import speiger.src.collections.floats.functions.consumer.FloatShortConsumer; import speiger.src.collections.floats.functions.function.Float2ShortFunction; import speiger.src.collections.floats.functions.function.FloatShortUnaryOperator; import speiger.src.collections.floats.functions.function.FloatFloatUnaryOperator; +import speiger.src.collections.floats.functions.OptionalFloat; import speiger.src.collections.floats.functions.function.FloatPredicate; import speiger.src.collections.floats.maps.abstracts.AbstractFloat2ShortMap; import speiger.src.collections.floats.maps.interfaces.Float2ShortMap; @@ -37,6 +39,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectShortConsumer; import speiger.src.collections.shorts.functions.function.ShortPredicate; +import speiger.src.collections.shorts.functions.OptionalShort; import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.sets.AbstractObjectSet; import speiger.src.collections.objects.sets.ObjectSet; @@ -952,7 +955,7 @@ public class Float2ShortOpenCustomHashMap extends AbstractFloat2ShortMap impleme } @Override - public Float2ShortMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Float2ShortMap.Entry state = null; boolean empty = true; @@ -969,25 +972,25 @@ public class Float2ShortOpenCustomHashMap extends AbstractFloat2ShortMap impleme } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Float2ShortMap.Entry 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], 0F)) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1161,7 +1164,7 @@ public class Float2ShortOpenCustomHashMap extends AbstractFloat2ShortMap impleme } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -1178,18 +1181,18 @@ public class Float2ShortOpenCustomHashMap extends AbstractFloat2ShortMap impleme } state = operator.applyAsFloat(state, keys[i]); } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0F; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OptionalFloat.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OptionalFloat.of(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], 0F) && filter.test(keys[i])) return keys[i]; + if(!strategy.equals(keys[i], 0F) && filter.test(keys[i])) return OptionalFloat.of(keys[i]); } - return 0F; + return OptionalFloat.empty(); } @Override @@ -1304,7 +1307,7 @@ public class Float2ShortOpenCustomHashMap extends AbstractFloat2ShortMap impleme } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -1321,18 +1324,18 @@ public class Float2ShortOpenCustomHashMap extends AbstractFloat2ShortMap impleme } state = operator.applyAsShort(state, values[i]); } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (short)0; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return OptionalShort.empty(); + if(containsNull && filter.test(values[nullIndex])) return OptionalShort.of(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], 0F) && filter.test(values[i])) return values[i]; + if(!strategy.equals(keys[i], 0F) && filter.test(values[i])) return OptionalShort.of(values[i]); } - return (short)0; + return OptionalShort.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/floats/maps/impl/hash/Float2BooleanLinkedOpenHashMap.java b/src/main/java/speiger/src/collections/floats/maps/impl/hash/Float2BooleanLinkedOpenHashMap.java index 3f81e58..60240a7 100644 --- a/src/main/java/speiger/src/collections/floats/maps/impl/hash/Float2BooleanLinkedOpenHashMap.java +++ b/src/main/java/speiger/src/collections/floats/maps/impl/hash/Float2BooleanLinkedOpenHashMap.java @@ -4,6 +4,7 @@ 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; @@ -14,6 +15,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectFloatConsumer; import speiger.src.collections.ints.functions.consumer.IntFloatConsumer; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntBooleanConsumer; +import speiger.src.collections.floats.functions.OptionalFloat; import speiger.src.collections.floats.functions.function.FloatPredicate; import speiger.src.collections.floats.functions.consumer.FloatBooleanConsumer; import speiger.src.collections.floats.functions.function.FloatFloatUnaryOperator; @@ -32,6 +34,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectBooleanConsumer; import speiger.src.collections.booleans.functions.function.BooleanPredicate; +import speiger.src.collections.booleans.functions.OptionalBoolean; import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOperator; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.objects.lists.ObjectListIterator; @@ -669,6 +672,10 @@ public class Float2BooleanLinkedOpenHashMap extends Float2BooleanOpenHashMap imp } private class MapEntrySet extends AbstractObjectSet implements Float2BooleanOrderedMap.FastOrderedSet { + @Override + public void addFirst(Float2BooleanMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Float2BooleanMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Float2BooleanMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -834,7 +841,7 @@ public class Float2BooleanLinkedOpenHashMap extends Float2BooleanOpenHashMap imp } @Override - public Float2BooleanMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Float2BooleanMap.Entry state = null; boolean empty = true; @@ -849,21 +856,21 @@ public class Float2BooleanLinkedOpenHashMap extends Float2BooleanOpenHashMap imp state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Float2BooleanMap.Entry 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 @@ -939,6 +946,12 @@ public class Float2BooleanLinkedOpenHashMap extends Float2BooleanOpenHashMap imp @Override public boolean add(float o) { throw new UnsupportedOperationException(); } + + @Override + public void addFirst(float o) { throw new UnsupportedOperationException(); } + + @Override + public void addLast(float o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(float o) { throw new UnsupportedOperationException(); } @@ -1085,7 +1098,7 @@ public class Float2BooleanLinkedOpenHashMap extends Float2BooleanOpenHashMap imp } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -1100,19 +1113,19 @@ public class Float2BooleanLinkedOpenHashMap extends Float2BooleanOpenHashMap imp state = operator.applyAsFloat(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0F; + if(size() <= 0) return OptionalFloat.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalFloat.of(keys[index]); index = (int)links[index]; } - return 0F; + return OptionalFloat.empty(); } @Override @@ -1248,7 +1261,7 @@ public class Float2BooleanLinkedOpenHashMap extends Float2BooleanOpenHashMap imp } @Override - public boolean reduce(BooleanBooleanUnaryOperator operator) { + public OptionalBoolean reduce(BooleanBooleanUnaryOperator operator) { Objects.requireNonNull(operator); boolean state = false; boolean empty = true; @@ -1263,19 +1276,19 @@ public class Float2BooleanLinkedOpenHashMap extends Float2BooleanOpenHashMap imp state = operator.applyAsBoolean(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalBoolean.empty() : OptionalBoolean.of(state); } @Override - public boolean findFirst(BooleanPredicate filter) { + public OptionalBoolean findFirst(BooleanPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return false; + if(size() <= 0) return OptionalBoolean.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalBoolean.of(values[index]); index = (int)links[index]; } - return false; + return OptionalBoolean.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/floats/maps/impl/hash/Float2BooleanOpenHashMap.java b/src/main/java/speiger/src/collections/floats/maps/impl/hash/Float2BooleanOpenHashMap.java index 44cc712..48f2d7e 100644 --- a/src/main/java/speiger/src/collections/floats/maps/impl/hash/Float2BooleanOpenHashMap.java +++ b/src/main/java/speiger/src/collections/floats/maps/impl/hash/Float2BooleanOpenHashMap.java @@ -5,6 +5,7 @@ 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; @@ -19,6 +20,7 @@ import speiger.src.collections.floats.functions.consumer.FloatBooleanConsumer; import speiger.src.collections.floats.functions.function.FloatPredicate; import speiger.src.collections.floats.functions.function.FloatBooleanUnaryOperator; import speiger.src.collections.floats.functions.function.FloatFloatUnaryOperator; +import speiger.src.collections.floats.functions.OptionalFloat; import speiger.src.collections.floats.maps.abstracts.AbstractFloat2BooleanMap; import speiger.src.collections.floats.maps.interfaces.Float2BooleanMap; import speiger.src.collections.floats.sets.AbstractFloatSet; @@ -33,6 +35,7 @@ import speiger.src.collections.booleans.functions.BooleanConsumer; import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectBooleanConsumer; import speiger.src.collections.booleans.functions.function.BooleanPredicate; +import speiger.src.collections.booleans.functions.OptionalBoolean; import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.sets.AbstractObjectSet; import speiger.src.collections.objects.sets.ObjectSet; @@ -898,7 +901,7 @@ public class Float2BooleanOpenHashMap extends AbstractFloat2BooleanMap implement } @Override - public Float2BooleanMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Float2BooleanMap.Entry state = null; boolean empty = true; @@ -915,25 +918,25 @@ public class Float2BooleanOpenHashMap extends AbstractFloat2BooleanMap implement } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Float2BooleanMap.Entry 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(Float.floatToIntBits(keys[i]) != 0) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1105,7 +1108,7 @@ public class Float2BooleanOpenHashMap extends AbstractFloat2BooleanMap implement } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -1122,18 +1125,18 @@ public class Float2BooleanOpenHashMap extends AbstractFloat2BooleanMap implement } state = operator.applyAsFloat(state, keys[i]); } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0F; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OptionalFloat.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OptionalFloat.of(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(Float.floatToIntBits(keys[i]) != 0 && filter.test(keys[i])) return keys[i]; + if(Float.floatToIntBits(keys[i]) != 0 && filter.test(keys[i])) return OptionalFloat.of(keys[i]); } - return 0F; + return OptionalFloat.empty(); } @Override @@ -1248,7 +1251,7 @@ public class Float2BooleanOpenHashMap extends AbstractFloat2BooleanMap implement } @Override - public boolean reduce(BooleanBooleanUnaryOperator operator) { + public OptionalBoolean reduce(BooleanBooleanUnaryOperator operator) { Objects.requireNonNull(operator); boolean state = false; boolean empty = true; @@ -1265,18 +1268,18 @@ public class Float2BooleanOpenHashMap extends AbstractFloat2BooleanMap implement } state = operator.applyAsBoolean(state, values[i]); } - return state; + return empty ? OptionalBoolean.empty() : OptionalBoolean.of(state); } @Override - public boolean findFirst(BooleanPredicate filter) { + public OptionalBoolean findFirst(BooleanPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return false; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return OptionalBoolean.empty(); + if(containsNull && filter.test(values[nullIndex])) return OptionalBoolean.of(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(Float.floatToIntBits(keys[i]) != 0 && filter.test(values[i])) return values[i]; + if(Float.floatToIntBits(keys[i]) != 0 && filter.test(values[i])) return OptionalBoolean.of(values[i]); } - return false; + return OptionalBoolean.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/floats/maps/impl/hash/Float2ByteLinkedOpenHashMap.java b/src/main/java/speiger/src/collections/floats/maps/impl/hash/Float2ByteLinkedOpenHashMap.java index 23dbc14..4c48c90 100644 --- a/src/main/java/speiger/src/collections/floats/maps/impl/hash/Float2ByteLinkedOpenHashMap.java +++ b/src/main/java/speiger/src/collections/floats/maps/impl/hash/Float2ByteLinkedOpenHashMap.java @@ -4,6 +4,7 @@ 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; @@ -14,6 +15,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectFloatConsumer; import speiger.src.collections.ints.functions.consumer.IntFloatConsumer; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntByteConsumer; +import speiger.src.collections.floats.functions.OptionalFloat; import speiger.src.collections.floats.functions.function.FloatPredicate; import speiger.src.collections.floats.functions.consumer.FloatByteConsumer; import speiger.src.collections.floats.functions.function.FloatFloatUnaryOperator; @@ -32,6 +34,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectByteConsumer; import speiger.src.collections.bytes.functions.function.BytePredicate; +import speiger.src.collections.bytes.functions.OptionalByte; import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOperator; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.objects.lists.ObjectListIterator; @@ -669,6 +672,10 @@ public class Float2ByteLinkedOpenHashMap extends Float2ByteOpenHashMap implement } private class MapEntrySet extends AbstractObjectSet implements Float2ByteOrderedMap.FastOrderedSet { + @Override + public void addFirst(Float2ByteMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Float2ByteMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Float2ByteMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -834,7 +841,7 @@ public class Float2ByteLinkedOpenHashMap extends Float2ByteOpenHashMap implement } @Override - public Float2ByteMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Float2ByteMap.Entry state = null; boolean empty = true; @@ -849,21 +856,21 @@ public class Float2ByteLinkedOpenHashMap extends Float2ByteOpenHashMap implement state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Float2ByteMap.Entry 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 @@ -939,6 +946,12 @@ public class Float2ByteLinkedOpenHashMap extends Float2ByteOpenHashMap implement @Override public boolean add(float o) { throw new UnsupportedOperationException(); } + + @Override + public void addFirst(float o) { throw new UnsupportedOperationException(); } + + @Override + public void addLast(float o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(float o) { throw new UnsupportedOperationException(); } @@ -1085,7 +1098,7 @@ public class Float2ByteLinkedOpenHashMap extends Float2ByteOpenHashMap implement } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -1100,19 +1113,19 @@ public class Float2ByteLinkedOpenHashMap extends Float2ByteOpenHashMap implement state = operator.applyAsFloat(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0F; + if(size() <= 0) return OptionalFloat.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalFloat.of(keys[index]); index = (int)links[index]; } - return 0F; + return OptionalFloat.empty(); } @Override @@ -1248,7 +1261,7 @@ public class Float2ByteLinkedOpenHashMap extends Float2ByteOpenHashMap implement } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -1263,19 +1276,19 @@ public class Float2ByteLinkedOpenHashMap extends Float2ByteOpenHashMap implement state = operator.applyAsByte(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (byte)0; + if(size() <= 0) return OptionalByte.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalByte.of(values[index]); index = (int)links[index]; } - return (byte)0; + return OptionalByte.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/floats/maps/impl/hash/Float2ByteOpenHashMap.java b/src/main/java/speiger/src/collections/floats/maps/impl/hash/Float2ByteOpenHashMap.java index 136f26c..a69d6d0 100644 --- a/src/main/java/speiger/src/collections/floats/maps/impl/hash/Float2ByteOpenHashMap.java +++ b/src/main/java/speiger/src/collections/floats/maps/impl/hash/Float2ByteOpenHashMap.java @@ -5,6 +5,7 @@ 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; @@ -19,6 +20,7 @@ import speiger.src.collections.floats.functions.consumer.FloatByteConsumer; import speiger.src.collections.floats.functions.function.Float2ByteFunction; import speiger.src.collections.floats.functions.function.FloatByteUnaryOperator; import speiger.src.collections.floats.functions.function.FloatFloatUnaryOperator; +import speiger.src.collections.floats.functions.OptionalFloat; import speiger.src.collections.floats.functions.function.FloatPredicate; import speiger.src.collections.floats.maps.abstracts.AbstractFloat2ByteMap; import speiger.src.collections.floats.maps.interfaces.Float2ByteMap; @@ -34,6 +36,7 @@ import speiger.src.collections.bytes.functions.ByteConsumer; import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectByteConsumer; import speiger.src.collections.bytes.functions.function.BytePredicate; +import speiger.src.collections.bytes.functions.OptionalByte; import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.sets.AbstractObjectSet; import speiger.src.collections.objects.sets.ObjectSet; @@ -920,7 +923,7 @@ public class Float2ByteOpenHashMap extends AbstractFloat2ByteMap implements ITri } @Override - public Float2ByteMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Float2ByteMap.Entry state = null; boolean empty = true; @@ -937,25 +940,25 @@ public class Float2ByteOpenHashMap extends AbstractFloat2ByteMap implements ITri } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Float2ByteMap.Entry 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(Float.floatToIntBits(keys[i]) != 0) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1127,7 +1130,7 @@ public class Float2ByteOpenHashMap extends AbstractFloat2ByteMap implements ITri } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -1144,18 +1147,18 @@ public class Float2ByteOpenHashMap extends AbstractFloat2ByteMap implements ITri } state = operator.applyAsFloat(state, keys[i]); } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0F; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OptionalFloat.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OptionalFloat.of(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(Float.floatToIntBits(keys[i]) != 0 && filter.test(keys[i])) return keys[i]; + if(Float.floatToIntBits(keys[i]) != 0 && filter.test(keys[i])) return OptionalFloat.of(keys[i]); } - return 0F; + return OptionalFloat.empty(); } @Override @@ -1270,7 +1273,7 @@ public class Float2ByteOpenHashMap extends AbstractFloat2ByteMap implements ITri } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -1287,18 +1290,18 @@ public class Float2ByteOpenHashMap extends AbstractFloat2ByteMap implements ITri } state = operator.applyAsByte(state, values[i]); } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (byte)0; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return OptionalByte.empty(); + if(containsNull && filter.test(values[nullIndex])) return OptionalByte.of(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(Float.floatToIntBits(keys[i]) != 0 && filter.test(values[i])) return values[i]; + if(Float.floatToIntBits(keys[i]) != 0 && filter.test(values[i])) return OptionalByte.of(values[i]); } - return (byte)0; + return OptionalByte.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/floats/maps/impl/hash/Float2CharLinkedOpenHashMap.java b/src/main/java/speiger/src/collections/floats/maps/impl/hash/Float2CharLinkedOpenHashMap.java index 44040ff..e097e61 100644 --- a/src/main/java/speiger/src/collections/floats/maps/impl/hash/Float2CharLinkedOpenHashMap.java +++ b/src/main/java/speiger/src/collections/floats/maps/impl/hash/Float2CharLinkedOpenHashMap.java @@ -4,6 +4,7 @@ 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; @@ -14,6 +15,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectFloatConsumer; import speiger.src.collections.ints.functions.consumer.IntFloatConsumer; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntCharConsumer; +import speiger.src.collections.floats.functions.OptionalFloat; import speiger.src.collections.floats.functions.function.FloatPredicate; import speiger.src.collections.floats.functions.consumer.FloatCharConsumer; import speiger.src.collections.floats.functions.function.FloatFloatUnaryOperator; @@ -32,6 +34,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectCharConsumer; import speiger.src.collections.chars.functions.function.CharPredicate; +import speiger.src.collections.chars.functions.OptionalChar; import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOperator; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.objects.lists.ObjectListIterator; @@ -669,6 +672,10 @@ public class Float2CharLinkedOpenHashMap extends Float2CharOpenHashMap implement } private class MapEntrySet extends AbstractObjectSet implements Float2CharOrderedMap.FastOrderedSet { + @Override + public void addFirst(Float2CharMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Float2CharMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Float2CharMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -834,7 +841,7 @@ public class Float2CharLinkedOpenHashMap extends Float2CharOpenHashMap implement } @Override - public Float2CharMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Float2CharMap.Entry state = null; boolean empty = true; @@ -849,21 +856,21 @@ public class Float2CharLinkedOpenHashMap extends Float2CharOpenHashMap implement state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Float2CharMap.Entry 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 @@ -939,6 +946,12 @@ public class Float2CharLinkedOpenHashMap extends Float2CharOpenHashMap implement @Override public boolean add(float o) { throw new UnsupportedOperationException(); } + + @Override + public void addFirst(float o) { throw new UnsupportedOperationException(); } + + @Override + public void addLast(float o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(float o) { throw new UnsupportedOperationException(); } @@ -1085,7 +1098,7 @@ public class Float2CharLinkedOpenHashMap extends Float2CharOpenHashMap implement } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -1100,19 +1113,19 @@ public class Float2CharLinkedOpenHashMap extends Float2CharOpenHashMap implement state = operator.applyAsFloat(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0F; + if(size() <= 0) return OptionalFloat.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalFloat.of(keys[index]); index = (int)links[index]; } - return 0F; + return OptionalFloat.empty(); } @Override @@ -1248,7 +1261,7 @@ public class Float2CharLinkedOpenHashMap extends Float2CharOpenHashMap implement } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -1263,19 +1276,19 @@ public class Float2CharLinkedOpenHashMap extends Float2CharOpenHashMap implement state = operator.applyAsChar(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (char)0; + if(size() <= 0) return OptionalChar.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalChar.of(values[index]); index = (int)links[index]; } - return (char)0; + return OptionalChar.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/floats/maps/impl/hash/Float2CharOpenHashMap.java b/src/main/java/speiger/src/collections/floats/maps/impl/hash/Float2CharOpenHashMap.java index 998e9a4..3326771 100644 --- a/src/main/java/speiger/src/collections/floats/maps/impl/hash/Float2CharOpenHashMap.java +++ b/src/main/java/speiger/src/collections/floats/maps/impl/hash/Float2CharOpenHashMap.java @@ -5,6 +5,7 @@ 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; @@ -19,6 +20,7 @@ import speiger.src.collections.floats.functions.consumer.FloatCharConsumer; import speiger.src.collections.floats.functions.function.Float2CharFunction; import speiger.src.collections.floats.functions.function.FloatCharUnaryOperator; import speiger.src.collections.floats.functions.function.FloatFloatUnaryOperator; +import speiger.src.collections.floats.functions.OptionalFloat; import speiger.src.collections.floats.functions.function.FloatPredicate; import speiger.src.collections.floats.maps.abstracts.AbstractFloat2CharMap; import speiger.src.collections.floats.maps.interfaces.Float2CharMap; @@ -34,6 +36,7 @@ import speiger.src.collections.chars.functions.CharConsumer; import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectCharConsumer; import speiger.src.collections.chars.functions.function.CharPredicate; +import speiger.src.collections.chars.functions.OptionalChar; import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.sets.AbstractObjectSet; import speiger.src.collections.objects.sets.ObjectSet; @@ -920,7 +923,7 @@ public class Float2CharOpenHashMap extends AbstractFloat2CharMap implements ITri } @Override - public Float2CharMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Float2CharMap.Entry state = null; boolean empty = true; @@ -937,25 +940,25 @@ public class Float2CharOpenHashMap extends AbstractFloat2CharMap implements ITri } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Float2CharMap.Entry 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(Float.floatToIntBits(keys[i]) != 0) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1127,7 +1130,7 @@ public class Float2CharOpenHashMap extends AbstractFloat2CharMap implements ITri } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -1144,18 +1147,18 @@ public class Float2CharOpenHashMap extends AbstractFloat2CharMap implements ITri } state = operator.applyAsFloat(state, keys[i]); } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0F; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OptionalFloat.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OptionalFloat.of(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(Float.floatToIntBits(keys[i]) != 0 && filter.test(keys[i])) return keys[i]; + if(Float.floatToIntBits(keys[i]) != 0 && filter.test(keys[i])) return OptionalFloat.of(keys[i]); } - return 0F; + return OptionalFloat.empty(); } @Override @@ -1270,7 +1273,7 @@ public class Float2CharOpenHashMap extends AbstractFloat2CharMap implements ITri } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -1287,18 +1290,18 @@ public class Float2CharOpenHashMap extends AbstractFloat2CharMap implements ITri } state = operator.applyAsChar(state, values[i]); } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (char)0; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return OptionalChar.empty(); + if(containsNull && filter.test(values[nullIndex])) return OptionalChar.of(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(Float.floatToIntBits(keys[i]) != 0 && filter.test(values[i])) return values[i]; + if(Float.floatToIntBits(keys[i]) != 0 && filter.test(values[i])) return OptionalChar.of(values[i]); } - return (char)0; + return OptionalChar.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/floats/maps/impl/hash/Float2DoubleLinkedOpenHashMap.java b/src/main/java/speiger/src/collections/floats/maps/impl/hash/Float2DoubleLinkedOpenHashMap.java index aeff47d..d672c95 100644 --- a/src/main/java/speiger/src/collections/floats/maps/impl/hash/Float2DoubleLinkedOpenHashMap.java +++ b/src/main/java/speiger/src/collections/floats/maps/impl/hash/Float2DoubleLinkedOpenHashMap.java @@ -4,10 +4,12 @@ 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; import java.util.function.DoublePredicate; +import java.util.OptionalDouble; import speiger.src.collections.floats.collections.FloatBidirectionalIterator; import speiger.src.collections.floats.functions.FloatConsumer; @@ -15,6 +17,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectFloatConsumer; import speiger.src.collections.ints.functions.consumer.IntFloatConsumer; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntDoubleConsumer; +import speiger.src.collections.floats.functions.OptionalFloat; import speiger.src.collections.floats.functions.function.FloatPredicate; import speiger.src.collections.floats.functions.consumer.FloatDoubleConsumer; import speiger.src.collections.floats.functions.function.FloatFloatUnaryOperator; @@ -669,6 +672,10 @@ public class Float2DoubleLinkedOpenHashMap extends Float2DoubleOpenHashMap imple } private class MapEntrySet extends AbstractObjectSet implements Float2DoubleOrderedMap.FastOrderedSet { + @Override + public void addFirst(Float2DoubleMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Float2DoubleMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Float2DoubleMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -834,7 +841,7 @@ public class Float2DoubleLinkedOpenHashMap extends Float2DoubleOpenHashMap imple } @Override - public Float2DoubleMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Float2DoubleMap.Entry state = null; boolean empty = true; @@ -849,21 +856,21 @@ public class Float2DoubleLinkedOpenHashMap extends Float2DoubleOpenHashMap imple state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Float2DoubleMap.Entry 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 @@ -939,6 +946,12 @@ public class Float2DoubleLinkedOpenHashMap extends Float2DoubleOpenHashMap imple @Override public boolean add(float o) { throw new UnsupportedOperationException(); } + + @Override + public void addFirst(float o) { throw new UnsupportedOperationException(); } + + @Override + public void addLast(float o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(float o) { throw new UnsupportedOperationException(); } @@ -1085,7 +1098,7 @@ public class Float2DoubleLinkedOpenHashMap extends Float2DoubleOpenHashMap imple } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -1100,19 +1113,19 @@ public class Float2DoubleLinkedOpenHashMap extends Float2DoubleOpenHashMap imple state = operator.applyAsFloat(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0F; + if(size() <= 0) return OptionalFloat.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalFloat.of(keys[index]); index = (int)links[index]; } - return 0F; + return OptionalFloat.empty(); } @Override @@ -1248,7 +1261,7 @@ public class Float2DoubleLinkedOpenHashMap extends Float2DoubleOpenHashMap imple } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -1263,19 +1276,19 @@ public class Float2DoubleLinkedOpenHashMap extends Float2DoubleOpenHashMap imple state = operator.applyAsDouble(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0D; + if(size() <= 0) return OptionalDouble.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalDouble.of(values[index]); index = (int)links[index]; } - return 0D; + return OptionalDouble.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/floats/maps/impl/hash/Float2DoubleOpenHashMap.java b/src/main/java/speiger/src/collections/floats/maps/impl/hash/Float2DoubleOpenHashMap.java index 32f9ab2..e412880 100644 --- a/src/main/java/speiger/src/collections/floats/maps/impl/hash/Float2DoubleOpenHashMap.java +++ b/src/main/java/speiger/src/collections/floats/maps/impl/hash/Float2DoubleOpenHashMap.java @@ -5,10 +5,12 @@ 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; import java.util.function.DoublePredicate; +import java.util.OptionalDouble; import speiger.src.collections.floats.collections.FloatIterator; import speiger.src.collections.floats.functions.FloatConsumer; @@ -20,6 +22,7 @@ import speiger.src.collections.floats.functions.consumer.FloatDoubleConsumer; import speiger.src.collections.floats.functions.function.Float2DoubleFunction; import speiger.src.collections.floats.functions.function.FloatDoubleUnaryOperator; import speiger.src.collections.floats.functions.function.FloatFloatUnaryOperator; +import speiger.src.collections.floats.functions.OptionalFloat; import speiger.src.collections.floats.functions.function.FloatPredicate; import speiger.src.collections.floats.maps.abstracts.AbstractFloat2DoubleMap; import speiger.src.collections.floats.maps.interfaces.Float2DoubleMap; @@ -920,7 +923,7 @@ public class Float2DoubleOpenHashMap extends AbstractFloat2DoubleMap implements } @Override - public Float2DoubleMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Float2DoubleMap.Entry state = null; boolean empty = true; @@ -937,25 +940,25 @@ public class Float2DoubleOpenHashMap extends AbstractFloat2DoubleMap implements } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Float2DoubleMap.Entry 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(Float.floatToIntBits(keys[i]) != 0) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1127,7 +1130,7 @@ public class Float2DoubleOpenHashMap extends AbstractFloat2DoubleMap implements } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -1144,18 +1147,18 @@ public class Float2DoubleOpenHashMap extends AbstractFloat2DoubleMap implements } state = operator.applyAsFloat(state, keys[i]); } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0F; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OptionalFloat.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OptionalFloat.of(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(Float.floatToIntBits(keys[i]) != 0 && filter.test(keys[i])) return keys[i]; + if(Float.floatToIntBits(keys[i]) != 0 && filter.test(keys[i])) return OptionalFloat.of(keys[i]); } - return 0F; + return OptionalFloat.empty(); } @Override @@ -1270,7 +1273,7 @@ public class Float2DoubleOpenHashMap extends AbstractFloat2DoubleMap implements } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -1287,18 +1290,18 @@ public class Float2DoubleOpenHashMap extends AbstractFloat2DoubleMap implements } state = operator.applyAsDouble(state, values[i]); } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0D; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return OptionalDouble.empty(); + if(containsNull && filter.test(values[nullIndex])) return OptionalDouble.of(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(Float.floatToIntBits(keys[i]) != 0 && filter.test(values[i])) return values[i]; + if(Float.floatToIntBits(keys[i]) != 0 && filter.test(values[i])) return OptionalDouble.of(values[i]); } - return 0D; + return OptionalDouble.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/floats/maps/impl/hash/Float2FloatLinkedOpenHashMap.java b/src/main/java/speiger/src/collections/floats/maps/impl/hash/Float2FloatLinkedOpenHashMap.java index 7ddb1af..a8d9423 100644 --- a/src/main/java/speiger/src/collections/floats/maps/impl/hash/Float2FloatLinkedOpenHashMap.java +++ b/src/main/java/speiger/src/collections/floats/maps/impl/hash/Float2FloatLinkedOpenHashMap.java @@ -4,6 +4,7 @@ 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; @@ -13,6 +14,7 @@ import speiger.src.collections.floats.functions.FloatConsumer; import speiger.src.collections.objects.functions.consumer.ObjectFloatConsumer; import speiger.src.collections.ints.functions.consumer.IntFloatConsumer; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; +import speiger.src.collections.floats.functions.OptionalFloat; import speiger.src.collections.floats.functions.function.FloatPredicate; import speiger.src.collections.floats.functions.consumer.FloatFloatConsumer; import speiger.src.collections.floats.functions.function.FloatFloatUnaryOperator; @@ -662,6 +664,10 @@ public class Float2FloatLinkedOpenHashMap extends Float2FloatOpenHashMap impleme } private class MapEntrySet extends AbstractObjectSet implements Float2FloatOrderedMap.FastOrderedSet { + @Override + public void addFirst(Float2FloatMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Float2FloatMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Float2FloatMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -827,7 +833,7 @@ public class Float2FloatLinkedOpenHashMap extends Float2FloatOpenHashMap impleme } @Override - public Float2FloatMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Float2FloatMap.Entry state = null; boolean empty = true; @@ -842,21 +848,21 @@ public class Float2FloatLinkedOpenHashMap extends Float2FloatOpenHashMap impleme state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Float2FloatMap.Entry 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 @@ -932,6 +938,12 @@ public class Float2FloatLinkedOpenHashMap extends Float2FloatOpenHashMap impleme @Override public boolean add(float o) { throw new UnsupportedOperationException(); } + + @Override + public void addFirst(float o) { throw new UnsupportedOperationException(); } + + @Override + public void addLast(float o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(float o) { throw new UnsupportedOperationException(); } @@ -1078,7 +1090,7 @@ public class Float2FloatLinkedOpenHashMap extends Float2FloatOpenHashMap impleme } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -1093,19 +1105,19 @@ public class Float2FloatLinkedOpenHashMap extends Float2FloatOpenHashMap impleme state = operator.applyAsFloat(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0F; + if(size() <= 0) return OptionalFloat.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalFloat.of(keys[index]); index = (int)links[index]; } - return 0F; + return OptionalFloat.empty(); } @Override @@ -1241,7 +1253,7 @@ public class Float2FloatLinkedOpenHashMap extends Float2FloatOpenHashMap impleme } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -1256,19 +1268,19 @@ public class Float2FloatLinkedOpenHashMap extends Float2FloatOpenHashMap impleme state = operator.applyAsFloat(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0F; + if(size() <= 0) return OptionalFloat.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalFloat.of(values[index]); index = (int)links[index]; } - return 0F; + return OptionalFloat.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/floats/maps/impl/hash/Float2FloatOpenHashMap.java b/src/main/java/speiger/src/collections/floats/maps/impl/hash/Float2FloatOpenHashMap.java index 5e27111..cf20e2d 100644 --- a/src/main/java/speiger/src/collections/floats/maps/impl/hash/Float2FloatOpenHashMap.java +++ b/src/main/java/speiger/src/collections/floats/maps/impl/hash/Float2FloatOpenHashMap.java @@ -5,6 +5,7 @@ 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; @@ -17,6 +18,7 @@ import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.floats.functions.consumer.FloatFloatConsumer; import speiger.src.collections.floats.functions.function.FloatUnaryOperator; import speiger.src.collections.floats.functions.function.FloatFloatUnaryOperator; +import speiger.src.collections.floats.functions.OptionalFloat; import speiger.src.collections.floats.functions.function.FloatPredicate; import speiger.src.collections.floats.maps.abstracts.AbstractFloat2FloatMap; import speiger.src.collections.floats.maps.interfaces.Float2FloatMap; @@ -914,7 +916,7 @@ public class Float2FloatOpenHashMap extends AbstractFloat2FloatMap implements IT } @Override - public Float2FloatMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Float2FloatMap.Entry state = null; boolean empty = true; @@ -931,25 +933,25 @@ public class Float2FloatOpenHashMap extends AbstractFloat2FloatMap implements IT } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Float2FloatMap.Entry 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(Float.floatToIntBits(keys[i]) != 0) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1121,7 +1123,7 @@ public class Float2FloatOpenHashMap extends AbstractFloat2FloatMap implements IT } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -1138,18 +1140,18 @@ public class Float2FloatOpenHashMap extends AbstractFloat2FloatMap implements IT } state = operator.applyAsFloat(state, keys[i]); } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0F; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OptionalFloat.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OptionalFloat.of(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(Float.floatToIntBits(keys[i]) != 0 && filter.test(keys[i])) return keys[i]; + if(Float.floatToIntBits(keys[i]) != 0 && filter.test(keys[i])) return OptionalFloat.of(keys[i]); } - return 0F; + return OptionalFloat.empty(); } @Override @@ -1264,7 +1266,7 @@ public class Float2FloatOpenHashMap extends AbstractFloat2FloatMap implements IT } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -1281,18 +1283,18 @@ public class Float2FloatOpenHashMap extends AbstractFloat2FloatMap implements IT } state = operator.applyAsFloat(state, values[i]); } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0F; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return OptionalFloat.empty(); + if(containsNull && filter.test(values[nullIndex])) return OptionalFloat.of(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(Float.floatToIntBits(keys[i]) != 0 && filter.test(values[i])) return values[i]; + if(Float.floatToIntBits(keys[i]) != 0 && filter.test(values[i])) return OptionalFloat.of(values[i]); } - return 0F; + return OptionalFloat.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/floats/maps/impl/hash/Float2IntLinkedOpenHashMap.java b/src/main/java/speiger/src/collections/floats/maps/impl/hash/Float2IntLinkedOpenHashMap.java index 9ec803b..9a6a3d9 100644 --- a/src/main/java/speiger/src/collections/floats/maps/impl/hash/Float2IntLinkedOpenHashMap.java +++ b/src/main/java/speiger/src/collections/floats/maps/impl/hash/Float2IntLinkedOpenHashMap.java @@ -4,10 +4,12 @@ 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; import java.util.function.IntPredicate; +import java.util.OptionalInt; import speiger.src.collections.floats.collections.FloatBidirectionalIterator; import speiger.src.collections.floats.functions.FloatConsumer; @@ -15,6 +17,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectFloatConsumer; import speiger.src.collections.ints.functions.consumer.IntFloatConsumer; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntIntConsumer; +import speiger.src.collections.floats.functions.OptionalFloat; import speiger.src.collections.floats.functions.function.FloatPredicate; import speiger.src.collections.floats.functions.consumer.FloatIntConsumer; import speiger.src.collections.floats.functions.function.FloatFloatUnaryOperator; @@ -669,6 +672,10 @@ public class Float2IntLinkedOpenHashMap extends Float2IntOpenHashMap implements } private class MapEntrySet extends AbstractObjectSet implements Float2IntOrderedMap.FastOrderedSet { + @Override + public void addFirst(Float2IntMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Float2IntMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Float2IntMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -834,7 +841,7 @@ public class Float2IntLinkedOpenHashMap extends Float2IntOpenHashMap implements } @Override - public Float2IntMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Float2IntMap.Entry state = null; boolean empty = true; @@ -849,21 +856,21 @@ public class Float2IntLinkedOpenHashMap extends Float2IntOpenHashMap implements state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Float2IntMap.Entry 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 @@ -939,6 +946,12 @@ public class Float2IntLinkedOpenHashMap extends Float2IntOpenHashMap implements @Override public boolean add(float o) { throw new UnsupportedOperationException(); } + + @Override + public void addFirst(float o) { throw new UnsupportedOperationException(); } + + @Override + public void addLast(float o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(float o) { throw new UnsupportedOperationException(); } @@ -1085,7 +1098,7 @@ public class Float2IntLinkedOpenHashMap extends Float2IntOpenHashMap implements } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -1100,19 +1113,19 @@ public class Float2IntLinkedOpenHashMap extends Float2IntOpenHashMap implements state = operator.applyAsFloat(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0F; + if(size() <= 0) return OptionalFloat.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalFloat.of(keys[index]); index = (int)links[index]; } - return 0F; + return OptionalFloat.empty(); } @Override @@ -1248,7 +1261,7 @@ public class Float2IntLinkedOpenHashMap extends Float2IntOpenHashMap implements } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -1263,19 +1276,19 @@ public class Float2IntLinkedOpenHashMap extends Float2IntOpenHashMap implements state = operator.applyAsInt(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0; + if(size() <= 0) return OptionalInt.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalInt.of(values[index]); index = (int)links[index]; } - return 0; + return OptionalInt.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/floats/maps/impl/hash/Float2IntOpenHashMap.java b/src/main/java/speiger/src/collections/floats/maps/impl/hash/Float2IntOpenHashMap.java index 0191957..7acea82 100644 --- a/src/main/java/speiger/src/collections/floats/maps/impl/hash/Float2IntOpenHashMap.java +++ b/src/main/java/speiger/src/collections/floats/maps/impl/hash/Float2IntOpenHashMap.java @@ -5,10 +5,12 @@ 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; import java.util.function.IntPredicate; +import java.util.OptionalInt; import speiger.src.collections.floats.collections.FloatIterator; import speiger.src.collections.floats.functions.FloatConsumer; @@ -20,6 +22,7 @@ import speiger.src.collections.floats.functions.consumer.FloatIntConsumer; import speiger.src.collections.floats.functions.function.Float2IntFunction; import speiger.src.collections.floats.functions.function.FloatIntUnaryOperator; import speiger.src.collections.floats.functions.function.FloatFloatUnaryOperator; +import speiger.src.collections.floats.functions.OptionalFloat; import speiger.src.collections.floats.functions.function.FloatPredicate; import speiger.src.collections.floats.maps.abstracts.AbstractFloat2IntMap; import speiger.src.collections.floats.maps.interfaces.Float2IntMap; @@ -920,7 +923,7 @@ public class Float2IntOpenHashMap extends AbstractFloat2IntMap implements ITrimm } @Override - public Float2IntMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Float2IntMap.Entry state = null; boolean empty = true; @@ -937,25 +940,25 @@ public class Float2IntOpenHashMap extends AbstractFloat2IntMap implements ITrimm } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Float2IntMap.Entry 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(Float.floatToIntBits(keys[i]) != 0) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1127,7 +1130,7 @@ public class Float2IntOpenHashMap extends AbstractFloat2IntMap implements ITrimm } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -1144,18 +1147,18 @@ public class Float2IntOpenHashMap extends AbstractFloat2IntMap implements ITrimm } state = operator.applyAsFloat(state, keys[i]); } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0F; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OptionalFloat.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OptionalFloat.of(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(Float.floatToIntBits(keys[i]) != 0 && filter.test(keys[i])) return keys[i]; + if(Float.floatToIntBits(keys[i]) != 0 && filter.test(keys[i])) return OptionalFloat.of(keys[i]); } - return 0F; + return OptionalFloat.empty(); } @Override @@ -1270,7 +1273,7 @@ public class Float2IntOpenHashMap extends AbstractFloat2IntMap implements ITrimm } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -1287,18 +1290,18 @@ public class Float2IntOpenHashMap extends AbstractFloat2IntMap implements ITrimm } state = operator.applyAsInt(state, values[i]); } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return OptionalInt.empty(); + if(containsNull && filter.test(values[nullIndex])) return OptionalInt.of(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(Float.floatToIntBits(keys[i]) != 0 && filter.test(values[i])) return values[i]; + if(Float.floatToIntBits(keys[i]) != 0 && filter.test(values[i])) return OptionalInt.of(values[i]); } - return 0; + return OptionalInt.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/floats/maps/impl/hash/Float2LongLinkedOpenHashMap.java b/src/main/java/speiger/src/collections/floats/maps/impl/hash/Float2LongLinkedOpenHashMap.java index 3caffdc..daaad8e 100644 --- a/src/main/java/speiger/src/collections/floats/maps/impl/hash/Float2LongLinkedOpenHashMap.java +++ b/src/main/java/speiger/src/collections/floats/maps/impl/hash/Float2LongLinkedOpenHashMap.java @@ -4,10 +4,12 @@ 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; import java.util.function.LongPredicate; +import java.util.OptionalLong; import speiger.src.collections.floats.collections.FloatBidirectionalIterator; import speiger.src.collections.floats.functions.FloatConsumer; @@ -15,6 +17,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectFloatConsumer; import speiger.src.collections.ints.functions.consumer.IntFloatConsumer; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntLongConsumer; +import speiger.src.collections.floats.functions.OptionalFloat; import speiger.src.collections.floats.functions.function.FloatPredicate; import speiger.src.collections.floats.functions.consumer.FloatLongConsumer; import speiger.src.collections.floats.functions.function.FloatFloatUnaryOperator; @@ -669,6 +672,10 @@ public class Float2LongLinkedOpenHashMap extends Float2LongOpenHashMap implement } private class MapEntrySet extends AbstractObjectSet implements Float2LongOrderedMap.FastOrderedSet { + @Override + public void addFirst(Float2LongMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Float2LongMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Float2LongMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -834,7 +841,7 @@ public class Float2LongLinkedOpenHashMap extends Float2LongOpenHashMap implement } @Override - public Float2LongMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Float2LongMap.Entry state = null; boolean empty = true; @@ -849,21 +856,21 @@ public class Float2LongLinkedOpenHashMap extends Float2LongOpenHashMap implement state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Float2LongMap.Entry 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 @@ -939,6 +946,12 @@ public class Float2LongLinkedOpenHashMap extends Float2LongOpenHashMap implement @Override public boolean add(float o) { throw new UnsupportedOperationException(); } + + @Override + public void addFirst(float o) { throw new UnsupportedOperationException(); } + + @Override + public void addLast(float o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(float o) { throw new UnsupportedOperationException(); } @@ -1085,7 +1098,7 @@ public class Float2LongLinkedOpenHashMap extends Float2LongOpenHashMap implement } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -1100,19 +1113,19 @@ public class Float2LongLinkedOpenHashMap extends Float2LongOpenHashMap implement state = operator.applyAsFloat(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0F; + if(size() <= 0) return OptionalFloat.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalFloat.of(keys[index]); index = (int)links[index]; } - return 0F; + return OptionalFloat.empty(); } @Override @@ -1248,7 +1261,7 @@ public class Float2LongLinkedOpenHashMap extends Float2LongOpenHashMap implement } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -1263,19 +1276,19 @@ public class Float2LongLinkedOpenHashMap extends Float2LongOpenHashMap implement state = operator.applyAsLong(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0L; + if(size() <= 0) return OptionalLong.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalLong.of(values[index]); index = (int)links[index]; } - return 0L; + return OptionalLong.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/floats/maps/impl/hash/Float2LongOpenHashMap.java b/src/main/java/speiger/src/collections/floats/maps/impl/hash/Float2LongOpenHashMap.java index a55f915..5f647fe 100644 --- a/src/main/java/speiger/src/collections/floats/maps/impl/hash/Float2LongOpenHashMap.java +++ b/src/main/java/speiger/src/collections/floats/maps/impl/hash/Float2LongOpenHashMap.java @@ -5,10 +5,12 @@ 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; import java.util.function.LongPredicate; +import java.util.OptionalLong; import speiger.src.collections.floats.collections.FloatIterator; import speiger.src.collections.floats.functions.FloatConsumer; @@ -20,6 +22,7 @@ import speiger.src.collections.floats.functions.consumer.FloatLongConsumer; import speiger.src.collections.floats.functions.function.Float2LongFunction; import speiger.src.collections.floats.functions.function.FloatLongUnaryOperator; import speiger.src.collections.floats.functions.function.FloatFloatUnaryOperator; +import speiger.src.collections.floats.functions.OptionalFloat; import speiger.src.collections.floats.functions.function.FloatPredicate; import speiger.src.collections.floats.maps.abstracts.AbstractFloat2LongMap; import speiger.src.collections.floats.maps.interfaces.Float2LongMap; @@ -920,7 +923,7 @@ public class Float2LongOpenHashMap extends AbstractFloat2LongMap implements ITri } @Override - public Float2LongMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Float2LongMap.Entry state = null; boolean empty = true; @@ -937,25 +940,25 @@ public class Float2LongOpenHashMap extends AbstractFloat2LongMap implements ITri } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Float2LongMap.Entry 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(Float.floatToIntBits(keys[i]) != 0) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1127,7 +1130,7 @@ public class Float2LongOpenHashMap extends AbstractFloat2LongMap implements ITri } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -1144,18 +1147,18 @@ public class Float2LongOpenHashMap extends AbstractFloat2LongMap implements ITri } state = operator.applyAsFloat(state, keys[i]); } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0F; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OptionalFloat.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OptionalFloat.of(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(Float.floatToIntBits(keys[i]) != 0 && filter.test(keys[i])) return keys[i]; + if(Float.floatToIntBits(keys[i]) != 0 && filter.test(keys[i])) return OptionalFloat.of(keys[i]); } - return 0F; + return OptionalFloat.empty(); } @Override @@ -1270,7 +1273,7 @@ public class Float2LongOpenHashMap extends AbstractFloat2LongMap implements ITri } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -1287,18 +1290,18 @@ public class Float2LongOpenHashMap extends AbstractFloat2LongMap implements ITri } state = operator.applyAsLong(state, values[i]); } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0L; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return OptionalLong.empty(); + if(containsNull && filter.test(values[nullIndex])) return OptionalLong.of(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(Float.floatToIntBits(keys[i]) != 0 && filter.test(values[i])) return values[i]; + if(Float.floatToIntBits(keys[i]) != 0 && filter.test(values[i])) return OptionalLong.of(values[i]); } - return 0L; + return OptionalLong.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/floats/maps/impl/hash/Float2ObjectLinkedOpenHashMap.java b/src/main/java/speiger/src/collections/floats/maps/impl/hash/Float2ObjectLinkedOpenHashMap.java index a241f51..28340bc 100644 --- a/src/main/java/speiger/src/collections/floats/maps/impl/hash/Float2ObjectLinkedOpenHashMap.java +++ b/src/main/java/speiger/src/collections/floats/maps/impl/hash/Float2ObjectLinkedOpenHashMap.java @@ -4,6 +4,7 @@ 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; @@ -13,6 +14,7 @@ import speiger.src.collections.floats.functions.FloatConsumer; import speiger.src.collections.objects.functions.consumer.ObjectFloatConsumer; import speiger.src.collections.ints.functions.consumer.IntFloatConsumer; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; +import speiger.src.collections.floats.functions.OptionalFloat; import speiger.src.collections.floats.functions.function.FloatPredicate; import speiger.src.collections.floats.functions.consumer.FloatObjectConsumer; import speiger.src.collections.floats.functions.function.FloatFloatUnaryOperator; @@ -653,6 +655,10 @@ public class Float2ObjectLinkedOpenHashMap extends Float2ObjectOpenHashMap } private class MapEntrySet extends AbstractObjectSet> implements Float2ObjectOrderedMap.FastOrderedSet { + @Override + public void addFirst(Float2ObjectMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Float2ObjectMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Float2ObjectMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -818,7 +824,7 @@ public class Float2ObjectLinkedOpenHashMap extends Float2ObjectOpenHashMap } @Override - public Float2ObjectMap.Entry reduce(ObjectObjectUnaryOperator, Float2ObjectMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Float2ObjectMap.Entry> operator) { Objects.requireNonNull(operator); Float2ObjectMap.Entry state = null; boolean empty = true; @@ -833,21 +839,21 @@ public class Float2ObjectLinkedOpenHashMap extends Float2ObjectOpenHashMap state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Float2ObjectMap.Entry 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 @@ -923,6 +929,12 @@ public class Float2ObjectLinkedOpenHashMap extends Float2ObjectOpenHashMap @Override public boolean add(float o) { throw new UnsupportedOperationException(); } + + @Override + public void addFirst(float o) { throw new UnsupportedOperationException(); } + + @Override + public void addLast(float o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(float o) { throw new UnsupportedOperationException(); } @@ -1069,7 +1081,7 @@ public class Float2ObjectLinkedOpenHashMap extends Float2ObjectOpenHashMap } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -1084,19 +1096,19 @@ public class Float2ObjectLinkedOpenHashMap extends Float2ObjectOpenHashMap state = operator.applyAsFloat(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0F; + if(size() <= 0) return OptionalFloat.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalFloat.of(keys[index]); index = (int)links[index]; } - return 0F; + return OptionalFloat.empty(); } @Override @@ -1233,7 +1245,7 @@ public class Float2ObjectLinkedOpenHashMap extends Float2ObjectOpenHashMap } @Override - public V reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); V state = null; boolean empty = true; @@ -1248,19 +1260,19 @@ public class Float2ObjectLinkedOpenHashMap extends Float2ObjectOpenHashMap state = operator.apply(state, values[index]); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public V findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return Optional.ofNullable(values[index]); index = (int)links[index]; } - return null; + return Optional.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/floats/maps/impl/hash/Float2ObjectOpenHashMap.java b/src/main/java/speiger/src/collections/floats/maps/impl/hash/Float2ObjectOpenHashMap.java index 06b429a..2f02054 100644 --- a/src/main/java/speiger/src/collections/floats/maps/impl/hash/Float2ObjectOpenHashMap.java +++ b/src/main/java/speiger/src/collections/floats/maps/impl/hash/Float2ObjectOpenHashMap.java @@ -5,6 +5,7 @@ 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; @@ -18,6 +19,7 @@ import speiger.src.collections.floats.functions.consumer.FloatObjectConsumer; import speiger.src.collections.floats.functions.function.FloatFunction; import speiger.src.collections.floats.functions.function.FloatObjectUnaryOperator; import speiger.src.collections.floats.functions.function.FloatFloatUnaryOperator; +import speiger.src.collections.floats.functions.OptionalFloat; import speiger.src.collections.floats.functions.function.FloatPredicate; import speiger.src.collections.floats.maps.abstracts.AbstractFloat2ObjectMap; import speiger.src.collections.floats.maps.interfaces.Float2ObjectMap; @@ -837,7 +839,7 @@ public class Float2ObjectOpenHashMap extends AbstractFloat2ObjectMap imple } @Override - public Float2ObjectMap.Entry reduce(ObjectObjectUnaryOperator, Float2ObjectMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Float2ObjectMap.Entry> operator) { Objects.requireNonNull(operator); Float2ObjectMap.Entry state = null; boolean empty = true; @@ -854,25 +856,25 @@ public class Float2ObjectOpenHashMap extends AbstractFloat2ObjectMap imple } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Float2ObjectMap.Entry 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(Float.floatToIntBits(keys[i]) != 0) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1044,7 +1046,7 @@ public class Float2ObjectOpenHashMap extends AbstractFloat2ObjectMap imple } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -1061,18 +1063,18 @@ public class Float2ObjectOpenHashMap extends AbstractFloat2ObjectMap imple } state = operator.applyAsFloat(state, keys[i]); } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0F; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OptionalFloat.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OptionalFloat.of(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(Float.floatToIntBits(keys[i]) != 0 && filter.test(keys[i])) return keys[i]; + if(Float.floatToIntBits(keys[i]) != 0 && filter.test(keys[i])) return OptionalFloat.of(keys[i]); } - return 0F; + return OptionalFloat.empty(); } @Override @@ -1187,7 +1189,7 @@ public class Float2ObjectOpenHashMap extends AbstractFloat2ObjectMap imple } @Override - public V reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); V state = null; boolean empty = true; @@ -1204,18 +1206,18 @@ public class Float2ObjectOpenHashMap extends AbstractFloat2ObjectMap imple } state = operator.apply(state, values[i]); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public V findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return Optional.empty(); + if(containsNull && filter.test(values[nullIndex])) return Optional.ofNullable(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(Float.floatToIntBits(keys[i]) != 0 && filter.test(values[i])) return values[i]; + if(Float.floatToIntBits(keys[i]) != 0 && filter.test(values[i])) return Optional.ofNullable(values[i]); } - return null; + return Optional.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/floats/maps/impl/hash/Float2ShortLinkedOpenHashMap.java b/src/main/java/speiger/src/collections/floats/maps/impl/hash/Float2ShortLinkedOpenHashMap.java index 671c155..203f861 100644 --- a/src/main/java/speiger/src/collections/floats/maps/impl/hash/Float2ShortLinkedOpenHashMap.java +++ b/src/main/java/speiger/src/collections/floats/maps/impl/hash/Float2ShortLinkedOpenHashMap.java @@ -4,6 +4,7 @@ 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; @@ -14,6 +15,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectFloatConsumer; import speiger.src.collections.ints.functions.consumer.IntFloatConsumer; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntShortConsumer; +import speiger.src.collections.floats.functions.OptionalFloat; import speiger.src.collections.floats.functions.function.FloatPredicate; import speiger.src.collections.floats.functions.consumer.FloatShortConsumer; import speiger.src.collections.floats.functions.function.FloatFloatUnaryOperator; @@ -32,6 +34,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectShortConsumer; import speiger.src.collections.shorts.functions.function.ShortPredicate; +import speiger.src.collections.shorts.functions.OptionalShort; import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOperator; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.objects.lists.ObjectListIterator; @@ -669,6 +672,10 @@ public class Float2ShortLinkedOpenHashMap extends Float2ShortOpenHashMap impleme } private class MapEntrySet extends AbstractObjectSet implements Float2ShortOrderedMap.FastOrderedSet { + @Override + public void addFirst(Float2ShortMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Float2ShortMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Float2ShortMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -834,7 +841,7 @@ public class Float2ShortLinkedOpenHashMap extends Float2ShortOpenHashMap impleme } @Override - public Float2ShortMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Float2ShortMap.Entry state = null; boolean empty = true; @@ -849,21 +856,21 @@ public class Float2ShortLinkedOpenHashMap extends Float2ShortOpenHashMap impleme state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Float2ShortMap.Entry 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 @@ -939,6 +946,12 @@ public class Float2ShortLinkedOpenHashMap extends Float2ShortOpenHashMap impleme @Override public boolean add(float o) { throw new UnsupportedOperationException(); } + + @Override + public void addFirst(float o) { throw new UnsupportedOperationException(); } + + @Override + public void addLast(float o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(float o) { throw new UnsupportedOperationException(); } @@ -1085,7 +1098,7 @@ public class Float2ShortLinkedOpenHashMap extends Float2ShortOpenHashMap impleme } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -1100,19 +1113,19 @@ public class Float2ShortLinkedOpenHashMap extends Float2ShortOpenHashMap impleme state = operator.applyAsFloat(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0F; + if(size() <= 0) return OptionalFloat.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalFloat.of(keys[index]); index = (int)links[index]; } - return 0F; + return OptionalFloat.empty(); } @Override @@ -1248,7 +1261,7 @@ public class Float2ShortLinkedOpenHashMap extends Float2ShortOpenHashMap impleme } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -1263,19 +1276,19 @@ public class Float2ShortLinkedOpenHashMap extends Float2ShortOpenHashMap impleme state = operator.applyAsShort(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (short)0; + if(size() <= 0) return OptionalShort.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalShort.of(values[index]); index = (int)links[index]; } - return (short)0; + return OptionalShort.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/floats/maps/impl/hash/Float2ShortOpenHashMap.java b/src/main/java/speiger/src/collections/floats/maps/impl/hash/Float2ShortOpenHashMap.java index 6a9b71d..dd9350d 100644 --- a/src/main/java/speiger/src/collections/floats/maps/impl/hash/Float2ShortOpenHashMap.java +++ b/src/main/java/speiger/src/collections/floats/maps/impl/hash/Float2ShortOpenHashMap.java @@ -5,6 +5,7 @@ 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; @@ -19,6 +20,7 @@ import speiger.src.collections.floats.functions.consumer.FloatShortConsumer; import speiger.src.collections.floats.functions.function.Float2ShortFunction; import speiger.src.collections.floats.functions.function.FloatShortUnaryOperator; import speiger.src.collections.floats.functions.function.FloatFloatUnaryOperator; +import speiger.src.collections.floats.functions.OptionalFloat; import speiger.src.collections.floats.functions.function.FloatPredicate; import speiger.src.collections.floats.maps.abstracts.AbstractFloat2ShortMap; import speiger.src.collections.floats.maps.interfaces.Float2ShortMap; @@ -34,6 +36,7 @@ import speiger.src.collections.shorts.functions.ShortConsumer; import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectShortConsumer; import speiger.src.collections.shorts.functions.function.ShortPredicate; +import speiger.src.collections.shorts.functions.OptionalShort; import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.sets.AbstractObjectSet; import speiger.src.collections.objects.sets.ObjectSet; @@ -920,7 +923,7 @@ public class Float2ShortOpenHashMap extends AbstractFloat2ShortMap implements IT } @Override - public Float2ShortMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Float2ShortMap.Entry state = null; boolean empty = true; @@ -937,25 +940,25 @@ public class Float2ShortOpenHashMap extends AbstractFloat2ShortMap implements IT } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Float2ShortMap.Entry 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(Float.floatToIntBits(keys[i]) != 0) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1127,7 +1130,7 @@ public class Float2ShortOpenHashMap extends AbstractFloat2ShortMap implements IT } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -1144,18 +1147,18 @@ public class Float2ShortOpenHashMap extends AbstractFloat2ShortMap implements IT } state = operator.applyAsFloat(state, keys[i]); } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0F; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OptionalFloat.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OptionalFloat.of(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(Float.floatToIntBits(keys[i]) != 0 && filter.test(keys[i])) return keys[i]; + if(Float.floatToIntBits(keys[i]) != 0 && filter.test(keys[i])) return OptionalFloat.of(keys[i]); } - return 0F; + return OptionalFloat.empty(); } @Override @@ -1270,7 +1273,7 @@ public class Float2ShortOpenHashMap extends AbstractFloat2ShortMap implements IT } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -1287,18 +1290,18 @@ public class Float2ShortOpenHashMap extends AbstractFloat2ShortMap implements IT } state = operator.applyAsShort(state, values[i]); } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (short)0; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return OptionalShort.empty(); + if(containsNull && filter.test(values[nullIndex])) return OptionalShort.of(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(Float.floatToIntBits(keys[i]) != 0 && filter.test(values[i])) return values[i]; + if(Float.floatToIntBits(keys[i]) != 0 && filter.test(values[i])) return OptionalShort.of(values[i]); } - return (short)0; + return OptionalShort.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/floats/maps/impl/immutable/ImmutableFloat2BooleanOpenHashMap.java b/src/main/java/speiger/src/collections/floats/maps/impl/immutable/ImmutableFloat2BooleanOpenHashMap.java index 3888a59..cc2b43e 100644 --- a/src/main/java/speiger/src/collections/floats/maps/impl/immutable/ImmutableFloat2BooleanOpenHashMap.java +++ b/src/main/java/speiger/src/collections/floats/maps/impl/immutable/ImmutableFloat2BooleanOpenHashMap.java @@ -4,6 +4,7 @@ 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; @@ -14,6 +15,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectFloatConsumer; import speiger.src.collections.ints.functions.consumer.IntFloatConsumer; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntBooleanConsumer; +import speiger.src.collections.floats.functions.OptionalFloat; import speiger.src.collections.floats.functions.consumer.FloatBooleanConsumer; import speiger.src.collections.floats.functions.function.FloatPredicate; import speiger.src.collections.floats.functions.function.FloatBooleanUnaryOperator; @@ -28,6 +30,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectBooleanConsumer; import speiger.src.collections.booleans.functions.function.BooleanPredicate; +import speiger.src.collections.booleans.functions.OptionalBoolean; import speiger.src.collections.floats.sets.AbstractFloatSet; import speiger.src.collections.booleans.collections.AbstractBooleanCollection; import speiger.src.collections.booleans.collections.BooleanOrderedCollection; @@ -533,6 +536,10 @@ public class ImmutableFloat2BooleanOpenHashMap extends AbstractFloat2BooleanMap } private class MapEntrySet extends AbstractObjectSet implements Float2BooleanOrderedMap.FastOrderedSet { + @Override + public void addFirst(Float2BooleanMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Float2BooleanMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Float2BooleanMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -686,7 +693,7 @@ public class ImmutableFloat2BooleanOpenHashMap extends AbstractFloat2BooleanMap } @Override - public Float2BooleanMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Float2BooleanMap.Entry state = null; boolean empty = true; @@ -701,21 +708,21 @@ public class ImmutableFloat2BooleanOpenHashMap extends AbstractFloat2BooleanMap state = operator.apply(state, new BasicEntry(keys[index], values[index])); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Float2BooleanMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry entry = new BasicEntry(); 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 @@ -778,6 +785,12 @@ public class ImmutableFloat2BooleanOpenHashMap extends AbstractFloat2BooleanMap throw new UnsupportedOperationException(); } + @Override + public void addFirst(float o) { throw new UnsupportedOperationException(); } + + @Override + public void addLast(float o) { throw new UnsupportedOperationException(); } + @Override public boolean addAndMoveToFirst(float o) { throw new UnsupportedOperationException(); } @@ -913,7 +926,7 @@ public class ImmutableFloat2BooleanOpenHashMap extends AbstractFloat2BooleanMap } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -928,19 +941,19 @@ public class ImmutableFloat2BooleanOpenHashMap extends AbstractFloat2BooleanMap state = operator.applyAsFloat(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0F; + if(size() <= 0) return OptionalFloat.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalFloat.of(keys[index]); index = (int)links[index]; } - return 0F; + return OptionalFloat.empty(); } @Override @@ -1067,7 +1080,7 @@ public class ImmutableFloat2BooleanOpenHashMap extends AbstractFloat2BooleanMap } @Override - public boolean reduce(BooleanBooleanUnaryOperator operator) { + public OptionalBoolean reduce(BooleanBooleanUnaryOperator operator) { Objects.requireNonNull(operator); boolean state = false; boolean empty = true; @@ -1082,19 +1095,19 @@ public class ImmutableFloat2BooleanOpenHashMap extends AbstractFloat2BooleanMap state = operator.applyAsBoolean(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalBoolean.empty() : OptionalBoolean.of(state); } @Override - public boolean findFirst(BooleanPredicate filter) { + public OptionalBoolean findFirst(BooleanPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return false; + if(size() <= 0) return OptionalBoolean.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalBoolean.of(values[index]); index = (int)links[index]; } - return false; + return OptionalBoolean.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/floats/maps/impl/immutable/ImmutableFloat2ByteOpenHashMap.java b/src/main/java/speiger/src/collections/floats/maps/impl/immutable/ImmutableFloat2ByteOpenHashMap.java index f946b64..d7bca72 100644 --- a/src/main/java/speiger/src/collections/floats/maps/impl/immutable/ImmutableFloat2ByteOpenHashMap.java +++ b/src/main/java/speiger/src/collections/floats/maps/impl/immutable/ImmutableFloat2ByteOpenHashMap.java @@ -4,6 +4,7 @@ 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; @@ -14,6 +15,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectFloatConsumer; import speiger.src.collections.ints.functions.consumer.IntFloatConsumer; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntByteConsumer; +import speiger.src.collections.floats.functions.OptionalFloat; import speiger.src.collections.floats.functions.function.FloatPredicate; import speiger.src.collections.floats.functions.consumer.FloatByteConsumer; import speiger.src.collections.floats.functions.function.Float2ByteFunction; @@ -29,6 +31,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectByteConsumer; import speiger.src.collections.bytes.functions.function.BytePredicate; +import speiger.src.collections.bytes.functions.OptionalByte; import speiger.src.collections.floats.sets.AbstractFloatSet; import speiger.src.collections.bytes.collections.AbstractByteCollection; import speiger.src.collections.bytes.collections.ByteOrderedCollection; @@ -538,6 +541,10 @@ public class ImmutableFloat2ByteOpenHashMap extends AbstractFloat2ByteMap implem } private class MapEntrySet extends AbstractObjectSet implements Float2ByteOrderedMap.FastOrderedSet { + @Override + public void addFirst(Float2ByteMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Float2ByteMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Float2ByteMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -691,7 +698,7 @@ public class ImmutableFloat2ByteOpenHashMap extends AbstractFloat2ByteMap implem } @Override - public Float2ByteMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Float2ByteMap.Entry state = null; boolean empty = true; @@ -706,21 +713,21 @@ public class ImmutableFloat2ByteOpenHashMap extends AbstractFloat2ByteMap implem state = operator.apply(state, new BasicEntry(keys[index], values[index])); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Float2ByteMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry entry = new BasicEntry(); 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 @@ -783,6 +790,12 @@ public class ImmutableFloat2ByteOpenHashMap extends AbstractFloat2ByteMap implem throw new UnsupportedOperationException(); } + @Override + public void addFirst(float o) { throw new UnsupportedOperationException(); } + + @Override + public void addLast(float o) { throw new UnsupportedOperationException(); } + @Override public boolean addAndMoveToFirst(float o) { throw new UnsupportedOperationException(); } @@ -918,7 +931,7 @@ public class ImmutableFloat2ByteOpenHashMap extends AbstractFloat2ByteMap implem } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -933,19 +946,19 @@ public class ImmutableFloat2ByteOpenHashMap extends AbstractFloat2ByteMap implem state = operator.applyAsFloat(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0F; + if(size() <= 0) return OptionalFloat.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalFloat.of(keys[index]); index = (int)links[index]; } - return 0F; + return OptionalFloat.empty(); } @Override @@ -1072,7 +1085,7 @@ public class ImmutableFloat2ByteOpenHashMap extends AbstractFloat2ByteMap implem } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -1087,19 +1100,19 @@ public class ImmutableFloat2ByteOpenHashMap extends AbstractFloat2ByteMap implem state = operator.applyAsByte(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (byte)0; + if(size() <= 0) return OptionalByte.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalByte.of(values[index]); index = (int)links[index]; } - return (byte)0; + return OptionalByte.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/floats/maps/impl/immutable/ImmutableFloat2CharOpenHashMap.java b/src/main/java/speiger/src/collections/floats/maps/impl/immutable/ImmutableFloat2CharOpenHashMap.java index 3101fbe..b630925 100644 --- a/src/main/java/speiger/src/collections/floats/maps/impl/immutable/ImmutableFloat2CharOpenHashMap.java +++ b/src/main/java/speiger/src/collections/floats/maps/impl/immutable/ImmutableFloat2CharOpenHashMap.java @@ -4,6 +4,7 @@ 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; @@ -14,6 +15,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectFloatConsumer; import speiger.src.collections.ints.functions.consumer.IntFloatConsumer; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntCharConsumer; +import speiger.src.collections.floats.functions.OptionalFloat; import speiger.src.collections.floats.functions.function.FloatPredicate; import speiger.src.collections.floats.functions.consumer.FloatCharConsumer; import speiger.src.collections.floats.functions.function.Float2CharFunction; @@ -29,6 +31,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectCharConsumer; import speiger.src.collections.chars.functions.function.CharPredicate; +import speiger.src.collections.chars.functions.OptionalChar; import speiger.src.collections.floats.sets.AbstractFloatSet; import speiger.src.collections.chars.collections.AbstractCharCollection; import speiger.src.collections.chars.collections.CharOrderedCollection; @@ -538,6 +541,10 @@ public class ImmutableFloat2CharOpenHashMap extends AbstractFloat2CharMap implem } private class MapEntrySet extends AbstractObjectSet implements Float2CharOrderedMap.FastOrderedSet { + @Override + public void addFirst(Float2CharMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Float2CharMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Float2CharMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -691,7 +698,7 @@ public class ImmutableFloat2CharOpenHashMap extends AbstractFloat2CharMap implem } @Override - public Float2CharMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Float2CharMap.Entry state = null; boolean empty = true; @@ -706,21 +713,21 @@ public class ImmutableFloat2CharOpenHashMap extends AbstractFloat2CharMap implem state = operator.apply(state, new BasicEntry(keys[index], values[index])); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Float2CharMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry entry = new BasicEntry(); 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 @@ -783,6 +790,12 @@ public class ImmutableFloat2CharOpenHashMap extends AbstractFloat2CharMap implem throw new UnsupportedOperationException(); } + @Override + public void addFirst(float o) { throw new UnsupportedOperationException(); } + + @Override + public void addLast(float o) { throw new UnsupportedOperationException(); } + @Override public boolean addAndMoveToFirst(float o) { throw new UnsupportedOperationException(); } @@ -918,7 +931,7 @@ public class ImmutableFloat2CharOpenHashMap extends AbstractFloat2CharMap implem } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -933,19 +946,19 @@ public class ImmutableFloat2CharOpenHashMap extends AbstractFloat2CharMap implem state = operator.applyAsFloat(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0F; + if(size() <= 0) return OptionalFloat.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalFloat.of(keys[index]); index = (int)links[index]; } - return 0F; + return OptionalFloat.empty(); } @Override @@ -1072,7 +1085,7 @@ public class ImmutableFloat2CharOpenHashMap extends AbstractFloat2CharMap implem } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -1087,19 +1100,19 @@ public class ImmutableFloat2CharOpenHashMap extends AbstractFloat2CharMap implem state = operator.applyAsChar(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (char)0; + if(size() <= 0) return OptionalChar.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalChar.of(values[index]); index = (int)links[index]; } - return (char)0; + return OptionalChar.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/floats/maps/impl/immutable/ImmutableFloat2DoubleOpenHashMap.java b/src/main/java/speiger/src/collections/floats/maps/impl/immutable/ImmutableFloat2DoubleOpenHashMap.java index 2a397b0..25333c0 100644 --- a/src/main/java/speiger/src/collections/floats/maps/impl/immutable/ImmutableFloat2DoubleOpenHashMap.java +++ b/src/main/java/speiger/src/collections/floats/maps/impl/immutable/ImmutableFloat2DoubleOpenHashMap.java @@ -4,10 +4,12 @@ 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; import java.util.function.DoublePredicate; +import java.util.OptionalDouble; import speiger.src.collections.floats.collections.FloatBidirectionalIterator; import speiger.src.collections.floats.functions.FloatConsumer; @@ -15,6 +17,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectFloatConsumer; import speiger.src.collections.ints.functions.consumer.IntFloatConsumer; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntDoubleConsumer; +import speiger.src.collections.floats.functions.OptionalFloat; import speiger.src.collections.floats.functions.function.FloatPredicate; import speiger.src.collections.floats.functions.consumer.FloatDoubleConsumer; import speiger.src.collections.floats.functions.function.Float2DoubleFunction; @@ -538,6 +541,10 @@ public class ImmutableFloat2DoubleOpenHashMap extends AbstractFloat2DoubleMap im } private class MapEntrySet extends AbstractObjectSet implements Float2DoubleOrderedMap.FastOrderedSet { + @Override + public void addFirst(Float2DoubleMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Float2DoubleMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Float2DoubleMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -691,7 +698,7 @@ public class ImmutableFloat2DoubleOpenHashMap extends AbstractFloat2DoubleMap im } @Override - public Float2DoubleMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Float2DoubleMap.Entry state = null; boolean empty = true; @@ -706,21 +713,21 @@ public class ImmutableFloat2DoubleOpenHashMap extends AbstractFloat2DoubleMap im state = operator.apply(state, new BasicEntry(keys[index], values[index])); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Float2DoubleMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry entry = new BasicEntry(); 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 @@ -783,6 +790,12 @@ public class ImmutableFloat2DoubleOpenHashMap extends AbstractFloat2DoubleMap im throw new UnsupportedOperationException(); } + @Override + public void addFirst(float o) { throw new UnsupportedOperationException(); } + + @Override + public void addLast(float o) { throw new UnsupportedOperationException(); } + @Override public boolean addAndMoveToFirst(float o) { throw new UnsupportedOperationException(); } @@ -918,7 +931,7 @@ public class ImmutableFloat2DoubleOpenHashMap extends AbstractFloat2DoubleMap im } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -933,19 +946,19 @@ public class ImmutableFloat2DoubleOpenHashMap extends AbstractFloat2DoubleMap im state = operator.applyAsFloat(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0F; + if(size() <= 0) return OptionalFloat.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalFloat.of(keys[index]); index = (int)links[index]; } - return 0F; + return OptionalFloat.empty(); } @Override @@ -1072,7 +1085,7 @@ public class ImmutableFloat2DoubleOpenHashMap extends AbstractFloat2DoubleMap im } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -1087,19 +1100,19 @@ public class ImmutableFloat2DoubleOpenHashMap extends AbstractFloat2DoubleMap im state = operator.applyAsDouble(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0D; + if(size() <= 0) return OptionalDouble.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalDouble.of(values[index]); index = (int)links[index]; } - return 0D; + return OptionalDouble.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/floats/maps/impl/immutable/ImmutableFloat2FloatOpenHashMap.java b/src/main/java/speiger/src/collections/floats/maps/impl/immutable/ImmutableFloat2FloatOpenHashMap.java index 10ba231..dda63f2 100644 --- a/src/main/java/speiger/src/collections/floats/maps/impl/immutable/ImmutableFloat2FloatOpenHashMap.java +++ b/src/main/java/speiger/src/collections/floats/maps/impl/immutable/ImmutableFloat2FloatOpenHashMap.java @@ -4,6 +4,7 @@ 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; @@ -13,6 +14,7 @@ import speiger.src.collections.floats.functions.FloatConsumer; import speiger.src.collections.objects.functions.consumer.ObjectFloatConsumer; import speiger.src.collections.ints.functions.consumer.IntFloatConsumer; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; +import speiger.src.collections.floats.functions.OptionalFloat; import speiger.src.collections.floats.functions.function.FloatPredicate; import speiger.src.collections.floats.functions.consumer.FloatFloatConsumer; import speiger.src.collections.floats.functions.function.FloatUnaryOperator; @@ -529,6 +531,10 @@ public class ImmutableFloat2FloatOpenHashMap extends AbstractFloat2FloatMap impl } private class MapEntrySet extends AbstractObjectSet implements Float2FloatOrderedMap.FastOrderedSet { + @Override + public void addFirst(Float2FloatMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Float2FloatMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Float2FloatMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -682,7 +688,7 @@ public class ImmutableFloat2FloatOpenHashMap extends AbstractFloat2FloatMap impl } @Override - public Float2FloatMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Float2FloatMap.Entry state = null; boolean empty = true; @@ -697,21 +703,21 @@ public class ImmutableFloat2FloatOpenHashMap extends AbstractFloat2FloatMap impl state = operator.apply(state, new BasicEntry(keys[index], values[index])); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Float2FloatMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry entry = new BasicEntry(); 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 @@ -774,6 +780,12 @@ public class ImmutableFloat2FloatOpenHashMap extends AbstractFloat2FloatMap impl throw new UnsupportedOperationException(); } + @Override + public void addFirst(float o) { throw new UnsupportedOperationException(); } + + @Override + public void addLast(float o) { throw new UnsupportedOperationException(); } + @Override public boolean addAndMoveToFirst(float o) { throw new UnsupportedOperationException(); } @@ -909,7 +921,7 @@ public class ImmutableFloat2FloatOpenHashMap extends AbstractFloat2FloatMap impl } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -924,19 +936,19 @@ public class ImmutableFloat2FloatOpenHashMap extends AbstractFloat2FloatMap impl state = operator.applyAsFloat(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0F; + if(size() <= 0) return OptionalFloat.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalFloat.of(keys[index]); index = (int)links[index]; } - return 0F; + return OptionalFloat.empty(); } @Override @@ -1063,7 +1075,7 @@ public class ImmutableFloat2FloatOpenHashMap extends AbstractFloat2FloatMap impl } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -1078,19 +1090,19 @@ public class ImmutableFloat2FloatOpenHashMap extends AbstractFloat2FloatMap impl state = operator.applyAsFloat(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0F; + if(size() <= 0) return OptionalFloat.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalFloat.of(values[index]); index = (int)links[index]; } - return 0F; + return OptionalFloat.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/floats/maps/impl/immutable/ImmutableFloat2IntOpenHashMap.java b/src/main/java/speiger/src/collections/floats/maps/impl/immutable/ImmutableFloat2IntOpenHashMap.java index ec0fcd9..3791a40 100644 --- a/src/main/java/speiger/src/collections/floats/maps/impl/immutable/ImmutableFloat2IntOpenHashMap.java +++ b/src/main/java/speiger/src/collections/floats/maps/impl/immutable/ImmutableFloat2IntOpenHashMap.java @@ -4,10 +4,12 @@ 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; import java.util.function.IntPredicate; +import java.util.OptionalInt; import speiger.src.collections.floats.collections.FloatBidirectionalIterator; import speiger.src.collections.floats.functions.FloatConsumer; @@ -15,6 +17,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectFloatConsumer; import speiger.src.collections.ints.functions.consumer.IntFloatConsumer; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntIntConsumer; +import speiger.src.collections.floats.functions.OptionalFloat; import speiger.src.collections.floats.functions.function.FloatPredicate; import speiger.src.collections.floats.functions.consumer.FloatIntConsumer; import speiger.src.collections.floats.functions.function.Float2IntFunction; @@ -538,6 +541,10 @@ public class ImmutableFloat2IntOpenHashMap extends AbstractFloat2IntMap implemen } private class MapEntrySet extends AbstractObjectSet implements Float2IntOrderedMap.FastOrderedSet { + @Override + public void addFirst(Float2IntMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Float2IntMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Float2IntMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -691,7 +698,7 @@ public class ImmutableFloat2IntOpenHashMap extends AbstractFloat2IntMap implemen } @Override - public Float2IntMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Float2IntMap.Entry state = null; boolean empty = true; @@ -706,21 +713,21 @@ public class ImmutableFloat2IntOpenHashMap extends AbstractFloat2IntMap implemen state = operator.apply(state, new BasicEntry(keys[index], values[index])); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Float2IntMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry entry = new BasicEntry(); 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 @@ -783,6 +790,12 @@ public class ImmutableFloat2IntOpenHashMap extends AbstractFloat2IntMap implemen throw new UnsupportedOperationException(); } + @Override + public void addFirst(float o) { throw new UnsupportedOperationException(); } + + @Override + public void addLast(float o) { throw new UnsupportedOperationException(); } + @Override public boolean addAndMoveToFirst(float o) { throw new UnsupportedOperationException(); } @@ -918,7 +931,7 @@ public class ImmutableFloat2IntOpenHashMap extends AbstractFloat2IntMap implemen } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -933,19 +946,19 @@ public class ImmutableFloat2IntOpenHashMap extends AbstractFloat2IntMap implemen state = operator.applyAsFloat(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0F; + if(size() <= 0) return OptionalFloat.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalFloat.of(keys[index]); index = (int)links[index]; } - return 0F; + return OptionalFloat.empty(); } @Override @@ -1072,7 +1085,7 @@ public class ImmutableFloat2IntOpenHashMap extends AbstractFloat2IntMap implemen } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -1087,19 +1100,19 @@ public class ImmutableFloat2IntOpenHashMap extends AbstractFloat2IntMap implemen state = operator.applyAsInt(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0; + if(size() <= 0) return OptionalInt.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalInt.of(values[index]); index = (int)links[index]; } - return 0; + return OptionalInt.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/floats/maps/impl/immutable/ImmutableFloat2LongOpenHashMap.java b/src/main/java/speiger/src/collections/floats/maps/impl/immutable/ImmutableFloat2LongOpenHashMap.java index 7934222..7068676 100644 --- a/src/main/java/speiger/src/collections/floats/maps/impl/immutable/ImmutableFloat2LongOpenHashMap.java +++ b/src/main/java/speiger/src/collections/floats/maps/impl/immutable/ImmutableFloat2LongOpenHashMap.java @@ -4,10 +4,12 @@ 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; import java.util.function.LongPredicate; +import java.util.OptionalLong; import speiger.src.collections.floats.collections.FloatBidirectionalIterator; import speiger.src.collections.floats.functions.FloatConsumer; @@ -15,6 +17,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectFloatConsumer; import speiger.src.collections.ints.functions.consumer.IntFloatConsumer; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntLongConsumer; +import speiger.src.collections.floats.functions.OptionalFloat; import speiger.src.collections.floats.functions.function.FloatPredicate; import speiger.src.collections.floats.functions.consumer.FloatLongConsumer; import speiger.src.collections.floats.functions.function.Float2LongFunction; @@ -538,6 +541,10 @@ public class ImmutableFloat2LongOpenHashMap extends AbstractFloat2LongMap implem } private class MapEntrySet extends AbstractObjectSet implements Float2LongOrderedMap.FastOrderedSet { + @Override + public void addFirst(Float2LongMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Float2LongMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Float2LongMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -691,7 +698,7 @@ public class ImmutableFloat2LongOpenHashMap extends AbstractFloat2LongMap implem } @Override - public Float2LongMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Float2LongMap.Entry state = null; boolean empty = true; @@ -706,21 +713,21 @@ public class ImmutableFloat2LongOpenHashMap extends AbstractFloat2LongMap implem state = operator.apply(state, new BasicEntry(keys[index], values[index])); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Float2LongMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry entry = new BasicEntry(); 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 @@ -783,6 +790,12 @@ public class ImmutableFloat2LongOpenHashMap extends AbstractFloat2LongMap implem throw new UnsupportedOperationException(); } + @Override + public void addFirst(float o) { throw new UnsupportedOperationException(); } + + @Override + public void addLast(float o) { throw new UnsupportedOperationException(); } + @Override public boolean addAndMoveToFirst(float o) { throw new UnsupportedOperationException(); } @@ -918,7 +931,7 @@ public class ImmutableFloat2LongOpenHashMap extends AbstractFloat2LongMap implem } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -933,19 +946,19 @@ public class ImmutableFloat2LongOpenHashMap extends AbstractFloat2LongMap implem state = operator.applyAsFloat(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0F; + if(size() <= 0) return OptionalFloat.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalFloat.of(keys[index]); index = (int)links[index]; } - return 0F; + return OptionalFloat.empty(); } @Override @@ -1072,7 +1085,7 @@ public class ImmutableFloat2LongOpenHashMap extends AbstractFloat2LongMap implem } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -1087,19 +1100,19 @@ public class ImmutableFloat2LongOpenHashMap extends AbstractFloat2LongMap implem state = operator.applyAsLong(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0L; + if(size() <= 0) return OptionalLong.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalLong.of(values[index]); index = (int)links[index]; } - return 0L; + return OptionalLong.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/floats/maps/impl/immutable/ImmutableFloat2ObjectOpenHashMap.java b/src/main/java/speiger/src/collections/floats/maps/impl/immutable/ImmutableFloat2ObjectOpenHashMap.java index 37ad108..a659486 100644 --- a/src/main/java/speiger/src/collections/floats/maps/impl/immutable/ImmutableFloat2ObjectOpenHashMap.java +++ b/src/main/java/speiger/src/collections/floats/maps/impl/immutable/ImmutableFloat2ObjectOpenHashMap.java @@ -4,6 +4,7 @@ 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; @@ -13,6 +14,7 @@ import speiger.src.collections.floats.functions.FloatConsumer; import speiger.src.collections.objects.functions.consumer.ObjectFloatConsumer; import speiger.src.collections.ints.functions.consumer.IntFloatConsumer; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; +import speiger.src.collections.floats.functions.OptionalFloat; import speiger.src.collections.floats.functions.function.FloatPredicate; import speiger.src.collections.floats.functions.consumer.FloatObjectConsumer; import speiger.src.collections.floats.functions.function.FloatFunction; @@ -509,6 +511,10 @@ public class ImmutableFloat2ObjectOpenHashMap extends AbstractFloat2ObjectMap } private class MapEntrySet extends AbstractObjectSet> implements Float2ObjectOrderedMap.FastOrderedSet { + @Override + public void addFirst(Float2ObjectMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Float2ObjectMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Float2ObjectMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -662,7 +668,7 @@ public class ImmutableFloat2ObjectOpenHashMap extends AbstractFloat2ObjectMap } @Override - public Float2ObjectMap.Entry reduce(ObjectObjectUnaryOperator, Float2ObjectMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Float2ObjectMap.Entry> operator) { Objects.requireNonNull(operator); Float2ObjectMap.Entry state = null; boolean empty = true; @@ -677,21 +683,21 @@ public class ImmutableFloat2ObjectOpenHashMap extends AbstractFloat2ObjectMap state = operator.apply(state, new BasicEntry<>(keys[index], values[index])); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Float2ObjectMap.Entry findFirst(Predicate> filter) { + public Optional> findFirst(Predicate> filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry entry = new BasicEntry<>(); 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 @@ -754,6 +760,12 @@ public class ImmutableFloat2ObjectOpenHashMap extends AbstractFloat2ObjectMap throw new UnsupportedOperationException(); } + @Override + public void addFirst(float o) { throw new UnsupportedOperationException(); } + + @Override + public void addLast(float o) { throw new UnsupportedOperationException(); } + @Override public boolean addAndMoveToFirst(float o) { throw new UnsupportedOperationException(); } @@ -889,7 +901,7 @@ public class ImmutableFloat2ObjectOpenHashMap extends AbstractFloat2ObjectMap } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -904,19 +916,19 @@ public class ImmutableFloat2ObjectOpenHashMap extends AbstractFloat2ObjectMap state = operator.applyAsFloat(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0F; + if(size() <= 0) return OptionalFloat.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalFloat.of(keys[index]); index = (int)links[index]; } - return 0F; + return OptionalFloat.empty(); } @Override @@ -1044,7 +1056,7 @@ public class ImmutableFloat2ObjectOpenHashMap extends AbstractFloat2ObjectMap } @Override - public V reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); V state = null; boolean empty = true; @@ -1059,19 +1071,19 @@ public class ImmutableFloat2ObjectOpenHashMap extends AbstractFloat2ObjectMap state = operator.apply(state, values[index]); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public V findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return Optional.ofNullable(values[index]); index = (int)links[index]; } - return null; + return Optional.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/floats/maps/impl/immutable/ImmutableFloat2ShortOpenHashMap.java b/src/main/java/speiger/src/collections/floats/maps/impl/immutable/ImmutableFloat2ShortOpenHashMap.java index 913488a..6726e55 100644 --- a/src/main/java/speiger/src/collections/floats/maps/impl/immutable/ImmutableFloat2ShortOpenHashMap.java +++ b/src/main/java/speiger/src/collections/floats/maps/impl/immutable/ImmutableFloat2ShortOpenHashMap.java @@ -4,6 +4,7 @@ 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; @@ -14,6 +15,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectFloatConsumer; import speiger.src.collections.ints.functions.consumer.IntFloatConsumer; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntShortConsumer; +import speiger.src.collections.floats.functions.OptionalFloat; import speiger.src.collections.floats.functions.function.FloatPredicate; import speiger.src.collections.floats.functions.consumer.FloatShortConsumer; import speiger.src.collections.floats.functions.function.Float2ShortFunction; @@ -29,6 +31,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectShortConsumer; import speiger.src.collections.shorts.functions.function.ShortPredicate; +import speiger.src.collections.shorts.functions.OptionalShort; import speiger.src.collections.floats.sets.AbstractFloatSet; import speiger.src.collections.shorts.collections.AbstractShortCollection; import speiger.src.collections.shorts.collections.ShortOrderedCollection; @@ -538,6 +541,10 @@ public class ImmutableFloat2ShortOpenHashMap extends AbstractFloat2ShortMap impl } private class MapEntrySet extends AbstractObjectSet implements Float2ShortOrderedMap.FastOrderedSet { + @Override + public void addFirst(Float2ShortMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Float2ShortMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Float2ShortMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -691,7 +698,7 @@ public class ImmutableFloat2ShortOpenHashMap extends AbstractFloat2ShortMap impl } @Override - public Float2ShortMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Float2ShortMap.Entry state = null; boolean empty = true; @@ -706,21 +713,21 @@ public class ImmutableFloat2ShortOpenHashMap extends AbstractFloat2ShortMap impl state = operator.apply(state, new BasicEntry(keys[index], values[index])); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Float2ShortMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry entry = new BasicEntry(); 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 @@ -783,6 +790,12 @@ public class ImmutableFloat2ShortOpenHashMap extends AbstractFloat2ShortMap impl throw new UnsupportedOperationException(); } + @Override + public void addFirst(float o) { throw new UnsupportedOperationException(); } + + @Override + public void addLast(float o) { throw new UnsupportedOperationException(); } + @Override public boolean addAndMoveToFirst(float o) { throw new UnsupportedOperationException(); } @@ -918,7 +931,7 @@ public class ImmutableFloat2ShortOpenHashMap extends AbstractFloat2ShortMap impl } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -933,19 +946,19 @@ public class ImmutableFloat2ShortOpenHashMap extends AbstractFloat2ShortMap impl state = operator.applyAsFloat(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0F; + if(size() <= 0) return OptionalFloat.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalFloat.of(keys[index]); index = (int)links[index]; } - return 0F; + return OptionalFloat.empty(); } @Override @@ -1072,7 +1085,7 @@ public class ImmutableFloat2ShortOpenHashMap extends AbstractFloat2ShortMap impl } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -1087,19 +1100,19 @@ public class ImmutableFloat2ShortOpenHashMap extends AbstractFloat2ShortMap impl state = operator.applyAsShort(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (short)0; + if(size() <= 0) return OptionalShort.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalShort.of(values[index]); index = (int)links[index]; } - return (short)0; + return OptionalShort.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/floats/maps/impl/misc/Float2BooleanArrayMap.java b/src/main/java/speiger/src/collections/floats/maps/impl/misc/Float2BooleanArrayMap.java index 361ced7..ade141f 100644 --- a/src/main/java/speiger/src/collections/floats/maps/impl/misc/Float2BooleanArrayMap.java +++ b/src/main/java/speiger/src/collections/floats/maps/impl/misc/Float2BooleanArrayMap.java @@ -4,6 +4,7 @@ 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; @@ -14,6 +15,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectFloatConsumer; import speiger.src.collections.ints.functions.consumer.IntFloatConsumer; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntBooleanConsumer; +import speiger.src.collections.floats.functions.OptionalFloat; import speiger.src.collections.floats.functions.consumer.FloatBooleanConsumer; import speiger.src.collections.floats.functions.function.FloatPredicate; import speiger.src.collections.floats.functions.function.FloatBooleanUnaryOperator; @@ -34,6 +36,7 @@ import speiger.src.collections.booleans.lists.BooleanListIterator; import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.booleans.functions.function.BooleanPredicate; +import speiger.src.collections.booleans.functions.OptionalBoolean; import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOperator; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; @@ -728,6 +731,10 @@ public class Float2BooleanArrayMap extends AbstractFloat2BooleanMap implements F } private class MapEntrySet extends AbstractObjectSet implements Float2BooleanOrderedMap.FastOrderedSet { + @Override + public void addFirst(Float2BooleanMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Float2BooleanMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Float2BooleanMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -876,7 +883,7 @@ public class Float2BooleanArrayMap extends AbstractFloat2BooleanMap implements F } @Override - public Float2BooleanMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Float2BooleanMap.Entry state = null; boolean empty = true; @@ -888,19 +895,19 @@ public class Float2BooleanArrayMap extends AbstractFloat2BooleanMap implements F } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Float2BooleanMap.Entry 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 implements Float2ByteOrderedMap.FastOrderedSet { + @Override + public void addFirst(Float2ByteMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Float2ByteMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Float2ByteMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -899,7 +906,7 @@ public class Float2ByteArrayMap extends AbstractFloat2ByteMap implements Float2B } @Override - public Float2ByteMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Float2ByteMap.Entry state = null; boolean empty = true; @@ -911,19 +918,19 @@ public class Float2ByteArrayMap extends AbstractFloat2ByteMap implements Float2B } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Float2ByteMap.Entry 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 implements Float2CharOrderedMap.FastOrderedSet { + @Override + public void addFirst(Float2CharMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Float2CharMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Float2CharMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -899,7 +906,7 @@ public class Float2CharArrayMap extends AbstractFloat2CharMap implements Float2C } @Override - public Float2CharMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Float2CharMap.Entry state = null; boolean empty = true; @@ -911,19 +918,19 @@ public class Float2CharArrayMap extends AbstractFloat2CharMap implements Float2C } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Float2CharMap.Entry 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 implements Float2DoubleOrderedMap.FastOrderedSet { + @Override + public void addFirst(Float2DoubleMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Float2DoubleMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Float2DoubleMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -899,7 +906,7 @@ public class Float2DoubleArrayMap extends AbstractFloat2DoubleMap implements Flo } @Override - public Float2DoubleMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Float2DoubleMap.Entry state = null; boolean empty = true; @@ -911,19 +918,19 @@ public class Float2DoubleArrayMap extends AbstractFloat2DoubleMap implements Flo } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Float2DoubleMap.Entry 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 implements Float2FloatOrderedMap.FastOrderedSet { + @Override + public void addFirst(Float2FloatMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Float2FloatMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Float2FloatMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -892,7 +898,7 @@ public class Float2FloatArrayMap extends AbstractFloat2FloatMap implements Float } @Override - public Float2FloatMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Float2FloatMap.Entry state = null; boolean empty = true; @@ -904,19 +910,19 @@ public class Float2FloatArrayMap extends AbstractFloat2FloatMap implements Float } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Float2FloatMap.Entry 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 implements Float2IntOrderedMap.FastOrderedSet { + @Override + public void addFirst(Float2IntMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Float2IntMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Float2IntMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -899,7 +906,7 @@ public class Float2IntArrayMap extends AbstractFloat2IntMap implements Float2Int } @Override - public Float2IntMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Float2IntMap.Entry state = null; boolean empty = true; @@ -911,19 +918,19 @@ public class Float2IntArrayMap extends AbstractFloat2IntMap implements Float2Int } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Float2IntMap.Entry 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 implements Float2LongOrderedMap.FastOrderedSet { + @Override + public void addFirst(Float2LongMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Float2LongMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Float2LongMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -899,7 +906,7 @@ public class Float2LongArrayMap extends AbstractFloat2LongMap implements Float2L } @Override - public Float2LongMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Float2LongMap.Entry state = null; boolean empty = true; @@ -911,19 +918,19 @@ public class Float2LongArrayMap extends AbstractFloat2LongMap implements Float2L } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Float2LongMap.Entry 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 extends AbstractFloat2ObjectMap implemen } private class MapEntrySet extends AbstractObjectSet> implements Float2ObjectOrderedMap.FastOrderedSet { + @Override + public void addFirst(Float2ObjectMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Float2ObjectMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Float2ObjectMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -812,7 +818,7 @@ public class Float2ObjectArrayMap extends AbstractFloat2ObjectMap implemen } @Override - public Float2ObjectMap.Entry reduce(ObjectObjectUnaryOperator, Float2ObjectMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Float2ObjectMap.Entry> operator) { Objects.requireNonNull(operator); Float2ObjectMap.Entry state = null; boolean empty = true; @@ -824,19 +830,19 @@ public class Float2ObjectArrayMap extends AbstractFloat2ObjectMap implemen } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Float2ObjectMap.Entry 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 extends AbstractFloat2ObjectMap implemen @Override public boolean add(float o) { throw new UnsupportedOperationException(); } @Override + public void addFirst(float o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(float o) { throw new UnsupportedOperationException(); } + @Override public boolean addAndMoveToFirst(float o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToLast(float o) { throw new UnsupportedOperationException(); } @@ -995,7 +1005,7 @@ public class Float2ObjectArrayMap extends AbstractFloat2ObjectMap implemen } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -1007,16 +1017,16 @@ public class Float2ObjectArrayMap extends AbstractFloat2ObjectMap implemen } state = operator.applyAsFloat(state, keys[i]); } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); for(int i = 0;i extends AbstractFloat2ObjectMap implemen } @Override - public V reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); V state = null; boolean empty = true; @@ -1129,16 +1139,16 @@ public class Float2ObjectArrayMap extends AbstractFloat2ObjectMap implemen } state = operator.apply(state, values[i]); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public V findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); for(int i = 0;i implements Float2ShortOrderedMap.FastOrderedSet { + @Override + public void addFirst(Float2ShortMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Float2ShortMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Float2ShortMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -899,7 +906,7 @@ public class Float2ShortArrayMap extends AbstractFloat2ShortMap implements Float } @Override - public Float2ShortMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Float2ShortMap.Entry state = null; boolean empty = true; @@ -911,19 +918,19 @@ public class Float2ShortArrayMap extends AbstractFloat2ShortMap implements Float } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Float2ShortMap.Entry 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); Float2BooleanMap.Entry state = null; boolean empty = true; @@ -1724,19 +1727,19 @@ public class Float2BooleanAVLTreeMap extends AbstractFloat2BooleanMap implements } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Float2BooleanMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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 @@ -1831,7 +1834,7 @@ public class Float2BooleanAVLTreeMap extends AbstractFloat2BooleanMap implements } @Override - public boolean reduce(BooleanBooleanUnaryOperator operator) { + public OptionalBoolean reduce(BooleanBooleanUnaryOperator operator) { Objects.requireNonNull(operator); boolean state = false; boolean empty = true; @@ -1843,15 +1846,15 @@ public class Float2BooleanAVLTreeMap extends AbstractFloat2BooleanMap implements } state = operator.applyAsBoolean(state, entry.value); } - return state; + return empty ? OptionalBoolean.empty() : OptionalBoolean.of(state); } @Override - public boolean findFirst(BooleanPredicate filter) { + public OptionalBoolean findFirst(BooleanPredicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return false; + if(filter.test(entry.value)) return OptionalBoolean.of(entry.value); + return OptionalBoolean.empty(); } @Override @@ -2167,7 +2170,7 @@ public class Float2BooleanAVLTreeMap extends AbstractFloat2BooleanMap implements } @Override - public boolean reduce(BooleanBooleanUnaryOperator operator) { + public OptionalBoolean reduce(BooleanBooleanUnaryOperator operator) { Objects.requireNonNull(operator); boolean state = false; boolean empty = true; @@ -2179,15 +2182,15 @@ public class Float2BooleanAVLTreeMap extends AbstractFloat2BooleanMap implements } state = operator.applyAsBoolean(state, entry.value); } - return state; + return empty ? OptionalBoolean.empty() : OptionalBoolean.of(state); } @Override - public boolean findFirst(BooleanPredicate filter) { + public OptionalBoolean findFirst(BooleanPredicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return false; + if(filter.test(entry.value)) return OptionalBoolean.of(entry.value); + return OptionalBoolean.empty(); } @Override @@ -2328,7 +2331,7 @@ public class Float2BooleanAVLTreeMap extends AbstractFloat2BooleanMap implements } @Override - public Float2BooleanMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Float2BooleanMap.Entry state = null; boolean empty = true; @@ -2340,19 +2343,19 @@ public class Float2BooleanAVLTreeMap extends AbstractFloat2BooleanMap implements } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Float2BooleanMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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/main/java/speiger/src/collections/floats/maps/impl/tree/Float2BooleanRBTreeMap.java b/src/main/java/speiger/src/collections/floats/maps/impl/tree/Float2BooleanRBTreeMap.java index eb87c05..857f3f4 100644 --- a/src/main/java/speiger/src/collections/floats/maps/impl/tree/Float2BooleanRBTreeMap.java +++ b/src/main/java/speiger/src/collections/floats/maps/impl/tree/Float2BooleanRBTreeMap.java @@ -3,6 +3,7 @@ package speiger.src.collections.floats.maps.impl.tree; import java.util.Collections; import java.util.Map; import java.util.Objects; +import java.util.Optional; import java.util.NoSuchElementException; import java.util.function.Consumer; import java.util.function.BiFunction; @@ -17,6 +18,7 @@ import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntBooleanConsumer; import speiger.src.collections.floats.functions.function.FloatPredicate; import speiger.src.collections.floats.functions.consumer.FloatBooleanConsumer; +import speiger.src.collections.floats.functions.OptionalFloat; import speiger.src.collections.floats.functions.function.FloatBooleanUnaryOperator; import speiger.src.collections.floats.functions.function.FloatFloatUnaryOperator; import speiger.src.collections.floats.maps.abstracts.AbstractFloat2BooleanMap; @@ -35,6 +37,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectBooleanConsumer; import speiger.src.collections.booleans.functions.function.BooleanPredicate; +import speiger.src.collections.booleans.functions.OptionalBoolean; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOperator; @@ -1123,7 +1126,7 @@ public class Float2BooleanRBTreeMap extends AbstractFloat2BooleanMap implements } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -1135,15 +1138,15 @@ public class Float2BooleanRBTreeMap extends AbstractFloat2BooleanMap implements } state = operator.applyAsFloat(state, entry.key); } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); for(Node entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.test(entry.key)) return entry.key; - return 0F; + if(filter.test(entry.key)) return OptionalFloat.of(entry.key); + return OptionalFloat.empty(); } @Override @@ -1775,7 +1778,7 @@ public class Float2BooleanRBTreeMap extends AbstractFloat2BooleanMap implements } @Override - public Float2BooleanMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Float2BooleanMap.Entry state = null; boolean empty = true; @@ -1787,19 +1790,19 @@ public class Float2BooleanRBTreeMap extends AbstractFloat2BooleanMap implements } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Float2BooleanMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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 @@ -1894,7 +1897,7 @@ public class Float2BooleanRBTreeMap extends AbstractFloat2BooleanMap implements } @Override - public boolean reduce(BooleanBooleanUnaryOperator operator) { + public OptionalBoolean reduce(BooleanBooleanUnaryOperator operator) { Objects.requireNonNull(operator); boolean state = false; boolean empty = true; @@ -1906,15 +1909,15 @@ public class Float2BooleanRBTreeMap extends AbstractFloat2BooleanMap implements } state = operator.applyAsBoolean(state, entry.value); } - return state; + return empty ? OptionalBoolean.empty() : OptionalBoolean.of(state); } @Override - public boolean findFirst(BooleanPredicate filter) { + public OptionalBoolean findFirst(BooleanPredicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return false; + if(filter.test(entry.value)) return OptionalBoolean.of(entry.value); + return OptionalBoolean.empty(); } @Override @@ -2230,7 +2233,7 @@ public class Float2BooleanRBTreeMap extends AbstractFloat2BooleanMap implements } @Override - public boolean reduce(BooleanBooleanUnaryOperator operator) { + public OptionalBoolean reduce(BooleanBooleanUnaryOperator operator) { Objects.requireNonNull(operator); boolean state = false; boolean empty = true; @@ -2242,15 +2245,15 @@ public class Float2BooleanRBTreeMap extends AbstractFloat2BooleanMap implements } state = operator.applyAsBoolean(state, entry.value); } - return state; + return empty ? OptionalBoolean.empty() : OptionalBoolean.of(state); } @Override - public boolean findFirst(BooleanPredicate filter) { + public OptionalBoolean findFirst(BooleanPredicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return false; + if(filter.test(entry.value)) return OptionalBoolean.of(entry.value); + return OptionalBoolean.empty(); } @Override @@ -2391,7 +2394,7 @@ public class Float2BooleanRBTreeMap extends AbstractFloat2BooleanMap implements } @Override - public Float2BooleanMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Float2BooleanMap.Entry state = null; boolean empty = true; @@ -2403,19 +2406,19 @@ public class Float2BooleanRBTreeMap extends AbstractFloat2BooleanMap implements } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Float2BooleanMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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/main/java/speiger/src/collections/floats/maps/impl/tree/Float2ByteAVLTreeMap.java b/src/main/java/speiger/src/collections/floats/maps/impl/tree/Float2ByteAVLTreeMap.java index 1d3c751..fb727cf 100644 --- a/src/main/java/speiger/src/collections/floats/maps/impl/tree/Float2ByteAVLTreeMap.java +++ b/src/main/java/speiger/src/collections/floats/maps/impl/tree/Float2ByteAVLTreeMap.java @@ -3,6 +3,7 @@ package speiger.src.collections.floats.maps.impl.tree; import java.util.Collections; import java.util.Map; import java.util.Objects; +import java.util.Optional; import java.util.NoSuchElementException; import java.util.function.Consumer; import java.util.function.BiFunction; @@ -18,6 +19,7 @@ import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntByteConsumer; import speiger.src.collections.floats.functions.function.Float2ByteFunction; import speiger.src.collections.floats.functions.consumer.FloatByteConsumer; +import speiger.src.collections.floats.functions.OptionalFloat; import speiger.src.collections.floats.functions.function.FloatPredicate; import speiger.src.collections.floats.functions.function.FloatByteUnaryOperator; import speiger.src.collections.floats.functions.function.FloatFloatUnaryOperator; @@ -37,6 +39,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectByteConsumer; import speiger.src.collections.bytes.functions.function.BytePredicate; +import speiger.src.collections.bytes.functions.OptionalByte; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOperator; @@ -1129,7 +1132,7 @@ public class Float2ByteAVLTreeMap extends AbstractFloat2ByteMap implements Float } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -1141,15 +1144,15 @@ public class Float2ByteAVLTreeMap extends AbstractFloat2ByteMap implements Float } state = operator.applyAsFloat(state, entry.key); } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); for(Node entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.test(entry.key)) return entry.key; - return 0F; + if(filter.test(entry.key)) return OptionalFloat.of(entry.key); + return OptionalFloat.empty(); } @Override @@ -1783,7 +1786,7 @@ public class Float2ByteAVLTreeMap extends AbstractFloat2ByteMap implements Float } @Override - public Float2ByteMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Float2ByteMap.Entry state = null; boolean empty = true; @@ -1795,19 +1798,19 @@ public class Float2ByteAVLTreeMap extends AbstractFloat2ByteMap implements Float } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Float2ByteMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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 @@ -1902,7 +1905,7 @@ public class Float2ByteAVLTreeMap extends AbstractFloat2ByteMap implements Float } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -1914,15 +1917,15 @@ public class Float2ByteAVLTreeMap extends AbstractFloat2ByteMap implements Float } state = operator.applyAsByte(state, entry.value); } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return (byte)0; + if(filter.test(entry.value)) return OptionalByte.of(entry.value); + return OptionalByte.empty(); } @Override @@ -2238,7 +2241,7 @@ public class Float2ByteAVLTreeMap extends AbstractFloat2ByteMap implements Float } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -2250,15 +2253,15 @@ public class Float2ByteAVLTreeMap extends AbstractFloat2ByteMap implements Float } state = operator.applyAsByte(state, entry.value); } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return (byte)0; + if(filter.test(entry.value)) return OptionalByte.of(entry.value); + return OptionalByte.empty(); } @Override @@ -2399,7 +2402,7 @@ public class Float2ByteAVLTreeMap extends AbstractFloat2ByteMap implements Float } @Override - public Float2ByteMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Float2ByteMap.Entry state = null; boolean empty = true; @@ -2411,19 +2414,19 @@ public class Float2ByteAVLTreeMap extends AbstractFloat2ByteMap implements Float } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Float2ByteMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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/main/java/speiger/src/collections/floats/maps/impl/tree/Float2ByteRBTreeMap.java b/src/main/java/speiger/src/collections/floats/maps/impl/tree/Float2ByteRBTreeMap.java index 7f11a44..e20ae1d 100644 --- a/src/main/java/speiger/src/collections/floats/maps/impl/tree/Float2ByteRBTreeMap.java +++ b/src/main/java/speiger/src/collections/floats/maps/impl/tree/Float2ByteRBTreeMap.java @@ -3,6 +3,7 @@ package speiger.src.collections.floats.maps.impl.tree; import java.util.Collections; import java.util.Map; import java.util.Objects; +import java.util.Optional; import java.util.NoSuchElementException; import java.util.function.Consumer; import java.util.function.BiFunction; @@ -17,6 +18,7 @@ import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntByteConsumer; import speiger.src.collections.floats.functions.function.Float2ByteFunction; import speiger.src.collections.floats.functions.consumer.FloatByteConsumer; +import speiger.src.collections.floats.functions.OptionalFloat; import speiger.src.collections.floats.functions.function.FloatPredicate; import speiger.src.collections.floats.functions.function.FloatByteUnaryOperator; import speiger.src.collections.floats.functions.function.FloatFloatUnaryOperator; @@ -36,6 +38,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectByteConsumer; import speiger.src.collections.bytes.functions.function.BytePredicate; +import speiger.src.collections.bytes.functions.OptionalByte; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOperator; @@ -1182,7 +1185,7 @@ public class Float2ByteRBTreeMap extends AbstractFloat2ByteMap implements Float2 } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -1194,15 +1197,15 @@ public class Float2ByteRBTreeMap extends AbstractFloat2ByteMap implements Float2 } state = operator.applyAsFloat(state, entry.key); } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); for(Node entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.test(entry.key)) return entry.key; - return 0F; + if(filter.test(entry.key)) return OptionalFloat.of(entry.key); + return OptionalFloat.empty(); } @Override @@ -1846,7 +1849,7 @@ public class Float2ByteRBTreeMap extends AbstractFloat2ByteMap implements Float2 } @Override - public Float2ByteMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Float2ByteMap.Entry state = null; boolean empty = true; @@ -1858,19 +1861,19 @@ public class Float2ByteRBTreeMap extends AbstractFloat2ByteMap implements Float2 } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Float2ByteMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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 @@ -1965,7 +1968,7 @@ public class Float2ByteRBTreeMap extends AbstractFloat2ByteMap implements Float2 } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -1977,15 +1980,15 @@ public class Float2ByteRBTreeMap extends AbstractFloat2ByteMap implements Float2 } state = operator.applyAsByte(state, entry.value); } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return (byte)0; + if(filter.test(entry.value)) return OptionalByte.of(entry.value); + return OptionalByte.empty(); } @Override @@ -2301,7 +2304,7 @@ public class Float2ByteRBTreeMap extends AbstractFloat2ByteMap implements Float2 } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -2313,15 +2316,15 @@ public class Float2ByteRBTreeMap extends AbstractFloat2ByteMap implements Float2 } state = operator.applyAsByte(state, entry.value); } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return (byte)0; + if(filter.test(entry.value)) return OptionalByte.of(entry.value); + return OptionalByte.empty(); } @Override @@ -2462,7 +2465,7 @@ public class Float2ByteRBTreeMap extends AbstractFloat2ByteMap implements Float2 } @Override - public Float2ByteMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Float2ByteMap.Entry state = null; boolean empty = true; @@ -2474,19 +2477,19 @@ public class Float2ByteRBTreeMap extends AbstractFloat2ByteMap implements Float2 } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Float2ByteMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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/main/java/speiger/src/collections/floats/maps/impl/tree/Float2CharAVLTreeMap.java b/src/main/java/speiger/src/collections/floats/maps/impl/tree/Float2CharAVLTreeMap.java index 9a2cc69..b5ed445 100644 --- a/src/main/java/speiger/src/collections/floats/maps/impl/tree/Float2CharAVLTreeMap.java +++ b/src/main/java/speiger/src/collections/floats/maps/impl/tree/Float2CharAVLTreeMap.java @@ -3,6 +3,7 @@ package speiger.src.collections.floats.maps.impl.tree; import java.util.Collections; import java.util.Map; import java.util.Objects; +import java.util.Optional; import java.util.NoSuchElementException; import java.util.function.Consumer; import java.util.function.BiFunction; @@ -18,6 +19,7 @@ import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntCharConsumer; import speiger.src.collections.floats.functions.function.Float2CharFunction; import speiger.src.collections.floats.functions.consumer.FloatCharConsumer; +import speiger.src.collections.floats.functions.OptionalFloat; import speiger.src.collections.floats.functions.function.FloatPredicate; import speiger.src.collections.floats.functions.function.FloatCharUnaryOperator; import speiger.src.collections.floats.functions.function.FloatFloatUnaryOperator; @@ -37,6 +39,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectCharConsumer; import speiger.src.collections.chars.functions.function.CharPredicate; +import speiger.src.collections.chars.functions.OptionalChar; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOperator; @@ -1129,7 +1132,7 @@ public class Float2CharAVLTreeMap extends AbstractFloat2CharMap implements Float } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -1141,15 +1144,15 @@ public class Float2CharAVLTreeMap extends AbstractFloat2CharMap implements Float } state = operator.applyAsFloat(state, entry.key); } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); for(Node entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.test(entry.key)) return entry.key; - return 0F; + if(filter.test(entry.key)) return OptionalFloat.of(entry.key); + return OptionalFloat.empty(); } @Override @@ -1783,7 +1786,7 @@ public class Float2CharAVLTreeMap extends AbstractFloat2CharMap implements Float } @Override - public Float2CharMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Float2CharMap.Entry state = null; boolean empty = true; @@ -1795,19 +1798,19 @@ public class Float2CharAVLTreeMap extends AbstractFloat2CharMap implements Float } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Float2CharMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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 @@ -1902,7 +1905,7 @@ public class Float2CharAVLTreeMap extends AbstractFloat2CharMap implements Float } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -1914,15 +1917,15 @@ public class Float2CharAVLTreeMap extends AbstractFloat2CharMap implements Float } state = operator.applyAsChar(state, entry.value); } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return (char)0; + if(filter.test(entry.value)) return OptionalChar.of(entry.value); + return OptionalChar.empty(); } @Override @@ -2238,7 +2241,7 @@ public class Float2CharAVLTreeMap extends AbstractFloat2CharMap implements Float } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -2250,15 +2253,15 @@ public class Float2CharAVLTreeMap extends AbstractFloat2CharMap implements Float } state = operator.applyAsChar(state, entry.value); } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return (char)0; + if(filter.test(entry.value)) return OptionalChar.of(entry.value); + return OptionalChar.empty(); } @Override @@ -2399,7 +2402,7 @@ public class Float2CharAVLTreeMap extends AbstractFloat2CharMap implements Float } @Override - public Float2CharMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Float2CharMap.Entry state = null; boolean empty = true; @@ -2411,19 +2414,19 @@ public class Float2CharAVLTreeMap extends AbstractFloat2CharMap implements Float } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Float2CharMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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/main/java/speiger/src/collections/floats/maps/impl/tree/Float2CharRBTreeMap.java b/src/main/java/speiger/src/collections/floats/maps/impl/tree/Float2CharRBTreeMap.java index fa584f7..f43ad91 100644 --- a/src/main/java/speiger/src/collections/floats/maps/impl/tree/Float2CharRBTreeMap.java +++ b/src/main/java/speiger/src/collections/floats/maps/impl/tree/Float2CharRBTreeMap.java @@ -3,6 +3,7 @@ package speiger.src.collections.floats.maps.impl.tree; import java.util.Collections; import java.util.Map; import java.util.Objects; +import java.util.Optional; import java.util.NoSuchElementException; import java.util.function.Consumer; import java.util.function.BiFunction; @@ -17,6 +18,7 @@ import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntCharConsumer; import speiger.src.collections.floats.functions.function.Float2CharFunction; import speiger.src.collections.floats.functions.consumer.FloatCharConsumer; +import speiger.src.collections.floats.functions.OptionalFloat; import speiger.src.collections.floats.functions.function.FloatPredicate; import speiger.src.collections.floats.functions.function.FloatCharUnaryOperator; import speiger.src.collections.floats.functions.function.FloatFloatUnaryOperator; @@ -36,6 +38,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectCharConsumer; import speiger.src.collections.chars.functions.function.CharPredicate; +import speiger.src.collections.chars.functions.OptionalChar; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOperator; @@ -1182,7 +1185,7 @@ public class Float2CharRBTreeMap extends AbstractFloat2CharMap implements Float2 } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -1194,15 +1197,15 @@ public class Float2CharRBTreeMap extends AbstractFloat2CharMap implements Float2 } state = operator.applyAsFloat(state, entry.key); } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); for(Node entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.test(entry.key)) return entry.key; - return 0F; + if(filter.test(entry.key)) return OptionalFloat.of(entry.key); + return OptionalFloat.empty(); } @Override @@ -1846,7 +1849,7 @@ public class Float2CharRBTreeMap extends AbstractFloat2CharMap implements Float2 } @Override - public Float2CharMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Float2CharMap.Entry state = null; boolean empty = true; @@ -1858,19 +1861,19 @@ public class Float2CharRBTreeMap extends AbstractFloat2CharMap implements Float2 } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Float2CharMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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 @@ -1965,7 +1968,7 @@ public class Float2CharRBTreeMap extends AbstractFloat2CharMap implements Float2 } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -1977,15 +1980,15 @@ public class Float2CharRBTreeMap extends AbstractFloat2CharMap implements Float2 } state = operator.applyAsChar(state, entry.value); } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return (char)0; + if(filter.test(entry.value)) return OptionalChar.of(entry.value); + return OptionalChar.empty(); } @Override @@ -2301,7 +2304,7 @@ public class Float2CharRBTreeMap extends AbstractFloat2CharMap implements Float2 } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -2313,15 +2316,15 @@ public class Float2CharRBTreeMap extends AbstractFloat2CharMap implements Float2 } state = operator.applyAsChar(state, entry.value); } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return (char)0; + if(filter.test(entry.value)) return OptionalChar.of(entry.value); + return OptionalChar.empty(); } @Override @@ -2462,7 +2465,7 @@ public class Float2CharRBTreeMap extends AbstractFloat2CharMap implements Float2 } @Override - public Float2CharMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Float2CharMap.Entry state = null; boolean empty = true; @@ -2474,19 +2477,19 @@ public class Float2CharRBTreeMap extends AbstractFloat2CharMap implements Float2 } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Float2CharMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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/main/java/speiger/src/collections/floats/maps/impl/tree/Float2DoubleAVLTreeMap.java b/src/main/java/speiger/src/collections/floats/maps/impl/tree/Float2DoubleAVLTreeMap.java index 1911638..5604c81 100644 --- a/src/main/java/speiger/src/collections/floats/maps/impl/tree/Float2DoubleAVLTreeMap.java +++ b/src/main/java/speiger/src/collections/floats/maps/impl/tree/Float2DoubleAVLTreeMap.java @@ -3,11 +3,13 @@ package speiger.src.collections.floats.maps.impl.tree; import java.util.Collections; import java.util.Map; 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; import java.util.function.DoublePredicate; +import java.util.OptionalDouble; import speiger.src.collections.floats.collections.FloatBidirectionalIterator; @@ -19,6 +21,7 @@ import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntDoubleConsumer; import speiger.src.collections.floats.functions.function.Float2DoubleFunction; import speiger.src.collections.floats.functions.consumer.FloatDoubleConsumer; +import speiger.src.collections.floats.functions.OptionalFloat; import speiger.src.collections.floats.functions.function.FloatPredicate; import speiger.src.collections.floats.functions.function.FloatDoubleUnaryOperator; import speiger.src.collections.floats.functions.function.FloatFloatUnaryOperator; @@ -1129,7 +1132,7 @@ public class Float2DoubleAVLTreeMap extends AbstractFloat2DoubleMap implements F } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -1141,15 +1144,15 @@ public class Float2DoubleAVLTreeMap extends AbstractFloat2DoubleMap implements F } state = operator.applyAsFloat(state, entry.key); } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); for(Node entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.test(entry.key)) return entry.key; - return 0F; + if(filter.test(entry.key)) return OptionalFloat.of(entry.key); + return OptionalFloat.empty(); } @Override @@ -1783,7 +1786,7 @@ public class Float2DoubleAVLTreeMap extends AbstractFloat2DoubleMap implements F } @Override - public Float2DoubleMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Float2DoubleMap.Entry state = null; boolean empty = true; @@ -1795,19 +1798,19 @@ public class Float2DoubleAVLTreeMap extends AbstractFloat2DoubleMap implements F } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Float2DoubleMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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 @@ -1902,7 +1905,7 @@ public class Float2DoubleAVLTreeMap extends AbstractFloat2DoubleMap implements F } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -1914,15 +1917,15 @@ public class Float2DoubleAVLTreeMap extends AbstractFloat2DoubleMap implements F } state = operator.applyAsDouble(state, entry.value); } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return 0D; + if(filter.test(entry.value)) return OptionalDouble.of(entry.value); + return OptionalDouble.empty(); } @Override @@ -2238,7 +2241,7 @@ public class Float2DoubleAVLTreeMap extends AbstractFloat2DoubleMap implements F } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -2250,15 +2253,15 @@ public class Float2DoubleAVLTreeMap extends AbstractFloat2DoubleMap implements F } state = operator.applyAsDouble(state, entry.value); } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return 0D; + if(filter.test(entry.value)) return OptionalDouble.of(entry.value); + return OptionalDouble.empty(); } @Override @@ -2399,7 +2402,7 @@ public class Float2DoubleAVLTreeMap extends AbstractFloat2DoubleMap implements F } @Override - public Float2DoubleMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Float2DoubleMap.Entry state = null; boolean empty = true; @@ -2411,19 +2414,19 @@ public class Float2DoubleAVLTreeMap extends AbstractFloat2DoubleMap implements F } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Float2DoubleMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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/main/java/speiger/src/collections/floats/maps/impl/tree/Float2DoubleRBTreeMap.java b/src/main/java/speiger/src/collections/floats/maps/impl/tree/Float2DoubleRBTreeMap.java index 41f64a0..25edad9 100644 --- a/src/main/java/speiger/src/collections/floats/maps/impl/tree/Float2DoubleRBTreeMap.java +++ b/src/main/java/speiger/src/collections/floats/maps/impl/tree/Float2DoubleRBTreeMap.java @@ -3,11 +3,13 @@ package speiger.src.collections.floats.maps.impl.tree; import java.util.Collections; import java.util.Map; 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; import java.util.function.DoublePredicate; +import java.util.OptionalDouble; import speiger.src.collections.floats.collections.FloatBidirectionalIterator; import speiger.src.collections.floats.functions.FloatComparator; @@ -18,6 +20,7 @@ import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntDoubleConsumer; import speiger.src.collections.floats.functions.function.Float2DoubleFunction; import speiger.src.collections.floats.functions.consumer.FloatDoubleConsumer; +import speiger.src.collections.floats.functions.OptionalFloat; import speiger.src.collections.floats.functions.function.FloatPredicate; import speiger.src.collections.floats.functions.function.FloatDoubleUnaryOperator; import speiger.src.collections.floats.functions.function.FloatFloatUnaryOperator; @@ -1182,7 +1185,7 @@ public class Float2DoubleRBTreeMap extends AbstractFloat2DoubleMap implements Fl } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -1194,15 +1197,15 @@ public class Float2DoubleRBTreeMap extends AbstractFloat2DoubleMap implements Fl } state = operator.applyAsFloat(state, entry.key); } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); for(Node entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.test(entry.key)) return entry.key; - return 0F; + if(filter.test(entry.key)) return OptionalFloat.of(entry.key); + return OptionalFloat.empty(); } @Override @@ -1846,7 +1849,7 @@ public class Float2DoubleRBTreeMap extends AbstractFloat2DoubleMap implements Fl } @Override - public Float2DoubleMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Float2DoubleMap.Entry state = null; boolean empty = true; @@ -1858,19 +1861,19 @@ public class Float2DoubleRBTreeMap extends AbstractFloat2DoubleMap implements Fl } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Float2DoubleMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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 @@ -1965,7 +1968,7 @@ public class Float2DoubleRBTreeMap extends AbstractFloat2DoubleMap implements Fl } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -1977,15 +1980,15 @@ public class Float2DoubleRBTreeMap extends AbstractFloat2DoubleMap implements Fl } state = operator.applyAsDouble(state, entry.value); } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return 0D; + if(filter.test(entry.value)) return OptionalDouble.of(entry.value); + return OptionalDouble.empty(); } @Override @@ -2301,7 +2304,7 @@ public class Float2DoubleRBTreeMap extends AbstractFloat2DoubleMap implements Fl } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -2313,15 +2316,15 @@ public class Float2DoubleRBTreeMap extends AbstractFloat2DoubleMap implements Fl } state = operator.applyAsDouble(state, entry.value); } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return 0D; + if(filter.test(entry.value)) return OptionalDouble.of(entry.value); + return OptionalDouble.empty(); } @Override @@ -2462,7 +2465,7 @@ public class Float2DoubleRBTreeMap extends AbstractFloat2DoubleMap implements Fl } @Override - public Float2DoubleMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Float2DoubleMap.Entry state = null; boolean empty = true; @@ -2474,19 +2477,19 @@ public class Float2DoubleRBTreeMap extends AbstractFloat2DoubleMap implements Fl } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Float2DoubleMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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/main/java/speiger/src/collections/floats/maps/impl/tree/Float2FloatAVLTreeMap.java b/src/main/java/speiger/src/collections/floats/maps/impl/tree/Float2FloatAVLTreeMap.java index c6de83b..d2f5678 100644 --- a/src/main/java/speiger/src/collections/floats/maps/impl/tree/Float2FloatAVLTreeMap.java +++ b/src/main/java/speiger/src/collections/floats/maps/impl/tree/Float2FloatAVLTreeMap.java @@ -3,6 +3,7 @@ package speiger.src.collections.floats.maps.impl.tree; import java.util.Collections; import java.util.Map; import java.util.Objects; +import java.util.Optional; import java.util.NoSuchElementException; import java.util.function.Consumer; import java.util.function.BiFunction; @@ -17,6 +18,7 @@ import speiger.src.collections.ints.functions.consumer.IntFloatConsumer; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.floats.functions.function.FloatUnaryOperator; import speiger.src.collections.floats.functions.consumer.FloatFloatConsumer; +import speiger.src.collections.floats.functions.OptionalFloat; import speiger.src.collections.floats.functions.function.FloatPredicate; import speiger.src.collections.floats.functions.function.FloatFloatUnaryOperator; import speiger.src.collections.floats.maps.abstracts.AbstractFloat2FloatMap; @@ -1121,7 +1123,7 @@ public class Float2FloatAVLTreeMap extends AbstractFloat2FloatMap implements Flo } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -1133,15 +1135,15 @@ public class Float2FloatAVLTreeMap extends AbstractFloat2FloatMap implements Flo } state = operator.applyAsFloat(state, entry.key); } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); for(Node entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.test(entry.key)) return entry.key; - return 0F; + if(filter.test(entry.key)) return OptionalFloat.of(entry.key); + return OptionalFloat.empty(); } @Override @@ -1775,7 +1777,7 @@ public class Float2FloatAVLTreeMap extends AbstractFloat2FloatMap implements Flo } @Override - public Float2FloatMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Float2FloatMap.Entry state = null; boolean empty = true; @@ -1787,19 +1789,19 @@ public class Float2FloatAVLTreeMap extends AbstractFloat2FloatMap implements Flo } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Float2FloatMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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 @@ -1894,7 +1896,7 @@ public class Float2FloatAVLTreeMap extends AbstractFloat2FloatMap implements Flo } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -1906,15 +1908,15 @@ public class Float2FloatAVLTreeMap extends AbstractFloat2FloatMap implements Flo } state = operator.applyAsFloat(state, entry.value); } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return 0F; + if(filter.test(entry.value)) return OptionalFloat.of(entry.value); + return OptionalFloat.empty(); } @Override @@ -2230,7 +2232,7 @@ public class Float2FloatAVLTreeMap extends AbstractFloat2FloatMap implements Flo } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -2242,15 +2244,15 @@ public class Float2FloatAVLTreeMap extends AbstractFloat2FloatMap implements Flo } state = operator.applyAsFloat(state, entry.value); } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return 0F; + if(filter.test(entry.value)) return OptionalFloat.of(entry.value); + return OptionalFloat.empty(); } @Override @@ -2391,7 +2393,7 @@ public class Float2FloatAVLTreeMap extends AbstractFloat2FloatMap implements Flo } @Override - public Float2FloatMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Float2FloatMap.Entry state = null; boolean empty = true; @@ -2403,19 +2405,19 @@ public class Float2FloatAVLTreeMap extends AbstractFloat2FloatMap implements Flo } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Float2FloatMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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/main/java/speiger/src/collections/floats/maps/impl/tree/Float2FloatRBTreeMap.java b/src/main/java/speiger/src/collections/floats/maps/impl/tree/Float2FloatRBTreeMap.java index 52665ce..c9e0cff 100644 --- a/src/main/java/speiger/src/collections/floats/maps/impl/tree/Float2FloatRBTreeMap.java +++ b/src/main/java/speiger/src/collections/floats/maps/impl/tree/Float2FloatRBTreeMap.java @@ -3,6 +3,7 @@ package speiger.src.collections.floats.maps.impl.tree; import java.util.Collections; import java.util.Map; import java.util.Objects; +import java.util.Optional; import java.util.NoSuchElementException; import java.util.function.Consumer; import java.util.function.BiFunction; @@ -16,6 +17,7 @@ import speiger.src.collections.ints.functions.consumer.IntFloatConsumer; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.floats.functions.function.FloatUnaryOperator; import speiger.src.collections.floats.functions.consumer.FloatFloatConsumer; +import speiger.src.collections.floats.functions.OptionalFloat; import speiger.src.collections.floats.functions.function.FloatPredicate; import speiger.src.collections.floats.functions.function.FloatFloatUnaryOperator; import speiger.src.collections.floats.maps.abstracts.AbstractFloat2FloatMap; @@ -1174,7 +1176,7 @@ public class Float2FloatRBTreeMap extends AbstractFloat2FloatMap implements Floa } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -1186,15 +1188,15 @@ public class Float2FloatRBTreeMap extends AbstractFloat2FloatMap implements Floa } state = operator.applyAsFloat(state, entry.key); } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); for(Node entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.test(entry.key)) return entry.key; - return 0F; + if(filter.test(entry.key)) return OptionalFloat.of(entry.key); + return OptionalFloat.empty(); } @Override @@ -1838,7 +1840,7 @@ public class Float2FloatRBTreeMap extends AbstractFloat2FloatMap implements Floa } @Override - public Float2FloatMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Float2FloatMap.Entry state = null; boolean empty = true; @@ -1850,19 +1852,19 @@ public class Float2FloatRBTreeMap extends AbstractFloat2FloatMap implements Floa } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Float2FloatMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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 @@ -1957,7 +1959,7 @@ public class Float2FloatRBTreeMap extends AbstractFloat2FloatMap implements Floa } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -1969,15 +1971,15 @@ public class Float2FloatRBTreeMap extends AbstractFloat2FloatMap implements Floa } state = operator.applyAsFloat(state, entry.value); } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return 0F; + if(filter.test(entry.value)) return OptionalFloat.of(entry.value); + return OptionalFloat.empty(); } @Override @@ -2293,7 +2295,7 @@ public class Float2FloatRBTreeMap extends AbstractFloat2FloatMap implements Floa } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -2305,15 +2307,15 @@ public class Float2FloatRBTreeMap extends AbstractFloat2FloatMap implements Floa } state = operator.applyAsFloat(state, entry.value); } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return 0F; + if(filter.test(entry.value)) return OptionalFloat.of(entry.value); + return OptionalFloat.empty(); } @Override @@ -2454,7 +2456,7 @@ public class Float2FloatRBTreeMap extends AbstractFloat2FloatMap implements Floa } @Override - public Float2FloatMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Float2FloatMap.Entry state = null; boolean empty = true; @@ -2466,19 +2468,19 @@ public class Float2FloatRBTreeMap extends AbstractFloat2FloatMap implements Floa } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Float2FloatMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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/main/java/speiger/src/collections/floats/maps/impl/tree/Float2IntAVLTreeMap.java b/src/main/java/speiger/src/collections/floats/maps/impl/tree/Float2IntAVLTreeMap.java index c764aec..462990e 100644 --- a/src/main/java/speiger/src/collections/floats/maps/impl/tree/Float2IntAVLTreeMap.java +++ b/src/main/java/speiger/src/collections/floats/maps/impl/tree/Float2IntAVLTreeMap.java @@ -3,11 +3,13 @@ package speiger.src.collections.floats.maps.impl.tree; import java.util.Collections; import java.util.Map; 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; import java.util.function.IntPredicate; +import java.util.OptionalInt; import speiger.src.collections.floats.collections.FloatBidirectionalIterator; @@ -19,6 +21,7 @@ import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntIntConsumer; import speiger.src.collections.floats.functions.function.Float2IntFunction; import speiger.src.collections.floats.functions.consumer.FloatIntConsumer; +import speiger.src.collections.floats.functions.OptionalFloat; import speiger.src.collections.floats.functions.function.FloatPredicate; import speiger.src.collections.floats.functions.function.FloatIntUnaryOperator; import speiger.src.collections.floats.functions.function.FloatFloatUnaryOperator; @@ -1129,7 +1132,7 @@ public class Float2IntAVLTreeMap extends AbstractFloat2IntMap implements Float2I } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -1141,15 +1144,15 @@ public class Float2IntAVLTreeMap extends AbstractFloat2IntMap implements Float2I } state = operator.applyAsFloat(state, entry.key); } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); for(Node entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.test(entry.key)) return entry.key; - return 0F; + if(filter.test(entry.key)) return OptionalFloat.of(entry.key); + return OptionalFloat.empty(); } @Override @@ -1783,7 +1786,7 @@ public class Float2IntAVLTreeMap extends AbstractFloat2IntMap implements Float2I } @Override - public Float2IntMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Float2IntMap.Entry state = null; boolean empty = true; @@ -1795,19 +1798,19 @@ public class Float2IntAVLTreeMap extends AbstractFloat2IntMap implements Float2I } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Float2IntMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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 @@ -1902,7 +1905,7 @@ public class Float2IntAVLTreeMap extends AbstractFloat2IntMap implements Float2I } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -1914,15 +1917,15 @@ public class Float2IntAVLTreeMap extends AbstractFloat2IntMap implements Float2I } state = operator.applyAsInt(state, entry.value); } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return 0; + if(filter.test(entry.value)) return OptionalInt.of(entry.value); + return OptionalInt.empty(); } @Override @@ -2238,7 +2241,7 @@ public class Float2IntAVLTreeMap extends AbstractFloat2IntMap implements Float2I } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -2250,15 +2253,15 @@ public class Float2IntAVLTreeMap extends AbstractFloat2IntMap implements Float2I } state = operator.applyAsInt(state, entry.value); } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return 0; + if(filter.test(entry.value)) return OptionalInt.of(entry.value); + return OptionalInt.empty(); } @Override @@ -2399,7 +2402,7 @@ public class Float2IntAVLTreeMap extends AbstractFloat2IntMap implements Float2I } @Override - public Float2IntMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Float2IntMap.Entry state = null; boolean empty = true; @@ -2411,19 +2414,19 @@ public class Float2IntAVLTreeMap extends AbstractFloat2IntMap implements Float2I } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Float2IntMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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/main/java/speiger/src/collections/floats/maps/impl/tree/Float2IntRBTreeMap.java b/src/main/java/speiger/src/collections/floats/maps/impl/tree/Float2IntRBTreeMap.java index b260194..820201d 100644 --- a/src/main/java/speiger/src/collections/floats/maps/impl/tree/Float2IntRBTreeMap.java +++ b/src/main/java/speiger/src/collections/floats/maps/impl/tree/Float2IntRBTreeMap.java @@ -3,11 +3,13 @@ package speiger.src.collections.floats.maps.impl.tree; import java.util.Collections; import java.util.Map; 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; import java.util.function.IntPredicate; +import java.util.OptionalInt; import speiger.src.collections.floats.collections.FloatBidirectionalIterator; import speiger.src.collections.floats.functions.FloatComparator; @@ -18,6 +20,7 @@ import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntIntConsumer; import speiger.src.collections.floats.functions.function.Float2IntFunction; import speiger.src.collections.floats.functions.consumer.FloatIntConsumer; +import speiger.src.collections.floats.functions.OptionalFloat; import speiger.src.collections.floats.functions.function.FloatPredicate; import speiger.src.collections.floats.functions.function.FloatIntUnaryOperator; import speiger.src.collections.floats.functions.function.FloatFloatUnaryOperator; @@ -1182,7 +1185,7 @@ public class Float2IntRBTreeMap extends AbstractFloat2IntMap implements Float2In } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -1194,15 +1197,15 @@ public class Float2IntRBTreeMap extends AbstractFloat2IntMap implements Float2In } state = operator.applyAsFloat(state, entry.key); } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); for(Node entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.test(entry.key)) return entry.key; - return 0F; + if(filter.test(entry.key)) return OptionalFloat.of(entry.key); + return OptionalFloat.empty(); } @Override @@ -1846,7 +1849,7 @@ public class Float2IntRBTreeMap extends AbstractFloat2IntMap implements Float2In } @Override - public Float2IntMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Float2IntMap.Entry state = null; boolean empty = true; @@ -1858,19 +1861,19 @@ public class Float2IntRBTreeMap extends AbstractFloat2IntMap implements Float2In } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Float2IntMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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 @@ -1965,7 +1968,7 @@ public class Float2IntRBTreeMap extends AbstractFloat2IntMap implements Float2In } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -1977,15 +1980,15 @@ public class Float2IntRBTreeMap extends AbstractFloat2IntMap implements Float2In } state = operator.applyAsInt(state, entry.value); } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return 0; + if(filter.test(entry.value)) return OptionalInt.of(entry.value); + return OptionalInt.empty(); } @Override @@ -2301,7 +2304,7 @@ public class Float2IntRBTreeMap extends AbstractFloat2IntMap implements Float2In } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -2313,15 +2316,15 @@ public class Float2IntRBTreeMap extends AbstractFloat2IntMap implements Float2In } state = operator.applyAsInt(state, entry.value); } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return 0; + if(filter.test(entry.value)) return OptionalInt.of(entry.value); + return OptionalInt.empty(); } @Override @@ -2462,7 +2465,7 @@ public class Float2IntRBTreeMap extends AbstractFloat2IntMap implements Float2In } @Override - public Float2IntMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Float2IntMap.Entry state = null; boolean empty = true; @@ -2474,19 +2477,19 @@ public class Float2IntRBTreeMap extends AbstractFloat2IntMap implements Float2In } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Float2IntMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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/main/java/speiger/src/collections/floats/maps/impl/tree/Float2LongAVLTreeMap.java b/src/main/java/speiger/src/collections/floats/maps/impl/tree/Float2LongAVLTreeMap.java index bd15c14..1ac3589 100644 --- a/src/main/java/speiger/src/collections/floats/maps/impl/tree/Float2LongAVLTreeMap.java +++ b/src/main/java/speiger/src/collections/floats/maps/impl/tree/Float2LongAVLTreeMap.java @@ -3,11 +3,13 @@ package speiger.src.collections.floats.maps.impl.tree; import java.util.Collections; import java.util.Map; 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; import java.util.function.LongPredicate; +import java.util.OptionalLong; import speiger.src.collections.floats.collections.FloatBidirectionalIterator; @@ -19,6 +21,7 @@ import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntLongConsumer; import speiger.src.collections.floats.functions.function.Float2LongFunction; import speiger.src.collections.floats.functions.consumer.FloatLongConsumer; +import speiger.src.collections.floats.functions.OptionalFloat; import speiger.src.collections.floats.functions.function.FloatPredicate; import speiger.src.collections.floats.functions.function.FloatLongUnaryOperator; import speiger.src.collections.floats.functions.function.FloatFloatUnaryOperator; @@ -1129,7 +1132,7 @@ public class Float2LongAVLTreeMap extends AbstractFloat2LongMap implements Float } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -1141,15 +1144,15 @@ public class Float2LongAVLTreeMap extends AbstractFloat2LongMap implements Float } state = operator.applyAsFloat(state, entry.key); } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); for(Node entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.test(entry.key)) return entry.key; - return 0F; + if(filter.test(entry.key)) return OptionalFloat.of(entry.key); + return OptionalFloat.empty(); } @Override @@ -1783,7 +1786,7 @@ public class Float2LongAVLTreeMap extends AbstractFloat2LongMap implements Float } @Override - public Float2LongMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Float2LongMap.Entry state = null; boolean empty = true; @@ -1795,19 +1798,19 @@ public class Float2LongAVLTreeMap extends AbstractFloat2LongMap implements Float } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Float2LongMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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 @@ -1902,7 +1905,7 @@ public class Float2LongAVLTreeMap extends AbstractFloat2LongMap implements Float } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -1914,15 +1917,15 @@ public class Float2LongAVLTreeMap extends AbstractFloat2LongMap implements Float } state = operator.applyAsLong(state, entry.value); } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return 0L; + if(filter.test(entry.value)) return OptionalLong.of(entry.value); + return OptionalLong.empty(); } @Override @@ -2238,7 +2241,7 @@ public class Float2LongAVLTreeMap extends AbstractFloat2LongMap implements Float } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -2250,15 +2253,15 @@ public class Float2LongAVLTreeMap extends AbstractFloat2LongMap implements Float } state = operator.applyAsLong(state, entry.value); } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return 0L; + if(filter.test(entry.value)) return OptionalLong.of(entry.value); + return OptionalLong.empty(); } @Override @@ -2399,7 +2402,7 @@ public class Float2LongAVLTreeMap extends AbstractFloat2LongMap implements Float } @Override - public Float2LongMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Float2LongMap.Entry state = null; boolean empty = true; @@ -2411,19 +2414,19 @@ public class Float2LongAVLTreeMap extends AbstractFloat2LongMap implements Float } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Float2LongMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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/main/java/speiger/src/collections/floats/maps/impl/tree/Float2LongRBTreeMap.java b/src/main/java/speiger/src/collections/floats/maps/impl/tree/Float2LongRBTreeMap.java index 2cf733e..a9e252a 100644 --- a/src/main/java/speiger/src/collections/floats/maps/impl/tree/Float2LongRBTreeMap.java +++ b/src/main/java/speiger/src/collections/floats/maps/impl/tree/Float2LongRBTreeMap.java @@ -3,11 +3,13 @@ package speiger.src.collections.floats.maps.impl.tree; import java.util.Collections; import java.util.Map; 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; import java.util.function.LongPredicate; +import java.util.OptionalLong; import speiger.src.collections.floats.collections.FloatBidirectionalIterator; import speiger.src.collections.floats.functions.FloatComparator; @@ -18,6 +20,7 @@ import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntLongConsumer; import speiger.src.collections.floats.functions.function.Float2LongFunction; import speiger.src.collections.floats.functions.consumer.FloatLongConsumer; +import speiger.src.collections.floats.functions.OptionalFloat; import speiger.src.collections.floats.functions.function.FloatPredicate; import speiger.src.collections.floats.functions.function.FloatLongUnaryOperator; import speiger.src.collections.floats.functions.function.FloatFloatUnaryOperator; @@ -1182,7 +1185,7 @@ public class Float2LongRBTreeMap extends AbstractFloat2LongMap implements Float2 } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -1194,15 +1197,15 @@ public class Float2LongRBTreeMap extends AbstractFloat2LongMap implements Float2 } state = operator.applyAsFloat(state, entry.key); } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); for(Node entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.test(entry.key)) return entry.key; - return 0F; + if(filter.test(entry.key)) return OptionalFloat.of(entry.key); + return OptionalFloat.empty(); } @Override @@ -1846,7 +1849,7 @@ public class Float2LongRBTreeMap extends AbstractFloat2LongMap implements Float2 } @Override - public Float2LongMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Float2LongMap.Entry state = null; boolean empty = true; @@ -1858,19 +1861,19 @@ public class Float2LongRBTreeMap extends AbstractFloat2LongMap implements Float2 } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Float2LongMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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 @@ -1965,7 +1968,7 @@ public class Float2LongRBTreeMap extends AbstractFloat2LongMap implements Float2 } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -1977,15 +1980,15 @@ public class Float2LongRBTreeMap extends AbstractFloat2LongMap implements Float2 } state = operator.applyAsLong(state, entry.value); } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return 0L; + if(filter.test(entry.value)) return OptionalLong.of(entry.value); + return OptionalLong.empty(); } @Override @@ -2301,7 +2304,7 @@ public class Float2LongRBTreeMap extends AbstractFloat2LongMap implements Float2 } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -2313,15 +2316,15 @@ public class Float2LongRBTreeMap extends AbstractFloat2LongMap implements Float2 } state = operator.applyAsLong(state, entry.value); } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return 0L; + if(filter.test(entry.value)) return OptionalLong.of(entry.value); + return OptionalLong.empty(); } @Override @@ -2462,7 +2465,7 @@ public class Float2LongRBTreeMap extends AbstractFloat2LongMap implements Float2 } @Override - public Float2LongMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Float2LongMap.Entry state = null; boolean empty = true; @@ -2474,19 +2477,19 @@ public class Float2LongRBTreeMap extends AbstractFloat2LongMap implements Float2 } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Float2LongMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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/main/java/speiger/src/collections/floats/maps/impl/tree/Float2ObjectAVLTreeMap.java b/src/main/java/speiger/src/collections/floats/maps/impl/tree/Float2ObjectAVLTreeMap.java index 3fba480..2b28c49 100644 --- a/src/main/java/speiger/src/collections/floats/maps/impl/tree/Float2ObjectAVLTreeMap.java +++ b/src/main/java/speiger/src/collections/floats/maps/impl/tree/Float2ObjectAVLTreeMap.java @@ -3,6 +3,7 @@ package speiger.src.collections.floats.maps.impl.tree; import java.util.Collections; import java.util.Map; import java.util.Objects; +import java.util.Optional; import java.util.NoSuchElementException; import java.util.function.Consumer; import java.util.function.BiFunction; @@ -17,6 +18,7 @@ import speiger.src.collections.ints.functions.consumer.IntFloatConsumer; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.floats.functions.function.FloatFunction; import speiger.src.collections.floats.functions.consumer.FloatObjectConsumer; +import speiger.src.collections.floats.functions.OptionalFloat; import speiger.src.collections.floats.functions.function.FloatPredicate; import speiger.src.collections.floats.functions.function.FloatObjectUnaryOperator; import speiger.src.collections.floats.functions.function.FloatFloatUnaryOperator; @@ -1017,7 +1019,7 @@ public class Float2ObjectAVLTreeMap extends AbstractFloat2ObjectMap implem } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -1029,15 +1031,15 @@ public class Float2ObjectAVLTreeMap extends AbstractFloat2ObjectMap implem } state = operator.applyAsFloat(state, entry.key); } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); for(Node entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.test(entry.key)) return entry.key; - return 0F; + if(filter.test(entry.key)) return OptionalFloat.of(entry.key); + return OptionalFloat.empty(); } @Override @@ -1664,7 +1666,7 @@ public class Float2ObjectAVLTreeMap extends AbstractFloat2ObjectMap implem } @Override - public Float2ObjectMap.Entry reduce(ObjectObjectUnaryOperator, Float2ObjectMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Float2ObjectMap.Entry> operator) { Objects.requireNonNull(operator); Float2ObjectMap.Entry state = null; boolean empty = true; @@ -1676,19 +1678,19 @@ public class Float2ObjectAVLTreeMap extends AbstractFloat2ObjectMap implem } state = operator.apply(state, new BasicEntry<>(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Float2ObjectMap.Entry findFirst(Predicate> filter) { + public Optional> findFirst(Predicate> filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry<>(); for(Node 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 @@ -1783,7 +1785,7 @@ public class Float2ObjectAVLTreeMap extends AbstractFloat2ObjectMap implem } @Override - public V reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); V state = null; boolean empty = true; @@ -1795,15 +1797,15 @@ public class Float2ObjectAVLTreeMap extends AbstractFloat2ObjectMap implem } state = operator.apply(state, entry.value); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public V findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return null; + if(filter.test(entry.value)) return Optional.ofNullable(entry.value); + return Optional.empty(); } @Override @@ -2119,7 +2121,7 @@ public class Float2ObjectAVLTreeMap extends AbstractFloat2ObjectMap implem } @Override - public V reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); V state = null; boolean empty = true; @@ -2131,15 +2133,15 @@ public class Float2ObjectAVLTreeMap extends AbstractFloat2ObjectMap implem } state = operator.apply(state, entry.value); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public V findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return null; + if(filter.test(entry.value)) return Optional.ofNullable(entry.value); + return Optional.empty(); } @Override @@ -2280,7 +2282,7 @@ public class Float2ObjectAVLTreeMap extends AbstractFloat2ObjectMap implem } @Override - public Float2ObjectMap.Entry reduce(ObjectObjectUnaryOperator, Float2ObjectMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Float2ObjectMap.Entry> operator) { Objects.requireNonNull(operator); Float2ObjectMap.Entry state = null; boolean empty = true; @@ -2292,19 +2294,19 @@ public class Float2ObjectAVLTreeMap extends AbstractFloat2ObjectMap implem } state = operator.apply(state, new BasicEntry<>(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Float2ObjectMap.Entry findFirst(Predicate> filter) { + public Optional> findFirst(Predicate> filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry<>(); for(Node 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/main/java/speiger/src/collections/floats/maps/impl/tree/Float2ObjectRBTreeMap.java b/src/main/java/speiger/src/collections/floats/maps/impl/tree/Float2ObjectRBTreeMap.java index 7ece5d8..443e47e 100644 --- a/src/main/java/speiger/src/collections/floats/maps/impl/tree/Float2ObjectRBTreeMap.java +++ b/src/main/java/speiger/src/collections/floats/maps/impl/tree/Float2ObjectRBTreeMap.java @@ -3,6 +3,7 @@ package speiger.src.collections.floats.maps.impl.tree; import java.util.Collections; import java.util.Map; import java.util.Objects; +import java.util.Optional; import java.util.NoSuchElementException; import java.util.function.Consumer; import java.util.function.BiFunction; @@ -16,6 +17,7 @@ import speiger.src.collections.ints.functions.consumer.IntFloatConsumer; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.floats.functions.function.FloatFunction; import speiger.src.collections.floats.functions.consumer.FloatObjectConsumer; +import speiger.src.collections.floats.functions.OptionalFloat; import speiger.src.collections.floats.functions.function.FloatPredicate; import speiger.src.collections.floats.functions.function.FloatObjectUnaryOperator; import speiger.src.collections.floats.functions.function.FloatFloatUnaryOperator; @@ -1070,7 +1072,7 @@ public class Float2ObjectRBTreeMap extends AbstractFloat2ObjectMap impleme } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -1082,15 +1084,15 @@ public class Float2ObjectRBTreeMap extends AbstractFloat2ObjectMap impleme } state = operator.applyAsFloat(state, entry.key); } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); for(Node entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.test(entry.key)) return entry.key; - return 0F; + if(filter.test(entry.key)) return OptionalFloat.of(entry.key); + return OptionalFloat.empty(); } @Override @@ -1727,7 +1729,7 @@ public class Float2ObjectRBTreeMap extends AbstractFloat2ObjectMap impleme } @Override - public Float2ObjectMap.Entry reduce(ObjectObjectUnaryOperator, Float2ObjectMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Float2ObjectMap.Entry> operator) { Objects.requireNonNull(operator); Float2ObjectMap.Entry state = null; boolean empty = true; @@ -1739,19 +1741,19 @@ public class Float2ObjectRBTreeMap extends AbstractFloat2ObjectMap impleme } state = operator.apply(state, new BasicEntry<>(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Float2ObjectMap.Entry findFirst(Predicate> filter) { + public Optional> findFirst(Predicate> filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry<>(); for(Node 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 @@ -1846,7 +1848,7 @@ public class Float2ObjectRBTreeMap extends AbstractFloat2ObjectMap impleme } @Override - public V reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); V state = null; boolean empty = true; @@ -1858,15 +1860,15 @@ public class Float2ObjectRBTreeMap extends AbstractFloat2ObjectMap impleme } state = operator.apply(state, entry.value); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public V findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return null; + if(filter.test(entry.value)) return Optional.ofNullable(entry.value); + return Optional.empty(); } @Override @@ -2182,7 +2184,7 @@ public class Float2ObjectRBTreeMap extends AbstractFloat2ObjectMap impleme } @Override - public V reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); V state = null; boolean empty = true; @@ -2194,15 +2196,15 @@ public class Float2ObjectRBTreeMap extends AbstractFloat2ObjectMap impleme } state = operator.apply(state, entry.value); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public V findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return null; + if(filter.test(entry.value)) return Optional.ofNullable(entry.value); + return Optional.empty(); } @Override @@ -2343,7 +2345,7 @@ public class Float2ObjectRBTreeMap extends AbstractFloat2ObjectMap impleme } @Override - public Float2ObjectMap.Entry reduce(ObjectObjectUnaryOperator, Float2ObjectMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Float2ObjectMap.Entry> operator) { Objects.requireNonNull(operator); Float2ObjectMap.Entry state = null; boolean empty = true; @@ -2355,19 +2357,19 @@ public class Float2ObjectRBTreeMap extends AbstractFloat2ObjectMap impleme } state = operator.apply(state, new BasicEntry<>(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Float2ObjectMap.Entry findFirst(Predicate> filter) { + public Optional> findFirst(Predicate> filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry<>(); for(Node 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/main/java/speiger/src/collections/floats/maps/impl/tree/Float2ShortAVLTreeMap.java b/src/main/java/speiger/src/collections/floats/maps/impl/tree/Float2ShortAVLTreeMap.java index 127187e..33d7d16 100644 --- a/src/main/java/speiger/src/collections/floats/maps/impl/tree/Float2ShortAVLTreeMap.java +++ b/src/main/java/speiger/src/collections/floats/maps/impl/tree/Float2ShortAVLTreeMap.java @@ -3,6 +3,7 @@ package speiger.src.collections.floats.maps.impl.tree; import java.util.Collections; import java.util.Map; import java.util.Objects; +import java.util.Optional; import java.util.NoSuchElementException; import java.util.function.Consumer; import java.util.function.BiFunction; @@ -18,6 +19,7 @@ import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntShortConsumer; import speiger.src.collections.floats.functions.function.Float2ShortFunction; import speiger.src.collections.floats.functions.consumer.FloatShortConsumer; +import speiger.src.collections.floats.functions.OptionalFloat; import speiger.src.collections.floats.functions.function.FloatPredicate; import speiger.src.collections.floats.functions.function.FloatShortUnaryOperator; import speiger.src.collections.floats.functions.function.FloatFloatUnaryOperator; @@ -37,6 +39,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectShortConsumer; import speiger.src.collections.shorts.functions.function.ShortPredicate; +import speiger.src.collections.shorts.functions.OptionalShort; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOperator; @@ -1129,7 +1132,7 @@ public class Float2ShortAVLTreeMap extends AbstractFloat2ShortMap implements Flo } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -1141,15 +1144,15 @@ public class Float2ShortAVLTreeMap extends AbstractFloat2ShortMap implements Flo } state = operator.applyAsFloat(state, entry.key); } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); for(Node entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.test(entry.key)) return entry.key; - return 0F; + if(filter.test(entry.key)) return OptionalFloat.of(entry.key); + return OptionalFloat.empty(); } @Override @@ -1783,7 +1786,7 @@ public class Float2ShortAVLTreeMap extends AbstractFloat2ShortMap implements Flo } @Override - public Float2ShortMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Float2ShortMap.Entry state = null; boolean empty = true; @@ -1795,19 +1798,19 @@ public class Float2ShortAVLTreeMap extends AbstractFloat2ShortMap implements Flo } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Float2ShortMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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 @@ -1902,7 +1905,7 @@ public class Float2ShortAVLTreeMap extends AbstractFloat2ShortMap implements Flo } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -1914,15 +1917,15 @@ public class Float2ShortAVLTreeMap extends AbstractFloat2ShortMap implements Flo } state = operator.applyAsShort(state, entry.value); } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return (short)0; + if(filter.test(entry.value)) return OptionalShort.of(entry.value); + return OptionalShort.empty(); } @Override @@ -2238,7 +2241,7 @@ public class Float2ShortAVLTreeMap extends AbstractFloat2ShortMap implements Flo } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -2250,15 +2253,15 @@ public class Float2ShortAVLTreeMap extends AbstractFloat2ShortMap implements Flo } state = operator.applyAsShort(state, entry.value); } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return (short)0; + if(filter.test(entry.value)) return OptionalShort.of(entry.value); + return OptionalShort.empty(); } @Override @@ -2399,7 +2402,7 @@ public class Float2ShortAVLTreeMap extends AbstractFloat2ShortMap implements Flo } @Override - public Float2ShortMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Float2ShortMap.Entry state = null; boolean empty = true; @@ -2411,19 +2414,19 @@ public class Float2ShortAVLTreeMap extends AbstractFloat2ShortMap implements Flo } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Float2ShortMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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/main/java/speiger/src/collections/floats/maps/impl/tree/Float2ShortRBTreeMap.java b/src/main/java/speiger/src/collections/floats/maps/impl/tree/Float2ShortRBTreeMap.java index 5da5d2c..a4c2288 100644 --- a/src/main/java/speiger/src/collections/floats/maps/impl/tree/Float2ShortRBTreeMap.java +++ b/src/main/java/speiger/src/collections/floats/maps/impl/tree/Float2ShortRBTreeMap.java @@ -3,6 +3,7 @@ package speiger.src.collections.floats.maps.impl.tree; import java.util.Collections; import java.util.Map; import java.util.Objects; +import java.util.Optional; import java.util.NoSuchElementException; import java.util.function.Consumer; import java.util.function.BiFunction; @@ -17,6 +18,7 @@ import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntShortConsumer; import speiger.src.collections.floats.functions.function.Float2ShortFunction; import speiger.src.collections.floats.functions.consumer.FloatShortConsumer; +import speiger.src.collections.floats.functions.OptionalFloat; import speiger.src.collections.floats.functions.function.FloatPredicate; import speiger.src.collections.floats.functions.function.FloatShortUnaryOperator; import speiger.src.collections.floats.functions.function.FloatFloatUnaryOperator; @@ -36,6 +38,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectShortConsumer; import speiger.src.collections.shorts.functions.function.ShortPredicate; +import speiger.src.collections.shorts.functions.OptionalShort; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOperator; @@ -1182,7 +1185,7 @@ public class Float2ShortRBTreeMap extends AbstractFloat2ShortMap implements Floa } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -1194,15 +1197,15 @@ public class Float2ShortRBTreeMap extends AbstractFloat2ShortMap implements Floa } state = operator.applyAsFloat(state, entry.key); } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); for(Node entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.test(entry.key)) return entry.key; - return 0F; + if(filter.test(entry.key)) return OptionalFloat.of(entry.key); + return OptionalFloat.empty(); } @Override @@ -1846,7 +1849,7 @@ public class Float2ShortRBTreeMap extends AbstractFloat2ShortMap implements Floa } @Override - public Float2ShortMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Float2ShortMap.Entry state = null; boolean empty = true; @@ -1858,19 +1861,19 @@ public class Float2ShortRBTreeMap extends AbstractFloat2ShortMap implements Floa } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Float2ShortMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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 @@ -1965,7 +1968,7 @@ public class Float2ShortRBTreeMap extends AbstractFloat2ShortMap implements Floa } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -1977,15 +1980,15 @@ public class Float2ShortRBTreeMap extends AbstractFloat2ShortMap implements Floa } state = operator.applyAsShort(state, entry.value); } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return (short)0; + if(filter.test(entry.value)) return OptionalShort.of(entry.value); + return OptionalShort.empty(); } @Override @@ -2301,7 +2304,7 @@ public class Float2ShortRBTreeMap extends AbstractFloat2ShortMap implements Floa } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -2313,15 +2316,15 @@ public class Float2ShortRBTreeMap extends AbstractFloat2ShortMap implements Floa } state = operator.applyAsShort(state, entry.value); } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return (short)0; + if(filter.test(entry.value)) return OptionalShort.of(entry.value); + return OptionalShort.empty(); } @Override @@ -2462,7 +2465,7 @@ public class Float2ShortRBTreeMap extends AbstractFloat2ShortMap implements Floa } @Override - public Float2ShortMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Float2ShortMap.Entry state = null; boolean empty = true; @@ -2474,19 +2477,19 @@ public class Float2ShortRBTreeMap extends AbstractFloat2ShortMap implements Floa } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Float2ShortMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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/main/java/speiger/src/collections/floats/queues/FloatArrayFIFOQueue.java b/src/main/java/speiger/src/collections/floats/queues/FloatArrayFIFOQueue.java index 1cfb72d..7445d79 100644 --- a/src/main/java/speiger/src/collections/floats/queues/FloatArrayFIFOQueue.java +++ b/src/main/java/speiger/src/collections/floats/queues/FloatArrayFIFOQueue.java @@ -5,6 +5,7 @@ import java.util.Objects; import java.util.NoSuchElementException; import speiger.src.collections.floats.collections.FloatIterator; +import speiger.src.collections.floats.functions.OptionalFloat; import speiger.src.collections.floats.functions.FloatComparator; import speiger.src.collections.floats.functions.FloatConsumer; import speiger.src.collections.ints.functions.consumer.IntFloatConsumer; @@ -278,17 +279,17 @@ public class FloatArrayFIFOQueue extends AbstractFloatPriorityQueue implements F } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); for(int i = 0,m=size();i= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor)); + } + + @Override + public void addLast(float o) { + if(strategy.equals(o, 0F)) { + if(containsNull) return; + containsNull = true; + onNodeAdded(nullIndex); + } + else { + int pos = HashUtil.mix(strategy.hashCode(o)) & mask; + while(!strategy.equals(keys[pos], 0F)) { + if(strategy.equals(keys[pos], o)) return; + pos = ++pos & mask; + } + keys[pos] = o; + onNodeAdded(pos); + } + if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor)); + } + @Override public boolean addAndMoveToFirst(float o) { if(strategy.equals(o, 0F)) { @@ -618,7 +659,7 @@ public class FloatLinkedOpenCustomHashSet extends FloatOpenCustomHashSet impleme } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -631,18 +672,18 @@ public class FloatLinkedOpenCustomHashSet extends FloatOpenCustomHashSet impleme else state = operator.applyAsFloat(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); int index = firstIndex; while(index != -1) { - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalFloat.of(keys[index]); index = (int)links[index]; } - return 0F; + return OptionalFloat.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/floats/sets/FloatLinkedOpenHashSet.java b/src/main/java/speiger/src/collections/floats/sets/FloatLinkedOpenHashSet.java index 9626db9..cf3e244 100644 --- a/src/main/java/speiger/src/collections/floats/sets/FloatLinkedOpenHashSet.java +++ b/src/main/java/speiger/src/collections/floats/sets/FloatLinkedOpenHashSet.java @@ -10,6 +10,7 @@ import speiger.src.collections.floats.collections.FloatCollection; import speiger.src.collections.floats.collections.FloatIterator; import speiger.src.collections.floats.collections.FloatBidirectionalIterator; import speiger.src.collections.floats.functions.FloatConsumer; +import speiger.src.collections.floats.functions.OptionalFloat; import speiger.src.collections.ints.functions.consumer.IntFloatConsumer; import speiger.src.collections.objects.functions.consumer.ObjectFloatConsumer; import speiger.src.collections.floats.functions.function.FloatPredicate; @@ -183,6 +184,46 @@ public class FloatLinkedOpenHashSet extends FloatOpenHashSet implements FloatOrd while(iterator.hasNext()) add(iterator.nextFloat()); } + @Override + public void addFirst(float o) { + if(Float.floatToIntBits(o) == 0) { + if(containsNull) return; + containsNull = true; + onNodeAdded(nullIndex); + moveToFirstIndex(nullIndex); + } + else { + int pos = HashUtil.mix(Float.hashCode(o)) & mask; + while(Float.floatToIntBits(keys[pos]) != 0) { + if(Float.floatToIntBits(keys[pos]) == Float.floatToIntBits(o)) return; + pos = ++pos & mask; + } + keys[pos] = o; + onNodeAdded(pos); + moveToFirstIndex(pos); + } + if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor)); + } + + @Override + public void addLast(float o) { + if(Float.floatToIntBits(o) == 0) { + if(containsNull) return; + containsNull = true; + onNodeAdded(nullIndex); + } + else { + int pos = HashUtil.mix(Float.hashCode(o)) & mask; + while(Float.floatToIntBits(keys[pos]) != 0) { + if(Float.floatToIntBits(keys[pos]) == Float.floatToIntBits(o)) return; + pos = ++pos & mask; + } + keys[pos] = o; + onNodeAdded(pos); + } + if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor)); + } + @Override public boolean addAndMoveToFirst(float o) { if(Float.floatToIntBits(o) == 0) { @@ -471,7 +512,7 @@ public class FloatLinkedOpenHashSet extends FloatOpenHashSet implements FloatOrd } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -484,18 +525,18 @@ public class FloatLinkedOpenHashSet extends FloatOpenHashSet implements FloatOrd else state = operator.applyAsFloat(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); int index = firstIndex; while(index != -1) { - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalFloat.of(keys[index]); index = (int)links[index]; } - return 0F; + return OptionalFloat.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/floats/sets/FloatOpenCustomHashSet.java b/src/main/java/speiger/src/collections/floats/sets/FloatOpenCustomHashSet.java index 76826a9..b01770e 100644 --- a/src/main/java/speiger/src/collections/floats/sets/FloatOpenCustomHashSet.java +++ b/src/main/java/speiger/src/collections/floats/sets/FloatOpenCustomHashSet.java @@ -9,6 +9,7 @@ import java.util.Objects; import speiger.src.collections.floats.collections.FloatCollection; import speiger.src.collections.floats.collections.FloatIterator; +import speiger.src.collections.floats.functions.OptionalFloat; import speiger.src.collections.floats.utils.FloatIterators; import speiger.src.collections.floats.functions.FloatConsumer; import speiger.src.collections.ints.functions.consumer.IntFloatConsumer; @@ -546,7 +547,7 @@ public class FloatOpenCustomHashSet extends AbstractFloatSet implements ITrimmab } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -563,18 +564,18 @@ public class FloatOpenCustomHashSet extends AbstractFloatSet implements ITrimmab } state = operator.applyAsFloat(state, keys[i]); } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0F; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OptionalFloat.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OptionalFloat.of(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], 0F) && filter.test(keys[i])) return keys[i]; + if(!strategy.equals(keys[i], 0F) && filter.test(keys[i])) return OptionalFloat.of(keys[i]); } - return 0F; + return OptionalFloat.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/floats/sets/FloatOpenHashSet.java b/src/main/java/speiger/src/collections/floats/sets/FloatOpenHashSet.java index 33a9c1f..9d56d9a 100644 --- a/src/main/java/speiger/src/collections/floats/sets/FloatOpenHashSet.java +++ b/src/main/java/speiger/src/collections/floats/sets/FloatOpenHashSet.java @@ -11,6 +11,7 @@ import speiger.src.collections.floats.collections.FloatCollection; import speiger.src.collections.floats.collections.FloatIterator; import speiger.src.collections.floats.utils.FloatIterators; import speiger.src.collections.floats.functions.FloatConsumer; +import speiger.src.collections.floats.functions.OptionalFloat; import speiger.src.collections.ints.functions.consumer.IntFloatConsumer; import speiger.src.collections.objects.functions.consumer.ObjectFloatConsumer; import speiger.src.collections.floats.functions.function.FloatPredicate; @@ -428,7 +429,7 @@ public class FloatOpenHashSet extends AbstractFloatSet implements ITrimmable } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -445,18 +446,18 @@ public class FloatOpenHashSet extends AbstractFloatSet implements ITrimmable } state = operator.applyAsFloat(state, keys[i]); } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0F; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OptionalFloat.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OptionalFloat.of(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(Float.floatToIntBits(keys[i]) != 0 && filter.test(keys[i])) return keys[i]; + if(Float.floatToIntBits(keys[i]) != 0 && filter.test(keys[i])) return OptionalFloat.of(keys[i]); } - return 0F; + return OptionalFloat.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/floats/sets/FloatOrderedSet.java b/src/main/java/speiger/src/collections/floats/sets/FloatOrderedSet.java index f02948c..0524bfb 100644 --- a/src/main/java/speiger/src/collections/floats/sets/FloatOrderedSet.java +++ b/src/main/java/speiger/src/collections/floats/sets/FloatOrderedSet.java @@ -1,6 +1,7 @@ package speiger.src.collections.floats.sets; import speiger.src.collections.floats.collections.FloatBidirectionalIterator; +import speiger.src.collections.floats.collections.FloatOrderedCollection; import speiger.src.collections.floats.collections.FloatSplititerator; import speiger.src.collections.floats.utils.FloatSplititerators; import speiger.src.collections.floats.sets.AbstractFloatSet.ReversedFloatOrderedSet; @@ -13,7 +14,7 @@ import speiger.src.collections.floats.utils.FloatSets; * This interface provides basic access to such functions while also providing some Sorted/NaivgableSet implementations that still fit into here. * */ -public interface FloatOrderedSet extends FloatSet +public interface FloatOrderedSet extends FloatSet, FloatOrderedCollection { /** * A customized add method that allows you to insert into the first index. diff --git a/src/main/java/speiger/src/collections/floats/sets/FloatRBTreeSet.java b/src/main/java/speiger/src/collections/floats/sets/FloatRBTreeSet.java index 4eb9300..b033fbf 100644 --- a/src/main/java/speiger/src/collections/floats/sets/FloatRBTreeSet.java +++ b/src/main/java/speiger/src/collections/floats/sets/FloatRBTreeSet.java @@ -9,6 +9,7 @@ import java.util.Objects; import speiger.src.collections.floats.collections.FloatBidirectionalIterator; import speiger.src.collections.floats.functions.FloatComparator; import speiger.src.collections.floats.functions.FloatConsumer; +import speiger.src.collections.floats.functions.OptionalFloat; import speiger.src.collections.ints.functions.consumer.IntFloatConsumer; import speiger.src.collections.objects.functions.consumer.ObjectFloatConsumer; import speiger.src.collections.floats.functions.function.FloatPredicate; @@ -334,7 +335,7 @@ public class FloatRBTreeSet extends AbstractFloatSet implements FloatNavigableSe } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -346,16 +347,16 @@ public class FloatRBTreeSet extends AbstractFloatSet implements FloatNavigableSe } state = operator.applyAsFloat(state, entry.key); } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); for(Entry entry = first;entry != null;entry = entry.next()) { - if(filter.test(entry.key)) return entry.key; + if(filter.test(entry.key)) return OptionalFloat.of(entry.key); } - return 0F; + return OptionalFloat.empty(); } @Override @@ -1241,7 +1242,7 @@ public class FloatRBTreeSet extends AbstractFloatSet implements FloatNavigableSe } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -1253,16 +1254,16 @@ public class FloatRBTreeSet extends AbstractFloatSet implements FloatNavigableSe } state = operator.applyAsFloat(state, entry.key); } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); for(Entry entry = start();entry != null && inRange(entry.key);entry = next(entry)) { - if(filter.test(entry.key)) return entry.key; + if(filter.test(entry.key)) return OptionalFloat.of(entry.key); } - return 0F; + return OptionalFloat.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/floats/sets/ImmutableFloatOpenHashSet.java b/src/main/java/speiger/src/collections/floats/sets/ImmutableFloatOpenHashSet.java index 54b3805..d48b335 100644 --- a/src/main/java/speiger/src/collections/floats/sets/ImmutableFloatOpenHashSet.java +++ b/src/main/java/speiger/src/collections/floats/sets/ImmutableFloatOpenHashSet.java @@ -10,6 +10,7 @@ import speiger.src.collections.floats.collections.FloatBidirectionalIterator; import speiger.src.collections.floats.collections.FloatCollection; import speiger.src.collections.floats.collections.FloatIterator; import speiger.src.collections.floats.functions.FloatConsumer; +import speiger.src.collections.floats.functions.OptionalFloat; import speiger.src.collections.ints.functions.consumer.IntFloatConsumer; import speiger.src.collections.objects.functions.consumer.ObjectFloatConsumer; import speiger.src.collections.floats.functions.function.FloatPredicate; @@ -236,6 +237,10 @@ public class ImmutableFloatOpenHashSet extends AbstractFloatSet implements Float @Override public boolean addAll(FloatCollection c) { throw new UnsupportedOperationException(); } @Override + public void addFirst(float o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(float o) { throw new UnsupportedOperationException(); } + @Override public boolean addAndMoveToFirst(float o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToLast(float o) { throw new UnsupportedOperationException(); } @@ -371,7 +376,7 @@ public class ImmutableFloatOpenHashSet extends AbstractFloatSet implements Float } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -384,18 +389,18 @@ public class ImmutableFloatOpenHashSet extends AbstractFloatSet implements Float else state = operator.applyAsFloat(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); int index = firstIndex; while(index != -1) { - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalFloat.of(keys[index]); index = (int)links[index]; } - return 0F; + return OptionalFloat.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/floats/utils/FloatCollections.java b/src/main/java/speiger/src/collections/floats/utils/FloatCollections.java index f471905..f6ee3db 100644 --- a/src/main/java/speiger/src/collections/floats/utils/FloatCollections.java +++ b/src/main/java/speiger/src/collections/floats/utils/FloatCollections.java @@ -15,6 +15,7 @@ import speiger.src.collections.floats.collections.FloatCollection; import speiger.src.collections.floats.collections.FloatOrderedCollection; import speiger.src.collections.floats.collections.FloatIterator; import speiger.src.collections.floats.functions.FloatComparator; +import speiger.src.collections.floats.functions.OptionalFloat; import speiger.src.collections.objects.utils.ObjectArrays; import speiger.src.collections.floats.functions.FloatConsumer; import speiger.src.collections.floats.functions.function.FloatPredicate; @@ -758,9 +759,9 @@ public class FloatCollections @Override public float reduce(float identity, FloatFloatUnaryOperator operator) { synchronized(mutex) { return c.reduce(identity, operator); } } @Override - public float reduce(FloatFloatUnaryOperator operator) { synchronized(mutex) { return c.reduce(operator); } } + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { synchronized(mutex) { return c.reduce(operator); } } @Override - public float findFirst(FloatPredicate filter) { synchronized(mutex) { return c.findFirst(filter); } } + public OptionalFloat findFirst(FloatPredicate filter) { synchronized(mutex) { return c.findFirst(filter); } } @Override public int count(FloatPredicate filter) { synchronized(mutex) { return c.count(filter); } } } @@ -888,9 +889,9 @@ public class FloatCollections @Override public float reduce(float identity, FloatFloatUnaryOperator operator) { return c.reduce(identity, operator); } @Override - public float reduce(FloatFloatUnaryOperator operator) { return c.reduce(operator); } + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { return c.reduce(operator); } @Override - public float findFirst(FloatPredicate filter) { return c.findFirst(filter); } + public OptionalFloat findFirst(FloatPredicate filter) { return c.findFirst(filter); } @Override public int count(FloatPredicate filter) { return c.count(filter); } } diff --git a/src/main/java/speiger/src/collections/floats/utils/FloatPriorityQueues.java b/src/main/java/speiger/src/collections/floats/utils/FloatPriorityQueues.java index c47e06f..687df19 100644 --- a/src/main/java/speiger/src/collections/floats/utils/FloatPriorityQueues.java +++ b/src/main/java/speiger/src/collections/floats/utils/FloatPriorityQueues.java @@ -1,13 +1,14 @@ package speiger.src.collections.floats.utils; - import speiger.src.collections.floats.collections.FloatIterator; import speiger.src.collections.floats.collections.FloatCollection; import speiger.src.collections.floats.functions.FloatComparator; +import speiger.src.collections.floats.functions.OptionalFloat; import speiger.src.collections.floats.queues.FloatPriorityDequeue; import speiger.src.collections.floats.queues.FloatPriorityQueue; import speiger.src.collections.floats.functions.FloatConsumer; import speiger.src.collections.floats.functions.function.FloatPredicate; +import speiger.src.collections.floats.functions.function.FloatFloatUnaryOperator; import speiger.src.collections.objects.functions.consumer.ObjectFloatConsumer; /** @@ -113,7 +114,11 @@ public class FloatPriorityQueues @Override public boolean matchesAll(FloatPredicate filter) { synchronized(mutex) { return queue.matchesAll(filter); } } @Override - public float findFirst(FloatPredicate filter) { synchronized(mutex) { return queue.findFirst(filter); } } + public OptionalFloat findFirst(FloatPredicate filter) { synchronized(mutex) { return queue.findFirst(filter); } } + @Override + public float reduce(float identity, FloatFloatUnaryOperator operator) { synchronized(mutex) { return queue.reduce(identity, operator); } } + @Override + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { synchronized(mutex) { return queue.reduce(operator); } } @Override public int count(FloatPredicate filter) { synchronized(mutex) { return queue.count(filter); } } } diff --git a/src/main/java/speiger/src/collections/floats/utils/FloatSets.java b/src/main/java/speiger/src/collections/floats/utils/FloatSets.java index 117b8e2..4ff6751 100644 --- a/src/main/java/speiger/src/collections/floats/utils/FloatSets.java +++ b/src/main/java/speiger/src/collections/floats/utils/FloatSets.java @@ -301,6 +301,10 @@ public class FloatSets s = c; } + @Override + public void addFirst(float o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(float o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(float o) { throw new UnsupportedOperationException(); } @Override @@ -573,7 +577,11 @@ public class FloatSets super(c, mutex); s = c; } - + + @Override + public void addFirst(float o) { synchronized(mutex) { s.addFirst(o); } } + @Override + public void addLast(float o) { synchronized(mutex) { s.addLast(o); } } @Override public boolean addAndMoveToFirst(float o) { synchronized(mutex) { return s.addAndMoveToFirst(o); } } @Override diff --git a/src/main/java/speiger/src/collections/floats/utils/maps/Float2BooleanMaps.java b/src/main/java/speiger/src/collections/floats/utils/maps/Float2BooleanMaps.java index 9f82fb6..7728cad 100644 --- a/src/main/java/speiger/src/collections/floats/utils/maps/Float2BooleanMaps.java +++ b/src/main/java/speiger/src/collections/floats/utils/maps/Float2BooleanMaps.java @@ -608,7 +608,11 @@ public class Float2BooleanMaps super(c); set = c; } - + + @Override + public void addFirst(Float2BooleanMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Float2BooleanMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Float2BooleanMap.Entry o) { throw new UnsupportedOperationException(); } @Override diff --git a/src/main/java/speiger/src/collections/floats/utils/maps/Float2ByteMaps.java b/src/main/java/speiger/src/collections/floats/utils/maps/Float2ByteMaps.java index 4b15398..6483410 100644 --- a/src/main/java/speiger/src/collections/floats/utils/maps/Float2ByteMaps.java +++ b/src/main/java/speiger/src/collections/floats/utils/maps/Float2ByteMaps.java @@ -620,7 +620,11 @@ public class Float2ByteMaps super(c); set = c; } - + + @Override + public void addFirst(Float2ByteMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Float2ByteMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Float2ByteMap.Entry o) { throw new UnsupportedOperationException(); } @Override diff --git a/src/main/java/speiger/src/collections/floats/utils/maps/Float2CharMaps.java b/src/main/java/speiger/src/collections/floats/utils/maps/Float2CharMaps.java index fa534e7..1c443b5 100644 --- a/src/main/java/speiger/src/collections/floats/utils/maps/Float2CharMaps.java +++ b/src/main/java/speiger/src/collections/floats/utils/maps/Float2CharMaps.java @@ -620,7 +620,11 @@ public class Float2CharMaps super(c); set = c; } - + + @Override + public void addFirst(Float2CharMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Float2CharMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Float2CharMap.Entry o) { throw new UnsupportedOperationException(); } @Override diff --git a/src/main/java/speiger/src/collections/floats/utils/maps/Float2DoubleMaps.java b/src/main/java/speiger/src/collections/floats/utils/maps/Float2DoubleMaps.java index 3114981..e166661 100644 --- a/src/main/java/speiger/src/collections/floats/utils/maps/Float2DoubleMaps.java +++ b/src/main/java/speiger/src/collections/floats/utils/maps/Float2DoubleMaps.java @@ -620,7 +620,11 @@ public class Float2DoubleMaps super(c); set = c; } - + + @Override + public void addFirst(Float2DoubleMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Float2DoubleMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Float2DoubleMap.Entry o) { throw new UnsupportedOperationException(); } @Override diff --git a/src/main/java/speiger/src/collections/floats/utils/maps/Float2FloatMaps.java b/src/main/java/speiger/src/collections/floats/utils/maps/Float2FloatMaps.java index 61fdfaa..6ff699e 100644 --- a/src/main/java/speiger/src/collections/floats/utils/maps/Float2FloatMaps.java +++ b/src/main/java/speiger/src/collections/floats/utils/maps/Float2FloatMaps.java @@ -619,7 +619,11 @@ public class Float2FloatMaps super(c); set = c; } - + + @Override + public void addFirst(Float2FloatMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Float2FloatMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Float2FloatMap.Entry o) { throw new UnsupportedOperationException(); } @Override diff --git a/src/main/java/speiger/src/collections/floats/utils/maps/Float2IntMaps.java b/src/main/java/speiger/src/collections/floats/utils/maps/Float2IntMaps.java index 164ed18..b0a1ebf 100644 --- a/src/main/java/speiger/src/collections/floats/utils/maps/Float2IntMaps.java +++ b/src/main/java/speiger/src/collections/floats/utils/maps/Float2IntMaps.java @@ -620,7 +620,11 @@ public class Float2IntMaps super(c); set = c; } - + + @Override + public void addFirst(Float2IntMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Float2IntMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Float2IntMap.Entry o) { throw new UnsupportedOperationException(); } @Override diff --git a/src/main/java/speiger/src/collections/floats/utils/maps/Float2LongMaps.java b/src/main/java/speiger/src/collections/floats/utils/maps/Float2LongMaps.java index f11cb08..156a46f 100644 --- a/src/main/java/speiger/src/collections/floats/utils/maps/Float2LongMaps.java +++ b/src/main/java/speiger/src/collections/floats/utils/maps/Float2LongMaps.java @@ -620,7 +620,11 @@ public class Float2LongMaps super(c); set = c; } - + + @Override + public void addFirst(Float2LongMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Float2LongMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Float2LongMap.Entry o) { throw new UnsupportedOperationException(); } @Override diff --git a/src/main/java/speiger/src/collections/floats/utils/maps/Float2ObjectMaps.java b/src/main/java/speiger/src/collections/floats/utils/maps/Float2ObjectMaps.java index 4351b2e..75a6960 100644 --- a/src/main/java/speiger/src/collections/floats/utils/maps/Float2ObjectMaps.java +++ b/src/main/java/speiger/src/collections/floats/utils/maps/Float2ObjectMaps.java @@ -611,7 +611,11 @@ public class Float2ObjectMaps super(c); set = c; } - + + @Override + public void addFirst(Float2ObjectMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Float2ObjectMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Float2ObjectMap.Entry o) { throw new UnsupportedOperationException(); } @Override diff --git a/src/main/java/speiger/src/collections/floats/utils/maps/Float2ShortMaps.java b/src/main/java/speiger/src/collections/floats/utils/maps/Float2ShortMaps.java index cfedf87..0cedff9 100644 --- a/src/main/java/speiger/src/collections/floats/utils/maps/Float2ShortMaps.java +++ b/src/main/java/speiger/src/collections/floats/utils/maps/Float2ShortMaps.java @@ -620,7 +620,11 @@ public class Float2ShortMaps super(c); set = c; } - + + @Override + public void addFirst(Float2ShortMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Float2ShortMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Float2ShortMap.Entry o) { throw new UnsupportedOperationException(); } @Override diff --git a/src/main/java/speiger/src/collections/ints/collections/IntIterable.java b/src/main/java/speiger/src/collections/ints/collections/IntIterable.java index c05c884..2fff54d 100644 --- a/src/main/java/speiger/src/collections/ints/collections/IntIterable.java +++ b/src/main/java/speiger/src/collections/ints/collections/IntIterable.java @@ -3,6 +3,7 @@ package speiger.src.collections.ints.collections; import java.util.Objects; import java.util.function.Consumer; import java.util.function.IntPredicate; +import java.util.OptionalInt; import speiger.src.collections.ints.functions.IntConsumer; @@ -281,13 +282,13 @@ public interface IntIterable extends Iterable * @param filter that should be applied * @return the found value or the null equivalent variant. */ - default int findFirst(IntPredicate filter) { + default OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); for(IntIterator iter = iterator();iter.hasNext();) { int entry = iter.nextInt(); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return OptionalInt.of(entry); } - return 0; + return OptionalInt.empty(); } /** @@ -312,7 +313,7 @@ public interface IntIterable extends Iterable * @param operator the operation that should be applied * @return the reduction result, returns null value if nothing was found */ - default int reduce(IntIntUnaryOperator operator) { + default OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -324,7 +325,7 @@ public interface IntIterable extends Iterable } state = operator.applyAsInt(state, iter.nextInt()); } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } /** diff --git a/src/main/java/speiger/src/collections/ints/lists/CopyOnWriteIntArrayList.java b/src/main/java/speiger/src/collections/ints/lists/CopyOnWriteIntArrayList.java index 35bab3f..29138fe 100644 --- a/src/main/java/speiger/src/collections/ints/lists/CopyOnWriteIntArrayList.java +++ b/src/main/java/speiger/src/collections/ints/lists/CopyOnWriteIntArrayList.java @@ -5,6 +5,7 @@ import java.util.Collection; import java.util.Iterator; import java.util.NoSuchElementException; import java.util.Objects; +import java.util.OptionalInt; import java.util.concurrent.locks.ReentrantLock; import java.util.function.Consumer; import java.util.function.Supplier; @@ -654,13 +655,13 @@ public class CopyOnWriteIntArrayList extends AbstractIntList implements ITrimmab } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); int[] data = this.data; for(int i = 0,m=data.length;i operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Int2BooleanMap.Entry state = null; boolean empty = true; @@ -778,11 +781,11 @@ public class Int2BooleanConcurrentOpenHashMap extends AbstractInt2BooleanMap imp seg.unlockRead(stamp); } } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Int2BooleanMap.Entry 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); Int2ByteMap.Entry state = null; boolean empty = true; @@ -791,11 +794,11 @@ public class Int2ByteConcurrentOpenHashMap extends AbstractInt2ByteMap implement seg.unlockRead(stamp); } } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Int2ByteMap.Entry 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); Int2CharMap.Entry state = null; boolean empty = true; @@ -791,11 +794,11 @@ public class Int2CharConcurrentOpenHashMap extends AbstractInt2CharMap implement seg.unlockRead(stamp); } } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Int2CharMap.Entry 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); Int2DoubleMap.Entry state = null; boolean empty = true; @@ -791,11 +794,11 @@ public class Int2DoubleConcurrentOpenHashMap extends AbstractInt2DoubleMap imple seg.unlockRead(stamp); } } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Int2DoubleMap.Entry 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); Int2FloatMap.Entry state = null; boolean empty = true; @@ -791,11 +794,11 @@ public class Int2FloatConcurrentOpenHashMap extends AbstractInt2FloatMap impleme seg.unlockRead(stamp); } } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Int2FloatMap.Entry 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); Int2IntMap.Entry state = null; boolean empty = true; @@ -784,11 +786,11 @@ public class Int2IntConcurrentOpenHashMap extends AbstractInt2IntMap implements seg.unlockRead(stamp); } } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Int2IntMap.Entry 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); Int2LongMap.Entry state = null; boolean empty = true; @@ -791,11 +794,11 @@ public class Int2LongConcurrentOpenHashMap extends AbstractInt2LongMap implement seg.unlockRead(stamp); } } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Int2LongMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); MapEntry entry = new MapEntry(); for(int i = 0,m=segments.length;i extends AbstractInt2ObjectMap } @Override - public Int2ObjectMap.Entry reduce(ObjectObjectUnaryOperator, Int2ObjectMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Int2ObjectMap.Entry> operator) { Objects.requireNonNull(operator); Int2ObjectMap.Entry state = null; boolean empty = true; @@ -738,11 +740,11 @@ public class Int2ObjectConcurrentOpenHashMap extends AbstractInt2ObjectMap seg.unlockRead(stamp); } } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Int2ObjectMap.Entry findFirst(Predicate> filter) { + public Optional> findFirst(Predicate> filter) { Objects.requireNonNull(filter); MapEntry entry = new MapEntry(); for(int i = 0,m=segments.length;i extends AbstractInt2ObjectMap try { while(index != -1) { entry.set(index, i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); index = (int)seg.links[index]; } } @@ -760,7 +762,7 @@ public class Int2ObjectConcurrentOpenHashMap extends AbstractInt2ObjectMap seg.unlockRead(stamp); } } - return null; + return Optional.empty(); } @Override @@ -1021,7 +1023,7 @@ public class Int2ObjectConcurrentOpenHashMap extends AbstractInt2ObjectMap } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -1045,11 +1047,11 @@ public class Int2ObjectConcurrentOpenHashMap extends AbstractInt2ObjectMap seg.unlockRead(stamp); } } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); for(int i = 0,m=segments.length;i seg = segments[i]; @@ -1057,7 +1059,7 @@ public class Int2ObjectConcurrentOpenHashMap extends AbstractInt2ObjectMap try { int index = seg.firstIndex; while(index != -1){ - if(filter.test(seg.keys[index])) return seg.keys[index]; + if(filter.test(seg.keys[index])) return OptionalInt.of(seg.keys[index]); index = (int)seg.links[index]; } } @@ -1065,7 +1067,7 @@ public class Int2ObjectConcurrentOpenHashMap extends AbstractInt2ObjectMap seg.unlockRead(stamp); } } - return 0; + return OptionalInt.empty(); } @Override @@ -1257,7 +1259,7 @@ public class Int2ObjectConcurrentOpenHashMap extends AbstractInt2ObjectMap } @Override - public V reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); V state = null; boolean empty = true; @@ -1281,20 +1283,20 @@ public class Int2ObjectConcurrentOpenHashMap extends AbstractInt2ObjectMap seg.unlockRead(stamp); } } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public V findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); for(int i = 0,m=segments.length;i seg = segments[i]; long stamp = seg.readLock(); try { int index = seg.firstIndex; while(index != -1){ - if(filter.test(seg.values[index])) return seg.values[index]; + if(filter.test(seg.values[index])) return Optional.ofNullable(seg.values[index]); index = (int)seg.links[index]; } } @@ -1302,7 +1304,7 @@ public class Int2ObjectConcurrentOpenHashMap extends AbstractInt2ObjectMap seg.unlockRead(stamp); } } - return null; + return Optional.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/ints/maps/impl/concurrent/Int2ShortConcurrentOpenHashMap.java b/src/main/java/speiger/src/collections/ints/maps/impl/concurrent/Int2ShortConcurrentOpenHashMap.java index f507ab2..bc853e3 100644 --- a/src/main/java/speiger/src/collections/ints/maps/impl/concurrent/Int2ShortConcurrentOpenHashMap.java +++ b/src/main/java/speiger/src/collections/ints/maps/impl/concurrent/Int2ShortConcurrentOpenHashMap.java @@ -4,11 +4,13 @@ import java.util.Arrays; import java.util.Map; import java.util.NoSuchElementException; import java.util.Objects; +import java.util.Optional; import java.util.concurrent.locks.StampedLock; import java.util.function.Consumer; import java.util.function.Predicate; import java.util.function.BiFunction; import java.util.function.IntPredicate; +import java.util.OptionalInt; import speiger.src.collections.ints.collections.IntBidirectionalIterator; import speiger.src.collections.ints.functions.IntConsumer; @@ -36,6 +38,7 @@ import speiger.src.collections.shorts.functions.ShortConsumer; import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectShortConsumer; import speiger.src.collections.shorts.functions.function.ShortPredicate; +import speiger.src.collections.shorts.functions.OptionalShort; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.objects.sets.AbstractObjectSet; import speiger.src.collections.objects.sets.ObjectSet; @@ -767,7 +770,7 @@ public class Int2ShortConcurrentOpenHashMap extends AbstractInt2ShortMap impleme } @Override - public Int2ShortMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Int2ShortMap.Entry state = null; boolean empty = true; @@ -791,11 +794,11 @@ public class Int2ShortConcurrentOpenHashMap extends AbstractInt2ShortMap impleme seg.unlockRead(stamp); } } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Int2ShortMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); MapEntry entry = new MapEntry(); for(int i = 0,m=segments.length;i implements Int2BooleanOrderedMap.FastOrderedSet { + @Override + public void addFirst(Int2BooleanMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Int2BooleanMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Int2BooleanMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -835,7 +842,7 @@ public class Int2BooleanLinkedOpenCustomHashMap extends Int2BooleanOpenCustomHas } @Override - public Int2BooleanMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Int2BooleanMap.Entry state = null; boolean empty = true; @@ -850,21 +857,21 @@ public class Int2BooleanLinkedOpenCustomHashMap extends Int2BooleanOpenCustomHas state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Int2BooleanMap.Entry 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 @@ -943,10 +950,13 @@ public class Int2BooleanLinkedOpenCustomHashMap extends Int2BooleanOpenCustomHas public boolean add(int o) { throw new UnsupportedOperationException(); } - + + @Override + public void addFirst(int o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(int o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(int o) { throw new UnsupportedOperationException(); } - @Override public boolean addAndMoveToLast(int o) { throw new UnsupportedOperationException(); } @@ -1089,7 +1099,7 @@ public class Int2BooleanLinkedOpenCustomHashMap extends Int2BooleanOpenCustomHas } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -1104,19 +1114,19 @@ public class Int2BooleanLinkedOpenCustomHashMap extends Int2BooleanOpenCustomHas state = operator.applyAsInt(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0; + if(size() <= 0) return OptionalInt.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalInt.of(keys[index]); index = (int)links[index]; } - return 0; + return OptionalInt.empty(); } @Override @@ -1251,7 +1261,7 @@ public class Int2BooleanLinkedOpenCustomHashMap extends Int2BooleanOpenCustomHas } @Override - public boolean reduce(BooleanBooleanUnaryOperator operator) { + public OptionalBoolean reduce(BooleanBooleanUnaryOperator operator) { Objects.requireNonNull(operator); boolean state = false; boolean empty = true; @@ -1266,19 +1276,19 @@ public class Int2BooleanLinkedOpenCustomHashMap extends Int2BooleanOpenCustomHas state = operator.applyAsBoolean(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalBoolean.empty() : OptionalBoolean.of(state); } @Override - public boolean findFirst(BooleanPredicate filter) { + public OptionalBoolean findFirst(BooleanPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return false; + if(size() <= 0) return OptionalBoolean.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalBoolean.of(values[index]); index = (int)links[index]; } - return false; + return OptionalBoolean.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/ints/maps/impl/customHash/Int2BooleanOpenCustomHashMap.java b/src/main/java/speiger/src/collections/ints/maps/impl/customHash/Int2BooleanOpenCustomHashMap.java index d22984b..f1c91f7 100644 --- a/src/main/java/speiger/src/collections/ints/maps/impl/customHash/Int2BooleanOpenCustomHashMap.java +++ b/src/main/java/speiger/src/collections/ints/maps/impl/customHash/Int2BooleanOpenCustomHashMap.java @@ -5,10 +5,12 @@ 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; import java.util.function.IntPredicate; +import java.util.OptionalInt; import speiger.src.collections.ints.collections.IntIterator; import speiger.src.collections.ints.functions.IntConsumer; @@ -35,6 +37,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectBooleanConsumer; import speiger.src.collections.booleans.functions.function.BooleanPredicate; +import speiger.src.collections.booleans.functions.OptionalBoolean; import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.sets.AbstractObjectSet; import speiger.src.collections.objects.sets.ObjectSet; @@ -929,7 +932,7 @@ public class Int2BooleanOpenCustomHashMap extends AbstractInt2BooleanMap impleme } @Override - public Int2BooleanMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Int2BooleanMap.Entry state = null; boolean empty = true; @@ -946,25 +949,25 @@ public class Int2BooleanOpenCustomHashMap extends AbstractInt2BooleanMap impleme } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Int2BooleanMap.Entry 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], 0)) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1138,7 +1141,7 @@ public class Int2BooleanOpenCustomHashMap extends AbstractInt2BooleanMap impleme } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -1155,18 +1158,18 @@ public class Int2BooleanOpenCustomHashMap extends AbstractInt2BooleanMap impleme } state = operator.applyAsInt(state, keys[i]); } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OptionalInt.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OptionalInt.of(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], 0) && filter.test(keys[i])) return keys[i]; + if(!strategy.equals(keys[i], 0) && filter.test(keys[i])) return OptionalInt.of(keys[i]); } - return 0; + return OptionalInt.empty(); } @Override @@ -1281,7 +1284,7 @@ public class Int2BooleanOpenCustomHashMap extends AbstractInt2BooleanMap impleme } @Override - public boolean reduce(BooleanBooleanUnaryOperator operator) { + public OptionalBoolean reduce(BooleanBooleanUnaryOperator operator) { Objects.requireNonNull(operator); boolean state = false; boolean empty = true; @@ -1298,18 +1301,18 @@ public class Int2BooleanOpenCustomHashMap extends AbstractInt2BooleanMap impleme } state = operator.applyAsBoolean(state, values[i]); } - return state; + return empty ? OptionalBoolean.empty() : OptionalBoolean.of(state); } @Override - public boolean findFirst(BooleanPredicate filter) { + public OptionalBoolean findFirst(BooleanPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return false; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return OptionalBoolean.empty(); + if(containsNull && filter.test(values[nullIndex])) return OptionalBoolean.of(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], 0) && filter.test(values[i])) return values[i]; + if(!strategy.equals(keys[i], 0) && filter.test(values[i])) return OptionalBoolean.of(values[i]); } - return false; + return OptionalBoolean.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/ints/maps/impl/customHash/Int2ByteLinkedOpenCustomHashMap.java b/src/main/java/speiger/src/collections/ints/maps/impl/customHash/Int2ByteLinkedOpenCustomHashMap.java index d2c76b6..69ebfcd 100644 --- a/src/main/java/speiger/src/collections/ints/maps/impl/customHash/Int2ByteLinkedOpenCustomHashMap.java +++ b/src/main/java/speiger/src/collections/ints/maps/impl/customHash/Int2ByteLinkedOpenCustomHashMap.java @@ -4,10 +4,12 @@ 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; import java.util.function.IntPredicate; +import java.util.OptionalInt; import speiger.src.collections.ints.collections.IntBidirectionalIterator; import speiger.src.collections.ints.functions.IntConsumer; @@ -32,6 +34,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectByteConsumer; import speiger.src.collections.bytes.functions.function.BytePredicate; +import speiger.src.collections.bytes.functions.OptionalByte; import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOperator; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.objects.lists.ObjectListIterator; @@ -670,6 +673,10 @@ public class Int2ByteLinkedOpenCustomHashMap extends Int2ByteOpenCustomHashMap i } private class MapEntrySet extends AbstractObjectSet implements Int2ByteOrderedMap.FastOrderedSet { + @Override + public void addFirst(Int2ByteMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Int2ByteMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Int2ByteMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -835,7 +842,7 @@ public class Int2ByteLinkedOpenCustomHashMap extends Int2ByteOpenCustomHashMap i } @Override - public Int2ByteMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Int2ByteMap.Entry state = null; boolean empty = true; @@ -850,21 +857,21 @@ public class Int2ByteLinkedOpenCustomHashMap extends Int2ByteOpenCustomHashMap i state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Int2ByteMap.Entry 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 @@ -943,10 +950,13 @@ public class Int2ByteLinkedOpenCustomHashMap extends Int2ByteOpenCustomHashMap i public boolean add(int o) { throw new UnsupportedOperationException(); } - + + @Override + public void addFirst(int o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(int o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(int o) { throw new UnsupportedOperationException(); } - @Override public boolean addAndMoveToLast(int o) { throw new UnsupportedOperationException(); } @@ -1089,7 +1099,7 @@ public class Int2ByteLinkedOpenCustomHashMap extends Int2ByteOpenCustomHashMap i } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -1104,19 +1114,19 @@ public class Int2ByteLinkedOpenCustomHashMap extends Int2ByteOpenCustomHashMap i state = operator.applyAsInt(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0; + if(size() <= 0) return OptionalInt.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalInt.of(keys[index]); index = (int)links[index]; } - return 0; + return OptionalInt.empty(); } @Override @@ -1251,7 +1261,7 @@ public class Int2ByteLinkedOpenCustomHashMap extends Int2ByteOpenCustomHashMap i } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -1266,19 +1276,19 @@ public class Int2ByteLinkedOpenCustomHashMap extends Int2ByteOpenCustomHashMap i state = operator.applyAsByte(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (byte)0; + if(size() <= 0) return OptionalByte.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalByte.of(values[index]); index = (int)links[index]; } - return (byte)0; + return OptionalByte.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/ints/maps/impl/customHash/Int2ByteOpenCustomHashMap.java b/src/main/java/speiger/src/collections/ints/maps/impl/customHash/Int2ByteOpenCustomHashMap.java index 9a31c73..6b0266f 100644 --- a/src/main/java/speiger/src/collections/ints/maps/impl/customHash/Int2ByteOpenCustomHashMap.java +++ b/src/main/java/speiger/src/collections/ints/maps/impl/customHash/Int2ByteOpenCustomHashMap.java @@ -5,10 +5,12 @@ 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; import java.util.function.IntPredicate; +import java.util.OptionalInt; import speiger.src.collections.ints.collections.IntIterator; import speiger.src.collections.ints.functions.IntConsumer; @@ -36,6 +38,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectByteConsumer; import speiger.src.collections.bytes.functions.function.BytePredicate; +import speiger.src.collections.bytes.functions.OptionalByte; import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.sets.AbstractObjectSet; import speiger.src.collections.objects.sets.ObjectSet; @@ -951,7 +954,7 @@ public class Int2ByteOpenCustomHashMap extends AbstractInt2ByteMap implements IT } @Override - public Int2ByteMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Int2ByteMap.Entry state = null; boolean empty = true; @@ -968,25 +971,25 @@ public class Int2ByteOpenCustomHashMap extends AbstractInt2ByteMap implements IT } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Int2ByteMap.Entry 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], 0)) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1160,7 +1163,7 @@ public class Int2ByteOpenCustomHashMap extends AbstractInt2ByteMap implements IT } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -1177,18 +1180,18 @@ public class Int2ByteOpenCustomHashMap extends AbstractInt2ByteMap implements IT } state = operator.applyAsInt(state, keys[i]); } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OptionalInt.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OptionalInt.of(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], 0) && filter.test(keys[i])) return keys[i]; + if(!strategy.equals(keys[i], 0) && filter.test(keys[i])) return OptionalInt.of(keys[i]); } - return 0; + return OptionalInt.empty(); } @Override @@ -1303,7 +1306,7 @@ public class Int2ByteOpenCustomHashMap extends AbstractInt2ByteMap implements IT } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -1320,18 +1323,18 @@ public class Int2ByteOpenCustomHashMap extends AbstractInt2ByteMap implements IT } state = operator.applyAsByte(state, values[i]); } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (byte)0; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return OptionalByte.empty(); + if(containsNull && filter.test(values[nullIndex])) return OptionalByte.of(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], 0) && filter.test(values[i])) return values[i]; + if(!strategy.equals(keys[i], 0) && filter.test(values[i])) return OptionalByte.of(values[i]); } - return (byte)0; + return OptionalByte.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/ints/maps/impl/customHash/Int2CharLinkedOpenCustomHashMap.java b/src/main/java/speiger/src/collections/ints/maps/impl/customHash/Int2CharLinkedOpenCustomHashMap.java index 5731e04..01e2af5 100644 --- a/src/main/java/speiger/src/collections/ints/maps/impl/customHash/Int2CharLinkedOpenCustomHashMap.java +++ b/src/main/java/speiger/src/collections/ints/maps/impl/customHash/Int2CharLinkedOpenCustomHashMap.java @@ -4,10 +4,12 @@ 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; import java.util.function.IntPredicate; +import java.util.OptionalInt; import speiger.src.collections.ints.collections.IntBidirectionalIterator; import speiger.src.collections.ints.functions.IntConsumer; @@ -32,6 +34,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectCharConsumer; import speiger.src.collections.chars.functions.function.CharPredicate; +import speiger.src.collections.chars.functions.OptionalChar; import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOperator; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.objects.lists.ObjectListIterator; @@ -670,6 +673,10 @@ public class Int2CharLinkedOpenCustomHashMap extends Int2CharOpenCustomHashMap i } private class MapEntrySet extends AbstractObjectSet implements Int2CharOrderedMap.FastOrderedSet { + @Override + public void addFirst(Int2CharMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Int2CharMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Int2CharMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -835,7 +842,7 @@ public class Int2CharLinkedOpenCustomHashMap extends Int2CharOpenCustomHashMap i } @Override - public Int2CharMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Int2CharMap.Entry state = null; boolean empty = true; @@ -850,21 +857,21 @@ public class Int2CharLinkedOpenCustomHashMap extends Int2CharOpenCustomHashMap i state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Int2CharMap.Entry 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 @@ -943,10 +950,13 @@ public class Int2CharLinkedOpenCustomHashMap extends Int2CharOpenCustomHashMap i public boolean add(int o) { throw new UnsupportedOperationException(); } - + + @Override + public void addFirst(int o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(int o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(int o) { throw new UnsupportedOperationException(); } - @Override public boolean addAndMoveToLast(int o) { throw new UnsupportedOperationException(); } @@ -1089,7 +1099,7 @@ public class Int2CharLinkedOpenCustomHashMap extends Int2CharOpenCustomHashMap i } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -1104,19 +1114,19 @@ public class Int2CharLinkedOpenCustomHashMap extends Int2CharOpenCustomHashMap i state = operator.applyAsInt(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0; + if(size() <= 0) return OptionalInt.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalInt.of(keys[index]); index = (int)links[index]; } - return 0; + return OptionalInt.empty(); } @Override @@ -1251,7 +1261,7 @@ public class Int2CharLinkedOpenCustomHashMap extends Int2CharOpenCustomHashMap i } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -1266,19 +1276,19 @@ public class Int2CharLinkedOpenCustomHashMap extends Int2CharOpenCustomHashMap i state = operator.applyAsChar(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (char)0; + if(size() <= 0) return OptionalChar.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalChar.of(values[index]); index = (int)links[index]; } - return (char)0; + return OptionalChar.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/ints/maps/impl/customHash/Int2CharOpenCustomHashMap.java b/src/main/java/speiger/src/collections/ints/maps/impl/customHash/Int2CharOpenCustomHashMap.java index c41498e..746c949 100644 --- a/src/main/java/speiger/src/collections/ints/maps/impl/customHash/Int2CharOpenCustomHashMap.java +++ b/src/main/java/speiger/src/collections/ints/maps/impl/customHash/Int2CharOpenCustomHashMap.java @@ -5,10 +5,12 @@ 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; import java.util.function.IntPredicate; +import java.util.OptionalInt; import speiger.src.collections.ints.collections.IntIterator; import speiger.src.collections.ints.functions.IntConsumer; @@ -36,6 +38,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectCharConsumer; import speiger.src.collections.chars.functions.function.CharPredicate; +import speiger.src.collections.chars.functions.OptionalChar; import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.sets.AbstractObjectSet; import speiger.src.collections.objects.sets.ObjectSet; @@ -951,7 +954,7 @@ public class Int2CharOpenCustomHashMap extends AbstractInt2CharMap implements IT } @Override - public Int2CharMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Int2CharMap.Entry state = null; boolean empty = true; @@ -968,25 +971,25 @@ public class Int2CharOpenCustomHashMap extends AbstractInt2CharMap implements IT } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Int2CharMap.Entry 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], 0)) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1160,7 +1163,7 @@ public class Int2CharOpenCustomHashMap extends AbstractInt2CharMap implements IT } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -1177,18 +1180,18 @@ public class Int2CharOpenCustomHashMap extends AbstractInt2CharMap implements IT } state = operator.applyAsInt(state, keys[i]); } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OptionalInt.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OptionalInt.of(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], 0) && filter.test(keys[i])) return keys[i]; + if(!strategy.equals(keys[i], 0) && filter.test(keys[i])) return OptionalInt.of(keys[i]); } - return 0; + return OptionalInt.empty(); } @Override @@ -1303,7 +1306,7 @@ public class Int2CharOpenCustomHashMap extends AbstractInt2CharMap implements IT } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -1320,18 +1323,18 @@ public class Int2CharOpenCustomHashMap extends AbstractInt2CharMap implements IT } state = operator.applyAsChar(state, values[i]); } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (char)0; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return OptionalChar.empty(); + if(containsNull && filter.test(values[nullIndex])) return OptionalChar.of(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], 0) && filter.test(values[i])) return values[i]; + if(!strategy.equals(keys[i], 0) && filter.test(values[i])) return OptionalChar.of(values[i]); } - return (char)0; + return OptionalChar.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/ints/maps/impl/customHash/Int2DoubleLinkedOpenCustomHashMap.java b/src/main/java/speiger/src/collections/ints/maps/impl/customHash/Int2DoubleLinkedOpenCustomHashMap.java index 4252aca..fb0e16d 100644 --- a/src/main/java/speiger/src/collections/ints/maps/impl/customHash/Int2DoubleLinkedOpenCustomHashMap.java +++ b/src/main/java/speiger/src/collections/ints/maps/impl/customHash/Int2DoubleLinkedOpenCustomHashMap.java @@ -4,11 +4,14 @@ 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; import java.util.function.IntPredicate; +import java.util.OptionalInt; import java.util.function.DoublePredicate; +import java.util.OptionalDouble; import speiger.src.collections.ints.collections.IntBidirectionalIterator; import speiger.src.collections.ints.functions.IntConsumer; @@ -670,6 +673,10 @@ public class Int2DoubleLinkedOpenCustomHashMap extends Int2DoubleOpenCustomHashM } private class MapEntrySet extends AbstractObjectSet implements Int2DoubleOrderedMap.FastOrderedSet { + @Override + public void addFirst(Int2DoubleMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Int2DoubleMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Int2DoubleMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -835,7 +842,7 @@ public class Int2DoubleLinkedOpenCustomHashMap extends Int2DoubleOpenCustomHashM } @Override - public Int2DoubleMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Int2DoubleMap.Entry state = null; boolean empty = true; @@ -850,21 +857,21 @@ public class Int2DoubleLinkedOpenCustomHashMap extends Int2DoubleOpenCustomHashM state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Int2DoubleMap.Entry 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 @@ -943,10 +950,13 @@ public class Int2DoubleLinkedOpenCustomHashMap extends Int2DoubleOpenCustomHashM public boolean add(int o) { throw new UnsupportedOperationException(); } - + + @Override + public void addFirst(int o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(int o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(int o) { throw new UnsupportedOperationException(); } - @Override public boolean addAndMoveToLast(int o) { throw new UnsupportedOperationException(); } @@ -1089,7 +1099,7 @@ public class Int2DoubleLinkedOpenCustomHashMap extends Int2DoubleOpenCustomHashM } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -1104,19 +1114,19 @@ public class Int2DoubleLinkedOpenCustomHashMap extends Int2DoubleOpenCustomHashM state = operator.applyAsInt(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0; + if(size() <= 0) return OptionalInt.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalInt.of(keys[index]); index = (int)links[index]; } - return 0; + return OptionalInt.empty(); } @Override @@ -1251,7 +1261,7 @@ public class Int2DoubleLinkedOpenCustomHashMap extends Int2DoubleOpenCustomHashM } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -1266,19 +1276,19 @@ public class Int2DoubleLinkedOpenCustomHashMap extends Int2DoubleOpenCustomHashM state = operator.applyAsDouble(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0D; + if(size() <= 0) return OptionalDouble.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalDouble.of(values[index]); index = (int)links[index]; } - return 0D; + return OptionalDouble.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/ints/maps/impl/customHash/Int2DoubleOpenCustomHashMap.java b/src/main/java/speiger/src/collections/ints/maps/impl/customHash/Int2DoubleOpenCustomHashMap.java index b5e8e24..f09dc27 100644 --- a/src/main/java/speiger/src/collections/ints/maps/impl/customHash/Int2DoubleOpenCustomHashMap.java +++ b/src/main/java/speiger/src/collections/ints/maps/impl/customHash/Int2DoubleOpenCustomHashMap.java @@ -5,11 +5,14 @@ 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; import java.util.function.IntPredicate; +import java.util.OptionalInt; import java.util.function.DoublePredicate; +import java.util.OptionalDouble; import speiger.src.collections.ints.collections.IntIterator; import speiger.src.collections.ints.functions.IntConsumer; @@ -951,7 +954,7 @@ public class Int2DoubleOpenCustomHashMap extends AbstractInt2DoubleMap implement } @Override - public Int2DoubleMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Int2DoubleMap.Entry state = null; boolean empty = true; @@ -968,25 +971,25 @@ public class Int2DoubleOpenCustomHashMap extends AbstractInt2DoubleMap implement } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Int2DoubleMap.Entry 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], 0)) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1160,7 +1163,7 @@ public class Int2DoubleOpenCustomHashMap extends AbstractInt2DoubleMap implement } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -1177,18 +1180,18 @@ public class Int2DoubleOpenCustomHashMap extends AbstractInt2DoubleMap implement } state = operator.applyAsInt(state, keys[i]); } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OptionalInt.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OptionalInt.of(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], 0) && filter.test(keys[i])) return keys[i]; + if(!strategy.equals(keys[i], 0) && filter.test(keys[i])) return OptionalInt.of(keys[i]); } - return 0; + return OptionalInt.empty(); } @Override @@ -1303,7 +1306,7 @@ public class Int2DoubleOpenCustomHashMap extends AbstractInt2DoubleMap implement } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -1320,18 +1323,18 @@ public class Int2DoubleOpenCustomHashMap extends AbstractInt2DoubleMap implement } state = operator.applyAsDouble(state, values[i]); } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0D; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return OptionalDouble.empty(); + if(containsNull && filter.test(values[nullIndex])) return OptionalDouble.of(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], 0) && filter.test(values[i])) return values[i]; + if(!strategy.equals(keys[i], 0) && filter.test(values[i])) return OptionalDouble.of(values[i]); } - return 0D; + return OptionalDouble.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/ints/maps/impl/customHash/Int2FloatLinkedOpenCustomHashMap.java b/src/main/java/speiger/src/collections/ints/maps/impl/customHash/Int2FloatLinkedOpenCustomHashMap.java index f6add50..105654f 100644 --- a/src/main/java/speiger/src/collections/ints/maps/impl/customHash/Int2FloatLinkedOpenCustomHashMap.java +++ b/src/main/java/speiger/src/collections/ints/maps/impl/customHash/Int2FloatLinkedOpenCustomHashMap.java @@ -4,10 +4,12 @@ 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; import java.util.function.IntPredicate; +import java.util.OptionalInt; import speiger.src.collections.ints.collections.IntBidirectionalIterator; import speiger.src.collections.ints.functions.IntConsumer; @@ -32,6 +34,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectFloatConsumer; import speiger.src.collections.floats.functions.function.FloatPredicate; +import speiger.src.collections.floats.functions.OptionalFloat; import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOperator; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.objects.lists.ObjectListIterator; @@ -670,6 +673,10 @@ public class Int2FloatLinkedOpenCustomHashMap extends Int2FloatOpenCustomHashMap } private class MapEntrySet extends AbstractObjectSet implements Int2FloatOrderedMap.FastOrderedSet { + @Override + public void addFirst(Int2FloatMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Int2FloatMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Int2FloatMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -835,7 +842,7 @@ public class Int2FloatLinkedOpenCustomHashMap extends Int2FloatOpenCustomHashMap } @Override - public Int2FloatMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Int2FloatMap.Entry state = null; boolean empty = true; @@ -850,21 +857,21 @@ public class Int2FloatLinkedOpenCustomHashMap extends Int2FloatOpenCustomHashMap state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Int2FloatMap.Entry 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 @@ -943,10 +950,13 @@ public class Int2FloatLinkedOpenCustomHashMap extends Int2FloatOpenCustomHashMap public boolean add(int o) { throw new UnsupportedOperationException(); } - + + @Override + public void addFirst(int o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(int o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(int o) { throw new UnsupportedOperationException(); } - @Override public boolean addAndMoveToLast(int o) { throw new UnsupportedOperationException(); } @@ -1089,7 +1099,7 @@ public class Int2FloatLinkedOpenCustomHashMap extends Int2FloatOpenCustomHashMap } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -1104,19 +1114,19 @@ public class Int2FloatLinkedOpenCustomHashMap extends Int2FloatOpenCustomHashMap state = operator.applyAsInt(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0; + if(size() <= 0) return OptionalInt.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalInt.of(keys[index]); index = (int)links[index]; } - return 0; + return OptionalInt.empty(); } @Override @@ -1251,7 +1261,7 @@ public class Int2FloatLinkedOpenCustomHashMap extends Int2FloatOpenCustomHashMap } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -1266,19 +1276,19 @@ public class Int2FloatLinkedOpenCustomHashMap extends Int2FloatOpenCustomHashMap state = operator.applyAsFloat(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0F; + if(size() <= 0) return OptionalFloat.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalFloat.of(values[index]); index = (int)links[index]; } - return 0F; + return OptionalFloat.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/ints/maps/impl/customHash/Int2FloatOpenCustomHashMap.java b/src/main/java/speiger/src/collections/ints/maps/impl/customHash/Int2FloatOpenCustomHashMap.java index d4542c2..f75491f 100644 --- a/src/main/java/speiger/src/collections/ints/maps/impl/customHash/Int2FloatOpenCustomHashMap.java +++ b/src/main/java/speiger/src/collections/ints/maps/impl/customHash/Int2FloatOpenCustomHashMap.java @@ -5,10 +5,12 @@ 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; import java.util.function.IntPredicate; +import java.util.OptionalInt; import speiger.src.collections.ints.collections.IntIterator; import speiger.src.collections.ints.functions.IntConsumer; @@ -36,6 +38,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectFloatConsumer; import speiger.src.collections.floats.functions.function.FloatPredicate; +import speiger.src.collections.floats.functions.OptionalFloat; import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.sets.AbstractObjectSet; import speiger.src.collections.objects.sets.ObjectSet; @@ -951,7 +954,7 @@ public class Int2FloatOpenCustomHashMap extends AbstractInt2FloatMap implements } @Override - public Int2FloatMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Int2FloatMap.Entry state = null; boolean empty = true; @@ -968,25 +971,25 @@ public class Int2FloatOpenCustomHashMap extends AbstractInt2FloatMap implements } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Int2FloatMap.Entry 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], 0)) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1160,7 +1163,7 @@ public class Int2FloatOpenCustomHashMap extends AbstractInt2FloatMap implements } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -1177,18 +1180,18 @@ public class Int2FloatOpenCustomHashMap extends AbstractInt2FloatMap implements } state = operator.applyAsInt(state, keys[i]); } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OptionalInt.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OptionalInt.of(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], 0) && filter.test(keys[i])) return keys[i]; + if(!strategy.equals(keys[i], 0) && filter.test(keys[i])) return OptionalInt.of(keys[i]); } - return 0; + return OptionalInt.empty(); } @Override @@ -1303,7 +1306,7 @@ public class Int2FloatOpenCustomHashMap extends AbstractInt2FloatMap implements } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -1320,18 +1323,18 @@ public class Int2FloatOpenCustomHashMap extends AbstractInt2FloatMap implements } state = operator.applyAsFloat(state, values[i]); } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0F; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return OptionalFloat.empty(); + if(containsNull && filter.test(values[nullIndex])) return OptionalFloat.of(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], 0) && filter.test(values[i])) return values[i]; + if(!strategy.equals(keys[i], 0) && filter.test(values[i])) return OptionalFloat.of(values[i]); } - return 0F; + return OptionalFloat.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/ints/maps/impl/customHash/Int2IntLinkedOpenCustomHashMap.java b/src/main/java/speiger/src/collections/ints/maps/impl/customHash/Int2IntLinkedOpenCustomHashMap.java index 78d45a7..0573228 100644 --- a/src/main/java/speiger/src/collections/ints/maps/impl/customHash/Int2IntLinkedOpenCustomHashMap.java +++ b/src/main/java/speiger/src/collections/ints/maps/impl/customHash/Int2IntLinkedOpenCustomHashMap.java @@ -4,10 +4,12 @@ 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; import java.util.function.IntPredicate; +import java.util.OptionalInt; import speiger.src.collections.ints.collections.IntBidirectionalIterator; import speiger.src.collections.ints.functions.IntConsumer; @@ -663,6 +665,10 @@ public class Int2IntLinkedOpenCustomHashMap extends Int2IntOpenCustomHashMap imp } private class MapEntrySet extends AbstractObjectSet implements Int2IntOrderedMap.FastOrderedSet { + @Override + public void addFirst(Int2IntMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Int2IntMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Int2IntMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -828,7 +834,7 @@ public class Int2IntLinkedOpenCustomHashMap extends Int2IntOpenCustomHashMap imp } @Override - public Int2IntMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Int2IntMap.Entry state = null; boolean empty = true; @@ -843,21 +849,21 @@ public class Int2IntLinkedOpenCustomHashMap extends Int2IntOpenCustomHashMap imp state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Int2IntMap.Entry 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 @@ -936,10 +942,13 @@ public class Int2IntLinkedOpenCustomHashMap extends Int2IntOpenCustomHashMap imp public boolean add(int o) { throw new UnsupportedOperationException(); } - + + @Override + public void addFirst(int o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(int o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(int o) { throw new UnsupportedOperationException(); } - @Override public boolean addAndMoveToLast(int o) { throw new UnsupportedOperationException(); } @@ -1082,7 +1091,7 @@ public class Int2IntLinkedOpenCustomHashMap extends Int2IntOpenCustomHashMap imp } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -1097,19 +1106,19 @@ public class Int2IntLinkedOpenCustomHashMap extends Int2IntOpenCustomHashMap imp state = operator.applyAsInt(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0; + if(size() <= 0) return OptionalInt.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalInt.of(keys[index]); index = (int)links[index]; } - return 0; + return OptionalInt.empty(); } @Override @@ -1244,7 +1253,7 @@ public class Int2IntLinkedOpenCustomHashMap extends Int2IntOpenCustomHashMap imp } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -1259,19 +1268,19 @@ public class Int2IntLinkedOpenCustomHashMap extends Int2IntOpenCustomHashMap imp state = operator.applyAsInt(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0; + if(size() <= 0) return OptionalInt.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalInt.of(values[index]); index = (int)links[index]; } - return 0; + return OptionalInt.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/ints/maps/impl/customHash/Int2IntOpenCustomHashMap.java b/src/main/java/speiger/src/collections/ints/maps/impl/customHash/Int2IntOpenCustomHashMap.java index 3e11e17..e8a4afe 100644 --- a/src/main/java/speiger/src/collections/ints/maps/impl/customHash/Int2IntOpenCustomHashMap.java +++ b/src/main/java/speiger/src/collections/ints/maps/impl/customHash/Int2IntOpenCustomHashMap.java @@ -5,10 +5,12 @@ 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; import java.util.function.IntPredicate; +import java.util.OptionalInt; import speiger.src.collections.ints.collections.IntIterator; import speiger.src.collections.ints.functions.IntConsumer; @@ -943,7 +945,7 @@ public class Int2IntOpenCustomHashMap extends AbstractInt2IntMap implements ITri } @Override - public Int2IntMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Int2IntMap.Entry state = null; boolean empty = true; @@ -960,25 +962,25 @@ public class Int2IntOpenCustomHashMap extends AbstractInt2IntMap implements ITri } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Int2IntMap.Entry 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], 0)) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1152,7 +1154,7 @@ public class Int2IntOpenCustomHashMap extends AbstractInt2IntMap implements ITri } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -1169,18 +1171,18 @@ public class Int2IntOpenCustomHashMap extends AbstractInt2IntMap implements ITri } state = operator.applyAsInt(state, keys[i]); } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OptionalInt.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OptionalInt.of(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], 0) && filter.test(keys[i])) return keys[i]; + if(!strategy.equals(keys[i], 0) && filter.test(keys[i])) return OptionalInt.of(keys[i]); } - return 0; + return OptionalInt.empty(); } @Override @@ -1295,7 +1297,7 @@ public class Int2IntOpenCustomHashMap extends AbstractInt2IntMap implements ITri } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -1312,18 +1314,18 @@ public class Int2IntOpenCustomHashMap extends AbstractInt2IntMap implements ITri } state = operator.applyAsInt(state, values[i]); } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return OptionalInt.empty(); + if(containsNull && filter.test(values[nullIndex])) return OptionalInt.of(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], 0) && filter.test(values[i])) return values[i]; + if(!strategy.equals(keys[i], 0) && filter.test(values[i])) return OptionalInt.of(values[i]); } - return 0; + return OptionalInt.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/ints/maps/impl/customHash/Int2LongLinkedOpenCustomHashMap.java b/src/main/java/speiger/src/collections/ints/maps/impl/customHash/Int2LongLinkedOpenCustomHashMap.java index a794c69..71cfaa0 100644 --- a/src/main/java/speiger/src/collections/ints/maps/impl/customHash/Int2LongLinkedOpenCustomHashMap.java +++ b/src/main/java/speiger/src/collections/ints/maps/impl/customHash/Int2LongLinkedOpenCustomHashMap.java @@ -4,11 +4,14 @@ 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; import java.util.function.IntPredicate; +import java.util.OptionalInt; import java.util.function.LongPredicate; +import java.util.OptionalLong; import speiger.src.collections.ints.collections.IntBidirectionalIterator; import speiger.src.collections.ints.functions.IntConsumer; @@ -670,6 +673,10 @@ public class Int2LongLinkedOpenCustomHashMap extends Int2LongOpenCustomHashMap i } private class MapEntrySet extends AbstractObjectSet implements Int2LongOrderedMap.FastOrderedSet { + @Override + public void addFirst(Int2LongMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Int2LongMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Int2LongMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -835,7 +842,7 @@ public class Int2LongLinkedOpenCustomHashMap extends Int2LongOpenCustomHashMap i } @Override - public Int2LongMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Int2LongMap.Entry state = null; boolean empty = true; @@ -850,21 +857,21 @@ public class Int2LongLinkedOpenCustomHashMap extends Int2LongOpenCustomHashMap i state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Int2LongMap.Entry 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 @@ -943,10 +950,13 @@ public class Int2LongLinkedOpenCustomHashMap extends Int2LongOpenCustomHashMap i public boolean add(int o) { throw new UnsupportedOperationException(); } - + + @Override + public void addFirst(int o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(int o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(int o) { throw new UnsupportedOperationException(); } - @Override public boolean addAndMoveToLast(int o) { throw new UnsupportedOperationException(); } @@ -1089,7 +1099,7 @@ public class Int2LongLinkedOpenCustomHashMap extends Int2LongOpenCustomHashMap i } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -1104,19 +1114,19 @@ public class Int2LongLinkedOpenCustomHashMap extends Int2LongOpenCustomHashMap i state = operator.applyAsInt(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0; + if(size() <= 0) return OptionalInt.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalInt.of(keys[index]); index = (int)links[index]; } - return 0; + return OptionalInt.empty(); } @Override @@ -1251,7 +1261,7 @@ public class Int2LongLinkedOpenCustomHashMap extends Int2LongOpenCustomHashMap i } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -1266,19 +1276,19 @@ public class Int2LongLinkedOpenCustomHashMap extends Int2LongOpenCustomHashMap i state = operator.applyAsLong(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0L; + if(size() <= 0) return OptionalLong.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalLong.of(values[index]); index = (int)links[index]; } - return 0L; + return OptionalLong.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/ints/maps/impl/customHash/Int2LongOpenCustomHashMap.java b/src/main/java/speiger/src/collections/ints/maps/impl/customHash/Int2LongOpenCustomHashMap.java index f813d92..fb3a2e2 100644 --- a/src/main/java/speiger/src/collections/ints/maps/impl/customHash/Int2LongOpenCustomHashMap.java +++ b/src/main/java/speiger/src/collections/ints/maps/impl/customHash/Int2LongOpenCustomHashMap.java @@ -5,11 +5,14 @@ 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; import java.util.function.IntPredicate; +import java.util.OptionalInt; import java.util.function.LongPredicate; +import java.util.OptionalLong; import speiger.src.collections.ints.collections.IntIterator; import speiger.src.collections.ints.functions.IntConsumer; @@ -951,7 +954,7 @@ public class Int2LongOpenCustomHashMap extends AbstractInt2LongMap implements IT } @Override - public Int2LongMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Int2LongMap.Entry state = null; boolean empty = true; @@ -968,25 +971,25 @@ public class Int2LongOpenCustomHashMap extends AbstractInt2LongMap implements IT } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Int2LongMap.Entry 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], 0)) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1160,7 +1163,7 @@ public class Int2LongOpenCustomHashMap extends AbstractInt2LongMap implements IT } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -1177,18 +1180,18 @@ public class Int2LongOpenCustomHashMap extends AbstractInt2LongMap implements IT } state = operator.applyAsInt(state, keys[i]); } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OptionalInt.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OptionalInt.of(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], 0) && filter.test(keys[i])) return keys[i]; + if(!strategy.equals(keys[i], 0) && filter.test(keys[i])) return OptionalInt.of(keys[i]); } - return 0; + return OptionalInt.empty(); } @Override @@ -1303,7 +1306,7 @@ public class Int2LongOpenCustomHashMap extends AbstractInt2LongMap implements IT } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -1320,18 +1323,18 @@ public class Int2LongOpenCustomHashMap extends AbstractInt2LongMap implements IT } state = operator.applyAsLong(state, values[i]); } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0L; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return OptionalLong.empty(); + if(containsNull && filter.test(values[nullIndex])) return OptionalLong.of(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], 0) && filter.test(values[i])) return values[i]; + if(!strategy.equals(keys[i], 0) && filter.test(values[i])) return OptionalLong.of(values[i]); } - return 0L; + return OptionalLong.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/ints/maps/impl/customHash/Int2ObjectLinkedOpenCustomHashMap.java b/src/main/java/speiger/src/collections/ints/maps/impl/customHash/Int2ObjectLinkedOpenCustomHashMap.java index 4ef18df..f3c34bd 100644 --- a/src/main/java/speiger/src/collections/ints/maps/impl/customHash/Int2ObjectLinkedOpenCustomHashMap.java +++ b/src/main/java/speiger/src/collections/ints/maps/impl/customHash/Int2ObjectLinkedOpenCustomHashMap.java @@ -4,10 +4,12 @@ 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; import java.util.function.IntPredicate; +import java.util.OptionalInt; import speiger.src.collections.ints.collections.IntBidirectionalIterator; import speiger.src.collections.ints.functions.IntConsumer; @@ -665,6 +667,10 @@ public class Int2ObjectLinkedOpenCustomHashMap extends Int2ObjectOpenCustomHa } private class MapEntrySet extends AbstractObjectSet> implements Int2ObjectOrderedMap.FastOrderedSet { + @Override + public void addFirst(Int2ObjectMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Int2ObjectMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Int2ObjectMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -830,7 +836,7 @@ public class Int2ObjectLinkedOpenCustomHashMap extends Int2ObjectOpenCustomHa } @Override - public Int2ObjectMap.Entry reduce(ObjectObjectUnaryOperator, Int2ObjectMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Int2ObjectMap.Entry> operator) { Objects.requireNonNull(operator); Int2ObjectMap.Entry state = null; boolean empty = true; @@ -845,21 +851,21 @@ public class Int2ObjectLinkedOpenCustomHashMap extends Int2ObjectOpenCustomHa state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Int2ObjectMap.Entry 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 @@ -938,10 +944,13 @@ public class Int2ObjectLinkedOpenCustomHashMap extends Int2ObjectOpenCustomHa public boolean add(int o) { throw new UnsupportedOperationException(); } - + + @Override + public void addFirst(int o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(int o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(int o) { throw new UnsupportedOperationException(); } - @Override public boolean addAndMoveToLast(int o) { throw new UnsupportedOperationException(); } @@ -1084,7 +1093,7 @@ public class Int2ObjectLinkedOpenCustomHashMap extends Int2ObjectOpenCustomHa } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -1099,19 +1108,19 @@ public class Int2ObjectLinkedOpenCustomHashMap extends Int2ObjectOpenCustomHa state = operator.applyAsInt(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0; + if(size() <= 0) return OptionalInt.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalInt.of(keys[index]); index = (int)links[index]; } - return 0; + return OptionalInt.empty(); } @Override @@ -1247,7 +1256,7 @@ public class Int2ObjectLinkedOpenCustomHashMap extends Int2ObjectOpenCustomHa } @Override - public V reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); V state = null; boolean empty = true; @@ -1262,19 +1271,19 @@ public class Int2ObjectLinkedOpenCustomHashMap extends Int2ObjectOpenCustomHa state = operator.apply(state, values[index]); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public V findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return Optional.ofNullable(values[index]); index = (int)links[index]; } - return null; + return Optional.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/ints/maps/impl/customHash/Int2ObjectOpenCustomHashMap.java b/src/main/java/speiger/src/collections/ints/maps/impl/customHash/Int2ObjectOpenCustomHashMap.java index c09cb15..28d0582 100644 --- a/src/main/java/speiger/src/collections/ints/maps/impl/customHash/Int2ObjectOpenCustomHashMap.java +++ b/src/main/java/speiger/src/collections/ints/maps/impl/customHash/Int2ObjectOpenCustomHashMap.java @@ -5,10 +5,12 @@ 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; import java.util.function.IntPredicate; +import java.util.OptionalInt; import speiger.src.collections.ints.collections.IntIterator; import speiger.src.collections.ints.functions.IntConsumer; @@ -867,7 +869,7 @@ public class Int2ObjectOpenCustomHashMap extends AbstractInt2ObjectMap imp } @Override - public Int2ObjectMap.Entry reduce(ObjectObjectUnaryOperator, Int2ObjectMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Int2ObjectMap.Entry> operator) { Objects.requireNonNull(operator); Int2ObjectMap.Entry state = null; boolean empty = true; @@ -884,25 +886,25 @@ public class Int2ObjectOpenCustomHashMap extends AbstractInt2ObjectMap imp } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Int2ObjectMap.Entry 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], 0)) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1076,7 +1078,7 @@ public class Int2ObjectOpenCustomHashMap extends AbstractInt2ObjectMap imp } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -1093,18 +1095,18 @@ public class Int2ObjectOpenCustomHashMap extends AbstractInt2ObjectMap imp } state = operator.applyAsInt(state, keys[i]); } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OptionalInt.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OptionalInt.of(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], 0) && filter.test(keys[i])) return keys[i]; + if(!strategy.equals(keys[i], 0) && filter.test(keys[i])) return OptionalInt.of(keys[i]); } - return 0; + return OptionalInt.empty(); } @Override @@ -1219,7 +1221,7 @@ public class Int2ObjectOpenCustomHashMap extends AbstractInt2ObjectMap imp } @Override - public V reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); V state = null; boolean empty = true; @@ -1236,18 +1238,18 @@ public class Int2ObjectOpenCustomHashMap extends AbstractInt2ObjectMap imp } state = operator.apply(state, values[i]); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public V findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return Optional.empty(); + if(containsNull && filter.test(values[nullIndex])) return Optional.ofNullable(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], 0) && filter.test(values[i])) return values[i]; + if(!strategy.equals(keys[i], 0) && filter.test(values[i])) return Optional.ofNullable(values[i]); } - return null; + return Optional.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/ints/maps/impl/customHash/Int2ShortLinkedOpenCustomHashMap.java b/src/main/java/speiger/src/collections/ints/maps/impl/customHash/Int2ShortLinkedOpenCustomHashMap.java index e29aa1c..035c8b8 100644 --- a/src/main/java/speiger/src/collections/ints/maps/impl/customHash/Int2ShortLinkedOpenCustomHashMap.java +++ b/src/main/java/speiger/src/collections/ints/maps/impl/customHash/Int2ShortLinkedOpenCustomHashMap.java @@ -4,10 +4,12 @@ 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; import java.util.function.IntPredicate; +import java.util.OptionalInt; import speiger.src.collections.ints.collections.IntBidirectionalIterator; import speiger.src.collections.ints.functions.IntConsumer; @@ -32,6 +34,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectShortConsumer; import speiger.src.collections.shorts.functions.function.ShortPredicate; +import speiger.src.collections.shorts.functions.OptionalShort; import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOperator; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.objects.lists.ObjectListIterator; @@ -670,6 +673,10 @@ public class Int2ShortLinkedOpenCustomHashMap extends Int2ShortOpenCustomHashMap } private class MapEntrySet extends AbstractObjectSet implements Int2ShortOrderedMap.FastOrderedSet { + @Override + public void addFirst(Int2ShortMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Int2ShortMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Int2ShortMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -835,7 +842,7 @@ public class Int2ShortLinkedOpenCustomHashMap extends Int2ShortOpenCustomHashMap } @Override - public Int2ShortMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Int2ShortMap.Entry state = null; boolean empty = true; @@ -850,21 +857,21 @@ public class Int2ShortLinkedOpenCustomHashMap extends Int2ShortOpenCustomHashMap state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Int2ShortMap.Entry 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 @@ -943,10 +950,13 @@ public class Int2ShortLinkedOpenCustomHashMap extends Int2ShortOpenCustomHashMap public boolean add(int o) { throw new UnsupportedOperationException(); } - + + @Override + public void addFirst(int o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(int o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(int o) { throw new UnsupportedOperationException(); } - @Override public boolean addAndMoveToLast(int o) { throw new UnsupportedOperationException(); } @@ -1089,7 +1099,7 @@ public class Int2ShortLinkedOpenCustomHashMap extends Int2ShortOpenCustomHashMap } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -1104,19 +1114,19 @@ public class Int2ShortLinkedOpenCustomHashMap extends Int2ShortOpenCustomHashMap state = operator.applyAsInt(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0; + if(size() <= 0) return OptionalInt.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalInt.of(keys[index]); index = (int)links[index]; } - return 0; + return OptionalInt.empty(); } @Override @@ -1251,7 +1261,7 @@ public class Int2ShortLinkedOpenCustomHashMap extends Int2ShortOpenCustomHashMap } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -1266,19 +1276,19 @@ public class Int2ShortLinkedOpenCustomHashMap extends Int2ShortOpenCustomHashMap state = operator.applyAsShort(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (short)0; + if(size() <= 0) return OptionalShort.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalShort.of(values[index]); index = (int)links[index]; } - return (short)0; + return OptionalShort.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/ints/maps/impl/customHash/Int2ShortOpenCustomHashMap.java b/src/main/java/speiger/src/collections/ints/maps/impl/customHash/Int2ShortOpenCustomHashMap.java index 2e1029d..b40a8c4 100644 --- a/src/main/java/speiger/src/collections/ints/maps/impl/customHash/Int2ShortOpenCustomHashMap.java +++ b/src/main/java/speiger/src/collections/ints/maps/impl/customHash/Int2ShortOpenCustomHashMap.java @@ -5,10 +5,12 @@ 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; import java.util.function.IntPredicate; +import java.util.OptionalInt; import speiger.src.collections.ints.collections.IntIterator; import speiger.src.collections.ints.functions.IntConsumer; @@ -36,6 +38,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectShortConsumer; import speiger.src.collections.shorts.functions.function.ShortPredicate; +import speiger.src.collections.shorts.functions.OptionalShort; import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.sets.AbstractObjectSet; import speiger.src.collections.objects.sets.ObjectSet; @@ -951,7 +954,7 @@ public class Int2ShortOpenCustomHashMap extends AbstractInt2ShortMap implements } @Override - public Int2ShortMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Int2ShortMap.Entry state = null; boolean empty = true; @@ -968,25 +971,25 @@ public class Int2ShortOpenCustomHashMap extends AbstractInt2ShortMap implements } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Int2ShortMap.Entry 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], 0)) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1160,7 +1163,7 @@ public class Int2ShortOpenCustomHashMap extends AbstractInt2ShortMap implements } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -1177,18 +1180,18 @@ public class Int2ShortOpenCustomHashMap extends AbstractInt2ShortMap implements } state = operator.applyAsInt(state, keys[i]); } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OptionalInt.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OptionalInt.of(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], 0) && filter.test(keys[i])) return keys[i]; + if(!strategy.equals(keys[i], 0) && filter.test(keys[i])) return OptionalInt.of(keys[i]); } - return 0; + return OptionalInt.empty(); } @Override @@ -1303,7 +1306,7 @@ public class Int2ShortOpenCustomHashMap extends AbstractInt2ShortMap implements } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -1320,18 +1323,18 @@ public class Int2ShortOpenCustomHashMap extends AbstractInt2ShortMap implements } state = operator.applyAsShort(state, values[i]); } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (short)0; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return OptionalShort.empty(); + if(containsNull && filter.test(values[nullIndex])) return OptionalShort.of(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], 0) && filter.test(values[i])) return values[i]; + if(!strategy.equals(keys[i], 0) && filter.test(values[i])) return OptionalShort.of(values[i]); } - return (short)0; + return OptionalShort.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/ints/maps/impl/hash/Int2BooleanLinkedOpenHashMap.java b/src/main/java/speiger/src/collections/ints/maps/impl/hash/Int2BooleanLinkedOpenHashMap.java index 9826fa3..aca93b9 100644 --- a/src/main/java/speiger/src/collections/ints/maps/impl/hash/Int2BooleanLinkedOpenHashMap.java +++ b/src/main/java/speiger/src/collections/ints/maps/impl/hash/Int2BooleanLinkedOpenHashMap.java @@ -4,10 +4,12 @@ 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; import java.util.function.IntPredicate; +import java.util.OptionalInt; import speiger.src.collections.ints.collections.IntBidirectionalIterator; import speiger.src.collections.ints.functions.IntConsumer; @@ -31,6 +33,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectBooleanConsumer; import speiger.src.collections.booleans.functions.function.BooleanPredicate; +import speiger.src.collections.booleans.functions.OptionalBoolean; import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOperator; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.objects.lists.ObjectListIterator; @@ -668,6 +671,10 @@ public class Int2BooleanLinkedOpenHashMap extends Int2BooleanOpenHashMap impleme } private class MapEntrySet extends AbstractObjectSet implements Int2BooleanOrderedMap.FastOrderedSet { + @Override + public void addFirst(Int2BooleanMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Int2BooleanMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Int2BooleanMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -833,7 +840,7 @@ public class Int2BooleanLinkedOpenHashMap extends Int2BooleanOpenHashMap impleme } @Override - public Int2BooleanMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Int2BooleanMap.Entry state = null; boolean empty = true; @@ -848,21 +855,21 @@ public class Int2BooleanLinkedOpenHashMap extends Int2BooleanOpenHashMap impleme state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Int2BooleanMap.Entry 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 @@ -938,6 +945,12 @@ public class Int2BooleanLinkedOpenHashMap extends Int2BooleanOpenHashMap impleme @Override public boolean add(int o) { throw new UnsupportedOperationException(); } + + @Override + public void addFirst(int o) { throw new UnsupportedOperationException(); } + + @Override + public void addLast(int o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(int o) { throw new UnsupportedOperationException(); } @@ -1084,7 +1097,7 @@ public class Int2BooleanLinkedOpenHashMap extends Int2BooleanOpenHashMap impleme } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -1099,19 +1112,19 @@ public class Int2BooleanLinkedOpenHashMap extends Int2BooleanOpenHashMap impleme state = operator.applyAsInt(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0; + if(size() <= 0) return OptionalInt.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalInt.of(keys[index]); index = (int)links[index]; } - return 0; + return OptionalInt.empty(); } @Override @@ -1247,7 +1260,7 @@ public class Int2BooleanLinkedOpenHashMap extends Int2BooleanOpenHashMap impleme } @Override - public boolean reduce(BooleanBooleanUnaryOperator operator) { + public OptionalBoolean reduce(BooleanBooleanUnaryOperator operator) { Objects.requireNonNull(operator); boolean state = false; boolean empty = true; @@ -1262,19 +1275,19 @@ public class Int2BooleanLinkedOpenHashMap extends Int2BooleanOpenHashMap impleme state = operator.applyAsBoolean(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalBoolean.empty() : OptionalBoolean.of(state); } @Override - public boolean findFirst(BooleanPredicate filter) { + public OptionalBoolean findFirst(BooleanPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return false; + if(size() <= 0) return OptionalBoolean.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalBoolean.of(values[index]); index = (int)links[index]; } - return false; + return OptionalBoolean.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/ints/maps/impl/hash/Int2BooleanOpenHashMap.java b/src/main/java/speiger/src/collections/ints/maps/impl/hash/Int2BooleanOpenHashMap.java index fec9234..abe4150 100644 --- a/src/main/java/speiger/src/collections/ints/maps/impl/hash/Int2BooleanOpenHashMap.java +++ b/src/main/java/speiger/src/collections/ints/maps/impl/hash/Int2BooleanOpenHashMap.java @@ -5,10 +5,12 @@ 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; import java.util.function.IntPredicate; +import java.util.OptionalInt; import speiger.src.collections.ints.collections.IntIterator; import speiger.src.collections.ints.functions.IntConsumer; @@ -32,6 +34,7 @@ import speiger.src.collections.booleans.functions.BooleanConsumer; import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectBooleanConsumer; import speiger.src.collections.booleans.functions.function.BooleanPredicate; +import speiger.src.collections.booleans.functions.OptionalBoolean; import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.sets.AbstractObjectSet; import speiger.src.collections.objects.sets.ObjectSet; @@ -897,7 +900,7 @@ public class Int2BooleanOpenHashMap extends AbstractInt2BooleanMap implements IT } @Override - public Int2BooleanMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Int2BooleanMap.Entry state = null; boolean empty = true; @@ -914,25 +917,25 @@ public class Int2BooleanOpenHashMap extends AbstractInt2BooleanMap implements IT } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Int2BooleanMap.Entry 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(keys[i] != 0) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1104,7 +1107,7 @@ public class Int2BooleanOpenHashMap extends AbstractInt2BooleanMap implements IT } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -1121,18 +1124,18 @@ public class Int2BooleanOpenHashMap extends AbstractInt2BooleanMap implements IT } state = operator.applyAsInt(state, keys[i]); } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OptionalInt.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OptionalInt.of(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(keys[i] != 0 && filter.test(keys[i])) return keys[i]; + if(keys[i] != 0 && filter.test(keys[i])) return OptionalInt.of(keys[i]); } - return 0; + return OptionalInt.empty(); } @Override @@ -1247,7 +1250,7 @@ public class Int2BooleanOpenHashMap extends AbstractInt2BooleanMap implements IT } @Override - public boolean reduce(BooleanBooleanUnaryOperator operator) { + public OptionalBoolean reduce(BooleanBooleanUnaryOperator operator) { Objects.requireNonNull(operator); boolean state = false; boolean empty = true; @@ -1264,18 +1267,18 @@ public class Int2BooleanOpenHashMap extends AbstractInt2BooleanMap implements IT } state = operator.applyAsBoolean(state, values[i]); } - return state; + return empty ? OptionalBoolean.empty() : OptionalBoolean.of(state); } @Override - public boolean findFirst(BooleanPredicate filter) { + public OptionalBoolean findFirst(BooleanPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return false; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return OptionalBoolean.empty(); + if(containsNull && filter.test(values[nullIndex])) return OptionalBoolean.of(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(keys[i] != 0 && filter.test(values[i])) return values[i]; + if(keys[i] != 0 && filter.test(values[i])) return OptionalBoolean.of(values[i]); } - return false; + return OptionalBoolean.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/ints/maps/impl/hash/Int2ByteLinkedOpenHashMap.java b/src/main/java/speiger/src/collections/ints/maps/impl/hash/Int2ByteLinkedOpenHashMap.java index 8d88b4f..2fb2a52 100644 --- a/src/main/java/speiger/src/collections/ints/maps/impl/hash/Int2ByteLinkedOpenHashMap.java +++ b/src/main/java/speiger/src/collections/ints/maps/impl/hash/Int2ByteLinkedOpenHashMap.java @@ -4,10 +4,12 @@ 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; import java.util.function.IntPredicate; +import java.util.OptionalInt; import speiger.src.collections.ints.collections.IntBidirectionalIterator; import speiger.src.collections.ints.functions.IntConsumer; @@ -31,6 +33,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectByteConsumer; import speiger.src.collections.bytes.functions.function.BytePredicate; +import speiger.src.collections.bytes.functions.OptionalByte; import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOperator; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.objects.lists.ObjectListIterator; @@ -668,6 +671,10 @@ public class Int2ByteLinkedOpenHashMap extends Int2ByteOpenHashMap implements In } private class MapEntrySet extends AbstractObjectSet implements Int2ByteOrderedMap.FastOrderedSet { + @Override + public void addFirst(Int2ByteMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Int2ByteMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Int2ByteMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -833,7 +840,7 @@ public class Int2ByteLinkedOpenHashMap extends Int2ByteOpenHashMap implements In } @Override - public Int2ByteMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Int2ByteMap.Entry state = null; boolean empty = true; @@ -848,21 +855,21 @@ public class Int2ByteLinkedOpenHashMap extends Int2ByteOpenHashMap implements In state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Int2ByteMap.Entry 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 @@ -938,6 +945,12 @@ public class Int2ByteLinkedOpenHashMap extends Int2ByteOpenHashMap implements In @Override public boolean add(int o) { throw new UnsupportedOperationException(); } + + @Override + public void addFirst(int o) { throw new UnsupportedOperationException(); } + + @Override + public void addLast(int o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(int o) { throw new UnsupportedOperationException(); } @@ -1084,7 +1097,7 @@ public class Int2ByteLinkedOpenHashMap extends Int2ByteOpenHashMap implements In } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -1099,19 +1112,19 @@ public class Int2ByteLinkedOpenHashMap extends Int2ByteOpenHashMap implements In state = operator.applyAsInt(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0; + if(size() <= 0) return OptionalInt.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalInt.of(keys[index]); index = (int)links[index]; } - return 0; + return OptionalInt.empty(); } @Override @@ -1247,7 +1260,7 @@ public class Int2ByteLinkedOpenHashMap extends Int2ByteOpenHashMap implements In } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -1262,19 +1275,19 @@ public class Int2ByteLinkedOpenHashMap extends Int2ByteOpenHashMap implements In state = operator.applyAsByte(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (byte)0; + if(size() <= 0) return OptionalByte.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalByte.of(values[index]); index = (int)links[index]; } - return (byte)0; + return OptionalByte.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/ints/maps/impl/hash/Int2ByteOpenHashMap.java b/src/main/java/speiger/src/collections/ints/maps/impl/hash/Int2ByteOpenHashMap.java index 1b18c50..c731d39 100644 --- a/src/main/java/speiger/src/collections/ints/maps/impl/hash/Int2ByteOpenHashMap.java +++ b/src/main/java/speiger/src/collections/ints/maps/impl/hash/Int2ByteOpenHashMap.java @@ -5,10 +5,12 @@ 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; import java.util.function.IntPredicate; +import java.util.OptionalInt; import speiger.src.collections.ints.collections.IntIterator; import speiger.src.collections.ints.functions.IntConsumer; @@ -33,6 +35,7 @@ import speiger.src.collections.bytes.functions.ByteConsumer; import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectByteConsumer; import speiger.src.collections.bytes.functions.function.BytePredicate; +import speiger.src.collections.bytes.functions.OptionalByte; import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.sets.AbstractObjectSet; import speiger.src.collections.objects.sets.ObjectSet; @@ -919,7 +922,7 @@ public class Int2ByteOpenHashMap extends AbstractInt2ByteMap implements ITrimmab } @Override - public Int2ByteMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Int2ByteMap.Entry state = null; boolean empty = true; @@ -936,25 +939,25 @@ public class Int2ByteOpenHashMap extends AbstractInt2ByteMap implements ITrimmab } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Int2ByteMap.Entry 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(keys[i] != 0) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1126,7 +1129,7 @@ public class Int2ByteOpenHashMap extends AbstractInt2ByteMap implements ITrimmab } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -1143,18 +1146,18 @@ public class Int2ByteOpenHashMap extends AbstractInt2ByteMap implements ITrimmab } state = operator.applyAsInt(state, keys[i]); } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OptionalInt.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OptionalInt.of(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(keys[i] != 0 && filter.test(keys[i])) return keys[i]; + if(keys[i] != 0 && filter.test(keys[i])) return OptionalInt.of(keys[i]); } - return 0; + return OptionalInt.empty(); } @Override @@ -1269,7 +1272,7 @@ public class Int2ByteOpenHashMap extends AbstractInt2ByteMap implements ITrimmab } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -1286,18 +1289,18 @@ public class Int2ByteOpenHashMap extends AbstractInt2ByteMap implements ITrimmab } state = operator.applyAsByte(state, values[i]); } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (byte)0; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return OptionalByte.empty(); + if(containsNull && filter.test(values[nullIndex])) return OptionalByte.of(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(keys[i] != 0 && filter.test(values[i])) return values[i]; + if(keys[i] != 0 && filter.test(values[i])) return OptionalByte.of(values[i]); } - return (byte)0; + return OptionalByte.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/ints/maps/impl/hash/Int2CharLinkedOpenHashMap.java b/src/main/java/speiger/src/collections/ints/maps/impl/hash/Int2CharLinkedOpenHashMap.java index 2ccda04..2c73ca7 100644 --- a/src/main/java/speiger/src/collections/ints/maps/impl/hash/Int2CharLinkedOpenHashMap.java +++ b/src/main/java/speiger/src/collections/ints/maps/impl/hash/Int2CharLinkedOpenHashMap.java @@ -4,10 +4,12 @@ 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; import java.util.function.IntPredicate; +import java.util.OptionalInt; import speiger.src.collections.ints.collections.IntBidirectionalIterator; import speiger.src.collections.ints.functions.IntConsumer; @@ -31,6 +33,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectCharConsumer; import speiger.src.collections.chars.functions.function.CharPredicate; +import speiger.src.collections.chars.functions.OptionalChar; import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOperator; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.objects.lists.ObjectListIterator; @@ -668,6 +671,10 @@ public class Int2CharLinkedOpenHashMap extends Int2CharOpenHashMap implements In } private class MapEntrySet extends AbstractObjectSet implements Int2CharOrderedMap.FastOrderedSet { + @Override + public void addFirst(Int2CharMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Int2CharMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Int2CharMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -833,7 +840,7 @@ public class Int2CharLinkedOpenHashMap extends Int2CharOpenHashMap implements In } @Override - public Int2CharMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Int2CharMap.Entry state = null; boolean empty = true; @@ -848,21 +855,21 @@ public class Int2CharLinkedOpenHashMap extends Int2CharOpenHashMap implements In state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Int2CharMap.Entry 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 @@ -938,6 +945,12 @@ public class Int2CharLinkedOpenHashMap extends Int2CharOpenHashMap implements In @Override public boolean add(int o) { throw new UnsupportedOperationException(); } + + @Override + public void addFirst(int o) { throw new UnsupportedOperationException(); } + + @Override + public void addLast(int o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(int o) { throw new UnsupportedOperationException(); } @@ -1084,7 +1097,7 @@ public class Int2CharLinkedOpenHashMap extends Int2CharOpenHashMap implements In } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -1099,19 +1112,19 @@ public class Int2CharLinkedOpenHashMap extends Int2CharOpenHashMap implements In state = operator.applyAsInt(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0; + if(size() <= 0) return OptionalInt.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalInt.of(keys[index]); index = (int)links[index]; } - return 0; + return OptionalInt.empty(); } @Override @@ -1247,7 +1260,7 @@ public class Int2CharLinkedOpenHashMap extends Int2CharOpenHashMap implements In } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -1262,19 +1275,19 @@ public class Int2CharLinkedOpenHashMap extends Int2CharOpenHashMap implements In state = operator.applyAsChar(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (char)0; + if(size() <= 0) return OptionalChar.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalChar.of(values[index]); index = (int)links[index]; } - return (char)0; + return OptionalChar.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/ints/maps/impl/hash/Int2CharOpenHashMap.java b/src/main/java/speiger/src/collections/ints/maps/impl/hash/Int2CharOpenHashMap.java index ac2e07c..739cdd4 100644 --- a/src/main/java/speiger/src/collections/ints/maps/impl/hash/Int2CharOpenHashMap.java +++ b/src/main/java/speiger/src/collections/ints/maps/impl/hash/Int2CharOpenHashMap.java @@ -5,10 +5,12 @@ 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; import java.util.function.IntPredicate; +import java.util.OptionalInt; import speiger.src.collections.ints.collections.IntIterator; import speiger.src.collections.ints.functions.IntConsumer; @@ -33,6 +35,7 @@ import speiger.src.collections.chars.functions.CharConsumer; import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectCharConsumer; import speiger.src.collections.chars.functions.function.CharPredicate; +import speiger.src.collections.chars.functions.OptionalChar; import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.sets.AbstractObjectSet; import speiger.src.collections.objects.sets.ObjectSet; @@ -919,7 +922,7 @@ public class Int2CharOpenHashMap extends AbstractInt2CharMap implements ITrimmab } @Override - public Int2CharMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Int2CharMap.Entry state = null; boolean empty = true; @@ -936,25 +939,25 @@ public class Int2CharOpenHashMap extends AbstractInt2CharMap implements ITrimmab } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Int2CharMap.Entry 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(keys[i] != 0) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1126,7 +1129,7 @@ public class Int2CharOpenHashMap extends AbstractInt2CharMap implements ITrimmab } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -1143,18 +1146,18 @@ public class Int2CharOpenHashMap extends AbstractInt2CharMap implements ITrimmab } state = operator.applyAsInt(state, keys[i]); } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OptionalInt.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OptionalInt.of(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(keys[i] != 0 && filter.test(keys[i])) return keys[i]; + if(keys[i] != 0 && filter.test(keys[i])) return OptionalInt.of(keys[i]); } - return 0; + return OptionalInt.empty(); } @Override @@ -1269,7 +1272,7 @@ public class Int2CharOpenHashMap extends AbstractInt2CharMap implements ITrimmab } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -1286,18 +1289,18 @@ public class Int2CharOpenHashMap extends AbstractInt2CharMap implements ITrimmab } state = operator.applyAsChar(state, values[i]); } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (char)0; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return OptionalChar.empty(); + if(containsNull && filter.test(values[nullIndex])) return OptionalChar.of(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(keys[i] != 0 && filter.test(values[i])) return values[i]; + if(keys[i] != 0 && filter.test(values[i])) return OptionalChar.of(values[i]); } - return (char)0; + return OptionalChar.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/ints/maps/impl/hash/Int2DoubleLinkedOpenHashMap.java b/src/main/java/speiger/src/collections/ints/maps/impl/hash/Int2DoubleLinkedOpenHashMap.java index 4cbe02b..69469a7 100644 --- a/src/main/java/speiger/src/collections/ints/maps/impl/hash/Int2DoubleLinkedOpenHashMap.java +++ b/src/main/java/speiger/src/collections/ints/maps/impl/hash/Int2DoubleLinkedOpenHashMap.java @@ -4,11 +4,14 @@ 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; import java.util.function.IntPredicate; +import java.util.OptionalInt; import java.util.function.DoublePredicate; +import java.util.OptionalDouble; import speiger.src.collections.ints.collections.IntBidirectionalIterator; import speiger.src.collections.ints.functions.IntConsumer; @@ -668,6 +671,10 @@ public class Int2DoubleLinkedOpenHashMap extends Int2DoubleOpenHashMap implement } private class MapEntrySet extends AbstractObjectSet implements Int2DoubleOrderedMap.FastOrderedSet { + @Override + public void addFirst(Int2DoubleMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Int2DoubleMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Int2DoubleMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -833,7 +840,7 @@ public class Int2DoubleLinkedOpenHashMap extends Int2DoubleOpenHashMap implement } @Override - public Int2DoubleMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Int2DoubleMap.Entry state = null; boolean empty = true; @@ -848,21 +855,21 @@ public class Int2DoubleLinkedOpenHashMap extends Int2DoubleOpenHashMap implement state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Int2DoubleMap.Entry 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 @@ -938,6 +945,12 @@ public class Int2DoubleLinkedOpenHashMap extends Int2DoubleOpenHashMap implement @Override public boolean add(int o) { throw new UnsupportedOperationException(); } + + @Override + public void addFirst(int o) { throw new UnsupportedOperationException(); } + + @Override + public void addLast(int o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(int o) { throw new UnsupportedOperationException(); } @@ -1084,7 +1097,7 @@ public class Int2DoubleLinkedOpenHashMap extends Int2DoubleOpenHashMap implement } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -1099,19 +1112,19 @@ public class Int2DoubleLinkedOpenHashMap extends Int2DoubleOpenHashMap implement state = operator.applyAsInt(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0; + if(size() <= 0) return OptionalInt.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalInt.of(keys[index]); index = (int)links[index]; } - return 0; + return OptionalInt.empty(); } @Override @@ -1247,7 +1260,7 @@ public class Int2DoubleLinkedOpenHashMap extends Int2DoubleOpenHashMap implement } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -1262,19 +1275,19 @@ public class Int2DoubleLinkedOpenHashMap extends Int2DoubleOpenHashMap implement state = operator.applyAsDouble(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0D; + if(size() <= 0) return OptionalDouble.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalDouble.of(values[index]); index = (int)links[index]; } - return 0D; + return OptionalDouble.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/ints/maps/impl/hash/Int2DoubleOpenHashMap.java b/src/main/java/speiger/src/collections/ints/maps/impl/hash/Int2DoubleOpenHashMap.java index 268326a..4fdcccb 100644 --- a/src/main/java/speiger/src/collections/ints/maps/impl/hash/Int2DoubleOpenHashMap.java +++ b/src/main/java/speiger/src/collections/ints/maps/impl/hash/Int2DoubleOpenHashMap.java @@ -5,11 +5,14 @@ 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; import java.util.function.IntPredicate; +import java.util.OptionalInt; import java.util.function.DoublePredicate; +import java.util.OptionalDouble; import speiger.src.collections.ints.collections.IntIterator; import speiger.src.collections.ints.functions.IntConsumer; @@ -919,7 +922,7 @@ public class Int2DoubleOpenHashMap extends AbstractInt2DoubleMap implements ITri } @Override - public Int2DoubleMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Int2DoubleMap.Entry state = null; boolean empty = true; @@ -936,25 +939,25 @@ public class Int2DoubleOpenHashMap extends AbstractInt2DoubleMap implements ITri } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Int2DoubleMap.Entry 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(keys[i] != 0) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1126,7 +1129,7 @@ public class Int2DoubleOpenHashMap extends AbstractInt2DoubleMap implements ITri } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -1143,18 +1146,18 @@ public class Int2DoubleOpenHashMap extends AbstractInt2DoubleMap implements ITri } state = operator.applyAsInt(state, keys[i]); } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OptionalInt.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OptionalInt.of(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(keys[i] != 0 && filter.test(keys[i])) return keys[i]; + if(keys[i] != 0 && filter.test(keys[i])) return OptionalInt.of(keys[i]); } - return 0; + return OptionalInt.empty(); } @Override @@ -1269,7 +1272,7 @@ public class Int2DoubleOpenHashMap extends AbstractInt2DoubleMap implements ITri } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -1286,18 +1289,18 @@ public class Int2DoubleOpenHashMap extends AbstractInt2DoubleMap implements ITri } state = operator.applyAsDouble(state, values[i]); } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0D; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return OptionalDouble.empty(); + if(containsNull && filter.test(values[nullIndex])) return OptionalDouble.of(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(keys[i] != 0 && filter.test(values[i])) return values[i]; + if(keys[i] != 0 && filter.test(values[i])) return OptionalDouble.of(values[i]); } - return 0D; + return OptionalDouble.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/ints/maps/impl/hash/Int2FloatLinkedOpenHashMap.java b/src/main/java/speiger/src/collections/ints/maps/impl/hash/Int2FloatLinkedOpenHashMap.java index d08b00a..6b4026c 100644 --- a/src/main/java/speiger/src/collections/ints/maps/impl/hash/Int2FloatLinkedOpenHashMap.java +++ b/src/main/java/speiger/src/collections/ints/maps/impl/hash/Int2FloatLinkedOpenHashMap.java @@ -4,10 +4,12 @@ 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; import java.util.function.IntPredicate; +import java.util.OptionalInt; import speiger.src.collections.ints.collections.IntBidirectionalIterator; import speiger.src.collections.ints.functions.IntConsumer; @@ -31,6 +33,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectFloatConsumer; import speiger.src.collections.floats.functions.function.FloatPredicate; +import speiger.src.collections.floats.functions.OptionalFloat; import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOperator; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.objects.lists.ObjectListIterator; @@ -668,6 +671,10 @@ public class Int2FloatLinkedOpenHashMap extends Int2FloatOpenHashMap implements } private class MapEntrySet extends AbstractObjectSet implements Int2FloatOrderedMap.FastOrderedSet { + @Override + public void addFirst(Int2FloatMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Int2FloatMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Int2FloatMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -833,7 +840,7 @@ public class Int2FloatLinkedOpenHashMap extends Int2FloatOpenHashMap implements } @Override - public Int2FloatMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Int2FloatMap.Entry state = null; boolean empty = true; @@ -848,21 +855,21 @@ public class Int2FloatLinkedOpenHashMap extends Int2FloatOpenHashMap implements state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Int2FloatMap.Entry 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 @@ -938,6 +945,12 @@ public class Int2FloatLinkedOpenHashMap extends Int2FloatOpenHashMap implements @Override public boolean add(int o) { throw new UnsupportedOperationException(); } + + @Override + public void addFirst(int o) { throw new UnsupportedOperationException(); } + + @Override + public void addLast(int o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(int o) { throw new UnsupportedOperationException(); } @@ -1084,7 +1097,7 @@ public class Int2FloatLinkedOpenHashMap extends Int2FloatOpenHashMap implements } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -1099,19 +1112,19 @@ public class Int2FloatLinkedOpenHashMap extends Int2FloatOpenHashMap implements state = operator.applyAsInt(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0; + if(size() <= 0) return OptionalInt.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalInt.of(keys[index]); index = (int)links[index]; } - return 0; + return OptionalInt.empty(); } @Override @@ -1247,7 +1260,7 @@ public class Int2FloatLinkedOpenHashMap extends Int2FloatOpenHashMap implements } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -1262,19 +1275,19 @@ public class Int2FloatLinkedOpenHashMap extends Int2FloatOpenHashMap implements state = operator.applyAsFloat(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0F; + if(size() <= 0) return OptionalFloat.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalFloat.of(values[index]); index = (int)links[index]; } - return 0F; + return OptionalFloat.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/ints/maps/impl/hash/Int2FloatOpenHashMap.java b/src/main/java/speiger/src/collections/ints/maps/impl/hash/Int2FloatOpenHashMap.java index ce2a763..cb7a40d 100644 --- a/src/main/java/speiger/src/collections/ints/maps/impl/hash/Int2FloatOpenHashMap.java +++ b/src/main/java/speiger/src/collections/ints/maps/impl/hash/Int2FloatOpenHashMap.java @@ -5,10 +5,12 @@ 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; import java.util.function.IntPredicate; +import java.util.OptionalInt; import speiger.src.collections.ints.collections.IntIterator; import speiger.src.collections.ints.functions.IntConsumer; @@ -33,6 +35,7 @@ import speiger.src.collections.floats.functions.FloatConsumer; import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectFloatConsumer; import speiger.src.collections.floats.functions.function.FloatPredicate; +import speiger.src.collections.floats.functions.OptionalFloat; import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.sets.AbstractObjectSet; import speiger.src.collections.objects.sets.ObjectSet; @@ -919,7 +922,7 @@ public class Int2FloatOpenHashMap extends AbstractInt2FloatMap implements ITrimm } @Override - public Int2FloatMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Int2FloatMap.Entry state = null; boolean empty = true; @@ -936,25 +939,25 @@ public class Int2FloatOpenHashMap extends AbstractInt2FloatMap implements ITrimm } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Int2FloatMap.Entry 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(keys[i] != 0) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1126,7 +1129,7 @@ public class Int2FloatOpenHashMap extends AbstractInt2FloatMap implements ITrimm } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -1143,18 +1146,18 @@ public class Int2FloatOpenHashMap extends AbstractInt2FloatMap implements ITrimm } state = operator.applyAsInt(state, keys[i]); } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OptionalInt.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OptionalInt.of(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(keys[i] != 0 && filter.test(keys[i])) return keys[i]; + if(keys[i] != 0 && filter.test(keys[i])) return OptionalInt.of(keys[i]); } - return 0; + return OptionalInt.empty(); } @Override @@ -1269,7 +1272,7 @@ public class Int2FloatOpenHashMap extends AbstractInt2FloatMap implements ITrimm } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -1286,18 +1289,18 @@ public class Int2FloatOpenHashMap extends AbstractInt2FloatMap implements ITrimm } state = operator.applyAsFloat(state, values[i]); } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0F; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return OptionalFloat.empty(); + if(containsNull && filter.test(values[nullIndex])) return OptionalFloat.of(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(keys[i] != 0 && filter.test(values[i])) return values[i]; + if(keys[i] != 0 && filter.test(values[i])) return OptionalFloat.of(values[i]); } - return 0F; + return OptionalFloat.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/ints/maps/impl/hash/Int2IntLinkedOpenHashMap.java b/src/main/java/speiger/src/collections/ints/maps/impl/hash/Int2IntLinkedOpenHashMap.java index 31c7cf9..23355b9 100644 --- a/src/main/java/speiger/src/collections/ints/maps/impl/hash/Int2IntLinkedOpenHashMap.java +++ b/src/main/java/speiger/src/collections/ints/maps/impl/hash/Int2IntLinkedOpenHashMap.java @@ -4,10 +4,12 @@ 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; import java.util.function.IntPredicate; +import java.util.OptionalInt; import speiger.src.collections.ints.collections.IntBidirectionalIterator; import speiger.src.collections.ints.functions.IntConsumer; @@ -661,6 +663,10 @@ public class Int2IntLinkedOpenHashMap extends Int2IntOpenHashMap implements Int2 } private class MapEntrySet extends AbstractObjectSet implements Int2IntOrderedMap.FastOrderedSet { + @Override + public void addFirst(Int2IntMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Int2IntMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Int2IntMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -826,7 +832,7 @@ public class Int2IntLinkedOpenHashMap extends Int2IntOpenHashMap implements Int2 } @Override - public Int2IntMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Int2IntMap.Entry state = null; boolean empty = true; @@ -841,21 +847,21 @@ public class Int2IntLinkedOpenHashMap extends Int2IntOpenHashMap implements Int2 state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Int2IntMap.Entry 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 @@ -931,6 +937,12 @@ public class Int2IntLinkedOpenHashMap extends Int2IntOpenHashMap implements Int2 @Override public boolean add(int o) { throw new UnsupportedOperationException(); } + + @Override + public void addFirst(int o) { throw new UnsupportedOperationException(); } + + @Override + public void addLast(int o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(int o) { throw new UnsupportedOperationException(); } @@ -1077,7 +1089,7 @@ public class Int2IntLinkedOpenHashMap extends Int2IntOpenHashMap implements Int2 } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -1092,19 +1104,19 @@ public class Int2IntLinkedOpenHashMap extends Int2IntOpenHashMap implements Int2 state = operator.applyAsInt(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0; + if(size() <= 0) return OptionalInt.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalInt.of(keys[index]); index = (int)links[index]; } - return 0; + return OptionalInt.empty(); } @Override @@ -1240,7 +1252,7 @@ public class Int2IntLinkedOpenHashMap extends Int2IntOpenHashMap implements Int2 } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -1255,19 +1267,19 @@ public class Int2IntLinkedOpenHashMap extends Int2IntOpenHashMap implements Int2 state = operator.applyAsInt(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0; + if(size() <= 0) return OptionalInt.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalInt.of(values[index]); index = (int)links[index]; } - return 0; + return OptionalInt.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/ints/maps/impl/hash/Int2IntOpenHashMap.java b/src/main/java/speiger/src/collections/ints/maps/impl/hash/Int2IntOpenHashMap.java index cd235ce..51cfc16 100644 --- a/src/main/java/speiger/src/collections/ints/maps/impl/hash/Int2IntOpenHashMap.java +++ b/src/main/java/speiger/src/collections/ints/maps/impl/hash/Int2IntOpenHashMap.java @@ -5,10 +5,12 @@ 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; import java.util.function.IntPredicate; +import java.util.OptionalInt; import speiger.src.collections.ints.collections.IntIterator; import speiger.src.collections.ints.functions.IntConsumer; @@ -913,7 +915,7 @@ public class Int2IntOpenHashMap extends AbstractInt2IntMap implements ITrimmable } @Override - public Int2IntMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Int2IntMap.Entry state = null; boolean empty = true; @@ -930,25 +932,25 @@ public class Int2IntOpenHashMap extends AbstractInt2IntMap implements ITrimmable } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Int2IntMap.Entry 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(keys[i] != 0) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1120,7 +1122,7 @@ public class Int2IntOpenHashMap extends AbstractInt2IntMap implements ITrimmable } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -1137,18 +1139,18 @@ public class Int2IntOpenHashMap extends AbstractInt2IntMap implements ITrimmable } state = operator.applyAsInt(state, keys[i]); } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OptionalInt.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OptionalInt.of(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(keys[i] != 0 && filter.test(keys[i])) return keys[i]; + if(keys[i] != 0 && filter.test(keys[i])) return OptionalInt.of(keys[i]); } - return 0; + return OptionalInt.empty(); } @Override @@ -1263,7 +1265,7 @@ public class Int2IntOpenHashMap extends AbstractInt2IntMap implements ITrimmable } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -1280,18 +1282,18 @@ public class Int2IntOpenHashMap extends AbstractInt2IntMap implements ITrimmable } state = operator.applyAsInt(state, values[i]); } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return OptionalInt.empty(); + if(containsNull && filter.test(values[nullIndex])) return OptionalInt.of(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(keys[i] != 0 && filter.test(values[i])) return values[i]; + if(keys[i] != 0 && filter.test(values[i])) return OptionalInt.of(values[i]); } - return 0; + return OptionalInt.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/ints/maps/impl/hash/Int2LongLinkedOpenHashMap.java b/src/main/java/speiger/src/collections/ints/maps/impl/hash/Int2LongLinkedOpenHashMap.java index 63559f2..fccc010 100644 --- a/src/main/java/speiger/src/collections/ints/maps/impl/hash/Int2LongLinkedOpenHashMap.java +++ b/src/main/java/speiger/src/collections/ints/maps/impl/hash/Int2LongLinkedOpenHashMap.java @@ -4,11 +4,14 @@ 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; import java.util.function.IntPredicate; +import java.util.OptionalInt; import java.util.function.LongPredicate; +import java.util.OptionalLong; import speiger.src.collections.ints.collections.IntBidirectionalIterator; import speiger.src.collections.ints.functions.IntConsumer; @@ -668,6 +671,10 @@ public class Int2LongLinkedOpenHashMap extends Int2LongOpenHashMap implements In } private class MapEntrySet extends AbstractObjectSet implements Int2LongOrderedMap.FastOrderedSet { + @Override + public void addFirst(Int2LongMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Int2LongMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Int2LongMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -833,7 +840,7 @@ public class Int2LongLinkedOpenHashMap extends Int2LongOpenHashMap implements In } @Override - public Int2LongMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Int2LongMap.Entry state = null; boolean empty = true; @@ -848,21 +855,21 @@ public class Int2LongLinkedOpenHashMap extends Int2LongOpenHashMap implements In state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Int2LongMap.Entry 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 @@ -938,6 +945,12 @@ public class Int2LongLinkedOpenHashMap extends Int2LongOpenHashMap implements In @Override public boolean add(int o) { throw new UnsupportedOperationException(); } + + @Override + public void addFirst(int o) { throw new UnsupportedOperationException(); } + + @Override + public void addLast(int o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(int o) { throw new UnsupportedOperationException(); } @@ -1084,7 +1097,7 @@ public class Int2LongLinkedOpenHashMap extends Int2LongOpenHashMap implements In } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -1099,19 +1112,19 @@ public class Int2LongLinkedOpenHashMap extends Int2LongOpenHashMap implements In state = operator.applyAsInt(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0; + if(size() <= 0) return OptionalInt.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalInt.of(keys[index]); index = (int)links[index]; } - return 0; + return OptionalInt.empty(); } @Override @@ -1247,7 +1260,7 @@ public class Int2LongLinkedOpenHashMap extends Int2LongOpenHashMap implements In } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -1262,19 +1275,19 @@ public class Int2LongLinkedOpenHashMap extends Int2LongOpenHashMap implements In state = operator.applyAsLong(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0L; + if(size() <= 0) return OptionalLong.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalLong.of(values[index]); index = (int)links[index]; } - return 0L; + return OptionalLong.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/ints/maps/impl/hash/Int2LongOpenHashMap.java b/src/main/java/speiger/src/collections/ints/maps/impl/hash/Int2LongOpenHashMap.java index b5d9524..b2d488a 100644 --- a/src/main/java/speiger/src/collections/ints/maps/impl/hash/Int2LongOpenHashMap.java +++ b/src/main/java/speiger/src/collections/ints/maps/impl/hash/Int2LongOpenHashMap.java @@ -5,11 +5,14 @@ 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; import java.util.function.IntPredicate; +import java.util.OptionalInt; import java.util.function.LongPredicate; +import java.util.OptionalLong; import speiger.src.collections.ints.collections.IntIterator; import speiger.src.collections.ints.functions.IntConsumer; @@ -919,7 +922,7 @@ public class Int2LongOpenHashMap extends AbstractInt2LongMap implements ITrimmab } @Override - public Int2LongMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Int2LongMap.Entry state = null; boolean empty = true; @@ -936,25 +939,25 @@ public class Int2LongOpenHashMap extends AbstractInt2LongMap implements ITrimmab } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Int2LongMap.Entry 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(keys[i] != 0) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1126,7 +1129,7 @@ public class Int2LongOpenHashMap extends AbstractInt2LongMap implements ITrimmab } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -1143,18 +1146,18 @@ public class Int2LongOpenHashMap extends AbstractInt2LongMap implements ITrimmab } state = operator.applyAsInt(state, keys[i]); } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OptionalInt.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OptionalInt.of(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(keys[i] != 0 && filter.test(keys[i])) return keys[i]; + if(keys[i] != 0 && filter.test(keys[i])) return OptionalInt.of(keys[i]); } - return 0; + return OptionalInt.empty(); } @Override @@ -1269,7 +1272,7 @@ public class Int2LongOpenHashMap extends AbstractInt2LongMap implements ITrimmab } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -1286,18 +1289,18 @@ public class Int2LongOpenHashMap extends AbstractInt2LongMap implements ITrimmab } state = operator.applyAsLong(state, values[i]); } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0L; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return OptionalLong.empty(); + if(containsNull && filter.test(values[nullIndex])) return OptionalLong.of(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(keys[i] != 0 && filter.test(values[i])) return values[i]; + if(keys[i] != 0 && filter.test(values[i])) return OptionalLong.of(values[i]); } - return 0L; + return OptionalLong.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/ints/maps/impl/hash/Int2ObjectLinkedOpenHashMap.java b/src/main/java/speiger/src/collections/ints/maps/impl/hash/Int2ObjectLinkedOpenHashMap.java index 98a0a89..7361d81 100644 --- a/src/main/java/speiger/src/collections/ints/maps/impl/hash/Int2ObjectLinkedOpenHashMap.java +++ b/src/main/java/speiger/src/collections/ints/maps/impl/hash/Int2ObjectLinkedOpenHashMap.java @@ -4,10 +4,12 @@ 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; import java.util.function.IntPredicate; +import java.util.OptionalInt; import speiger.src.collections.ints.collections.IntBidirectionalIterator; import speiger.src.collections.ints.functions.IntConsumer; @@ -652,6 +654,10 @@ public class Int2ObjectLinkedOpenHashMap extends Int2ObjectOpenHashMap imp } private class MapEntrySet extends AbstractObjectSet> implements Int2ObjectOrderedMap.FastOrderedSet { + @Override + public void addFirst(Int2ObjectMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Int2ObjectMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Int2ObjectMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -817,7 +823,7 @@ public class Int2ObjectLinkedOpenHashMap extends Int2ObjectOpenHashMap imp } @Override - public Int2ObjectMap.Entry reduce(ObjectObjectUnaryOperator, Int2ObjectMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Int2ObjectMap.Entry> operator) { Objects.requireNonNull(operator); Int2ObjectMap.Entry state = null; boolean empty = true; @@ -832,21 +838,21 @@ public class Int2ObjectLinkedOpenHashMap extends Int2ObjectOpenHashMap imp state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Int2ObjectMap.Entry 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 @@ -922,6 +928,12 @@ public class Int2ObjectLinkedOpenHashMap extends Int2ObjectOpenHashMap imp @Override public boolean add(int o) { throw new UnsupportedOperationException(); } + + @Override + public void addFirst(int o) { throw new UnsupportedOperationException(); } + + @Override + public void addLast(int o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(int o) { throw new UnsupportedOperationException(); } @@ -1068,7 +1080,7 @@ public class Int2ObjectLinkedOpenHashMap extends Int2ObjectOpenHashMap imp } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -1083,19 +1095,19 @@ public class Int2ObjectLinkedOpenHashMap extends Int2ObjectOpenHashMap imp state = operator.applyAsInt(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0; + if(size() <= 0) return OptionalInt.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalInt.of(keys[index]); index = (int)links[index]; } - return 0; + return OptionalInt.empty(); } @Override @@ -1232,7 +1244,7 @@ public class Int2ObjectLinkedOpenHashMap extends Int2ObjectOpenHashMap imp } @Override - public V reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); V state = null; boolean empty = true; @@ -1247,19 +1259,19 @@ public class Int2ObjectLinkedOpenHashMap extends Int2ObjectOpenHashMap imp state = operator.apply(state, values[index]); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public V findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return Optional.ofNullable(values[index]); index = (int)links[index]; } - return null; + return Optional.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/ints/maps/impl/hash/Int2ObjectOpenHashMap.java b/src/main/java/speiger/src/collections/ints/maps/impl/hash/Int2ObjectOpenHashMap.java index 17d07cf..c6be4d9 100644 --- a/src/main/java/speiger/src/collections/ints/maps/impl/hash/Int2ObjectOpenHashMap.java +++ b/src/main/java/speiger/src/collections/ints/maps/impl/hash/Int2ObjectOpenHashMap.java @@ -5,10 +5,12 @@ 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; import java.util.function.IntPredicate; +import java.util.OptionalInt; import speiger.src.collections.ints.collections.IntIterator; import speiger.src.collections.ints.functions.IntConsumer; @@ -836,7 +838,7 @@ public class Int2ObjectOpenHashMap extends AbstractInt2ObjectMap implement } @Override - public Int2ObjectMap.Entry reduce(ObjectObjectUnaryOperator, Int2ObjectMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Int2ObjectMap.Entry> operator) { Objects.requireNonNull(operator); Int2ObjectMap.Entry state = null; boolean empty = true; @@ -853,25 +855,25 @@ public class Int2ObjectOpenHashMap extends AbstractInt2ObjectMap implement } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Int2ObjectMap.Entry 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(keys[i] != 0) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1043,7 +1045,7 @@ public class Int2ObjectOpenHashMap extends AbstractInt2ObjectMap implement } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -1060,18 +1062,18 @@ public class Int2ObjectOpenHashMap extends AbstractInt2ObjectMap implement } state = operator.applyAsInt(state, keys[i]); } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OptionalInt.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OptionalInt.of(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(keys[i] != 0 && filter.test(keys[i])) return keys[i]; + if(keys[i] != 0 && filter.test(keys[i])) return OptionalInt.of(keys[i]); } - return 0; + return OptionalInt.empty(); } @Override @@ -1186,7 +1188,7 @@ public class Int2ObjectOpenHashMap extends AbstractInt2ObjectMap implement } @Override - public V reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); V state = null; boolean empty = true; @@ -1203,18 +1205,18 @@ public class Int2ObjectOpenHashMap extends AbstractInt2ObjectMap implement } state = operator.apply(state, values[i]); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public V findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return Optional.empty(); + if(containsNull && filter.test(values[nullIndex])) return Optional.ofNullable(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(keys[i] != 0 && filter.test(values[i])) return values[i]; + if(keys[i] != 0 && filter.test(values[i])) return Optional.ofNullable(values[i]); } - return null; + return Optional.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/ints/maps/impl/hash/Int2ShortLinkedOpenHashMap.java b/src/main/java/speiger/src/collections/ints/maps/impl/hash/Int2ShortLinkedOpenHashMap.java index 99201b1..b0b6e53 100644 --- a/src/main/java/speiger/src/collections/ints/maps/impl/hash/Int2ShortLinkedOpenHashMap.java +++ b/src/main/java/speiger/src/collections/ints/maps/impl/hash/Int2ShortLinkedOpenHashMap.java @@ -4,10 +4,12 @@ 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; import java.util.function.IntPredicate; +import java.util.OptionalInt; import speiger.src.collections.ints.collections.IntBidirectionalIterator; import speiger.src.collections.ints.functions.IntConsumer; @@ -31,6 +33,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectShortConsumer; import speiger.src.collections.shorts.functions.function.ShortPredicate; +import speiger.src.collections.shorts.functions.OptionalShort; import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOperator; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.objects.lists.ObjectListIterator; @@ -668,6 +671,10 @@ public class Int2ShortLinkedOpenHashMap extends Int2ShortOpenHashMap implements } private class MapEntrySet extends AbstractObjectSet implements Int2ShortOrderedMap.FastOrderedSet { + @Override + public void addFirst(Int2ShortMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Int2ShortMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Int2ShortMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -833,7 +840,7 @@ public class Int2ShortLinkedOpenHashMap extends Int2ShortOpenHashMap implements } @Override - public Int2ShortMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Int2ShortMap.Entry state = null; boolean empty = true; @@ -848,21 +855,21 @@ public class Int2ShortLinkedOpenHashMap extends Int2ShortOpenHashMap implements state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Int2ShortMap.Entry 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 @@ -938,6 +945,12 @@ public class Int2ShortLinkedOpenHashMap extends Int2ShortOpenHashMap implements @Override public boolean add(int o) { throw new UnsupportedOperationException(); } + + @Override + public void addFirst(int o) { throw new UnsupportedOperationException(); } + + @Override + public void addLast(int o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(int o) { throw new UnsupportedOperationException(); } @@ -1084,7 +1097,7 @@ public class Int2ShortLinkedOpenHashMap extends Int2ShortOpenHashMap implements } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -1099,19 +1112,19 @@ public class Int2ShortLinkedOpenHashMap extends Int2ShortOpenHashMap implements state = operator.applyAsInt(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0; + if(size() <= 0) return OptionalInt.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalInt.of(keys[index]); index = (int)links[index]; } - return 0; + return OptionalInt.empty(); } @Override @@ -1247,7 +1260,7 @@ public class Int2ShortLinkedOpenHashMap extends Int2ShortOpenHashMap implements } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -1262,19 +1275,19 @@ public class Int2ShortLinkedOpenHashMap extends Int2ShortOpenHashMap implements state = operator.applyAsShort(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (short)0; + if(size() <= 0) return OptionalShort.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalShort.of(values[index]); index = (int)links[index]; } - return (short)0; + return OptionalShort.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/ints/maps/impl/hash/Int2ShortOpenHashMap.java b/src/main/java/speiger/src/collections/ints/maps/impl/hash/Int2ShortOpenHashMap.java index 50ac239..c1ba58c 100644 --- a/src/main/java/speiger/src/collections/ints/maps/impl/hash/Int2ShortOpenHashMap.java +++ b/src/main/java/speiger/src/collections/ints/maps/impl/hash/Int2ShortOpenHashMap.java @@ -5,10 +5,12 @@ 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; import java.util.function.IntPredicate; +import java.util.OptionalInt; import speiger.src.collections.ints.collections.IntIterator; import speiger.src.collections.ints.functions.IntConsumer; @@ -33,6 +35,7 @@ import speiger.src.collections.shorts.functions.ShortConsumer; import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectShortConsumer; import speiger.src.collections.shorts.functions.function.ShortPredicate; +import speiger.src.collections.shorts.functions.OptionalShort; import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.sets.AbstractObjectSet; import speiger.src.collections.objects.sets.ObjectSet; @@ -919,7 +922,7 @@ public class Int2ShortOpenHashMap extends AbstractInt2ShortMap implements ITrimm } @Override - public Int2ShortMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Int2ShortMap.Entry state = null; boolean empty = true; @@ -936,25 +939,25 @@ public class Int2ShortOpenHashMap extends AbstractInt2ShortMap implements ITrimm } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Int2ShortMap.Entry 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(keys[i] != 0) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1126,7 +1129,7 @@ public class Int2ShortOpenHashMap extends AbstractInt2ShortMap implements ITrimm } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -1143,18 +1146,18 @@ public class Int2ShortOpenHashMap extends AbstractInt2ShortMap implements ITrimm } state = operator.applyAsInt(state, keys[i]); } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OptionalInt.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OptionalInt.of(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(keys[i] != 0 && filter.test(keys[i])) return keys[i]; + if(keys[i] != 0 && filter.test(keys[i])) return OptionalInt.of(keys[i]); } - return 0; + return OptionalInt.empty(); } @Override @@ -1269,7 +1272,7 @@ public class Int2ShortOpenHashMap extends AbstractInt2ShortMap implements ITrimm } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -1286,18 +1289,18 @@ public class Int2ShortOpenHashMap extends AbstractInt2ShortMap implements ITrimm } state = operator.applyAsShort(state, values[i]); } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (short)0; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return OptionalShort.empty(); + if(containsNull && filter.test(values[nullIndex])) return OptionalShort.of(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(keys[i] != 0 && filter.test(values[i])) return values[i]; + if(keys[i] != 0 && filter.test(values[i])) return OptionalShort.of(values[i]); } - return (short)0; + return OptionalShort.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/ints/maps/impl/immutable/ImmutableInt2BooleanOpenHashMap.java b/src/main/java/speiger/src/collections/ints/maps/impl/immutable/ImmutableInt2BooleanOpenHashMap.java index 1753c2a..7b6173a 100644 --- a/src/main/java/speiger/src/collections/ints/maps/impl/immutable/ImmutableInt2BooleanOpenHashMap.java +++ b/src/main/java/speiger/src/collections/ints/maps/impl/immutable/ImmutableInt2BooleanOpenHashMap.java @@ -4,10 +4,12 @@ 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; import java.util.function.IntPredicate; +import java.util.OptionalInt; import speiger.src.collections.ints.collections.IntBidirectionalIterator; import speiger.src.collections.ints.functions.IntConsumer; @@ -27,6 +29,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectBooleanConsumer; import speiger.src.collections.booleans.functions.function.BooleanPredicate; +import speiger.src.collections.booleans.functions.OptionalBoolean; import speiger.src.collections.ints.sets.AbstractIntSet; import speiger.src.collections.booleans.collections.AbstractBooleanCollection; import speiger.src.collections.booleans.collections.BooleanOrderedCollection; @@ -532,6 +535,10 @@ public class ImmutableInt2BooleanOpenHashMap extends AbstractInt2BooleanMap impl } private class MapEntrySet extends AbstractObjectSet implements Int2BooleanOrderedMap.FastOrderedSet { + @Override + public void addFirst(Int2BooleanMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Int2BooleanMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Int2BooleanMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -685,7 +692,7 @@ public class ImmutableInt2BooleanOpenHashMap extends AbstractInt2BooleanMap impl } @Override - public Int2BooleanMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Int2BooleanMap.Entry state = null; boolean empty = true; @@ -700,21 +707,21 @@ public class ImmutableInt2BooleanOpenHashMap extends AbstractInt2BooleanMap impl state = operator.apply(state, new BasicEntry(keys[index], values[index])); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Int2BooleanMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry entry = new BasicEntry(); 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 @@ -777,6 +784,12 @@ public class ImmutableInt2BooleanOpenHashMap extends AbstractInt2BooleanMap impl throw new UnsupportedOperationException(); } + @Override + public void addFirst(int o) { throw new UnsupportedOperationException(); } + + @Override + public void addLast(int o) { throw new UnsupportedOperationException(); } + @Override public boolean addAndMoveToFirst(int o) { throw new UnsupportedOperationException(); } @@ -912,7 +925,7 @@ public class ImmutableInt2BooleanOpenHashMap extends AbstractInt2BooleanMap impl } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -927,19 +940,19 @@ public class ImmutableInt2BooleanOpenHashMap extends AbstractInt2BooleanMap impl state = operator.applyAsInt(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0; + if(size() <= 0) return OptionalInt.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalInt.of(keys[index]); index = (int)links[index]; } - return 0; + return OptionalInt.empty(); } @Override @@ -1066,7 +1079,7 @@ public class ImmutableInt2BooleanOpenHashMap extends AbstractInt2BooleanMap impl } @Override - public boolean reduce(BooleanBooleanUnaryOperator operator) { + public OptionalBoolean reduce(BooleanBooleanUnaryOperator operator) { Objects.requireNonNull(operator); boolean state = false; boolean empty = true; @@ -1081,19 +1094,19 @@ public class ImmutableInt2BooleanOpenHashMap extends AbstractInt2BooleanMap impl state = operator.applyAsBoolean(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalBoolean.empty() : OptionalBoolean.of(state); } @Override - public boolean findFirst(BooleanPredicate filter) { + public OptionalBoolean findFirst(BooleanPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return false; + if(size() <= 0) return OptionalBoolean.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalBoolean.of(values[index]); index = (int)links[index]; } - return false; + return OptionalBoolean.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/ints/maps/impl/immutable/ImmutableInt2ByteOpenHashMap.java b/src/main/java/speiger/src/collections/ints/maps/impl/immutable/ImmutableInt2ByteOpenHashMap.java index 321b69d..175a789 100644 --- a/src/main/java/speiger/src/collections/ints/maps/impl/immutable/ImmutableInt2ByteOpenHashMap.java +++ b/src/main/java/speiger/src/collections/ints/maps/impl/immutable/ImmutableInt2ByteOpenHashMap.java @@ -4,10 +4,12 @@ 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; import java.util.function.IntPredicate; +import java.util.OptionalInt; import speiger.src.collections.ints.collections.IntBidirectionalIterator; import speiger.src.collections.ints.functions.IntConsumer; @@ -28,6 +30,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectByteConsumer; import speiger.src.collections.bytes.functions.function.BytePredicate; +import speiger.src.collections.bytes.functions.OptionalByte; import speiger.src.collections.ints.sets.AbstractIntSet; import speiger.src.collections.bytes.collections.AbstractByteCollection; import speiger.src.collections.bytes.collections.ByteOrderedCollection; @@ -537,6 +540,10 @@ public class ImmutableInt2ByteOpenHashMap extends AbstractInt2ByteMap implements } private class MapEntrySet extends AbstractObjectSet implements Int2ByteOrderedMap.FastOrderedSet { + @Override + public void addFirst(Int2ByteMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Int2ByteMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Int2ByteMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -690,7 +697,7 @@ public class ImmutableInt2ByteOpenHashMap extends AbstractInt2ByteMap implements } @Override - public Int2ByteMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Int2ByteMap.Entry state = null; boolean empty = true; @@ -705,21 +712,21 @@ public class ImmutableInt2ByteOpenHashMap extends AbstractInt2ByteMap implements state = operator.apply(state, new BasicEntry(keys[index], values[index])); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Int2ByteMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry entry = new BasicEntry(); 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 @@ -782,6 +789,12 @@ public class ImmutableInt2ByteOpenHashMap extends AbstractInt2ByteMap implements throw new UnsupportedOperationException(); } + @Override + public void addFirst(int o) { throw new UnsupportedOperationException(); } + + @Override + public void addLast(int o) { throw new UnsupportedOperationException(); } + @Override public boolean addAndMoveToFirst(int o) { throw new UnsupportedOperationException(); } @@ -917,7 +930,7 @@ public class ImmutableInt2ByteOpenHashMap extends AbstractInt2ByteMap implements } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -932,19 +945,19 @@ public class ImmutableInt2ByteOpenHashMap extends AbstractInt2ByteMap implements state = operator.applyAsInt(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0; + if(size() <= 0) return OptionalInt.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalInt.of(keys[index]); index = (int)links[index]; } - return 0; + return OptionalInt.empty(); } @Override @@ -1071,7 +1084,7 @@ public class ImmutableInt2ByteOpenHashMap extends AbstractInt2ByteMap implements } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -1086,19 +1099,19 @@ public class ImmutableInt2ByteOpenHashMap extends AbstractInt2ByteMap implements state = operator.applyAsByte(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (byte)0; + if(size() <= 0) return OptionalByte.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalByte.of(values[index]); index = (int)links[index]; } - return (byte)0; + return OptionalByte.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/ints/maps/impl/immutable/ImmutableInt2CharOpenHashMap.java b/src/main/java/speiger/src/collections/ints/maps/impl/immutable/ImmutableInt2CharOpenHashMap.java index e4c891d..5a07e65 100644 --- a/src/main/java/speiger/src/collections/ints/maps/impl/immutable/ImmutableInt2CharOpenHashMap.java +++ b/src/main/java/speiger/src/collections/ints/maps/impl/immutable/ImmutableInt2CharOpenHashMap.java @@ -4,10 +4,12 @@ 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; import java.util.function.IntPredicate; +import java.util.OptionalInt; import speiger.src.collections.ints.collections.IntBidirectionalIterator; import speiger.src.collections.ints.functions.IntConsumer; @@ -28,6 +30,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectCharConsumer; import speiger.src.collections.chars.functions.function.CharPredicate; +import speiger.src.collections.chars.functions.OptionalChar; import speiger.src.collections.ints.sets.AbstractIntSet; import speiger.src.collections.chars.collections.AbstractCharCollection; import speiger.src.collections.chars.collections.CharOrderedCollection; @@ -537,6 +540,10 @@ public class ImmutableInt2CharOpenHashMap extends AbstractInt2CharMap implements } private class MapEntrySet extends AbstractObjectSet implements Int2CharOrderedMap.FastOrderedSet { + @Override + public void addFirst(Int2CharMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Int2CharMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Int2CharMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -690,7 +697,7 @@ public class ImmutableInt2CharOpenHashMap extends AbstractInt2CharMap implements } @Override - public Int2CharMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Int2CharMap.Entry state = null; boolean empty = true; @@ -705,21 +712,21 @@ public class ImmutableInt2CharOpenHashMap extends AbstractInt2CharMap implements state = operator.apply(state, new BasicEntry(keys[index], values[index])); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Int2CharMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry entry = new BasicEntry(); 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 @@ -782,6 +789,12 @@ public class ImmutableInt2CharOpenHashMap extends AbstractInt2CharMap implements throw new UnsupportedOperationException(); } + @Override + public void addFirst(int o) { throw new UnsupportedOperationException(); } + + @Override + public void addLast(int o) { throw new UnsupportedOperationException(); } + @Override public boolean addAndMoveToFirst(int o) { throw new UnsupportedOperationException(); } @@ -917,7 +930,7 @@ public class ImmutableInt2CharOpenHashMap extends AbstractInt2CharMap implements } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -932,19 +945,19 @@ public class ImmutableInt2CharOpenHashMap extends AbstractInt2CharMap implements state = operator.applyAsInt(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0; + if(size() <= 0) return OptionalInt.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalInt.of(keys[index]); index = (int)links[index]; } - return 0; + return OptionalInt.empty(); } @Override @@ -1071,7 +1084,7 @@ public class ImmutableInt2CharOpenHashMap extends AbstractInt2CharMap implements } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -1086,19 +1099,19 @@ public class ImmutableInt2CharOpenHashMap extends AbstractInt2CharMap implements state = operator.applyAsChar(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (char)0; + if(size() <= 0) return OptionalChar.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalChar.of(values[index]); index = (int)links[index]; } - return (char)0; + return OptionalChar.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/ints/maps/impl/immutable/ImmutableInt2DoubleOpenHashMap.java b/src/main/java/speiger/src/collections/ints/maps/impl/immutable/ImmutableInt2DoubleOpenHashMap.java index cc1fcaa..40f4fba 100644 --- a/src/main/java/speiger/src/collections/ints/maps/impl/immutable/ImmutableInt2DoubleOpenHashMap.java +++ b/src/main/java/speiger/src/collections/ints/maps/impl/immutable/ImmutableInt2DoubleOpenHashMap.java @@ -4,11 +4,14 @@ 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; import java.util.function.IntPredicate; +import java.util.OptionalInt; import java.util.function.DoublePredicate; +import java.util.OptionalDouble; import speiger.src.collections.ints.collections.IntBidirectionalIterator; import speiger.src.collections.ints.functions.IntConsumer; @@ -537,6 +540,10 @@ public class ImmutableInt2DoubleOpenHashMap extends AbstractInt2DoubleMap implem } private class MapEntrySet extends AbstractObjectSet implements Int2DoubleOrderedMap.FastOrderedSet { + @Override + public void addFirst(Int2DoubleMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Int2DoubleMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Int2DoubleMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -690,7 +697,7 @@ public class ImmutableInt2DoubleOpenHashMap extends AbstractInt2DoubleMap implem } @Override - public Int2DoubleMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Int2DoubleMap.Entry state = null; boolean empty = true; @@ -705,21 +712,21 @@ public class ImmutableInt2DoubleOpenHashMap extends AbstractInt2DoubleMap implem state = operator.apply(state, new BasicEntry(keys[index], values[index])); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Int2DoubleMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry entry = new BasicEntry(); 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 @@ -782,6 +789,12 @@ public class ImmutableInt2DoubleOpenHashMap extends AbstractInt2DoubleMap implem throw new UnsupportedOperationException(); } + @Override + public void addFirst(int o) { throw new UnsupportedOperationException(); } + + @Override + public void addLast(int o) { throw new UnsupportedOperationException(); } + @Override public boolean addAndMoveToFirst(int o) { throw new UnsupportedOperationException(); } @@ -917,7 +930,7 @@ public class ImmutableInt2DoubleOpenHashMap extends AbstractInt2DoubleMap implem } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -932,19 +945,19 @@ public class ImmutableInt2DoubleOpenHashMap extends AbstractInt2DoubleMap implem state = operator.applyAsInt(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0; + if(size() <= 0) return OptionalInt.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalInt.of(keys[index]); index = (int)links[index]; } - return 0; + return OptionalInt.empty(); } @Override @@ -1071,7 +1084,7 @@ public class ImmutableInt2DoubleOpenHashMap extends AbstractInt2DoubleMap implem } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -1086,19 +1099,19 @@ public class ImmutableInt2DoubleOpenHashMap extends AbstractInt2DoubleMap implem state = operator.applyAsDouble(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0D; + if(size() <= 0) return OptionalDouble.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalDouble.of(values[index]); index = (int)links[index]; } - return 0D; + return OptionalDouble.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/ints/maps/impl/immutable/ImmutableInt2FloatOpenHashMap.java b/src/main/java/speiger/src/collections/ints/maps/impl/immutable/ImmutableInt2FloatOpenHashMap.java index bb58ea0..cc786c1 100644 --- a/src/main/java/speiger/src/collections/ints/maps/impl/immutable/ImmutableInt2FloatOpenHashMap.java +++ b/src/main/java/speiger/src/collections/ints/maps/impl/immutable/ImmutableInt2FloatOpenHashMap.java @@ -4,10 +4,12 @@ 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; import java.util.function.IntPredicate; +import java.util.OptionalInt; import speiger.src.collections.ints.collections.IntBidirectionalIterator; import speiger.src.collections.ints.functions.IntConsumer; @@ -28,6 +30,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectFloatConsumer; import speiger.src.collections.floats.functions.function.FloatPredicate; +import speiger.src.collections.floats.functions.OptionalFloat; import speiger.src.collections.ints.sets.AbstractIntSet; import speiger.src.collections.floats.collections.AbstractFloatCollection; import speiger.src.collections.floats.collections.FloatOrderedCollection; @@ -537,6 +540,10 @@ public class ImmutableInt2FloatOpenHashMap extends AbstractInt2FloatMap implemen } private class MapEntrySet extends AbstractObjectSet implements Int2FloatOrderedMap.FastOrderedSet { + @Override + public void addFirst(Int2FloatMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Int2FloatMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Int2FloatMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -690,7 +697,7 @@ public class ImmutableInt2FloatOpenHashMap extends AbstractInt2FloatMap implemen } @Override - public Int2FloatMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Int2FloatMap.Entry state = null; boolean empty = true; @@ -705,21 +712,21 @@ public class ImmutableInt2FloatOpenHashMap extends AbstractInt2FloatMap implemen state = operator.apply(state, new BasicEntry(keys[index], values[index])); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Int2FloatMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry entry = new BasicEntry(); 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 @@ -782,6 +789,12 @@ public class ImmutableInt2FloatOpenHashMap extends AbstractInt2FloatMap implemen throw new UnsupportedOperationException(); } + @Override + public void addFirst(int o) { throw new UnsupportedOperationException(); } + + @Override + public void addLast(int o) { throw new UnsupportedOperationException(); } + @Override public boolean addAndMoveToFirst(int o) { throw new UnsupportedOperationException(); } @@ -917,7 +930,7 @@ public class ImmutableInt2FloatOpenHashMap extends AbstractInt2FloatMap implemen } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -932,19 +945,19 @@ public class ImmutableInt2FloatOpenHashMap extends AbstractInt2FloatMap implemen state = operator.applyAsInt(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0; + if(size() <= 0) return OptionalInt.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalInt.of(keys[index]); index = (int)links[index]; } - return 0; + return OptionalInt.empty(); } @Override @@ -1071,7 +1084,7 @@ public class ImmutableInt2FloatOpenHashMap extends AbstractInt2FloatMap implemen } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -1086,19 +1099,19 @@ public class ImmutableInt2FloatOpenHashMap extends AbstractInt2FloatMap implemen state = operator.applyAsFloat(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0F; + if(size() <= 0) return OptionalFloat.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalFloat.of(values[index]); index = (int)links[index]; } - return 0F; + return OptionalFloat.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/ints/maps/impl/immutable/ImmutableInt2IntOpenHashMap.java b/src/main/java/speiger/src/collections/ints/maps/impl/immutable/ImmutableInt2IntOpenHashMap.java index cf37b08..b9b8813 100644 --- a/src/main/java/speiger/src/collections/ints/maps/impl/immutable/ImmutableInt2IntOpenHashMap.java +++ b/src/main/java/speiger/src/collections/ints/maps/impl/immutable/ImmutableInt2IntOpenHashMap.java @@ -4,10 +4,12 @@ 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; import java.util.function.IntPredicate; +import java.util.OptionalInt; import speiger.src.collections.ints.collections.IntBidirectionalIterator; import speiger.src.collections.ints.functions.IntConsumer; @@ -528,6 +530,10 @@ public class ImmutableInt2IntOpenHashMap extends AbstractInt2IntMap implements I } private class MapEntrySet extends AbstractObjectSet implements Int2IntOrderedMap.FastOrderedSet { + @Override + public void addFirst(Int2IntMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Int2IntMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Int2IntMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -681,7 +687,7 @@ public class ImmutableInt2IntOpenHashMap extends AbstractInt2IntMap implements I } @Override - public Int2IntMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Int2IntMap.Entry state = null; boolean empty = true; @@ -696,21 +702,21 @@ public class ImmutableInt2IntOpenHashMap extends AbstractInt2IntMap implements I state = operator.apply(state, new BasicEntry(keys[index], values[index])); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Int2IntMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry entry = new BasicEntry(); 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 @@ -773,6 +779,12 @@ public class ImmutableInt2IntOpenHashMap extends AbstractInt2IntMap implements I throw new UnsupportedOperationException(); } + @Override + public void addFirst(int o) { throw new UnsupportedOperationException(); } + + @Override + public void addLast(int o) { throw new UnsupportedOperationException(); } + @Override public boolean addAndMoveToFirst(int o) { throw new UnsupportedOperationException(); } @@ -908,7 +920,7 @@ public class ImmutableInt2IntOpenHashMap extends AbstractInt2IntMap implements I } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -923,19 +935,19 @@ public class ImmutableInt2IntOpenHashMap extends AbstractInt2IntMap implements I state = operator.applyAsInt(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0; + if(size() <= 0) return OptionalInt.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalInt.of(keys[index]); index = (int)links[index]; } - return 0; + return OptionalInt.empty(); } @Override @@ -1062,7 +1074,7 @@ public class ImmutableInt2IntOpenHashMap extends AbstractInt2IntMap implements I } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -1077,19 +1089,19 @@ public class ImmutableInt2IntOpenHashMap extends AbstractInt2IntMap implements I state = operator.applyAsInt(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0; + if(size() <= 0) return OptionalInt.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalInt.of(values[index]); index = (int)links[index]; } - return 0; + return OptionalInt.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/ints/maps/impl/immutable/ImmutableInt2LongOpenHashMap.java b/src/main/java/speiger/src/collections/ints/maps/impl/immutable/ImmutableInt2LongOpenHashMap.java index ddd7b15..49e6289 100644 --- a/src/main/java/speiger/src/collections/ints/maps/impl/immutable/ImmutableInt2LongOpenHashMap.java +++ b/src/main/java/speiger/src/collections/ints/maps/impl/immutable/ImmutableInt2LongOpenHashMap.java @@ -4,11 +4,14 @@ 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; import java.util.function.IntPredicate; +import java.util.OptionalInt; import java.util.function.LongPredicate; +import java.util.OptionalLong; import speiger.src.collections.ints.collections.IntBidirectionalIterator; import speiger.src.collections.ints.functions.IntConsumer; @@ -537,6 +540,10 @@ public class ImmutableInt2LongOpenHashMap extends AbstractInt2LongMap implements } private class MapEntrySet extends AbstractObjectSet implements Int2LongOrderedMap.FastOrderedSet { + @Override + public void addFirst(Int2LongMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Int2LongMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Int2LongMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -690,7 +697,7 @@ public class ImmutableInt2LongOpenHashMap extends AbstractInt2LongMap implements } @Override - public Int2LongMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Int2LongMap.Entry state = null; boolean empty = true; @@ -705,21 +712,21 @@ public class ImmutableInt2LongOpenHashMap extends AbstractInt2LongMap implements state = operator.apply(state, new BasicEntry(keys[index], values[index])); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Int2LongMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry entry = new BasicEntry(); 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 @@ -782,6 +789,12 @@ public class ImmutableInt2LongOpenHashMap extends AbstractInt2LongMap implements throw new UnsupportedOperationException(); } + @Override + public void addFirst(int o) { throw new UnsupportedOperationException(); } + + @Override + public void addLast(int o) { throw new UnsupportedOperationException(); } + @Override public boolean addAndMoveToFirst(int o) { throw new UnsupportedOperationException(); } @@ -917,7 +930,7 @@ public class ImmutableInt2LongOpenHashMap extends AbstractInt2LongMap implements } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -932,19 +945,19 @@ public class ImmutableInt2LongOpenHashMap extends AbstractInt2LongMap implements state = operator.applyAsInt(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0; + if(size() <= 0) return OptionalInt.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalInt.of(keys[index]); index = (int)links[index]; } - return 0; + return OptionalInt.empty(); } @Override @@ -1071,7 +1084,7 @@ public class ImmutableInt2LongOpenHashMap extends AbstractInt2LongMap implements } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -1086,19 +1099,19 @@ public class ImmutableInt2LongOpenHashMap extends AbstractInt2LongMap implements state = operator.applyAsLong(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0L; + if(size() <= 0) return OptionalLong.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalLong.of(values[index]); index = (int)links[index]; } - return 0L; + return OptionalLong.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/ints/maps/impl/immutable/ImmutableInt2ObjectOpenHashMap.java b/src/main/java/speiger/src/collections/ints/maps/impl/immutable/ImmutableInt2ObjectOpenHashMap.java index b858713..e5762de 100644 --- a/src/main/java/speiger/src/collections/ints/maps/impl/immutable/ImmutableInt2ObjectOpenHashMap.java +++ b/src/main/java/speiger/src/collections/ints/maps/impl/immutable/ImmutableInt2ObjectOpenHashMap.java @@ -4,10 +4,12 @@ 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; import java.util.function.IntPredicate; +import java.util.OptionalInt; import speiger.src.collections.ints.collections.IntBidirectionalIterator; import speiger.src.collections.ints.functions.IntConsumer; @@ -508,6 +510,10 @@ public class ImmutableInt2ObjectOpenHashMap extends AbstractInt2ObjectMap } private class MapEntrySet extends AbstractObjectSet> implements Int2ObjectOrderedMap.FastOrderedSet { + @Override + public void addFirst(Int2ObjectMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Int2ObjectMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Int2ObjectMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -661,7 +667,7 @@ public class ImmutableInt2ObjectOpenHashMap extends AbstractInt2ObjectMap } @Override - public Int2ObjectMap.Entry reduce(ObjectObjectUnaryOperator, Int2ObjectMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Int2ObjectMap.Entry> operator) { Objects.requireNonNull(operator); Int2ObjectMap.Entry state = null; boolean empty = true; @@ -676,21 +682,21 @@ public class ImmutableInt2ObjectOpenHashMap extends AbstractInt2ObjectMap state = operator.apply(state, new BasicEntry<>(keys[index], values[index])); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Int2ObjectMap.Entry findFirst(Predicate> filter) { + public Optional> findFirst(Predicate> filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry entry = new BasicEntry<>(); 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 @@ -753,6 +759,12 @@ public class ImmutableInt2ObjectOpenHashMap extends AbstractInt2ObjectMap throw new UnsupportedOperationException(); } + @Override + public void addFirst(int o) { throw new UnsupportedOperationException(); } + + @Override + public void addLast(int o) { throw new UnsupportedOperationException(); } + @Override public boolean addAndMoveToFirst(int o) { throw new UnsupportedOperationException(); } @@ -888,7 +900,7 @@ public class ImmutableInt2ObjectOpenHashMap extends AbstractInt2ObjectMap } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -903,19 +915,19 @@ public class ImmutableInt2ObjectOpenHashMap extends AbstractInt2ObjectMap state = operator.applyAsInt(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0; + if(size() <= 0) return OptionalInt.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalInt.of(keys[index]); index = (int)links[index]; } - return 0; + return OptionalInt.empty(); } @Override @@ -1043,7 +1055,7 @@ public class ImmutableInt2ObjectOpenHashMap extends AbstractInt2ObjectMap } @Override - public V reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); V state = null; boolean empty = true; @@ -1058,19 +1070,19 @@ public class ImmutableInt2ObjectOpenHashMap extends AbstractInt2ObjectMap state = operator.apply(state, values[index]); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public V findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return Optional.ofNullable(values[index]); index = (int)links[index]; } - return null; + return Optional.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/ints/maps/impl/immutable/ImmutableInt2ShortOpenHashMap.java b/src/main/java/speiger/src/collections/ints/maps/impl/immutable/ImmutableInt2ShortOpenHashMap.java index 8067a31..62d8343 100644 --- a/src/main/java/speiger/src/collections/ints/maps/impl/immutable/ImmutableInt2ShortOpenHashMap.java +++ b/src/main/java/speiger/src/collections/ints/maps/impl/immutable/ImmutableInt2ShortOpenHashMap.java @@ -4,10 +4,12 @@ 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; import java.util.function.IntPredicate; +import java.util.OptionalInt; import speiger.src.collections.ints.collections.IntBidirectionalIterator; import speiger.src.collections.ints.functions.IntConsumer; @@ -28,6 +30,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectShortConsumer; import speiger.src.collections.shorts.functions.function.ShortPredicate; +import speiger.src.collections.shorts.functions.OptionalShort; import speiger.src.collections.ints.sets.AbstractIntSet; import speiger.src.collections.shorts.collections.AbstractShortCollection; import speiger.src.collections.shorts.collections.ShortOrderedCollection; @@ -537,6 +540,10 @@ public class ImmutableInt2ShortOpenHashMap extends AbstractInt2ShortMap implemen } private class MapEntrySet extends AbstractObjectSet implements Int2ShortOrderedMap.FastOrderedSet { + @Override + public void addFirst(Int2ShortMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Int2ShortMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Int2ShortMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -690,7 +697,7 @@ public class ImmutableInt2ShortOpenHashMap extends AbstractInt2ShortMap implemen } @Override - public Int2ShortMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Int2ShortMap.Entry state = null; boolean empty = true; @@ -705,21 +712,21 @@ public class ImmutableInt2ShortOpenHashMap extends AbstractInt2ShortMap implemen state = operator.apply(state, new BasicEntry(keys[index], values[index])); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Int2ShortMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry entry = new BasicEntry(); 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 @@ -782,6 +789,12 @@ public class ImmutableInt2ShortOpenHashMap extends AbstractInt2ShortMap implemen throw new UnsupportedOperationException(); } + @Override + public void addFirst(int o) { throw new UnsupportedOperationException(); } + + @Override + public void addLast(int o) { throw new UnsupportedOperationException(); } + @Override public boolean addAndMoveToFirst(int o) { throw new UnsupportedOperationException(); } @@ -917,7 +930,7 @@ public class ImmutableInt2ShortOpenHashMap extends AbstractInt2ShortMap implemen } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -932,19 +945,19 @@ public class ImmutableInt2ShortOpenHashMap extends AbstractInt2ShortMap implemen state = operator.applyAsInt(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0; + if(size() <= 0) return OptionalInt.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalInt.of(keys[index]); index = (int)links[index]; } - return 0; + return OptionalInt.empty(); } @Override @@ -1071,7 +1084,7 @@ public class ImmutableInt2ShortOpenHashMap extends AbstractInt2ShortMap implemen } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -1086,19 +1099,19 @@ public class ImmutableInt2ShortOpenHashMap extends AbstractInt2ShortMap implemen state = operator.applyAsShort(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (short)0; + if(size() <= 0) return OptionalShort.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalShort.of(values[index]); index = (int)links[index]; } - return (short)0; + return OptionalShort.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/ints/maps/impl/misc/Int2BooleanArrayMap.java b/src/main/java/speiger/src/collections/ints/maps/impl/misc/Int2BooleanArrayMap.java index 67f1c8f..ef7a267 100644 --- a/src/main/java/speiger/src/collections/ints/maps/impl/misc/Int2BooleanArrayMap.java +++ b/src/main/java/speiger/src/collections/ints/maps/impl/misc/Int2BooleanArrayMap.java @@ -4,10 +4,12 @@ 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; import java.util.function.IntPredicate; +import java.util.OptionalInt; import speiger.src.collections.ints.collections.IntBidirectionalIterator; import speiger.src.collections.ints.functions.IntConsumer; @@ -33,6 +35,7 @@ import speiger.src.collections.booleans.lists.BooleanListIterator; import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.booleans.functions.function.BooleanPredicate; +import speiger.src.collections.booleans.functions.OptionalBoolean; import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOperator; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; @@ -727,6 +730,10 @@ public class Int2BooleanArrayMap extends AbstractInt2BooleanMap implements Int2B } private class MapEntrySet extends AbstractObjectSet implements Int2BooleanOrderedMap.FastOrderedSet { + @Override + public void addFirst(Int2BooleanMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Int2BooleanMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Int2BooleanMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -875,7 +882,7 @@ public class Int2BooleanArrayMap extends AbstractInt2BooleanMap implements Int2B } @Override - public Int2BooleanMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Int2BooleanMap.Entry state = null; boolean empty = true; @@ -887,19 +894,19 @@ public class Int2BooleanArrayMap extends AbstractInt2BooleanMap implements Int2B } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Int2BooleanMap.Entry 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 implements Int2ByteOrderedMap.FastOrderedSet { + @Override + public void addFirst(Int2ByteMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Int2ByteMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Int2ByteMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -898,7 +905,7 @@ public class Int2ByteArrayMap extends AbstractInt2ByteMap implements Int2ByteOrd } @Override - public Int2ByteMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Int2ByteMap.Entry state = null; boolean empty = true; @@ -910,19 +917,19 @@ public class Int2ByteArrayMap extends AbstractInt2ByteMap implements Int2ByteOrd } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Int2ByteMap.Entry 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 implements Int2CharOrderedMap.FastOrderedSet { + @Override + public void addFirst(Int2CharMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Int2CharMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Int2CharMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -898,7 +905,7 @@ public class Int2CharArrayMap extends AbstractInt2CharMap implements Int2CharOrd } @Override - public Int2CharMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Int2CharMap.Entry state = null; boolean empty = true; @@ -910,19 +917,19 @@ public class Int2CharArrayMap extends AbstractInt2CharMap implements Int2CharOrd } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Int2CharMap.Entry 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 implements Int2DoubleOrderedMap.FastOrderedSet { + @Override + public void addFirst(Int2DoubleMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Int2DoubleMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Int2DoubleMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -898,7 +905,7 @@ public class Int2DoubleArrayMap extends AbstractInt2DoubleMap implements Int2Dou } @Override - public Int2DoubleMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Int2DoubleMap.Entry state = null; boolean empty = true; @@ -910,19 +917,19 @@ public class Int2DoubleArrayMap extends AbstractInt2DoubleMap implements Int2Dou } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Int2DoubleMap.Entry 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 implements Int2FloatOrderedMap.FastOrderedSet { + @Override + public void addFirst(Int2FloatMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Int2FloatMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Int2FloatMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -898,7 +905,7 @@ public class Int2FloatArrayMap extends AbstractInt2FloatMap implements Int2Float } @Override - public Int2FloatMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Int2FloatMap.Entry state = null; boolean empty = true; @@ -910,19 +917,19 @@ public class Int2FloatArrayMap extends AbstractInt2FloatMap implements Int2Float } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Int2FloatMap.Entry 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 implements Int2IntOrderedMap.FastOrderedSet { + @Override + public void addFirst(Int2IntMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Int2IntMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Int2IntMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -891,7 +897,7 @@ public class Int2IntArrayMap extends AbstractInt2IntMap implements Int2IntOrdere } @Override - public Int2IntMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Int2IntMap.Entry state = null; boolean empty = true; @@ -903,19 +909,19 @@ public class Int2IntArrayMap extends AbstractInt2IntMap implements Int2IntOrdere } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Int2IntMap.Entry 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 implements Int2LongOrderedMap.FastOrderedSet { + @Override + public void addFirst(Int2LongMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Int2LongMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Int2LongMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -898,7 +905,7 @@ public class Int2LongArrayMap extends AbstractInt2LongMap implements Int2LongOrd } @Override - public Int2LongMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Int2LongMap.Entry state = null; boolean empty = true; @@ -910,19 +917,19 @@ public class Int2LongArrayMap extends AbstractInt2LongMap implements Int2LongOrd } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Int2LongMap.Entry 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 extends AbstractInt2ObjectMap implements I } private class MapEntrySet extends AbstractObjectSet> implements Int2ObjectOrderedMap.FastOrderedSet { + @Override + public void addFirst(Int2ObjectMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Int2ObjectMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Int2ObjectMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -811,7 +817,7 @@ public class Int2ObjectArrayMap extends AbstractInt2ObjectMap implements I } @Override - public Int2ObjectMap.Entry reduce(ObjectObjectUnaryOperator, Int2ObjectMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Int2ObjectMap.Entry> operator) { Objects.requireNonNull(operator); Int2ObjectMap.Entry state = null; boolean empty = true; @@ -823,19 +829,19 @@ public class Int2ObjectArrayMap extends AbstractInt2ObjectMap implements I } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Int2ObjectMap.Entry 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 extends AbstractInt2ObjectMap implements I @Override public boolean add(int o) { throw new UnsupportedOperationException(); } @Override + public void addFirst(int o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(int o) { throw new UnsupportedOperationException(); } + @Override public boolean addAndMoveToFirst(int o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToLast(int o) { throw new UnsupportedOperationException(); } @@ -994,7 +1004,7 @@ public class Int2ObjectArrayMap extends AbstractInt2ObjectMap implements I } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -1006,16 +1016,16 @@ public class Int2ObjectArrayMap extends AbstractInt2ObjectMap implements I } state = operator.applyAsInt(state, keys[i]); } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); for(int i = 0;i extends AbstractInt2ObjectMap implements I } @Override - public V reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); V state = null; boolean empty = true; @@ -1128,16 +1138,16 @@ public class Int2ObjectArrayMap extends AbstractInt2ObjectMap implements I } state = operator.apply(state, values[i]); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public V findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); for(int i = 0;i implements Int2ShortOrderedMap.FastOrderedSet { + @Override + public void addFirst(Int2ShortMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Int2ShortMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Int2ShortMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -898,7 +905,7 @@ public class Int2ShortArrayMap extends AbstractInt2ShortMap implements Int2Short } @Override - public Int2ShortMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Int2ShortMap.Entry state = null; boolean empty = true; @@ -910,19 +917,19 @@ public class Int2ShortArrayMap extends AbstractInt2ShortMap implements Int2Short } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Int2ShortMap.Entry 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); Int2BooleanMap.Entry state = null; boolean empty = true; @@ -1723,19 +1726,19 @@ public class Int2BooleanAVLTreeMap extends AbstractInt2BooleanMap implements Int } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Int2BooleanMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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 @@ -1830,7 +1833,7 @@ public class Int2BooleanAVLTreeMap extends AbstractInt2BooleanMap implements Int } @Override - public boolean reduce(BooleanBooleanUnaryOperator operator) { + public OptionalBoolean reduce(BooleanBooleanUnaryOperator operator) { Objects.requireNonNull(operator); boolean state = false; boolean empty = true; @@ -1842,15 +1845,15 @@ public class Int2BooleanAVLTreeMap extends AbstractInt2BooleanMap implements Int } state = operator.applyAsBoolean(state, entry.value); } - return state; + return empty ? OptionalBoolean.empty() : OptionalBoolean.of(state); } @Override - public boolean findFirst(BooleanPredicate filter) { + public OptionalBoolean findFirst(BooleanPredicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return false; + if(filter.test(entry.value)) return OptionalBoolean.of(entry.value); + return OptionalBoolean.empty(); } @Override @@ -2166,7 +2169,7 @@ public class Int2BooleanAVLTreeMap extends AbstractInt2BooleanMap implements Int } @Override - public boolean reduce(BooleanBooleanUnaryOperator operator) { + public OptionalBoolean reduce(BooleanBooleanUnaryOperator operator) { Objects.requireNonNull(operator); boolean state = false; boolean empty = true; @@ -2178,15 +2181,15 @@ public class Int2BooleanAVLTreeMap extends AbstractInt2BooleanMap implements Int } state = operator.applyAsBoolean(state, entry.value); } - return state; + return empty ? OptionalBoolean.empty() : OptionalBoolean.of(state); } @Override - public boolean findFirst(BooleanPredicate filter) { + public OptionalBoolean findFirst(BooleanPredicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return false; + if(filter.test(entry.value)) return OptionalBoolean.of(entry.value); + return OptionalBoolean.empty(); } @Override @@ -2327,7 +2330,7 @@ public class Int2BooleanAVLTreeMap extends AbstractInt2BooleanMap implements Int } @Override - public Int2BooleanMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Int2BooleanMap.Entry state = null; boolean empty = true; @@ -2339,19 +2342,19 @@ public class Int2BooleanAVLTreeMap extends AbstractInt2BooleanMap implements Int } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Int2BooleanMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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/main/java/speiger/src/collections/ints/maps/impl/tree/Int2BooleanRBTreeMap.java b/src/main/java/speiger/src/collections/ints/maps/impl/tree/Int2BooleanRBTreeMap.java index b1c568b..0c045e1 100644 --- a/src/main/java/speiger/src/collections/ints/maps/impl/tree/Int2BooleanRBTreeMap.java +++ b/src/main/java/speiger/src/collections/ints/maps/impl/tree/Int2BooleanRBTreeMap.java @@ -3,11 +3,13 @@ package speiger.src.collections.ints.maps.impl.tree; import java.util.Collections; import java.util.Map; 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; import java.util.function.IntPredicate; +import java.util.OptionalInt; import speiger.src.collections.ints.collections.IntBidirectionalIterator; import speiger.src.collections.ints.functions.IntComparator; @@ -34,6 +36,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectBooleanConsumer; import speiger.src.collections.booleans.functions.function.BooleanPredicate; +import speiger.src.collections.booleans.functions.OptionalBoolean; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOperator; @@ -1122,7 +1125,7 @@ public class Int2BooleanRBTreeMap extends AbstractInt2BooleanMap implements Int2 } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -1134,15 +1137,15 @@ public class Int2BooleanRBTreeMap extends AbstractInt2BooleanMap implements Int2 } state = operator.applyAsInt(state, entry.key); } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); for(Node entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.test(entry.key)) return entry.key; - return 0; + if(filter.test(entry.key)) return OptionalInt.of(entry.key); + return OptionalInt.empty(); } @Override @@ -1774,7 +1777,7 @@ public class Int2BooleanRBTreeMap extends AbstractInt2BooleanMap implements Int2 } @Override - public Int2BooleanMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Int2BooleanMap.Entry state = null; boolean empty = true; @@ -1786,19 +1789,19 @@ public class Int2BooleanRBTreeMap extends AbstractInt2BooleanMap implements Int2 } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Int2BooleanMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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 @@ -1893,7 +1896,7 @@ public class Int2BooleanRBTreeMap extends AbstractInt2BooleanMap implements Int2 } @Override - public boolean reduce(BooleanBooleanUnaryOperator operator) { + public OptionalBoolean reduce(BooleanBooleanUnaryOperator operator) { Objects.requireNonNull(operator); boolean state = false; boolean empty = true; @@ -1905,15 +1908,15 @@ public class Int2BooleanRBTreeMap extends AbstractInt2BooleanMap implements Int2 } state = operator.applyAsBoolean(state, entry.value); } - return state; + return empty ? OptionalBoolean.empty() : OptionalBoolean.of(state); } @Override - public boolean findFirst(BooleanPredicate filter) { + public OptionalBoolean findFirst(BooleanPredicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return false; + if(filter.test(entry.value)) return OptionalBoolean.of(entry.value); + return OptionalBoolean.empty(); } @Override @@ -2229,7 +2232,7 @@ public class Int2BooleanRBTreeMap extends AbstractInt2BooleanMap implements Int2 } @Override - public boolean reduce(BooleanBooleanUnaryOperator operator) { + public OptionalBoolean reduce(BooleanBooleanUnaryOperator operator) { Objects.requireNonNull(operator); boolean state = false; boolean empty = true; @@ -2241,15 +2244,15 @@ public class Int2BooleanRBTreeMap extends AbstractInt2BooleanMap implements Int2 } state = operator.applyAsBoolean(state, entry.value); } - return state; + return empty ? OptionalBoolean.empty() : OptionalBoolean.of(state); } @Override - public boolean findFirst(BooleanPredicate filter) { + public OptionalBoolean findFirst(BooleanPredicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return false; + if(filter.test(entry.value)) return OptionalBoolean.of(entry.value); + return OptionalBoolean.empty(); } @Override @@ -2390,7 +2393,7 @@ public class Int2BooleanRBTreeMap extends AbstractInt2BooleanMap implements Int2 } @Override - public Int2BooleanMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Int2BooleanMap.Entry state = null; boolean empty = true; @@ -2402,19 +2405,19 @@ public class Int2BooleanRBTreeMap extends AbstractInt2BooleanMap implements Int2 } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Int2BooleanMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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/main/java/speiger/src/collections/ints/maps/impl/tree/Int2ByteAVLTreeMap.java b/src/main/java/speiger/src/collections/ints/maps/impl/tree/Int2ByteAVLTreeMap.java index 0e91dbf..3d32f82 100644 --- a/src/main/java/speiger/src/collections/ints/maps/impl/tree/Int2ByteAVLTreeMap.java +++ b/src/main/java/speiger/src/collections/ints/maps/impl/tree/Int2ByteAVLTreeMap.java @@ -3,11 +3,13 @@ package speiger.src.collections.ints.maps.impl.tree; import java.util.Collections; import java.util.Map; 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; import java.util.function.IntPredicate; +import java.util.OptionalInt; import speiger.src.collections.ints.collections.IntBidirectionalIterator; @@ -36,6 +38,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectByteConsumer; import speiger.src.collections.bytes.functions.function.BytePredicate; +import speiger.src.collections.bytes.functions.OptionalByte; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOperator; @@ -1128,7 +1131,7 @@ public class Int2ByteAVLTreeMap extends AbstractInt2ByteMap implements Int2ByteN } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -1140,15 +1143,15 @@ public class Int2ByteAVLTreeMap extends AbstractInt2ByteMap implements Int2ByteN } state = operator.applyAsInt(state, entry.key); } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); for(Node entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.test(entry.key)) return entry.key; - return 0; + if(filter.test(entry.key)) return OptionalInt.of(entry.key); + return OptionalInt.empty(); } @Override @@ -1782,7 +1785,7 @@ public class Int2ByteAVLTreeMap extends AbstractInt2ByteMap implements Int2ByteN } @Override - public Int2ByteMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Int2ByteMap.Entry state = null; boolean empty = true; @@ -1794,19 +1797,19 @@ public class Int2ByteAVLTreeMap extends AbstractInt2ByteMap implements Int2ByteN } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Int2ByteMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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 @@ -1901,7 +1904,7 @@ public class Int2ByteAVLTreeMap extends AbstractInt2ByteMap implements Int2ByteN } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -1913,15 +1916,15 @@ public class Int2ByteAVLTreeMap extends AbstractInt2ByteMap implements Int2ByteN } state = operator.applyAsByte(state, entry.value); } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return (byte)0; + if(filter.test(entry.value)) return OptionalByte.of(entry.value); + return OptionalByte.empty(); } @Override @@ -2237,7 +2240,7 @@ public class Int2ByteAVLTreeMap extends AbstractInt2ByteMap implements Int2ByteN } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -2249,15 +2252,15 @@ public class Int2ByteAVLTreeMap extends AbstractInt2ByteMap implements Int2ByteN } state = operator.applyAsByte(state, entry.value); } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return (byte)0; + if(filter.test(entry.value)) return OptionalByte.of(entry.value); + return OptionalByte.empty(); } @Override @@ -2398,7 +2401,7 @@ public class Int2ByteAVLTreeMap extends AbstractInt2ByteMap implements Int2ByteN } @Override - public Int2ByteMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Int2ByteMap.Entry state = null; boolean empty = true; @@ -2410,19 +2413,19 @@ public class Int2ByteAVLTreeMap extends AbstractInt2ByteMap implements Int2ByteN } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Int2ByteMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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/main/java/speiger/src/collections/ints/maps/impl/tree/Int2ByteRBTreeMap.java b/src/main/java/speiger/src/collections/ints/maps/impl/tree/Int2ByteRBTreeMap.java index fa84284..f1a5744 100644 --- a/src/main/java/speiger/src/collections/ints/maps/impl/tree/Int2ByteRBTreeMap.java +++ b/src/main/java/speiger/src/collections/ints/maps/impl/tree/Int2ByteRBTreeMap.java @@ -3,11 +3,13 @@ package speiger.src.collections.ints.maps.impl.tree; import java.util.Collections; import java.util.Map; 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; import java.util.function.IntPredicate; +import java.util.OptionalInt; import speiger.src.collections.ints.collections.IntBidirectionalIterator; import speiger.src.collections.ints.functions.IntComparator; @@ -35,6 +37,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectByteConsumer; import speiger.src.collections.bytes.functions.function.BytePredicate; +import speiger.src.collections.bytes.functions.OptionalByte; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOperator; @@ -1181,7 +1184,7 @@ public class Int2ByteRBTreeMap extends AbstractInt2ByteMap implements Int2ByteNa } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -1193,15 +1196,15 @@ public class Int2ByteRBTreeMap extends AbstractInt2ByteMap implements Int2ByteNa } state = operator.applyAsInt(state, entry.key); } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); for(Node entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.test(entry.key)) return entry.key; - return 0; + if(filter.test(entry.key)) return OptionalInt.of(entry.key); + return OptionalInt.empty(); } @Override @@ -1845,7 +1848,7 @@ public class Int2ByteRBTreeMap extends AbstractInt2ByteMap implements Int2ByteNa } @Override - public Int2ByteMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Int2ByteMap.Entry state = null; boolean empty = true; @@ -1857,19 +1860,19 @@ public class Int2ByteRBTreeMap extends AbstractInt2ByteMap implements Int2ByteNa } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Int2ByteMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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 @@ -1964,7 +1967,7 @@ public class Int2ByteRBTreeMap extends AbstractInt2ByteMap implements Int2ByteNa } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -1976,15 +1979,15 @@ public class Int2ByteRBTreeMap extends AbstractInt2ByteMap implements Int2ByteNa } state = operator.applyAsByte(state, entry.value); } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return (byte)0; + if(filter.test(entry.value)) return OptionalByte.of(entry.value); + return OptionalByte.empty(); } @Override @@ -2300,7 +2303,7 @@ public class Int2ByteRBTreeMap extends AbstractInt2ByteMap implements Int2ByteNa } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -2312,15 +2315,15 @@ public class Int2ByteRBTreeMap extends AbstractInt2ByteMap implements Int2ByteNa } state = operator.applyAsByte(state, entry.value); } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return (byte)0; + if(filter.test(entry.value)) return OptionalByte.of(entry.value); + return OptionalByte.empty(); } @Override @@ -2461,7 +2464,7 @@ public class Int2ByteRBTreeMap extends AbstractInt2ByteMap implements Int2ByteNa } @Override - public Int2ByteMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Int2ByteMap.Entry state = null; boolean empty = true; @@ -2473,19 +2476,19 @@ public class Int2ByteRBTreeMap extends AbstractInt2ByteMap implements Int2ByteNa } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Int2ByteMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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/main/java/speiger/src/collections/ints/maps/impl/tree/Int2CharAVLTreeMap.java b/src/main/java/speiger/src/collections/ints/maps/impl/tree/Int2CharAVLTreeMap.java index 1ed98d4..358f07e 100644 --- a/src/main/java/speiger/src/collections/ints/maps/impl/tree/Int2CharAVLTreeMap.java +++ b/src/main/java/speiger/src/collections/ints/maps/impl/tree/Int2CharAVLTreeMap.java @@ -3,11 +3,13 @@ package speiger.src.collections.ints.maps.impl.tree; import java.util.Collections; import java.util.Map; 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; import java.util.function.IntPredicate; +import java.util.OptionalInt; import speiger.src.collections.ints.collections.IntBidirectionalIterator; @@ -36,6 +38,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectCharConsumer; import speiger.src.collections.chars.functions.function.CharPredicate; +import speiger.src.collections.chars.functions.OptionalChar; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOperator; @@ -1128,7 +1131,7 @@ public class Int2CharAVLTreeMap extends AbstractInt2CharMap implements Int2CharN } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -1140,15 +1143,15 @@ public class Int2CharAVLTreeMap extends AbstractInt2CharMap implements Int2CharN } state = operator.applyAsInt(state, entry.key); } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); for(Node entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.test(entry.key)) return entry.key; - return 0; + if(filter.test(entry.key)) return OptionalInt.of(entry.key); + return OptionalInt.empty(); } @Override @@ -1782,7 +1785,7 @@ public class Int2CharAVLTreeMap extends AbstractInt2CharMap implements Int2CharN } @Override - public Int2CharMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Int2CharMap.Entry state = null; boolean empty = true; @@ -1794,19 +1797,19 @@ public class Int2CharAVLTreeMap extends AbstractInt2CharMap implements Int2CharN } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Int2CharMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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 @@ -1901,7 +1904,7 @@ public class Int2CharAVLTreeMap extends AbstractInt2CharMap implements Int2CharN } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -1913,15 +1916,15 @@ public class Int2CharAVLTreeMap extends AbstractInt2CharMap implements Int2CharN } state = operator.applyAsChar(state, entry.value); } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return (char)0; + if(filter.test(entry.value)) return OptionalChar.of(entry.value); + return OptionalChar.empty(); } @Override @@ -2237,7 +2240,7 @@ public class Int2CharAVLTreeMap extends AbstractInt2CharMap implements Int2CharN } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -2249,15 +2252,15 @@ public class Int2CharAVLTreeMap extends AbstractInt2CharMap implements Int2CharN } state = operator.applyAsChar(state, entry.value); } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return (char)0; + if(filter.test(entry.value)) return OptionalChar.of(entry.value); + return OptionalChar.empty(); } @Override @@ -2398,7 +2401,7 @@ public class Int2CharAVLTreeMap extends AbstractInt2CharMap implements Int2CharN } @Override - public Int2CharMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Int2CharMap.Entry state = null; boolean empty = true; @@ -2410,19 +2413,19 @@ public class Int2CharAVLTreeMap extends AbstractInt2CharMap implements Int2CharN } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Int2CharMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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/main/java/speiger/src/collections/ints/maps/impl/tree/Int2CharRBTreeMap.java b/src/main/java/speiger/src/collections/ints/maps/impl/tree/Int2CharRBTreeMap.java index d595de7..6c6e18a 100644 --- a/src/main/java/speiger/src/collections/ints/maps/impl/tree/Int2CharRBTreeMap.java +++ b/src/main/java/speiger/src/collections/ints/maps/impl/tree/Int2CharRBTreeMap.java @@ -3,11 +3,13 @@ package speiger.src.collections.ints.maps.impl.tree; import java.util.Collections; import java.util.Map; 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; import java.util.function.IntPredicate; +import java.util.OptionalInt; import speiger.src.collections.ints.collections.IntBidirectionalIterator; import speiger.src.collections.ints.functions.IntComparator; @@ -35,6 +37,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectCharConsumer; import speiger.src.collections.chars.functions.function.CharPredicate; +import speiger.src.collections.chars.functions.OptionalChar; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOperator; @@ -1181,7 +1184,7 @@ public class Int2CharRBTreeMap extends AbstractInt2CharMap implements Int2CharNa } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -1193,15 +1196,15 @@ public class Int2CharRBTreeMap extends AbstractInt2CharMap implements Int2CharNa } state = operator.applyAsInt(state, entry.key); } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); for(Node entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.test(entry.key)) return entry.key; - return 0; + if(filter.test(entry.key)) return OptionalInt.of(entry.key); + return OptionalInt.empty(); } @Override @@ -1845,7 +1848,7 @@ public class Int2CharRBTreeMap extends AbstractInt2CharMap implements Int2CharNa } @Override - public Int2CharMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Int2CharMap.Entry state = null; boolean empty = true; @@ -1857,19 +1860,19 @@ public class Int2CharRBTreeMap extends AbstractInt2CharMap implements Int2CharNa } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Int2CharMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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 @@ -1964,7 +1967,7 @@ public class Int2CharRBTreeMap extends AbstractInt2CharMap implements Int2CharNa } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -1976,15 +1979,15 @@ public class Int2CharRBTreeMap extends AbstractInt2CharMap implements Int2CharNa } state = operator.applyAsChar(state, entry.value); } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return (char)0; + if(filter.test(entry.value)) return OptionalChar.of(entry.value); + return OptionalChar.empty(); } @Override @@ -2300,7 +2303,7 @@ public class Int2CharRBTreeMap extends AbstractInt2CharMap implements Int2CharNa } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -2312,15 +2315,15 @@ public class Int2CharRBTreeMap extends AbstractInt2CharMap implements Int2CharNa } state = operator.applyAsChar(state, entry.value); } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return (char)0; + if(filter.test(entry.value)) return OptionalChar.of(entry.value); + return OptionalChar.empty(); } @Override @@ -2461,7 +2464,7 @@ public class Int2CharRBTreeMap extends AbstractInt2CharMap implements Int2CharNa } @Override - public Int2CharMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Int2CharMap.Entry state = null; boolean empty = true; @@ -2473,19 +2476,19 @@ public class Int2CharRBTreeMap extends AbstractInt2CharMap implements Int2CharNa } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Int2CharMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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/main/java/speiger/src/collections/ints/maps/impl/tree/Int2DoubleAVLTreeMap.java b/src/main/java/speiger/src/collections/ints/maps/impl/tree/Int2DoubleAVLTreeMap.java index d2724dc..bff94c9 100644 --- a/src/main/java/speiger/src/collections/ints/maps/impl/tree/Int2DoubleAVLTreeMap.java +++ b/src/main/java/speiger/src/collections/ints/maps/impl/tree/Int2DoubleAVLTreeMap.java @@ -3,12 +3,15 @@ package speiger.src.collections.ints.maps.impl.tree; import java.util.Collections; import java.util.Map; 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; import java.util.function.IntPredicate; +import java.util.OptionalInt; import java.util.function.DoublePredicate; +import java.util.OptionalDouble; import speiger.src.collections.ints.collections.IntBidirectionalIterator; @@ -1128,7 +1131,7 @@ public class Int2DoubleAVLTreeMap extends AbstractInt2DoubleMap implements Int2D } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -1140,15 +1143,15 @@ public class Int2DoubleAVLTreeMap extends AbstractInt2DoubleMap implements Int2D } state = operator.applyAsInt(state, entry.key); } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); for(Node entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.test(entry.key)) return entry.key; - return 0; + if(filter.test(entry.key)) return OptionalInt.of(entry.key); + return OptionalInt.empty(); } @Override @@ -1782,7 +1785,7 @@ public class Int2DoubleAVLTreeMap extends AbstractInt2DoubleMap implements Int2D } @Override - public Int2DoubleMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Int2DoubleMap.Entry state = null; boolean empty = true; @@ -1794,19 +1797,19 @@ public class Int2DoubleAVLTreeMap extends AbstractInt2DoubleMap implements Int2D } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Int2DoubleMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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 @@ -1901,7 +1904,7 @@ public class Int2DoubleAVLTreeMap extends AbstractInt2DoubleMap implements Int2D } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -1913,15 +1916,15 @@ public class Int2DoubleAVLTreeMap extends AbstractInt2DoubleMap implements Int2D } state = operator.applyAsDouble(state, entry.value); } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return 0D; + if(filter.test(entry.value)) return OptionalDouble.of(entry.value); + return OptionalDouble.empty(); } @Override @@ -2237,7 +2240,7 @@ public class Int2DoubleAVLTreeMap extends AbstractInt2DoubleMap implements Int2D } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -2249,15 +2252,15 @@ public class Int2DoubleAVLTreeMap extends AbstractInt2DoubleMap implements Int2D } state = operator.applyAsDouble(state, entry.value); } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return 0D; + if(filter.test(entry.value)) return OptionalDouble.of(entry.value); + return OptionalDouble.empty(); } @Override @@ -2398,7 +2401,7 @@ public class Int2DoubleAVLTreeMap extends AbstractInt2DoubleMap implements Int2D } @Override - public Int2DoubleMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Int2DoubleMap.Entry state = null; boolean empty = true; @@ -2410,19 +2413,19 @@ public class Int2DoubleAVLTreeMap extends AbstractInt2DoubleMap implements Int2D } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Int2DoubleMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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/main/java/speiger/src/collections/ints/maps/impl/tree/Int2DoubleRBTreeMap.java b/src/main/java/speiger/src/collections/ints/maps/impl/tree/Int2DoubleRBTreeMap.java index 4fdbc56..80710cd 100644 --- a/src/main/java/speiger/src/collections/ints/maps/impl/tree/Int2DoubleRBTreeMap.java +++ b/src/main/java/speiger/src/collections/ints/maps/impl/tree/Int2DoubleRBTreeMap.java @@ -3,12 +3,15 @@ package speiger.src.collections.ints.maps.impl.tree; import java.util.Collections; import java.util.Map; 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; import java.util.function.IntPredicate; +import java.util.OptionalInt; import java.util.function.DoublePredicate; +import java.util.OptionalDouble; import speiger.src.collections.ints.collections.IntBidirectionalIterator; import speiger.src.collections.ints.functions.IntComparator; @@ -1181,7 +1184,7 @@ public class Int2DoubleRBTreeMap extends AbstractInt2DoubleMap implements Int2Do } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -1193,15 +1196,15 @@ public class Int2DoubleRBTreeMap extends AbstractInt2DoubleMap implements Int2Do } state = operator.applyAsInt(state, entry.key); } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); for(Node entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.test(entry.key)) return entry.key; - return 0; + if(filter.test(entry.key)) return OptionalInt.of(entry.key); + return OptionalInt.empty(); } @Override @@ -1845,7 +1848,7 @@ public class Int2DoubleRBTreeMap extends AbstractInt2DoubleMap implements Int2Do } @Override - public Int2DoubleMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Int2DoubleMap.Entry state = null; boolean empty = true; @@ -1857,19 +1860,19 @@ public class Int2DoubleRBTreeMap extends AbstractInt2DoubleMap implements Int2Do } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Int2DoubleMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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 @@ -1964,7 +1967,7 @@ public class Int2DoubleRBTreeMap extends AbstractInt2DoubleMap implements Int2Do } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -1976,15 +1979,15 @@ public class Int2DoubleRBTreeMap extends AbstractInt2DoubleMap implements Int2Do } state = operator.applyAsDouble(state, entry.value); } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return 0D; + if(filter.test(entry.value)) return OptionalDouble.of(entry.value); + return OptionalDouble.empty(); } @Override @@ -2300,7 +2303,7 @@ public class Int2DoubleRBTreeMap extends AbstractInt2DoubleMap implements Int2Do } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -2312,15 +2315,15 @@ public class Int2DoubleRBTreeMap extends AbstractInt2DoubleMap implements Int2Do } state = operator.applyAsDouble(state, entry.value); } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return 0D; + if(filter.test(entry.value)) return OptionalDouble.of(entry.value); + return OptionalDouble.empty(); } @Override @@ -2461,7 +2464,7 @@ public class Int2DoubleRBTreeMap extends AbstractInt2DoubleMap implements Int2Do } @Override - public Int2DoubleMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Int2DoubleMap.Entry state = null; boolean empty = true; @@ -2473,19 +2476,19 @@ public class Int2DoubleRBTreeMap extends AbstractInt2DoubleMap implements Int2Do } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Int2DoubleMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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/main/java/speiger/src/collections/ints/maps/impl/tree/Int2FloatAVLTreeMap.java b/src/main/java/speiger/src/collections/ints/maps/impl/tree/Int2FloatAVLTreeMap.java index eb228e3..2ab7949 100644 --- a/src/main/java/speiger/src/collections/ints/maps/impl/tree/Int2FloatAVLTreeMap.java +++ b/src/main/java/speiger/src/collections/ints/maps/impl/tree/Int2FloatAVLTreeMap.java @@ -3,11 +3,13 @@ package speiger.src.collections.ints.maps.impl.tree; import java.util.Collections; import java.util.Map; 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; import java.util.function.IntPredicate; +import java.util.OptionalInt; import speiger.src.collections.ints.collections.IntBidirectionalIterator; @@ -36,6 +38,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectFloatConsumer; import speiger.src.collections.floats.functions.function.FloatPredicate; +import speiger.src.collections.floats.functions.OptionalFloat; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOperator; @@ -1128,7 +1131,7 @@ public class Int2FloatAVLTreeMap extends AbstractInt2FloatMap implements Int2Flo } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -1140,15 +1143,15 @@ public class Int2FloatAVLTreeMap extends AbstractInt2FloatMap implements Int2Flo } state = operator.applyAsInt(state, entry.key); } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); for(Node entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.test(entry.key)) return entry.key; - return 0; + if(filter.test(entry.key)) return OptionalInt.of(entry.key); + return OptionalInt.empty(); } @Override @@ -1782,7 +1785,7 @@ public class Int2FloatAVLTreeMap extends AbstractInt2FloatMap implements Int2Flo } @Override - public Int2FloatMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Int2FloatMap.Entry state = null; boolean empty = true; @@ -1794,19 +1797,19 @@ public class Int2FloatAVLTreeMap extends AbstractInt2FloatMap implements Int2Flo } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Int2FloatMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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 @@ -1901,7 +1904,7 @@ public class Int2FloatAVLTreeMap extends AbstractInt2FloatMap implements Int2Flo } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -1913,15 +1916,15 @@ public class Int2FloatAVLTreeMap extends AbstractInt2FloatMap implements Int2Flo } state = operator.applyAsFloat(state, entry.value); } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return 0F; + if(filter.test(entry.value)) return OptionalFloat.of(entry.value); + return OptionalFloat.empty(); } @Override @@ -2237,7 +2240,7 @@ public class Int2FloatAVLTreeMap extends AbstractInt2FloatMap implements Int2Flo } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -2249,15 +2252,15 @@ public class Int2FloatAVLTreeMap extends AbstractInt2FloatMap implements Int2Flo } state = operator.applyAsFloat(state, entry.value); } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return 0F; + if(filter.test(entry.value)) return OptionalFloat.of(entry.value); + return OptionalFloat.empty(); } @Override @@ -2398,7 +2401,7 @@ public class Int2FloatAVLTreeMap extends AbstractInt2FloatMap implements Int2Flo } @Override - public Int2FloatMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Int2FloatMap.Entry state = null; boolean empty = true; @@ -2410,19 +2413,19 @@ public class Int2FloatAVLTreeMap extends AbstractInt2FloatMap implements Int2Flo } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Int2FloatMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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/main/java/speiger/src/collections/ints/maps/impl/tree/Int2FloatRBTreeMap.java b/src/main/java/speiger/src/collections/ints/maps/impl/tree/Int2FloatRBTreeMap.java index 084f78c..6544dbd 100644 --- a/src/main/java/speiger/src/collections/ints/maps/impl/tree/Int2FloatRBTreeMap.java +++ b/src/main/java/speiger/src/collections/ints/maps/impl/tree/Int2FloatRBTreeMap.java @@ -3,11 +3,13 @@ package speiger.src.collections.ints.maps.impl.tree; import java.util.Collections; import java.util.Map; 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; import java.util.function.IntPredicate; +import java.util.OptionalInt; import speiger.src.collections.ints.collections.IntBidirectionalIterator; import speiger.src.collections.ints.functions.IntComparator; @@ -35,6 +37,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectFloatConsumer; import speiger.src.collections.floats.functions.function.FloatPredicate; +import speiger.src.collections.floats.functions.OptionalFloat; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOperator; @@ -1181,7 +1184,7 @@ public class Int2FloatRBTreeMap extends AbstractInt2FloatMap implements Int2Floa } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -1193,15 +1196,15 @@ public class Int2FloatRBTreeMap extends AbstractInt2FloatMap implements Int2Floa } state = operator.applyAsInt(state, entry.key); } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); for(Node entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.test(entry.key)) return entry.key; - return 0; + if(filter.test(entry.key)) return OptionalInt.of(entry.key); + return OptionalInt.empty(); } @Override @@ -1845,7 +1848,7 @@ public class Int2FloatRBTreeMap extends AbstractInt2FloatMap implements Int2Floa } @Override - public Int2FloatMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Int2FloatMap.Entry state = null; boolean empty = true; @@ -1857,19 +1860,19 @@ public class Int2FloatRBTreeMap extends AbstractInt2FloatMap implements Int2Floa } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Int2FloatMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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 @@ -1964,7 +1967,7 @@ public class Int2FloatRBTreeMap extends AbstractInt2FloatMap implements Int2Floa } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -1976,15 +1979,15 @@ public class Int2FloatRBTreeMap extends AbstractInt2FloatMap implements Int2Floa } state = operator.applyAsFloat(state, entry.value); } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return 0F; + if(filter.test(entry.value)) return OptionalFloat.of(entry.value); + return OptionalFloat.empty(); } @Override @@ -2300,7 +2303,7 @@ public class Int2FloatRBTreeMap extends AbstractInt2FloatMap implements Int2Floa } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -2312,15 +2315,15 @@ public class Int2FloatRBTreeMap extends AbstractInt2FloatMap implements Int2Floa } state = operator.applyAsFloat(state, entry.value); } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return 0F; + if(filter.test(entry.value)) return OptionalFloat.of(entry.value); + return OptionalFloat.empty(); } @Override @@ -2461,7 +2464,7 @@ public class Int2FloatRBTreeMap extends AbstractInt2FloatMap implements Int2Floa } @Override - public Int2FloatMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Int2FloatMap.Entry state = null; boolean empty = true; @@ -2473,19 +2476,19 @@ public class Int2FloatRBTreeMap extends AbstractInt2FloatMap implements Int2Floa } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Int2FloatMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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/main/java/speiger/src/collections/ints/maps/impl/tree/Int2IntAVLTreeMap.java b/src/main/java/speiger/src/collections/ints/maps/impl/tree/Int2IntAVLTreeMap.java index ebcf788..c1f4637 100644 --- a/src/main/java/speiger/src/collections/ints/maps/impl/tree/Int2IntAVLTreeMap.java +++ b/src/main/java/speiger/src/collections/ints/maps/impl/tree/Int2IntAVLTreeMap.java @@ -3,11 +3,13 @@ package speiger.src.collections.ints.maps.impl.tree; import java.util.Collections; import java.util.Map; 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; import java.util.function.IntPredicate; +import java.util.OptionalInt; import speiger.src.collections.ints.collections.IntBidirectionalIterator; @@ -1120,7 +1122,7 @@ public class Int2IntAVLTreeMap extends AbstractInt2IntMap implements Int2IntNavi } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -1132,15 +1134,15 @@ public class Int2IntAVLTreeMap extends AbstractInt2IntMap implements Int2IntNavi } state = operator.applyAsInt(state, entry.key); } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); for(Node entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.test(entry.key)) return entry.key; - return 0; + if(filter.test(entry.key)) return OptionalInt.of(entry.key); + return OptionalInt.empty(); } @Override @@ -1774,7 +1776,7 @@ public class Int2IntAVLTreeMap extends AbstractInt2IntMap implements Int2IntNavi } @Override - public Int2IntMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Int2IntMap.Entry state = null; boolean empty = true; @@ -1786,19 +1788,19 @@ public class Int2IntAVLTreeMap extends AbstractInt2IntMap implements Int2IntNavi } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Int2IntMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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 @@ -1893,7 +1895,7 @@ public class Int2IntAVLTreeMap extends AbstractInt2IntMap implements Int2IntNavi } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -1905,15 +1907,15 @@ public class Int2IntAVLTreeMap extends AbstractInt2IntMap implements Int2IntNavi } state = operator.applyAsInt(state, entry.value); } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return 0; + if(filter.test(entry.value)) return OptionalInt.of(entry.value); + return OptionalInt.empty(); } @Override @@ -2229,7 +2231,7 @@ public class Int2IntAVLTreeMap extends AbstractInt2IntMap implements Int2IntNavi } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -2241,15 +2243,15 @@ public class Int2IntAVLTreeMap extends AbstractInt2IntMap implements Int2IntNavi } state = operator.applyAsInt(state, entry.value); } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return 0; + if(filter.test(entry.value)) return OptionalInt.of(entry.value); + return OptionalInt.empty(); } @Override @@ -2390,7 +2392,7 @@ public class Int2IntAVLTreeMap extends AbstractInt2IntMap implements Int2IntNavi } @Override - public Int2IntMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Int2IntMap.Entry state = null; boolean empty = true; @@ -2402,19 +2404,19 @@ public class Int2IntAVLTreeMap extends AbstractInt2IntMap implements Int2IntNavi } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Int2IntMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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/main/java/speiger/src/collections/ints/maps/impl/tree/Int2IntRBTreeMap.java b/src/main/java/speiger/src/collections/ints/maps/impl/tree/Int2IntRBTreeMap.java index 9dc7932..815d99f 100644 --- a/src/main/java/speiger/src/collections/ints/maps/impl/tree/Int2IntRBTreeMap.java +++ b/src/main/java/speiger/src/collections/ints/maps/impl/tree/Int2IntRBTreeMap.java @@ -3,11 +3,13 @@ package speiger.src.collections.ints.maps.impl.tree; import java.util.Collections; import java.util.Map; 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; import java.util.function.IntPredicate; +import java.util.OptionalInt; import speiger.src.collections.ints.collections.IntBidirectionalIterator; import speiger.src.collections.ints.functions.IntComparator; @@ -1173,7 +1175,7 @@ public class Int2IntRBTreeMap extends AbstractInt2IntMap implements Int2IntNavig } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -1185,15 +1187,15 @@ public class Int2IntRBTreeMap extends AbstractInt2IntMap implements Int2IntNavig } state = operator.applyAsInt(state, entry.key); } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); for(Node entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.test(entry.key)) return entry.key; - return 0; + if(filter.test(entry.key)) return OptionalInt.of(entry.key); + return OptionalInt.empty(); } @Override @@ -1837,7 +1839,7 @@ public class Int2IntRBTreeMap extends AbstractInt2IntMap implements Int2IntNavig } @Override - public Int2IntMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Int2IntMap.Entry state = null; boolean empty = true; @@ -1849,19 +1851,19 @@ public class Int2IntRBTreeMap extends AbstractInt2IntMap implements Int2IntNavig } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Int2IntMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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 @@ -1956,7 +1958,7 @@ public class Int2IntRBTreeMap extends AbstractInt2IntMap implements Int2IntNavig } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -1968,15 +1970,15 @@ public class Int2IntRBTreeMap extends AbstractInt2IntMap implements Int2IntNavig } state = operator.applyAsInt(state, entry.value); } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return 0; + if(filter.test(entry.value)) return OptionalInt.of(entry.value); + return OptionalInt.empty(); } @Override @@ -2292,7 +2294,7 @@ public class Int2IntRBTreeMap extends AbstractInt2IntMap implements Int2IntNavig } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -2304,15 +2306,15 @@ public class Int2IntRBTreeMap extends AbstractInt2IntMap implements Int2IntNavig } state = operator.applyAsInt(state, entry.value); } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return 0; + if(filter.test(entry.value)) return OptionalInt.of(entry.value); + return OptionalInt.empty(); } @Override @@ -2453,7 +2455,7 @@ public class Int2IntRBTreeMap extends AbstractInt2IntMap implements Int2IntNavig } @Override - public Int2IntMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Int2IntMap.Entry state = null; boolean empty = true; @@ -2465,19 +2467,19 @@ public class Int2IntRBTreeMap extends AbstractInt2IntMap implements Int2IntNavig } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Int2IntMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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/main/java/speiger/src/collections/ints/maps/impl/tree/Int2LongAVLTreeMap.java b/src/main/java/speiger/src/collections/ints/maps/impl/tree/Int2LongAVLTreeMap.java index 48bbcfe..2dc95d6 100644 --- a/src/main/java/speiger/src/collections/ints/maps/impl/tree/Int2LongAVLTreeMap.java +++ b/src/main/java/speiger/src/collections/ints/maps/impl/tree/Int2LongAVLTreeMap.java @@ -3,12 +3,15 @@ package speiger.src.collections.ints.maps.impl.tree; import java.util.Collections; import java.util.Map; 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; import java.util.function.IntPredicate; +import java.util.OptionalInt; import java.util.function.LongPredicate; +import java.util.OptionalLong; import speiger.src.collections.ints.collections.IntBidirectionalIterator; @@ -1128,7 +1131,7 @@ public class Int2LongAVLTreeMap extends AbstractInt2LongMap implements Int2LongN } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -1140,15 +1143,15 @@ public class Int2LongAVLTreeMap extends AbstractInt2LongMap implements Int2LongN } state = operator.applyAsInt(state, entry.key); } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); for(Node entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.test(entry.key)) return entry.key; - return 0; + if(filter.test(entry.key)) return OptionalInt.of(entry.key); + return OptionalInt.empty(); } @Override @@ -1782,7 +1785,7 @@ public class Int2LongAVLTreeMap extends AbstractInt2LongMap implements Int2LongN } @Override - public Int2LongMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Int2LongMap.Entry state = null; boolean empty = true; @@ -1794,19 +1797,19 @@ public class Int2LongAVLTreeMap extends AbstractInt2LongMap implements Int2LongN } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Int2LongMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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 @@ -1901,7 +1904,7 @@ public class Int2LongAVLTreeMap extends AbstractInt2LongMap implements Int2LongN } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -1913,15 +1916,15 @@ public class Int2LongAVLTreeMap extends AbstractInt2LongMap implements Int2LongN } state = operator.applyAsLong(state, entry.value); } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return 0L; + if(filter.test(entry.value)) return OptionalLong.of(entry.value); + return OptionalLong.empty(); } @Override @@ -2237,7 +2240,7 @@ public class Int2LongAVLTreeMap extends AbstractInt2LongMap implements Int2LongN } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -2249,15 +2252,15 @@ public class Int2LongAVLTreeMap extends AbstractInt2LongMap implements Int2LongN } state = operator.applyAsLong(state, entry.value); } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return 0L; + if(filter.test(entry.value)) return OptionalLong.of(entry.value); + return OptionalLong.empty(); } @Override @@ -2398,7 +2401,7 @@ public class Int2LongAVLTreeMap extends AbstractInt2LongMap implements Int2LongN } @Override - public Int2LongMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Int2LongMap.Entry state = null; boolean empty = true; @@ -2410,19 +2413,19 @@ public class Int2LongAVLTreeMap extends AbstractInt2LongMap implements Int2LongN } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Int2LongMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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/main/java/speiger/src/collections/ints/maps/impl/tree/Int2LongRBTreeMap.java b/src/main/java/speiger/src/collections/ints/maps/impl/tree/Int2LongRBTreeMap.java index 1fb48c8..00dfe29 100644 --- a/src/main/java/speiger/src/collections/ints/maps/impl/tree/Int2LongRBTreeMap.java +++ b/src/main/java/speiger/src/collections/ints/maps/impl/tree/Int2LongRBTreeMap.java @@ -3,12 +3,15 @@ package speiger.src.collections.ints.maps.impl.tree; import java.util.Collections; import java.util.Map; 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; import java.util.function.IntPredicate; +import java.util.OptionalInt; import java.util.function.LongPredicate; +import java.util.OptionalLong; import speiger.src.collections.ints.collections.IntBidirectionalIterator; import speiger.src.collections.ints.functions.IntComparator; @@ -1181,7 +1184,7 @@ public class Int2LongRBTreeMap extends AbstractInt2LongMap implements Int2LongNa } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -1193,15 +1196,15 @@ public class Int2LongRBTreeMap extends AbstractInt2LongMap implements Int2LongNa } state = operator.applyAsInt(state, entry.key); } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); for(Node entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.test(entry.key)) return entry.key; - return 0; + if(filter.test(entry.key)) return OptionalInt.of(entry.key); + return OptionalInt.empty(); } @Override @@ -1845,7 +1848,7 @@ public class Int2LongRBTreeMap extends AbstractInt2LongMap implements Int2LongNa } @Override - public Int2LongMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Int2LongMap.Entry state = null; boolean empty = true; @@ -1857,19 +1860,19 @@ public class Int2LongRBTreeMap extends AbstractInt2LongMap implements Int2LongNa } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Int2LongMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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 @@ -1964,7 +1967,7 @@ public class Int2LongRBTreeMap extends AbstractInt2LongMap implements Int2LongNa } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -1976,15 +1979,15 @@ public class Int2LongRBTreeMap extends AbstractInt2LongMap implements Int2LongNa } state = operator.applyAsLong(state, entry.value); } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return 0L; + if(filter.test(entry.value)) return OptionalLong.of(entry.value); + return OptionalLong.empty(); } @Override @@ -2300,7 +2303,7 @@ public class Int2LongRBTreeMap extends AbstractInt2LongMap implements Int2LongNa } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -2312,15 +2315,15 @@ public class Int2LongRBTreeMap extends AbstractInt2LongMap implements Int2LongNa } state = operator.applyAsLong(state, entry.value); } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return 0L; + if(filter.test(entry.value)) return OptionalLong.of(entry.value); + return OptionalLong.empty(); } @Override @@ -2461,7 +2464,7 @@ public class Int2LongRBTreeMap extends AbstractInt2LongMap implements Int2LongNa } @Override - public Int2LongMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Int2LongMap.Entry state = null; boolean empty = true; @@ -2473,19 +2476,19 @@ public class Int2LongRBTreeMap extends AbstractInt2LongMap implements Int2LongNa } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Int2LongMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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/main/java/speiger/src/collections/ints/maps/impl/tree/Int2ObjectAVLTreeMap.java b/src/main/java/speiger/src/collections/ints/maps/impl/tree/Int2ObjectAVLTreeMap.java index 2d59413..50aaf7a 100644 --- a/src/main/java/speiger/src/collections/ints/maps/impl/tree/Int2ObjectAVLTreeMap.java +++ b/src/main/java/speiger/src/collections/ints/maps/impl/tree/Int2ObjectAVLTreeMap.java @@ -3,11 +3,13 @@ package speiger.src.collections.ints.maps.impl.tree; import java.util.Collections; import java.util.Map; 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; import java.util.function.IntPredicate; +import java.util.OptionalInt; import speiger.src.collections.ints.collections.IntBidirectionalIterator; @@ -1016,7 +1018,7 @@ public class Int2ObjectAVLTreeMap extends AbstractInt2ObjectMap implements } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -1028,15 +1030,15 @@ public class Int2ObjectAVLTreeMap extends AbstractInt2ObjectMap implements } state = operator.applyAsInt(state, entry.key); } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); for(Node entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.test(entry.key)) return entry.key; - return 0; + if(filter.test(entry.key)) return OptionalInt.of(entry.key); + return OptionalInt.empty(); } @Override @@ -1663,7 +1665,7 @@ public class Int2ObjectAVLTreeMap extends AbstractInt2ObjectMap implements } @Override - public Int2ObjectMap.Entry reduce(ObjectObjectUnaryOperator, Int2ObjectMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Int2ObjectMap.Entry> operator) { Objects.requireNonNull(operator); Int2ObjectMap.Entry state = null; boolean empty = true; @@ -1675,19 +1677,19 @@ public class Int2ObjectAVLTreeMap extends AbstractInt2ObjectMap implements } state = operator.apply(state, new BasicEntry<>(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Int2ObjectMap.Entry findFirst(Predicate> filter) { + public Optional> findFirst(Predicate> filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry<>(); for(Node 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 @@ -1782,7 +1784,7 @@ public class Int2ObjectAVLTreeMap extends AbstractInt2ObjectMap implements } @Override - public V reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); V state = null; boolean empty = true; @@ -1794,15 +1796,15 @@ public class Int2ObjectAVLTreeMap extends AbstractInt2ObjectMap implements } state = operator.apply(state, entry.value); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public V findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return null; + if(filter.test(entry.value)) return Optional.ofNullable(entry.value); + return Optional.empty(); } @Override @@ -2118,7 +2120,7 @@ public class Int2ObjectAVLTreeMap extends AbstractInt2ObjectMap implements } @Override - public V reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); V state = null; boolean empty = true; @@ -2130,15 +2132,15 @@ public class Int2ObjectAVLTreeMap extends AbstractInt2ObjectMap implements } state = operator.apply(state, entry.value); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public V findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return null; + if(filter.test(entry.value)) return Optional.ofNullable(entry.value); + return Optional.empty(); } @Override @@ -2279,7 +2281,7 @@ public class Int2ObjectAVLTreeMap extends AbstractInt2ObjectMap implements } @Override - public Int2ObjectMap.Entry reduce(ObjectObjectUnaryOperator, Int2ObjectMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Int2ObjectMap.Entry> operator) { Objects.requireNonNull(operator); Int2ObjectMap.Entry state = null; boolean empty = true; @@ -2291,19 +2293,19 @@ public class Int2ObjectAVLTreeMap extends AbstractInt2ObjectMap implements } state = operator.apply(state, new BasicEntry<>(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Int2ObjectMap.Entry findFirst(Predicate> filter) { + public Optional> findFirst(Predicate> filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry<>(); for(Node 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/main/java/speiger/src/collections/ints/maps/impl/tree/Int2ObjectRBTreeMap.java b/src/main/java/speiger/src/collections/ints/maps/impl/tree/Int2ObjectRBTreeMap.java index a301dde..e2d5348 100644 --- a/src/main/java/speiger/src/collections/ints/maps/impl/tree/Int2ObjectRBTreeMap.java +++ b/src/main/java/speiger/src/collections/ints/maps/impl/tree/Int2ObjectRBTreeMap.java @@ -3,11 +3,13 @@ package speiger.src.collections.ints.maps.impl.tree; import java.util.Collections; import java.util.Map; 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; import java.util.function.IntPredicate; +import java.util.OptionalInt; import speiger.src.collections.ints.collections.IntBidirectionalIterator; import speiger.src.collections.ints.functions.IntComparator; @@ -1069,7 +1071,7 @@ public class Int2ObjectRBTreeMap extends AbstractInt2ObjectMap implements } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -1081,15 +1083,15 @@ public class Int2ObjectRBTreeMap extends AbstractInt2ObjectMap implements } state = operator.applyAsInt(state, entry.key); } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); for(Node entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.test(entry.key)) return entry.key; - return 0; + if(filter.test(entry.key)) return OptionalInt.of(entry.key); + return OptionalInt.empty(); } @Override @@ -1726,7 +1728,7 @@ public class Int2ObjectRBTreeMap extends AbstractInt2ObjectMap implements } @Override - public Int2ObjectMap.Entry reduce(ObjectObjectUnaryOperator, Int2ObjectMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Int2ObjectMap.Entry> operator) { Objects.requireNonNull(operator); Int2ObjectMap.Entry state = null; boolean empty = true; @@ -1738,19 +1740,19 @@ public class Int2ObjectRBTreeMap extends AbstractInt2ObjectMap implements } state = operator.apply(state, new BasicEntry<>(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Int2ObjectMap.Entry findFirst(Predicate> filter) { + public Optional> findFirst(Predicate> filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry<>(); for(Node 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 @@ -1845,7 +1847,7 @@ public class Int2ObjectRBTreeMap extends AbstractInt2ObjectMap implements } @Override - public V reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); V state = null; boolean empty = true; @@ -1857,15 +1859,15 @@ public class Int2ObjectRBTreeMap extends AbstractInt2ObjectMap implements } state = operator.apply(state, entry.value); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public V findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return null; + if(filter.test(entry.value)) return Optional.ofNullable(entry.value); + return Optional.empty(); } @Override @@ -2181,7 +2183,7 @@ public class Int2ObjectRBTreeMap extends AbstractInt2ObjectMap implements } @Override - public V reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); V state = null; boolean empty = true; @@ -2193,15 +2195,15 @@ public class Int2ObjectRBTreeMap extends AbstractInt2ObjectMap implements } state = operator.apply(state, entry.value); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public V findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return null; + if(filter.test(entry.value)) return Optional.ofNullable(entry.value); + return Optional.empty(); } @Override @@ -2342,7 +2344,7 @@ public class Int2ObjectRBTreeMap extends AbstractInt2ObjectMap implements } @Override - public Int2ObjectMap.Entry reduce(ObjectObjectUnaryOperator, Int2ObjectMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Int2ObjectMap.Entry> operator) { Objects.requireNonNull(operator); Int2ObjectMap.Entry state = null; boolean empty = true; @@ -2354,19 +2356,19 @@ public class Int2ObjectRBTreeMap extends AbstractInt2ObjectMap implements } state = operator.apply(state, new BasicEntry<>(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Int2ObjectMap.Entry findFirst(Predicate> filter) { + public Optional> findFirst(Predicate> filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry<>(); for(Node 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/main/java/speiger/src/collections/ints/maps/impl/tree/Int2ShortAVLTreeMap.java b/src/main/java/speiger/src/collections/ints/maps/impl/tree/Int2ShortAVLTreeMap.java index ec89575..5d4ecbc 100644 --- a/src/main/java/speiger/src/collections/ints/maps/impl/tree/Int2ShortAVLTreeMap.java +++ b/src/main/java/speiger/src/collections/ints/maps/impl/tree/Int2ShortAVLTreeMap.java @@ -3,11 +3,13 @@ package speiger.src.collections.ints.maps.impl.tree; import java.util.Collections; import java.util.Map; 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; import java.util.function.IntPredicate; +import java.util.OptionalInt; import speiger.src.collections.ints.collections.IntBidirectionalIterator; @@ -36,6 +38,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectShortConsumer; import speiger.src.collections.shorts.functions.function.ShortPredicate; +import speiger.src.collections.shorts.functions.OptionalShort; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOperator; @@ -1128,7 +1131,7 @@ public class Int2ShortAVLTreeMap extends AbstractInt2ShortMap implements Int2Sho } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -1140,15 +1143,15 @@ public class Int2ShortAVLTreeMap extends AbstractInt2ShortMap implements Int2Sho } state = operator.applyAsInt(state, entry.key); } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); for(Node entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.test(entry.key)) return entry.key; - return 0; + if(filter.test(entry.key)) return OptionalInt.of(entry.key); + return OptionalInt.empty(); } @Override @@ -1782,7 +1785,7 @@ public class Int2ShortAVLTreeMap extends AbstractInt2ShortMap implements Int2Sho } @Override - public Int2ShortMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Int2ShortMap.Entry state = null; boolean empty = true; @@ -1794,19 +1797,19 @@ public class Int2ShortAVLTreeMap extends AbstractInt2ShortMap implements Int2Sho } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Int2ShortMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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 @@ -1901,7 +1904,7 @@ public class Int2ShortAVLTreeMap extends AbstractInt2ShortMap implements Int2Sho } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -1913,15 +1916,15 @@ public class Int2ShortAVLTreeMap extends AbstractInt2ShortMap implements Int2Sho } state = operator.applyAsShort(state, entry.value); } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return (short)0; + if(filter.test(entry.value)) return OptionalShort.of(entry.value); + return OptionalShort.empty(); } @Override @@ -2237,7 +2240,7 @@ public class Int2ShortAVLTreeMap extends AbstractInt2ShortMap implements Int2Sho } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -2249,15 +2252,15 @@ public class Int2ShortAVLTreeMap extends AbstractInt2ShortMap implements Int2Sho } state = operator.applyAsShort(state, entry.value); } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return (short)0; + if(filter.test(entry.value)) return OptionalShort.of(entry.value); + return OptionalShort.empty(); } @Override @@ -2398,7 +2401,7 @@ public class Int2ShortAVLTreeMap extends AbstractInt2ShortMap implements Int2Sho } @Override - public Int2ShortMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Int2ShortMap.Entry state = null; boolean empty = true; @@ -2410,19 +2413,19 @@ public class Int2ShortAVLTreeMap extends AbstractInt2ShortMap implements Int2Sho } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Int2ShortMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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/main/java/speiger/src/collections/ints/maps/impl/tree/Int2ShortRBTreeMap.java b/src/main/java/speiger/src/collections/ints/maps/impl/tree/Int2ShortRBTreeMap.java index aba819c..ead31ea 100644 --- a/src/main/java/speiger/src/collections/ints/maps/impl/tree/Int2ShortRBTreeMap.java +++ b/src/main/java/speiger/src/collections/ints/maps/impl/tree/Int2ShortRBTreeMap.java @@ -3,11 +3,13 @@ package speiger.src.collections.ints.maps.impl.tree; import java.util.Collections; import java.util.Map; 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; import java.util.function.IntPredicate; +import java.util.OptionalInt; import speiger.src.collections.ints.collections.IntBidirectionalIterator; import speiger.src.collections.ints.functions.IntComparator; @@ -35,6 +37,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectShortConsumer; import speiger.src.collections.shorts.functions.function.ShortPredicate; +import speiger.src.collections.shorts.functions.OptionalShort; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOperator; @@ -1181,7 +1184,7 @@ public class Int2ShortRBTreeMap extends AbstractInt2ShortMap implements Int2Shor } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -1193,15 +1196,15 @@ public class Int2ShortRBTreeMap extends AbstractInt2ShortMap implements Int2Shor } state = operator.applyAsInt(state, entry.key); } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); for(Node entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.test(entry.key)) return entry.key; - return 0; + if(filter.test(entry.key)) return OptionalInt.of(entry.key); + return OptionalInt.empty(); } @Override @@ -1845,7 +1848,7 @@ public class Int2ShortRBTreeMap extends AbstractInt2ShortMap implements Int2Shor } @Override - public Int2ShortMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Int2ShortMap.Entry state = null; boolean empty = true; @@ -1857,19 +1860,19 @@ public class Int2ShortRBTreeMap extends AbstractInt2ShortMap implements Int2Shor } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Int2ShortMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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 @@ -1964,7 +1967,7 @@ public class Int2ShortRBTreeMap extends AbstractInt2ShortMap implements Int2Shor } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -1976,15 +1979,15 @@ public class Int2ShortRBTreeMap extends AbstractInt2ShortMap implements Int2Shor } state = operator.applyAsShort(state, entry.value); } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return (short)0; + if(filter.test(entry.value)) return OptionalShort.of(entry.value); + return OptionalShort.empty(); } @Override @@ -2300,7 +2303,7 @@ public class Int2ShortRBTreeMap extends AbstractInt2ShortMap implements Int2Shor } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -2312,15 +2315,15 @@ public class Int2ShortRBTreeMap extends AbstractInt2ShortMap implements Int2Shor } state = operator.applyAsShort(state, entry.value); } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return (short)0; + if(filter.test(entry.value)) return OptionalShort.of(entry.value); + return OptionalShort.empty(); } @Override @@ -2461,7 +2464,7 @@ public class Int2ShortRBTreeMap extends AbstractInt2ShortMap implements Int2Shor } @Override - public Int2ShortMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Int2ShortMap.Entry state = null; boolean empty = true; @@ -2473,19 +2476,19 @@ public class Int2ShortRBTreeMap extends AbstractInt2ShortMap implements Int2Shor } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Int2ShortMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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/main/java/speiger/src/collections/ints/queues/IntArrayFIFOQueue.java b/src/main/java/speiger/src/collections/ints/queues/IntArrayFIFOQueue.java index 7003d20..1345973 100644 --- a/src/main/java/speiger/src/collections/ints/queues/IntArrayFIFOQueue.java +++ b/src/main/java/speiger/src/collections/ints/queues/IntArrayFIFOQueue.java @@ -2,6 +2,7 @@ package speiger.src.collections.ints.queues; import java.util.Arrays; import java.util.Objects; +import java.util.OptionalInt; import java.util.NoSuchElementException; import java.util.function.IntPredicate; @@ -278,17 +279,17 @@ public class IntArrayFIFOQueue extends AbstractIntPriorityQueue implements IntPr } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); for(int i = 0,m=size();i= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor)); + } + + @Override + public void addLast(int o) { + if(strategy.equals(o, 0)) { + if(containsNull) return; + containsNull = true; + onNodeAdded(nullIndex); + } + else { + int pos = HashUtil.mix(strategy.hashCode(o)) & mask; + while(!strategy.equals(keys[pos], 0)) { + if(strategy.equals(keys[pos], o)) return; + pos = ++pos & mask; + } + keys[pos] = o; + onNodeAdded(pos); + } + if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor)); + } + @Override public boolean addAndMoveToFirst(int o) { if(strategy.equals(o, 0)) { @@ -618,7 +659,7 @@ public class IntLinkedOpenCustomHashSet extends IntOpenCustomHashSet implements } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -631,18 +672,18 @@ public class IntLinkedOpenCustomHashSet extends IntOpenCustomHashSet implements else state = operator.applyAsInt(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); int index = firstIndex; while(index != -1) { - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalInt.of(keys[index]); index = (int)links[index]; } - return 0; + return OptionalInt.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/ints/sets/IntLinkedOpenHashSet.java b/src/main/java/speiger/src/collections/ints/sets/IntLinkedOpenHashSet.java index 00548f5..544d7f0 100644 --- a/src/main/java/speiger/src/collections/ints/sets/IntLinkedOpenHashSet.java +++ b/src/main/java/speiger/src/collections/ints/sets/IntLinkedOpenHashSet.java @@ -5,6 +5,7 @@ import java.util.Collection; import java.util.Iterator; import java.util.NoSuchElementException; import java.util.Objects; +import java.util.OptionalInt; import java.util.function.IntPredicate; import speiger.src.collections.ints.collections.IntCollection; @@ -183,6 +184,46 @@ public class IntLinkedOpenHashSet extends IntOpenHashSet implements IntOrderedSe while(iterator.hasNext()) add(iterator.nextInt()); } + @Override + public void addFirst(int o) { + if(o == 0) { + if(containsNull) return; + containsNull = true; + onNodeAdded(nullIndex); + moveToFirstIndex(nullIndex); + } + else { + int pos = HashUtil.mix(Integer.hashCode(o)) & mask; + while(keys[pos] != 0) { + if(keys[pos] == o) return; + pos = ++pos & mask; + } + keys[pos] = o; + onNodeAdded(pos); + moveToFirstIndex(pos); + } + if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor)); + } + + @Override + public void addLast(int o) { + if(o == 0) { + if(containsNull) return; + containsNull = true; + onNodeAdded(nullIndex); + } + else { + int pos = HashUtil.mix(Integer.hashCode(o)) & mask; + while(keys[pos] != 0) { + if(keys[pos] == o) return; + pos = ++pos & mask; + } + keys[pos] = o; + onNodeAdded(pos); + } + if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor)); + } + @Override public boolean addAndMoveToFirst(int o) { if(o == 0) { @@ -471,7 +512,7 @@ public class IntLinkedOpenHashSet extends IntOpenHashSet implements IntOrderedSe } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -484,18 +525,18 @@ public class IntLinkedOpenHashSet extends IntOpenHashSet implements IntOrderedSe else state = operator.applyAsInt(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); int index = firstIndex; while(index != -1) { - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalInt.of(keys[index]); index = (int)links[index]; } - return 0; + return OptionalInt.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/ints/sets/IntOpenCustomHashSet.java b/src/main/java/speiger/src/collections/ints/sets/IntOpenCustomHashSet.java index 762137a..c6a3191 100644 --- a/src/main/java/speiger/src/collections/ints/sets/IntOpenCustomHashSet.java +++ b/src/main/java/speiger/src/collections/ints/sets/IntOpenCustomHashSet.java @@ -6,6 +6,7 @@ import java.util.ConcurrentModificationException; import java.util.Iterator; import java.util.NoSuchElementException; import java.util.Objects; +import java.util.OptionalInt; import java.util.function.IntPredicate; import speiger.src.collections.ints.collections.IntCollection; @@ -546,7 +547,7 @@ public class IntOpenCustomHashSet extends AbstractIntSet implements ITrimmable } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -563,18 +564,18 @@ public class IntOpenCustomHashSet extends AbstractIntSet implements ITrimmable } state = operator.applyAsInt(state, keys[i]); } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OptionalInt.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OptionalInt.of(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], 0) && filter.test(keys[i])) return keys[i]; + if(!strategy.equals(keys[i], 0) && filter.test(keys[i])) return OptionalInt.of(keys[i]); } - return 0; + return OptionalInt.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/ints/sets/IntOpenHashSet.java b/src/main/java/speiger/src/collections/ints/sets/IntOpenHashSet.java index 6862496..57f8e29 100644 --- a/src/main/java/speiger/src/collections/ints/sets/IntOpenHashSet.java +++ b/src/main/java/speiger/src/collections/ints/sets/IntOpenHashSet.java @@ -6,6 +6,7 @@ import java.util.ConcurrentModificationException; import java.util.Iterator; import java.util.NoSuchElementException; import java.util.Objects; +import java.util.OptionalInt; import java.util.function.IntPredicate; import speiger.src.collections.ints.collections.IntCollection; @@ -428,7 +429,7 @@ public class IntOpenHashSet extends AbstractIntSet implements ITrimmable } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -445,18 +446,18 @@ public class IntOpenHashSet extends AbstractIntSet implements ITrimmable } state = operator.applyAsInt(state, keys[i]); } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OptionalInt.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OptionalInt.of(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(keys[i] != 0 && filter.test(keys[i])) return keys[i]; + if(keys[i] != 0 && filter.test(keys[i])) return OptionalInt.of(keys[i]); } - return 0; + return OptionalInt.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/ints/sets/IntOrderedSet.java b/src/main/java/speiger/src/collections/ints/sets/IntOrderedSet.java index 9c9600b..8645445 100644 --- a/src/main/java/speiger/src/collections/ints/sets/IntOrderedSet.java +++ b/src/main/java/speiger/src/collections/ints/sets/IntOrderedSet.java @@ -1,6 +1,7 @@ package speiger.src.collections.ints.sets; import speiger.src.collections.ints.collections.IntBidirectionalIterator; +import speiger.src.collections.ints.collections.IntOrderedCollection; import speiger.src.collections.ints.collections.IntSplititerator; import speiger.src.collections.ints.utils.IntSplititerators; import speiger.src.collections.ints.sets.AbstractIntSet.ReversedIntOrderedSet; @@ -13,7 +14,7 @@ import speiger.src.collections.ints.utils.IntSets; * This interface provides basic access to such functions while also providing some Sorted/NaivgableSet implementations that still fit into here. * */ -public interface IntOrderedSet extends IntSet +public interface IntOrderedSet extends IntSet, IntOrderedCollection { /** * A customized add method that allows you to insert into the first index. diff --git a/src/main/java/speiger/src/collections/ints/sets/IntRBTreeSet.java b/src/main/java/speiger/src/collections/ints/sets/IntRBTreeSet.java index 4418d2d..8fdc764 100644 --- a/src/main/java/speiger/src/collections/ints/sets/IntRBTreeSet.java +++ b/src/main/java/speiger/src/collections/ints/sets/IntRBTreeSet.java @@ -2,6 +2,7 @@ package speiger.src.collections.ints.sets; import java.util.Collection; import java.util.Collections; +import java.util.OptionalInt; import java.util.Iterator; import java.util.NoSuchElementException; import java.util.Objects; @@ -334,7 +335,7 @@ public class IntRBTreeSet extends AbstractIntSet implements IntNavigableSet } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -346,16 +347,16 @@ public class IntRBTreeSet extends AbstractIntSet implements IntNavigableSet } state = operator.applyAsInt(state, entry.key); } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); for(Entry entry = first;entry != null;entry = entry.next()) { - if(filter.test(entry.key)) return entry.key; + if(filter.test(entry.key)) return OptionalInt.of(entry.key); } - return 0; + return OptionalInt.empty(); } @Override @@ -1241,7 +1242,7 @@ public class IntRBTreeSet extends AbstractIntSet implements IntNavigableSet } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -1253,16 +1254,16 @@ public class IntRBTreeSet extends AbstractIntSet implements IntNavigableSet } state = operator.applyAsInt(state, entry.key); } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); for(Entry entry = start();entry != null && inRange(entry.key);entry = next(entry)) { - if(filter.test(entry.key)) return entry.key; + if(filter.test(entry.key)) return OptionalInt.of(entry.key); } - return 0; + return OptionalInt.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/ints/utils/IntCollections.java b/src/main/java/speiger/src/collections/ints/utils/IntCollections.java index 84825d7..1b09dcd 100644 --- a/src/main/java/speiger/src/collections/ints/utils/IntCollections.java +++ b/src/main/java/speiger/src/collections/ints/utils/IntCollections.java @@ -6,6 +6,7 @@ import java.util.ConcurrentModificationException; import java.util.Iterator; import java.util.NoSuchElementException; import java.util.Objects; +import java.util.OptionalInt; import java.util.function.Predicate; import java.util.function.IntPredicate; import java.util.function.Consumer; @@ -757,9 +758,9 @@ public class IntCollections @Override public int reduce(int identity, IntIntUnaryOperator operator) { synchronized(mutex) { return c.reduce(identity, operator); } } @Override - public int reduce(IntIntUnaryOperator operator) { synchronized(mutex) { return c.reduce(operator); } } + public OptionalInt reduce(IntIntUnaryOperator operator) { synchronized(mutex) { return c.reduce(operator); } } @Override - public int findFirst(IntPredicate filter) { synchronized(mutex) { return c.findFirst(filter); } } + public OptionalInt findFirst(IntPredicate filter) { synchronized(mutex) { return c.findFirst(filter); } } @Override public int count(IntPredicate filter) { synchronized(mutex) { return c.count(filter); } } } @@ -887,9 +888,9 @@ public class IntCollections @Override public int reduce(int identity, IntIntUnaryOperator operator) { return c.reduce(identity, operator); } @Override - public int reduce(IntIntUnaryOperator operator) { return c.reduce(operator); } + public OptionalInt reduce(IntIntUnaryOperator operator) { return c.reduce(operator); } @Override - public int findFirst(IntPredicate filter) { return c.findFirst(filter); } + public OptionalInt findFirst(IntPredicate filter) { return c.findFirst(filter); } @Override public int count(IntPredicate filter) { return c.count(filter); } } diff --git a/src/main/java/speiger/src/collections/ints/utils/IntPriorityQueues.java b/src/main/java/speiger/src/collections/ints/utils/IntPriorityQueues.java index 3cc8bb4..704d49b 100644 --- a/src/main/java/speiger/src/collections/ints/utils/IntPriorityQueues.java +++ b/src/main/java/speiger/src/collections/ints/utils/IntPriorityQueues.java @@ -1,13 +1,14 @@ package speiger.src.collections.ints.utils; import java.util.function.IntPredicate; - +import java.util.OptionalInt; import speiger.src.collections.ints.collections.IntIterator; import speiger.src.collections.ints.collections.IntCollection; import speiger.src.collections.ints.functions.IntComparator; import speiger.src.collections.ints.queues.IntPriorityDequeue; import speiger.src.collections.ints.queues.IntPriorityQueue; import speiger.src.collections.ints.functions.IntConsumer; +import speiger.src.collections.ints.functions.function.IntIntUnaryOperator; import speiger.src.collections.objects.functions.consumer.ObjectIntConsumer; /** @@ -113,7 +114,11 @@ public class IntPriorityQueues @Override public boolean matchesAll(IntPredicate filter) { synchronized(mutex) { return queue.matchesAll(filter); } } @Override - public int findFirst(IntPredicate filter) { synchronized(mutex) { return queue.findFirst(filter); } } + public OptionalInt findFirst(IntPredicate filter) { synchronized(mutex) { return queue.findFirst(filter); } } + @Override + public int reduce(int identity, IntIntUnaryOperator operator) { synchronized(mutex) { return queue.reduce(identity, operator); } } + @Override + public OptionalInt reduce(IntIntUnaryOperator operator) { synchronized(mutex) { return queue.reduce(operator); } } @Override public int count(IntPredicate filter) { synchronized(mutex) { return queue.count(filter); } } } diff --git a/src/main/java/speiger/src/collections/ints/utils/IntSets.java b/src/main/java/speiger/src/collections/ints/utils/IntSets.java index 87b8220..f9d6e7a 100644 --- a/src/main/java/speiger/src/collections/ints/utils/IntSets.java +++ b/src/main/java/speiger/src/collections/ints/utils/IntSets.java @@ -301,6 +301,10 @@ public class IntSets s = c; } + @Override + public void addFirst(int o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(int o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(int o) { throw new UnsupportedOperationException(); } @Override @@ -573,7 +577,11 @@ public class IntSets super(c, mutex); s = c; } - + + @Override + public void addFirst(int o) { synchronized(mutex) { s.addFirst(o); } } + @Override + public void addLast(int o) { synchronized(mutex) { s.addLast(o); } } @Override public boolean addAndMoveToFirst(int o) { synchronized(mutex) { return s.addAndMoveToFirst(o); } } @Override diff --git a/src/main/java/speiger/src/collections/ints/utils/maps/Int2BooleanMaps.java b/src/main/java/speiger/src/collections/ints/utils/maps/Int2BooleanMaps.java index 16d2e86..016d5f2 100644 --- a/src/main/java/speiger/src/collections/ints/utils/maps/Int2BooleanMaps.java +++ b/src/main/java/speiger/src/collections/ints/utils/maps/Int2BooleanMaps.java @@ -608,7 +608,11 @@ public class Int2BooleanMaps super(c); set = c; } - + + @Override + public void addFirst(Int2BooleanMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Int2BooleanMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Int2BooleanMap.Entry o) { throw new UnsupportedOperationException(); } @Override diff --git a/src/main/java/speiger/src/collections/ints/utils/maps/Int2ByteMaps.java b/src/main/java/speiger/src/collections/ints/utils/maps/Int2ByteMaps.java index 83c87b6..079c62b 100644 --- a/src/main/java/speiger/src/collections/ints/utils/maps/Int2ByteMaps.java +++ b/src/main/java/speiger/src/collections/ints/utils/maps/Int2ByteMaps.java @@ -620,7 +620,11 @@ public class Int2ByteMaps super(c); set = c; } - + + @Override + public void addFirst(Int2ByteMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Int2ByteMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Int2ByteMap.Entry o) { throw new UnsupportedOperationException(); } @Override diff --git a/src/main/java/speiger/src/collections/ints/utils/maps/Int2CharMaps.java b/src/main/java/speiger/src/collections/ints/utils/maps/Int2CharMaps.java index eb55285..1caba3f 100644 --- a/src/main/java/speiger/src/collections/ints/utils/maps/Int2CharMaps.java +++ b/src/main/java/speiger/src/collections/ints/utils/maps/Int2CharMaps.java @@ -620,7 +620,11 @@ public class Int2CharMaps super(c); set = c; } - + + @Override + public void addFirst(Int2CharMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Int2CharMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Int2CharMap.Entry o) { throw new UnsupportedOperationException(); } @Override diff --git a/src/main/java/speiger/src/collections/ints/utils/maps/Int2DoubleMaps.java b/src/main/java/speiger/src/collections/ints/utils/maps/Int2DoubleMaps.java index 2d75347..0018b47 100644 --- a/src/main/java/speiger/src/collections/ints/utils/maps/Int2DoubleMaps.java +++ b/src/main/java/speiger/src/collections/ints/utils/maps/Int2DoubleMaps.java @@ -620,7 +620,11 @@ public class Int2DoubleMaps super(c); set = c; } - + + @Override + public void addFirst(Int2DoubleMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Int2DoubleMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Int2DoubleMap.Entry o) { throw new UnsupportedOperationException(); } @Override diff --git a/src/main/java/speiger/src/collections/ints/utils/maps/Int2FloatMaps.java b/src/main/java/speiger/src/collections/ints/utils/maps/Int2FloatMaps.java index 0784bd4..a3d4394 100644 --- a/src/main/java/speiger/src/collections/ints/utils/maps/Int2FloatMaps.java +++ b/src/main/java/speiger/src/collections/ints/utils/maps/Int2FloatMaps.java @@ -620,7 +620,11 @@ public class Int2FloatMaps super(c); set = c; } - + + @Override + public void addFirst(Int2FloatMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Int2FloatMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Int2FloatMap.Entry o) { throw new UnsupportedOperationException(); } @Override diff --git a/src/main/java/speiger/src/collections/ints/utils/maps/Int2IntMaps.java b/src/main/java/speiger/src/collections/ints/utils/maps/Int2IntMaps.java index 76d0b2a..cb48925 100644 --- a/src/main/java/speiger/src/collections/ints/utils/maps/Int2IntMaps.java +++ b/src/main/java/speiger/src/collections/ints/utils/maps/Int2IntMaps.java @@ -619,7 +619,11 @@ public class Int2IntMaps super(c); set = c; } - + + @Override + public void addFirst(Int2IntMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Int2IntMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Int2IntMap.Entry o) { throw new UnsupportedOperationException(); } @Override diff --git a/src/main/java/speiger/src/collections/ints/utils/maps/Int2LongMaps.java b/src/main/java/speiger/src/collections/ints/utils/maps/Int2LongMaps.java index c6ff278..71707ad 100644 --- a/src/main/java/speiger/src/collections/ints/utils/maps/Int2LongMaps.java +++ b/src/main/java/speiger/src/collections/ints/utils/maps/Int2LongMaps.java @@ -620,7 +620,11 @@ public class Int2LongMaps super(c); set = c; } - + + @Override + public void addFirst(Int2LongMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Int2LongMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Int2LongMap.Entry o) { throw new UnsupportedOperationException(); } @Override diff --git a/src/main/java/speiger/src/collections/ints/utils/maps/Int2ObjectMaps.java b/src/main/java/speiger/src/collections/ints/utils/maps/Int2ObjectMaps.java index a610ef1..59cc247 100644 --- a/src/main/java/speiger/src/collections/ints/utils/maps/Int2ObjectMaps.java +++ b/src/main/java/speiger/src/collections/ints/utils/maps/Int2ObjectMaps.java @@ -611,7 +611,11 @@ public class Int2ObjectMaps super(c); set = c; } - + + @Override + public void addFirst(Int2ObjectMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Int2ObjectMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Int2ObjectMap.Entry o) { throw new UnsupportedOperationException(); } @Override diff --git a/src/main/java/speiger/src/collections/ints/utils/maps/Int2ShortMaps.java b/src/main/java/speiger/src/collections/ints/utils/maps/Int2ShortMaps.java index 14d25b3..3967732 100644 --- a/src/main/java/speiger/src/collections/ints/utils/maps/Int2ShortMaps.java +++ b/src/main/java/speiger/src/collections/ints/utils/maps/Int2ShortMaps.java @@ -620,7 +620,11 @@ public class Int2ShortMaps super(c); set = c; } - + + @Override + public void addFirst(Int2ShortMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Int2ShortMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Int2ShortMap.Entry o) { throw new UnsupportedOperationException(); } @Override diff --git a/src/main/java/speiger/src/collections/longs/collections/LongIterable.java b/src/main/java/speiger/src/collections/longs/collections/LongIterable.java index 8ac3cb2..fe3f830 100644 --- a/src/main/java/speiger/src/collections/longs/collections/LongIterable.java +++ b/src/main/java/speiger/src/collections/longs/collections/LongIterable.java @@ -3,6 +3,7 @@ package speiger.src.collections.longs.collections; import java.util.Objects; import java.util.function.Consumer; import java.util.function.LongPredicate; +import java.util.OptionalLong; import speiger.src.collections.longs.functions.LongConsumer; @@ -281,13 +282,13 @@ public interface LongIterable extends Iterable * @param filter that should be applied * @return the found value or the null equivalent variant. */ - default long findFirst(LongPredicate filter) { + default OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); for(LongIterator iter = iterator();iter.hasNext();) { long entry = iter.nextLong(); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return OptionalLong.of(entry); } - return 0L; + return OptionalLong.empty(); } /** @@ -312,7 +313,7 @@ public interface LongIterable extends Iterable * @param operator the operation that should be applied * @return the reduction result, returns null value if nothing was found */ - default long reduce(LongLongUnaryOperator operator) { + default OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -324,7 +325,7 @@ public interface LongIterable extends Iterable } state = operator.applyAsLong(state, iter.nextLong()); } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } /** diff --git a/src/main/java/speiger/src/collections/longs/lists/CopyOnWriteLongArrayList.java b/src/main/java/speiger/src/collections/longs/lists/CopyOnWriteLongArrayList.java index 785305f..ed11cbb 100644 --- a/src/main/java/speiger/src/collections/longs/lists/CopyOnWriteLongArrayList.java +++ b/src/main/java/speiger/src/collections/longs/lists/CopyOnWriteLongArrayList.java @@ -5,6 +5,7 @@ import java.util.Collection; import java.util.Iterator; import java.util.NoSuchElementException; import java.util.Objects; +import java.util.OptionalLong; import java.util.concurrent.locks.ReentrantLock; import java.util.function.Consumer; import java.util.function.Supplier; @@ -654,13 +655,13 @@ public class CopyOnWriteLongArrayList extends AbstractLongList implements ITrimm } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); long[] data = this.data; for(int i = 0,m=data.length;i operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Long2BooleanMap.Entry state = null; boolean empty = true; @@ -779,11 +782,11 @@ public class Long2BooleanConcurrentOpenHashMap extends AbstractLong2BooleanMap i seg.unlockRead(stamp); } } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Long2BooleanMap.Entry 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); Long2ByteMap.Entry state = null; boolean empty = true; @@ -792,11 +795,11 @@ public class Long2ByteConcurrentOpenHashMap extends AbstractLong2ByteMap impleme seg.unlockRead(stamp); } } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Long2ByteMap.Entry 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); Long2CharMap.Entry state = null; boolean empty = true; @@ -792,11 +795,11 @@ public class Long2CharConcurrentOpenHashMap extends AbstractLong2CharMap impleme seg.unlockRead(stamp); } } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Long2CharMap.Entry 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); Long2DoubleMap.Entry state = null; boolean empty = true; @@ -792,11 +795,11 @@ public class Long2DoubleConcurrentOpenHashMap extends AbstractLong2DoubleMap imp seg.unlockRead(stamp); } } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Long2DoubleMap.Entry 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); Long2FloatMap.Entry state = null; boolean empty = true; @@ -792,11 +795,11 @@ public class Long2FloatConcurrentOpenHashMap extends AbstractLong2FloatMap imple seg.unlockRead(stamp); } } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Long2FloatMap.Entry 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); Long2IntMap.Entry state = null; boolean empty = true; @@ -792,11 +795,11 @@ public class Long2IntConcurrentOpenHashMap extends AbstractLong2IntMap implement seg.unlockRead(stamp); } } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Long2IntMap.Entry 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); Long2LongMap.Entry state = null; boolean empty = true; @@ -785,11 +787,11 @@ public class Long2LongConcurrentOpenHashMap extends AbstractLong2LongMap impleme seg.unlockRead(stamp); } } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Long2LongMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); MapEntry entry = new MapEntry(); for(int i = 0,m=segments.length;i extends AbstractLong2ObjectMap< } @Override - public Long2ObjectMap.Entry reduce(ObjectObjectUnaryOperator, Long2ObjectMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Long2ObjectMap.Entry> operator) { Objects.requireNonNull(operator); Long2ObjectMap.Entry state = null; boolean empty = true; @@ -739,11 +741,11 @@ public class Long2ObjectConcurrentOpenHashMap extends AbstractLong2ObjectMap< seg.unlockRead(stamp); } } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Long2ObjectMap.Entry findFirst(Predicate> filter) { + public Optional> findFirst(Predicate> filter) { Objects.requireNonNull(filter); MapEntry entry = new MapEntry(); for(int i = 0,m=segments.length;i extends AbstractLong2ObjectMap< try { while(index != -1) { entry.set(index, i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); index = (int)seg.links[index]; } } @@ -761,7 +763,7 @@ public class Long2ObjectConcurrentOpenHashMap extends AbstractLong2ObjectMap< seg.unlockRead(stamp); } } - return null; + return Optional.empty(); } @Override @@ -1022,7 +1024,7 @@ public class Long2ObjectConcurrentOpenHashMap extends AbstractLong2ObjectMap< } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -1046,11 +1048,11 @@ public class Long2ObjectConcurrentOpenHashMap extends AbstractLong2ObjectMap< seg.unlockRead(stamp); } } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); for(int i = 0,m=segments.length;i seg = segments[i]; @@ -1058,7 +1060,7 @@ public class Long2ObjectConcurrentOpenHashMap extends AbstractLong2ObjectMap< try { int index = seg.firstIndex; while(index != -1){ - if(filter.test(seg.keys[index])) return seg.keys[index]; + if(filter.test(seg.keys[index])) return OptionalLong.of(seg.keys[index]); index = (int)seg.links[index]; } } @@ -1066,7 +1068,7 @@ public class Long2ObjectConcurrentOpenHashMap extends AbstractLong2ObjectMap< seg.unlockRead(stamp); } } - return 0L; + return OptionalLong.empty(); } @Override @@ -1258,7 +1260,7 @@ public class Long2ObjectConcurrentOpenHashMap extends AbstractLong2ObjectMap< } @Override - public V reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); V state = null; boolean empty = true; @@ -1282,20 +1284,20 @@ public class Long2ObjectConcurrentOpenHashMap extends AbstractLong2ObjectMap< seg.unlockRead(stamp); } } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public V findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); for(int i = 0,m=segments.length;i seg = segments[i]; long stamp = seg.readLock(); try { int index = seg.firstIndex; while(index != -1){ - if(filter.test(seg.values[index])) return seg.values[index]; + if(filter.test(seg.values[index])) return Optional.ofNullable(seg.values[index]); index = (int)seg.links[index]; } } @@ -1303,7 +1305,7 @@ public class Long2ObjectConcurrentOpenHashMap extends AbstractLong2ObjectMap< seg.unlockRead(stamp); } } - return null; + return Optional.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/longs/maps/impl/concurrent/Long2ShortConcurrentOpenHashMap.java b/src/main/java/speiger/src/collections/longs/maps/impl/concurrent/Long2ShortConcurrentOpenHashMap.java index 1638b43..2ee2dff 100644 --- a/src/main/java/speiger/src/collections/longs/maps/impl/concurrent/Long2ShortConcurrentOpenHashMap.java +++ b/src/main/java/speiger/src/collections/longs/maps/impl/concurrent/Long2ShortConcurrentOpenHashMap.java @@ -4,11 +4,13 @@ import java.util.Arrays; import java.util.Map; import java.util.NoSuchElementException; import java.util.Objects; +import java.util.Optional; import java.util.concurrent.locks.StampedLock; import java.util.function.Consumer; import java.util.function.Predicate; import java.util.function.BiFunction; import java.util.function.LongPredicate; +import java.util.OptionalLong; import speiger.src.collections.longs.collections.LongBidirectionalIterator; import speiger.src.collections.longs.functions.LongConsumer; @@ -37,6 +39,7 @@ import speiger.src.collections.shorts.functions.ShortConsumer; import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectShortConsumer; import speiger.src.collections.shorts.functions.function.ShortPredicate; +import speiger.src.collections.shorts.functions.OptionalShort; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.objects.sets.AbstractObjectSet; import speiger.src.collections.objects.sets.ObjectSet; @@ -768,7 +771,7 @@ public class Long2ShortConcurrentOpenHashMap extends AbstractLong2ShortMap imple } @Override - public Long2ShortMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Long2ShortMap.Entry state = null; boolean empty = true; @@ -792,11 +795,11 @@ public class Long2ShortConcurrentOpenHashMap extends AbstractLong2ShortMap imple seg.unlockRead(stamp); } } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Long2ShortMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); MapEntry entry = new MapEntry(); for(int i = 0,m=segments.length;i implements Long2BooleanOrderedMap.FastOrderedSet { + @Override + public void addFirst(Long2BooleanMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Long2BooleanMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Long2BooleanMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -836,7 +843,7 @@ public class Long2BooleanLinkedOpenCustomHashMap extends Long2BooleanOpenCustomH } @Override - public Long2BooleanMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Long2BooleanMap.Entry state = null; boolean empty = true; @@ -851,21 +858,21 @@ public class Long2BooleanLinkedOpenCustomHashMap extends Long2BooleanOpenCustomH state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Long2BooleanMap.Entry 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 @@ -944,10 +951,13 @@ public class Long2BooleanLinkedOpenCustomHashMap extends Long2BooleanOpenCustomH public boolean add(long o) { throw new UnsupportedOperationException(); } - + + @Override + public void addFirst(long o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(long o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(long o) { throw new UnsupportedOperationException(); } - @Override public boolean addAndMoveToLast(long o) { throw new UnsupportedOperationException(); } @@ -1090,7 +1100,7 @@ public class Long2BooleanLinkedOpenCustomHashMap extends Long2BooleanOpenCustomH } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -1105,19 +1115,19 @@ public class Long2BooleanLinkedOpenCustomHashMap extends Long2BooleanOpenCustomH state = operator.applyAsLong(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0L; + if(size() <= 0) return OptionalLong.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalLong.of(keys[index]); index = (int)links[index]; } - return 0L; + return OptionalLong.empty(); } @Override @@ -1252,7 +1262,7 @@ public class Long2BooleanLinkedOpenCustomHashMap extends Long2BooleanOpenCustomH } @Override - public boolean reduce(BooleanBooleanUnaryOperator operator) { + public OptionalBoolean reduce(BooleanBooleanUnaryOperator operator) { Objects.requireNonNull(operator); boolean state = false; boolean empty = true; @@ -1267,19 +1277,19 @@ public class Long2BooleanLinkedOpenCustomHashMap extends Long2BooleanOpenCustomH state = operator.applyAsBoolean(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalBoolean.empty() : OptionalBoolean.of(state); } @Override - public boolean findFirst(BooleanPredicate filter) { + public OptionalBoolean findFirst(BooleanPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return false; + if(size() <= 0) return OptionalBoolean.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalBoolean.of(values[index]); index = (int)links[index]; } - return false; + return OptionalBoolean.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/longs/maps/impl/customHash/Long2BooleanOpenCustomHashMap.java b/src/main/java/speiger/src/collections/longs/maps/impl/customHash/Long2BooleanOpenCustomHashMap.java index f9dab5f..cf9ab2e 100644 --- a/src/main/java/speiger/src/collections/longs/maps/impl/customHash/Long2BooleanOpenCustomHashMap.java +++ b/src/main/java/speiger/src/collections/longs/maps/impl/customHash/Long2BooleanOpenCustomHashMap.java @@ -5,10 +5,12 @@ 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; import java.util.function.LongPredicate; +import java.util.OptionalLong; import speiger.src.collections.longs.collections.LongIterator; import speiger.src.collections.longs.functions.LongConsumer; @@ -36,6 +38,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectBooleanConsumer; import speiger.src.collections.booleans.functions.function.BooleanPredicate; +import speiger.src.collections.booleans.functions.OptionalBoolean; import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.sets.AbstractObjectSet; import speiger.src.collections.objects.sets.ObjectSet; @@ -930,7 +933,7 @@ public class Long2BooleanOpenCustomHashMap extends AbstractLong2BooleanMap imple } @Override - public Long2BooleanMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Long2BooleanMap.Entry state = null; boolean empty = true; @@ -947,25 +950,25 @@ public class Long2BooleanOpenCustomHashMap extends AbstractLong2BooleanMap imple } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Long2BooleanMap.Entry 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], 0L)) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1139,7 +1142,7 @@ public class Long2BooleanOpenCustomHashMap extends AbstractLong2BooleanMap imple } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -1156,18 +1159,18 @@ public class Long2BooleanOpenCustomHashMap extends AbstractLong2BooleanMap imple } state = operator.applyAsLong(state, keys[i]); } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0L; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OptionalLong.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OptionalLong.of(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], 0L) && filter.test(keys[i])) return keys[i]; + if(!strategy.equals(keys[i], 0L) && filter.test(keys[i])) return OptionalLong.of(keys[i]); } - return 0L; + return OptionalLong.empty(); } @Override @@ -1282,7 +1285,7 @@ public class Long2BooleanOpenCustomHashMap extends AbstractLong2BooleanMap imple } @Override - public boolean reduce(BooleanBooleanUnaryOperator operator) { + public OptionalBoolean reduce(BooleanBooleanUnaryOperator operator) { Objects.requireNonNull(operator); boolean state = false; boolean empty = true; @@ -1299,18 +1302,18 @@ public class Long2BooleanOpenCustomHashMap extends AbstractLong2BooleanMap imple } state = operator.applyAsBoolean(state, values[i]); } - return state; + return empty ? OptionalBoolean.empty() : OptionalBoolean.of(state); } @Override - public boolean findFirst(BooleanPredicate filter) { + public OptionalBoolean findFirst(BooleanPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return false; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return OptionalBoolean.empty(); + if(containsNull && filter.test(values[nullIndex])) return OptionalBoolean.of(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], 0L) && filter.test(values[i])) return values[i]; + if(!strategy.equals(keys[i], 0L) && filter.test(values[i])) return OptionalBoolean.of(values[i]); } - return false; + return OptionalBoolean.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/longs/maps/impl/customHash/Long2ByteLinkedOpenCustomHashMap.java b/src/main/java/speiger/src/collections/longs/maps/impl/customHash/Long2ByteLinkedOpenCustomHashMap.java index 400bb4f..cfdf90c 100644 --- a/src/main/java/speiger/src/collections/longs/maps/impl/customHash/Long2ByteLinkedOpenCustomHashMap.java +++ b/src/main/java/speiger/src/collections/longs/maps/impl/customHash/Long2ByteLinkedOpenCustomHashMap.java @@ -4,10 +4,12 @@ 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; import java.util.function.LongPredicate; +import java.util.OptionalLong; import speiger.src.collections.longs.collections.LongBidirectionalIterator; import speiger.src.collections.longs.functions.LongConsumer; @@ -33,6 +35,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectByteConsumer; import speiger.src.collections.bytes.functions.function.BytePredicate; +import speiger.src.collections.bytes.functions.OptionalByte; import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOperator; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.objects.lists.ObjectListIterator; @@ -671,6 +674,10 @@ public class Long2ByteLinkedOpenCustomHashMap extends Long2ByteOpenCustomHashMap } private class MapEntrySet extends AbstractObjectSet implements Long2ByteOrderedMap.FastOrderedSet { + @Override + public void addFirst(Long2ByteMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Long2ByteMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Long2ByteMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -836,7 +843,7 @@ public class Long2ByteLinkedOpenCustomHashMap extends Long2ByteOpenCustomHashMap } @Override - public Long2ByteMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Long2ByteMap.Entry state = null; boolean empty = true; @@ -851,21 +858,21 @@ public class Long2ByteLinkedOpenCustomHashMap extends Long2ByteOpenCustomHashMap state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Long2ByteMap.Entry 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 @@ -944,10 +951,13 @@ public class Long2ByteLinkedOpenCustomHashMap extends Long2ByteOpenCustomHashMap public boolean add(long o) { throw new UnsupportedOperationException(); } - + + @Override + public void addFirst(long o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(long o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(long o) { throw new UnsupportedOperationException(); } - @Override public boolean addAndMoveToLast(long o) { throw new UnsupportedOperationException(); } @@ -1090,7 +1100,7 @@ public class Long2ByteLinkedOpenCustomHashMap extends Long2ByteOpenCustomHashMap } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -1105,19 +1115,19 @@ public class Long2ByteLinkedOpenCustomHashMap extends Long2ByteOpenCustomHashMap state = operator.applyAsLong(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0L; + if(size() <= 0) return OptionalLong.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalLong.of(keys[index]); index = (int)links[index]; } - return 0L; + return OptionalLong.empty(); } @Override @@ -1252,7 +1262,7 @@ public class Long2ByteLinkedOpenCustomHashMap extends Long2ByteOpenCustomHashMap } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -1267,19 +1277,19 @@ public class Long2ByteLinkedOpenCustomHashMap extends Long2ByteOpenCustomHashMap state = operator.applyAsByte(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (byte)0; + if(size() <= 0) return OptionalByte.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalByte.of(values[index]); index = (int)links[index]; } - return (byte)0; + return OptionalByte.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/longs/maps/impl/customHash/Long2ByteOpenCustomHashMap.java b/src/main/java/speiger/src/collections/longs/maps/impl/customHash/Long2ByteOpenCustomHashMap.java index 71d93f1..af01e0d 100644 --- a/src/main/java/speiger/src/collections/longs/maps/impl/customHash/Long2ByteOpenCustomHashMap.java +++ b/src/main/java/speiger/src/collections/longs/maps/impl/customHash/Long2ByteOpenCustomHashMap.java @@ -5,10 +5,12 @@ 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; import java.util.function.LongPredicate; +import java.util.OptionalLong; import speiger.src.collections.longs.collections.LongIterator; import speiger.src.collections.longs.functions.LongConsumer; @@ -37,6 +39,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectByteConsumer; import speiger.src.collections.bytes.functions.function.BytePredicate; +import speiger.src.collections.bytes.functions.OptionalByte; import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.sets.AbstractObjectSet; import speiger.src.collections.objects.sets.ObjectSet; @@ -952,7 +955,7 @@ public class Long2ByteOpenCustomHashMap extends AbstractLong2ByteMap implements } @Override - public Long2ByteMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Long2ByteMap.Entry state = null; boolean empty = true; @@ -969,25 +972,25 @@ public class Long2ByteOpenCustomHashMap extends AbstractLong2ByteMap implements } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Long2ByteMap.Entry 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], 0L)) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1161,7 +1164,7 @@ public class Long2ByteOpenCustomHashMap extends AbstractLong2ByteMap implements } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -1178,18 +1181,18 @@ public class Long2ByteOpenCustomHashMap extends AbstractLong2ByteMap implements } state = operator.applyAsLong(state, keys[i]); } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0L; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OptionalLong.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OptionalLong.of(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], 0L) && filter.test(keys[i])) return keys[i]; + if(!strategy.equals(keys[i], 0L) && filter.test(keys[i])) return OptionalLong.of(keys[i]); } - return 0L; + return OptionalLong.empty(); } @Override @@ -1304,7 +1307,7 @@ public class Long2ByteOpenCustomHashMap extends AbstractLong2ByteMap implements } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -1321,18 +1324,18 @@ public class Long2ByteOpenCustomHashMap extends AbstractLong2ByteMap implements } state = operator.applyAsByte(state, values[i]); } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (byte)0; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return OptionalByte.empty(); + if(containsNull && filter.test(values[nullIndex])) return OptionalByte.of(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], 0L) && filter.test(values[i])) return values[i]; + if(!strategy.equals(keys[i], 0L) && filter.test(values[i])) return OptionalByte.of(values[i]); } - return (byte)0; + return OptionalByte.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/longs/maps/impl/customHash/Long2CharLinkedOpenCustomHashMap.java b/src/main/java/speiger/src/collections/longs/maps/impl/customHash/Long2CharLinkedOpenCustomHashMap.java index 4076180..dee8e0c 100644 --- a/src/main/java/speiger/src/collections/longs/maps/impl/customHash/Long2CharLinkedOpenCustomHashMap.java +++ b/src/main/java/speiger/src/collections/longs/maps/impl/customHash/Long2CharLinkedOpenCustomHashMap.java @@ -4,10 +4,12 @@ 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; import java.util.function.LongPredicate; +import java.util.OptionalLong; import speiger.src.collections.longs.collections.LongBidirectionalIterator; import speiger.src.collections.longs.functions.LongConsumer; @@ -33,6 +35,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectCharConsumer; import speiger.src.collections.chars.functions.function.CharPredicate; +import speiger.src.collections.chars.functions.OptionalChar; import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOperator; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.objects.lists.ObjectListIterator; @@ -671,6 +674,10 @@ public class Long2CharLinkedOpenCustomHashMap extends Long2CharOpenCustomHashMap } private class MapEntrySet extends AbstractObjectSet implements Long2CharOrderedMap.FastOrderedSet { + @Override + public void addFirst(Long2CharMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Long2CharMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Long2CharMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -836,7 +843,7 @@ public class Long2CharLinkedOpenCustomHashMap extends Long2CharOpenCustomHashMap } @Override - public Long2CharMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Long2CharMap.Entry state = null; boolean empty = true; @@ -851,21 +858,21 @@ public class Long2CharLinkedOpenCustomHashMap extends Long2CharOpenCustomHashMap state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Long2CharMap.Entry 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 @@ -944,10 +951,13 @@ public class Long2CharLinkedOpenCustomHashMap extends Long2CharOpenCustomHashMap public boolean add(long o) { throw new UnsupportedOperationException(); } - + + @Override + public void addFirst(long o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(long o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(long o) { throw new UnsupportedOperationException(); } - @Override public boolean addAndMoveToLast(long o) { throw new UnsupportedOperationException(); } @@ -1090,7 +1100,7 @@ public class Long2CharLinkedOpenCustomHashMap extends Long2CharOpenCustomHashMap } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -1105,19 +1115,19 @@ public class Long2CharLinkedOpenCustomHashMap extends Long2CharOpenCustomHashMap state = operator.applyAsLong(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0L; + if(size() <= 0) return OptionalLong.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalLong.of(keys[index]); index = (int)links[index]; } - return 0L; + return OptionalLong.empty(); } @Override @@ -1252,7 +1262,7 @@ public class Long2CharLinkedOpenCustomHashMap extends Long2CharOpenCustomHashMap } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -1267,19 +1277,19 @@ public class Long2CharLinkedOpenCustomHashMap extends Long2CharOpenCustomHashMap state = operator.applyAsChar(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (char)0; + if(size() <= 0) return OptionalChar.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalChar.of(values[index]); index = (int)links[index]; } - return (char)0; + return OptionalChar.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/longs/maps/impl/customHash/Long2CharOpenCustomHashMap.java b/src/main/java/speiger/src/collections/longs/maps/impl/customHash/Long2CharOpenCustomHashMap.java index 3c6dd5b..d9b1599 100644 --- a/src/main/java/speiger/src/collections/longs/maps/impl/customHash/Long2CharOpenCustomHashMap.java +++ b/src/main/java/speiger/src/collections/longs/maps/impl/customHash/Long2CharOpenCustomHashMap.java @@ -5,10 +5,12 @@ 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; import java.util.function.LongPredicate; +import java.util.OptionalLong; import speiger.src.collections.longs.collections.LongIterator; import speiger.src.collections.longs.functions.LongConsumer; @@ -37,6 +39,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectCharConsumer; import speiger.src.collections.chars.functions.function.CharPredicate; +import speiger.src.collections.chars.functions.OptionalChar; import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.sets.AbstractObjectSet; import speiger.src.collections.objects.sets.ObjectSet; @@ -952,7 +955,7 @@ public class Long2CharOpenCustomHashMap extends AbstractLong2CharMap implements } @Override - public Long2CharMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Long2CharMap.Entry state = null; boolean empty = true; @@ -969,25 +972,25 @@ public class Long2CharOpenCustomHashMap extends AbstractLong2CharMap implements } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Long2CharMap.Entry 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], 0L)) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1161,7 +1164,7 @@ public class Long2CharOpenCustomHashMap extends AbstractLong2CharMap implements } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -1178,18 +1181,18 @@ public class Long2CharOpenCustomHashMap extends AbstractLong2CharMap implements } state = operator.applyAsLong(state, keys[i]); } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0L; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OptionalLong.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OptionalLong.of(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], 0L) && filter.test(keys[i])) return keys[i]; + if(!strategy.equals(keys[i], 0L) && filter.test(keys[i])) return OptionalLong.of(keys[i]); } - return 0L; + return OptionalLong.empty(); } @Override @@ -1304,7 +1307,7 @@ public class Long2CharOpenCustomHashMap extends AbstractLong2CharMap implements } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -1321,18 +1324,18 @@ public class Long2CharOpenCustomHashMap extends AbstractLong2CharMap implements } state = operator.applyAsChar(state, values[i]); } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (char)0; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return OptionalChar.empty(); + if(containsNull && filter.test(values[nullIndex])) return OptionalChar.of(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], 0L) && filter.test(values[i])) return values[i]; + if(!strategy.equals(keys[i], 0L) && filter.test(values[i])) return OptionalChar.of(values[i]); } - return (char)0; + return OptionalChar.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/longs/maps/impl/customHash/Long2DoubleLinkedOpenCustomHashMap.java b/src/main/java/speiger/src/collections/longs/maps/impl/customHash/Long2DoubleLinkedOpenCustomHashMap.java index 00aa676..c250292 100644 --- a/src/main/java/speiger/src/collections/longs/maps/impl/customHash/Long2DoubleLinkedOpenCustomHashMap.java +++ b/src/main/java/speiger/src/collections/longs/maps/impl/customHash/Long2DoubleLinkedOpenCustomHashMap.java @@ -4,11 +4,14 @@ 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; import java.util.function.LongPredicate; +import java.util.OptionalLong; import java.util.function.DoublePredicate; +import java.util.OptionalDouble; import speiger.src.collections.longs.collections.LongBidirectionalIterator; import speiger.src.collections.longs.functions.LongConsumer; @@ -671,6 +674,10 @@ public class Long2DoubleLinkedOpenCustomHashMap extends Long2DoubleOpenCustomHas } private class MapEntrySet extends AbstractObjectSet implements Long2DoubleOrderedMap.FastOrderedSet { + @Override + public void addFirst(Long2DoubleMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Long2DoubleMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Long2DoubleMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -836,7 +843,7 @@ public class Long2DoubleLinkedOpenCustomHashMap extends Long2DoubleOpenCustomHas } @Override - public Long2DoubleMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Long2DoubleMap.Entry state = null; boolean empty = true; @@ -851,21 +858,21 @@ public class Long2DoubleLinkedOpenCustomHashMap extends Long2DoubleOpenCustomHas state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Long2DoubleMap.Entry 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 @@ -944,10 +951,13 @@ public class Long2DoubleLinkedOpenCustomHashMap extends Long2DoubleOpenCustomHas public boolean add(long o) { throw new UnsupportedOperationException(); } - + + @Override + public void addFirst(long o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(long o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(long o) { throw new UnsupportedOperationException(); } - @Override public boolean addAndMoveToLast(long o) { throw new UnsupportedOperationException(); } @@ -1090,7 +1100,7 @@ public class Long2DoubleLinkedOpenCustomHashMap extends Long2DoubleOpenCustomHas } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -1105,19 +1115,19 @@ public class Long2DoubleLinkedOpenCustomHashMap extends Long2DoubleOpenCustomHas state = operator.applyAsLong(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0L; + if(size() <= 0) return OptionalLong.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalLong.of(keys[index]); index = (int)links[index]; } - return 0L; + return OptionalLong.empty(); } @Override @@ -1252,7 +1262,7 @@ public class Long2DoubleLinkedOpenCustomHashMap extends Long2DoubleOpenCustomHas } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -1267,19 +1277,19 @@ public class Long2DoubleLinkedOpenCustomHashMap extends Long2DoubleOpenCustomHas state = operator.applyAsDouble(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0D; + if(size() <= 0) return OptionalDouble.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalDouble.of(values[index]); index = (int)links[index]; } - return 0D; + return OptionalDouble.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/longs/maps/impl/customHash/Long2DoubleOpenCustomHashMap.java b/src/main/java/speiger/src/collections/longs/maps/impl/customHash/Long2DoubleOpenCustomHashMap.java index 8e8fe03..8353f86 100644 --- a/src/main/java/speiger/src/collections/longs/maps/impl/customHash/Long2DoubleOpenCustomHashMap.java +++ b/src/main/java/speiger/src/collections/longs/maps/impl/customHash/Long2DoubleOpenCustomHashMap.java @@ -5,11 +5,14 @@ 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; import java.util.function.LongPredicate; +import java.util.OptionalLong; import java.util.function.DoublePredicate; +import java.util.OptionalDouble; import speiger.src.collections.longs.collections.LongIterator; import speiger.src.collections.longs.functions.LongConsumer; @@ -952,7 +955,7 @@ public class Long2DoubleOpenCustomHashMap extends AbstractLong2DoubleMap impleme } @Override - public Long2DoubleMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Long2DoubleMap.Entry state = null; boolean empty = true; @@ -969,25 +972,25 @@ public class Long2DoubleOpenCustomHashMap extends AbstractLong2DoubleMap impleme } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Long2DoubleMap.Entry 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], 0L)) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1161,7 +1164,7 @@ public class Long2DoubleOpenCustomHashMap extends AbstractLong2DoubleMap impleme } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -1178,18 +1181,18 @@ public class Long2DoubleOpenCustomHashMap extends AbstractLong2DoubleMap impleme } state = operator.applyAsLong(state, keys[i]); } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0L; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OptionalLong.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OptionalLong.of(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], 0L) && filter.test(keys[i])) return keys[i]; + if(!strategy.equals(keys[i], 0L) && filter.test(keys[i])) return OptionalLong.of(keys[i]); } - return 0L; + return OptionalLong.empty(); } @Override @@ -1304,7 +1307,7 @@ public class Long2DoubleOpenCustomHashMap extends AbstractLong2DoubleMap impleme } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -1321,18 +1324,18 @@ public class Long2DoubleOpenCustomHashMap extends AbstractLong2DoubleMap impleme } state = operator.applyAsDouble(state, values[i]); } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0D; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return OptionalDouble.empty(); + if(containsNull && filter.test(values[nullIndex])) return OptionalDouble.of(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], 0L) && filter.test(values[i])) return values[i]; + if(!strategy.equals(keys[i], 0L) && filter.test(values[i])) return OptionalDouble.of(values[i]); } - return 0D; + return OptionalDouble.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/longs/maps/impl/customHash/Long2FloatLinkedOpenCustomHashMap.java b/src/main/java/speiger/src/collections/longs/maps/impl/customHash/Long2FloatLinkedOpenCustomHashMap.java index fdcaf42..d96b63b 100644 --- a/src/main/java/speiger/src/collections/longs/maps/impl/customHash/Long2FloatLinkedOpenCustomHashMap.java +++ b/src/main/java/speiger/src/collections/longs/maps/impl/customHash/Long2FloatLinkedOpenCustomHashMap.java @@ -4,10 +4,12 @@ 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; import java.util.function.LongPredicate; +import java.util.OptionalLong; import speiger.src.collections.longs.collections.LongBidirectionalIterator; import speiger.src.collections.longs.functions.LongConsumer; @@ -33,6 +35,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectFloatConsumer; import speiger.src.collections.floats.functions.function.FloatPredicate; +import speiger.src.collections.floats.functions.OptionalFloat; import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOperator; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.objects.lists.ObjectListIterator; @@ -671,6 +674,10 @@ public class Long2FloatLinkedOpenCustomHashMap extends Long2FloatOpenCustomHashM } private class MapEntrySet extends AbstractObjectSet implements Long2FloatOrderedMap.FastOrderedSet { + @Override + public void addFirst(Long2FloatMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Long2FloatMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Long2FloatMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -836,7 +843,7 @@ public class Long2FloatLinkedOpenCustomHashMap extends Long2FloatOpenCustomHashM } @Override - public Long2FloatMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Long2FloatMap.Entry state = null; boolean empty = true; @@ -851,21 +858,21 @@ public class Long2FloatLinkedOpenCustomHashMap extends Long2FloatOpenCustomHashM state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Long2FloatMap.Entry 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 @@ -944,10 +951,13 @@ public class Long2FloatLinkedOpenCustomHashMap extends Long2FloatOpenCustomHashM public boolean add(long o) { throw new UnsupportedOperationException(); } - + + @Override + public void addFirst(long o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(long o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(long o) { throw new UnsupportedOperationException(); } - @Override public boolean addAndMoveToLast(long o) { throw new UnsupportedOperationException(); } @@ -1090,7 +1100,7 @@ public class Long2FloatLinkedOpenCustomHashMap extends Long2FloatOpenCustomHashM } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -1105,19 +1115,19 @@ public class Long2FloatLinkedOpenCustomHashMap extends Long2FloatOpenCustomHashM state = operator.applyAsLong(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0L; + if(size() <= 0) return OptionalLong.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalLong.of(keys[index]); index = (int)links[index]; } - return 0L; + return OptionalLong.empty(); } @Override @@ -1252,7 +1262,7 @@ public class Long2FloatLinkedOpenCustomHashMap extends Long2FloatOpenCustomHashM } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -1267,19 +1277,19 @@ public class Long2FloatLinkedOpenCustomHashMap extends Long2FloatOpenCustomHashM state = operator.applyAsFloat(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0F; + if(size() <= 0) return OptionalFloat.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalFloat.of(values[index]); index = (int)links[index]; } - return 0F; + return OptionalFloat.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/longs/maps/impl/customHash/Long2FloatOpenCustomHashMap.java b/src/main/java/speiger/src/collections/longs/maps/impl/customHash/Long2FloatOpenCustomHashMap.java index c61bd14..7e72afd 100644 --- a/src/main/java/speiger/src/collections/longs/maps/impl/customHash/Long2FloatOpenCustomHashMap.java +++ b/src/main/java/speiger/src/collections/longs/maps/impl/customHash/Long2FloatOpenCustomHashMap.java @@ -5,10 +5,12 @@ 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; import java.util.function.LongPredicate; +import java.util.OptionalLong; import speiger.src.collections.longs.collections.LongIterator; import speiger.src.collections.longs.functions.LongConsumer; @@ -37,6 +39,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectFloatConsumer; import speiger.src.collections.floats.functions.function.FloatPredicate; +import speiger.src.collections.floats.functions.OptionalFloat; import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.sets.AbstractObjectSet; import speiger.src.collections.objects.sets.ObjectSet; @@ -952,7 +955,7 @@ public class Long2FloatOpenCustomHashMap extends AbstractLong2FloatMap implement } @Override - public Long2FloatMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Long2FloatMap.Entry state = null; boolean empty = true; @@ -969,25 +972,25 @@ public class Long2FloatOpenCustomHashMap extends AbstractLong2FloatMap implement } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Long2FloatMap.Entry 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], 0L)) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1161,7 +1164,7 @@ public class Long2FloatOpenCustomHashMap extends AbstractLong2FloatMap implement } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -1178,18 +1181,18 @@ public class Long2FloatOpenCustomHashMap extends AbstractLong2FloatMap implement } state = operator.applyAsLong(state, keys[i]); } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0L; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OptionalLong.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OptionalLong.of(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], 0L) && filter.test(keys[i])) return keys[i]; + if(!strategy.equals(keys[i], 0L) && filter.test(keys[i])) return OptionalLong.of(keys[i]); } - return 0L; + return OptionalLong.empty(); } @Override @@ -1304,7 +1307,7 @@ public class Long2FloatOpenCustomHashMap extends AbstractLong2FloatMap implement } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -1321,18 +1324,18 @@ public class Long2FloatOpenCustomHashMap extends AbstractLong2FloatMap implement } state = operator.applyAsFloat(state, values[i]); } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0F; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return OptionalFloat.empty(); + if(containsNull && filter.test(values[nullIndex])) return OptionalFloat.of(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], 0L) && filter.test(values[i])) return values[i]; + if(!strategy.equals(keys[i], 0L) && filter.test(values[i])) return OptionalFloat.of(values[i]); } - return 0F; + return OptionalFloat.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/longs/maps/impl/customHash/Long2IntLinkedOpenCustomHashMap.java b/src/main/java/speiger/src/collections/longs/maps/impl/customHash/Long2IntLinkedOpenCustomHashMap.java index a1c320c..8ca0a07 100644 --- a/src/main/java/speiger/src/collections/longs/maps/impl/customHash/Long2IntLinkedOpenCustomHashMap.java +++ b/src/main/java/speiger/src/collections/longs/maps/impl/customHash/Long2IntLinkedOpenCustomHashMap.java @@ -4,11 +4,14 @@ 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; import java.util.function.LongPredicate; +import java.util.OptionalLong; import java.util.function.IntPredicate; +import java.util.OptionalInt; import speiger.src.collections.longs.collections.LongBidirectionalIterator; import speiger.src.collections.longs.functions.LongConsumer; @@ -671,6 +674,10 @@ public class Long2IntLinkedOpenCustomHashMap extends Long2IntOpenCustomHashMap i } private class MapEntrySet extends AbstractObjectSet implements Long2IntOrderedMap.FastOrderedSet { + @Override + public void addFirst(Long2IntMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Long2IntMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Long2IntMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -836,7 +843,7 @@ public class Long2IntLinkedOpenCustomHashMap extends Long2IntOpenCustomHashMap i } @Override - public Long2IntMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Long2IntMap.Entry state = null; boolean empty = true; @@ -851,21 +858,21 @@ public class Long2IntLinkedOpenCustomHashMap extends Long2IntOpenCustomHashMap i state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Long2IntMap.Entry 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 @@ -944,10 +951,13 @@ public class Long2IntLinkedOpenCustomHashMap extends Long2IntOpenCustomHashMap i public boolean add(long o) { throw new UnsupportedOperationException(); } - + + @Override + public void addFirst(long o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(long o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(long o) { throw new UnsupportedOperationException(); } - @Override public boolean addAndMoveToLast(long o) { throw new UnsupportedOperationException(); } @@ -1090,7 +1100,7 @@ public class Long2IntLinkedOpenCustomHashMap extends Long2IntOpenCustomHashMap i } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -1105,19 +1115,19 @@ public class Long2IntLinkedOpenCustomHashMap extends Long2IntOpenCustomHashMap i state = operator.applyAsLong(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0L; + if(size() <= 0) return OptionalLong.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalLong.of(keys[index]); index = (int)links[index]; } - return 0L; + return OptionalLong.empty(); } @Override @@ -1252,7 +1262,7 @@ public class Long2IntLinkedOpenCustomHashMap extends Long2IntOpenCustomHashMap i } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -1267,19 +1277,19 @@ public class Long2IntLinkedOpenCustomHashMap extends Long2IntOpenCustomHashMap i state = operator.applyAsInt(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0; + if(size() <= 0) return OptionalInt.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalInt.of(values[index]); index = (int)links[index]; } - return 0; + return OptionalInt.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/longs/maps/impl/customHash/Long2IntOpenCustomHashMap.java b/src/main/java/speiger/src/collections/longs/maps/impl/customHash/Long2IntOpenCustomHashMap.java index b8e8376..9a05395 100644 --- a/src/main/java/speiger/src/collections/longs/maps/impl/customHash/Long2IntOpenCustomHashMap.java +++ b/src/main/java/speiger/src/collections/longs/maps/impl/customHash/Long2IntOpenCustomHashMap.java @@ -5,11 +5,14 @@ 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; import java.util.function.LongPredicate; +import java.util.OptionalLong; import java.util.function.IntPredicate; +import java.util.OptionalInt; import speiger.src.collections.longs.collections.LongIterator; import speiger.src.collections.longs.functions.LongConsumer; @@ -952,7 +955,7 @@ public class Long2IntOpenCustomHashMap extends AbstractLong2IntMap implements IT } @Override - public Long2IntMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Long2IntMap.Entry state = null; boolean empty = true; @@ -969,25 +972,25 @@ public class Long2IntOpenCustomHashMap extends AbstractLong2IntMap implements IT } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Long2IntMap.Entry 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], 0L)) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1161,7 +1164,7 @@ public class Long2IntOpenCustomHashMap extends AbstractLong2IntMap implements IT } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -1178,18 +1181,18 @@ public class Long2IntOpenCustomHashMap extends AbstractLong2IntMap implements IT } state = operator.applyAsLong(state, keys[i]); } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0L; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OptionalLong.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OptionalLong.of(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], 0L) && filter.test(keys[i])) return keys[i]; + if(!strategy.equals(keys[i], 0L) && filter.test(keys[i])) return OptionalLong.of(keys[i]); } - return 0L; + return OptionalLong.empty(); } @Override @@ -1304,7 +1307,7 @@ public class Long2IntOpenCustomHashMap extends AbstractLong2IntMap implements IT } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -1321,18 +1324,18 @@ public class Long2IntOpenCustomHashMap extends AbstractLong2IntMap implements IT } state = operator.applyAsInt(state, values[i]); } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return OptionalInt.empty(); + if(containsNull && filter.test(values[nullIndex])) return OptionalInt.of(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], 0L) && filter.test(values[i])) return values[i]; + if(!strategy.equals(keys[i], 0L) && filter.test(values[i])) return OptionalInt.of(values[i]); } - return 0; + return OptionalInt.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/longs/maps/impl/customHash/Long2LongLinkedOpenCustomHashMap.java b/src/main/java/speiger/src/collections/longs/maps/impl/customHash/Long2LongLinkedOpenCustomHashMap.java index 79c24ad..445972b 100644 --- a/src/main/java/speiger/src/collections/longs/maps/impl/customHash/Long2LongLinkedOpenCustomHashMap.java +++ b/src/main/java/speiger/src/collections/longs/maps/impl/customHash/Long2LongLinkedOpenCustomHashMap.java @@ -4,10 +4,12 @@ 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; import java.util.function.LongPredicate; +import java.util.OptionalLong; import speiger.src.collections.longs.collections.LongBidirectionalIterator; import speiger.src.collections.longs.functions.LongConsumer; @@ -664,6 +666,10 @@ public class Long2LongLinkedOpenCustomHashMap extends Long2LongOpenCustomHashMap } private class MapEntrySet extends AbstractObjectSet implements Long2LongOrderedMap.FastOrderedSet { + @Override + public void addFirst(Long2LongMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Long2LongMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Long2LongMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -829,7 +835,7 @@ public class Long2LongLinkedOpenCustomHashMap extends Long2LongOpenCustomHashMap } @Override - public Long2LongMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Long2LongMap.Entry state = null; boolean empty = true; @@ -844,21 +850,21 @@ public class Long2LongLinkedOpenCustomHashMap extends Long2LongOpenCustomHashMap state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Long2LongMap.Entry 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 @@ -937,10 +943,13 @@ public class Long2LongLinkedOpenCustomHashMap extends Long2LongOpenCustomHashMap public boolean add(long o) { throw new UnsupportedOperationException(); } - + + @Override + public void addFirst(long o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(long o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(long o) { throw new UnsupportedOperationException(); } - @Override public boolean addAndMoveToLast(long o) { throw new UnsupportedOperationException(); } @@ -1083,7 +1092,7 @@ public class Long2LongLinkedOpenCustomHashMap extends Long2LongOpenCustomHashMap } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -1098,19 +1107,19 @@ public class Long2LongLinkedOpenCustomHashMap extends Long2LongOpenCustomHashMap state = operator.applyAsLong(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0L; + if(size() <= 0) return OptionalLong.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalLong.of(keys[index]); index = (int)links[index]; } - return 0L; + return OptionalLong.empty(); } @Override @@ -1245,7 +1254,7 @@ public class Long2LongLinkedOpenCustomHashMap extends Long2LongOpenCustomHashMap } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -1260,19 +1269,19 @@ public class Long2LongLinkedOpenCustomHashMap extends Long2LongOpenCustomHashMap state = operator.applyAsLong(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0L; + if(size() <= 0) return OptionalLong.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalLong.of(values[index]); index = (int)links[index]; } - return 0L; + return OptionalLong.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/longs/maps/impl/customHash/Long2LongOpenCustomHashMap.java b/src/main/java/speiger/src/collections/longs/maps/impl/customHash/Long2LongOpenCustomHashMap.java index 5e806bf..8a62fac 100644 --- a/src/main/java/speiger/src/collections/longs/maps/impl/customHash/Long2LongOpenCustomHashMap.java +++ b/src/main/java/speiger/src/collections/longs/maps/impl/customHash/Long2LongOpenCustomHashMap.java @@ -5,10 +5,12 @@ 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; import java.util.function.LongPredicate; +import java.util.OptionalLong; import speiger.src.collections.longs.collections.LongIterator; import speiger.src.collections.longs.functions.LongConsumer; @@ -944,7 +946,7 @@ public class Long2LongOpenCustomHashMap extends AbstractLong2LongMap implements } @Override - public Long2LongMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Long2LongMap.Entry state = null; boolean empty = true; @@ -961,25 +963,25 @@ public class Long2LongOpenCustomHashMap extends AbstractLong2LongMap implements } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Long2LongMap.Entry 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], 0L)) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1153,7 +1155,7 @@ public class Long2LongOpenCustomHashMap extends AbstractLong2LongMap implements } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -1170,18 +1172,18 @@ public class Long2LongOpenCustomHashMap extends AbstractLong2LongMap implements } state = operator.applyAsLong(state, keys[i]); } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0L; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OptionalLong.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OptionalLong.of(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], 0L) && filter.test(keys[i])) return keys[i]; + if(!strategy.equals(keys[i], 0L) && filter.test(keys[i])) return OptionalLong.of(keys[i]); } - return 0L; + return OptionalLong.empty(); } @Override @@ -1296,7 +1298,7 @@ public class Long2LongOpenCustomHashMap extends AbstractLong2LongMap implements } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -1313,18 +1315,18 @@ public class Long2LongOpenCustomHashMap extends AbstractLong2LongMap implements } state = operator.applyAsLong(state, values[i]); } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0L; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return OptionalLong.empty(); + if(containsNull && filter.test(values[nullIndex])) return OptionalLong.of(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], 0L) && filter.test(values[i])) return values[i]; + if(!strategy.equals(keys[i], 0L) && filter.test(values[i])) return OptionalLong.of(values[i]); } - return 0L; + return OptionalLong.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/longs/maps/impl/customHash/Long2ObjectLinkedOpenCustomHashMap.java b/src/main/java/speiger/src/collections/longs/maps/impl/customHash/Long2ObjectLinkedOpenCustomHashMap.java index cdf7aef..54f8c16 100644 --- a/src/main/java/speiger/src/collections/longs/maps/impl/customHash/Long2ObjectLinkedOpenCustomHashMap.java +++ b/src/main/java/speiger/src/collections/longs/maps/impl/customHash/Long2ObjectLinkedOpenCustomHashMap.java @@ -4,10 +4,12 @@ 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; import java.util.function.LongPredicate; +import java.util.OptionalLong; import speiger.src.collections.longs.collections.LongBidirectionalIterator; import speiger.src.collections.longs.functions.LongConsumer; @@ -666,6 +668,10 @@ public class Long2ObjectLinkedOpenCustomHashMap extends Long2ObjectOpenCustom } private class MapEntrySet extends AbstractObjectSet> implements Long2ObjectOrderedMap.FastOrderedSet { + @Override + public void addFirst(Long2ObjectMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Long2ObjectMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Long2ObjectMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -831,7 +837,7 @@ public class Long2ObjectLinkedOpenCustomHashMap extends Long2ObjectOpenCustom } @Override - public Long2ObjectMap.Entry reduce(ObjectObjectUnaryOperator, Long2ObjectMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Long2ObjectMap.Entry> operator) { Objects.requireNonNull(operator); Long2ObjectMap.Entry state = null; boolean empty = true; @@ -846,21 +852,21 @@ public class Long2ObjectLinkedOpenCustomHashMap extends Long2ObjectOpenCustom state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Long2ObjectMap.Entry 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 @@ -939,10 +945,13 @@ public class Long2ObjectLinkedOpenCustomHashMap extends Long2ObjectOpenCustom public boolean add(long o) { throw new UnsupportedOperationException(); } - + + @Override + public void addFirst(long o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(long o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(long o) { throw new UnsupportedOperationException(); } - @Override public boolean addAndMoveToLast(long o) { throw new UnsupportedOperationException(); } @@ -1085,7 +1094,7 @@ public class Long2ObjectLinkedOpenCustomHashMap extends Long2ObjectOpenCustom } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -1100,19 +1109,19 @@ public class Long2ObjectLinkedOpenCustomHashMap extends Long2ObjectOpenCustom state = operator.applyAsLong(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0L; + if(size() <= 0) return OptionalLong.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalLong.of(keys[index]); index = (int)links[index]; } - return 0L; + return OptionalLong.empty(); } @Override @@ -1248,7 +1257,7 @@ public class Long2ObjectLinkedOpenCustomHashMap extends Long2ObjectOpenCustom } @Override - public V reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); V state = null; boolean empty = true; @@ -1263,19 +1272,19 @@ public class Long2ObjectLinkedOpenCustomHashMap extends Long2ObjectOpenCustom state = operator.apply(state, values[index]); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public V findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return Optional.ofNullable(values[index]); index = (int)links[index]; } - return null; + return Optional.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/longs/maps/impl/customHash/Long2ObjectOpenCustomHashMap.java b/src/main/java/speiger/src/collections/longs/maps/impl/customHash/Long2ObjectOpenCustomHashMap.java index 9dcae59..94a6eae 100644 --- a/src/main/java/speiger/src/collections/longs/maps/impl/customHash/Long2ObjectOpenCustomHashMap.java +++ b/src/main/java/speiger/src/collections/longs/maps/impl/customHash/Long2ObjectOpenCustomHashMap.java @@ -5,10 +5,12 @@ 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; import java.util.function.LongPredicate; +import java.util.OptionalLong; import speiger.src.collections.longs.collections.LongIterator; import speiger.src.collections.longs.functions.LongConsumer; @@ -868,7 +870,7 @@ public class Long2ObjectOpenCustomHashMap extends AbstractLong2ObjectMap i } @Override - public Long2ObjectMap.Entry reduce(ObjectObjectUnaryOperator, Long2ObjectMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Long2ObjectMap.Entry> operator) { Objects.requireNonNull(operator); Long2ObjectMap.Entry state = null; boolean empty = true; @@ -885,25 +887,25 @@ public class Long2ObjectOpenCustomHashMap extends AbstractLong2ObjectMap i } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Long2ObjectMap.Entry 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], 0L)) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1077,7 +1079,7 @@ public class Long2ObjectOpenCustomHashMap extends AbstractLong2ObjectMap i } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -1094,18 +1096,18 @@ public class Long2ObjectOpenCustomHashMap extends AbstractLong2ObjectMap i } state = operator.applyAsLong(state, keys[i]); } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0L; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OptionalLong.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OptionalLong.of(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], 0L) && filter.test(keys[i])) return keys[i]; + if(!strategy.equals(keys[i], 0L) && filter.test(keys[i])) return OptionalLong.of(keys[i]); } - return 0L; + return OptionalLong.empty(); } @Override @@ -1220,7 +1222,7 @@ public class Long2ObjectOpenCustomHashMap extends AbstractLong2ObjectMap i } @Override - public V reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); V state = null; boolean empty = true; @@ -1237,18 +1239,18 @@ public class Long2ObjectOpenCustomHashMap extends AbstractLong2ObjectMap i } state = operator.apply(state, values[i]); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public V findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return Optional.empty(); + if(containsNull && filter.test(values[nullIndex])) return Optional.ofNullable(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], 0L) && filter.test(values[i])) return values[i]; + if(!strategy.equals(keys[i], 0L) && filter.test(values[i])) return Optional.ofNullable(values[i]); } - return null; + return Optional.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/longs/maps/impl/customHash/Long2ShortLinkedOpenCustomHashMap.java b/src/main/java/speiger/src/collections/longs/maps/impl/customHash/Long2ShortLinkedOpenCustomHashMap.java index 8283cba..79b2870 100644 --- a/src/main/java/speiger/src/collections/longs/maps/impl/customHash/Long2ShortLinkedOpenCustomHashMap.java +++ b/src/main/java/speiger/src/collections/longs/maps/impl/customHash/Long2ShortLinkedOpenCustomHashMap.java @@ -4,10 +4,12 @@ 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; import java.util.function.LongPredicate; +import java.util.OptionalLong; import speiger.src.collections.longs.collections.LongBidirectionalIterator; import speiger.src.collections.longs.functions.LongConsumer; @@ -33,6 +35,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectShortConsumer; import speiger.src.collections.shorts.functions.function.ShortPredicate; +import speiger.src.collections.shorts.functions.OptionalShort; import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOperator; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.objects.lists.ObjectListIterator; @@ -671,6 +674,10 @@ public class Long2ShortLinkedOpenCustomHashMap extends Long2ShortOpenCustomHashM } private class MapEntrySet extends AbstractObjectSet implements Long2ShortOrderedMap.FastOrderedSet { + @Override + public void addFirst(Long2ShortMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Long2ShortMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Long2ShortMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -836,7 +843,7 @@ public class Long2ShortLinkedOpenCustomHashMap extends Long2ShortOpenCustomHashM } @Override - public Long2ShortMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Long2ShortMap.Entry state = null; boolean empty = true; @@ -851,21 +858,21 @@ public class Long2ShortLinkedOpenCustomHashMap extends Long2ShortOpenCustomHashM state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Long2ShortMap.Entry 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 @@ -944,10 +951,13 @@ public class Long2ShortLinkedOpenCustomHashMap extends Long2ShortOpenCustomHashM public boolean add(long o) { throw new UnsupportedOperationException(); } - + + @Override + public void addFirst(long o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(long o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(long o) { throw new UnsupportedOperationException(); } - @Override public boolean addAndMoveToLast(long o) { throw new UnsupportedOperationException(); } @@ -1090,7 +1100,7 @@ public class Long2ShortLinkedOpenCustomHashMap extends Long2ShortOpenCustomHashM } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -1105,19 +1115,19 @@ public class Long2ShortLinkedOpenCustomHashMap extends Long2ShortOpenCustomHashM state = operator.applyAsLong(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0L; + if(size() <= 0) return OptionalLong.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalLong.of(keys[index]); index = (int)links[index]; } - return 0L; + return OptionalLong.empty(); } @Override @@ -1252,7 +1262,7 @@ public class Long2ShortLinkedOpenCustomHashMap extends Long2ShortOpenCustomHashM } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -1267,19 +1277,19 @@ public class Long2ShortLinkedOpenCustomHashMap extends Long2ShortOpenCustomHashM state = operator.applyAsShort(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (short)0; + if(size() <= 0) return OptionalShort.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalShort.of(values[index]); index = (int)links[index]; } - return (short)0; + return OptionalShort.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/longs/maps/impl/customHash/Long2ShortOpenCustomHashMap.java b/src/main/java/speiger/src/collections/longs/maps/impl/customHash/Long2ShortOpenCustomHashMap.java index 244d63a..a86a8bc 100644 --- a/src/main/java/speiger/src/collections/longs/maps/impl/customHash/Long2ShortOpenCustomHashMap.java +++ b/src/main/java/speiger/src/collections/longs/maps/impl/customHash/Long2ShortOpenCustomHashMap.java @@ -5,10 +5,12 @@ 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; import java.util.function.LongPredicate; +import java.util.OptionalLong; import speiger.src.collections.longs.collections.LongIterator; import speiger.src.collections.longs.functions.LongConsumer; @@ -37,6 +39,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectShortConsumer; import speiger.src.collections.shorts.functions.function.ShortPredicate; +import speiger.src.collections.shorts.functions.OptionalShort; import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.sets.AbstractObjectSet; import speiger.src.collections.objects.sets.ObjectSet; @@ -952,7 +955,7 @@ public class Long2ShortOpenCustomHashMap extends AbstractLong2ShortMap implement } @Override - public Long2ShortMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Long2ShortMap.Entry state = null; boolean empty = true; @@ -969,25 +972,25 @@ public class Long2ShortOpenCustomHashMap extends AbstractLong2ShortMap implement } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Long2ShortMap.Entry 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], 0L)) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1161,7 +1164,7 @@ public class Long2ShortOpenCustomHashMap extends AbstractLong2ShortMap implement } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -1178,18 +1181,18 @@ public class Long2ShortOpenCustomHashMap extends AbstractLong2ShortMap implement } state = operator.applyAsLong(state, keys[i]); } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0L; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OptionalLong.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OptionalLong.of(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], 0L) && filter.test(keys[i])) return keys[i]; + if(!strategy.equals(keys[i], 0L) && filter.test(keys[i])) return OptionalLong.of(keys[i]); } - return 0L; + return OptionalLong.empty(); } @Override @@ -1304,7 +1307,7 @@ public class Long2ShortOpenCustomHashMap extends AbstractLong2ShortMap implement } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -1321,18 +1324,18 @@ public class Long2ShortOpenCustomHashMap extends AbstractLong2ShortMap implement } state = operator.applyAsShort(state, values[i]); } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (short)0; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return OptionalShort.empty(); + if(containsNull && filter.test(values[nullIndex])) return OptionalShort.of(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], 0L) && filter.test(values[i])) return values[i]; + if(!strategy.equals(keys[i], 0L) && filter.test(values[i])) return OptionalShort.of(values[i]); } - return (short)0; + return OptionalShort.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/longs/maps/impl/hash/Long2BooleanLinkedOpenHashMap.java b/src/main/java/speiger/src/collections/longs/maps/impl/hash/Long2BooleanLinkedOpenHashMap.java index 3b4d6b2..0e04647 100644 --- a/src/main/java/speiger/src/collections/longs/maps/impl/hash/Long2BooleanLinkedOpenHashMap.java +++ b/src/main/java/speiger/src/collections/longs/maps/impl/hash/Long2BooleanLinkedOpenHashMap.java @@ -4,10 +4,12 @@ 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; import java.util.function.LongPredicate; +import java.util.OptionalLong; import speiger.src.collections.longs.collections.LongBidirectionalIterator; import speiger.src.collections.longs.functions.LongConsumer; @@ -32,6 +34,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectBooleanConsumer; import speiger.src.collections.booleans.functions.function.BooleanPredicate; +import speiger.src.collections.booleans.functions.OptionalBoolean; import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOperator; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.objects.lists.ObjectListIterator; @@ -669,6 +672,10 @@ public class Long2BooleanLinkedOpenHashMap extends Long2BooleanOpenHashMap imple } private class MapEntrySet extends AbstractObjectSet implements Long2BooleanOrderedMap.FastOrderedSet { + @Override + public void addFirst(Long2BooleanMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Long2BooleanMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Long2BooleanMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -834,7 +841,7 @@ public class Long2BooleanLinkedOpenHashMap extends Long2BooleanOpenHashMap imple } @Override - public Long2BooleanMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Long2BooleanMap.Entry state = null; boolean empty = true; @@ -849,21 +856,21 @@ public class Long2BooleanLinkedOpenHashMap extends Long2BooleanOpenHashMap imple state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Long2BooleanMap.Entry 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 @@ -939,6 +946,12 @@ public class Long2BooleanLinkedOpenHashMap extends Long2BooleanOpenHashMap imple @Override public boolean add(long o) { throw new UnsupportedOperationException(); } + + @Override + public void addFirst(long o) { throw new UnsupportedOperationException(); } + + @Override + public void addLast(long o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(long o) { throw new UnsupportedOperationException(); } @@ -1085,7 +1098,7 @@ public class Long2BooleanLinkedOpenHashMap extends Long2BooleanOpenHashMap imple } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -1100,19 +1113,19 @@ public class Long2BooleanLinkedOpenHashMap extends Long2BooleanOpenHashMap imple state = operator.applyAsLong(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0L; + if(size() <= 0) return OptionalLong.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalLong.of(keys[index]); index = (int)links[index]; } - return 0L; + return OptionalLong.empty(); } @Override @@ -1248,7 +1261,7 @@ public class Long2BooleanLinkedOpenHashMap extends Long2BooleanOpenHashMap imple } @Override - public boolean reduce(BooleanBooleanUnaryOperator operator) { + public OptionalBoolean reduce(BooleanBooleanUnaryOperator operator) { Objects.requireNonNull(operator); boolean state = false; boolean empty = true; @@ -1263,19 +1276,19 @@ public class Long2BooleanLinkedOpenHashMap extends Long2BooleanOpenHashMap imple state = operator.applyAsBoolean(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalBoolean.empty() : OptionalBoolean.of(state); } @Override - public boolean findFirst(BooleanPredicate filter) { + public OptionalBoolean findFirst(BooleanPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return false; + if(size() <= 0) return OptionalBoolean.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalBoolean.of(values[index]); index = (int)links[index]; } - return false; + return OptionalBoolean.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/longs/maps/impl/hash/Long2BooleanOpenHashMap.java b/src/main/java/speiger/src/collections/longs/maps/impl/hash/Long2BooleanOpenHashMap.java index a74edb3..3333198 100644 --- a/src/main/java/speiger/src/collections/longs/maps/impl/hash/Long2BooleanOpenHashMap.java +++ b/src/main/java/speiger/src/collections/longs/maps/impl/hash/Long2BooleanOpenHashMap.java @@ -5,10 +5,12 @@ 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; import java.util.function.LongPredicate; +import java.util.OptionalLong; import speiger.src.collections.longs.collections.LongIterator; import speiger.src.collections.longs.functions.LongConsumer; @@ -33,6 +35,7 @@ import speiger.src.collections.booleans.functions.BooleanConsumer; import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectBooleanConsumer; import speiger.src.collections.booleans.functions.function.BooleanPredicate; +import speiger.src.collections.booleans.functions.OptionalBoolean; import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.sets.AbstractObjectSet; import speiger.src.collections.objects.sets.ObjectSet; @@ -898,7 +901,7 @@ public class Long2BooleanOpenHashMap extends AbstractLong2BooleanMap implements } @Override - public Long2BooleanMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Long2BooleanMap.Entry state = null; boolean empty = true; @@ -915,25 +918,25 @@ public class Long2BooleanOpenHashMap extends AbstractLong2BooleanMap implements } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Long2BooleanMap.Entry 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(keys[i] != 0) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1105,7 +1108,7 @@ public class Long2BooleanOpenHashMap extends AbstractLong2BooleanMap implements } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -1122,18 +1125,18 @@ public class Long2BooleanOpenHashMap extends AbstractLong2BooleanMap implements } state = operator.applyAsLong(state, keys[i]); } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0L; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OptionalLong.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OptionalLong.of(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(keys[i] != 0 && filter.test(keys[i])) return keys[i]; + if(keys[i] != 0 && filter.test(keys[i])) return OptionalLong.of(keys[i]); } - return 0L; + return OptionalLong.empty(); } @Override @@ -1248,7 +1251,7 @@ public class Long2BooleanOpenHashMap extends AbstractLong2BooleanMap implements } @Override - public boolean reduce(BooleanBooleanUnaryOperator operator) { + public OptionalBoolean reduce(BooleanBooleanUnaryOperator operator) { Objects.requireNonNull(operator); boolean state = false; boolean empty = true; @@ -1265,18 +1268,18 @@ public class Long2BooleanOpenHashMap extends AbstractLong2BooleanMap implements } state = operator.applyAsBoolean(state, values[i]); } - return state; + return empty ? OptionalBoolean.empty() : OptionalBoolean.of(state); } @Override - public boolean findFirst(BooleanPredicate filter) { + public OptionalBoolean findFirst(BooleanPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return false; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return OptionalBoolean.empty(); + if(containsNull && filter.test(values[nullIndex])) return OptionalBoolean.of(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(keys[i] != 0 && filter.test(values[i])) return values[i]; + if(keys[i] != 0 && filter.test(values[i])) return OptionalBoolean.of(values[i]); } - return false; + return OptionalBoolean.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/longs/maps/impl/hash/Long2ByteLinkedOpenHashMap.java b/src/main/java/speiger/src/collections/longs/maps/impl/hash/Long2ByteLinkedOpenHashMap.java index 79b716d..894196b 100644 --- a/src/main/java/speiger/src/collections/longs/maps/impl/hash/Long2ByteLinkedOpenHashMap.java +++ b/src/main/java/speiger/src/collections/longs/maps/impl/hash/Long2ByteLinkedOpenHashMap.java @@ -4,10 +4,12 @@ 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; import java.util.function.LongPredicate; +import java.util.OptionalLong; import speiger.src.collections.longs.collections.LongBidirectionalIterator; import speiger.src.collections.longs.functions.LongConsumer; @@ -32,6 +34,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectByteConsumer; import speiger.src.collections.bytes.functions.function.BytePredicate; +import speiger.src.collections.bytes.functions.OptionalByte; import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOperator; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.objects.lists.ObjectListIterator; @@ -669,6 +672,10 @@ public class Long2ByteLinkedOpenHashMap extends Long2ByteOpenHashMap implements } private class MapEntrySet extends AbstractObjectSet implements Long2ByteOrderedMap.FastOrderedSet { + @Override + public void addFirst(Long2ByteMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Long2ByteMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Long2ByteMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -834,7 +841,7 @@ public class Long2ByteLinkedOpenHashMap extends Long2ByteOpenHashMap implements } @Override - public Long2ByteMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Long2ByteMap.Entry state = null; boolean empty = true; @@ -849,21 +856,21 @@ public class Long2ByteLinkedOpenHashMap extends Long2ByteOpenHashMap implements state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Long2ByteMap.Entry 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 @@ -939,6 +946,12 @@ public class Long2ByteLinkedOpenHashMap extends Long2ByteOpenHashMap implements @Override public boolean add(long o) { throw new UnsupportedOperationException(); } + + @Override + public void addFirst(long o) { throw new UnsupportedOperationException(); } + + @Override + public void addLast(long o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(long o) { throw new UnsupportedOperationException(); } @@ -1085,7 +1098,7 @@ public class Long2ByteLinkedOpenHashMap extends Long2ByteOpenHashMap implements } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -1100,19 +1113,19 @@ public class Long2ByteLinkedOpenHashMap extends Long2ByteOpenHashMap implements state = operator.applyAsLong(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0L; + if(size() <= 0) return OptionalLong.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalLong.of(keys[index]); index = (int)links[index]; } - return 0L; + return OptionalLong.empty(); } @Override @@ -1248,7 +1261,7 @@ public class Long2ByteLinkedOpenHashMap extends Long2ByteOpenHashMap implements } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -1263,19 +1276,19 @@ public class Long2ByteLinkedOpenHashMap extends Long2ByteOpenHashMap implements state = operator.applyAsByte(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (byte)0; + if(size() <= 0) return OptionalByte.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalByte.of(values[index]); index = (int)links[index]; } - return (byte)0; + return OptionalByte.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/longs/maps/impl/hash/Long2ByteOpenHashMap.java b/src/main/java/speiger/src/collections/longs/maps/impl/hash/Long2ByteOpenHashMap.java index 4426acd..5c98155 100644 --- a/src/main/java/speiger/src/collections/longs/maps/impl/hash/Long2ByteOpenHashMap.java +++ b/src/main/java/speiger/src/collections/longs/maps/impl/hash/Long2ByteOpenHashMap.java @@ -5,10 +5,12 @@ 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; import java.util.function.LongPredicate; +import java.util.OptionalLong; import speiger.src.collections.longs.collections.LongIterator; import speiger.src.collections.longs.functions.LongConsumer; @@ -34,6 +36,7 @@ import speiger.src.collections.bytes.functions.ByteConsumer; import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectByteConsumer; import speiger.src.collections.bytes.functions.function.BytePredicate; +import speiger.src.collections.bytes.functions.OptionalByte; import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.sets.AbstractObjectSet; import speiger.src.collections.objects.sets.ObjectSet; @@ -920,7 +923,7 @@ public class Long2ByteOpenHashMap extends AbstractLong2ByteMap implements ITrimm } @Override - public Long2ByteMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Long2ByteMap.Entry state = null; boolean empty = true; @@ -937,25 +940,25 @@ public class Long2ByteOpenHashMap extends AbstractLong2ByteMap implements ITrimm } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Long2ByteMap.Entry 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(keys[i] != 0) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1127,7 +1130,7 @@ public class Long2ByteOpenHashMap extends AbstractLong2ByteMap implements ITrimm } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -1144,18 +1147,18 @@ public class Long2ByteOpenHashMap extends AbstractLong2ByteMap implements ITrimm } state = operator.applyAsLong(state, keys[i]); } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0L; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OptionalLong.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OptionalLong.of(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(keys[i] != 0 && filter.test(keys[i])) return keys[i]; + if(keys[i] != 0 && filter.test(keys[i])) return OptionalLong.of(keys[i]); } - return 0L; + return OptionalLong.empty(); } @Override @@ -1270,7 +1273,7 @@ public class Long2ByteOpenHashMap extends AbstractLong2ByteMap implements ITrimm } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -1287,18 +1290,18 @@ public class Long2ByteOpenHashMap extends AbstractLong2ByteMap implements ITrimm } state = operator.applyAsByte(state, values[i]); } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (byte)0; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return OptionalByte.empty(); + if(containsNull && filter.test(values[nullIndex])) return OptionalByte.of(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(keys[i] != 0 && filter.test(values[i])) return values[i]; + if(keys[i] != 0 && filter.test(values[i])) return OptionalByte.of(values[i]); } - return (byte)0; + return OptionalByte.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/longs/maps/impl/hash/Long2CharLinkedOpenHashMap.java b/src/main/java/speiger/src/collections/longs/maps/impl/hash/Long2CharLinkedOpenHashMap.java index c1becaf..de71fca 100644 --- a/src/main/java/speiger/src/collections/longs/maps/impl/hash/Long2CharLinkedOpenHashMap.java +++ b/src/main/java/speiger/src/collections/longs/maps/impl/hash/Long2CharLinkedOpenHashMap.java @@ -4,10 +4,12 @@ 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; import java.util.function.LongPredicate; +import java.util.OptionalLong; import speiger.src.collections.longs.collections.LongBidirectionalIterator; import speiger.src.collections.longs.functions.LongConsumer; @@ -32,6 +34,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectCharConsumer; import speiger.src.collections.chars.functions.function.CharPredicate; +import speiger.src.collections.chars.functions.OptionalChar; import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOperator; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.objects.lists.ObjectListIterator; @@ -669,6 +672,10 @@ public class Long2CharLinkedOpenHashMap extends Long2CharOpenHashMap implements } private class MapEntrySet extends AbstractObjectSet implements Long2CharOrderedMap.FastOrderedSet { + @Override + public void addFirst(Long2CharMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Long2CharMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Long2CharMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -834,7 +841,7 @@ public class Long2CharLinkedOpenHashMap extends Long2CharOpenHashMap implements } @Override - public Long2CharMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Long2CharMap.Entry state = null; boolean empty = true; @@ -849,21 +856,21 @@ public class Long2CharLinkedOpenHashMap extends Long2CharOpenHashMap implements state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Long2CharMap.Entry 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 @@ -939,6 +946,12 @@ public class Long2CharLinkedOpenHashMap extends Long2CharOpenHashMap implements @Override public boolean add(long o) { throw new UnsupportedOperationException(); } + + @Override + public void addFirst(long o) { throw new UnsupportedOperationException(); } + + @Override + public void addLast(long o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(long o) { throw new UnsupportedOperationException(); } @@ -1085,7 +1098,7 @@ public class Long2CharLinkedOpenHashMap extends Long2CharOpenHashMap implements } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -1100,19 +1113,19 @@ public class Long2CharLinkedOpenHashMap extends Long2CharOpenHashMap implements state = operator.applyAsLong(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0L; + if(size() <= 0) return OptionalLong.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalLong.of(keys[index]); index = (int)links[index]; } - return 0L; + return OptionalLong.empty(); } @Override @@ -1248,7 +1261,7 @@ public class Long2CharLinkedOpenHashMap extends Long2CharOpenHashMap implements } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -1263,19 +1276,19 @@ public class Long2CharLinkedOpenHashMap extends Long2CharOpenHashMap implements state = operator.applyAsChar(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (char)0; + if(size() <= 0) return OptionalChar.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalChar.of(values[index]); index = (int)links[index]; } - return (char)0; + return OptionalChar.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/longs/maps/impl/hash/Long2CharOpenHashMap.java b/src/main/java/speiger/src/collections/longs/maps/impl/hash/Long2CharOpenHashMap.java index 0a40b2a..ab682e0 100644 --- a/src/main/java/speiger/src/collections/longs/maps/impl/hash/Long2CharOpenHashMap.java +++ b/src/main/java/speiger/src/collections/longs/maps/impl/hash/Long2CharOpenHashMap.java @@ -5,10 +5,12 @@ 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; import java.util.function.LongPredicate; +import java.util.OptionalLong; import speiger.src.collections.longs.collections.LongIterator; import speiger.src.collections.longs.functions.LongConsumer; @@ -34,6 +36,7 @@ import speiger.src.collections.chars.functions.CharConsumer; import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectCharConsumer; import speiger.src.collections.chars.functions.function.CharPredicate; +import speiger.src.collections.chars.functions.OptionalChar; import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.sets.AbstractObjectSet; import speiger.src.collections.objects.sets.ObjectSet; @@ -920,7 +923,7 @@ public class Long2CharOpenHashMap extends AbstractLong2CharMap implements ITrimm } @Override - public Long2CharMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Long2CharMap.Entry state = null; boolean empty = true; @@ -937,25 +940,25 @@ public class Long2CharOpenHashMap extends AbstractLong2CharMap implements ITrimm } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Long2CharMap.Entry 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(keys[i] != 0) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1127,7 +1130,7 @@ public class Long2CharOpenHashMap extends AbstractLong2CharMap implements ITrimm } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -1144,18 +1147,18 @@ public class Long2CharOpenHashMap extends AbstractLong2CharMap implements ITrimm } state = operator.applyAsLong(state, keys[i]); } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0L; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OptionalLong.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OptionalLong.of(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(keys[i] != 0 && filter.test(keys[i])) return keys[i]; + if(keys[i] != 0 && filter.test(keys[i])) return OptionalLong.of(keys[i]); } - return 0L; + return OptionalLong.empty(); } @Override @@ -1270,7 +1273,7 @@ public class Long2CharOpenHashMap extends AbstractLong2CharMap implements ITrimm } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -1287,18 +1290,18 @@ public class Long2CharOpenHashMap extends AbstractLong2CharMap implements ITrimm } state = operator.applyAsChar(state, values[i]); } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (char)0; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return OptionalChar.empty(); + if(containsNull && filter.test(values[nullIndex])) return OptionalChar.of(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(keys[i] != 0 && filter.test(values[i])) return values[i]; + if(keys[i] != 0 && filter.test(values[i])) return OptionalChar.of(values[i]); } - return (char)0; + return OptionalChar.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/longs/maps/impl/hash/Long2DoubleLinkedOpenHashMap.java b/src/main/java/speiger/src/collections/longs/maps/impl/hash/Long2DoubleLinkedOpenHashMap.java index 7c9a676..30e5b7d 100644 --- a/src/main/java/speiger/src/collections/longs/maps/impl/hash/Long2DoubleLinkedOpenHashMap.java +++ b/src/main/java/speiger/src/collections/longs/maps/impl/hash/Long2DoubleLinkedOpenHashMap.java @@ -4,11 +4,14 @@ 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; import java.util.function.LongPredicate; +import java.util.OptionalLong; import java.util.function.DoublePredicate; +import java.util.OptionalDouble; import speiger.src.collections.longs.collections.LongBidirectionalIterator; import speiger.src.collections.longs.functions.LongConsumer; @@ -669,6 +672,10 @@ public class Long2DoubleLinkedOpenHashMap extends Long2DoubleOpenHashMap impleme } private class MapEntrySet extends AbstractObjectSet implements Long2DoubleOrderedMap.FastOrderedSet { + @Override + public void addFirst(Long2DoubleMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Long2DoubleMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Long2DoubleMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -834,7 +841,7 @@ public class Long2DoubleLinkedOpenHashMap extends Long2DoubleOpenHashMap impleme } @Override - public Long2DoubleMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Long2DoubleMap.Entry state = null; boolean empty = true; @@ -849,21 +856,21 @@ public class Long2DoubleLinkedOpenHashMap extends Long2DoubleOpenHashMap impleme state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Long2DoubleMap.Entry 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 @@ -939,6 +946,12 @@ public class Long2DoubleLinkedOpenHashMap extends Long2DoubleOpenHashMap impleme @Override public boolean add(long o) { throw new UnsupportedOperationException(); } + + @Override + public void addFirst(long o) { throw new UnsupportedOperationException(); } + + @Override + public void addLast(long o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(long o) { throw new UnsupportedOperationException(); } @@ -1085,7 +1098,7 @@ public class Long2DoubleLinkedOpenHashMap extends Long2DoubleOpenHashMap impleme } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -1100,19 +1113,19 @@ public class Long2DoubleLinkedOpenHashMap extends Long2DoubleOpenHashMap impleme state = operator.applyAsLong(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0L; + if(size() <= 0) return OptionalLong.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalLong.of(keys[index]); index = (int)links[index]; } - return 0L; + return OptionalLong.empty(); } @Override @@ -1248,7 +1261,7 @@ public class Long2DoubleLinkedOpenHashMap extends Long2DoubleOpenHashMap impleme } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -1263,19 +1276,19 @@ public class Long2DoubleLinkedOpenHashMap extends Long2DoubleOpenHashMap impleme state = operator.applyAsDouble(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0D; + if(size() <= 0) return OptionalDouble.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalDouble.of(values[index]); index = (int)links[index]; } - return 0D; + return OptionalDouble.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/longs/maps/impl/hash/Long2DoubleOpenHashMap.java b/src/main/java/speiger/src/collections/longs/maps/impl/hash/Long2DoubleOpenHashMap.java index 4121458..159946e 100644 --- a/src/main/java/speiger/src/collections/longs/maps/impl/hash/Long2DoubleOpenHashMap.java +++ b/src/main/java/speiger/src/collections/longs/maps/impl/hash/Long2DoubleOpenHashMap.java @@ -5,11 +5,14 @@ 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; import java.util.function.LongPredicate; +import java.util.OptionalLong; import java.util.function.DoublePredicate; +import java.util.OptionalDouble; import speiger.src.collections.longs.collections.LongIterator; import speiger.src.collections.longs.functions.LongConsumer; @@ -920,7 +923,7 @@ public class Long2DoubleOpenHashMap extends AbstractLong2DoubleMap implements IT } @Override - public Long2DoubleMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Long2DoubleMap.Entry state = null; boolean empty = true; @@ -937,25 +940,25 @@ public class Long2DoubleOpenHashMap extends AbstractLong2DoubleMap implements IT } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Long2DoubleMap.Entry 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(keys[i] != 0) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1127,7 +1130,7 @@ public class Long2DoubleOpenHashMap extends AbstractLong2DoubleMap implements IT } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -1144,18 +1147,18 @@ public class Long2DoubleOpenHashMap extends AbstractLong2DoubleMap implements IT } state = operator.applyAsLong(state, keys[i]); } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0L; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OptionalLong.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OptionalLong.of(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(keys[i] != 0 && filter.test(keys[i])) return keys[i]; + if(keys[i] != 0 && filter.test(keys[i])) return OptionalLong.of(keys[i]); } - return 0L; + return OptionalLong.empty(); } @Override @@ -1270,7 +1273,7 @@ public class Long2DoubleOpenHashMap extends AbstractLong2DoubleMap implements IT } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -1287,18 +1290,18 @@ public class Long2DoubleOpenHashMap extends AbstractLong2DoubleMap implements IT } state = operator.applyAsDouble(state, values[i]); } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0D; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return OptionalDouble.empty(); + if(containsNull && filter.test(values[nullIndex])) return OptionalDouble.of(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(keys[i] != 0 && filter.test(values[i])) return values[i]; + if(keys[i] != 0 && filter.test(values[i])) return OptionalDouble.of(values[i]); } - return 0D; + return OptionalDouble.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/longs/maps/impl/hash/Long2FloatLinkedOpenHashMap.java b/src/main/java/speiger/src/collections/longs/maps/impl/hash/Long2FloatLinkedOpenHashMap.java index 724e413..7189f59 100644 --- a/src/main/java/speiger/src/collections/longs/maps/impl/hash/Long2FloatLinkedOpenHashMap.java +++ b/src/main/java/speiger/src/collections/longs/maps/impl/hash/Long2FloatLinkedOpenHashMap.java @@ -4,10 +4,12 @@ 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; import java.util.function.LongPredicate; +import java.util.OptionalLong; import speiger.src.collections.longs.collections.LongBidirectionalIterator; import speiger.src.collections.longs.functions.LongConsumer; @@ -32,6 +34,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectFloatConsumer; import speiger.src.collections.floats.functions.function.FloatPredicate; +import speiger.src.collections.floats.functions.OptionalFloat; import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOperator; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.objects.lists.ObjectListIterator; @@ -669,6 +672,10 @@ public class Long2FloatLinkedOpenHashMap extends Long2FloatOpenHashMap implement } private class MapEntrySet extends AbstractObjectSet implements Long2FloatOrderedMap.FastOrderedSet { + @Override + public void addFirst(Long2FloatMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Long2FloatMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Long2FloatMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -834,7 +841,7 @@ public class Long2FloatLinkedOpenHashMap extends Long2FloatOpenHashMap implement } @Override - public Long2FloatMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Long2FloatMap.Entry state = null; boolean empty = true; @@ -849,21 +856,21 @@ public class Long2FloatLinkedOpenHashMap extends Long2FloatOpenHashMap implement state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Long2FloatMap.Entry 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 @@ -939,6 +946,12 @@ public class Long2FloatLinkedOpenHashMap extends Long2FloatOpenHashMap implement @Override public boolean add(long o) { throw new UnsupportedOperationException(); } + + @Override + public void addFirst(long o) { throw new UnsupportedOperationException(); } + + @Override + public void addLast(long o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(long o) { throw new UnsupportedOperationException(); } @@ -1085,7 +1098,7 @@ public class Long2FloatLinkedOpenHashMap extends Long2FloatOpenHashMap implement } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -1100,19 +1113,19 @@ public class Long2FloatLinkedOpenHashMap extends Long2FloatOpenHashMap implement state = operator.applyAsLong(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0L; + if(size() <= 0) return OptionalLong.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalLong.of(keys[index]); index = (int)links[index]; } - return 0L; + return OptionalLong.empty(); } @Override @@ -1248,7 +1261,7 @@ public class Long2FloatLinkedOpenHashMap extends Long2FloatOpenHashMap implement } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -1263,19 +1276,19 @@ public class Long2FloatLinkedOpenHashMap extends Long2FloatOpenHashMap implement state = operator.applyAsFloat(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0F; + if(size() <= 0) return OptionalFloat.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalFloat.of(values[index]); index = (int)links[index]; } - return 0F; + return OptionalFloat.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/longs/maps/impl/hash/Long2FloatOpenHashMap.java b/src/main/java/speiger/src/collections/longs/maps/impl/hash/Long2FloatOpenHashMap.java index 6766d65..2706696 100644 --- a/src/main/java/speiger/src/collections/longs/maps/impl/hash/Long2FloatOpenHashMap.java +++ b/src/main/java/speiger/src/collections/longs/maps/impl/hash/Long2FloatOpenHashMap.java @@ -5,10 +5,12 @@ 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; import java.util.function.LongPredicate; +import java.util.OptionalLong; import speiger.src.collections.longs.collections.LongIterator; import speiger.src.collections.longs.functions.LongConsumer; @@ -34,6 +36,7 @@ import speiger.src.collections.floats.functions.FloatConsumer; import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectFloatConsumer; import speiger.src.collections.floats.functions.function.FloatPredicate; +import speiger.src.collections.floats.functions.OptionalFloat; import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.sets.AbstractObjectSet; import speiger.src.collections.objects.sets.ObjectSet; @@ -920,7 +923,7 @@ public class Long2FloatOpenHashMap extends AbstractLong2FloatMap implements ITri } @Override - public Long2FloatMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Long2FloatMap.Entry state = null; boolean empty = true; @@ -937,25 +940,25 @@ public class Long2FloatOpenHashMap extends AbstractLong2FloatMap implements ITri } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Long2FloatMap.Entry 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(keys[i] != 0) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1127,7 +1130,7 @@ public class Long2FloatOpenHashMap extends AbstractLong2FloatMap implements ITri } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -1144,18 +1147,18 @@ public class Long2FloatOpenHashMap extends AbstractLong2FloatMap implements ITri } state = operator.applyAsLong(state, keys[i]); } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0L; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OptionalLong.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OptionalLong.of(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(keys[i] != 0 && filter.test(keys[i])) return keys[i]; + if(keys[i] != 0 && filter.test(keys[i])) return OptionalLong.of(keys[i]); } - return 0L; + return OptionalLong.empty(); } @Override @@ -1270,7 +1273,7 @@ public class Long2FloatOpenHashMap extends AbstractLong2FloatMap implements ITri } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -1287,18 +1290,18 @@ public class Long2FloatOpenHashMap extends AbstractLong2FloatMap implements ITri } state = operator.applyAsFloat(state, values[i]); } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0F; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return OptionalFloat.empty(); + if(containsNull && filter.test(values[nullIndex])) return OptionalFloat.of(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(keys[i] != 0 && filter.test(values[i])) return values[i]; + if(keys[i] != 0 && filter.test(values[i])) return OptionalFloat.of(values[i]); } - return 0F; + return OptionalFloat.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/longs/maps/impl/hash/Long2IntLinkedOpenHashMap.java b/src/main/java/speiger/src/collections/longs/maps/impl/hash/Long2IntLinkedOpenHashMap.java index 9e82c21..b50809a 100644 --- a/src/main/java/speiger/src/collections/longs/maps/impl/hash/Long2IntLinkedOpenHashMap.java +++ b/src/main/java/speiger/src/collections/longs/maps/impl/hash/Long2IntLinkedOpenHashMap.java @@ -4,11 +4,14 @@ 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; import java.util.function.LongPredicate; +import java.util.OptionalLong; import java.util.function.IntPredicate; +import java.util.OptionalInt; import speiger.src.collections.longs.collections.LongBidirectionalIterator; import speiger.src.collections.longs.functions.LongConsumer; @@ -669,6 +672,10 @@ public class Long2IntLinkedOpenHashMap extends Long2IntOpenHashMap implements Lo } private class MapEntrySet extends AbstractObjectSet implements Long2IntOrderedMap.FastOrderedSet { + @Override + public void addFirst(Long2IntMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Long2IntMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Long2IntMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -834,7 +841,7 @@ public class Long2IntLinkedOpenHashMap extends Long2IntOpenHashMap implements Lo } @Override - public Long2IntMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Long2IntMap.Entry state = null; boolean empty = true; @@ -849,21 +856,21 @@ public class Long2IntLinkedOpenHashMap extends Long2IntOpenHashMap implements Lo state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Long2IntMap.Entry 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 @@ -939,6 +946,12 @@ public class Long2IntLinkedOpenHashMap extends Long2IntOpenHashMap implements Lo @Override public boolean add(long o) { throw new UnsupportedOperationException(); } + + @Override + public void addFirst(long o) { throw new UnsupportedOperationException(); } + + @Override + public void addLast(long o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(long o) { throw new UnsupportedOperationException(); } @@ -1085,7 +1098,7 @@ public class Long2IntLinkedOpenHashMap extends Long2IntOpenHashMap implements Lo } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -1100,19 +1113,19 @@ public class Long2IntLinkedOpenHashMap extends Long2IntOpenHashMap implements Lo state = operator.applyAsLong(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0L; + if(size() <= 0) return OptionalLong.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalLong.of(keys[index]); index = (int)links[index]; } - return 0L; + return OptionalLong.empty(); } @Override @@ -1248,7 +1261,7 @@ public class Long2IntLinkedOpenHashMap extends Long2IntOpenHashMap implements Lo } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -1263,19 +1276,19 @@ public class Long2IntLinkedOpenHashMap extends Long2IntOpenHashMap implements Lo state = operator.applyAsInt(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0; + if(size() <= 0) return OptionalInt.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalInt.of(values[index]); index = (int)links[index]; } - return 0; + return OptionalInt.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/longs/maps/impl/hash/Long2IntOpenHashMap.java b/src/main/java/speiger/src/collections/longs/maps/impl/hash/Long2IntOpenHashMap.java index ae711f8..91693d9 100644 --- a/src/main/java/speiger/src/collections/longs/maps/impl/hash/Long2IntOpenHashMap.java +++ b/src/main/java/speiger/src/collections/longs/maps/impl/hash/Long2IntOpenHashMap.java @@ -5,11 +5,14 @@ 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; import java.util.function.LongPredicate; +import java.util.OptionalLong; import java.util.function.IntPredicate; +import java.util.OptionalInt; import speiger.src.collections.longs.collections.LongIterator; import speiger.src.collections.longs.functions.LongConsumer; @@ -920,7 +923,7 @@ public class Long2IntOpenHashMap extends AbstractLong2IntMap implements ITrimmab } @Override - public Long2IntMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Long2IntMap.Entry state = null; boolean empty = true; @@ -937,25 +940,25 @@ public class Long2IntOpenHashMap extends AbstractLong2IntMap implements ITrimmab } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Long2IntMap.Entry 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(keys[i] != 0) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1127,7 +1130,7 @@ public class Long2IntOpenHashMap extends AbstractLong2IntMap implements ITrimmab } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -1144,18 +1147,18 @@ public class Long2IntOpenHashMap extends AbstractLong2IntMap implements ITrimmab } state = operator.applyAsLong(state, keys[i]); } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0L; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OptionalLong.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OptionalLong.of(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(keys[i] != 0 && filter.test(keys[i])) return keys[i]; + if(keys[i] != 0 && filter.test(keys[i])) return OptionalLong.of(keys[i]); } - return 0L; + return OptionalLong.empty(); } @Override @@ -1270,7 +1273,7 @@ public class Long2IntOpenHashMap extends AbstractLong2IntMap implements ITrimmab } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -1287,18 +1290,18 @@ public class Long2IntOpenHashMap extends AbstractLong2IntMap implements ITrimmab } state = operator.applyAsInt(state, values[i]); } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return OptionalInt.empty(); + if(containsNull && filter.test(values[nullIndex])) return OptionalInt.of(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(keys[i] != 0 && filter.test(values[i])) return values[i]; + if(keys[i] != 0 && filter.test(values[i])) return OptionalInt.of(values[i]); } - return 0; + return OptionalInt.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/longs/maps/impl/hash/Long2LongLinkedOpenHashMap.java b/src/main/java/speiger/src/collections/longs/maps/impl/hash/Long2LongLinkedOpenHashMap.java index b6f30d8..6055f47 100644 --- a/src/main/java/speiger/src/collections/longs/maps/impl/hash/Long2LongLinkedOpenHashMap.java +++ b/src/main/java/speiger/src/collections/longs/maps/impl/hash/Long2LongLinkedOpenHashMap.java @@ -4,10 +4,12 @@ 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; import java.util.function.LongPredicate; +import java.util.OptionalLong; import speiger.src.collections.longs.collections.LongBidirectionalIterator; import speiger.src.collections.longs.functions.LongConsumer; @@ -662,6 +664,10 @@ public class Long2LongLinkedOpenHashMap extends Long2LongOpenHashMap implements } private class MapEntrySet extends AbstractObjectSet implements Long2LongOrderedMap.FastOrderedSet { + @Override + public void addFirst(Long2LongMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Long2LongMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Long2LongMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -827,7 +833,7 @@ public class Long2LongLinkedOpenHashMap extends Long2LongOpenHashMap implements } @Override - public Long2LongMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Long2LongMap.Entry state = null; boolean empty = true; @@ -842,21 +848,21 @@ public class Long2LongLinkedOpenHashMap extends Long2LongOpenHashMap implements state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Long2LongMap.Entry 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 @@ -932,6 +938,12 @@ public class Long2LongLinkedOpenHashMap extends Long2LongOpenHashMap implements @Override public boolean add(long o) { throw new UnsupportedOperationException(); } + + @Override + public void addFirst(long o) { throw new UnsupportedOperationException(); } + + @Override + public void addLast(long o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(long o) { throw new UnsupportedOperationException(); } @@ -1078,7 +1090,7 @@ public class Long2LongLinkedOpenHashMap extends Long2LongOpenHashMap implements } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -1093,19 +1105,19 @@ public class Long2LongLinkedOpenHashMap extends Long2LongOpenHashMap implements state = operator.applyAsLong(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0L; + if(size() <= 0) return OptionalLong.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalLong.of(keys[index]); index = (int)links[index]; } - return 0L; + return OptionalLong.empty(); } @Override @@ -1241,7 +1253,7 @@ public class Long2LongLinkedOpenHashMap extends Long2LongOpenHashMap implements } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -1256,19 +1268,19 @@ public class Long2LongLinkedOpenHashMap extends Long2LongOpenHashMap implements state = operator.applyAsLong(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0L; + if(size() <= 0) return OptionalLong.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalLong.of(values[index]); index = (int)links[index]; } - return 0L; + return OptionalLong.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/longs/maps/impl/hash/Long2LongOpenHashMap.java b/src/main/java/speiger/src/collections/longs/maps/impl/hash/Long2LongOpenHashMap.java index 6321015..fdcb032 100644 --- a/src/main/java/speiger/src/collections/longs/maps/impl/hash/Long2LongOpenHashMap.java +++ b/src/main/java/speiger/src/collections/longs/maps/impl/hash/Long2LongOpenHashMap.java @@ -5,10 +5,12 @@ 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; import java.util.function.LongPredicate; +import java.util.OptionalLong; import speiger.src.collections.longs.collections.LongIterator; import speiger.src.collections.longs.functions.LongConsumer; @@ -914,7 +916,7 @@ public class Long2LongOpenHashMap extends AbstractLong2LongMap implements ITrimm } @Override - public Long2LongMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Long2LongMap.Entry state = null; boolean empty = true; @@ -931,25 +933,25 @@ public class Long2LongOpenHashMap extends AbstractLong2LongMap implements ITrimm } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Long2LongMap.Entry 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(keys[i] != 0) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1121,7 +1123,7 @@ public class Long2LongOpenHashMap extends AbstractLong2LongMap implements ITrimm } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -1138,18 +1140,18 @@ public class Long2LongOpenHashMap extends AbstractLong2LongMap implements ITrimm } state = operator.applyAsLong(state, keys[i]); } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0L; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OptionalLong.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OptionalLong.of(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(keys[i] != 0 && filter.test(keys[i])) return keys[i]; + if(keys[i] != 0 && filter.test(keys[i])) return OptionalLong.of(keys[i]); } - return 0L; + return OptionalLong.empty(); } @Override @@ -1264,7 +1266,7 @@ public class Long2LongOpenHashMap extends AbstractLong2LongMap implements ITrimm } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -1281,18 +1283,18 @@ public class Long2LongOpenHashMap extends AbstractLong2LongMap implements ITrimm } state = operator.applyAsLong(state, values[i]); } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0L; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return OptionalLong.empty(); + if(containsNull && filter.test(values[nullIndex])) return OptionalLong.of(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(keys[i] != 0 && filter.test(values[i])) return values[i]; + if(keys[i] != 0 && filter.test(values[i])) return OptionalLong.of(values[i]); } - return 0L; + return OptionalLong.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/longs/maps/impl/hash/Long2ObjectLinkedOpenHashMap.java b/src/main/java/speiger/src/collections/longs/maps/impl/hash/Long2ObjectLinkedOpenHashMap.java index b17cc4a..e55166c 100644 --- a/src/main/java/speiger/src/collections/longs/maps/impl/hash/Long2ObjectLinkedOpenHashMap.java +++ b/src/main/java/speiger/src/collections/longs/maps/impl/hash/Long2ObjectLinkedOpenHashMap.java @@ -4,10 +4,12 @@ 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; import java.util.function.LongPredicate; +import java.util.OptionalLong; import speiger.src.collections.longs.collections.LongBidirectionalIterator; import speiger.src.collections.longs.functions.LongConsumer; @@ -653,6 +655,10 @@ public class Long2ObjectLinkedOpenHashMap extends Long2ObjectOpenHashMap i } private class MapEntrySet extends AbstractObjectSet> implements Long2ObjectOrderedMap.FastOrderedSet { + @Override + public void addFirst(Long2ObjectMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Long2ObjectMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Long2ObjectMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -818,7 +824,7 @@ public class Long2ObjectLinkedOpenHashMap extends Long2ObjectOpenHashMap i } @Override - public Long2ObjectMap.Entry reduce(ObjectObjectUnaryOperator, Long2ObjectMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Long2ObjectMap.Entry> operator) { Objects.requireNonNull(operator); Long2ObjectMap.Entry state = null; boolean empty = true; @@ -833,21 +839,21 @@ public class Long2ObjectLinkedOpenHashMap extends Long2ObjectOpenHashMap i state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Long2ObjectMap.Entry 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 @@ -923,6 +929,12 @@ public class Long2ObjectLinkedOpenHashMap extends Long2ObjectOpenHashMap i @Override public boolean add(long o) { throw new UnsupportedOperationException(); } + + @Override + public void addFirst(long o) { throw new UnsupportedOperationException(); } + + @Override + public void addLast(long o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(long o) { throw new UnsupportedOperationException(); } @@ -1069,7 +1081,7 @@ public class Long2ObjectLinkedOpenHashMap extends Long2ObjectOpenHashMap i } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -1084,19 +1096,19 @@ public class Long2ObjectLinkedOpenHashMap extends Long2ObjectOpenHashMap i state = operator.applyAsLong(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0L; + if(size() <= 0) return OptionalLong.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalLong.of(keys[index]); index = (int)links[index]; } - return 0L; + return OptionalLong.empty(); } @Override @@ -1233,7 +1245,7 @@ public class Long2ObjectLinkedOpenHashMap extends Long2ObjectOpenHashMap i } @Override - public V reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); V state = null; boolean empty = true; @@ -1248,19 +1260,19 @@ public class Long2ObjectLinkedOpenHashMap extends Long2ObjectOpenHashMap i state = operator.apply(state, values[index]); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public V findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return Optional.ofNullable(values[index]); index = (int)links[index]; } - return null; + return Optional.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/longs/maps/impl/hash/Long2ObjectOpenHashMap.java b/src/main/java/speiger/src/collections/longs/maps/impl/hash/Long2ObjectOpenHashMap.java index 6adb62b..b7ba97d 100644 --- a/src/main/java/speiger/src/collections/longs/maps/impl/hash/Long2ObjectOpenHashMap.java +++ b/src/main/java/speiger/src/collections/longs/maps/impl/hash/Long2ObjectOpenHashMap.java @@ -5,10 +5,12 @@ 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; import java.util.function.LongPredicate; +import java.util.OptionalLong; import speiger.src.collections.longs.collections.LongIterator; import speiger.src.collections.longs.functions.LongConsumer; @@ -837,7 +839,7 @@ public class Long2ObjectOpenHashMap extends AbstractLong2ObjectMap impleme } @Override - public Long2ObjectMap.Entry reduce(ObjectObjectUnaryOperator, Long2ObjectMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Long2ObjectMap.Entry> operator) { Objects.requireNonNull(operator); Long2ObjectMap.Entry state = null; boolean empty = true; @@ -854,25 +856,25 @@ public class Long2ObjectOpenHashMap extends AbstractLong2ObjectMap impleme } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Long2ObjectMap.Entry 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(keys[i] != 0) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1044,7 +1046,7 @@ public class Long2ObjectOpenHashMap extends AbstractLong2ObjectMap impleme } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -1061,18 +1063,18 @@ public class Long2ObjectOpenHashMap extends AbstractLong2ObjectMap impleme } state = operator.applyAsLong(state, keys[i]); } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0L; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OptionalLong.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OptionalLong.of(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(keys[i] != 0 && filter.test(keys[i])) return keys[i]; + if(keys[i] != 0 && filter.test(keys[i])) return OptionalLong.of(keys[i]); } - return 0L; + return OptionalLong.empty(); } @Override @@ -1187,7 +1189,7 @@ public class Long2ObjectOpenHashMap extends AbstractLong2ObjectMap impleme } @Override - public V reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); V state = null; boolean empty = true; @@ -1204,18 +1206,18 @@ public class Long2ObjectOpenHashMap extends AbstractLong2ObjectMap impleme } state = operator.apply(state, values[i]); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public V findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return Optional.empty(); + if(containsNull && filter.test(values[nullIndex])) return Optional.ofNullable(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(keys[i] != 0 && filter.test(values[i])) return values[i]; + if(keys[i] != 0 && filter.test(values[i])) return Optional.ofNullable(values[i]); } - return null; + return Optional.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/longs/maps/impl/hash/Long2ShortLinkedOpenHashMap.java b/src/main/java/speiger/src/collections/longs/maps/impl/hash/Long2ShortLinkedOpenHashMap.java index d63506e..4cfd346 100644 --- a/src/main/java/speiger/src/collections/longs/maps/impl/hash/Long2ShortLinkedOpenHashMap.java +++ b/src/main/java/speiger/src/collections/longs/maps/impl/hash/Long2ShortLinkedOpenHashMap.java @@ -4,10 +4,12 @@ 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; import java.util.function.LongPredicate; +import java.util.OptionalLong; import speiger.src.collections.longs.collections.LongBidirectionalIterator; import speiger.src.collections.longs.functions.LongConsumer; @@ -32,6 +34,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectShortConsumer; import speiger.src.collections.shorts.functions.function.ShortPredicate; +import speiger.src.collections.shorts.functions.OptionalShort; import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOperator; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.objects.lists.ObjectListIterator; @@ -669,6 +672,10 @@ public class Long2ShortLinkedOpenHashMap extends Long2ShortOpenHashMap implement } private class MapEntrySet extends AbstractObjectSet implements Long2ShortOrderedMap.FastOrderedSet { + @Override + public void addFirst(Long2ShortMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Long2ShortMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Long2ShortMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -834,7 +841,7 @@ public class Long2ShortLinkedOpenHashMap extends Long2ShortOpenHashMap implement } @Override - public Long2ShortMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Long2ShortMap.Entry state = null; boolean empty = true; @@ -849,21 +856,21 @@ public class Long2ShortLinkedOpenHashMap extends Long2ShortOpenHashMap implement state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Long2ShortMap.Entry 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 @@ -939,6 +946,12 @@ public class Long2ShortLinkedOpenHashMap extends Long2ShortOpenHashMap implement @Override public boolean add(long o) { throw new UnsupportedOperationException(); } + + @Override + public void addFirst(long o) { throw new UnsupportedOperationException(); } + + @Override + public void addLast(long o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(long o) { throw new UnsupportedOperationException(); } @@ -1085,7 +1098,7 @@ public class Long2ShortLinkedOpenHashMap extends Long2ShortOpenHashMap implement } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -1100,19 +1113,19 @@ public class Long2ShortLinkedOpenHashMap extends Long2ShortOpenHashMap implement state = operator.applyAsLong(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0L; + if(size() <= 0) return OptionalLong.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalLong.of(keys[index]); index = (int)links[index]; } - return 0L; + return OptionalLong.empty(); } @Override @@ -1248,7 +1261,7 @@ public class Long2ShortLinkedOpenHashMap extends Long2ShortOpenHashMap implement } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -1263,19 +1276,19 @@ public class Long2ShortLinkedOpenHashMap extends Long2ShortOpenHashMap implement state = operator.applyAsShort(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (short)0; + if(size() <= 0) return OptionalShort.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalShort.of(values[index]); index = (int)links[index]; } - return (short)0; + return OptionalShort.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/longs/maps/impl/hash/Long2ShortOpenHashMap.java b/src/main/java/speiger/src/collections/longs/maps/impl/hash/Long2ShortOpenHashMap.java index bbc5a82..46c8554 100644 --- a/src/main/java/speiger/src/collections/longs/maps/impl/hash/Long2ShortOpenHashMap.java +++ b/src/main/java/speiger/src/collections/longs/maps/impl/hash/Long2ShortOpenHashMap.java @@ -5,10 +5,12 @@ 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; import java.util.function.LongPredicate; +import java.util.OptionalLong; import speiger.src.collections.longs.collections.LongIterator; import speiger.src.collections.longs.functions.LongConsumer; @@ -34,6 +36,7 @@ import speiger.src.collections.shorts.functions.ShortConsumer; import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectShortConsumer; import speiger.src.collections.shorts.functions.function.ShortPredicate; +import speiger.src.collections.shorts.functions.OptionalShort; import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.sets.AbstractObjectSet; import speiger.src.collections.objects.sets.ObjectSet; @@ -920,7 +923,7 @@ public class Long2ShortOpenHashMap extends AbstractLong2ShortMap implements ITri } @Override - public Long2ShortMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Long2ShortMap.Entry state = null; boolean empty = true; @@ -937,25 +940,25 @@ public class Long2ShortOpenHashMap extends AbstractLong2ShortMap implements ITri } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Long2ShortMap.Entry 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(keys[i] != 0) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1127,7 +1130,7 @@ public class Long2ShortOpenHashMap extends AbstractLong2ShortMap implements ITri } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -1144,18 +1147,18 @@ public class Long2ShortOpenHashMap extends AbstractLong2ShortMap implements ITri } state = operator.applyAsLong(state, keys[i]); } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0L; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OptionalLong.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OptionalLong.of(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(keys[i] != 0 && filter.test(keys[i])) return keys[i]; + if(keys[i] != 0 && filter.test(keys[i])) return OptionalLong.of(keys[i]); } - return 0L; + return OptionalLong.empty(); } @Override @@ -1270,7 +1273,7 @@ public class Long2ShortOpenHashMap extends AbstractLong2ShortMap implements ITri } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -1287,18 +1290,18 @@ public class Long2ShortOpenHashMap extends AbstractLong2ShortMap implements ITri } state = operator.applyAsShort(state, values[i]); } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (short)0; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return OptionalShort.empty(); + if(containsNull && filter.test(values[nullIndex])) return OptionalShort.of(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(keys[i] != 0 && filter.test(values[i])) return values[i]; + if(keys[i] != 0 && filter.test(values[i])) return OptionalShort.of(values[i]); } - return (short)0; + return OptionalShort.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/longs/maps/impl/immutable/ImmutableLong2BooleanOpenHashMap.java b/src/main/java/speiger/src/collections/longs/maps/impl/immutable/ImmutableLong2BooleanOpenHashMap.java index 5b84b36..62300ef 100644 --- a/src/main/java/speiger/src/collections/longs/maps/impl/immutable/ImmutableLong2BooleanOpenHashMap.java +++ b/src/main/java/speiger/src/collections/longs/maps/impl/immutable/ImmutableLong2BooleanOpenHashMap.java @@ -4,10 +4,12 @@ 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; import java.util.function.LongPredicate; +import java.util.OptionalLong; import speiger.src.collections.longs.collections.LongBidirectionalIterator; import speiger.src.collections.longs.functions.LongConsumer; @@ -28,6 +30,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectBooleanConsumer; import speiger.src.collections.booleans.functions.function.BooleanPredicate; +import speiger.src.collections.booleans.functions.OptionalBoolean; import speiger.src.collections.longs.sets.AbstractLongSet; import speiger.src.collections.booleans.collections.AbstractBooleanCollection; import speiger.src.collections.booleans.collections.BooleanOrderedCollection; @@ -533,6 +536,10 @@ public class ImmutableLong2BooleanOpenHashMap extends AbstractLong2BooleanMap im } private class MapEntrySet extends AbstractObjectSet implements Long2BooleanOrderedMap.FastOrderedSet { + @Override + public void addFirst(Long2BooleanMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Long2BooleanMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Long2BooleanMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -686,7 +693,7 @@ public class ImmutableLong2BooleanOpenHashMap extends AbstractLong2BooleanMap im } @Override - public Long2BooleanMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Long2BooleanMap.Entry state = null; boolean empty = true; @@ -701,21 +708,21 @@ public class ImmutableLong2BooleanOpenHashMap extends AbstractLong2BooleanMap im state = operator.apply(state, new BasicEntry(keys[index], values[index])); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Long2BooleanMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry entry = new BasicEntry(); 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 @@ -778,6 +785,12 @@ public class ImmutableLong2BooleanOpenHashMap extends AbstractLong2BooleanMap im throw new UnsupportedOperationException(); } + @Override + public void addFirst(long o) { throw new UnsupportedOperationException(); } + + @Override + public void addLast(long o) { throw new UnsupportedOperationException(); } + @Override public boolean addAndMoveToFirst(long o) { throw new UnsupportedOperationException(); } @@ -913,7 +926,7 @@ public class ImmutableLong2BooleanOpenHashMap extends AbstractLong2BooleanMap im } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -928,19 +941,19 @@ public class ImmutableLong2BooleanOpenHashMap extends AbstractLong2BooleanMap im state = operator.applyAsLong(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0L; + if(size() <= 0) return OptionalLong.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalLong.of(keys[index]); index = (int)links[index]; } - return 0L; + return OptionalLong.empty(); } @Override @@ -1067,7 +1080,7 @@ public class ImmutableLong2BooleanOpenHashMap extends AbstractLong2BooleanMap im } @Override - public boolean reduce(BooleanBooleanUnaryOperator operator) { + public OptionalBoolean reduce(BooleanBooleanUnaryOperator operator) { Objects.requireNonNull(operator); boolean state = false; boolean empty = true; @@ -1082,19 +1095,19 @@ public class ImmutableLong2BooleanOpenHashMap extends AbstractLong2BooleanMap im state = operator.applyAsBoolean(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalBoolean.empty() : OptionalBoolean.of(state); } @Override - public boolean findFirst(BooleanPredicate filter) { + public OptionalBoolean findFirst(BooleanPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return false; + if(size() <= 0) return OptionalBoolean.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalBoolean.of(values[index]); index = (int)links[index]; } - return false; + return OptionalBoolean.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/longs/maps/impl/immutable/ImmutableLong2ByteOpenHashMap.java b/src/main/java/speiger/src/collections/longs/maps/impl/immutable/ImmutableLong2ByteOpenHashMap.java index aa3202c..2af294b 100644 --- a/src/main/java/speiger/src/collections/longs/maps/impl/immutable/ImmutableLong2ByteOpenHashMap.java +++ b/src/main/java/speiger/src/collections/longs/maps/impl/immutable/ImmutableLong2ByteOpenHashMap.java @@ -4,10 +4,12 @@ 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; import java.util.function.LongPredicate; +import java.util.OptionalLong; import speiger.src.collections.longs.collections.LongBidirectionalIterator; import speiger.src.collections.longs.functions.LongConsumer; @@ -29,6 +31,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectByteConsumer; import speiger.src.collections.bytes.functions.function.BytePredicate; +import speiger.src.collections.bytes.functions.OptionalByte; import speiger.src.collections.longs.sets.AbstractLongSet; import speiger.src.collections.bytes.collections.AbstractByteCollection; import speiger.src.collections.bytes.collections.ByteOrderedCollection; @@ -538,6 +541,10 @@ public class ImmutableLong2ByteOpenHashMap extends AbstractLong2ByteMap implemen } private class MapEntrySet extends AbstractObjectSet implements Long2ByteOrderedMap.FastOrderedSet { + @Override + public void addFirst(Long2ByteMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Long2ByteMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Long2ByteMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -691,7 +698,7 @@ public class ImmutableLong2ByteOpenHashMap extends AbstractLong2ByteMap implemen } @Override - public Long2ByteMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Long2ByteMap.Entry state = null; boolean empty = true; @@ -706,21 +713,21 @@ public class ImmutableLong2ByteOpenHashMap extends AbstractLong2ByteMap implemen state = operator.apply(state, new BasicEntry(keys[index], values[index])); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Long2ByteMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry entry = new BasicEntry(); 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 @@ -783,6 +790,12 @@ public class ImmutableLong2ByteOpenHashMap extends AbstractLong2ByteMap implemen throw new UnsupportedOperationException(); } + @Override + public void addFirst(long o) { throw new UnsupportedOperationException(); } + + @Override + public void addLast(long o) { throw new UnsupportedOperationException(); } + @Override public boolean addAndMoveToFirst(long o) { throw new UnsupportedOperationException(); } @@ -918,7 +931,7 @@ public class ImmutableLong2ByteOpenHashMap extends AbstractLong2ByteMap implemen } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -933,19 +946,19 @@ public class ImmutableLong2ByteOpenHashMap extends AbstractLong2ByteMap implemen state = operator.applyAsLong(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0L; + if(size() <= 0) return OptionalLong.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalLong.of(keys[index]); index = (int)links[index]; } - return 0L; + return OptionalLong.empty(); } @Override @@ -1072,7 +1085,7 @@ public class ImmutableLong2ByteOpenHashMap extends AbstractLong2ByteMap implemen } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -1087,19 +1100,19 @@ public class ImmutableLong2ByteOpenHashMap extends AbstractLong2ByteMap implemen state = operator.applyAsByte(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (byte)0; + if(size() <= 0) return OptionalByte.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalByte.of(values[index]); index = (int)links[index]; } - return (byte)0; + return OptionalByte.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/longs/maps/impl/immutable/ImmutableLong2CharOpenHashMap.java b/src/main/java/speiger/src/collections/longs/maps/impl/immutable/ImmutableLong2CharOpenHashMap.java index 298e402..ed51612 100644 --- a/src/main/java/speiger/src/collections/longs/maps/impl/immutable/ImmutableLong2CharOpenHashMap.java +++ b/src/main/java/speiger/src/collections/longs/maps/impl/immutable/ImmutableLong2CharOpenHashMap.java @@ -4,10 +4,12 @@ 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; import java.util.function.LongPredicate; +import java.util.OptionalLong; import speiger.src.collections.longs.collections.LongBidirectionalIterator; import speiger.src.collections.longs.functions.LongConsumer; @@ -29,6 +31,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectCharConsumer; import speiger.src.collections.chars.functions.function.CharPredicate; +import speiger.src.collections.chars.functions.OptionalChar; import speiger.src.collections.longs.sets.AbstractLongSet; import speiger.src.collections.chars.collections.AbstractCharCollection; import speiger.src.collections.chars.collections.CharOrderedCollection; @@ -538,6 +541,10 @@ public class ImmutableLong2CharOpenHashMap extends AbstractLong2CharMap implemen } private class MapEntrySet extends AbstractObjectSet implements Long2CharOrderedMap.FastOrderedSet { + @Override + public void addFirst(Long2CharMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Long2CharMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Long2CharMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -691,7 +698,7 @@ public class ImmutableLong2CharOpenHashMap extends AbstractLong2CharMap implemen } @Override - public Long2CharMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Long2CharMap.Entry state = null; boolean empty = true; @@ -706,21 +713,21 @@ public class ImmutableLong2CharOpenHashMap extends AbstractLong2CharMap implemen state = operator.apply(state, new BasicEntry(keys[index], values[index])); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Long2CharMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry entry = new BasicEntry(); 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 @@ -783,6 +790,12 @@ public class ImmutableLong2CharOpenHashMap extends AbstractLong2CharMap implemen throw new UnsupportedOperationException(); } + @Override + public void addFirst(long o) { throw new UnsupportedOperationException(); } + + @Override + public void addLast(long o) { throw new UnsupportedOperationException(); } + @Override public boolean addAndMoveToFirst(long o) { throw new UnsupportedOperationException(); } @@ -918,7 +931,7 @@ public class ImmutableLong2CharOpenHashMap extends AbstractLong2CharMap implemen } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -933,19 +946,19 @@ public class ImmutableLong2CharOpenHashMap extends AbstractLong2CharMap implemen state = operator.applyAsLong(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0L; + if(size() <= 0) return OptionalLong.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalLong.of(keys[index]); index = (int)links[index]; } - return 0L; + return OptionalLong.empty(); } @Override @@ -1072,7 +1085,7 @@ public class ImmutableLong2CharOpenHashMap extends AbstractLong2CharMap implemen } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -1087,19 +1100,19 @@ public class ImmutableLong2CharOpenHashMap extends AbstractLong2CharMap implemen state = operator.applyAsChar(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (char)0; + if(size() <= 0) return OptionalChar.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalChar.of(values[index]); index = (int)links[index]; } - return (char)0; + return OptionalChar.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/longs/maps/impl/immutable/ImmutableLong2DoubleOpenHashMap.java b/src/main/java/speiger/src/collections/longs/maps/impl/immutable/ImmutableLong2DoubleOpenHashMap.java index 4a1ad16..4f9d47d 100644 --- a/src/main/java/speiger/src/collections/longs/maps/impl/immutable/ImmutableLong2DoubleOpenHashMap.java +++ b/src/main/java/speiger/src/collections/longs/maps/impl/immutable/ImmutableLong2DoubleOpenHashMap.java @@ -4,11 +4,14 @@ 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; import java.util.function.LongPredicate; +import java.util.OptionalLong; import java.util.function.DoublePredicate; +import java.util.OptionalDouble; import speiger.src.collections.longs.collections.LongBidirectionalIterator; import speiger.src.collections.longs.functions.LongConsumer; @@ -538,6 +541,10 @@ public class ImmutableLong2DoubleOpenHashMap extends AbstractLong2DoubleMap impl } private class MapEntrySet extends AbstractObjectSet implements Long2DoubleOrderedMap.FastOrderedSet { + @Override + public void addFirst(Long2DoubleMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Long2DoubleMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Long2DoubleMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -691,7 +698,7 @@ public class ImmutableLong2DoubleOpenHashMap extends AbstractLong2DoubleMap impl } @Override - public Long2DoubleMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Long2DoubleMap.Entry state = null; boolean empty = true; @@ -706,21 +713,21 @@ public class ImmutableLong2DoubleOpenHashMap extends AbstractLong2DoubleMap impl state = operator.apply(state, new BasicEntry(keys[index], values[index])); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Long2DoubleMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry entry = new BasicEntry(); 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 @@ -783,6 +790,12 @@ public class ImmutableLong2DoubleOpenHashMap extends AbstractLong2DoubleMap impl throw new UnsupportedOperationException(); } + @Override + public void addFirst(long o) { throw new UnsupportedOperationException(); } + + @Override + public void addLast(long o) { throw new UnsupportedOperationException(); } + @Override public boolean addAndMoveToFirst(long o) { throw new UnsupportedOperationException(); } @@ -918,7 +931,7 @@ public class ImmutableLong2DoubleOpenHashMap extends AbstractLong2DoubleMap impl } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -933,19 +946,19 @@ public class ImmutableLong2DoubleOpenHashMap extends AbstractLong2DoubleMap impl state = operator.applyAsLong(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0L; + if(size() <= 0) return OptionalLong.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalLong.of(keys[index]); index = (int)links[index]; } - return 0L; + return OptionalLong.empty(); } @Override @@ -1072,7 +1085,7 @@ public class ImmutableLong2DoubleOpenHashMap extends AbstractLong2DoubleMap impl } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -1087,19 +1100,19 @@ public class ImmutableLong2DoubleOpenHashMap extends AbstractLong2DoubleMap impl state = operator.applyAsDouble(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0D; + if(size() <= 0) return OptionalDouble.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalDouble.of(values[index]); index = (int)links[index]; } - return 0D; + return OptionalDouble.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/longs/maps/impl/immutable/ImmutableLong2FloatOpenHashMap.java b/src/main/java/speiger/src/collections/longs/maps/impl/immutable/ImmutableLong2FloatOpenHashMap.java index 0bbd1aa..5103ad1 100644 --- a/src/main/java/speiger/src/collections/longs/maps/impl/immutable/ImmutableLong2FloatOpenHashMap.java +++ b/src/main/java/speiger/src/collections/longs/maps/impl/immutable/ImmutableLong2FloatOpenHashMap.java @@ -4,10 +4,12 @@ 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; import java.util.function.LongPredicate; +import java.util.OptionalLong; import speiger.src.collections.longs.collections.LongBidirectionalIterator; import speiger.src.collections.longs.functions.LongConsumer; @@ -29,6 +31,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectFloatConsumer; import speiger.src.collections.floats.functions.function.FloatPredicate; +import speiger.src.collections.floats.functions.OptionalFloat; import speiger.src.collections.longs.sets.AbstractLongSet; import speiger.src.collections.floats.collections.AbstractFloatCollection; import speiger.src.collections.floats.collections.FloatOrderedCollection; @@ -538,6 +541,10 @@ public class ImmutableLong2FloatOpenHashMap extends AbstractLong2FloatMap implem } private class MapEntrySet extends AbstractObjectSet implements Long2FloatOrderedMap.FastOrderedSet { + @Override + public void addFirst(Long2FloatMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Long2FloatMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Long2FloatMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -691,7 +698,7 @@ public class ImmutableLong2FloatOpenHashMap extends AbstractLong2FloatMap implem } @Override - public Long2FloatMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Long2FloatMap.Entry state = null; boolean empty = true; @@ -706,21 +713,21 @@ public class ImmutableLong2FloatOpenHashMap extends AbstractLong2FloatMap implem state = operator.apply(state, new BasicEntry(keys[index], values[index])); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Long2FloatMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry entry = new BasicEntry(); 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 @@ -783,6 +790,12 @@ public class ImmutableLong2FloatOpenHashMap extends AbstractLong2FloatMap implem throw new UnsupportedOperationException(); } + @Override + public void addFirst(long o) { throw new UnsupportedOperationException(); } + + @Override + public void addLast(long o) { throw new UnsupportedOperationException(); } + @Override public boolean addAndMoveToFirst(long o) { throw new UnsupportedOperationException(); } @@ -918,7 +931,7 @@ public class ImmutableLong2FloatOpenHashMap extends AbstractLong2FloatMap implem } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -933,19 +946,19 @@ public class ImmutableLong2FloatOpenHashMap extends AbstractLong2FloatMap implem state = operator.applyAsLong(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0L; + if(size() <= 0) return OptionalLong.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalLong.of(keys[index]); index = (int)links[index]; } - return 0L; + return OptionalLong.empty(); } @Override @@ -1072,7 +1085,7 @@ public class ImmutableLong2FloatOpenHashMap extends AbstractLong2FloatMap implem } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -1087,19 +1100,19 @@ public class ImmutableLong2FloatOpenHashMap extends AbstractLong2FloatMap implem state = operator.applyAsFloat(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0F; + if(size() <= 0) return OptionalFloat.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalFloat.of(values[index]); index = (int)links[index]; } - return 0F; + return OptionalFloat.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/longs/maps/impl/immutable/ImmutableLong2IntOpenHashMap.java b/src/main/java/speiger/src/collections/longs/maps/impl/immutable/ImmutableLong2IntOpenHashMap.java index 4407c32..93f192e 100644 --- a/src/main/java/speiger/src/collections/longs/maps/impl/immutable/ImmutableLong2IntOpenHashMap.java +++ b/src/main/java/speiger/src/collections/longs/maps/impl/immutable/ImmutableLong2IntOpenHashMap.java @@ -4,11 +4,14 @@ 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; import java.util.function.LongPredicate; +import java.util.OptionalLong; import java.util.function.IntPredicate; +import java.util.OptionalInt; import speiger.src.collections.longs.collections.LongBidirectionalIterator; import speiger.src.collections.longs.functions.LongConsumer; @@ -538,6 +541,10 @@ public class ImmutableLong2IntOpenHashMap extends AbstractLong2IntMap implements } private class MapEntrySet extends AbstractObjectSet implements Long2IntOrderedMap.FastOrderedSet { + @Override + public void addFirst(Long2IntMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Long2IntMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Long2IntMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -691,7 +698,7 @@ public class ImmutableLong2IntOpenHashMap extends AbstractLong2IntMap implements } @Override - public Long2IntMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Long2IntMap.Entry state = null; boolean empty = true; @@ -706,21 +713,21 @@ public class ImmutableLong2IntOpenHashMap extends AbstractLong2IntMap implements state = operator.apply(state, new BasicEntry(keys[index], values[index])); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Long2IntMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry entry = new BasicEntry(); 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 @@ -783,6 +790,12 @@ public class ImmutableLong2IntOpenHashMap extends AbstractLong2IntMap implements throw new UnsupportedOperationException(); } + @Override + public void addFirst(long o) { throw new UnsupportedOperationException(); } + + @Override + public void addLast(long o) { throw new UnsupportedOperationException(); } + @Override public boolean addAndMoveToFirst(long o) { throw new UnsupportedOperationException(); } @@ -918,7 +931,7 @@ public class ImmutableLong2IntOpenHashMap extends AbstractLong2IntMap implements } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -933,19 +946,19 @@ public class ImmutableLong2IntOpenHashMap extends AbstractLong2IntMap implements state = operator.applyAsLong(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0L; + if(size() <= 0) return OptionalLong.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalLong.of(keys[index]); index = (int)links[index]; } - return 0L; + return OptionalLong.empty(); } @Override @@ -1072,7 +1085,7 @@ public class ImmutableLong2IntOpenHashMap extends AbstractLong2IntMap implements } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -1087,19 +1100,19 @@ public class ImmutableLong2IntOpenHashMap extends AbstractLong2IntMap implements state = operator.applyAsInt(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0; + if(size() <= 0) return OptionalInt.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalInt.of(values[index]); index = (int)links[index]; } - return 0; + return OptionalInt.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/longs/maps/impl/immutable/ImmutableLong2LongOpenHashMap.java b/src/main/java/speiger/src/collections/longs/maps/impl/immutable/ImmutableLong2LongOpenHashMap.java index a686e0e..eafc892 100644 --- a/src/main/java/speiger/src/collections/longs/maps/impl/immutable/ImmutableLong2LongOpenHashMap.java +++ b/src/main/java/speiger/src/collections/longs/maps/impl/immutable/ImmutableLong2LongOpenHashMap.java @@ -4,10 +4,12 @@ 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; import java.util.function.LongPredicate; +import java.util.OptionalLong; import speiger.src.collections.longs.collections.LongBidirectionalIterator; import speiger.src.collections.longs.functions.LongConsumer; @@ -529,6 +531,10 @@ public class ImmutableLong2LongOpenHashMap extends AbstractLong2LongMap implemen } private class MapEntrySet extends AbstractObjectSet implements Long2LongOrderedMap.FastOrderedSet { + @Override + public void addFirst(Long2LongMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Long2LongMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Long2LongMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -682,7 +688,7 @@ public class ImmutableLong2LongOpenHashMap extends AbstractLong2LongMap implemen } @Override - public Long2LongMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Long2LongMap.Entry state = null; boolean empty = true; @@ -697,21 +703,21 @@ public class ImmutableLong2LongOpenHashMap extends AbstractLong2LongMap implemen state = operator.apply(state, new BasicEntry(keys[index], values[index])); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Long2LongMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry entry = new BasicEntry(); 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 @@ -774,6 +780,12 @@ public class ImmutableLong2LongOpenHashMap extends AbstractLong2LongMap implemen throw new UnsupportedOperationException(); } + @Override + public void addFirst(long o) { throw new UnsupportedOperationException(); } + + @Override + public void addLast(long o) { throw new UnsupportedOperationException(); } + @Override public boolean addAndMoveToFirst(long o) { throw new UnsupportedOperationException(); } @@ -909,7 +921,7 @@ public class ImmutableLong2LongOpenHashMap extends AbstractLong2LongMap implemen } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -924,19 +936,19 @@ public class ImmutableLong2LongOpenHashMap extends AbstractLong2LongMap implemen state = operator.applyAsLong(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0L; + if(size() <= 0) return OptionalLong.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalLong.of(keys[index]); index = (int)links[index]; } - return 0L; + return OptionalLong.empty(); } @Override @@ -1063,7 +1075,7 @@ public class ImmutableLong2LongOpenHashMap extends AbstractLong2LongMap implemen } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -1078,19 +1090,19 @@ public class ImmutableLong2LongOpenHashMap extends AbstractLong2LongMap implemen state = operator.applyAsLong(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0L; + if(size() <= 0) return OptionalLong.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalLong.of(values[index]); index = (int)links[index]; } - return 0L; + return OptionalLong.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/longs/maps/impl/immutable/ImmutableLong2ObjectOpenHashMap.java b/src/main/java/speiger/src/collections/longs/maps/impl/immutable/ImmutableLong2ObjectOpenHashMap.java index 18022c0..ea734c6 100644 --- a/src/main/java/speiger/src/collections/longs/maps/impl/immutable/ImmutableLong2ObjectOpenHashMap.java +++ b/src/main/java/speiger/src/collections/longs/maps/impl/immutable/ImmutableLong2ObjectOpenHashMap.java @@ -4,10 +4,12 @@ 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; import java.util.function.LongPredicate; +import java.util.OptionalLong; import speiger.src.collections.longs.collections.LongBidirectionalIterator; import speiger.src.collections.longs.functions.LongConsumer; @@ -509,6 +511,10 @@ public class ImmutableLong2ObjectOpenHashMap extends AbstractLong2ObjectMap> implements Long2ObjectOrderedMap.FastOrderedSet { + @Override + public void addFirst(Long2ObjectMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Long2ObjectMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Long2ObjectMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -662,7 +668,7 @@ public class ImmutableLong2ObjectOpenHashMap extends AbstractLong2ObjectMap reduce(ObjectObjectUnaryOperator, Long2ObjectMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Long2ObjectMap.Entry> operator) { Objects.requireNonNull(operator); Long2ObjectMap.Entry state = null; boolean empty = true; @@ -677,21 +683,21 @@ public class ImmutableLong2ObjectOpenHashMap extends AbstractLong2ObjectMap(keys[index], values[index])); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Long2ObjectMap.Entry findFirst(Predicate> filter) { + public Optional> findFirst(Predicate> filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry entry = new BasicEntry<>(); 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 @@ -754,6 +760,12 @@ public class ImmutableLong2ObjectOpenHashMap extends AbstractLong2ObjectMap extends AbstractLong2ObjectMap extends AbstractLong2ObjectMap extends AbstractLong2ObjectMap operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); V state = null; boolean empty = true; @@ -1059,19 +1071,19 @@ public class ImmutableLong2ObjectOpenHashMap extends AbstractLong2ObjectMap filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return Optional.ofNullable(values[index]); index = (int)links[index]; } - return null; + return Optional.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/longs/maps/impl/immutable/ImmutableLong2ShortOpenHashMap.java b/src/main/java/speiger/src/collections/longs/maps/impl/immutable/ImmutableLong2ShortOpenHashMap.java index abe8532..ef7b869 100644 --- a/src/main/java/speiger/src/collections/longs/maps/impl/immutable/ImmutableLong2ShortOpenHashMap.java +++ b/src/main/java/speiger/src/collections/longs/maps/impl/immutable/ImmutableLong2ShortOpenHashMap.java @@ -4,10 +4,12 @@ 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; import java.util.function.LongPredicate; +import java.util.OptionalLong; import speiger.src.collections.longs.collections.LongBidirectionalIterator; import speiger.src.collections.longs.functions.LongConsumer; @@ -29,6 +31,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectShortConsumer; import speiger.src.collections.shorts.functions.function.ShortPredicate; +import speiger.src.collections.shorts.functions.OptionalShort; import speiger.src.collections.longs.sets.AbstractLongSet; import speiger.src.collections.shorts.collections.AbstractShortCollection; import speiger.src.collections.shorts.collections.ShortOrderedCollection; @@ -538,6 +541,10 @@ public class ImmutableLong2ShortOpenHashMap extends AbstractLong2ShortMap implem } private class MapEntrySet extends AbstractObjectSet implements Long2ShortOrderedMap.FastOrderedSet { + @Override + public void addFirst(Long2ShortMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Long2ShortMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Long2ShortMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -691,7 +698,7 @@ public class ImmutableLong2ShortOpenHashMap extends AbstractLong2ShortMap implem } @Override - public Long2ShortMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Long2ShortMap.Entry state = null; boolean empty = true; @@ -706,21 +713,21 @@ public class ImmutableLong2ShortOpenHashMap extends AbstractLong2ShortMap implem state = operator.apply(state, new BasicEntry(keys[index], values[index])); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Long2ShortMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry entry = new BasicEntry(); 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 @@ -783,6 +790,12 @@ public class ImmutableLong2ShortOpenHashMap extends AbstractLong2ShortMap implem throw new UnsupportedOperationException(); } + @Override + public void addFirst(long o) { throw new UnsupportedOperationException(); } + + @Override + public void addLast(long o) { throw new UnsupportedOperationException(); } + @Override public boolean addAndMoveToFirst(long o) { throw new UnsupportedOperationException(); } @@ -918,7 +931,7 @@ public class ImmutableLong2ShortOpenHashMap extends AbstractLong2ShortMap implem } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -933,19 +946,19 @@ public class ImmutableLong2ShortOpenHashMap extends AbstractLong2ShortMap implem state = operator.applyAsLong(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0L; + if(size() <= 0) return OptionalLong.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalLong.of(keys[index]); index = (int)links[index]; } - return 0L; + return OptionalLong.empty(); } @Override @@ -1072,7 +1085,7 @@ public class ImmutableLong2ShortOpenHashMap extends AbstractLong2ShortMap implem } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -1087,19 +1100,19 @@ public class ImmutableLong2ShortOpenHashMap extends AbstractLong2ShortMap implem state = operator.applyAsShort(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (short)0; + if(size() <= 0) return OptionalShort.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalShort.of(values[index]); index = (int)links[index]; } - return (short)0; + return OptionalShort.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/longs/maps/impl/misc/Long2BooleanArrayMap.java b/src/main/java/speiger/src/collections/longs/maps/impl/misc/Long2BooleanArrayMap.java index 38f31de..305b82d 100644 --- a/src/main/java/speiger/src/collections/longs/maps/impl/misc/Long2BooleanArrayMap.java +++ b/src/main/java/speiger/src/collections/longs/maps/impl/misc/Long2BooleanArrayMap.java @@ -4,10 +4,12 @@ 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; import java.util.function.LongPredicate; +import java.util.OptionalLong; import speiger.src.collections.longs.collections.LongBidirectionalIterator; import speiger.src.collections.longs.functions.LongConsumer; @@ -34,6 +36,7 @@ import speiger.src.collections.booleans.lists.BooleanListIterator; import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.booleans.functions.function.BooleanPredicate; +import speiger.src.collections.booleans.functions.OptionalBoolean; import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOperator; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; @@ -728,6 +731,10 @@ public class Long2BooleanArrayMap extends AbstractLong2BooleanMap implements Lon } private class MapEntrySet extends AbstractObjectSet implements Long2BooleanOrderedMap.FastOrderedSet { + @Override + public void addFirst(Long2BooleanMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Long2BooleanMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Long2BooleanMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -876,7 +883,7 @@ public class Long2BooleanArrayMap extends AbstractLong2BooleanMap implements Lon } @Override - public Long2BooleanMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Long2BooleanMap.Entry state = null; boolean empty = true; @@ -888,19 +895,19 @@ public class Long2BooleanArrayMap extends AbstractLong2BooleanMap implements Lon } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Long2BooleanMap.Entry 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 implements Long2ByteOrderedMap.FastOrderedSet { + @Override + public void addFirst(Long2ByteMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Long2ByteMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Long2ByteMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -899,7 +906,7 @@ public class Long2ByteArrayMap extends AbstractLong2ByteMap implements Long2Byte } @Override - public Long2ByteMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Long2ByteMap.Entry state = null; boolean empty = true; @@ -911,19 +918,19 @@ public class Long2ByteArrayMap extends AbstractLong2ByteMap implements Long2Byte } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Long2ByteMap.Entry 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 implements Long2CharOrderedMap.FastOrderedSet { + @Override + public void addFirst(Long2CharMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Long2CharMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Long2CharMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -899,7 +906,7 @@ public class Long2CharArrayMap extends AbstractLong2CharMap implements Long2Char } @Override - public Long2CharMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Long2CharMap.Entry state = null; boolean empty = true; @@ -911,19 +918,19 @@ public class Long2CharArrayMap extends AbstractLong2CharMap implements Long2Char } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Long2CharMap.Entry 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 implements Long2DoubleOrderedMap.FastOrderedSet { + @Override + public void addFirst(Long2DoubleMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Long2DoubleMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Long2DoubleMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -899,7 +906,7 @@ public class Long2DoubleArrayMap extends AbstractLong2DoubleMap implements Long2 } @Override - public Long2DoubleMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Long2DoubleMap.Entry state = null; boolean empty = true; @@ -911,19 +918,19 @@ public class Long2DoubleArrayMap extends AbstractLong2DoubleMap implements Long2 } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Long2DoubleMap.Entry 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 implements Long2FloatOrderedMap.FastOrderedSet { + @Override + public void addFirst(Long2FloatMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Long2FloatMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Long2FloatMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -899,7 +906,7 @@ public class Long2FloatArrayMap extends AbstractLong2FloatMap implements Long2Fl } @Override - public Long2FloatMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Long2FloatMap.Entry state = null; boolean empty = true; @@ -911,19 +918,19 @@ public class Long2FloatArrayMap extends AbstractLong2FloatMap implements Long2Fl } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Long2FloatMap.Entry 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 implements Long2IntOrderedMap.FastOrderedSet { + @Override + public void addFirst(Long2IntMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Long2IntMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Long2IntMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -899,7 +906,7 @@ public class Long2IntArrayMap extends AbstractLong2IntMap implements Long2IntOrd } @Override - public Long2IntMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Long2IntMap.Entry state = null; boolean empty = true; @@ -911,19 +918,19 @@ public class Long2IntArrayMap extends AbstractLong2IntMap implements Long2IntOrd } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Long2IntMap.Entry 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 implements Long2LongOrderedMap.FastOrderedSet { + @Override + public void addFirst(Long2LongMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Long2LongMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Long2LongMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -892,7 +898,7 @@ public class Long2LongArrayMap extends AbstractLong2LongMap implements Long2Long } @Override - public Long2LongMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Long2LongMap.Entry state = null; boolean empty = true; @@ -904,19 +910,19 @@ public class Long2LongArrayMap extends AbstractLong2LongMap implements Long2Long } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Long2LongMap.Entry 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 extends AbstractLong2ObjectMap implements } private class MapEntrySet extends AbstractObjectSet> implements Long2ObjectOrderedMap.FastOrderedSet { + @Override + public void addFirst(Long2ObjectMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Long2ObjectMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Long2ObjectMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -812,7 +818,7 @@ public class Long2ObjectArrayMap extends AbstractLong2ObjectMap implements } @Override - public Long2ObjectMap.Entry reduce(ObjectObjectUnaryOperator, Long2ObjectMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Long2ObjectMap.Entry> operator) { Objects.requireNonNull(operator); Long2ObjectMap.Entry state = null; boolean empty = true; @@ -824,19 +830,19 @@ public class Long2ObjectArrayMap extends AbstractLong2ObjectMap implements } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Long2ObjectMap.Entry 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 extends AbstractLong2ObjectMap implements @Override public boolean add(long o) { throw new UnsupportedOperationException(); } @Override + public void addFirst(long o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(long o) { throw new UnsupportedOperationException(); } + @Override public boolean addAndMoveToFirst(long o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToLast(long o) { throw new UnsupportedOperationException(); } @@ -995,7 +1005,7 @@ public class Long2ObjectArrayMap extends AbstractLong2ObjectMap implements } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -1007,16 +1017,16 @@ public class Long2ObjectArrayMap extends AbstractLong2ObjectMap implements } state = operator.applyAsLong(state, keys[i]); } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); for(int i = 0;i extends AbstractLong2ObjectMap implements } @Override - public V reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); V state = null; boolean empty = true; @@ -1129,16 +1139,16 @@ public class Long2ObjectArrayMap extends AbstractLong2ObjectMap implements } state = operator.apply(state, values[i]); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public V findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); for(int i = 0;i implements Long2ShortOrderedMap.FastOrderedSet { + @Override + public void addFirst(Long2ShortMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Long2ShortMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Long2ShortMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -899,7 +906,7 @@ public class Long2ShortArrayMap extends AbstractLong2ShortMap implements Long2Sh } @Override - public Long2ShortMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Long2ShortMap.Entry state = null; boolean empty = true; @@ -911,19 +918,19 @@ public class Long2ShortArrayMap extends AbstractLong2ShortMap implements Long2Sh } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Long2ShortMap.Entry 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); Long2BooleanMap.Entry state = null; boolean empty = true; @@ -1724,19 +1727,19 @@ public class Long2BooleanAVLTreeMap extends AbstractLong2BooleanMap implements L } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Long2BooleanMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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 @@ -1831,7 +1834,7 @@ public class Long2BooleanAVLTreeMap extends AbstractLong2BooleanMap implements L } @Override - public boolean reduce(BooleanBooleanUnaryOperator operator) { + public OptionalBoolean reduce(BooleanBooleanUnaryOperator operator) { Objects.requireNonNull(operator); boolean state = false; boolean empty = true; @@ -1843,15 +1846,15 @@ public class Long2BooleanAVLTreeMap extends AbstractLong2BooleanMap implements L } state = operator.applyAsBoolean(state, entry.value); } - return state; + return empty ? OptionalBoolean.empty() : OptionalBoolean.of(state); } @Override - public boolean findFirst(BooleanPredicate filter) { + public OptionalBoolean findFirst(BooleanPredicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return false; + if(filter.test(entry.value)) return OptionalBoolean.of(entry.value); + return OptionalBoolean.empty(); } @Override @@ -2167,7 +2170,7 @@ public class Long2BooleanAVLTreeMap extends AbstractLong2BooleanMap implements L } @Override - public boolean reduce(BooleanBooleanUnaryOperator operator) { + public OptionalBoolean reduce(BooleanBooleanUnaryOperator operator) { Objects.requireNonNull(operator); boolean state = false; boolean empty = true; @@ -2179,15 +2182,15 @@ public class Long2BooleanAVLTreeMap extends AbstractLong2BooleanMap implements L } state = operator.applyAsBoolean(state, entry.value); } - return state; + return empty ? OptionalBoolean.empty() : OptionalBoolean.of(state); } @Override - public boolean findFirst(BooleanPredicate filter) { + public OptionalBoolean findFirst(BooleanPredicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return false; + if(filter.test(entry.value)) return OptionalBoolean.of(entry.value); + return OptionalBoolean.empty(); } @Override @@ -2328,7 +2331,7 @@ public class Long2BooleanAVLTreeMap extends AbstractLong2BooleanMap implements L } @Override - public Long2BooleanMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Long2BooleanMap.Entry state = null; boolean empty = true; @@ -2340,19 +2343,19 @@ public class Long2BooleanAVLTreeMap extends AbstractLong2BooleanMap implements L } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Long2BooleanMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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/main/java/speiger/src/collections/longs/maps/impl/tree/Long2BooleanRBTreeMap.java b/src/main/java/speiger/src/collections/longs/maps/impl/tree/Long2BooleanRBTreeMap.java index d2b721b..6371a6a 100644 --- a/src/main/java/speiger/src/collections/longs/maps/impl/tree/Long2BooleanRBTreeMap.java +++ b/src/main/java/speiger/src/collections/longs/maps/impl/tree/Long2BooleanRBTreeMap.java @@ -3,11 +3,13 @@ package speiger.src.collections.longs.maps.impl.tree; import java.util.Collections; import java.util.Map; 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; import java.util.function.LongPredicate; +import java.util.OptionalLong; import speiger.src.collections.longs.collections.LongBidirectionalIterator; import speiger.src.collections.longs.functions.LongComparator; @@ -35,6 +37,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectBooleanConsumer; import speiger.src.collections.booleans.functions.function.BooleanPredicate; +import speiger.src.collections.booleans.functions.OptionalBoolean; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOperator; @@ -1123,7 +1126,7 @@ public class Long2BooleanRBTreeMap extends AbstractLong2BooleanMap implements Lo } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -1135,15 +1138,15 @@ public class Long2BooleanRBTreeMap extends AbstractLong2BooleanMap implements Lo } state = operator.applyAsLong(state, entry.key); } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); for(Node entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.test(entry.key)) return entry.key; - return 0L; + if(filter.test(entry.key)) return OptionalLong.of(entry.key); + return OptionalLong.empty(); } @Override @@ -1775,7 +1778,7 @@ public class Long2BooleanRBTreeMap extends AbstractLong2BooleanMap implements Lo } @Override - public Long2BooleanMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Long2BooleanMap.Entry state = null; boolean empty = true; @@ -1787,19 +1790,19 @@ public class Long2BooleanRBTreeMap extends AbstractLong2BooleanMap implements Lo } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Long2BooleanMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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 @@ -1894,7 +1897,7 @@ public class Long2BooleanRBTreeMap extends AbstractLong2BooleanMap implements Lo } @Override - public boolean reduce(BooleanBooleanUnaryOperator operator) { + public OptionalBoolean reduce(BooleanBooleanUnaryOperator operator) { Objects.requireNonNull(operator); boolean state = false; boolean empty = true; @@ -1906,15 +1909,15 @@ public class Long2BooleanRBTreeMap extends AbstractLong2BooleanMap implements Lo } state = operator.applyAsBoolean(state, entry.value); } - return state; + return empty ? OptionalBoolean.empty() : OptionalBoolean.of(state); } @Override - public boolean findFirst(BooleanPredicate filter) { + public OptionalBoolean findFirst(BooleanPredicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return false; + if(filter.test(entry.value)) return OptionalBoolean.of(entry.value); + return OptionalBoolean.empty(); } @Override @@ -2230,7 +2233,7 @@ public class Long2BooleanRBTreeMap extends AbstractLong2BooleanMap implements Lo } @Override - public boolean reduce(BooleanBooleanUnaryOperator operator) { + public OptionalBoolean reduce(BooleanBooleanUnaryOperator operator) { Objects.requireNonNull(operator); boolean state = false; boolean empty = true; @@ -2242,15 +2245,15 @@ public class Long2BooleanRBTreeMap extends AbstractLong2BooleanMap implements Lo } state = operator.applyAsBoolean(state, entry.value); } - return state; + return empty ? OptionalBoolean.empty() : OptionalBoolean.of(state); } @Override - public boolean findFirst(BooleanPredicate filter) { + public OptionalBoolean findFirst(BooleanPredicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return false; + if(filter.test(entry.value)) return OptionalBoolean.of(entry.value); + return OptionalBoolean.empty(); } @Override @@ -2391,7 +2394,7 @@ public class Long2BooleanRBTreeMap extends AbstractLong2BooleanMap implements Lo } @Override - public Long2BooleanMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Long2BooleanMap.Entry state = null; boolean empty = true; @@ -2403,19 +2406,19 @@ public class Long2BooleanRBTreeMap extends AbstractLong2BooleanMap implements Lo } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Long2BooleanMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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/main/java/speiger/src/collections/longs/maps/impl/tree/Long2ByteAVLTreeMap.java b/src/main/java/speiger/src/collections/longs/maps/impl/tree/Long2ByteAVLTreeMap.java index 1799820..988abe5 100644 --- a/src/main/java/speiger/src/collections/longs/maps/impl/tree/Long2ByteAVLTreeMap.java +++ b/src/main/java/speiger/src/collections/longs/maps/impl/tree/Long2ByteAVLTreeMap.java @@ -3,11 +3,13 @@ package speiger.src.collections.longs.maps.impl.tree; import java.util.Collections; import java.util.Map; 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; import java.util.function.LongPredicate; +import java.util.OptionalLong; import speiger.src.collections.longs.collections.LongBidirectionalIterator; @@ -37,6 +39,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectByteConsumer; import speiger.src.collections.bytes.functions.function.BytePredicate; +import speiger.src.collections.bytes.functions.OptionalByte; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOperator; @@ -1129,7 +1132,7 @@ public class Long2ByteAVLTreeMap extends AbstractLong2ByteMap implements Long2By } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -1141,15 +1144,15 @@ public class Long2ByteAVLTreeMap extends AbstractLong2ByteMap implements Long2By } state = operator.applyAsLong(state, entry.key); } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); for(Node entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.test(entry.key)) return entry.key; - return 0L; + if(filter.test(entry.key)) return OptionalLong.of(entry.key); + return OptionalLong.empty(); } @Override @@ -1783,7 +1786,7 @@ public class Long2ByteAVLTreeMap extends AbstractLong2ByteMap implements Long2By } @Override - public Long2ByteMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Long2ByteMap.Entry state = null; boolean empty = true; @@ -1795,19 +1798,19 @@ public class Long2ByteAVLTreeMap extends AbstractLong2ByteMap implements Long2By } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Long2ByteMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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 @@ -1902,7 +1905,7 @@ public class Long2ByteAVLTreeMap extends AbstractLong2ByteMap implements Long2By } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -1914,15 +1917,15 @@ public class Long2ByteAVLTreeMap extends AbstractLong2ByteMap implements Long2By } state = operator.applyAsByte(state, entry.value); } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return (byte)0; + if(filter.test(entry.value)) return OptionalByte.of(entry.value); + return OptionalByte.empty(); } @Override @@ -2238,7 +2241,7 @@ public class Long2ByteAVLTreeMap extends AbstractLong2ByteMap implements Long2By } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -2250,15 +2253,15 @@ public class Long2ByteAVLTreeMap extends AbstractLong2ByteMap implements Long2By } state = operator.applyAsByte(state, entry.value); } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return (byte)0; + if(filter.test(entry.value)) return OptionalByte.of(entry.value); + return OptionalByte.empty(); } @Override @@ -2399,7 +2402,7 @@ public class Long2ByteAVLTreeMap extends AbstractLong2ByteMap implements Long2By } @Override - public Long2ByteMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Long2ByteMap.Entry state = null; boolean empty = true; @@ -2411,19 +2414,19 @@ public class Long2ByteAVLTreeMap extends AbstractLong2ByteMap implements Long2By } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Long2ByteMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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/main/java/speiger/src/collections/longs/maps/impl/tree/Long2ByteRBTreeMap.java b/src/main/java/speiger/src/collections/longs/maps/impl/tree/Long2ByteRBTreeMap.java index bb87ce7..f6ee8a3 100644 --- a/src/main/java/speiger/src/collections/longs/maps/impl/tree/Long2ByteRBTreeMap.java +++ b/src/main/java/speiger/src/collections/longs/maps/impl/tree/Long2ByteRBTreeMap.java @@ -3,11 +3,13 @@ package speiger.src.collections.longs.maps.impl.tree; import java.util.Collections; import java.util.Map; 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; import java.util.function.LongPredicate; +import java.util.OptionalLong; import speiger.src.collections.longs.collections.LongBidirectionalIterator; import speiger.src.collections.longs.functions.LongComparator; @@ -36,6 +38,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectByteConsumer; import speiger.src.collections.bytes.functions.function.BytePredicate; +import speiger.src.collections.bytes.functions.OptionalByte; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOperator; @@ -1182,7 +1185,7 @@ public class Long2ByteRBTreeMap extends AbstractLong2ByteMap implements Long2Byt } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -1194,15 +1197,15 @@ public class Long2ByteRBTreeMap extends AbstractLong2ByteMap implements Long2Byt } state = operator.applyAsLong(state, entry.key); } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); for(Node entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.test(entry.key)) return entry.key; - return 0L; + if(filter.test(entry.key)) return OptionalLong.of(entry.key); + return OptionalLong.empty(); } @Override @@ -1846,7 +1849,7 @@ public class Long2ByteRBTreeMap extends AbstractLong2ByteMap implements Long2Byt } @Override - public Long2ByteMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Long2ByteMap.Entry state = null; boolean empty = true; @@ -1858,19 +1861,19 @@ public class Long2ByteRBTreeMap extends AbstractLong2ByteMap implements Long2Byt } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Long2ByteMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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 @@ -1965,7 +1968,7 @@ public class Long2ByteRBTreeMap extends AbstractLong2ByteMap implements Long2Byt } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -1977,15 +1980,15 @@ public class Long2ByteRBTreeMap extends AbstractLong2ByteMap implements Long2Byt } state = operator.applyAsByte(state, entry.value); } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return (byte)0; + if(filter.test(entry.value)) return OptionalByte.of(entry.value); + return OptionalByte.empty(); } @Override @@ -2301,7 +2304,7 @@ public class Long2ByteRBTreeMap extends AbstractLong2ByteMap implements Long2Byt } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -2313,15 +2316,15 @@ public class Long2ByteRBTreeMap extends AbstractLong2ByteMap implements Long2Byt } state = operator.applyAsByte(state, entry.value); } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return (byte)0; + if(filter.test(entry.value)) return OptionalByte.of(entry.value); + return OptionalByte.empty(); } @Override @@ -2462,7 +2465,7 @@ public class Long2ByteRBTreeMap extends AbstractLong2ByteMap implements Long2Byt } @Override - public Long2ByteMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Long2ByteMap.Entry state = null; boolean empty = true; @@ -2474,19 +2477,19 @@ public class Long2ByteRBTreeMap extends AbstractLong2ByteMap implements Long2Byt } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Long2ByteMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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/main/java/speiger/src/collections/longs/maps/impl/tree/Long2CharAVLTreeMap.java b/src/main/java/speiger/src/collections/longs/maps/impl/tree/Long2CharAVLTreeMap.java index 04b531e..7ef354e 100644 --- a/src/main/java/speiger/src/collections/longs/maps/impl/tree/Long2CharAVLTreeMap.java +++ b/src/main/java/speiger/src/collections/longs/maps/impl/tree/Long2CharAVLTreeMap.java @@ -3,11 +3,13 @@ package speiger.src.collections.longs.maps.impl.tree; import java.util.Collections; import java.util.Map; 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; import java.util.function.LongPredicate; +import java.util.OptionalLong; import speiger.src.collections.longs.collections.LongBidirectionalIterator; @@ -37,6 +39,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectCharConsumer; import speiger.src.collections.chars.functions.function.CharPredicate; +import speiger.src.collections.chars.functions.OptionalChar; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOperator; @@ -1129,7 +1132,7 @@ public class Long2CharAVLTreeMap extends AbstractLong2CharMap implements Long2Ch } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -1141,15 +1144,15 @@ public class Long2CharAVLTreeMap extends AbstractLong2CharMap implements Long2Ch } state = operator.applyAsLong(state, entry.key); } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); for(Node entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.test(entry.key)) return entry.key; - return 0L; + if(filter.test(entry.key)) return OptionalLong.of(entry.key); + return OptionalLong.empty(); } @Override @@ -1783,7 +1786,7 @@ public class Long2CharAVLTreeMap extends AbstractLong2CharMap implements Long2Ch } @Override - public Long2CharMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Long2CharMap.Entry state = null; boolean empty = true; @@ -1795,19 +1798,19 @@ public class Long2CharAVLTreeMap extends AbstractLong2CharMap implements Long2Ch } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Long2CharMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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 @@ -1902,7 +1905,7 @@ public class Long2CharAVLTreeMap extends AbstractLong2CharMap implements Long2Ch } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -1914,15 +1917,15 @@ public class Long2CharAVLTreeMap extends AbstractLong2CharMap implements Long2Ch } state = operator.applyAsChar(state, entry.value); } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return (char)0; + if(filter.test(entry.value)) return OptionalChar.of(entry.value); + return OptionalChar.empty(); } @Override @@ -2238,7 +2241,7 @@ public class Long2CharAVLTreeMap extends AbstractLong2CharMap implements Long2Ch } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -2250,15 +2253,15 @@ public class Long2CharAVLTreeMap extends AbstractLong2CharMap implements Long2Ch } state = operator.applyAsChar(state, entry.value); } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return (char)0; + if(filter.test(entry.value)) return OptionalChar.of(entry.value); + return OptionalChar.empty(); } @Override @@ -2399,7 +2402,7 @@ public class Long2CharAVLTreeMap extends AbstractLong2CharMap implements Long2Ch } @Override - public Long2CharMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Long2CharMap.Entry state = null; boolean empty = true; @@ -2411,19 +2414,19 @@ public class Long2CharAVLTreeMap extends AbstractLong2CharMap implements Long2Ch } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Long2CharMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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/main/java/speiger/src/collections/longs/maps/impl/tree/Long2CharRBTreeMap.java b/src/main/java/speiger/src/collections/longs/maps/impl/tree/Long2CharRBTreeMap.java index 9fdec06..ff8696a 100644 --- a/src/main/java/speiger/src/collections/longs/maps/impl/tree/Long2CharRBTreeMap.java +++ b/src/main/java/speiger/src/collections/longs/maps/impl/tree/Long2CharRBTreeMap.java @@ -3,11 +3,13 @@ package speiger.src.collections.longs.maps.impl.tree; import java.util.Collections; import java.util.Map; 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; import java.util.function.LongPredicate; +import java.util.OptionalLong; import speiger.src.collections.longs.collections.LongBidirectionalIterator; import speiger.src.collections.longs.functions.LongComparator; @@ -36,6 +38,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectCharConsumer; import speiger.src.collections.chars.functions.function.CharPredicate; +import speiger.src.collections.chars.functions.OptionalChar; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOperator; @@ -1182,7 +1185,7 @@ public class Long2CharRBTreeMap extends AbstractLong2CharMap implements Long2Cha } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -1194,15 +1197,15 @@ public class Long2CharRBTreeMap extends AbstractLong2CharMap implements Long2Cha } state = operator.applyAsLong(state, entry.key); } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); for(Node entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.test(entry.key)) return entry.key; - return 0L; + if(filter.test(entry.key)) return OptionalLong.of(entry.key); + return OptionalLong.empty(); } @Override @@ -1846,7 +1849,7 @@ public class Long2CharRBTreeMap extends AbstractLong2CharMap implements Long2Cha } @Override - public Long2CharMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Long2CharMap.Entry state = null; boolean empty = true; @@ -1858,19 +1861,19 @@ public class Long2CharRBTreeMap extends AbstractLong2CharMap implements Long2Cha } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Long2CharMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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 @@ -1965,7 +1968,7 @@ public class Long2CharRBTreeMap extends AbstractLong2CharMap implements Long2Cha } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -1977,15 +1980,15 @@ public class Long2CharRBTreeMap extends AbstractLong2CharMap implements Long2Cha } state = operator.applyAsChar(state, entry.value); } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return (char)0; + if(filter.test(entry.value)) return OptionalChar.of(entry.value); + return OptionalChar.empty(); } @Override @@ -2301,7 +2304,7 @@ public class Long2CharRBTreeMap extends AbstractLong2CharMap implements Long2Cha } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -2313,15 +2316,15 @@ public class Long2CharRBTreeMap extends AbstractLong2CharMap implements Long2Cha } state = operator.applyAsChar(state, entry.value); } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return (char)0; + if(filter.test(entry.value)) return OptionalChar.of(entry.value); + return OptionalChar.empty(); } @Override @@ -2462,7 +2465,7 @@ public class Long2CharRBTreeMap extends AbstractLong2CharMap implements Long2Cha } @Override - public Long2CharMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Long2CharMap.Entry state = null; boolean empty = true; @@ -2474,19 +2477,19 @@ public class Long2CharRBTreeMap extends AbstractLong2CharMap implements Long2Cha } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Long2CharMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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/main/java/speiger/src/collections/longs/maps/impl/tree/Long2DoubleAVLTreeMap.java b/src/main/java/speiger/src/collections/longs/maps/impl/tree/Long2DoubleAVLTreeMap.java index c127195..a32f0e0 100644 --- a/src/main/java/speiger/src/collections/longs/maps/impl/tree/Long2DoubleAVLTreeMap.java +++ b/src/main/java/speiger/src/collections/longs/maps/impl/tree/Long2DoubleAVLTreeMap.java @@ -3,12 +3,15 @@ package speiger.src.collections.longs.maps.impl.tree; import java.util.Collections; import java.util.Map; 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; import java.util.function.LongPredicate; +import java.util.OptionalLong; import java.util.function.DoublePredicate; +import java.util.OptionalDouble; import speiger.src.collections.longs.collections.LongBidirectionalIterator; @@ -1129,7 +1132,7 @@ public class Long2DoubleAVLTreeMap extends AbstractLong2DoubleMap implements Lon } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -1141,15 +1144,15 @@ public class Long2DoubleAVLTreeMap extends AbstractLong2DoubleMap implements Lon } state = operator.applyAsLong(state, entry.key); } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); for(Node entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.test(entry.key)) return entry.key; - return 0L; + if(filter.test(entry.key)) return OptionalLong.of(entry.key); + return OptionalLong.empty(); } @Override @@ -1783,7 +1786,7 @@ public class Long2DoubleAVLTreeMap extends AbstractLong2DoubleMap implements Lon } @Override - public Long2DoubleMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Long2DoubleMap.Entry state = null; boolean empty = true; @@ -1795,19 +1798,19 @@ public class Long2DoubleAVLTreeMap extends AbstractLong2DoubleMap implements Lon } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Long2DoubleMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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 @@ -1902,7 +1905,7 @@ public class Long2DoubleAVLTreeMap extends AbstractLong2DoubleMap implements Lon } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -1914,15 +1917,15 @@ public class Long2DoubleAVLTreeMap extends AbstractLong2DoubleMap implements Lon } state = operator.applyAsDouble(state, entry.value); } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return 0D; + if(filter.test(entry.value)) return OptionalDouble.of(entry.value); + return OptionalDouble.empty(); } @Override @@ -2238,7 +2241,7 @@ public class Long2DoubleAVLTreeMap extends AbstractLong2DoubleMap implements Lon } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -2250,15 +2253,15 @@ public class Long2DoubleAVLTreeMap extends AbstractLong2DoubleMap implements Lon } state = operator.applyAsDouble(state, entry.value); } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return 0D; + if(filter.test(entry.value)) return OptionalDouble.of(entry.value); + return OptionalDouble.empty(); } @Override @@ -2399,7 +2402,7 @@ public class Long2DoubleAVLTreeMap extends AbstractLong2DoubleMap implements Lon } @Override - public Long2DoubleMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Long2DoubleMap.Entry state = null; boolean empty = true; @@ -2411,19 +2414,19 @@ public class Long2DoubleAVLTreeMap extends AbstractLong2DoubleMap implements Lon } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Long2DoubleMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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/main/java/speiger/src/collections/longs/maps/impl/tree/Long2DoubleRBTreeMap.java b/src/main/java/speiger/src/collections/longs/maps/impl/tree/Long2DoubleRBTreeMap.java index 264f708..29557e7 100644 --- a/src/main/java/speiger/src/collections/longs/maps/impl/tree/Long2DoubleRBTreeMap.java +++ b/src/main/java/speiger/src/collections/longs/maps/impl/tree/Long2DoubleRBTreeMap.java @@ -3,12 +3,15 @@ package speiger.src.collections.longs.maps.impl.tree; import java.util.Collections; import java.util.Map; 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; import java.util.function.LongPredicate; +import java.util.OptionalLong; import java.util.function.DoublePredicate; +import java.util.OptionalDouble; import speiger.src.collections.longs.collections.LongBidirectionalIterator; import speiger.src.collections.longs.functions.LongComparator; @@ -1182,7 +1185,7 @@ public class Long2DoubleRBTreeMap extends AbstractLong2DoubleMap implements Long } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -1194,15 +1197,15 @@ public class Long2DoubleRBTreeMap extends AbstractLong2DoubleMap implements Long } state = operator.applyAsLong(state, entry.key); } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); for(Node entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.test(entry.key)) return entry.key; - return 0L; + if(filter.test(entry.key)) return OptionalLong.of(entry.key); + return OptionalLong.empty(); } @Override @@ -1846,7 +1849,7 @@ public class Long2DoubleRBTreeMap extends AbstractLong2DoubleMap implements Long } @Override - public Long2DoubleMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Long2DoubleMap.Entry state = null; boolean empty = true; @@ -1858,19 +1861,19 @@ public class Long2DoubleRBTreeMap extends AbstractLong2DoubleMap implements Long } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Long2DoubleMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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 @@ -1965,7 +1968,7 @@ public class Long2DoubleRBTreeMap extends AbstractLong2DoubleMap implements Long } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -1977,15 +1980,15 @@ public class Long2DoubleRBTreeMap extends AbstractLong2DoubleMap implements Long } state = operator.applyAsDouble(state, entry.value); } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return 0D; + if(filter.test(entry.value)) return OptionalDouble.of(entry.value); + return OptionalDouble.empty(); } @Override @@ -2301,7 +2304,7 @@ public class Long2DoubleRBTreeMap extends AbstractLong2DoubleMap implements Long } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -2313,15 +2316,15 @@ public class Long2DoubleRBTreeMap extends AbstractLong2DoubleMap implements Long } state = operator.applyAsDouble(state, entry.value); } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return 0D; + if(filter.test(entry.value)) return OptionalDouble.of(entry.value); + return OptionalDouble.empty(); } @Override @@ -2462,7 +2465,7 @@ public class Long2DoubleRBTreeMap extends AbstractLong2DoubleMap implements Long } @Override - public Long2DoubleMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Long2DoubleMap.Entry state = null; boolean empty = true; @@ -2474,19 +2477,19 @@ public class Long2DoubleRBTreeMap extends AbstractLong2DoubleMap implements Long } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Long2DoubleMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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/main/java/speiger/src/collections/longs/maps/impl/tree/Long2FloatAVLTreeMap.java b/src/main/java/speiger/src/collections/longs/maps/impl/tree/Long2FloatAVLTreeMap.java index db1646d..82a5ea4 100644 --- a/src/main/java/speiger/src/collections/longs/maps/impl/tree/Long2FloatAVLTreeMap.java +++ b/src/main/java/speiger/src/collections/longs/maps/impl/tree/Long2FloatAVLTreeMap.java @@ -3,11 +3,13 @@ package speiger.src.collections.longs.maps.impl.tree; import java.util.Collections; import java.util.Map; 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; import java.util.function.LongPredicate; +import java.util.OptionalLong; import speiger.src.collections.longs.collections.LongBidirectionalIterator; @@ -37,6 +39,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectFloatConsumer; import speiger.src.collections.floats.functions.function.FloatPredicate; +import speiger.src.collections.floats.functions.OptionalFloat; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOperator; @@ -1129,7 +1132,7 @@ public class Long2FloatAVLTreeMap extends AbstractLong2FloatMap implements Long2 } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -1141,15 +1144,15 @@ public class Long2FloatAVLTreeMap extends AbstractLong2FloatMap implements Long2 } state = operator.applyAsLong(state, entry.key); } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); for(Node entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.test(entry.key)) return entry.key; - return 0L; + if(filter.test(entry.key)) return OptionalLong.of(entry.key); + return OptionalLong.empty(); } @Override @@ -1783,7 +1786,7 @@ public class Long2FloatAVLTreeMap extends AbstractLong2FloatMap implements Long2 } @Override - public Long2FloatMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Long2FloatMap.Entry state = null; boolean empty = true; @@ -1795,19 +1798,19 @@ public class Long2FloatAVLTreeMap extends AbstractLong2FloatMap implements Long2 } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Long2FloatMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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 @@ -1902,7 +1905,7 @@ public class Long2FloatAVLTreeMap extends AbstractLong2FloatMap implements Long2 } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -1914,15 +1917,15 @@ public class Long2FloatAVLTreeMap extends AbstractLong2FloatMap implements Long2 } state = operator.applyAsFloat(state, entry.value); } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return 0F; + if(filter.test(entry.value)) return OptionalFloat.of(entry.value); + return OptionalFloat.empty(); } @Override @@ -2238,7 +2241,7 @@ public class Long2FloatAVLTreeMap extends AbstractLong2FloatMap implements Long2 } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -2250,15 +2253,15 @@ public class Long2FloatAVLTreeMap extends AbstractLong2FloatMap implements Long2 } state = operator.applyAsFloat(state, entry.value); } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return 0F; + if(filter.test(entry.value)) return OptionalFloat.of(entry.value); + return OptionalFloat.empty(); } @Override @@ -2399,7 +2402,7 @@ public class Long2FloatAVLTreeMap extends AbstractLong2FloatMap implements Long2 } @Override - public Long2FloatMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Long2FloatMap.Entry state = null; boolean empty = true; @@ -2411,19 +2414,19 @@ public class Long2FloatAVLTreeMap extends AbstractLong2FloatMap implements Long2 } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Long2FloatMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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/main/java/speiger/src/collections/longs/maps/impl/tree/Long2FloatRBTreeMap.java b/src/main/java/speiger/src/collections/longs/maps/impl/tree/Long2FloatRBTreeMap.java index 7b28ce3..7ea88de 100644 --- a/src/main/java/speiger/src/collections/longs/maps/impl/tree/Long2FloatRBTreeMap.java +++ b/src/main/java/speiger/src/collections/longs/maps/impl/tree/Long2FloatRBTreeMap.java @@ -3,11 +3,13 @@ package speiger.src.collections.longs.maps.impl.tree; import java.util.Collections; import java.util.Map; 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; import java.util.function.LongPredicate; +import java.util.OptionalLong; import speiger.src.collections.longs.collections.LongBidirectionalIterator; import speiger.src.collections.longs.functions.LongComparator; @@ -36,6 +38,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectFloatConsumer; import speiger.src.collections.floats.functions.function.FloatPredicate; +import speiger.src.collections.floats.functions.OptionalFloat; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOperator; @@ -1182,7 +1185,7 @@ public class Long2FloatRBTreeMap extends AbstractLong2FloatMap implements Long2F } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -1194,15 +1197,15 @@ public class Long2FloatRBTreeMap extends AbstractLong2FloatMap implements Long2F } state = operator.applyAsLong(state, entry.key); } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); for(Node entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.test(entry.key)) return entry.key; - return 0L; + if(filter.test(entry.key)) return OptionalLong.of(entry.key); + return OptionalLong.empty(); } @Override @@ -1846,7 +1849,7 @@ public class Long2FloatRBTreeMap extends AbstractLong2FloatMap implements Long2F } @Override - public Long2FloatMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Long2FloatMap.Entry state = null; boolean empty = true; @@ -1858,19 +1861,19 @@ public class Long2FloatRBTreeMap extends AbstractLong2FloatMap implements Long2F } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Long2FloatMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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 @@ -1965,7 +1968,7 @@ public class Long2FloatRBTreeMap extends AbstractLong2FloatMap implements Long2F } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -1977,15 +1980,15 @@ public class Long2FloatRBTreeMap extends AbstractLong2FloatMap implements Long2F } state = operator.applyAsFloat(state, entry.value); } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return 0F; + if(filter.test(entry.value)) return OptionalFloat.of(entry.value); + return OptionalFloat.empty(); } @Override @@ -2301,7 +2304,7 @@ public class Long2FloatRBTreeMap extends AbstractLong2FloatMap implements Long2F } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -2313,15 +2316,15 @@ public class Long2FloatRBTreeMap extends AbstractLong2FloatMap implements Long2F } state = operator.applyAsFloat(state, entry.value); } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return 0F; + if(filter.test(entry.value)) return OptionalFloat.of(entry.value); + return OptionalFloat.empty(); } @Override @@ -2462,7 +2465,7 @@ public class Long2FloatRBTreeMap extends AbstractLong2FloatMap implements Long2F } @Override - public Long2FloatMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Long2FloatMap.Entry state = null; boolean empty = true; @@ -2474,19 +2477,19 @@ public class Long2FloatRBTreeMap extends AbstractLong2FloatMap implements Long2F } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Long2FloatMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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/main/java/speiger/src/collections/longs/maps/impl/tree/Long2IntAVLTreeMap.java b/src/main/java/speiger/src/collections/longs/maps/impl/tree/Long2IntAVLTreeMap.java index d03e1d2..30ce3c4 100644 --- a/src/main/java/speiger/src/collections/longs/maps/impl/tree/Long2IntAVLTreeMap.java +++ b/src/main/java/speiger/src/collections/longs/maps/impl/tree/Long2IntAVLTreeMap.java @@ -3,12 +3,15 @@ package speiger.src.collections.longs.maps.impl.tree; import java.util.Collections; import java.util.Map; 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; import java.util.function.LongPredicate; +import java.util.OptionalLong; import java.util.function.IntPredicate; +import java.util.OptionalInt; import speiger.src.collections.longs.collections.LongBidirectionalIterator; @@ -1129,7 +1132,7 @@ public class Long2IntAVLTreeMap extends AbstractLong2IntMap implements Long2IntN } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -1141,15 +1144,15 @@ public class Long2IntAVLTreeMap extends AbstractLong2IntMap implements Long2IntN } state = operator.applyAsLong(state, entry.key); } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); for(Node entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.test(entry.key)) return entry.key; - return 0L; + if(filter.test(entry.key)) return OptionalLong.of(entry.key); + return OptionalLong.empty(); } @Override @@ -1783,7 +1786,7 @@ public class Long2IntAVLTreeMap extends AbstractLong2IntMap implements Long2IntN } @Override - public Long2IntMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Long2IntMap.Entry state = null; boolean empty = true; @@ -1795,19 +1798,19 @@ public class Long2IntAVLTreeMap extends AbstractLong2IntMap implements Long2IntN } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Long2IntMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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 @@ -1902,7 +1905,7 @@ public class Long2IntAVLTreeMap extends AbstractLong2IntMap implements Long2IntN } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -1914,15 +1917,15 @@ public class Long2IntAVLTreeMap extends AbstractLong2IntMap implements Long2IntN } state = operator.applyAsInt(state, entry.value); } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return 0; + if(filter.test(entry.value)) return OptionalInt.of(entry.value); + return OptionalInt.empty(); } @Override @@ -2238,7 +2241,7 @@ public class Long2IntAVLTreeMap extends AbstractLong2IntMap implements Long2IntN } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -2250,15 +2253,15 @@ public class Long2IntAVLTreeMap extends AbstractLong2IntMap implements Long2IntN } state = operator.applyAsInt(state, entry.value); } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return 0; + if(filter.test(entry.value)) return OptionalInt.of(entry.value); + return OptionalInt.empty(); } @Override @@ -2399,7 +2402,7 @@ public class Long2IntAVLTreeMap extends AbstractLong2IntMap implements Long2IntN } @Override - public Long2IntMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Long2IntMap.Entry state = null; boolean empty = true; @@ -2411,19 +2414,19 @@ public class Long2IntAVLTreeMap extends AbstractLong2IntMap implements Long2IntN } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Long2IntMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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/main/java/speiger/src/collections/longs/maps/impl/tree/Long2IntRBTreeMap.java b/src/main/java/speiger/src/collections/longs/maps/impl/tree/Long2IntRBTreeMap.java index 87b405f..8ec8579 100644 --- a/src/main/java/speiger/src/collections/longs/maps/impl/tree/Long2IntRBTreeMap.java +++ b/src/main/java/speiger/src/collections/longs/maps/impl/tree/Long2IntRBTreeMap.java @@ -3,12 +3,15 @@ package speiger.src.collections.longs.maps.impl.tree; import java.util.Collections; import java.util.Map; 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; import java.util.function.LongPredicate; +import java.util.OptionalLong; import java.util.function.IntPredicate; +import java.util.OptionalInt; import speiger.src.collections.longs.collections.LongBidirectionalIterator; import speiger.src.collections.longs.functions.LongComparator; @@ -1182,7 +1185,7 @@ public class Long2IntRBTreeMap extends AbstractLong2IntMap implements Long2IntNa } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -1194,15 +1197,15 @@ public class Long2IntRBTreeMap extends AbstractLong2IntMap implements Long2IntNa } state = operator.applyAsLong(state, entry.key); } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); for(Node entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.test(entry.key)) return entry.key; - return 0L; + if(filter.test(entry.key)) return OptionalLong.of(entry.key); + return OptionalLong.empty(); } @Override @@ -1846,7 +1849,7 @@ public class Long2IntRBTreeMap extends AbstractLong2IntMap implements Long2IntNa } @Override - public Long2IntMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Long2IntMap.Entry state = null; boolean empty = true; @@ -1858,19 +1861,19 @@ public class Long2IntRBTreeMap extends AbstractLong2IntMap implements Long2IntNa } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Long2IntMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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 @@ -1965,7 +1968,7 @@ public class Long2IntRBTreeMap extends AbstractLong2IntMap implements Long2IntNa } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -1977,15 +1980,15 @@ public class Long2IntRBTreeMap extends AbstractLong2IntMap implements Long2IntNa } state = operator.applyAsInt(state, entry.value); } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return 0; + if(filter.test(entry.value)) return OptionalInt.of(entry.value); + return OptionalInt.empty(); } @Override @@ -2301,7 +2304,7 @@ public class Long2IntRBTreeMap extends AbstractLong2IntMap implements Long2IntNa } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -2313,15 +2316,15 @@ public class Long2IntRBTreeMap extends AbstractLong2IntMap implements Long2IntNa } state = operator.applyAsInt(state, entry.value); } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return 0; + if(filter.test(entry.value)) return OptionalInt.of(entry.value); + return OptionalInt.empty(); } @Override @@ -2462,7 +2465,7 @@ public class Long2IntRBTreeMap extends AbstractLong2IntMap implements Long2IntNa } @Override - public Long2IntMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Long2IntMap.Entry state = null; boolean empty = true; @@ -2474,19 +2477,19 @@ public class Long2IntRBTreeMap extends AbstractLong2IntMap implements Long2IntNa } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Long2IntMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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/main/java/speiger/src/collections/longs/maps/impl/tree/Long2LongAVLTreeMap.java b/src/main/java/speiger/src/collections/longs/maps/impl/tree/Long2LongAVLTreeMap.java index d3f8671..8cbf718 100644 --- a/src/main/java/speiger/src/collections/longs/maps/impl/tree/Long2LongAVLTreeMap.java +++ b/src/main/java/speiger/src/collections/longs/maps/impl/tree/Long2LongAVLTreeMap.java @@ -3,11 +3,13 @@ package speiger.src.collections.longs.maps.impl.tree; import java.util.Collections; import java.util.Map; 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; import java.util.function.LongPredicate; +import java.util.OptionalLong; import speiger.src.collections.longs.collections.LongBidirectionalIterator; @@ -1121,7 +1123,7 @@ public class Long2LongAVLTreeMap extends AbstractLong2LongMap implements Long2Lo } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -1133,15 +1135,15 @@ public class Long2LongAVLTreeMap extends AbstractLong2LongMap implements Long2Lo } state = operator.applyAsLong(state, entry.key); } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); for(Node entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.test(entry.key)) return entry.key; - return 0L; + if(filter.test(entry.key)) return OptionalLong.of(entry.key); + return OptionalLong.empty(); } @Override @@ -1775,7 +1777,7 @@ public class Long2LongAVLTreeMap extends AbstractLong2LongMap implements Long2Lo } @Override - public Long2LongMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Long2LongMap.Entry state = null; boolean empty = true; @@ -1787,19 +1789,19 @@ public class Long2LongAVLTreeMap extends AbstractLong2LongMap implements Long2Lo } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Long2LongMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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 @@ -1894,7 +1896,7 @@ public class Long2LongAVLTreeMap extends AbstractLong2LongMap implements Long2Lo } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -1906,15 +1908,15 @@ public class Long2LongAVLTreeMap extends AbstractLong2LongMap implements Long2Lo } state = operator.applyAsLong(state, entry.value); } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return 0L; + if(filter.test(entry.value)) return OptionalLong.of(entry.value); + return OptionalLong.empty(); } @Override @@ -2230,7 +2232,7 @@ public class Long2LongAVLTreeMap extends AbstractLong2LongMap implements Long2Lo } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -2242,15 +2244,15 @@ public class Long2LongAVLTreeMap extends AbstractLong2LongMap implements Long2Lo } state = operator.applyAsLong(state, entry.value); } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return 0L; + if(filter.test(entry.value)) return OptionalLong.of(entry.value); + return OptionalLong.empty(); } @Override @@ -2391,7 +2393,7 @@ public class Long2LongAVLTreeMap extends AbstractLong2LongMap implements Long2Lo } @Override - public Long2LongMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Long2LongMap.Entry state = null; boolean empty = true; @@ -2403,19 +2405,19 @@ public class Long2LongAVLTreeMap extends AbstractLong2LongMap implements Long2Lo } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Long2LongMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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/main/java/speiger/src/collections/longs/maps/impl/tree/Long2LongRBTreeMap.java b/src/main/java/speiger/src/collections/longs/maps/impl/tree/Long2LongRBTreeMap.java index 737e935..6933065 100644 --- a/src/main/java/speiger/src/collections/longs/maps/impl/tree/Long2LongRBTreeMap.java +++ b/src/main/java/speiger/src/collections/longs/maps/impl/tree/Long2LongRBTreeMap.java @@ -3,11 +3,13 @@ package speiger.src.collections.longs.maps.impl.tree; import java.util.Collections; import java.util.Map; 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; import java.util.function.LongPredicate; +import java.util.OptionalLong; import speiger.src.collections.longs.collections.LongBidirectionalIterator; import speiger.src.collections.longs.functions.LongComparator; @@ -1174,7 +1176,7 @@ public class Long2LongRBTreeMap extends AbstractLong2LongMap implements Long2Lon } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -1186,15 +1188,15 @@ public class Long2LongRBTreeMap extends AbstractLong2LongMap implements Long2Lon } state = operator.applyAsLong(state, entry.key); } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); for(Node entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.test(entry.key)) return entry.key; - return 0L; + if(filter.test(entry.key)) return OptionalLong.of(entry.key); + return OptionalLong.empty(); } @Override @@ -1838,7 +1840,7 @@ public class Long2LongRBTreeMap extends AbstractLong2LongMap implements Long2Lon } @Override - public Long2LongMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Long2LongMap.Entry state = null; boolean empty = true; @@ -1850,19 +1852,19 @@ public class Long2LongRBTreeMap extends AbstractLong2LongMap implements Long2Lon } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Long2LongMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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 @@ -1957,7 +1959,7 @@ public class Long2LongRBTreeMap extends AbstractLong2LongMap implements Long2Lon } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -1969,15 +1971,15 @@ public class Long2LongRBTreeMap extends AbstractLong2LongMap implements Long2Lon } state = operator.applyAsLong(state, entry.value); } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return 0L; + if(filter.test(entry.value)) return OptionalLong.of(entry.value); + return OptionalLong.empty(); } @Override @@ -2293,7 +2295,7 @@ public class Long2LongRBTreeMap extends AbstractLong2LongMap implements Long2Lon } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -2305,15 +2307,15 @@ public class Long2LongRBTreeMap extends AbstractLong2LongMap implements Long2Lon } state = operator.applyAsLong(state, entry.value); } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return 0L; + if(filter.test(entry.value)) return OptionalLong.of(entry.value); + return OptionalLong.empty(); } @Override @@ -2454,7 +2456,7 @@ public class Long2LongRBTreeMap extends AbstractLong2LongMap implements Long2Lon } @Override - public Long2LongMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Long2LongMap.Entry state = null; boolean empty = true; @@ -2466,19 +2468,19 @@ public class Long2LongRBTreeMap extends AbstractLong2LongMap implements Long2Lon } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Long2LongMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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/main/java/speiger/src/collections/longs/maps/impl/tree/Long2ObjectAVLTreeMap.java b/src/main/java/speiger/src/collections/longs/maps/impl/tree/Long2ObjectAVLTreeMap.java index e7d4b3d..b82b4ce 100644 --- a/src/main/java/speiger/src/collections/longs/maps/impl/tree/Long2ObjectAVLTreeMap.java +++ b/src/main/java/speiger/src/collections/longs/maps/impl/tree/Long2ObjectAVLTreeMap.java @@ -3,11 +3,13 @@ package speiger.src.collections.longs.maps.impl.tree; import java.util.Collections; import java.util.Map; 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; import java.util.function.LongPredicate; +import java.util.OptionalLong; import speiger.src.collections.longs.collections.LongBidirectionalIterator; @@ -1017,7 +1019,7 @@ public class Long2ObjectAVLTreeMap extends AbstractLong2ObjectMap implemen } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -1029,15 +1031,15 @@ public class Long2ObjectAVLTreeMap extends AbstractLong2ObjectMap implemen } state = operator.applyAsLong(state, entry.key); } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); for(Node entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.test(entry.key)) return entry.key; - return 0L; + if(filter.test(entry.key)) return OptionalLong.of(entry.key); + return OptionalLong.empty(); } @Override @@ -1664,7 +1666,7 @@ public class Long2ObjectAVLTreeMap extends AbstractLong2ObjectMap implemen } @Override - public Long2ObjectMap.Entry reduce(ObjectObjectUnaryOperator, Long2ObjectMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Long2ObjectMap.Entry> operator) { Objects.requireNonNull(operator); Long2ObjectMap.Entry state = null; boolean empty = true; @@ -1676,19 +1678,19 @@ public class Long2ObjectAVLTreeMap extends AbstractLong2ObjectMap implemen } state = operator.apply(state, new BasicEntry<>(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Long2ObjectMap.Entry findFirst(Predicate> filter) { + public Optional> findFirst(Predicate> filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry<>(); for(Node 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 @@ -1783,7 +1785,7 @@ public class Long2ObjectAVLTreeMap extends AbstractLong2ObjectMap implemen } @Override - public V reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); V state = null; boolean empty = true; @@ -1795,15 +1797,15 @@ public class Long2ObjectAVLTreeMap extends AbstractLong2ObjectMap implemen } state = operator.apply(state, entry.value); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public V findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return null; + if(filter.test(entry.value)) return Optional.ofNullable(entry.value); + return Optional.empty(); } @Override @@ -2119,7 +2121,7 @@ public class Long2ObjectAVLTreeMap extends AbstractLong2ObjectMap implemen } @Override - public V reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); V state = null; boolean empty = true; @@ -2131,15 +2133,15 @@ public class Long2ObjectAVLTreeMap extends AbstractLong2ObjectMap implemen } state = operator.apply(state, entry.value); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public V findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return null; + if(filter.test(entry.value)) return Optional.ofNullable(entry.value); + return Optional.empty(); } @Override @@ -2280,7 +2282,7 @@ public class Long2ObjectAVLTreeMap extends AbstractLong2ObjectMap implemen } @Override - public Long2ObjectMap.Entry reduce(ObjectObjectUnaryOperator, Long2ObjectMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Long2ObjectMap.Entry> operator) { Objects.requireNonNull(operator); Long2ObjectMap.Entry state = null; boolean empty = true; @@ -2292,19 +2294,19 @@ public class Long2ObjectAVLTreeMap extends AbstractLong2ObjectMap implemen } state = operator.apply(state, new BasicEntry<>(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Long2ObjectMap.Entry findFirst(Predicate> filter) { + public Optional> findFirst(Predicate> filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry<>(); for(Node 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/main/java/speiger/src/collections/longs/maps/impl/tree/Long2ObjectRBTreeMap.java b/src/main/java/speiger/src/collections/longs/maps/impl/tree/Long2ObjectRBTreeMap.java index 4ae07a4..d0d4b28 100644 --- a/src/main/java/speiger/src/collections/longs/maps/impl/tree/Long2ObjectRBTreeMap.java +++ b/src/main/java/speiger/src/collections/longs/maps/impl/tree/Long2ObjectRBTreeMap.java @@ -3,11 +3,13 @@ package speiger.src.collections.longs.maps.impl.tree; import java.util.Collections; import java.util.Map; 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; import java.util.function.LongPredicate; +import java.util.OptionalLong; import speiger.src.collections.longs.collections.LongBidirectionalIterator; import speiger.src.collections.longs.functions.LongComparator; @@ -1070,7 +1072,7 @@ public class Long2ObjectRBTreeMap extends AbstractLong2ObjectMap implement } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -1082,15 +1084,15 @@ public class Long2ObjectRBTreeMap extends AbstractLong2ObjectMap implement } state = operator.applyAsLong(state, entry.key); } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); for(Node entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.test(entry.key)) return entry.key; - return 0L; + if(filter.test(entry.key)) return OptionalLong.of(entry.key); + return OptionalLong.empty(); } @Override @@ -1727,7 +1729,7 @@ public class Long2ObjectRBTreeMap extends AbstractLong2ObjectMap implement } @Override - public Long2ObjectMap.Entry reduce(ObjectObjectUnaryOperator, Long2ObjectMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Long2ObjectMap.Entry> operator) { Objects.requireNonNull(operator); Long2ObjectMap.Entry state = null; boolean empty = true; @@ -1739,19 +1741,19 @@ public class Long2ObjectRBTreeMap extends AbstractLong2ObjectMap implement } state = operator.apply(state, new BasicEntry<>(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Long2ObjectMap.Entry findFirst(Predicate> filter) { + public Optional> findFirst(Predicate> filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry<>(); for(Node 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 @@ -1846,7 +1848,7 @@ public class Long2ObjectRBTreeMap extends AbstractLong2ObjectMap implement } @Override - public V reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); V state = null; boolean empty = true; @@ -1858,15 +1860,15 @@ public class Long2ObjectRBTreeMap extends AbstractLong2ObjectMap implement } state = operator.apply(state, entry.value); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public V findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return null; + if(filter.test(entry.value)) return Optional.ofNullable(entry.value); + return Optional.empty(); } @Override @@ -2182,7 +2184,7 @@ public class Long2ObjectRBTreeMap extends AbstractLong2ObjectMap implement } @Override - public V reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); V state = null; boolean empty = true; @@ -2194,15 +2196,15 @@ public class Long2ObjectRBTreeMap extends AbstractLong2ObjectMap implement } state = operator.apply(state, entry.value); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public V findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return null; + if(filter.test(entry.value)) return Optional.ofNullable(entry.value); + return Optional.empty(); } @Override @@ -2343,7 +2345,7 @@ public class Long2ObjectRBTreeMap extends AbstractLong2ObjectMap implement } @Override - public Long2ObjectMap.Entry reduce(ObjectObjectUnaryOperator, Long2ObjectMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Long2ObjectMap.Entry> operator) { Objects.requireNonNull(operator); Long2ObjectMap.Entry state = null; boolean empty = true; @@ -2355,19 +2357,19 @@ public class Long2ObjectRBTreeMap extends AbstractLong2ObjectMap implement } state = operator.apply(state, new BasicEntry<>(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Long2ObjectMap.Entry findFirst(Predicate> filter) { + public Optional> findFirst(Predicate> filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry<>(); for(Node 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/main/java/speiger/src/collections/longs/maps/impl/tree/Long2ShortAVLTreeMap.java b/src/main/java/speiger/src/collections/longs/maps/impl/tree/Long2ShortAVLTreeMap.java index b40812d..2a5dbdf 100644 --- a/src/main/java/speiger/src/collections/longs/maps/impl/tree/Long2ShortAVLTreeMap.java +++ b/src/main/java/speiger/src/collections/longs/maps/impl/tree/Long2ShortAVLTreeMap.java @@ -3,11 +3,13 @@ package speiger.src.collections.longs.maps.impl.tree; import java.util.Collections; import java.util.Map; 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; import java.util.function.LongPredicate; +import java.util.OptionalLong; import speiger.src.collections.longs.collections.LongBidirectionalIterator; @@ -37,6 +39,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectShortConsumer; import speiger.src.collections.shorts.functions.function.ShortPredicate; +import speiger.src.collections.shorts.functions.OptionalShort; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOperator; @@ -1129,7 +1132,7 @@ public class Long2ShortAVLTreeMap extends AbstractLong2ShortMap implements Long2 } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -1141,15 +1144,15 @@ public class Long2ShortAVLTreeMap extends AbstractLong2ShortMap implements Long2 } state = operator.applyAsLong(state, entry.key); } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); for(Node entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.test(entry.key)) return entry.key; - return 0L; + if(filter.test(entry.key)) return OptionalLong.of(entry.key); + return OptionalLong.empty(); } @Override @@ -1783,7 +1786,7 @@ public class Long2ShortAVLTreeMap extends AbstractLong2ShortMap implements Long2 } @Override - public Long2ShortMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Long2ShortMap.Entry state = null; boolean empty = true; @@ -1795,19 +1798,19 @@ public class Long2ShortAVLTreeMap extends AbstractLong2ShortMap implements Long2 } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Long2ShortMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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 @@ -1902,7 +1905,7 @@ public class Long2ShortAVLTreeMap extends AbstractLong2ShortMap implements Long2 } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -1914,15 +1917,15 @@ public class Long2ShortAVLTreeMap extends AbstractLong2ShortMap implements Long2 } state = operator.applyAsShort(state, entry.value); } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return (short)0; + if(filter.test(entry.value)) return OptionalShort.of(entry.value); + return OptionalShort.empty(); } @Override @@ -2238,7 +2241,7 @@ public class Long2ShortAVLTreeMap extends AbstractLong2ShortMap implements Long2 } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -2250,15 +2253,15 @@ public class Long2ShortAVLTreeMap extends AbstractLong2ShortMap implements Long2 } state = operator.applyAsShort(state, entry.value); } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return (short)0; + if(filter.test(entry.value)) return OptionalShort.of(entry.value); + return OptionalShort.empty(); } @Override @@ -2399,7 +2402,7 @@ public class Long2ShortAVLTreeMap extends AbstractLong2ShortMap implements Long2 } @Override - public Long2ShortMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Long2ShortMap.Entry state = null; boolean empty = true; @@ -2411,19 +2414,19 @@ public class Long2ShortAVLTreeMap extends AbstractLong2ShortMap implements Long2 } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Long2ShortMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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/main/java/speiger/src/collections/longs/maps/impl/tree/Long2ShortRBTreeMap.java b/src/main/java/speiger/src/collections/longs/maps/impl/tree/Long2ShortRBTreeMap.java index 80373e3..648975d 100644 --- a/src/main/java/speiger/src/collections/longs/maps/impl/tree/Long2ShortRBTreeMap.java +++ b/src/main/java/speiger/src/collections/longs/maps/impl/tree/Long2ShortRBTreeMap.java @@ -3,11 +3,13 @@ package speiger.src.collections.longs.maps.impl.tree; import java.util.Collections; import java.util.Map; 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; import java.util.function.LongPredicate; +import java.util.OptionalLong; import speiger.src.collections.longs.collections.LongBidirectionalIterator; import speiger.src.collections.longs.functions.LongComparator; @@ -36,6 +38,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectShortConsumer; import speiger.src.collections.shorts.functions.function.ShortPredicate; +import speiger.src.collections.shorts.functions.OptionalShort; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOperator; @@ -1182,7 +1185,7 @@ public class Long2ShortRBTreeMap extends AbstractLong2ShortMap implements Long2S } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -1194,15 +1197,15 @@ public class Long2ShortRBTreeMap extends AbstractLong2ShortMap implements Long2S } state = operator.applyAsLong(state, entry.key); } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); for(Node entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.test(entry.key)) return entry.key; - return 0L; + if(filter.test(entry.key)) return OptionalLong.of(entry.key); + return OptionalLong.empty(); } @Override @@ -1846,7 +1849,7 @@ public class Long2ShortRBTreeMap extends AbstractLong2ShortMap implements Long2S } @Override - public Long2ShortMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Long2ShortMap.Entry state = null; boolean empty = true; @@ -1858,19 +1861,19 @@ public class Long2ShortRBTreeMap extends AbstractLong2ShortMap implements Long2S } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Long2ShortMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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 @@ -1965,7 +1968,7 @@ public class Long2ShortRBTreeMap extends AbstractLong2ShortMap implements Long2S } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -1977,15 +1980,15 @@ public class Long2ShortRBTreeMap extends AbstractLong2ShortMap implements Long2S } state = operator.applyAsShort(state, entry.value); } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return (short)0; + if(filter.test(entry.value)) return OptionalShort.of(entry.value); + return OptionalShort.empty(); } @Override @@ -2301,7 +2304,7 @@ public class Long2ShortRBTreeMap extends AbstractLong2ShortMap implements Long2S } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -2313,15 +2316,15 @@ public class Long2ShortRBTreeMap extends AbstractLong2ShortMap implements Long2S } state = operator.applyAsShort(state, entry.value); } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return (short)0; + if(filter.test(entry.value)) return OptionalShort.of(entry.value); + return OptionalShort.empty(); } @Override @@ -2462,7 +2465,7 @@ public class Long2ShortRBTreeMap extends AbstractLong2ShortMap implements Long2S } @Override - public Long2ShortMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Long2ShortMap.Entry state = null; boolean empty = true; @@ -2474,19 +2477,19 @@ public class Long2ShortRBTreeMap extends AbstractLong2ShortMap implements Long2S } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Long2ShortMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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/main/java/speiger/src/collections/longs/queues/LongArrayFIFOQueue.java b/src/main/java/speiger/src/collections/longs/queues/LongArrayFIFOQueue.java index 6dbb147..bfa3f20 100644 --- a/src/main/java/speiger/src/collections/longs/queues/LongArrayFIFOQueue.java +++ b/src/main/java/speiger/src/collections/longs/queues/LongArrayFIFOQueue.java @@ -2,6 +2,7 @@ package speiger.src.collections.longs.queues; import java.util.Arrays; import java.util.Objects; +import java.util.OptionalLong; import java.util.NoSuchElementException; import java.util.function.LongPredicate; @@ -278,17 +279,17 @@ public class LongArrayFIFOQueue extends AbstractLongPriorityQueue implements Lon } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); for(int i = 0,m=size();i= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor)); + } + + @Override + public void addLast(long o) { + if(strategy.equals(o, 0L)) { + if(containsNull) return; + containsNull = true; + onNodeAdded(nullIndex); + } + else { + int pos = HashUtil.mix(strategy.hashCode(o)) & mask; + while(!strategy.equals(keys[pos], 0L)) { + if(strategy.equals(keys[pos], o)) return; + pos = ++pos & mask; + } + keys[pos] = o; + onNodeAdded(pos); + } + if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor)); + } + @Override public boolean addAndMoveToFirst(long o) { if(strategy.equals(o, 0L)) { @@ -618,7 +659,7 @@ public class LongLinkedOpenCustomHashSet extends LongOpenCustomHashSet implement } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -631,18 +672,18 @@ public class LongLinkedOpenCustomHashSet extends LongOpenCustomHashSet implement else state = operator.applyAsLong(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); int index = firstIndex; while(index != -1) { - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalLong.of(keys[index]); index = (int)links[index]; } - return 0L; + return OptionalLong.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/longs/sets/LongLinkedOpenHashSet.java b/src/main/java/speiger/src/collections/longs/sets/LongLinkedOpenHashSet.java index 5d7aca3..109ed7d 100644 --- a/src/main/java/speiger/src/collections/longs/sets/LongLinkedOpenHashSet.java +++ b/src/main/java/speiger/src/collections/longs/sets/LongLinkedOpenHashSet.java @@ -5,6 +5,7 @@ import java.util.Collection; import java.util.Iterator; import java.util.NoSuchElementException; import java.util.Objects; +import java.util.OptionalLong; import java.util.function.LongPredicate; import speiger.src.collections.longs.collections.LongCollection; @@ -183,6 +184,46 @@ public class LongLinkedOpenHashSet extends LongOpenHashSet implements LongOrdere while(iterator.hasNext()) add(iterator.nextLong()); } + @Override + public void addFirst(long o) { + if(o == 0) { + if(containsNull) return; + containsNull = true; + onNodeAdded(nullIndex); + moveToFirstIndex(nullIndex); + } + else { + int pos = HashUtil.mix(Long.hashCode(o)) & mask; + while(keys[pos] != 0) { + if(keys[pos] == o) return; + pos = ++pos & mask; + } + keys[pos] = o; + onNodeAdded(pos); + moveToFirstIndex(pos); + } + if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor)); + } + + @Override + public void addLast(long o) { + if(o == 0) { + if(containsNull) return; + containsNull = true; + onNodeAdded(nullIndex); + } + else { + int pos = HashUtil.mix(Long.hashCode(o)) & mask; + while(keys[pos] != 0) { + if(keys[pos] == o) return; + pos = ++pos & mask; + } + keys[pos] = o; + onNodeAdded(pos); + } + if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor)); + } + @Override public boolean addAndMoveToFirst(long o) { if(o == 0) { @@ -471,7 +512,7 @@ public class LongLinkedOpenHashSet extends LongOpenHashSet implements LongOrdere } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -484,18 +525,18 @@ public class LongLinkedOpenHashSet extends LongOpenHashSet implements LongOrdere else state = operator.applyAsLong(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); int index = firstIndex; while(index != -1) { - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalLong.of(keys[index]); index = (int)links[index]; } - return 0L; + return OptionalLong.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/longs/sets/LongOpenCustomHashSet.java b/src/main/java/speiger/src/collections/longs/sets/LongOpenCustomHashSet.java index 6840d52..bfbd7bc 100644 --- a/src/main/java/speiger/src/collections/longs/sets/LongOpenCustomHashSet.java +++ b/src/main/java/speiger/src/collections/longs/sets/LongOpenCustomHashSet.java @@ -6,6 +6,7 @@ import java.util.ConcurrentModificationException; import java.util.Iterator; import java.util.NoSuchElementException; import java.util.Objects; +import java.util.OptionalLong; import java.util.function.LongPredicate; import speiger.src.collections.longs.collections.LongCollection; @@ -546,7 +547,7 @@ public class LongOpenCustomHashSet extends AbstractLongSet implements ITrimmable } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -563,18 +564,18 @@ public class LongOpenCustomHashSet extends AbstractLongSet implements ITrimmable } state = operator.applyAsLong(state, keys[i]); } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0L; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OptionalLong.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OptionalLong.of(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], 0L) && filter.test(keys[i])) return keys[i]; + if(!strategy.equals(keys[i], 0L) && filter.test(keys[i])) return OptionalLong.of(keys[i]); } - return 0L; + return OptionalLong.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/longs/sets/LongOpenHashSet.java b/src/main/java/speiger/src/collections/longs/sets/LongOpenHashSet.java index 71c91bf..37df91b 100644 --- a/src/main/java/speiger/src/collections/longs/sets/LongOpenHashSet.java +++ b/src/main/java/speiger/src/collections/longs/sets/LongOpenHashSet.java @@ -6,6 +6,7 @@ import java.util.ConcurrentModificationException; import java.util.Iterator; import java.util.NoSuchElementException; import java.util.Objects; +import java.util.OptionalLong; import java.util.function.LongPredicate; import speiger.src.collections.longs.collections.LongCollection; @@ -428,7 +429,7 @@ public class LongOpenHashSet extends AbstractLongSet implements ITrimmable } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -445,18 +446,18 @@ public class LongOpenHashSet extends AbstractLongSet implements ITrimmable } state = operator.applyAsLong(state, keys[i]); } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0L; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OptionalLong.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OptionalLong.of(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(keys[i] != 0 && filter.test(keys[i])) return keys[i]; + if(keys[i] != 0 && filter.test(keys[i])) return OptionalLong.of(keys[i]); } - return 0L; + return OptionalLong.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/longs/sets/LongOrderedSet.java b/src/main/java/speiger/src/collections/longs/sets/LongOrderedSet.java index d21ceff..fd1a9ed 100644 --- a/src/main/java/speiger/src/collections/longs/sets/LongOrderedSet.java +++ b/src/main/java/speiger/src/collections/longs/sets/LongOrderedSet.java @@ -1,6 +1,7 @@ package speiger.src.collections.longs.sets; import speiger.src.collections.longs.collections.LongBidirectionalIterator; +import speiger.src.collections.longs.collections.LongOrderedCollection; import speiger.src.collections.longs.collections.LongSplititerator; import speiger.src.collections.longs.utils.LongSplititerators; import speiger.src.collections.longs.sets.AbstractLongSet.ReversedLongOrderedSet; @@ -13,7 +14,7 @@ import speiger.src.collections.longs.utils.LongSets; * This interface provides basic access to such functions while also providing some Sorted/NaivgableSet implementations that still fit into here. * */ -public interface LongOrderedSet extends LongSet +public interface LongOrderedSet extends LongSet, LongOrderedCollection { /** * A customized add method that allows you to insert into the first index. diff --git a/src/main/java/speiger/src/collections/longs/sets/LongRBTreeSet.java b/src/main/java/speiger/src/collections/longs/sets/LongRBTreeSet.java index 3dc05dc..60268f8 100644 --- a/src/main/java/speiger/src/collections/longs/sets/LongRBTreeSet.java +++ b/src/main/java/speiger/src/collections/longs/sets/LongRBTreeSet.java @@ -2,6 +2,7 @@ package speiger.src.collections.longs.sets; import java.util.Collection; import java.util.Collections; +import java.util.OptionalLong; import java.util.Iterator; import java.util.NoSuchElementException; import java.util.Objects; @@ -334,7 +335,7 @@ public class LongRBTreeSet extends AbstractLongSet implements LongNavigableSet } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -346,16 +347,16 @@ public class LongRBTreeSet extends AbstractLongSet implements LongNavigableSet } state = operator.applyAsLong(state, entry.key); } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); for(Entry entry = first;entry != null;entry = entry.next()) { - if(filter.test(entry.key)) return entry.key; + if(filter.test(entry.key)) return OptionalLong.of(entry.key); } - return 0L; + return OptionalLong.empty(); } @Override @@ -1241,7 +1242,7 @@ public class LongRBTreeSet extends AbstractLongSet implements LongNavigableSet } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -1253,16 +1254,16 @@ public class LongRBTreeSet extends AbstractLongSet implements LongNavigableSet } state = operator.applyAsLong(state, entry.key); } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); for(Entry entry = start();entry != null && inRange(entry.key);entry = next(entry)) { - if(filter.test(entry.key)) return entry.key; + if(filter.test(entry.key)) return OptionalLong.of(entry.key); } - return 0L; + return OptionalLong.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/longs/utils/LongCollections.java b/src/main/java/speiger/src/collections/longs/utils/LongCollections.java index f082a75..50b97c9 100644 --- a/src/main/java/speiger/src/collections/longs/utils/LongCollections.java +++ b/src/main/java/speiger/src/collections/longs/utils/LongCollections.java @@ -6,6 +6,7 @@ import java.util.ConcurrentModificationException; import java.util.Iterator; import java.util.NoSuchElementException; import java.util.Objects; +import java.util.OptionalLong; import java.util.function.Predicate; import java.util.function.LongPredicate; import java.util.function.Consumer; @@ -757,9 +758,9 @@ public class LongCollections @Override public long reduce(long identity, LongLongUnaryOperator operator) { synchronized(mutex) { return c.reduce(identity, operator); } } @Override - public long reduce(LongLongUnaryOperator operator) { synchronized(mutex) { return c.reduce(operator); } } + public OptionalLong reduce(LongLongUnaryOperator operator) { synchronized(mutex) { return c.reduce(operator); } } @Override - public long findFirst(LongPredicate filter) { synchronized(mutex) { return c.findFirst(filter); } } + public OptionalLong findFirst(LongPredicate filter) { synchronized(mutex) { return c.findFirst(filter); } } @Override public int count(LongPredicate filter) { synchronized(mutex) { return c.count(filter); } } } @@ -887,9 +888,9 @@ public class LongCollections @Override public long reduce(long identity, LongLongUnaryOperator operator) { return c.reduce(identity, operator); } @Override - public long reduce(LongLongUnaryOperator operator) { return c.reduce(operator); } + public OptionalLong reduce(LongLongUnaryOperator operator) { return c.reduce(operator); } @Override - public long findFirst(LongPredicate filter) { return c.findFirst(filter); } + public OptionalLong findFirst(LongPredicate filter) { return c.findFirst(filter); } @Override public int count(LongPredicate filter) { return c.count(filter); } } diff --git a/src/main/java/speiger/src/collections/longs/utils/LongPriorityQueues.java b/src/main/java/speiger/src/collections/longs/utils/LongPriorityQueues.java index c739a1e..547373d 100644 --- a/src/main/java/speiger/src/collections/longs/utils/LongPriorityQueues.java +++ b/src/main/java/speiger/src/collections/longs/utils/LongPriorityQueues.java @@ -1,13 +1,14 @@ package speiger.src.collections.longs.utils; import java.util.function.LongPredicate; - +import java.util.OptionalLong; import speiger.src.collections.longs.collections.LongIterator; import speiger.src.collections.longs.collections.LongCollection; import speiger.src.collections.longs.functions.LongComparator; import speiger.src.collections.longs.queues.LongPriorityDequeue; import speiger.src.collections.longs.queues.LongPriorityQueue; import speiger.src.collections.longs.functions.LongConsumer; +import speiger.src.collections.longs.functions.function.LongLongUnaryOperator; import speiger.src.collections.objects.functions.consumer.ObjectLongConsumer; /** @@ -113,7 +114,11 @@ public class LongPriorityQueues @Override public boolean matchesAll(LongPredicate filter) { synchronized(mutex) { return queue.matchesAll(filter); } } @Override - public long findFirst(LongPredicate filter) { synchronized(mutex) { return queue.findFirst(filter); } } + public OptionalLong findFirst(LongPredicate filter) { synchronized(mutex) { return queue.findFirst(filter); } } + @Override + public long reduce(long identity, LongLongUnaryOperator operator) { synchronized(mutex) { return queue.reduce(identity, operator); } } + @Override + public OptionalLong reduce(LongLongUnaryOperator operator) { synchronized(mutex) { return queue.reduce(operator); } } @Override public int count(LongPredicate filter) { synchronized(mutex) { return queue.count(filter); } } } diff --git a/src/main/java/speiger/src/collections/longs/utils/LongSets.java b/src/main/java/speiger/src/collections/longs/utils/LongSets.java index ceba2ee..8342694 100644 --- a/src/main/java/speiger/src/collections/longs/utils/LongSets.java +++ b/src/main/java/speiger/src/collections/longs/utils/LongSets.java @@ -301,6 +301,10 @@ public class LongSets s = c; } + @Override + public void addFirst(long o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(long o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(long o) { throw new UnsupportedOperationException(); } @Override @@ -573,7 +577,11 @@ public class LongSets super(c, mutex); s = c; } - + + @Override + public void addFirst(long o) { synchronized(mutex) { s.addFirst(o); } } + @Override + public void addLast(long o) { synchronized(mutex) { s.addLast(o); } } @Override public boolean addAndMoveToFirst(long o) { synchronized(mutex) { return s.addAndMoveToFirst(o); } } @Override diff --git a/src/main/java/speiger/src/collections/longs/utils/maps/Long2BooleanMaps.java b/src/main/java/speiger/src/collections/longs/utils/maps/Long2BooleanMaps.java index c1f7220..142534f 100644 --- a/src/main/java/speiger/src/collections/longs/utils/maps/Long2BooleanMaps.java +++ b/src/main/java/speiger/src/collections/longs/utils/maps/Long2BooleanMaps.java @@ -608,7 +608,11 @@ public class Long2BooleanMaps super(c); set = c; } - + + @Override + public void addFirst(Long2BooleanMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Long2BooleanMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Long2BooleanMap.Entry o) { throw new UnsupportedOperationException(); } @Override diff --git a/src/main/java/speiger/src/collections/longs/utils/maps/Long2ByteMaps.java b/src/main/java/speiger/src/collections/longs/utils/maps/Long2ByteMaps.java index f970a15..c385308 100644 --- a/src/main/java/speiger/src/collections/longs/utils/maps/Long2ByteMaps.java +++ b/src/main/java/speiger/src/collections/longs/utils/maps/Long2ByteMaps.java @@ -620,7 +620,11 @@ public class Long2ByteMaps super(c); set = c; } - + + @Override + public void addFirst(Long2ByteMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Long2ByteMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Long2ByteMap.Entry o) { throw new UnsupportedOperationException(); } @Override diff --git a/src/main/java/speiger/src/collections/longs/utils/maps/Long2CharMaps.java b/src/main/java/speiger/src/collections/longs/utils/maps/Long2CharMaps.java index f129662..a08c5d9 100644 --- a/src/main/java/speiger/src/collections/longs/utils/maps/Long2CharMaps.java +++ b/src/main/java/speiger/src/collections/longs/utils/maps/Long2CharMaps.java @@ -620,7 +620,11 @@ public class Long2CharMaps super(c); set = c; } - + + @Override + public void addFirst(Long2CharMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Long2CharMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Long2CharMap.Entry o) { throw new UnsupportedOperationException(); } @Override diff --git a/src/main/java/speiger/src/collections/longs/utils/maps/Long2DoubleMaps.java b/src/main/java/speiger/src/collections/longs/utils/maps/Long2DoubleMaps.java index f2d69a9..9cfc447 100644 --- a/src/main/java/speiger/src/collections/longs/utils/maps/Long2DoubleMaps.java +++ b/src/main/java/speiger/src/collections/longs/utils/maps/Long2DoubleMaps.java @@ -620,7 +620,11 @@ public class Long2DoubleMaps super(c); set = c; } - + + @Override + public void addFirst(Long2DoubleMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Long2DoubleMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Long2DoubleMap.Entry o) { throw new UnsupportedOperationException(); } @Override diff --git a/src/main/java/speiger/src/collections/longs/utils/maps/Long2FloatMaps.java b/src/main/java/speiger/src/collections/longs/utils/maps/Long2FloatMaps.java index b9f782f..52119f0 100644 --- a/src/main/java/speiger/src/collections/longs/utils/maps/Long2FloatMaps.java +++ b/src/main/java/speiger/src/collections/longs/utils/maps/Long2FloatMaps.java @@ -620,7 +620,11 @@ public class Long2FloatMaps super(c); set = c; } - + + @Override + public void addFirst(Long2FloatMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Long2FloatMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Long2FloatMap.Entry o) { throw new UnsupportedOperationException(); } @Override diff --git a/src/main/java/speiger/src/collections/longs/utils/maps/Long2IntMaps.java b/src/main/java/speiger/src/collections/longs/utils/maps/Long2IntMaps.java index 6834eea..b6dc083 100644 --- a/src/main/java/speiger/src/collections/longs/utils/maps/Long2IntMaps.java +++ b/src/main/java/speiger/src/collections/longs/utils/maps/Long2IntMaps.java @@ -620,7 +620,11 @@ public class Long2IntMaps super(c); set = c; } - + + @Override + public void addFirst(Long2IntMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Long2IntMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Long2IntMap.Entry o) { throw new UnsupportedOperationException(); } @Override diff --git a/src/main/java/speiger/src/collections/longs/utils/maps/Long2LongMaps.java b/src/main/java/speiger/src/collections/longs/utils/maps/Long2LongMaps.java index ed12013..8056595 100644 --- a/src/main/java/speiger/src/collections/longs/utils/maps/Long2LongMaps.java +++ b/src/main/java/speiger/src/collections/longs/utils/maps/Long2LongMaps.java @@ -619,7 +619,11 @@ public class Long2LongMaps super(c); set = c; } - + + @Override + public void addFirst(Long2LongMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Long2LongMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Long2LongMap.Entry o) { throw new UnsupportedOperationException(); } @Override diff --git a/src/main/java/speiger/src/collections/longs/utils/maps/Long2ObjectMaps.java b/src/main/java/speiger/src/collections/longs/utils/maps/Long2ObjectMaps.java index 0d1c2ba..1a2f56d 100644 --- a/src/main/java/speiger/src/collections/longs/utils/maps/Long2ObjectMaps.java +++ b/src/main/java/speiger/src/collections/longs/utils/maps/Long2ObjectMaps.java @@ -611,7 +611,11 @@ public class Long2ObjectMaps super(c); set = c; } - + + @Override + public void addFirst(Long2ObjectMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Long2ObjectMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Long2ObjectMap.Entry o) { throw new UnsupportedOperationException(); } @Override diff --git a/src/main/java/speiger/src/collections/longs/utils/maps/Long2ShortMaps.java b/src/main/java/speiger/src/collections/longs/utils/maps/Long2ShortMaps.java index 2b60591..593c611 100644 --- a/src/main/java/speiger/src/collections/longs/utils/maps/Long2ShortMaps.java +++ b/src/main/java/speiger/src/collections/longs/utils/maps/Long2ShortMaps.java @@ -620,7 +620,11 @@ public class Long2ShortMaps super(c); set = c; } - + + @Override + public void addFirst(Long2ShortMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Long2ShortMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Long2ShortMap.Entry o) { throw new UnsupportedOperationException(); } @Override diff --git a/src/main/java/speiger/src/collections/objects/collections/ObjectIterable.java b/src/main/java/speiger/src/collections/objects/collections/ObjectIterable.java index 11209e4..2f9dbdb 100644 --- a/src/main/java/speiger/src/collections/objects/collections/ObjectIterable.java +++ b/src/main/java/speiger/src/collections/objects/collections/ObjectIterable.java @@ -3,6 +3,7 @@ package speiger.src.collections.objects.collections; import java.util.Objects; import java.util.function.Consumer; import java.util.function.Predicate; +import java.util.Optional; import java.util.function.BiFunction; import java.util.function.IntFunction; @@ -330,13 +331,13 @@ public interface ObjectIterable extends Iterable * @param filter that should be applied * @return the found value or the null equivalent variant. */ - default T findFirst(Predicate filter) { + default Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); for(ObjectIterator iter = iterator();iter.hasNext();) { T entry = iter.next(); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } - return null; + return Optional.empty(); } /** @@ -362,7 +363,7 @@ public interface ObjectIterable extends Iterable * @param operator the operation that should be applied * @return the reduction result, returns null value if nothing was found */ - default T reduce(ObjectObjectUnaryOperator operator) { + default Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); T state = null; boolean empty = true; @@ -374,7 +375,7 @@ public interface ObjectIterable extends Iterable } state = operator.apply(state, iter.next()); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } /** diff --git a/src/main/java/speiger/src/collections/objects/lists/CopyOnWriteObjectArrayList.java b/src/main/java/speiger/src/collections/objects/lists/CopyOnWriteObjectArrayList.java index 02fc025..43f5d5a 100644 --- a/src/main/java/speiger/src/collections/objects/lists/CopyOnWriteObjectArrayList.java +++ b/src/main/java/speiger/src/collections/objects/lists/CopyOnWriteObjectArrayList.java @@ -6,6 +6,7 @@ import java.util.Collection; import java.util.Iterator; import java.util.NoSuchElementException; import java.util.Objects; +import java.util.Optional; import java.util.concurrent.locks.ReentrantLock; import java.util.function.Consumer; import java.util.function.Supplier; @@ -623,13 +624,13 @@ public class CopyOnWriteObjectArrayList extends AbstractObjectList impleme } @Override - public T findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); T[] data = this.data; for(int i = 0,m=data.length;i extends AbstractObjectList impleme } @Override - public T reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); T[] data = this.data; T state = null; @@ -657,7 +658,7 @@ public class CopyOnWriteObjectArrayList extends AbstractObjectList impleme } state = operator.apply(state, data[i]); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override diff --git a/src/main/java/speiger/src/collections/objects/lists/ImmutableObjectList.java b/src/main/java/speiger/src/collections/objects/lists/ImmutableObjectList.java index 39376ed..7177004 100644 --- a/src/main/java/speiger/src/collections/objects/lists/ImmutableObjectList.java +++ b/src/main/java/speiger/src/collections/objects/lists/ImmutableObjectList.java @@ -5,6 +5,7 @@ import java.util.Comparator; import java.util.Collection; import java.util.NoSuchElementException; import java.util.Objects; +import java.util.Optional; import java.util.function.BiFunction; import java.util.function.Consumer; import java.util.function.Predicate; @@ -238,12 +239,12 @@ public class ImmutableObjectList extends AbstractObjectList } @Override - public T findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); for(int i = 0,m=data.length;i extends AbstractObjectList } @Override - public T reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); T state = null; boolean empty = true; @@ -269,7 +270,7 @@ public class ImmutableObjectList extends AbstractObjectList } state = operator.apply(state, data[i]); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override diff --git a/src/main/java/speiger/src/collections/objects/lists/ObjectArrayList.java b/src/main/java/speiger/src/collections/objects/lists/ObjectArrayList.java index e01b150..837a37b 100644 --- a/src/main/java/speiger/src/collections/objects/lists/ObjectArrayList.java +++ b/src/main/java/speiger/src/collections/objects/lists/ObjectArrayList.java @@ -5,6 +5,7 @@ import java.util.Comparator; import java.util.Collection; import java.util.Iterator; import java.util.Objects; +import java.util.Optional; import java.util.function.Consumer; import java.util.function.BiFunction; import java.util.function.IntFunction; @@ -517,12 +518,12 @@ public class ObjectArrayList extends AbstractObjectList implements IObject } @Override - public T findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); for(int i = 0;i extends AbstractObjectList implements IObject } @Override - public T reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); T state = null; boolean empty = true; @@ -548,7 +549,7 @@ public class ObjectArrayList extends AbstractObjectList implements IObject } state = operator.apply(state, data[i]); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override diff --git a/src/main/java/speiger/src/collections/objects/lists/ObjectLinkedList.java b/src/main/java/speiger/src/collections/objects/lists/ObjectLinkedList.java index 696c47c..67792fa 100644 --- a/src/main/java/speiger/src/collections/objects/lists/ObjectLinkedList.java +++ b/src/main/java/speiger/src/collections/objects/lists/ObjectLinkedList.java @@ -1,10 +1,10 @@ package speiger.src.collections.objects.lists; -import java.util.Comparator; -import java.util.function.BiFunction; +import java.util.Comparator;import java.util.function.BiFunction; import java.util.Collection; import java.util.Iterator; import java.util.Objects; +import java.util.Optional; import java.util.NoSuchElementException; import java.util.Spliterator; import java.util.function.Consumer; @@ -388,12 +388,12 @@ public class ObjectLinkedList extends AbstractObjectList implements Object } @Override - public T findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); for(Entry entry = first;entry != null;entry = entry.next) { - if(filter.test(entry.value)) return entry.value; + if(filter.test(entry.value)) return Optional.ofNullable(entry.value); } - return null; + return Optional.empty(); } @Override @@ -407,7 +407,7 @@ public class ObjectLinkedList extends AbstractObjectList implements Object } @Override - public T reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); T state = null; boolean empty = true; @@ -419,7 +419,7 @@ public class ObjectLinkedList extends AbstractObjectList implements Object } state = operator.apply(state, entry.value); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override diff --git a/src/main/java/speiger/src/collections/objects/maps/impl/concurrent/Object2BooleanConcurrentOpenHashMap.java b/src/main/java/speiger/src/collections/objects/maps/impl/concurrent/Object2BooleanConcurrentOpenHashMap.java index bc5d081..c5a965e 100644 --- a/src/main/java/speiger/src/collections/objects/maps/impl/concurrent/Object2BooleanConcurrentOpenHashMap.java +++ b/src/main/java/speiger/src/collections/objects/maps/impl/concurrent/Object2BooleanConcurrentOpenHashMap.java @@ -4,6 +4,7 @@ import java.util.Arrays; import java.util.Map; import java.util.NoSuchElementException; import java.util.Objects; +import java.util.Optional; import java.util.concurrent.locks.StampedLock; import java.util.function.Consumer; import java.util.function.Predicate; @@ -29,6 +30,7 @@ import speiger.src.collections.booleans.functions.BooleanConsumer; import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.booleans.functions.function.BooleanPredicate; +import speiger.src.collections.booleans.functions.OptionalBoolean; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.objects.sets.AbstractObjectSet; import speiger.src.collections.objects.sets.ObjectSet; @@ -739,7 +741,7 @@ public class Object2BooleanConcurrentOpenHashMap extends AbstractObject2Boole } @Override - public Object2BooleanMap.Entry reduce(ObjectObjectUnaryOperator, Object2BooleanMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Object2BooleanMap.Entry> operator) { Objects.requireNonNull(operator); Object2BooleanMap.Entry state = null; boolean empty = true; @@ -763,11 +765,11 @@ public class Object2BooleanConcurrentOpenHashMap extends AbstractObject2Boole seg.unlockRead(stamp); } } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Object2BooleanMap.Entry findFirst(Predicate> filter) { + public Optional> findFirst(Predicate> filter) { Objects.requireNonNull(filter); MapEntry entry = new MapEntry(); for(int i = 0,m=segments.length;i extends AbstractObject2Boole try { while(index != -1) { entry.set(index, i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); index = (int)seg.links[index]; } } @@ -785,7 +787,7 @@ public class Object2BooleanConcurrentOpenHashMap extends AbstractObject2Boole seg.unlockRead(stamp); } } - return null; + return Optional.empty(); } @Override @@ -1047,7 +1049,7 @@ public class Object2BooleanConcurrentOpenHashMap extends AbstractObject2Boole } @Override - public T reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); T state = null; boolean empty = true; @@ -1071,11 +1073,11 @@ public class Object2BooleanConcurrentOpenHashMap extends AbstractObject2Boole seg.unlockRead(stamp); } } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public T findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); for(int i = 0,m=segments.length;i seg = segments[i]; @@ -1083,7 +1085,7 @@ public class Object2BooleanConcurrentOpenHashMap extends AbstractObject2Boole try { int index = seg.firstIndex; while(index != -1){ - if(filter.test(seg.keys[index])) return seg.keys[index]; + if(filter.test(seg.keys[index])) return Optional.ofNullable(seg.keys[index]); index = (int)seg.links[index]; } } @@ -1091,7 +1093,7 @@ public class Object2BooleanConcurrentOpenHashMap extends AbstractObject2Boole seg.unlockRead(stamp); } } - return null; + return Optional.empty(); } @Override @@ -1282,7 +1284,7 @@ public class Object2BooleanConcurrentOpenHashMap extends AbstractObject2Boole } @Override - public boolean reduce(BooleanBooleanUnaryOperator operator) { + public OptionalBoolean reduce(BooleanBooleanUnaryOperator operator) { Objects.requireNonNull(operator); boolean state = false; boolean empty = true; @@ -1306,20 +1308,20 @@ public class Object2BooleanConcurrentOpenHashMap extends AbstractObject2Boole seg.unlockRead(stamp); } } - return state; + return empty ? OptionalBoolean.empty() : OptionalBoolean.of(state); } @Override - public boolean findFirst(BooleanPredicate filter) { + public OptionalBoolean findFirst(BooleanPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return false; + if(size() <= 0) return OptionalBoolean.empty(); for(int i = 0,m=segments.length;i seg = segments[i]; long stamp = seg.readLock(); try { int index = seg.firstIndex; while(index != -1){ - if(filter.test(seg.values[index])) return seg.values[index]; + if(filter.test(seg.values[index])) return OptionalBoolean.of(seg.values[index]); index = (int)seg.links[index]; } } @@ -1327,7 +1329,7 @@ public class Object2BooleanConcurrentOpenHashMap extends AbstractObject2Boole seg.unlockRead(stamp); } } - return false; + return OptionalBoolean.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/objects/maps/impl/concurrent/Object2ByteConcurrentOpenHashMap.java b/src/main/java/speiger/src/collections/objects/maps/impl/concurrent/Object2ByteConcurrentOpenHashMap.java index 968741e..d7e655b 100644 --- a/src/main/java/speiger/src/collections/objects/maps/impl/concurrent/Object2ByteConcurrentOpenHashMap.java +++ b/src/main/java/speiger/src/collections/objects/maps/impl/concurrent/Object2ByteConcurrentOpenHashMap.java @@ -4,6 +4,7 @@ import java.util.Arrays; import java.util.Map; import java.util.NoSuchElementException; import java.util.Objects; +import java.util.Optional; import java.util.concurrent.locks.StampedLock; import java.util.function.Consumer; import java.util.function.Predicate; @@ -30,6 +31,7 @@ import speiger.src.collections.bytes.functions.ByteConsumer; import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.bytes.functions.function.BytePredicate; +import speiger.src.collections.bytes.functions.OptionalByte; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.objects.sets.AbstractObjectSet; import speiger.src.collections.objects.sets.ObjectSet; @@ -752,7 +754,7 @@ public class Object2ByteConcurrentOpenHashMap extends AbstractObject2ByteMap< } @Override - public Object2ByteMap.Entry reduce(ObjectObjectUnaryOperator, Object2ByteMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Object2ByteMap.Entry> operator) { Objects.requireNonNull(operator); Object2ByteMap.Entry state = null; boolean empty = true; @@ -776,11 +778,11 @@ public class Object2ByteConcurrentOpenHashMap extends AbstractObject2ByteMap< seg.unlockRead(stamp); } } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Object2ByteMap.Entry findFirst(Predicate> filter) { + public Optional> findFirst(Predicate> filter) { Objects.requireNonNull(filter); MapEntry entry = new MapEntry(); for(int i = 0,m=segments.length;i extends AbstractObject2ByteMap< try { while(index != -1) { entry.set(index, i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); index = (int)seg.links[index]; } } @@ -798,7 +800,7 @@ public class Object2ByteConcurrentOpenHashMap extends AbstractObject2ByteMap< seg.unlockRead(stamp); } } - return null; + return Optional.empty(); } @Override @@ -1060,7 +1062,7 @@ public class Object2ByteConcurrentOpenHashMap extends AbstractObject2ByteMap< } @Override - public T reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); T state = null; boolean empty = true; @@ -1084,11 +1086,11 @@ public class Object2ByteConcurrentOpenHashMap extends AbstractObject2ByteMap< seg.unlockRead(stamp); } } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public T findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); for(int i = 0,m=segments.length;i seg = segments[i]; @@ -1096,7 +1098,7 @@ public class Object2ByteConcurrentOpenHashMap extends AbstractObject2ByteMap< try { int index = seg.firstIndex; while(index != -1){ - if(filter.test(seg.keys[index])) return seg.keys[index]; + if(filter.test(seg.keys[index])) return Optional.ofNullable(seg.keys[index]); index = (int)seg.links[index]; } } @@ -1104,7 +1106,7 @@ public class Object2ByteConcurrentOpenHashMap extends AbstractObject2ByteMap< seg.unlockRead(stamp); } } - return null; + return Optional.empty(); } @Override @@ -1295,7 +1297,7 @@ public class Object2ByteConcurrentOpenHashMap extends AbstractObject2ByteMap< } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -1319,20 +1321,20 @@ public class Object2ByteConcurrentOpenHashMap extends AbstractObject2ByteMap< seg.unlockRead(stamp); } } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (byte)0; + if(size() <= 0) return OptionalByte.empty(); for(int i = 0,m=segments.length;i seg = segments[i]; long stamp = seg.readLock(); try { int index = seg.firstIndex; while(index != -1){ - if(filter.test(seg.values[index])) return seg.values[index]; + if(filter.test(seg.values[index])) return OptionalByte.of(seg.values[index]); index = (int)seg.links[index]; } } @@ -1340,7 +1342,7 @@ public class Object2ByteConcurrentOpenHashMap extends AbstractObject2ByteMap< seg.unlockRead(stamp); } } - return (byte)0; + return OptionalByte.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/objects/maps/impl/concurrent/Object2CharConcurrentOpenHashMap.java b/src/main/java/speiger/src/collections/objects/maps/impl/concurrent/Object2CharConcurrentOpenHashMap.java index 4881516..3aea65c 100644 --- a/src/main/java/speiger/src/collections/objects/maps/impl/concurrent/Object2CharConcurrentOpenHashMap.java +++ b/src/main/java/speiger/src/collections/objects/maps/impl/concurrent/Object2CharConcurrentOpenHashMap.java @@ -4,6 +4,7 @@ import java.util.Arrays; import java.util.Map; import java.util.NoSuchElementException; import java.util.Objects; +import java.util.Optional; import java.util.concurrent.locks.StampedLock; import java.util.function.Consumer; import java.util.function.Predicate; @@ -30,6 +31,7 @@ import speiger.src.collections.chars.functions.CharConsumer; import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.chars.functions.function.CharPredicate; +import speiger.src.collections.chars.functions.OptionalChar; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.objects.sets.AbstractObjectSet; import speiger.src.collections.objects.sets.ObjectSet; @@ -752,7 +754,7 @@ public class Object2CharConcurrentOpenHashMap extends AbstractObject2CharMap< } @Override - public Object2CharMap.Entry reduce(ObjectObjectUnaryOperator, Object2CharMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Object2CharMap.Entry> operator) { Objects.requireNonNull(operator); Object2CharMap.Entry state = null; boolean empty = true; @@ -776,11 +778,11 @@ public class Object2CharConcurrentOpenHashMap extends AbstractObject2CharMap< seg.unlockRead(stamp); } } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Object2CharMap.Entry findFirst(Predicate> filter) { + public Optional> findFirst(Predicate> filter) { Objects.requireNonNull(filter); MapEntry entry = new MapEntry(); for(int i = 0,m=segments.length;i extends AbstractObject2CharMap< try { while(index != -1) { entry.set(index, i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); index = (int)seg.links[index]; } } @@ -798,7 +800,7 @@ public class Object2CharConcurrentOpenHashMap extends AbstractObject2CharMap< seg.unlockRead(stamp); } } - return null; + return Optional.empty(); } @Override @@ -1060,7 +1062,7 @@ public class Object2CharConcurrentOpenHashMap extends AbstractObject2CharMap< } @Override - public T reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); T state = null; boolean empty = true; @@ -1084,11 +1086,11 @@ public class Object2CharConcurrentOpenHashMap extends AbstractObject2CharMap< seg.unlockRead(stamp); } } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public T findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); for(int i = 0,m=segments.length;i seg = segments[i]; @@ -1096,7 +1098,7 @@ public class Object2CharConcurrentOpenHashMap extends AbstractObject2CharMap< try { int index = seg.firstIndex; while(index != -1){ - if(filter.test(seg.keys[index])) return seg.keys[index]; + if(filter.test(seg.keys[index])) return Optional.ofNullable(seg.keys[index]); index = (int)seg.links[index]; } } @@ -1104,7 +1106,7 @@ public class Object2CharConcurrentOpenHashMap extends AbstractObject2CharMap< seg.unlockRead(stamp); } } - return null; + return Optional.empty(); } @Override @@ -1295,7 +1297,7 @@ public class Object2CharConcurrentOpenHashMap extends AbstractObject2CharMap< } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -1319,20 +1321,20 @@ public class Object2CharConcurrentOpenHashMap extends AbstractObject2CharMap< seg.unlockRead(stamp); } } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (char)0; + if(size() <= 0) return OptionalChar.empty(); for(int i = 0,m=segments.length;i seg = segments[i]; long stamp = seg.readLock(); try { int index = seg.firstIndex; while(index != -1){ - if(filter.test(seg.values[index])) return seg.values[index]; + if(filter.test(seg.values[index])) return OptionalChar.of(seg.values[index]); index = (int)seg.links[index]; } } @@ -1340,7 +1342,7 @@ public class Object2CharConcurrentOpenHashMap extends AbstractObject2CharMap< seg.unlockRead(stamp); } } - return (char)0; + return OptionalChar.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/objects/maps/impl/concurrent/Object2DoubleConcurrentOpenHashMap.java b/src/main/java/speiger/src/collections/objects/maps/impl/concurrent/Object2DoubleConcurrentOpenHashMap.java index 2e98776..c4b5744 100644 --- a/src/main/java/speiger/src/collections/objects/maps/impl/concurrent/Object2DoubleConcurrentOpenHashMap.java +++ b/src/main/java/speiger/src/collections/objects/maps/impl/concurrent/Object2DoubleConcurrentOpenHashMap.java @@ -4,11 +4,13 @@ import java.util.Arrays; import java.util.Map; import java.util.NoSuchElementException; import java.util.Objects; +import java.util.Optional; import java.util.concurrent.locks.StampedLock; import java.util.function.Consumer; import java.util.function.Predicate; import java.util.function.BiFunction; import java.util.function.DoublePredicate; +import java.util.OptionalDouble; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntDoubleConsumer; @@ -752,7 +754,7 @@ public class Object2DoubleConcurrentOpenHashMap extends AbstractObject2Double } @Override - public Object2DoubleMap.Entry reduce(ObjectObjectUnaryOperator, Object2DoubleMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Object2DoubleMap.Entry> operator) { Objects.requireNonNull(operator); Object2DoubleMap.Entry state = null; boolean empty = true; @@ -776,11 +778,11 @@ public class Object2DoubleConcurrentOpenHashMap extends AbstractObject2Double seg.unlockRead(stamp); } } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Object2DoubleMap.Entry findFirst(Predicate> filter) { + public Optional> findFirst(Predicate> filter) { Objects.requireNonNull(filter); MapEntry entry = new MapEntry(); for(int i = 0,m=segments.length;i extends AbstractObject2Double try { while(index != -1) { entry.set(index, i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); index = (int)seg.links[index]; } } @@ -798,7 +800,7 @@ public class Object2DoubleConcurrentOpenHashMap extends AbstractObject2Double seg.unlockRead(stamp); } } - return null; + return Optional.empty(); } @Override @@ -1060,7 +1062,7 @@ public class Object2DoubleConcurrentOpenHashMap extends AbstractObject2Double } @Override - public T reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); T state = null; boolean empty = true; @@ -1084,11 +1086,11 @@ public class Object2DoubleConcurrentOpenHashMap extends AbstractObject2Double seg.unlockRead(stamp); } } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public T findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); for(int i = 0,m=segments.length;i seg = segments[i]; @@ -1096,7 +1098,7 @@ public class Object2DoubleConcurrentOpenHashMap extends AbstractObject2Double try { int index = seg.firstIndex; while(index != -1){ - if(filter.test(seg.keys[index])) return seg.keys[index]; + if(filter.test(seg.keys[index])) return Optional.ofNullable(seg.keys[index]); index = (int)seg.links[index]; } } @@ -1104,7 +1106,7 @@ public class Object2DoubleConcurrentOpenHashMap extends AbstractObject2Double seg.unlockRead(stamp); } } - return null; + return Optional.empty(); } @Override @@ -1295,7 +1297,7 @@ public class Object2DoubleConcurrentOpenHashMap extends AbstractObject2Double } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -1319,20 +1321,20 @@ public class Object2DoubleConcurrentOpenHashMap extends AbstractObject2Double seg.unlockRead(stamp); } } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0D; + if(size() <= 0) return OptionalDouble.empty(); for(int i = 0,m=segments.length;i seg = segments[i]; long stamp = seg.readLock(); try { int index = seg.firstIndex; while(index != -1){ - if(filter.test(seg.values[index])) return seg.values[index]; + if(filter.test(seg.values[index])) return OptionalDouble.of(seg.values[index]); index = (int)seg.links[index]; } } @@ -1340,7 +1342,7 @@ public class Object2DoubleConcurrentOpenHashMap extends AbstractObject2Double seg.unlockRead(stamp); } } - return 0D; + return OptionalDouble.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/objects/maps/impl/concurrent/Object2FloatConcurrentOpenHashMap.java b/src/main/java/speiger/src/collections/objects/maps/impl/concurrent/Object2FloatConcurrentOpenHashMap.java index 4e79d12..99e101f 100644 --- a/src/main/java/speiger/src/collections/objects/maps/impl/concurrent/Object2FloatConcurrentOpenHashMap.java +++ b/src/main/java/speiger/src/collections/objects/maps/impl/concurrent/Object2FloatConcurrentOpenHashMap.java @@ -4,6 +4,7 @@ import java.util.Arrays; import java.util.Map; import java.util.NoSuchElementException; import java.util.Objects; +import java.util.Optional; import java.util.concurrent.locks.StampedLock; import java.util.function.Consumer; import java.util.function.Predicate; @@ -30,6 +31,7 @@ import speiger.src.collections.floats.functions.FloatConsumer; import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.floats.functions.function.FloatPredicate; +import speiger.src.collections.floats.functions.OptionalFloat; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.objects.sets.AbstractObjectSet; import speiger.src.collections.objects.sets.ObjectSet; @@ -752,7 +754,7 @@ public class Object2FloatConcurrentOpenHashMap extends AbstractObject2FloatMa } @Override - public Object2FloatMap.Entry reduce(ObjectObjectUnaryOperator, Object2FloatMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Object2FloatMap.Entry> operator) { Objects.requireNonNull(operator); Object2FloatMap.Entry state = null; boolean empty = true; @@ -776,11 +778,11 @@ public class Object2FloatConcurrentOpenHashMap extends AbstractObject2FloatMa seg.unlockRead(stamp); } } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Object2FloatMap.Entry findFirst(Predicate> filter) { + public Optional> findFirst(Predicate> filter) { Objects.requireNonNull(filter); MapEntry entry = new MapEntry(); for(int i = 0,m=segments.length;i extends AbstractObject2FloatMa try { while(index != -1) { entry.set(index, i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); index = (int)seg.links[index]; } } @@ -798,7 +800,7 @@ public class Object2FloatConcurrentOpenHashMap extends AbstractObject2FloatMa seg.unlockRead(stamp); } } - return null; + return Optional.empty(); } @Override @@ -1060,7 +1062,7 @@ public class Object2FloatConcurrentOpenHashMap extends AbstractObject2FloatMa } @Override - public T reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); T state = null; boolean empty = true; @@ -1084,11 +1086,11 @@ public class Object2FloatConcurrentOpenHashMap extends AbstractObject2FloatMa seg.unlockRead(stamp); } } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public T findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); for(int i = 0,m=segments.length;i seg = segments[i]; @@ -1096,7 +1098,7 @@ public class Object2FloatConcurrentOpenHashMap extends AbstractObject2FloatMa try { int index = seg.firstIndex; while(index != -1){ - if(filter.test(seg.keys[index])) return seg.keys[index]; + if(filter.test(seg.keys[index])) return Optional.ofNullable(seg.keys[index]); index = (int)seg.links[index]; } } @@ -1104,7 +1106,7 @@ public class Object2FloatConcurrentOpenHashMap extends AbstractObject2FloatMa seg.unlockRead(stamp); } } - return null; + return Optional.empty(); } @Override @@ -1295,7 +1297,7 @@ public class Object2FloatConcurrentOpenHashMap extends AbstractObject2FloatMa } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -1319,20 +1321,20 @@ public class Object2FloatConcurrentOpenHashMap extends AbstractObject2FloatMa seg.unlockRead(stamp); } } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0F; + if(size() <= 0) return OptionalFloat.empty(); for(int i = 0,m=segments.length;i seg = segments[i]; long stamp = seg.readLock(); try { int index = seg.firstIndex; while(index != -1){ - if(filter.test(seg.values[index])) return seg.values[index]; + if(filter.test(seg.values[index])) return OptionalFloat.of(seg.values[index]); index = (int)seg.links[index]; } } @@ -1340,7 +1342,7 @@ public class Object2FloatConcurrentOpenHashMap extends AbstractObject2FloatMa seg.unlockRead(stamp); } } - return 0F; + return OptionalFloat.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/objects/maps/impl/concurrent/Object2IntConcurrentOpenHashMap.java b/src/main/java/speiger/src/collections/objects/maps/impl/concurrent/Object2IntConcurrentOpenHashMap.java index b51ad7b..ed576fe 100644 --- a/src/main/java/speiger/src/collections/objects/maps/impl/concurrent/Object2IntConcurrentOpenHashMap.java +++ b/src/main/java/speiger/src/collections/objects/maps/impl/concurrent/Object2IntConcurrentOpenHashMap.java @@ -4,11 +4,13 @@ import java.util.Arrays; import java.util.Map; import java.util.NoSuchElementException; import java.util.Objects; +import java.util.Optional; import java.util.concurrent.locks.StampedLock; import java.util.function.Consumer; import java.util.function.Predicate; import java.util.function.BiFunction; import java.util.function.IntPredicate; +import java.util.OptionalInt; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntIntConsumer; @@ -752,7 +754,7 @@ public class Object2IntConcurrentOpenHashMap extends AbstractObject2IntMap } @Override - public Object2IntMap.Entry reduce(ObjectObjectUnaryOperator, Object2IntMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Object2IntMap.Entry> operator) { Objects.requireNonNull(operator); Object2IntMap.Entry state = null; boolean empty = true; @@ -776,11 +778,11 @@ public class Object2IntConcurrentOpenHashMap extends AbstractObject2IntMap seg.unlockRead(stamp); } } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Object2IntMap.Entry findFirst(Predicate> filter) { + public Optional> findFirst(Predicate> filter) { Objects.requireNonNull(filter); MapEntry entry = new MapEntry(); for(int i = 0,m=segments.length;i extends AbstractObject2IntMap try { while(index != -1) { entry.set(index, i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); index = (int)seg.links[index]; } } @@ -798,7 +800,7 @@ public class Object2IntConcurrentOpenHashMap extends AbstractObject2IntMap seg.unlockRead(stamp); } } - return null; + return Optional.empty(); } @Override @@ -1060,7 +1062,7 @@ public class Object2IntConcurrentOpenHashMap extends AbstractObject2IntMap } @Override - public T reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); T state = null; boolean empty = true; @@ -1084,11 +1086,11 @@ public class Object2IntConcurrentOpenHashMap extends AbstractObject2IntMap seg.unlockRead(stamp); } } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public T findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); for(int i = 0,m=segments.length;i seg = segments[i]; @@ -1096,7 +1098,7 @@ public class Object2IntConcurrentOpenHashMap extends AbstractObject2IntMap try { int index = seg.firstIndex; while(index != -1){ - if(filter.test(seg.keys[index])) return seg.keys[index]; + if(filter.test(seg.keys[index])) return Optional.ofNullable(seg.keys[index]); index = (int)seg.links[index]; } } @@ -1104,7 +1106,7 @@ public class Object2IntConcurrentOpenHashMap extends AbstractObject2IntMap seg.unlockRead(stamp); } } - return null; + return Optional.empty(); } @Override @@ -1295,7 +1297,7 @@ public class Object2IntConcurrentOpenHashMap extends AbstractObject2IntMap } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -1319,20 +1321,20 @@ public class Object2IntConcurrentOpenHashMap extends AbstractObject2IntMap seg.unlockRead(stamp); } } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0; + if(size() <= 0) return OptionalInt.empty(); for(int i = 0,m=segments.length;i seg = segments[i]; long stamp = seg.readLock(); try { int index = seg.firstIndex; while(index != -1){ - if(filter.test(seg.values[index])) return seg.values[index]; + if(filter.test(seg.values[index])) return OptionalInt.of(seg.values[index]); index = (int)seg.links[index]; } } @@ -1340,7 +1342,7 @@ public class Object2IntConcurrentOpenHashMap extends AbstractObject2IntMap seg.unlockRead(stamp); } } - return 0; + return OptionalInt.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/objects/maps/impl/concurrent/Object2LongConcurrentOpenHashMap.java b/src/main/java/speiger/src/collections/objects/maps/impl/concurrent/Object2LongConcurrentOpenHashMap.java index 35cc307..0a92d0c 100644 --- a/src/main/java/speiger/src/collections/objects/maps/impl/concurrent/Object2LongConcurrentOpenHashMap.java +++ b/src/main/java/speiger/src/collections/objects/maps/impl/concurrent/Object2LongConcurrentOpenHashMap.java @@ -4,11 +4,13 @@ import java.util.Arrays; import java.util.Map; import java.util.NoSuchElementException; import java.util.Objects; +import java.util.Optional; import java.util.concurrent.locks.StampedLock; import java.util.function.Consumer; import java.util.function.Predicate; import java.util.function.BiFunction; import java.util.function.LongPredicate; +import java.util.OptionalLong; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntLongConsumer; @@ -752,7 +754,7 @@ public class Object2LongConcurrentOpenHashMap extends AbstractObject2LongMap< } @Override - public Object2LongMap.Entry reduce(ObjectObjectUnaryOperator, Object2LongMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Object2LongMap.Entry> operator) { Objects.requireNonNull(operator); Object2LongMap.Entry state = null; boolean empty = true; @@ -776,11 +778,11 @@ public class Object2LongConcurrentOpenHashMap extends AbstractObject2LongMap< seg.unlockRead(stamp); } } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Object2LongMap.Entry findFirst(Predicate> filter) { + public Optional> findFirst(Predicate> filter) { Objects.requireNonNull(filter); MapEntry entry = new MapEntry(); for(int i = 0,m=segments.length;i extends AbstractObject2LongMap< try { while(index != -1) { entry.set(index, i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); index = (int)seg.links[index]; } } @@ -798,7 +800,7 @@ public class Object2LongConcurrentOpenHashMap extends AbstractObject2LongMap< seg.unlockRead(stamp); } } - return null; + return Optional.empty(); } @Override @@ -1060,7 +1062,7 @@ public class Object2LongConcurrentOpenHashMap extends AbstractObject2LongMap< } @Override - public T reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); T state = null; boolean empty = true; @@ -1084,11 +1086,11 @@ public class Object2LongConcurrentOpenHashMap extends AbstractObject2LongMap< seg.unlockRead(stamp); } } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public T findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); for(int i = 0,m=segments.length;i seg = segments[i]; @@ -1096,7 +1098,7 @@ public class Object2LongConcurrentOpenHashMap extends AbstractObject2LongMap< try { int index = seg.firstIndex; while(index != -1){ - if(filter.test(seg.keys[index])) return seg.keys[index]; + if(filter.test(seg.keys[index])) return Optional.ofNullable(seg.keys[index]); index = (int)seg.links[index]; } } @@ -1104,7 +1106,7 @@ public class Object2LongConcurrentOpenHashMap extends AbstractObject2LongMap< seg.unlockRead(stamp); } } - return null; + return Optional.empty(); } @Override @@ -1295,7 +1297,7 @@ public class Object2LongConcurrentOpenHashMap extends AbstractObject2LongMap< } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -1319,20 +1321,20 @@ public class Object2LongConcurrentOpenHashMap extends AbstractObject2LongMap< seg.unlockRead(stamp); } } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0L; + if(size() <= 0) return OptionalLong.empty(); for(int i = 0,m=segments.length;i seg = segments[i]; long stamp = seg.readLock(); try { int index = seg.firstIndex; while(index != -1){ - if(filter.test(seg.values[index])) return seg.values[index]; + if(filter.test(seg.values[index])) return OptionalLong.of(seg.values[index]); index = (int)seg.links[index]; } } @@ -1340,7 +1342,7 @@ public class Object2LongConcurrentOpenHashMap extends AbstractObject2LongMap< seg.unlockRead(stamp); } } - return 0L; + return OptionalLong.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/objects/maps/impl/concurrent/Object2ObjectConcurrentOpenHashMap.java b/src/main/java/speiger/src/collections/objects/maps/impl/concurrent/Object2ObjectConcurrentOpenHashMap.java index 700b2f5..8538969 100644 --- a/src/main/java/speiger/src/collections/objects/maps/impl/concurrent/Object2ObjectConcurrentOpenHashMap.java +++ b/src/main/java/speiger/src/collections/objects/maps/impl/concurrent/Object2ObjectConcurrentOpenHashMap.java @@ -4,6 +4,7 @@ import java.util.Arrays; import java.util.Map; import java.util.NoSuchElementException; import java.util.Objects; +import java.util.Optional; import java.util.concurrent.locks.StampedLock; import java.util.function.Consumer; import java.util.function.Predicate; @@ -639,7 +640,7 @@ public class Object2ObjectConcurrentOpenHashMap extends AbstractObject2Obj } @Override - public Object2ObjectMap.Entry reduce(ObjectObjectUnaryOperator, Object2ObjectMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Object2ObjectMap.Entry> operator) { Objects.requireNonNull(operator); Object2ObjectMap.Entry state = null; boolean empty = true; @@ -663,11 +664,11 @@ public class Object2ObjectConcurrentOpenHashMap extends AbstractObject2Obj seg.unlockRead(stamp); } } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Object2ObjectMap.Entry findFirst(Predicate> filter) { + public Optional> findFirst(Predicate> filter) { Objects.requireNonNull(filter); MapEntry entry = new MapEntry(); for(int i = 0,m=segments.length;i extends AbstractObject2Obj try { while(index != -1) { entry.set(index, i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); index = (int)seg.links[index]; } } @@ -685,7 +686,7 @@ public class Object2ObjectConcurrentOpenHashMap extends AbstractObject2Obj seg.unlockRead(stamp); } } - return null; + return Optional.empty(); } @Override @@ -947,7 +948,7 @@ public class Object2ObjectConcurrentOpenHashMap extends AbstractObject2Obj } @Override - public T reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); T state = null; boolean empty = true; @@ -971,11 +972,11 @@ public class Object2ObjectConcurrentOpenHashMap extends AbstractObject2Obj seg.unlockRead(stamp); } } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public T findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); for(int i = 0,m=segments.length;i seg = segments[i]; @@ -983,7 +984,7 @@ public class Object2ObjectConcurrentOpenHashMap extends AbstractObject2Obj try { int index = seg.firstIndex; while(index != -1){ - if(filter.test(seg.keys[index])) return seg.keys[index]; + if(filter.test(seg.keys[index])) return Optional.ofNullable(seg.keys[index]); index = (int)seg.links[index]; } } @@ -991,7 +992,7 @@ public class Object2ObjectConcurrentOpenHashMap extends AbstractObject2Obj seg.unlockRead(stamp); } } - return null; + return Optional.empty(); } @Override @@ -1183,7 +1184,7 @@ public class Object2ObjectConcurrentOpenHashMap extends AbstractObject2Obj } @Override - public V reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); V state = null; boolean empty = true; @@ -1207,20 +1208,20 @@ public class Object2ObjectConcurrentOpenHashMap extends AbstractObject2Obj seg.unlockRead(stamp); } } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public V findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); for(int i = 0,m=segments.length;i seg = segments[i]; long stamp = seg.readLock(); try { int index = seg.firstIndex; while(index != -1){ - if(filter.test(seg.values[index])) return seg.values[index]; + if(filter.test(seg.values[index])) return Optional.ofNullable(seg.values[index]); index = (int)seg.links[index]; } } @@ -1228,7 +1229,7 @@ public class Object2ObjectConcurrentOpenHashMap extends AbstractObject2Obj seg.unlockRead(stamp); } } - return null; + return Optional.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/objects/maps/impl/concurrent/Object2ShortConcurrentOpenHashMap.java b/src/main/java/speiger/src/collections/objects/maps/impl/concurrent/Object2ShortConcurrentOpenHashMap.java index b06e725..cbf1997 100644 --- a/src/main/java/speiger/src/collections/objects/maps/impl/concurrent/Object2ShortConcurrentOpenHashMap.java +++ b/src/main/java/speiger/src/collections/objects/maps/impl/concurrent/Object2ShortConcurrentOpenHashMap.java @@ -4,6 +4,7 @@ import java.util.Arrays; import java.util.Map; import java.util.NoSuchElementException; import java.util.Objects; +import java.util.Optional; import java.util.concurrent.locks.StampedLock; import java.util.function.Consumer; import java.util.function.Predicate; @@ -30,6 +31,7 @@ import speiger.src.collections.shorts.functions.ShortConsumer; import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.shorts.functions.function.ShortPredicate; +import speiger.src.collections.shorts.functions.OptionalShort; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.objects.sets.AbstractObjectSet; import speiger.src.collections.objects.sets.ObjectSet; @@ -752,7 +754,7 @@ public class Object2ShortConcurrentOpenHashMap extends AbstractObject2ShortMa } @Override - public Object2ShortMap.Entry reduce(ObjectObjectUnaryOperator, Object2ShortMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Object2ShortMap.Entry> operator) { Objects.requireNonNull(operator); Object2ShortMap.Entry state = null; boolean empty = true; @@ -776,11 +778,11 @@ public class Object2ShortConcurrentOpenHashMap extends AbstractObject2ShortMa seg.unlockRead(stamp); } } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Object2ShortMap.Entry findFirst(Predicate> filter) { + public Optional> findFirst(Predicate> filter) { Objects.requireNonNull(filter); MapEntry entry = new MapEntry(); for(int i = 0,m=segments.length;i extends AbstractObject2ShortMa try { while(index != -1) { entry.set(index, i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); index = (int)seg.links[index]; } } @@ -798,7 +800,7 @@ public class Object2ShortConcurrentOpenHashMap extends AbstractObject2ShortMa seg.unlockRead(stamp); } } - return null; + return Optional.empty(); } @Override @@ -1060,7 +1062,7 @@ public class Object2ShortConcurrentOpenHashMap extends AbstractObject2ShortMa } @Override - public T reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); T state = null; boolean empty = true; @@ -1084,11 +1086,11 @@ public class Object2ShortConcurrentOpenHashMap extends AbstractObject2ShortMa seg.unlockRead(stamp); } } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public T findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); for(int i = 0,m=segments.length;i seg = segments[i]; @@ -1096,7 +1098,7 @@ public class Object2ShortConcurrentOpenHashMap extends AbstractObject2ShortMa try { int index = seg.firstIndex; while(index != -1){ - if(filter.test(seg.keys[index])) return seg.keys[index]; + if(filter.test(seg.keys[index])) return Optional.ofNullable(seg.keys[index]); index = (int)seg.links[index]; } } @@ -1104,7 +1106,7 @@ public class Object2ShortConcurrentOpenHashMap extends AbstractObject2ShortMa seg.unlockRead(stamp); } } - return null; + return Optional.empty(); } @Override @@ -1295,7 +1297,7 @@ public class Object2ShortConcurrentOpenHashMap extends AbstractObject2ShortMa } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -1319,20 +1321,20 @@ public class Object2ShortConcurrentOpenHashMap extends AbstractObject2ShortMa seg.unlockRead(stamp); } } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (short)0; + if(size() <= 0) return OptionalShort.empty(); for(int i = 0,m=segments.length;i seg = segments[i]; long stamp = seg.readLock(); try { int index = seg.firstIndex; while(index != -1){ - if(filter.test(seg.values[index])) return seg.values[index]; + if(filter.test(seg.values[index])) return OptionalShort.of(seg.values[index]); index = (int)seg.links[index]; } } @@ -1340,7 +1342,7 @@ public class Object2ShortConcurrentOpenHashMap extends AbstractObject2ShortMa seg.unlockRead(stamp); } } - return (short)0; + return OptionalShort.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/objects/maps/impl/customHash/Object2BooleanLinkedOpenCustomHashMap.java b/src/main/java/speiger/src/collections/objects/maps/impl/customHash/Object2BooleanLinkedOpenCustomHashMap.java index e953cab..455bfe5 100644 --- a/src/main/java/speiger/src/collections/objects/maps/impl/customHash/Object2BooleanLinkedOpenCustomHashMap.java +++ b/src/main/java/speiger/src/collections/objects/maps/impl/customHash/Object2BooleanLinkedOpenCustomHashMap.java @@ -4,6 +4,7 @@ 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; @@ -28,6 +29,7 @@ import speiger.src.collections.booleans.lists.BooleanListIterator; import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.booleans.functions.function.BooleanPredicate; +import speiger.src.collections.booleans.functions.OptionalBoolean; import speiger.src.collections.utils.HashUtil; /** @@ -662,6 +664,10 @@ public class Object2BooleanLinkedOpenCustomHashMap extends Object2BooleanOpen } private class MapEntrySet extends AbstractObjectSet> implements Object2BooleanOrderedMap.FastOrderedSet { + @Override + public void addFirst(Object2BooleanMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Object2BooleanMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Object2BooleanMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -827,7 +833,7 @@ public class Object2BooleanLinkedOpenCustomHashMap extends Object2BooleanOpen } @Override - public Object2BooleanMap.Entry reduce(ObjectObjectUnaryOperator, Object2BooleanMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Object2BooleanMap.Entry> operator) { Objects.requireNonNull(operator); Object2BooleanMap.Entry state = null; boolean empty = true; @@ -842,21 +848,21 @@ public class Object2BooleanLinkedOpenCustomHashMap extends Object2BooleanOpen state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Object2BooleanMap.Entry 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 @@ -935,10 +941,13 @@ public class Object2BooleanLinkedOpenCustomHashMap extends Object2BooleanOpen public boolean add(T o) { throw new UnsupportedOperationException(); } - + + @Override + public void addFirst(T o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(T o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(T o) { throw new UnsupportedOperationException(); } - @Override public boolean addAndMoveToLast(T o) { throw new UnsupportedOperationException(); } @@ -1081,7 +1090,7 @@ public class Object2BooleanLinkedOpenCustomHashMap extends Object2BooleanOpen } @Override - public T reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); T state = null; boolean empty = true; @@ -1096,19 +1105,19 @@ public class Object2BooleanLinkedOpenCustomHashMap extends Object2BooleanOpen state = operator.apply(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public T findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + 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.ofNullable(keys[index]); index = (int)links[index]; } - return null; + return Optional.empty(); } @Override @@ -1243,7 +1252,7 @@ public class Object2BooleanLinkedOpenCustomHashMap extends Object2BooleanOpen } @Override - public boolean reduce(BooleanBooleanUnaryOperator operator) { + public OptionalBoolean reduce(BooleanBooleanUnaryOperator operator) { Objects.requireNonNull(operator); boolean state = false; boolean empty = true; @@ -1258,19 +1267,19 @@ public class Object2BooleanLinkedOpenCustomHashMap extends Object2BooleanOpen state = operator.applyAsBoolean(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalBoolean.empty() : OptionalBoolean.of(state); } @Override - public boolean findFirst(BooleanPredicate filter) { + public OptionalBoolean findFirst(BooleanPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return false; + if(size() <= 0) return OptionalBoolean.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalBoolean.of(values[index]); index = (int)links[index]; } - return false; + return OptionalBoolean.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/objects/maps/impl/customHash/Object2BooleanOpenCustomHashMap.java b/src/main/java/speiger/src/collections/objects/maps/impl/customHash/Object2BooleanOpenCustomHashMap.java index df027ea..f56d5ab 100644 --- a/src/main/java/speiger/src/collections/objects/maps/impl/customHash/Object2BooleanOpenCustomHashMap.java +++ b/src/main/java/speiger/src/collections/objects/maps/impl/customHash/Object2BooleanOpenCustomHashMap.java @@ -5,6 +5,7 @@ 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; @@ -28,6 +29,7 @@ import speiger.src.collections.booleans.functions.BooleanConsumer; import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.booleans.functions.function.BooleanPredicate; +import speiger.src.collections.booleans.functions.OptionalBoolean; import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.sets.AbstractObjectSet; import speiger.src.collections.objects.sets.ObjectSet; @@ -901,7 +903,7 @@ public class Object2BooleanOpenCustomHashMap extends AbstractObject2BooleanMa } @Override - public Object2BooleanMap.Entry reduce(ObjectObjectUnaryOperator, Object2BooleanMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Object2BooleanMap.Entry> operator) { Objects.requireNonNull(operator); Object2BooleanMap.Entry state = null; boolean empty = true; @@ -918,25 +920,25 @@ public class Object2BooleanOpenCustomHashMap extends AbstractObject2BooleanMa } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Object2BooleanMap.Entry 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], null)) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1109,7 +1111,7 @@ public class Object2BooleanOpenCustomHashMap extends AbstractObject2BooleanMa } @Override - public T reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); T state = null; boolean empty = true; @@ -1126,18 +1128,18 @@ public class Object2BooleanOpenCustomHashMap extends AbstractObject2BooleanMa } state = operator.apply(state, keys[i]); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public T findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return Optional.empty(); + if(containsNull && filter.test(keys[nullIndex])) return Optional.ofNullable(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], null) && filter.test(keys[i])) return keys[i]; + if(!strategy.equals(keys[i], null) && filter.test(keys[i])) return Optional.ofNullable(keys[i]); } - return null; + return Optional.empty(); } @Override @@ -1252,7 +1254,7 @@ public class Object2BooleanOpenCustomHashMap extends AbstractObject2BooleanMa } @Override - public boolean reduce(BooleanBooleanUnaryOperator operator) { + public OptionalBoolean reduce(BooleanBooleanUnaryOperator operator) { Objects.requireNonNull(operator); boolean state = false; boolean empty = true; @@ -1269,18 +1271,18 @@ public class Object2BooleanOpenCustomHashMap extends AbstractObject2BooleanMa } state = operator.applyAsBoolean(state, values[i]); } - return state; + return empty ? OptionalBoolean.empty() : OptionalBoolean.of(state); } @Override - public boolean findFirst(BooleanPredicate filter) { + public OptionalBoolean findFirst(BooleanPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return false; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return OptionalBoolean.empty(); + if(containsNull && filter.test(values[nullIndex])) return OptionalBoolean.of(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], null) && filter.test(values[i])) return values[i]; + if(!strategy.equals(keys[i], null) && filter.test(values[i])) return OptionalBoolean.of(values[i]); } - return false; + return OptionalBoolean.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/objects/maps/impl/customHash/Object2ByteLinkedOpenCustomHashMap.java b/src/main/java/speiger/src/collections/objects/maps/impl/customHash/Object2ByteLinkedOpenCustomHashMap.java index 2aefd20..ad9fab6 100644 --- a/src/main/java/speiger/src/collections/objects/maps/impl/customHash/Object2ByteLinkedOpenCustomHashMap.java +++ b/src/main/java/speiger/src/collections/objects/maps/impl/customHash/Object2ByteLinkedOpenCustomHashMap.java @@ -4,6 +4,7 @@ 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; @@ -28,6 +29,7 @@ import speiger.src.collections.bytes.lists.ByteListIterator; import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.bytes.functions.function.BytePredicate; +import speiger.src.collections.bytes.functions.OptionalByte; import speiger.src.collections.utils.HashUtil; /** @@ -662,6 +664,10 @@ public class Object2ByteLinkedOpenCustomHashMap extends Object2ByteOpenCustom } private class MapEntrySet extends AbstractObjectSet> implements Object2ByteOrderedMap.FastOrderedSet { + @Override + public void addFirst(Object2ByteMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Object2ByteMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Object2ByteMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -827,7 +833,7 @@ public class Object2ByteLinkedOpenCustomHashMap extends Object2ByteOpenCustom } @Override - public Object2ByteMap.Entry reduce(ObjectObjectUnaryOperator, Object2ByteMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Object2ByteMap.Entry> operator) { Objects.requireNonNull(operator); Object2ByteMap.Entry state = null; boolean empty = true; @@ -842,21 +848,21 @@ public class Object2ByteLinkedOpenCustomHashMap extends Object2ByteOpenCustom state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Object2ByteMap.Entry 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 @@ -935,10 +941,13 @@ public class Object2ByteLinkedOpenCustomHashMap extends Object2ByteOpenCustom public boolean add(T o) { throw new UnsupportedOperationException(); } - + + @Override + public void addFirst(T o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(T o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(T o) { throw new UnsupportedOperationException(); } - @Override public boolean addAndMoveToLast(T o) { throw new UnsupportedOperationException(); } @@ -1081,7 +1090,7 @@ public class Object2ByteLinkedOpenCustomHashMap extends Object2ByteOpenCustom } @Override - public T reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); T state = null; boolean empty = true; @@ -1096,19 +1105,19 @@ public class Object2ByteLinkedOpenCustomHashMap extends Object2ByteOpenCustom state = operator.apply(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public T findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + 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.ofNullable(keys[index]); index = (int)links[index]; } - return null; + return Optional.empty(); } @Override @@ -1243,7 +1252,7 @@ public class Object2ByteLinkedOpenCustomHashMap extends Object2ByteOpenCustom } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -1258,19 +1267,19 @@ public class Object2ByteLinkedOpenCustomHashMap extends Object2ByteOpenCustom state = operator.applyAsByte(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (byte)0; + if(size() <= 0) return OptionalByte.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalByte.of(values[index]); index = (int)links[index]; } - return (byte)0; + return OptionalByte.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/objects/maps/impl/customHash/Object2ByteOpenCustomHashMap.java b/src/main/java/speiger/src/collections/objects/maps/impl/customHash/Object2ByteOpenCustomHashMap.java index abbafaf..8beb599 100644 --- a/src/main/java/speiger/src/collections/objects/maps/impl/customHash/Object2ByteOpenCustomHashMap.java +++ b/src/main/java/speiger/src/collections/objects/maps/impl/customHash/Object2ByteOpenCustomHashMap.java @@ -5,6 +5,7 @@ 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; @@ -29,6 +30,7 @@ import speiger.src.collections.bytes.functions.ByteConsumer; import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.bytes.functions.function.BytePredicate; +import speiger.src.collections.bytes.functions.OptionalByte; import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.sets.AbstractObjectSet; import speiger.src.collections.objects.sets.ObjectSet; @@ -923,7 +925,7 @@ public class Object2ByteOpenCustomHashMap extends AbstractObject2ByteMap i } @Override - public Object2ByteMap.Entry reduce(ObjectObjectUnaryOperator, Object2ByteMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Object2ByteMap.Entry> operator) { Objects.requireNonNull(operator); Object2ByteMap.Entry state = null; boolean empty = true; @@ -940,25 +942,25 @@ public class Object2ByteOpenCustomHashMap extends AbstractObject2ByteMap i } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Object2ByteMap.Entry 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], null)) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1131,7 +1133,7 @@ public class Object2ByteOpenCustomHashMap extends AbstractObject2ByteMap i } @Override - public T reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); T state = null; boolean empty = true; @@ -1148,18 +1150,18 @@ public class Object2ByteOpenCustomHashMap extends AbstractObject2ByteMap i } state = operator.apply(state, keys[i]); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public T findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return Optional.empty(); + if(containsNull && filter.test(keys[nullIndex])) return Optional.ofNullable(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], null) && filter.test(keys[i])) return keys[i]; + if(!strategy.equals(keys[i], null) && filter.test(keys[i])) return Optional.ofNullable(keys[i]); } - return null; + return Optional.empty(); } @Override @@ -1274,7 +1276,7 @@ public class Object2ByteOpenCustomHashMap extends AbstractObject2ByteMap i } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -1291,18 +1293,18 @@ public class Object2ByteOpenCustomHashMap extends AbstractObject2ByteMap i } state = operator.applyAsByte(state, values[i]); } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (byte)0; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return OptionalByte.empty(); + if(containsNull && filter.test(values[nullIndex])) return OptionalByte.of(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], null) && filter.test(values[i])) return values[i]; + if(!strategy.equals(keys[i], null) && filter.test(values[i])) return OptionalByte.of(values[i]); } - return (byte)0; + return OptionalByte.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/objects/maps/impl/customHash/Object2CharLinkedOpenCustomHashMap.java b/src/main/java/speiger/src/collections/objects/maps/impl/customHash/Object2CharLinkedOpenCustomHashMap.java index 6ee2470..01f55c4 100644 --- a/src/main/java/speiger/src/collections/objects/maps/impl/customHash/Object2CharLinkedOpenCustomHashMap.java +++ b/src/main/java/speiger/src/collections/objects/maps/impl/customHash/Object2CharLinkedOpenCustomHashMap.java @@ -4,6 +4,7 @@ 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; @@ -28,6 +29,7 @@ import speiger.src.collections.chars.lists.CharListIterator; import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.chars.functions.function.CharPredicate; +import speiger.src.collections.chars.functions.OptionalChar; import speiger.src.collections.utils.HashUtil; /** @@ -662,6 +664,10 @@ public class Object2CharLinkedOpenCustomHashMap extends Object2CharOpenCustom } private class MapEntrySet extends AbstractObjectSet> implements Object2CharOrderedMap.FastOrderedSet { + @Override + public void addFirst(Object2CharMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Object2CharMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Object2CharMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -827,7 +833,7 @@ public class Object2CharLinkedOpenCustomHashMap extends Object2CharOpenCustom } @Override - public Object2CharMap.Entry reduce(ObjectObjectUnaryOperator, Object2CharMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Object2CharMap.Entry> operator) { Objects.requireNonNull(operator); Object2CharMap.Entry state = null; boolean empty = true; @@ -842,21 +848,21 @@ public class Object2CharLinkedOpenCustomHashMap extends Object2CharOpenCustom state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Object2CharMap.Entry 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 @@ -935,10 +941,13 @@ public class Object2CharLinkedOpenCustomHashMap extends Object2CharOpenCustom public boolean add(T o) { throw new UnsupportedOperationException(); } - + + @Override + public void addFirst(T o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(T o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(T o) { throw new UnsupportedOperationException(); } - @Override public boolean addAndMoveToLast(T o) { throw new UnsupportedOperationException(); } @@ -1081,7 +1090,7 @@ public class Object2CharLinkedOpenCustomHashMap extends Object2CharOpenCustom } @Override - public T reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); T state = null; boolean empty = true; @@ -1096,19 +1105,19 @@ public class Object2CharLinkedOpenCustomHashMap extends Object2CharOpenCustom state = operator.apply(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public T findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + 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.ofNullable(keys[index]); index = (int)links[index]; } - return null; + return Optional.empty(); } @Override @@ -1243,7 +1252,7 @@ public class Object2CharLinkedOpenCustomHashMap extends Object2CharOpenCustom } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -1258,19 +1267,19 @@ public class Object2CharLinkedOpenCustomHashMap extends Object2CharOpenCustom state = operator.applyAsChar(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (char)0; + if(size() <= 0) return OptionalChar.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalChar.of(values[index]); index = (int)links[index]; } - return (char)0; + return OptionalChar.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/objects/maps/impl/customHash/Object2CharOpenCustomHashMap.java b/src/main/java/speiger/src/collections/objects/maps/impl/customHash/Object2CharOpenCustomHashMap.java index 3163824..6ebda91 100644 --- a/src/main/java/speiger/src/collections/objects/maps/impl/customHash/Object2CharOpenCustomHashMap.java +++ b/src/main/java/speiger/src/collections/objects/maps/impl/customHash/Object2CharOpenCustomHashMap.java @@ -5,6 +5,7 @@ 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; @@ -29,6 +30,7 @@ import speiger.src.collections.chars.functions.CharConsumer; import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.chars.functions.function.CharPredicate; +import speiger.src.collections.chars.functions.OptionalChar; import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.sets.AbstractObjectSet; import speiger.src.collections.objects.sets.ObjectSet; @@ -923,7 +925,7 @@ public class Object2CharOpenCustomHashMap extends AbstractObject2CharMap i } @Override - public Object2CharMap.Entry reduce(ObjectObjectUnaryOperator, Object2CharMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Object2CharMap.Entry> operator) { Objects.requireNonNull(operator); Object2CharMap.Entry state = null; boolean empty = true; @@ -940,25 +942,25 @@ public class Object2CharOpenCustomHashMap extends AbstractObject2CharMap i } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Object2CharMap.Entry 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], null)) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1131,7 +1133,7 @@ public class Object2CharOpenCustomHashMap extends AbstractObject2CharMap i } @Override - public T reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); T state = null; boolean empty = true; @@ -1148,18 +1150,18 @@ public class Object2CharOpenCustomHashMap extends AbstractObject2CharMap i } state = operator.apply(state, keys[i]); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public T findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return Optional.empty(); + if(containsNull && filter.test(keys[nullIndex])) return Optional.ofNullable(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], null) && filter.test(keys[i])) return keys[i]; + if(!strategy.equals(keys[i], null) && filter.test(keys[i])) return Optional.ofNullable(keys[i]); } - return null; + return Optional.empty(); } @Override @@ -1274,7 +1276,7 @@ public class Object2CharOpenCustomHashMap extends AbstractObject2CharMap i } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -1291,18 +1293,18 @@ public class Object2CharOpenCustomHashMap extends AbstractObject2CharMap i } state = operator.applyAsChar(state, values[i]); } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (char)0; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return OptionalChar.empty(); + if(containsNull && filter.test(values[nullIndex])) return OptionalChar.of(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], null) && filter.test(values[i])) return values[i]; + if(!strategy.equals(keys[i], null) && filter.test(values[i])) return OptionalChar.of(values[i]); } - return (char)0; + return OptionalChar.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/objects/maps/impl/customHash/Object2DoubleLinkedOpenCustomHashMap.java b/src/main/java/speiger/src/collections/objects/maps/impl/customHash/Object2DoubleLinkedOpenCustomHashMap.java index c64ac4e..6a30af0 100644 --- a/src/main/java/speiger/src/collections/objects/maps/impl/customHash/Object2DoubleLinkedOpenCustomHashMap.java +++ b/src/main/java/speiger/src/collections/objects/maps/impl/customHash/Object2DoubleLinkedOpenCustomHashMap.java @@ -4,10 +4,12 @@ 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; import java.util.function.DoublePredicate; +import java.util.OptionalDouble; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; @@ -662,6 +664,10 @@ public class Object2DoubleLinkedOpenCustomHashMap extends Object2DoubleOpenCu } private class MapEntrySet extends AbstractObjectSet> implements Object2DoubleOrderedMap.FastOrderedSet { + @Override + public void addFirst(Object2DoubleMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Object2DoubleMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Object2DoubleMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -827,7 +833,7 @@ public class Object2DoubleLinkedOpenCustomHashMap extends Object2DoubleOpenCu } @Override - public Object2DoubleMap.Entry reduce(ObjectObjectUnaryOperator, Object2DoubleMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Object2DoubleMap.Entry> operator) { Objects.requireNonNull(operator); Object2DoubleMap.Entry state = null; boolean empty = true; @@ -842,21 +848,21 @@ public class Object2DoubleLinkedOpenCustomHashMap extends Object2DoubleOpenCu state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Object2DoubleMap.Entry 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 @@ -935,10 +941,13 @@ public class Object2DoubleLinkedOpenCustomHashMap extends Object2DoubleOpenCu public boolean add(T o) { throw new UnsupportedOperationException(); } - + + @Override + public void addFirst(T o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(T o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(T o) { throw new UnsupportedOperationException(); } - @Override public boolean addAndMoveToLast(T o) { throw new UnsupportedOperationException(); } @@ -1081,7 +1090,7 @@ public class Object2DoubleLinkedOpenCustomHashMap extends Object2DoubleOpenCu } @Override - public T reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); T state = null; boolean empty = true; @@ -1096,19 +1105,19 @@ public class Object2DoubleLinkedOpenCustomHashMap extends Object2DoubleOpenCu state = operator.apply(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public T findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + 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.ofNullable(keys[index]); index = (int)links[index]; } - return null; + return Optional.empty(); } @Override @@ -1243,7 +1252,7 @@ public class Object2DoubleLinkedOpenCustomHashMap extends Object2DoubleOpenCu } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -1258,19 +1267,19 @@ public class Object2DoubleLinkedOpenCustomHashMap extends Object2DoubleOpenCu state = operator.applyAsDouble(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0D; + if(size() <= 0) return OptionalDouble.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalDouble.of(values[index]); index = (int)links[index]; } - return 0D; + return OptionalDouble.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/objects/maps/impl/customHash/Object2DoubleOpenCustomHashMap.java b/src/main/java/speiger/src/collections/objects/maps/impl/customHash/Object2DoubleOpenCustomHashMap.java index 281c4d3..a883a4b 100644 --- a/src/main/java/speiger/src/collections/objects/maps/impl/customHash/Object2DoubleOpenCustomHashMap.java +++ b/src/main/java/speiger/src/collections/objects/maps/impl/customHash/Object2DoubleOpenCustomHashMap.java @@ -5,10 +5,12 @@ 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; import java.util.function.DoublePredicate; +import java.util.OptionalDouble; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntDoubleConsumer; @@ -923,7 +925,7 @@ public class Object2DoubleOpenCustomHashMap extends AbstractObject2DoubleMap< } @Override - public Object2DoubleMap.Entry reduce(ObjectObjectUnaryOperator, Object2DoubleMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Object2DoubleMap.Entry> operator) { Objects.requireNonNull(operator); Object2DoubleMap.Entry state = null; boolean empty = true; @@ -940,25 +942,25 @@ public class Object2DoubleOpenCustomHashMap extends AbstractObject2DoubleMap< } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Object2DoubleMap.Entry 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], null)) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1131,7 +1133,7 @@ public class Object2DoubleOpenCustomHashMap extends AbstractObject2DoubleMap< } @Override - public T reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); T state = null; boolean empty = true; @@ -1148,18 +1150,18 @@ public class Object2DoubleOpenCustomHashMap extends AbstractObject2DoubleMap< } state = operator.apply(state, keys[i]); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public T findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return Optional.empty(); + if(containsNull && filter.test(keys[nullIndex])) return Optional.ofNullable(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], null) && filter.test(keys[i])) return keys[i]; + if(!strategy.equals(keys[i], null) && filter.test(keys[i])) return Optional.ofNullable(keys[i]); } - return null; + return Optional.empty(); } @Override @@ -1274,7 +1276,7 @@ public class Object2DoubleOpenCustomHashMap extends AbstractObject2DoubleMap< } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -1291,18 +1293,18 @@ public class Object2DoubleOpenCustomHashMap extends AbstractObject2DoubleMap< } state = operator.applyAsDouble(state, values[i]); } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0D; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return OptionalDouble.empty(); + if(containsNull && filter.test(values[nullIndex])) return OptionalDouble.of(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], null) && filter.test(values[i])) return values[i]; + if(!strategy.equals(keys[i], null) && filter.test(values[i])) return OptionalDouble.of(values[i]); } - return 0D; + return OptionalDouble.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/objects/maps/impl/customHash/Object2FloatLinkedOpenCustomHashMap.java b/src/main/java/speiger/src/collections/objects/maps/impl/customHash/Object2FloatLinkedOpenCustomHashMap.java index d14ac92..1f0f637 100644 --- a/src/main/java/speiger/src/collections/objects/maps/impl/customHash/Object2FloatLinkedOpenCustomHashMap.java +++ b/src/main/java/speiger/src/collections/objects/maps/impl/customHash/Object2FloatLinkedOpenCustomHashMap.java @@ -4,6 +4,7 @@ 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; @@ -28,6 +29,7 @@ import speiger.src.collections.floats.lists.FloatListIterator; import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.floats.functions.function.FloatPredicate; +import speiger.src.collections.floats.functions.OptionalFloat; import speiger.src.collections.utils.HashUtil; /** @@ -662,6 +664,10 @@ public class Object2FloatLinkedOpenCustomHashMap extends Object2FloatOpenCust } private class MapEntrySet extends AbstractObjectSet> implements Object2FloatOrderedMap.FastOrderedSet { + @Override + public void addFirst(Object2FloatMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Object2FloatMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Object2FloatMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -827,7 +833,7 @@ public class Object2FloatLinkedOpenCustomHashMap extends Object2FloatOpenCust } @Override - public Object2FloatMap.Entry reduce(ObjectObjectUnaryOperator, Object2FloatMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Object2FloatMap.Entry> operator) { Objects.requireNonNull(operator); Object2FloatMap.Entry state = null; boolean empty = true; @@ -842,21 +848,21 @@ public class Object2FloatLinkedOpenCustomHashMap extends Object2FloatOpenCust state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Object2FloatMap.Entry 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 @@ -935,10 +941,13 @@ public class Object2FloatLinkedOpenCustomHashMap extends Object2FloatOpenCust public boolean add(T o) { throw new UnsupportedOperationException(); } - + + @Override + public void addFirst(T o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(T o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(T o) { throw new UnsupportedOperationException(); } - @Override public boolean addAndMoveToLast(T o) { throw new UnsupportedOperationException(); } @@ -1081,7 +1090,7 @@ public class Object2FloatLinkedOpenCustomHashMap extends Object2FloatOpenCust } @Override - public T reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); T state = null; boolean empty = true; @@ -1096,19 +1105,19 @@ public class Object2FloatLinkedOpenCustomHashMap extends Object2FloatOpenCust state = operator.apply(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public T findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + 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.ofNullable(keys[index]); index = (int)links[index]; } - return null; + return Optional.empty(); } @Override @@ -1243,7 +1252,7 @@ public class Object2FloatLinkedOpenCustomHashMap extends Object2FloatOpenCust } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -1258,19 +1267,19 @@ public class Object2FloatLinkedOpenCustomHashMap extends Object2FloatOpenCust state = operator.applyAsFloat(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0F; + if(size() <= 0) return OptionalFloat.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalFloat.of(values[index]); index = (int)links[index]; } - return 0F; + return OptionalFloat.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/objects/maps/impl/customHash/Object2FloatOpenCustomHashMap.java b/src/main/java/speiger/src/collections/objects/maps/impl/customHash/Object2FloatOpenCustomHashMap.java index 741f672..9cafaa9 100644 --- a/src/main/java/speiger/src/collections/objects/maps/impl/customHash/Object2FloatOpenCustomHashMap.java +++ b/src/main/java/speiger/src/collections/objects/maps/impl/customHash/Object2FloatOpenCustomHashMap.java @@ -5,6 +5,7 @@ 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; @@ -29,6 +30,7 @@ import speiger.src.collections.floats.functions.FloatConsumer; import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.floats.functions.function.FloatPredicate; +import speiger.src.collections.floats.functions.OptionalFloat; import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.sets.AbstractObjectSet; import speiger.src.collections.objects.sets.ObjectSet; @@ -923,7 +925,7 @@ public class Object2FloatOpenCustomHashMap extends AbstractObject2FloatMap } @Override - public Object2FloatMap.Entry reduce(ObjectObjectUnaryOperator, Object2FloatMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Object2FloatMap.Entry> operator) { Objects.requireNonNull(operator); Object2FloatMap.Entry state = null; boolean empty = true; @@ -940,25 +942,25 @@ public class Object2FloatOpenCustomHashMap extends AbstractObject2FloatMap } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Object2FloatMap.Entry 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], null)) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1131,7 +1133,7 @@ public class Object2FloatOpenCustomHashMap extends AbstractObject2FloatMap } @Override - public T reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); T state = null; boolean empty = true; @@ -1148,18 +1150,18 @@ public class Object2FloatOpenCustomHashMap extends AbstractObject2FloatMap } state = operator.apply(state, keys[i]); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public T findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return Optional.empty(); + if(containsNull && filter.test(keys[nullIndex])) return Optional.ofNullable(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], null) && filter.test(keys[i])) return keys[i]; + if(!strategy.equals(keys[i], null) && filter.test(keys[i])) return Optional.ofNullable(keys[i]); } - return null; + return Optional.empty(); } @Override @@ -1274,7 +1276,7 @@ public class Object2FloatOpenCustomHashMap extends AbstractObject2FloatMap } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -1291,18 +1293,18 @@ public class Object2FloatOpenCustomHashMap extends AbstractObject2FloatMap } state = operator.applyAsFloat(state, values[i]); } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0F; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return OptionalFloat.empty(); + if(containsNull && filter.test(values[nullIndex])) return OptionalFloat.of(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], null) && filter.test(values[i])) return values[i]; + if(!strategy.equals(keys[i], null) && filter.test(values[i])) return OptionalFloat.of(values[i]); } - return 0F; + return OptionalFloat.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/objects/maps/impl/customHash/Object2IntLinkedOpenCustomHashMap.java b/src/main/java/speiger/src/collections/objects/maps/impl/customHash/Object2IntLinkedOpenCustomHashMap.java index 9c999ec..3910662 100644 --- a/src/main/java/speiger/src/collections/objects/maps/impl/customHash/Object2IntLinkedOpenCustomHashMap.java +++ b/src/main/java/speiger/src/collections/objects/maps/impl/customHash/Object2IntLinkedOpenCustomHashMap.java @@ -4,10 +4,12 @@ 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; import java.util.function.IntPredicate; +import java.util.OptionalInt; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; @@ -662,6 +664,10 @@ public class Object2IntLinkedOpenCustomHashMap extends Object2IntOpenCustomHa } private class MapEntrySet extends AbstractObjectSet> implements Object2IntOrderedMap.FastOrderedSet { + @Override + public void addFirst(Object2IntMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Object2IntMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Object2IntMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -827,7 +833,7 @@ public class Object2IntLinkedOpenCustomHashMap extends Object2IntOpenCustomHa } @Override - public Object2IntMap.Entry reduce(ObjectObjectUnaryOperator, Object2IntMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Object2IntMap.Entry> operator) { Objects.requireNonNull(operator); Object2IntMap.Entry state = null; boolean empty = true; @@ -842,21 +848,21 @@ public class Object2IntLinkedOpenCustomHashMap extends Object2IntOpenCustomHa state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Object2IntMap.Entry 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 @@ -935,10 +941,13 @@ public class Object2IntLinkedOpenCustomHashMap extends Object2IntOpenCustomHa public boolean add(T o) { throw new UnsupportedOperationException(); } - + + @Override + public void addFirst(T o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(T o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(T o) { throw new UnsupportedOperationException(); } - @Override public boolean addAndMoveToLast(T o) { throw new UnsupportedOperationException(); } @@ -1081,7 +1090,7 @@ public class Object2IntLinkedOpenCustomHashMap extends Object2IntOpenCustomHa } @Override - public T reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); T state = null; boolean empty = true; @@ -1096,19 +1105,19 @@ public class Object2IntLinkedOpenCustomHashMap extends Object2IntOpenCustomHa state = operator.apply(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public T findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + 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.ofNullable(keys[index]); index = (int)links[index]; } - return null; + return Optional.empty(); } @Override @@ -1243,7 +1252,7 @@ public class Object2IntLinkedOpenCustomHashMap extends Object2IntOpenCustomHa } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -1258,19 +1267,19 @@ public class Object2IntLinkedOpenCustomHashMap extends Object2IntOpenCustomHa state = operator.applyAsInt(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0; + if(size() <= 0) return OptionalInt.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalInt.of(values[index]); index = (int)links[index]; } - return 0; + return OptionalInt.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/objects/maps/impl/customHash/Object2IntOpenCustomHashMap.java b/src/main/java/speiger/src/collections/objects/maps/impl/customHash/Object2IntOpenCustomHashMap.java index 8441830..6d6bbda 100644 --- a/src/main/java/speiger/src/collections/objects/maps/impl/customHash/Object2IntOpenCustomHashMap.java +++ b/src/main/java/speiger/src/collections/objects/maps/impl/customHash/Object2IntOpenCustomHashMap.java @@ -5,10 +5,12 @@ 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; import java.util.function.IntPredicate; +import java.util.OptionalInt; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntIntConsumer; @@ -923,7 +925,7 @@ public class Object2IntOpenCustomHashMap extends AbstractObject2IntMap imp } @Override - public Object2IntMap.Entry reduce(ObjectObjectUnaryOperator, Object2IntMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Object2IntMap.Entry> operator) { Objects.requireNonNull(operator); Object2IntMap.Entry state = null; boolean empty = true; @@ -940,25 +942,25 @@ public class Object2IntOpenCustomHashMap extends AbstractObject2IntMap imp } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Object2IntMap.Entry 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], null)) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1131,7 +1133,7 @@ public class Object2IntOpenCustomHashMap extends AbstractObject2IntMap imp } @Override - public T reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); T state = null; boolean empty = true; @@ -1148,18 +1150,18 @@ public class Object2IntOpenCustomHashMap extends AbstractObject2IntMap imp } state = operator.apply(state, keys[i]); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public T findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return Optional.empty(); + if(containsNull && filter.test(keys[nullIndex])) return Optional.ofNullable(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], null) && filter.test(keys[i])) return keys[i]; + if(!strategy.equals(keys[i], null) && filter.test(keys[i])) return Optional.ofNullable(keys[i]); } - return null; + return Optional.empty(); } @Override @@ -1274,7 +1276,7 @@ public class Object2IntOpenCustomHashMap extends AbstractObject2IntMap imp } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -1291,18 +1293,18 @@ public class Object2IntOpenCustomHashMap extends AbstractObject2IntMap imp } state = operator.applyAsInt(state, values[i]); } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return OptionalInt.empty(); + if(containsNull && filter.test(values[nullIndex])) return OptionalInt.of(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], null) && filter.test(values[i])) return values[i]; + if(!strategy.equals(keys[i], null) && filter.test(values[i])) return OptionalInt.of(values[i]); } - return 0; + return OptionalInt.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/objects/maps/impl/customHash/Object2LongLinkedOpenCustomHashMap.java b/src/main/java/speiger/src/collections/objects/maps/impl/customHash/Object2LongLinkedOpenCustomHashMap.java index 7b3c4d2..5ff4ffa 100644 --- a/src/main/java/speiger/src/collections/objects/maps/impl/customHash/Object2LongLinkedOpenCustomHashMap.java +++ b/src/main/java/speiger/src/collections/objects/maps/impl/customHash/Object2LongLinkedOpenCustomHashMap.java @@ -4,10 +4,12 @@ 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; import java.util.function.LongPredicate; +import java.util.OptionalLong; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; @@ -662,6 +664,10 @@ public class Object2LongLinkedOpenCustomHashMap extends Object2LongOpenCustom } private class MapEntrySet extends AbstractObjectSet> implements Object2LongOrderedMap.FastOrderedSet { + @Override + public void addFirst(Object2LongMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Object2LongMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Object2LongMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -827,7 +833,7 @@ public class Object2LongLinkedOpenCustomHashMap extends Object2LongOpenCustom } @Override - public Object2LongMap.Entry reduce(ObjectObjectUnaryOperator, Object2LongMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Object2LongMap.Entry> operator) { Objects.requireNonNull(operator); Object2LongMap.Entry state = null; boolean empty = true; @@ -842,21 +848,21 @@ public class Object2LongLinkedOpenCustomHashMap extends Object2LongOpenCustom state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Object2LongMap.Entry 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 @@ -935,10 +941,13 @@ public class Object2LongLinkedOpenCustomHashMap extends Object2LongOpenCustom public boolean add(T o) { throw new UnsupportedOperationException(); } - + + @Override + public void addFirst(T o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(T o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(T o) { throw new UnsupportedOperationException(); } - @Override public boolean addAndMoveToLast(T o) { throw new UnsupportedOperationException(); } @@ -1081,7 +1090,7 @@ public class Object2LongLinkedOpenCustomHashMap extends Object2LongOpenCustom } @Override - public T reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); T state = null; boolean empty = true; @@ -1096,19 +1105,19 @@ public class Object2LongLinkedOpenCustomHashMap extends Object2LongOpenCustom state = operator.apply(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public T findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + 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.ofNullable(keys[index]); index = (int)links[index]; } - return null; + return Optional.empty(); } @Override @@ -1243,7 +1252,7 @@ public class Object2LongLinkedOpenCustomHashMap extends Object2LongOpenCustom } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -1258,19 +1267,19 @@ public class Object2LongLinkedOpenCustomHashMap extends Object2LongOpenCustom state = operator.applyAsLong(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0L; + if(size() <= 0) return OptionalLong.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalLong.of(values[index]); index = (int)links[index]; } - return 0L; + return OptionalLong.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/objects/maps/impl/customHash/Object2LongOpenCustomHashMap.java b/src/main/java/speiger/src/collections/objects/maps/impl/customHash/Object2LongOpenCustomHashMap.java index f3245eb..0487190 100644 --- a/src/main/java/speiger/src/collections/objects/maps/impl/customHash/Object2LongOpenCustomHashMap.java +++ b/src/main/java/speiger/src/collections/objects/maps/impl/customHash/Object2LongOpenCustomHashMap.java @@ -5,10 +5,12 @@ 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; import java.util.function.LongPredicate; +import java.util.OptionalLong; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntLongConsumer; @@ -923,7 +925,7 @@ public class Object2LongOpenCustomHashMap extends AbstractObject2LongMap i } @Override - public Object2LongMap.Entry reduce(ObjectObjectUnaryOperator, Object2LongMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Object2LongMap.Entry> operator) { Objects.requireNonNull(operator); Object2LongMap.Entry state = null; boolean empty = true; @@ -940,25 +942,25 @@ public class Object2LongOpenCustomHashMap extends AbstractObject2LongMap i } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Object2LongMap.Entry 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], null)) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1131,7 +1133,7 @@ public class Object2LongOpenCustomHashMap extends AbstractObject2LongMap i } @Override - public T reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); T state = null; boolean empty = true; @@ -1148,18 +1150,18 @@ public class Object2LongOpenCustomHashMap extends AbstractObject2LongMap i } state = operator.apply(state, keys[i]); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public T findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return Optional.empty(); + if(containsNull && filter.test(keys[nullIndex])) return Optional.ofNullable(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], null) && filter.test(keys[i])) return keys[i]; + if(!strategy.equals(keys[i], null) && filter.test(keys[i])) return Optional.ofNullable(keys[i]); } - return null; + return Optional.empty(); } @Override @@ -1274,7 +1276,7 @@ public class Object2LongOpenCustomHashMap extends AbstractObject2LongMap i } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -1291,18 +1293,18 @@ public class Object2LongOpenCustomHashMap extends AbstractObject2LongMap i } state = operator.applyAsLong(state, values[i]); } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0L; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return OptionalLong.empty(); + if(containsNull && filter.test(values[nullIndex])) return OptionalLong.of(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], null) && filter.test(values[i])) return values[i]; + if(!strategy.equals(keys[i], null) && filter.test(values[i])) return OptionalLong.of(values[i]); } - return 0L; + return OptionalLong.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/objects/maps/impl/customHash/Object2ObjectLinkedOpenCustomHashMap.java b/src/main/java/speiger/src/collections/objects/maps/impl/customHash/Object2ObjectLinkedOpenCustomHashMap.java index 41cb1c1..239872d 100644 --- a/src/main/java/speiger/src/collections/objects/maps/impl/customHash/Object2ObjectLinkedOpenCustomHashMap.java +++ b/src/main/java/speiger/src/collections/objects/maps/impl/customHash/Object2ObjectLinkedOpenCustomHashMap.java @@ -4,6 +4,7 @@ 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; @@ -628,6 +629,10 @@ public class Object2ObjectLinkedOpenCustomHashMap extends Object2ObjectOpe } private class MapEntrySet extends AbstractObjectSet> implements Object2ObjectOrderedMap.FastOrderedSet { + @Override + public void addFirst(Object2ObjectMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Object2ObjectMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Object2ObjectMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -793,7 +798,7 @@ public class Object2ObjectLinkedOpenCustomHashMap extends Object2ObjectOpe } @Override - public Object2ObjectMap.Entry reduce(ObjectObjectUnaryOperator, Object2ObjectMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Object2ObjectMap.Entry> operator) { Objects.requireNonNull(operator); Object2ObjectMap.Entry state = null; boolean empty = true; @@ -808,21 +813,21 @@ public class Object2ObjectLinkedOpenCustomHashMap extends Object2ObjectOpe state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Object2ObjectMap.Entry 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 @@ -901,10 +906,13 @@ public class Object2ObjectLinkedOpenCustomHashMap extends Object2ObjectOpe public boolean add(T o) { throw new UnsupportedOperationException(); } - + + @Override + public void addFirst(T o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(T o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(T o) { throw new UnsupportedOperationException(); } - @Override public boolean addAndMoveToLast(T o) { throw new UnsupportedOperationException(); } @@ -1047,7 +1055,7 @@ public class Object2ObjectLinkedOpenCustomHashMap extends Object2ObjectOpe } @Override - public T reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); T state = null; boolean empty = true; @@ -1062,19 +1070,19 @@ public class Object2ObjectLinkedOpenCustomHashMap extends Object2ObjectOpe state = operator.apply(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public T findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + 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.ofNullable(keys[index]); index = (int)links[index]; } - return null; + return Optional.empty(); } @Override @@ -1210,7 +1218,7 @@ public class Object2ObjectLinkedOpenCustomHashMap extends Object2ObjectOpe } @Override - public V reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); V state = null; boolean empty = true; @@ -1225,19 +1233,19 @@ public class Object2ObjectLinkedOpenCustomHashMap extends Object2ObjectOpe state = operator.apply(state, values[index]); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public V findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return Optional.ofNullable(values[index]); index = (int)links[index]; } - return null; + return Optional.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/objects/maps/impl/customHash/Object2ObjectOpenCustomHashMap.java b/src/main/java/speiger/src/collections/objects/maps/impl/customHash/Object2ObjectOpenCustomHashMap.java index bb39a87..1cb06e4 100644 --- a/src/main/java/speiger/src/collections/objects/maps/impl/customHash/Object2ObjectOpenCustomHashMap.java +++ b/src/main/java/speiger/src/collections/objects/maps/impl/customHash/Object2ObjectOpenCustomHashMap.java @@ -5,6 +5,7 @@ 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; @@ -782,7 +783,7 @@ public class Object2ObjectOpenCustomHashMap extends AbstractObject2ObjectM } @Override - public Object2ObjectMap.Entry reduce(ObjectObjectUnaryOperator, Object2ObjectMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Object2ObjectMap.Entry> operator) { Objects.requireNonNull(operator); Object2ObjectMap.Entry state = null; boolean empty = true; @@ -799,25 +800,25 @@ public class Object2ObjectOpenCustomHashMap extends AbstractObject2ObjectM } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Object2ObjectMap.Entry 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], null)) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -990,7 +991,7 @@ public class Object2ObjectOpenCustomHashMap extends AbstractObject2ObjectM } @Override - public T reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); T state = null; boolean empty = true; @@ -1007,18 +1008,18 @@ public class Object2ObjectOpenCustomHashMap extends AbstractObject2ObjectM } state = operator.apply(state, keys[i]); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public T findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return Optional.empty(); + if(containsNull && filter.test(keys[nullIndex])) return Optional.ofNullable(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], null) && filter.test(keys[i])) return keys[i]; + if(!strategy.equals(keys[i], null) && filter.test(keys[i])) return Optional.ofNullable(keys[i]); } - return null; + return Optional.empty(); } @Override @@ -1133,7 +1134,7 @@ public class Object2ObjectOpenCustomHashMap extends AbstractObject2ObjectM } @Override - public V reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); V state = null; boolean empty = true; @@ -1150,18 +1151,18 @@ public class Object2ObjectOpenCustomHashMap extends AbstractObject2ObjectM } state = operator.apply(state, values[i]); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public V findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return Optional.empty(); + if(containsNull && filter.test(values[nullIndex])) return Optional.ofNullable(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], null) && filter.test(values[i])) return values[i]; + if(!strategy.equals(keys[i], null) && filter.test(values[i])) return Optional.ofNullable(values[i]); } - return null; + return Optional.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/objects/maps/impl/customHash/Object2ShortLinkedOpenCustomHashMap.java b/src/main/java/speiger/src/collections/objects/maps/impl/customHash/Object2ShortLinkedOpenCustomHashMap.java index af60ea1..ff6cd48 100644 --- a/src/main/java/speiger/src/collections/objects/maps/impl/customHash/Object2ShortLinkedOpenCustomHashMap.java +++ b/src/main/java/speiger/src/collections/objects/maps/impl/customHash/Object2ShortLinkedOpenCustomHashMap.java @@ -4,6 +4,7 @@ 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; @@ -28,6 +29,7 @@ import speiger.src.collections.shorts.lists.ShortListIterator; import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.shorts.functions.function.ShortPredicate; +import speiger.src.collections.shorts.functions.OptionalShort; import speiger.src.collections.utils.HashUtil; /** @@ -662,6 +664,10 @@ public class Object2ShortLinkedOpenCustomHashMap extends Object2ShortOpenCust } private class MapEntrySet extends AbstractObjectSet> implements Object2ShortOrderedMap.FastOrderedSet { + @Override + public void addFirst(Object2ShortMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Object2ShortMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Object2ShortMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -827,7 +833,7 @@ public class Object2ShortLinkedOpenCustomHashMap extends Object2ShortOpenCust } @Override - public Object2ShortMap.Entry reduce(ObjectObjectUnaryOperator, Object2ShortMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Object2ShortMap.Entry> operator) { Objects.requireNonNull(operator); Object2ShortMap.Entry state = null; boolean empty = true; @@ -842,21 +848,21 @@ public class Object2ShortLinkedOpenCustomHashMap extends Object2ShortOpenCust state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Object2ShortMap.Entry 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 @@ -935,10 +941,13 @@ public class Object2ShortLinkedOpenCustomHashMap extends Object2ShortOpenCust public boolean add(T o) { throw new UnsupportedOperationException(); } - + + @Override + public void addFirst(T o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(T o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(T o) { throw new UnsupportedOperationException(); } - @Override public boolean addAndMoveToLast(T o) { throw new UnsupportedOperationException(); } @@ -1081,7 +1090,7 @@ public class Object2ShortLinkedOpenCustomHashMap extends Object2ShortOpenCust } @Override - public T reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); T state = null; boolean empty = true; @@ -1096,19 +1105,19 @@ public class Object2ShortLinkedOpenCustomHashMap extends Object2ShortOpenCust state = operator.apply(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public T findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + 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.ofNullable(keys[index]); index = (int)links[index]; } - return null; + return Optional.empty(); } @Override @@ -1243,7 +1252,7 @@ public class Object2ShortLinkedOpenCustomHashMap extends Object2ShortOpenCust } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -1258,19 +1267,19 @@ public class Object2ShortLinkedOpenCustomHashMap extends Object2ShortOpenCust state = operator.applyAsShort(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (short)0; + if(size() <= 0) return OptionalShort.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalShort.of(values[index]); index = (int)links[index]; } - return (short)0; + return OptionalShort.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/objects/maps/impl/customHash/Object2ShortOpenCustomHashMap.java b/src/main/java/speiger/src/collections/objects/maps/impl/customHash/Object2ShortOpenCustomHashMap.java index 3359c79..cadf530 100644 --- a/src/main/java/speiger/src/collections/objects/maps/impl/customHash/Object2ShortOpenCustomHashMap.java +++ b/src/main/java/speiger/src/collections/objects/maps/impl/customHash/Object2ShortOpenCustomHashMap.java @@ -5,6 +5,7 @@ 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; @@ -29,6 +30,7 @@ import speiger.src.collections.shorts.functions.ShortConsumer; import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.shorts.functions.function.ShortPredicate; +import speiger.src.collections.shorts.functions.OptionalShort; import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.sets.AbstractObjectSet; import speiger.src.collections.objects.sets.ObjectSet; @@ -923,7 +925,7 @@ public class Object2ShortOpenCustomHashMap extends AbstractObject2ShortMap } @Override - public Object2ShortMap.Entry reduce(ObjectObjectUnaryOperator, Object2ShortMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Object2ShortMap.Entry> operator) { Objects.requireNonNull(operator); Object2ShortMap.Entry state = null; boolean empty = true; @@ -940,25 +942,25 @@ public class Object2ShortOpenCustomHashMap extends AbstractObject2ShortMap } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Object2ShortMap.Entry 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], null)) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1131,7 +1133,7 @@ public class Object2ShortOpenCustomHashMap extends AbstractObject2ShortMap } @Override - public T reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); T state = null; boolean empty = true; @@ -1148,18 +1150,18 @@ public class Object2ShortOpenCustomHashMap extends AbstractObject2ShortMap } state = operator.apply(state, keys[i]); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public T findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return Optional.empty(); + if(containsNull && filter.test(keys[nullIndex])) return Optional.ofNullable(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], null) && filter.test(keys[i])) return keys[i]; + if(!strategy.equals(keys[i], null) && filter.test(keys[i])) return Optional.ofNullable(keys[i]); } - return null; + return Optional.empty(); } @Override @@ -1274,7 +1276,7 @@ public class Object2ShortOpenCustomHashMap extends AbstractObject2ShortMap } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -1291,18 +1293,18 @@ public class Object2ShortOpenCustomHashMap extends AbstractObject2ShortMap } state = operator.applyAsShort(state, values[i]); } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (short)0; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return OptionalShort.empty(); + if(containsNull && filter.test(values[nullIndex])) return OptionalShort.of(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], null) && filter.test(values[i])) return values[i]; + if(!strategy.equals(keys[i], null) && filter.test(values[i])) return OptionalShort.of(values[i]); } - return (short)0; + return OptionalShort.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/objects/maps/impl/hash/Object2BooleanLinkedOpenHashMap.java b/src/main/java/speiger/src/collections/objects/maps/impl/hash/Object2BooleanLinkedOpenHashMap.java index 9a08c5c..bfc10d7 100644 --- a/src/main/java/speiger/src/collections/objects/maps/impl/hash/Object2BooleanLinkedOpenHashMap.java +++ b/src/main/java/speiger/src/collections/objects/maps/impl/hash/Object2BooleanLinkedOpenHashMap.java @@ -4,6 +4,7 @@ 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; @@ -27,6 +28,7 @@ import speiger.src.collections.booleans.lists.BooleanListIterator; import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.booleans.functions.function.BooleanPredicate; +import speiger.src.collections.booleans.functions.OptionalBoolean; import speiger.src.collections.utils.HashUtil; /** @@ -660,6 +662,10 @@ public class Object2BooleanLinkedOpenHashMap extends Object2BooleanOpenHashMa } private class MapEntrySet extends AbstractObjectSet> implements Object2BooleanOrderedMap.FastOrderedSet { + @Override + public void addFirst(Object2BooleanMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Object2BooleanMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Object2BooleanMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -825,7 +831,7 @@ public class Object2BooleanLinkedOpenHashMap extends Object2BooleanOpenHashMa } @Override - public Object2BooleanMap.Entry reduce(ObjectObjectUnaryOperator, Object2BooleanMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Object2BooleanMap.Entry> operator) { Objects.requireNonNull(operator); Object2BooleanMap.Entry state = null; boolean empty = true; @@ -840,21 +846,21 @@ public class Object2BooleanLinkedOpenHashMap extends Object2BooleanOpenHashMa state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Object2BooleanMap.Entry 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 @@ -931,6 +937,12 @@ public class Object2BooleanLinkedOpenHashMap extends Object2BooleanOpenHashMa @Override public boolean add(T o) { throw new UnsupportedOperationException(); } + + @Override + public void addFirst(T o) { throw new UnsupportedOperationException(); } + + @Override + public void addLast(T o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(T o) { throw new UnsupportedOperationException(); } @@ -1077,7 +1089,7 @@ public class Object2BooleanLinkedOpenHashMap extends Object2BooleanOpenHashMa } @Override - public T reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); T state = null; boolean empty = true; @@ -1092,19 +1104,19 @@ public class Object2BooleanLinkedOpenHashMap extends Object2BooleanOpenHashMa state = operator.apply(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public T findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + 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.ofNullable(keys[index]); index = (int)links[index]; } - return null; + return Optional.empty(); } @Override @@ -1240,7 +1252,7 @@ public class Object2BooleanLinkedOpenHashMap extends Object2BooleanOpenHashMa } @Override - public boolean reduce(BooleanBooleanUnaryOperator operator) { + public OptionalBoolean reduce(BooleanBooleanUnaryOperator operator) { Objects.requireNonNull(operator); boolean state = false; boolean empty = true; @@ -1255,19 +1267,19 @@ public class Object2BooleanLinkedOpenHashMap extends Object2BooleanOpenHashMa state = operator.applyAsBoolean(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalBoolean.empty() : OptionalBoolean.of(state); } @Override - public boolean findFirst(BooleanPredicate filter) { + public OptionalBoolean findFirst(BooleanPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return false; + if(size() <= 0) return OptionalBoolean.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalBoolean.of(values[index]); index = (int)links[index]; } - return false; + return OptionalBoolean.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/objects/maps/impl/hash/Object2BooleanOpenHashMap.java b/src/main/java/speiger/src/collections/objects/maps/impl/hash/Object2BooleanOpenHashMap.java index d70a287..c0bbc17 100644 --- a/src/main/java/speiger/src/collections/objects/maps/impl/hash/Object2BooleanOpenHashMap.java +++ b/src/main/java/speiger/src/collections/objects/maps/impl/hash/Object2BooleanOpenHashMap.java @@ -5,6 +5,7 @@ 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; @@ -26,6 +27,7 @@ import speiger.src.collections.booleans.functions.BooleanConsumer; import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.booleans.functions.function.BooleanPredicate; +import speiger.src.collections.booleans.functions.OptionalBoolean; import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.sets.AbstractObjectSet; import speiger.src.collections.objects.sets.ObjectSet; @@ -873,7 +875,7 @@ public class Object2BooleanOpenHashMap extends AbstractObject2BooleanMap i } @Override - public Object2BooleanMap.Entry reduce(ObjectObjectUnaryOperator, Object2BooleanMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Object2BooleanMap.Entry> operator) { Objects.requireNonNull(operator); Object2BooleanMap.Entry state = null; boolean empty = true; @@ -890,25 +892,25 @@ public class Object2BooleanOpenHashMap extends AbstractObject2BooleanMap i } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Object2BooleanMap.Entry 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(keys[i] != null) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1080,7 +1082,7 @@ public class Object2BooleanOpenHashMap extends AbstractObject2BooleanMap i } @Override - public T reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); T state = null; boolean empty = true; @@ -1097,18 +1099,18 @@ public class Object2BooleanOpenHashMap extends AbstractObject2BooleanMap i } state = operator.apply(state, keys[i]); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public T findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return Optional.empty(); + if(containsNull && filter.test(keys[nullIndex])) return Optional.ofNullable(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(keys[i] != null && filter.test(keys[i])) return keys[i]; + if(keys[i] != null && filter.test(keys[i])) return Optional.ofNullable(keys[i]); } - return null; + return Optional.empty(); } @Override @@ -1223,7 +1225,7 @@ public class Object2BooleanOpenHashMap extends AbstractObject2BooleanMap i } @Override - public boolean reduce(BooleanBooleanUnaryOperator operator) { + public OptionalBoolean reduce(BooleanBooleanUnaryOperator operator) { Objects.requireNonNull(operator); boolean state = false; boolean empty = true; @@ -1240,18 +1242,18 @@ public class Object2BooleanOpenHashMap extends AbstractObject2BooleanMap i } state = operator.applyAsBoolean(state, values[i]); } - return state; + return empty ? OptionalBoolean.empty() : OptionalBoolean.of(state); } @Override - public boolean findFirst(BooleanPredicate filter) { + public OptionalBoolean findFirst(BooleanPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return false; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return OptionalBoolean.empty(); + if(containsNull && filter.test(values[nullIndex])) return OptionalBoolean.of(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(keys[i] != null && filter.test(values[i])) return values[i]; + if(keys[i] != null && filter.test(values[i])) return OptionalBoolean.of(values[i]); } - return false; + return OptionalBoolean.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/objects/maps/impl/hash/Object2ByteLinkedOpenHashMap.java b/src/main/java/speiger/src/collections/objects/maps/impl/hash/Object2ByteLinkedOpenHashMap.java index 71b62ac..66dd2c0 100644 --- a/src/main/java/speiger/src/collections/objects/maps/impl/hash/Object2ByteLinkedOpenHashMap.java +++ b/src/main/java/speiger/src/collections/objects/maps/impl/hash/Object2ByteLinkedOpenHashMap.java @@ -4,6 +4,7 @@ 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; @@ -27,6 +28,7 @@ import speiger.src.collections.bytes.lists.ByteListIterator; import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.bytes.functions.function.BytePredicate; +import speiger.src.collections.bytes.functions.OptionalByte; import speiger.src.collections.utils.HashUtil; /** @@ -660,6 +662,10 @@ public class Object2ByteLinkedOpenHashMap extends Object2ByteOpenHashMap i } private class MapEntrySet extends AbstractObjectSet> implements Object2ByteOrderedMap.FastOrderedSet { + @Override + public void addFirst(Object2ByteMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Object2ByteMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Object2ByteMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -825,7 +831,7 @@ public class Object2ByteLinkedOpenHashMap extends Object2ByteOpenHashMap i } @Override - public Object2ByteMap.Entry reduce(ObjectObjectUnaryOperator, Object2ByteMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Object2ByteMap.Entry> operator) { Objects.requireNonNull(operator); Object2ByteMap.Entry state = null; boolean empty = true; @@ -840,21 +846,21 @@ public class Object2ByteLinkedOpenHashMap extends Object2ByteOpenHashMap i state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Object2ByteMap.Entry 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 @@ -931,6 +937,12 @@ public class Object2ByteLinkedOpenHashMap extends Object2ByteOpenHashMap i @Override public boolean add(T o) { throw new UnsupportedOperationException(); } + + @Override + public void addFirst(T o) { throw new UnsupportedOperationException(); } + + @Override + public void addLast(T o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(T o) { throw new UnsupportedOperationException(); } @@ -1077,7 +1089,7 @@ public class Object2ByteLinkedOpenHashMap extends Object2ByteOpenHashMap i } @Override - public T reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); T state = null; boolean empty = true; @@ -1092,19 +1104,19 @@ public class Object2ByteLinkedOpenHashMap extends Object2ByteOpenHashMap i state = operator.apply(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public T findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + 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.ofNullable(keys[index]); index = (int)links[index]; } - return null; + return Optional.empty(); } @Override @@ -1240,7 +1252,7 @@ public class Object2ByteLinkedOpenHashMap extends Object2ByteOpenHashMap i } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -1255,19 +1267,19 @@ public class Object2ByteLinkedOpenHashMap extends Object2ByteOpenHashMap i state = operator.applyAsByte(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (byte)0; + if(size() <= 0) return OptionalByte.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalByte.of(values[index]); index = (int)links[index]; } - return (byte)0; + return OptionalByte.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/objects/maps/impl/hash/Object2ByteOpenHashMap.java b/src/main/java/speiger/src/collections/objects/maps/impl/hash/Object2ByteOpenHashMap.java index a5d7f78..c130680 100644 --- a/src/main/java/speiger/src/collections/objects/maps/impl/hash/Object2ByteOpenHashMap.java +++ b/src/main/java/speiger/src/collections/objects/maps/impl/hash/Object2ByteOpenHashMap.java @@ -5,6 +5,7 @@ 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; @@ -27,6 +28,7 @@ import speiger.src.collections.bytes.functions.ByteConsumer; import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.bytes.functions.function.BytePredicate; +import speiger.src.collections.bytes.functions.OptionalByte; import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.sets.AbstractObjectSet; import speiger.src.collections.objects.sets.ObjectSet; @@ -895,7 +897,7 @@ public class Object2ByteOpenHashMap extends AbstractObject2ByteMap impleme } @Override - public Object2ByteMap.Entry reduce(ObjectObjectUnaryOperator, Object2ByteMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Object2ByteMap.Entry> operator) { Objects.requireNonNull(operator); Object2ByteMap.Entry state = null; boolean empty = true; @@ -912,25 +914,25 @@ public class Object2ByteOpenHashMap extends AbstractObject2ByteMap impleme } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Object2ByteMap.Entry 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(keys[i] != null) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1102,7 +1104,7 @@ public class Object2ByteOpenHashMap extends AbstractObject2ByteMap impleme } @Override - public T reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); T state = null; boolean empty = true; @@ -1119,18 +1121,18 @@ public class Object2ByteOpenHashMap extends AbstractObject2ByteMap impleme } state = operator.apply(state, keys[i]); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public T findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return Optional.empty(); + if(containsNull && filter.test(keys[nullIndex])) return Optional.ofNullable(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(keys[i] != null && filter.test(keys[i])) return keys[i]; + if(keys[i] != null && filter.test(keys[i])) return Optional.ofNullable(keys[i]); } - return null; + return Optional.empty(); } @Override @@ -1245,7 +1247,7 @@ public class Object2ByteOpenHashMap extends AbstractObject2ByteMap impleme } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -1262,18 +1264,18 @@ public class Object2ByteOpenHashMap extends AbstractObject2ByteMap impleme } state = operator.applyAsByte(state, values[i]); } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (byte)0; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return OptionalByte.empty(); + if(containsNull && filter.test(values[nullIndex])) return OptionalByte.of(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(keys[i] != null && filter.test(values[i])) return values[i]; + if(keys[i] != null && filter.test(values[i])) return OptionalByte.of(values[i]); } - return (byte)0; + return OptionalByte.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/objects/maps/impl/hash/Object2CharLinkedOpenHashMap.java b/src/main/java/speiger/src/collections/objects/maps/impl/hash/Object2CharLinkedOpenHashMap.java index 086be49..e1fd37c 100644 --- a/src/main/java/speiger/src/collections/objects/maps/impl/hash/Object2CharLinkedOpenHashMap.java +++ b/src/main/java/speiger/src/collections/objects/maps/impl/hash/Object2CharLinkedOpenHashMap.java @@ -4,6 +4,7 @@ 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; @@ -27,6 +28,7 @@ import speiger.src.collections.chars.lists.CharListIterator; import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.chars.functions.function.CharPredicate; +import speiger.src.collections.chars.functions.OptionalChar; import speiger.src.collections.utils.HashUtil; /** @@ -660,6 +662,10 @@ public class Object2CharLinkedOpenHashMap extends Object2CharOpenHashMap i } private class MapEntrySet extends AbstractObjectSet> implements Object2CharOrderedMap.FastOrderedSet { + @Override + public void addFirst(Object2CharMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Object2CharMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Object2CharMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -825,7 +831,7 @@ public class Object2CharLinkedOpenHashMap extends Object2CharOpenHashMap i } @Override - public Object2CharMap.Entry reduce(ObjectObjectUnaryOperator, Object2CharMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Object2CharMap.Entry> operator) { Objects.requireNonNull(operator); Object2CharMap.Entry state = null; boolean empty = true; @@ -840,21 +846,21 @@ public class Object2CharLinkedOpenHashMap extends Object2CharOpenHashMap i state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Object2CharMap.Entry 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 @@ -931,6 +937,12 @@ public class Object2CharLinkedOpenHashMap extends Object2CharOpenHashMap i @Override public boolean add(T o) { throw new UnsupportedOperationException(); } + + @Override + public void addFirst(T o) { throw new UnsupportedOperationException(); } + + @Override + public void addLast(T o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(T o) { throw new UnsupportedOperationException(); } @@ -1077,7 +1089,7 @@ public class Object2CharLinkedOpenHashMap extends Object2CharOpenHashMap i } @Override - public T reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); T state = null; boolean empty = true; @@ -1092,19 +1104,19 @@ public class Object2CharLinkedOpenHashMap extends Object2CharOpenHashMap i state = operator.apply(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public T findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + 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.ofNullable(keys[index]); index = (int)links[index]; } - return null; + return Optional.empty(); } @Override @@ -1240,7 +1252,7 @@ public class Object2CharLinkedOpenHashMap extends Object2CharOpenHashMap i } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -1255,19 +1267,19 @@ public class Object2CharLinkedOpenHashMap extends Object2CharOpenHashMap i state = operator.applyAsChar(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (char)0; + if(size() <= 0) return OptionalChar.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalChar.of(values[index]); index = (int)links[index]; } - return (char)0; + return OptionalChar.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/objects/maps/impl/hash/Object2CharOpenHashMap.java b/src/main/java/speiger/src/collections/objects/maps/impl/hash/Object2CharOpenHashMap.java index 6c921e4..5812ae8 100644 --- a/src/main/java/speiger/src/collections/objects/maps/impl/hash/Object2CharOpenHashMap.java +++ b/src/main/java/speiger/src/collections/objects/maps/impl/hash/Object2CharOpenHashMap.java @@ -5,6 +5,7 @@ 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; @@ -27,6 +28,7 @@ import speiger.src.collections.chars.functions.CharConsumer; import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.chars.functions.function.CharPredicate; +import speiger.src.collections.chars.functions.OptionalChar; import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.sets.AbstractObjectSet; import speiger.src.collections.objects.sets.ObjectSet; @@ -895,7 +897,7 @@ public class Object2CharOpenHashMap extends AbstractObject2CharMap impleme } @Override - public Object2CharMap.Entry reduce(ObjectObjectUnaryOperator, Object2CharMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Object2CharMap.Entry> operator) { Objects.requireNonNull(operator); Object2CharMap.Entry state = null; boolean empty = true; @@ -912,25 +914,25 @@ public class Object2CharOpenHashMap extends AbstractObject2CharMap impleme } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Object2CharMap.Entry 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(keys[i] != null) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1102,7 +1104,7 @@ public class Object2CharOpenHashMap extends AbstractObject2CharMap impleme } @Override - public T reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); T state = null; boolean empty = true; @@ -1119,18 +1121,18 @@ public class Object2CharOpenHashMap extends AbstractObject2CharMap impleme } state = operator.apply(state, keys[i]); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public T findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return Optional.empty(); + if(containsNull && filter.test(keys[nullIndex])) return Optional.ofNullable(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(keys[i] != null && filter.test(keys[i])) return keys[i]; + if(keys[i] != null && filter.test(keys[i])) return Optional.ofNullable(keys[i]); } - return null; + return Optional.empty(); } @Override @@ -1245,7 +1247,7 @@ public class Object2CharOpenHashMap extends AbstractObject2CharMap impleme } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -1262,18 +1264,18 @@ public class Object2CharOpenHashMap extends AbstractObject2CharMap impleme } state = operator.applyAsChar(state, values[i]); } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (char)0; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return OptionalChar.empty(); + if(containsNull && filter.test(values[nullIndex])) return OptionalChar.of(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(keys[i] != null && filter.test(values[i])) return values[i]; + if(keys[i] != null && filter.test(values[i])) return OptionalChar.of(values[i]); } - return (char)0; + return OptionalChar.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/objects/maps/impl/hash/Object2DoubleLinkedOpenHashMap.java b/src/main/java/speiger/src/collections/objects/maps/impl/hash/Object2DoubleLinkedOpenHashMap.java index fbc7e63..a6bae9a 100644 --- a/src/main/java/speiger/src/collections/objects/maps/impl/hash/Object2DoubleLinkedOpenHashMap.java +++ b/src/main/java/speiger/src/collections/objects/maps/impl/hash/Object2DoubleLinkedOpenHashMap.java @@ -4,10 +4,12 @@ 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; import java.util.function.DoublePredicate; +import java.util.OptionalDouble; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; @@ -660,6 +662,10 @@ public class Object2DoubleLinkedOpenHashMap extends Object2DoubleOpenHashMap< } private class MapEntrySet extends AbstractObjectSet> implements Object2DoubleOrderedMap.FastOrderedSet { + @Override + public void addFirst(Object2DoubleMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Object2DoubleMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Object2DoubleMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -825,7 +831,7 @@ public class Object2DoubleLinkedOpenHashMap extends Object2DoubleOpenHashMap< } @Override - public Object2DoubleMap.Entry reduce(ObjectObjectUnaryOperator, Object2DoubleMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Object2DoubleMap.Entry> operator) { Objects.requireNonNull(operator); Object2DoubleMap.Entry state = null; boolean empty = true; @@ -840,21 +846,21 @@ public class Object2DoubleLinkedOpenHashMap extends Object2DoubleOpenHashMap< state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Object2DoubleMap.Entry 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 @@ -931,6 +937,12 @@ public class Object2DoubleLinkedOpenHashMap extends Object2DoubleOpenHashMap< @Override public boolean add(T o) { throw new UnsupportedOperationException(); } + + @Override + public void addFirst(T o) { throw new UnsupportedOperationException(); } + + @Override + public void addLast(T o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(T o) { throw new UnsupportedOperationException(); } @@ -1077,7 +1089,7 @@ public class Object2DoubleLinkedOpenHashMap extends Object2DoubleOpenHashMap< } @Override - public T reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); T state = null; boolean empty = true; @@ -1092,19 +1104,19 @@ public class Object2DoubleLinkedOpenHashMap extends Object2DoubleOpenHashMap< state = operator.apply(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public T findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + 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.ofNullable(keys[index]); index = (int)links[index]; } - return null; + return Optional.empty(); } @Override @@ -1240,7 +1252,7 @@ public class Object2DoubleLinkedOpenHashMap extends Object2DoubleOpenHashMap< } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -1255,19 +1267,19 @@ public class Object2DoubleLinkedOpenHashMap extends Object2DoubleOpenHashMap< state = operator.applyAsDouble(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0D; + if(size() <= 0) return OptionalDouble.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalDouble.of(values[index]); index = (int)links[index]; } - return 0D; + return OptionalDouble.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/objects/maps/impl/hash/Object2DoubleOpenHashMap.java b/src/main/java/speiger/src/collections/objects/maps/impl/hash/Object2DoubleOpenHashMap.java index fde7098..1b60f2f 100644 --- a/src/main/java/speiger/src/collections/objects/maps/impl/hash/Object2DoubleOpenHashMap.java +++ b/src/main/java/speiger/src/collections/objects/maps/impl/hash/Object2DoubleOpenHashMap.java @@ -5,10 +5,12 @@ 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; import java.util.function.DoublePredicate; +import java.util.OptionalDouble; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntDoubleConsumer; @@ -895,7 +897,7 @@ public class Object2DoubleOpenHashMap extends AbstractObject2DoubleMap imp } @Override - public Object2DoubleMap.Entry reduce(ObjectObjectUnaryOperator, Object2DoubleMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Object2DoubleMap.Entry> operator) { Objects.requireNonNull(operator); Object2DoubleMap.Entry state = null; boolean empty = true; @@ -912,25 +914,25 @@ public class Object2DoubleOpenHashMap extends AbstractObject2DoubleMap imp } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Object2DoubleMap.Entry 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(keys[i] != null) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1102,7 +1104,7 @@ public class Object2DoubleOpenHashMap extends AbstractObject2DoubleMap imp } @Override - public T reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); T state = null; boolean empty = true; @@ -1119,18 +1121,18 @@ public class Object2DoubleOpenHashMap extends AbstractObject2DoubleMap imp } state = operator.apply(state, keys[i]); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public T findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return Optional.empty(); + if(containsNull && filter.test(keys[nullIndex])) return Optional.ofNullable(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(keys[i] != null && filter.test(keys[i])) return keys[i]; + if(keys[i] != null && filter.test(keys[i])) return Optional.ofNullable(keys[i]); } - return null; + return Optional.empty(); } @Override @@ -1245,7 +1247,7 @@ public class Object2DoubleOpenHashMap extends AbstractObject2DoubleMap imp } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -1262,18 +1264,18 @@ public class Object2DoubleOpenHashMap extends AbstractObject2DoubleMap imp } state = operator.applyAsDouble(state, values[i]); } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0D; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return OptionalDouble.empty(); + if(containsNull && filter.test(values[nullIndex])) return OptionalDouble.of(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(keys[i] != null && filter.test(values[i])) return values[i]; + if(keys[i] != null && filter.test(values[i])) return OptionalDouble.of(values[i]); } - return 0D; + return OptionalDouble.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/objects/maps/impl/hash/Object2FloatLinkedOpenHashMap.java b/src/main/java/speiger/src/collections/objects/maps/impl/hash/Object2FloatLinkedOpenHashMap.java index c64ffb6..7cca887 100644 --- a/src/main/java/speiger/src/collections/objects/maps/impl/hash/Object2FloatLinkedOpenHashMap.java +++ b/src/main/java/speiger/src/collections/objects/maps/impl/hash/Object2FloatLinkedOpenHashMap.java @@ -4,6 +4,7 @@ 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; @@ -27,6 +28,7 @@ import speiger.src.collections.floats.lists.FloatListIterator; import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.floats.functions.function.FloatPredicate; +import speiger.src.collections.floats.functions.OptionalFloat; import speiger.src.collections.utils.HashUtil; /** @@ -660,6 +662,10 @@ public class Object2FloatLinkedOpenHashMap extends Object2FloatOpenHashMap } private class MapEntrySet extends AbstractObjectSet> implements Object2FloatOrderedMap.FastOrderedSet { + @Override + public void addFirst(Object2FloatMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Object2FloatMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Object2FloatMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -825,7 +831,7 @@ public class Object2FloatLinkedOpenHashMap extends Object2FloatOpenHashMap } @Override - public Object2FloatMap.Entry reduce(ObjectObjectUnaryOperator, Object2FloatMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Object2FloatMap.Entry> operator) { Objects.requireNonNull(operator); Object2FloatMap.Entry state = null; boolean empty = true; @@ -840,21 +846,21 @@ public class Object2FloatLinkedOpenHashMap extends Object2FloatOpenHashMap state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Object2FloatMap.Entry 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 @@ -931,6 +937,12 @@ public class Object2FloatLinkedOpenHashMap extends Object2FloatOpenHashMap @Override public boolean add(T o) { throw new UnsupportedOperationException(); } + + @Override + public void addFirst(T o) { throw new UnsupportedOperationException(); } + + @Override + public void addLast(T o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(T o) { throw new UnsupportedOperationException(); } @@ -1077,7 +1089,7 @@ public class Object2FloatLinkedOpenHashMap extends Object2FloatOpenHashMap } @Override - public T reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); T state = null; boolean empty = true; @@ -1092,19 +1104,19 @@ public class Object2FloatLinkedOpenHashMap extends Object2FloatOpenHashMap state = operator.apply(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public T findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + 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.ofNullable(keys[index]); index = (int)links[index]; } - return null; + return Optional.empty(); } @Override @@ -1240,7 +1252,7 @@ public class Object2FloatLinkedOpenHashMap extends Object2FloatOpenHashMap } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -1255,19 +1267,19 @@ public class Object2FloatLinkedOpenHashMap extends Object2FloatOpenHashMap state = operator.applyAsFloat(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0F; + if(size() <= 0) return OptionalFloat.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalFloat.of(values[index]); index = (int)links[index]; } - return 0F; + return OptionalFloat.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/objects/maps/impl/hash/Object2FloatOpenHashMap.java b/src/main/java/speiger/src/collections/objects/maps/impl/hash/Object2FloatOpenHashMap.java index 0c9f39b..c0c221d 100644 --- a/src/main/java/speiger/src/collections/objects/maps/impl/hash/Object2FloatOpenHashMap.java +++ b/src/main/java/speiger/src/collections/objects/maps/impl/hash/Object2FloatOpenHashMap.java @@ -5,6 +5,7 @@ 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; @@ -27,6 +28,7 @@ import speiger.src.collections.floats.functions.FloatConsumer; import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.floats.functions.function.FloatPredicate; +import speiger.src.collections.floats.functions.OptionalFloat; import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.sets.AbstractObjectSet; import speiger.src.collections.objects.sets.ObjectSet; @@ -895,7 +897,7 @@ public class Object2FloatOpenHashMap extends AbstractObject2FloatMap imple } @Override - public Object2FloatMap.Entry reduce(ObjectObjectUnaryOperator, Object2FloatMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Object2FloatMap.Entry> operator) { Objects.requireNonNull(operator); Object2FloatMap.Entry state = null; boolean empty = true; @@ -912,25 +914,25 @@ public class Object2FloatOpenHashMap extends AbstractObject2FloatMap imple } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Object2FloatMap.Entry 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(keys[i] != null) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1102,7 +1104,7 @@ public class Object2FloatOpenHashMap extends AbstractObject2FloatMap imple } @Override - public T reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); T state = null; boolean empty = true; @@ -1119,18 +1121,18 @@ public class Object2FloatOpenHashMap extends AbstractObject2FloatMap imple } state = operator.apply(state, keys[i]); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public T findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return Optional.empty(); + if(containsNull && filter.test(keys[nullIndex])) return Optional.ofNullable(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(keys[i] != null && filter.test(keys[i])) return keys[i]; + if(keys[i] != null && filter.test(keys[i])) return Optional.ofNullable(keys[i]); } - return null; + return Optional.empty(); } @Override @@ -1245,7 +1247,7 @@ public class Object2FloatOpenHashMap extends AbstractObject2FloatMap imple } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -1262,18 +1264,18 @@ public class Object2FloatOpenHashMap extends AbstractObject2FloatMap imple } state = operator.applyAsFloat(state, values[i]); } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0F; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return OptionalFloat.empty(); + if(containsNull && filter.test(values[nullIndex])) return OptionalFloat.of(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(keys[i] != null && filter.test(values[i])) return values[i]; + if(keys[i] != null && filter.test(values[i])) return OptionalFloat.of(values[i]); } - return 0F; + return OptionalFloat.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/objects/maps/impl/hash/Object2IntLinkedOpenHashMap.java b/src/main/java/speiger/src/collections/objects/maps/impl/hash/Object2IntLinkedOpenHashMap.java index bd53273..0eebf38 100644 --- a/src/main/java/speiger/src/collections/objects/maps/impl/hash/Object2IntLinkedOpenHashMap.java +++ b/src/main/java/speiger/src/collections/objects/maps/impl/hash/Object2IntLinkedOpenHashMap.java @@ -4,10 +4,12 @@ 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; import java.util.function.IntPredicate; +import java.util.OptionalInt; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; @@ -660,6 +662,10 @@ public class Object2IntLinkedOpenHashMap extends Object2IntOpenHashMap imp } private class MapEntrySet extends AbstractObjectSet> implements Object2IntOrderedMap.FastOrderedSet { + @Override + public void addFirst(Object2IntMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Object2IntMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Object2IntMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -825,7 +831,7 @@ public class Object2IntLinkedOpenHashMap extends Object2IntOpenHashMap imp } @Override - public Object2IntMap.Entry reduce(ObjectObjectUnaryOperator, Object2IntMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Object2IntMap.Entry> operator) { Objects.requireNonNull(operator); Object2IntMap.Entry state = null; boolean empty = true; @@ -840,21 +846,21 @@ public class Object2IntLinkedOpenHashMap extends Object2IntOpenHashMap imp state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Object2IntMap.Entry 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 @@ -931,6 +937,12 @@ public class Object2IntLinkedOpenHashMap extends Object2IntOpenHashMap imp @Override public boolean add(T o) { throw new UnsupportedOperationException(); } + + @Override + public void addFirst(T o) { throw new UnsupportedOperationException(); } + + @Override + public void addLast(T o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(T o) { throw new UnsupportedOperationException(); } @@ -1077,7 +1089,7 @@ public class Object2IntLinkedOpenHashMap extends Object2IntOpenHashMap imp } @Override - public T reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); T state = null; boolean empty = true; @@ -1092,19 +1104,19 @@ public class Object2IntLinkedOpenHashMap extends Object2IntOpenHashMap imp state = operator.apply(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public T findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + 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.ofNullable(keys[index]); index = (int)links[index]; } - return null; + return Optional.empty(); } @Override @@ -1240,7 +1252,7 @@ public class Object2IntLinkedOpenHashMap extends Object2IntOpenHashMap imp } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -1255,19 +1267,19 @@ public class Object2IntLinkedOpenHashMap extends Object2IntOpenHashMap imp state = operator.applyAsInt(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0; + if(size() <= 0) return OptionalInt.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalInt.of(values[index]); index = (int)links[index]; } - return 0; + return OptionalInt.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/objects/maps/impl/hash/Object2IntOpenHashMap.java b/src/main/java/speiger/src/collections/objects/maps/impl/hash/Object2IntOpenHashMap.java index 6b580f3..9b67451 100644 --- a/src/main/java/speiger/src/collections/objects/maps/impl/hash/Object2IntOpenHashMap.java +++ b/src/main/java/speiger/src/collections/objects/maps/impl/hash/Object2IntOpenHashMap.java @@ -5,10 +5,12 @@ 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; import java.util.function.IntPredicate; +import java.util.OptionalInt; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntIntConsumer; @@ -895,7 +897,7 @@ public class Object2IntOpenHashMap extends AbstractObject2IntMap implement } @Override - public Object2IntMap.Entry reduce(ObjectObjectUnaryOperator, Object2IntMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Object2IntMap.Entry> operator) { Objects.requireNonNull(operator); Object2IntMap.Entry state = null; boolean empty = true; @@ -912,25 +914,25 @@ public class Object2IntOpenHashMap extends AbstractObject2IntMap implement } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Object2IntMap.Entry 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(keys[i] != null) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1102,7 +1104,7 @@ public class Object2IntOpenHashMap extends AbstractObject2IntMap implement } @Override - public T reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); T state = null; boolean empty = true; @@ -1119,18 +1121,18 @@ public class Object2IntOpenHashMap extends AbstractObject2IntMap implement } state = operator.apply(state, keys[i]); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public T findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return Optional.empty(); + if(containsNull && filter.test(keys[nullIndex])) return Optional.ofNullable(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(keys[i] != null && filter.test(keys[i])) return keys[i]; + if(keys[i] != null && filter.test(keys[i])) return Optional.ofNullable(keys[i]); } - return null; + return Optional.empty(); } @Override @@ -1245,7 +1247,7 @@ public class Object2IntOpenHashMap extends AbstractObject2IntMap implement } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -1262,18 +1264,18 @@ public class Object2IntOpenHashMap extends AbstractObject2IntMap implement } state = operator.applyAsInt(state, values[i]); } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return OptionalInt.empty(); + if(containsNull && filter.test(values[nullIndex])) return OptionalInt.of(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(keys[i] != null && filter.test(values[i])) return values[i]; + if(keys[i] != null && filter.test(values[i])) return OptionalInt.of(values[i]); } - return 0; + return OptionalInt.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/objects/maps/impl/hash/Object2LongLinkedOpenHashMap.java b/src/main/java/speiger/src/collections/objects/maps/impl/hash/Object2LongLinkedOpenHashMap.java index a220e20..47e3db3 100644 --- a/src/main/java/speiger/src/collections/objects/maps/impl/hash/Object2LongLinkedOpenHashMap.java +++ b/src/main/java/speiger/src/collections/objects/maps/impl/hash/Object2LongLinkedOpenHashMap.java @@ -4,10 +4,12 @@ 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; import java.util.function.LongPredicate; +import java.util.OptionalLong; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; @@ -660,6 +662,10 @@ public class Object2LongLinkedOpenHashMap extends Object2LongOpenHashMap i } private class MapEntrySet extends AbstractObjectSet> implements Object2LongOrderedMap.FastOrderedSet { + @Override + public void addFirst(Object2LongMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Object2LongMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Object2LongMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -825,7 +831,7 @@ public class Object2LongLinkedOpenHashMap extends Object2LongOpenHashMap i } @Override - public Object2LongMap.Entry reduce(ObjectObjectUnaryOperator, Object2LongMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Object2LongMap.Entry> operator) { Objects.requireNonNull(operator); Object2LongMap.Entry state = null; boolean empty = true; @@ -840,21 +846,21 @@ public class Object2LongLinkedOpenHashMap extends Object2LongOpenHashMap i state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Object2LongMap.Entry 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 @@ -931,6 +937,12 @@ public class Object2LongLinkedOpenHashMap extends Object2LongOpenHashMap i @Override public boolean add(T o) { throw new UnsupportedOperationException(); } + + @Override + public void addFirst(T o) { throw new UnsupportedOperationException(); } + + @Override + public void addLast(T o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(T o) { throw new UnsupportedOperationException(); } @@ -1077,7 +1089,7 @@ public class Object2LongLinkedOpenHashMap extends Object2LongOpenHashMap i } @Override - public T reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); T state = null; boolean empty = true; @@ -1092,19 +1104,19 @@ public class Object2LongLinkedOpenHashMap extends Object2LongOpenHashMap i state = operator.apply(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public T findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + 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.ofNullable(keys[index]); index = (int)links[index]; } - return null; + return Optional.empty(); } @Override @@ -1240,7 +1252,7 @@ public class Object2LongLinkedOpenHashMap extends Object2LongOpenHashMap i } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -1255,19 +1267,19 @@ public class Object2LongLinkedOpenHashMap extends Object2LongOpenHashMap i state = operator.applyAsLong(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0L; + if(size() <= 0) return OptionalLong.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalLong.of(values[index]); index = (int)links[index]; } - return 0L; + return OptionalLong.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/objects/maps/impl/hash/Object2LongOpenHashMap.java b/src/main/java/speiger/src/collections/objects/maps/impl/hash/Object2LongOpenHashMap.java index 22c1ba5..fa1ab2f 100644 --- a/src/main/java/speiger/src/collections/objects/maps/impl/hash/Object2LongOpenHashMap.java +++ b/src/main/java/speiger/src/collections/objects/maps/impl/hash/Object2LongOpenHashMap.java @@ -5,10 +5,12 @@ 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; import java.util.function.LongPredicate; +import java.util.OptionalLong; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntLongConsumer; @@ -895,7 +897,7 @@ public class Object2LongOpenHashMap extends AbstractObject2LongMap impleme } @Override - public Object2LongMap.Entry reduce(ObjectObjectUnaryOperator, Object2LongMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Object2LongMap.Entry> operator) { Objects.requireNonNull(operator); Object2LongMap.Entry state = null; boolean empty = true; @@ -912,25 +914,25 @@ public class Object2LongOpenHashMap extends AbstractObject2LongMap impleme } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Object2LongMap.Entry 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(keys[i] != null) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1102,7 +1104,7 @@ public class Object2LongOpenHashMap extends AbstractObject2LongMap impleme } @Override - public T reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); T state = null; boolean empty = true; @@ -1119,18 +1121,18 @@ public class Object2LongOpenHashMap extends AbstractObject2LongMap impleme } state = operator.apply(state, keys[i]); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public T findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return Optional.empty(); + if(containsNull && filter.test(keys[nullIndex])) return Optional.ofNullable(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(keys[i] != null && filter.test(keys[i])) return keys[i]; + if(keys[i] != null && filter.test(keys[i])) return Optional.ofNullable(keys[i]); } - return null; + return Optional.empty(); } @Override @@ -1245,7 +1247,7 @@ public class Object2LongOpenHashMap extends AbstractObject2LongMap impleme } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -1262,18 +1264,18 @@ public class Object2LongOpenHashMap extends AbstractObject2LongMap impleme } state = operator.applyAsLong(state, values[i]); } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0L; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return OptionalLong.empty(); + if(containsNull && filter.test(values[nullIndex])) return OptionalLong.of(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(keys[i] != null && filter.test(values[i])) return values[i]; + if(keys[i] != null && filter.test(values[i])) return OptionalLong.of(values[i]); } - return 0L; + return OptionalLong.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/objects/maps/impl/hash/Object2ObjectLinkedOpenHashMap.java b/src/main/java/speiger/src/collections/objects/maps/impl/hash/Object2ObjectLinkedOpenHashMap.java index cab9ae3..5324ca9 100644 --- a/src/main/java/speiger/src/collections/objects/maps/impl/hash/Object2ObjectLinkedOpenHashMap.java +++ b/src/main/java/speiger/src/collections/objects/maps/impl/hash/Object2ObjectLinkedOpenHashMap.java @@ -4,6 +4,7 @@ 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; @@ -619,6 +620,10 @@ public class Object2ObjectLinkedOpenHashMap extends Object2ObjectOpenHashM } private class MapEntrySet extends AbstractObjectSet> implements Object2ObjectOrderedMap.FastOrderedSet { + @Override + public void addFirst(Object2ObjectMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Object2ObjectMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Object2ObjectMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -784,7 +789,7 @@ public class Object2ObjectLinkedOpenHashMap extends Object2ObjectOpenHashM } @Override - public Object2ObjectMap.Entry reduce(ObjectObjectUnaryOperator, Object2ObjectMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Object2ObjectMap.Entry> operator) { Objects.requireNonNull(operator); Object2ObjectMap.Entry state = null; boolean empty = true; @@ -799,21 +804,21 @@ public class Object2ObjectLinkedOpenHashMap extends Object2ObjectOpenHashM state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Object2ObjectMap.Entry 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 @@ -890,6 +895,12 @@ public class Object2ObjectLinkedOpenHashMap extends Object2ObjectOpenHashM @Override public boolean add(T o) { throw new UnsupportedOperationException(); } + + @Override + public void addFirst(T o) { throw new UnsupportedOperationException(); } + + @Override + public void addLast(T o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(T o) { throw new UnsupportedOperationException(); } @@ -1036,7 +1047,7 @@ public class Object2ObjectLinkedOpenHashMap extends Object2ObjectOpenHashM } @Override - public T reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); T state = null; boolean empty = true; @@ -1051,19 +1062,19 @@ public class Object2ObjectLinkedOpenHashMap extends Object2ObjectOpenHashM state = operator.apply(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public T findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + 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.ofNullable(keys[index]); index = (int)links[index]; } - return null; + return Optional.empty(); } @Override @@ -1200,7 +1211,7 @@ public class Object2ObjectLinkedOpenHashMap extends Object2ObjectOpenHashM } @Override - public V reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); V state = null; boolean empty = true; @@ -1215,19 +1226,19 @@ public class Object2ObjectLinkedOpenHashMap extends Object2ObjectOpenHashM state = operator.apply(state, values[index]); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public V findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return Optional.ofNullable(values[index]); index = (int)links[index]; } - return null; + return Optional.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/objects/maps/impl/hash/Object2ObjectOpenHashMap.java b/src/main/java/speiger/src/collections/objects/maps/impl/hash/Object2ObjectOpenHashMap.java index b0a03cd..a9df9bf 100644 --- a/src/main/java/speiger/src/collections/objects/maps/impl/hash/Object2ObjectOpenHashMap.java +++ b/src/main/java/speiger/src/collections/objects/maps/impl/hash/Object2ObjectOpenHashMap.java @@ -5,6 +5,7 @@ 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; @@ -759,7 +760,7 @@ public class Object2ObjectOpenHashMap extends AbstractObject2ObjectMap reduce(ObjectObjectUnaryOperator, Object2ObjectMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Object2ObjectMap.Entry> operator) { Objects.requireNonNull(operator); Object2ObjectMap.Entry state = null; boolean empty = true; @@ -776,25 +777,25 @@ public class Object2ObjectOpenHashMap extends AbstractObject2ObjectMap 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(keys[i] != null) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -966,7 +967,7 @@ public class Object2ObjectOpenHashMap extends AbstractObject2ObjectMap operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); T state = null; boolean empty = true; @@ -983,18 +984,18 @@ public class Object2ObjectOpenHashMap extends AbstractObject2ObjectMap filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return Optional.empty(); + if(containsNull && filter.test(keys[nullIndex])) return Optional.ofNullable(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(keys[i] != null && filter.test(keys[i])) return keys[i]; + if(keys[i] != null && filter.test(keys[i])) return Optional.ofNullable(keys[i]); } - return null; + return Optional.empty(); } @Override @@ -1109,7 +1110,7 @@ public class Object2ObjectOpenHashMap extends AbstractObject2ObjectMap operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); V state = null; boolean empty = true; @@ -1126,18 +1127,18 @@ public class Object2ObjectOpenHashMap extends AbstractObject2ObjectMap filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return Optional.empty(); + if(containsNull && filter.test(values[nullIndex])) return Optional.ofNullable(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(keys[i] != null && filter.test(values[i])) return values[i]; + if(keys[i] != null && filter.test(values[i])) return Optional.ofNullable(values[i]); } - return null; + return Optional.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/objects/maps/impl/hash/Object2ShortLinkedOpenHashMap.java b/src/main/java/speiger/src/collections/objects/maps/impl/hash/Object2ShortLinkedOpenHashMap.java index 5512fd1..8a6ac42 100644 --- a/src/main/java/speiger/src/collections/objects/maps/impl/hash/Object2ShortLinkedOpenHashMap.java +++ b/src/main/java/speiger/src/collections/objects/maps/impl/hash/Object2ShortLinkedOpenHashMap.java @@ -4,6 +4,7 @@ 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; @@ -27,6 +28,7 @@ import speiger.src.collections.shorts.lists.ShortListIterator; import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.shorts.functions.function.ShortPredicate; +import speiger.src.collections.shorts.functions.OptionalShort; import speiger.src.collections.utils.HashUtil; /** @@ -660,6 +662,10 @@ public class Object2ShortLinkedOpenHashMap extends Object2ShortOpenHashMap } private class MapEntrySet extends AbstractObjectSet> implements Object2ShortOrderedMap.FastOrderedSet { + @Override + public void addFirst(Object2ShortMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Object2ShortMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Object2ShortMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -825,7 +831,7 @@ public class Object2ShortLinkedOpenHashMap extends Object2ShortOpenHashMap } @Override - public Object2ShortMap.Entry reduce(ObjectObjectUnaryOperator, Object2ShortMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Object2ShortMap.Entry> operator) { Objects.requireNonNull(operator); Object2ShortMap.Entry state = null; boolean empty = true; @@ -840,21 +846,21 @@ public class Object2ShortLinkedOpenHashMap extends Object2ShortOpenHashMap state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Object2ShortMap.Entry 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 @@ -931,6 +937,12 @@ public class Object2ShortLinkedOpenHashMap extends Object2ShortOpenHashMap @Override public boolean add(T o) { throw new UnsupportedOperationException(); } + + @Override + public void addFirst(T o) { throw new UnsupportedOperationException(); } + + @Override + public void addLast(T o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(T o) { throw new UnsupportedOperationException(); } @@ -1077,7 +1089,7 @@ public class Object2ShortLinkedOpenHashMap extends Object2ShortOpenHashMap } @Override - public T reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); T state = null; boolean empty = true; @@ -1092,19 +1104,19 @@ public class Object2ShortLinkedOpenHashMap extends Object2ShortOpenHashMap state = operator.apply(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public T findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + 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.ofNullable(keys[index]); index = (int)links[index]; } - return null; + return Optional.empty(); } @Override @@ -1240,7 +1252,7 @@ public class Object2ShortLinkedOpenHashMap extends Object2ShortOpenHashMap } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -1255,19 +1267,19 @@ public class Object2ShortLinkedOpenHashMap extends Object2ShortOpenHashMap state = operator.applyAsShort(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (short)0; + if(size() <= 0) return OptionalShort.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalShort.of(values[index]); index = (int)links[index]; } - return (short)0; + return OptionalShort.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/objects/maps/impl/hash/Object2ShortOpenHashMap.java b/src/main/java/speiger/src/collections/objects/maps/impl/hash/Object2ShortOpenHashMap.java index 9e94053..71bb9f4 100644 --- a/src/main/java/speiger/src/collections/objects/maps/impl/hash/Object2ShortOpenHashMap.java +++ b/src/main/java/speiger/src/collections/objects/maps/impl/hash/Object2ShortOpenHashMap.java @@ -5,6 +5,7 @@ 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; @@ -27,6 +28,7 @@ import speiger.src.collections.shorts.functions.ShortConsumer; import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.shorts.functions.function.ShortPredicate; +import speiger.src.collections.shorts.functions.OptionalShort; import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.sets.AbstractObjectSet; import speiger.src.collections.objects.sets.ObjectSet; @@ -895,7 +897,7 @@ public class Object2ShortOpenHashMap extends AbstractObject2ShortMap imple } @Override - public Object2ShortMap.Entry reduce(ObjectObjectUnaryOperator, Object2ShortMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Object2ShortMap.Entry> operator) { Objects.requireNonNull(operator); Object2ShortMap.Entry state = null; boolean empty = true; @@ -912,25 +914,25 @@ public class Object2ShortOpenHashMap extends AbstractObject2ShortMap imple } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Object2ShortMap.Entry 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(keys[i] != null) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1102,7 +1104,7 @@ public class Object2ShortOpenHashMap extends AbstractObject2ShortMap imple } @Override - public T reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); T state = null; boolean empty = true; @@ -1119,18 +1121,18 @@ public class Object2ShortOpenHashMap extends AbstractObject2ShortMap imple } state = operator.apply(state, keys[i]); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public T findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return Optional.empty(); + if(containsNull && filter.test(keys[nullIndex])) return Optional.ofNullable(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(keys[i] != null && filter.test(keys[i])) return keys[i]; + if(keys[i] != null && filter.test(keys[i])) return Optional.ofNullable(keys[i]); } - return null; + return Optional.empty(); } @Override @@ -1245,7 +1247,7 @@ public class Object2ShortOpenHashMap extends AbstractObject2ShortMap imple } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -1262,18 +1264,18 @@ public class Object2ShortOpenHashMap extends AbstractObject2ShortMap imple } state = operator.applyAsShort(state, values[i]); } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (short)0; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return OptionalShort.empty(); + if(containsNull && filter.test(values[nullIndex])) return OptionalShort.of(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(keys[i] != null && filter.test(values[i])) return values[i]; + if(keys[i] != null && filter.test(values[i])) return OptionalShort.of(values[i]); } - return (short)0; + return OptionalShort.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/objects/maps/impl/immutable/ImmutableObject2BooleanOpenHashMap.java b/src/main/java/speiger/src/collections/objects/maps/impl/immutable/ImmutableObject2BooleanOpenHashMap.java index 2fb2cb9..c8acee7 100644 --- a/src/main/java/speiger/src/collections/objects/maps/impl/immutable/ImmutableObject2BooleanOpenHashMap.java +++ b/src/main/java/speiger/src/collections/objects/maps/impl/immutable/ImmutableObject2BooleanOpenHashMap.java @@ -4,6 +4,7 @@ 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; @@ -20,6 +21,7 @@ import speiger.src.collections.objects.maps.interfaces.Object2BooleanMap; import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.booleans.functions.function.BooleanPredicate; +import speiger.src.collections.booleans.functions.OptionalBoolean; import speiger.src.collections.booleans.collections.AbstractBooleanCollection; import speiger.src.collections.booleans.collections.BooleanOrderedCollection; import speiger.src.collections.booleans.collections.BooleanIterator; @@ -503,6 +505,10 @@ public class ImmutableObject2BooleanOpenHashMap extends AbstractObject2Boolea } private class MapEntrySet extends AbstractObjectSet> implements Object2BooleanOrderedMap.FastOrderedSet { + @Override + public void addFirst(Object2BooleanMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Object2BooleanMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Object2BooleanMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -656,7 +662,7 @@ public class ImmutableObject2BooleanOpenHashMap extends AbstractObject2Boolea } @Override - public Object2BooleanMap.Entry reduce(ObjectObjectUnaryOperator, Object2BooleanMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Object2BooleanMap.Entry> operator) { Objects.requireNonNull(operator); Object2BooleanMap.Entry state = null; boolean empty = true; @@ -671,21 +677,21 @@ public class ImmutableObject2BooleanOpenHashMap extends AbstractObject2Boolea state = operator.apply(state, new BasicEntry<>(keys[index], values[index])); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Object2BooleanMap.Entry findFirst(Predicate> filter) { + public Optional> findFirst(Predicate> filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry entry = new BasicEntry<>(); 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 @@ -749,6 +755,12 @@ public class ImmutableObject2BooleanOpenHashMap extends AbstractObject2Boolea throw new UnsupportedOperationException(); } + @Override + public void addFirst(T o) { throw new UnsupportedOperationException(); } + + @Override + public void addLast(T o) { throw new UnsupportedOperationException(); } + @Override public boolean addAndMoveToFirst(T o) { throw new UnsupportedOperationException(); } @@ -884,7 +896,7 @@ public class ImmutableObject2BooleanOpenHashMap extends AbstractObject2Boolea } @Override - public T reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); T state = null; boolean empty = true; @@ -899,19 +911,19 @@ public class ImmutableObject2BooleanOpenHashMap extends AbstractObject2Boolea state = operator.apply(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public T findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + 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.ofNullable(keys[index]); index = (int)links[index]; } - return null; + return Optional.empty(); } @Override @@ -1038,7 +1050,7 @@ public class ImmutableObject2BooleanOpenHashMap extends AbstractObject2Boolea } @Override - public boolean reduce(BooleanBooleanUnaryOperator operator) { + public OptionalBoolean reduce(BooleanBooleanUnaryOperator operator) { Objects.requireNonNull(operator); boolean state = false; boolean empty = true; @@ -1053,19 +1065,19 @@ public class ImmutableObject2BooleanOpenHashMap extends AbstractObject2Boolea state = operator.applyAsBoolean(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalBoolean.empty() : OptionalBoolean.of(state); } @Override - public boolean findFirst(BooleanPredicate filter) { + public OptionalBoolean findFirst(BooleanPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return false; + if(size() <= 0) return OptionalBoolean.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalBoolean.of(values[index]); index = (int)links[index]; } - return false; + return OptionalBoolean.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/objects/maps/impl/immutable/ImmutableObject2ByteOpenHashMap.java b/src/main/java/speiger/src/collections/objects/maps/impl/immutable/ImmutableObject2ByteOpenHashMap.java index abf0d99..214843f 100644 --- a/src/main/java/speiger/src/collections/objects/maps/impl/immutable/ImmutableObject2ByteOpenHashMap.java +++ b/src/main/java/speiger/src/collections/objects/maps/impl/immutable/ImmutableObject2ByteOpenHashMap.java @@ -4,6 +4,7 @@ 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; @@ -21,6 +22,7 @@ import speiger.src.collections.objects.maps.interfaces.Object2ByteMap; import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.bytes.functions.function.BytePredicate; +import speiger.src.collections.bytes.functions.OptionalByte; import speiger.src.collections.bytes.collections.AbstractByteCollection; import speiger.src.collections.bytes.collections.ByteOrderedCollection; import speiger.src.collections.bytes.collections.ByteIterator; @@ -508,6 +510,10 @@ public class ImmutableObject2ByteOpenHashMap extends AbstractObject2ByteMap> implements Object2ByteOrderedMap.FastOrderedSet { + @Override + public void addFirst(Object2ByteMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Object2ByteMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Object2ByteMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -661,7 +667,7 @@ public class ImmutableObject2ByteOpenHashMap extends AbstractObject2ByteMap reduce(ObjectObjectUnaryOperator, Object2ByteMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Object2ByteMap.Entry> operator) { Objects.requireNonNull(operator); Object2ByteMap.Entry state = null; boolean empty = true; @@ -676,21 +682,21 @@ public class ImmutableObject2ByteOpenHashMap extends AbstractObject2ByteMap(keys[index], values[index])); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Object2ByteMap.Entry findFirst(Predicate> filter) { + public Optional> findFirst(Predicate> filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry entry = new BasicEntry<>(); 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 @@ -754,6 +760,12 @@ public class ImmutableObject2ByteOpenHashMap extends AbstractObject2ByteMap extends AbstractObject2ByteMap operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); T state = null; boolean empty = true; @@ -904,19 +916,19 @@ public class ImmutableObject2ByteOpenHashMap extends AbstractObject2ByteMap filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + 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.ofNullable(keys[index]); index = (int)links[index]; } - return null; + return Optional.empty(); } @Override @@ -1043,7 +1055,7 @@ public class ImmutableObject2ByteOpenHashMap extends AbstractObject2ByteMap extends AbstractObject2ByteMap extends AbstractObject2CharMap> implements Object2CharOrderedMap.FastOrderedSet { + @Override + public void addFirst(Object2CharMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Object2CharMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Object2CharMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -661,7 +667,7 @@ public class ImmutableObject2CharOpenHashMap extends AbstractObject2CharMap reduce(ObjectObjectUnaryOperator, Object2CharMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Object2CharMap.Entry> operator) { Objects.requireNonNull(operator); Object2CharMap.Entry state = null; boolean empty = true; @@ -676,21 +682,21 @@ public class ImmutableObject2CharOpenHashMap extends AbstractObject2CharMap(keys[index], values[index])); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Object2CharMap.Entry findFirst(Predicate> filter) { + public Optional> findFirst(Predicate> filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry entry = new BasicEntry<>(); 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 @@ -754,6 +760,12 @@ public class ImmutableObject2CharOpenHashMap extends AbstractObject2CharMap extends AbstractObject2CharMap operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); T state = null; boolean empty = true; @@ -904,19 +916,19 @@ public class ImmutableObject2CharOpenHashMap extends AbstractObject2CharMap filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + 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.ofNullable(keys[index]); index = (int)links[index]; } - return null; + return Optional.empty(); } @Override @@ -1043,7 +1055,7 @@ public class ImmutableObject2CharOpenHashMap extends AbstractObject2CharMap extends AbstractObject2CharMap extends AbstractObject2DoubleM } private class MapEntrySet extends AbstractObjectSet> implements Object2DoubleOrderedMap.FastOrderedSet { + @Override + public void addFirst(Object2DoubleMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Object2DoubleMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Object2DoubleMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -661,7 +667,7 @@ public class ImmutableObject2DoubleOpenHashMap extends AbstractObject2DoubleM } @Override - public Object2DoubleMap.Entry reduce(ObjectObjectUnaryOperator, Object2DoubleMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Object2DoubleMap.Entry> operator) { Objects.requireNonNull(operator); Object2DoubleMap.Entry state = null; boolean empty = true; @@ -676,21 +682,21 @@ public class ImmutableObject2DoubleOpenHashMap extends AbstractObject2DoubleM state = operator.apply(state, new BasicEntry<>(keys[index], values[index])); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Object2DoubleMap.Entry findFirst(Predicate> filter) { + public Optional> findFirst(Predicate> filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry entry = new BasicEntry<>(); 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 @@ -754,6 +760,12 @@ public class ImmutableObject2DoubleOpenHashMap extends AbstractObject2DoubleM throw new UnsupportedOperationException(); } + @Override + public void addFirst(T o) { throw new UnsupportedOperationException(); } + + @Override + public void addLast(T o) { throw new UnsupportedOperationException(); } + @Override public boolean addAndMoveToFirst(T o) { throw new UnsupportedOperationException(); } @@ -889,7 +901,7 @@ public class ImmutableObject2DoubleOpenHashMap extends AbstractObject2DoubleM } @Override - public T reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); T state = null; boolean empty = true; @@ -904,19 +916,19 @@ public class ImmutableObject2DoubleOpenHashMap extends AbstractObject2DoubleM state = operator.apply(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public T findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + 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.ofNullable(keys[index]); index = (int)links[index]; } - return null; + return Optional.empty(); } @Override @@ -1043,7 +1055,7 @@ public class ImmutableObject2DoubleOpenHashMap extends AbstractObject2DoubleM } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -1058,19 +1070,19 @@ public class ImmutableObject2DoubleOpenHashMap extends AbstractObject2DoubleM state = operator.applyAsDouble(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0D; + if(size() <= 0) return OptionalDouble.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalDouble.of(values[index]); index = (int)links[index]; } - return 0D; + return OptionalDouble.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/objects/maps/impl/immutable/ImmutableObject2FloatOpenHashMap.java b/src/main/java/speiger/src/collections/objects/maps/impl/immutable/ImmutableObject2FloatOpenHashMap.java index ffe7a8b..d60303e 100644 --- a/src/main/java/speiger/src/collections/objects/maps/impl/immutable/ImmutableObject2FloatOpenHashMap.java +++ b/src/main/java/speiger/src/collections/objects/maps/impl/immutable/ImmutableObject2FloatOpenHashMap.java @@ -4,6 +4,7 @@ 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; @@ -21,6 +22,7 @@ import speiger.src.collections.objects.maps.interfaces.Object2FloatMap; import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.floats.functions.function.FloatPredicate; +import speiger.src.collections.floats.functions.OptionalFloat; import speiger.src.collections.floats.collections.AbstractFloatCollection; import speiger.src.collections.floats.collections.FloatOrderedCollection; import speiger.src.collections.floats.collections.FloatIterator; @@ -508,6 +510,10 @@ public class ImmutableObject2FloatOpenHashMap extends AbstractObject2FloatMap } private class MapEntrySet extends AbstractObjectSet> implements Object2FloatOrderedMap.FastOrderedSet { + @Override + public void addFirst(Object2FloatMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Object2FloatMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Object2FloatMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -661,7 +667,7 @@ public class ImmutableObject2FloatOpenHashMap extends AbstractObject2FloatMap } @Override - public Object2FloatMap.Entry reduce(ObjectObjectUnaryOperator, Object2FloatMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Object2FloatMap.Entry> operator) { Objects.requireNonNull(operator); Object2FloatMap.Entry state = null; boolean empty = true; @@ -676,21 +682,21 @@ public class ImmutableObject2FloatOpenHashMap extends AbstractObject2FloatMap state = operator.apply(state, new BasicEntry<>(keys[index], values[index])); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Object2FloatMap.Entry findFirst(Predicate> filter) { + public Optional> findFirst(Predicate> filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry entry = new BasicEntry<>(); 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 @@ -754,6 +760,12 @@ public class ImmutableObject2FloatOpenHashMap extends AbstractObject2FloatMap throw new UnsupportedOperationException(); } + @Override + public void addFirst(T o) { throw new UnsupportedOperationException(); } + + @Override + public void addLast(T o) { throw new UnsupportedOperationException(); } + @Override public boolean addAndMoveToFirst(T o) { throw new UnsupportedOperationException(); } @@ -889,7 +901,7 @@ public class ImmutableObject2FloatOpenHashMap extends AbstractObject2FloatMap } @Override - public T reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); T state = null; boolean empty = true; @@ -904,19 +916,19 @@ public class ImmutableObject2FloatOpenHashMap extends AbstractObject2FloatMap state = operator.apply(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public T findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + 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.ofNullable(keys[index]); index = (int)links[index]; } - return null; + return Optional.empty(); } @Override @@ -1043,7 +1055,7 @@ public class ImmutableObject2FloatOpenHashMap extends AbstractObject2FloatMap } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -1058,19 +1070,19 @@ public class ImmutableObject2FloatOpenHashMap extends AbstractObject2FloatMap state = operator.applyAsFloat(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0F; + if(size() <= 0) return OptionalFloat.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalFloat.of(values[index]); index = (int)links[index]; } - return 0F; + return OptionalFloat.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/objects/maps/impl/immutable/ImmutableObject2IntOpenHashMap.java b/src/main/java/speiger/src/collections/objects/maps/impl/immutable/ImmutableObject2IntOpenHashMap.java index feff8fd..373be26 100644 --- a/src/main/java/speiger/src/collections/objects/maps/impl/immutable/ImmutableObject2IntOpenHashMap.java +++ b/src/main/java/speiger/src/collections/objects/maps/impl/immutable/ImmutableObject2IntOpenHashMap.java @@ -4,10 +4,12 @@ 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; import java.util.function.IntPredicate; +import java.util.OptionalInt; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntIntConsumer; @@ -508,6 +510,10 @@ public class ImmutableObject2IntOpenHashMap extends AbstractObject2IntMap } private class MapEntrySet extends AbstractObjectSet> implements Object2IntOrderedMap.FastOrderedSet { + @Override + public void addFirst(Object2IntMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Object2IntMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Object2IntMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -661,7 +667,7 @@ public class ImmutableObject2IntOpenHashMap extends AbstractObject2IntMap } @Override - public Object2IntMap.Entry reduce(ObjectObjectUnaryOperator, Object2IntMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Object2IntMap.Entry> operator) { Objects.requireNonNull(operator); Object2IntMap.Entry state = null; boolean empty = true; @@ -676,21 +682,21 @@ public class ImmutableObject2IntOpenHashMap extends AbstractObject2IntMap state = operator.apply(state, new BasicEntry<>(keys[index], values[index])); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Object2IntMap.Entry findFirst(Predicate> filter) { + public Optional> findFirst(Predicate> filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry entry = new BasicEntry<>(); 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 @@ -754,6 +760,12 @@ public class ImmutableObject2IntOpenHashMap extends AbstractObject2IntMap throw new UnsupportedOperationException(); } + @Override + public void addFirst(T o) { throw new UnsupportedOperationException(); } + + @Override + public void addLast(T o) { throw new UnsupportedOperationException(); } + @Override public boolean addAndMoveToFirst(T o) { throw new UnsupportedOperationException(); } @@ -889,7 +901,7 @@ public class ImmutableObject2IntOpenHashMap extends AbstractObject2IntMap } @Override - public T reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); T state = null; boolean empty = true; @@ -904,19 +916,19 @@ public class ImmutableObject2IntOpenHashMap extends AbstractObject2IntMap state = operator.apply(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public T findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + 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.ofNullable(keys[index]); index = (int)links[index]; } - return null; + return Optional.empty(); } @Override @@ -1043,7 +1055,7 @@ public class ImmutableObject2IntOpenHashMap extends AbstractObject2IntMap } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -1058,19 +1070,19 @@ public class ImmutableObject2IntOpenHashMap extends AbstractObject2IntMap state = operator.applyAsInt(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0; + if(size() <= 0) return OptionalInt.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalInt.of(values[index]); index = (int)links[index]; } - return 0; + return OptionalInt.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/objects/maps/impl/immutable/ImmutableObject2LongOpenHashMap.java b/src/main/java/speiger/src/collections/objects/maps/impl/immutable/ImmutableObject2LongOpenHashMap.java index f9697b4..09827e6 100644 --- a/src/main/java/speiger/src/collections/objects/maps/impl/immutable/ImmutableObject2LongOpenHashMap.java +++ b/src/main/java/speiger/src/collections/objects/maps/impl/immutable/ImmutableObject2LongOpenHashMap.java @@ -4,10 +4,12 @@ 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; import java.util.function.LongPredicate; +import java.util.OptionalLong; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntLongConsumer; @@ -508,6 +510,10 @@ public class ImmutableObject2LongOpenHashMap extends AbstractObject2LongMap> implements Object2LongOrderedMap.FastOrderedSet { + @Override + public void addFirst(Object2LongMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Object2LongMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Object2LongMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -661,7 +667,7 @@ public class ImmutableObject2LongOpenHashMap extends AbstractObject2LongMap reduce(ObjectObjectUnaryOperator, Object2LongMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Object2LongMap.Entry> operator) { Objects.requireNonNull(operator); Object2LongMap.Entry state = null; boolean empty = true; @@ -676,21 +682,21 @@ public class ImmutableObject2LongOpenHashMap extends AbstractObject2LongMap(keys[index], values[index])); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Object2LongMap.Entry findFirst(Predicate> filter) { + public Optional> findFirst(Predicate> filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry entry = new BasicEntry<>(); 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 @@ -754,6 +760,12 @@ public class ImmutableObject2LongOpenHashMap extends AbstractObject2LongMap extends AbstractObject2LongMap operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); T state = null; boolean empty = true; @@ -904,19 +916,19 @@ public class ImmutableObject2LongOpenHashMap extends AbstractObject2LongMap filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + 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.ofNullable(keys[index]); index = (int)links[index]; } - return null; + return Optional.empty(); } @Override @@ -1043,7 +1055,7 @@ public class ImmutableObject2LongOpenHashMap extends AbstractObject2LongMap extends AbstractObject2LongMap extends AbstractObject2Obje } private class MapEntrySet extends AbstractObjectSet> implements Object2ObjectOrderedMap.FastOrderedSet { + @Override + public void addFirst(Object2ObjectMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Object2ObjectMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Object2ObjectMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -604,7 +609,7 @@ public class ImmutableObject2ObjectOpenHashMap extends AbstractObject2Obje } @Override - public Object2ObjectMap.Entry reduce(ObjectObjectUnaryOperator, Object2ObjectMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Object2ObjectMap.Entry> operator) { Objects.requireNonNull(operator); Object2ObjectMap.Entry state = null; boolean empty = true; @@ -619,21 +624,21 @@ public class ImmutableObject2ObjectOpenHashMap extends AbstractObject2Obje state = operator.apply(state, new BasicEntry<>(keys[index], values[index])); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Object2ObjectMap.Entry findFirst(Predicate> filter) { + public Optional> findFirst(Predicate> filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry entry = new BasicEntry<>(); 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 @@ -697,6 +702,12 @@ public class ImmutableObject2ObjectOpenHashMap extends AbstractObject2Obje throw new UnsupportedOperationException(); } + @Override + public void addFirst(T o) { throw new UnsupportedOperationException(); } + + @Override + public void addLast(T o) { throw new UnsupportedOperationException(); } + @Override public boolean addAndMoveToFirst(T o) { throw new UnsupportedOperationException(); } @@ -832,7 +843,7 @@ public class ImmutableObject2ObjectOpenHashMap extends AbstractObject2Obje } @Override - public T reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); T state = null; boolean empty = true; @@ -847,19 +858,19 @@ public class ImmutableObject2ObjectOpenHashMap extends AbstractObject2Obje state = operator.apply(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public T findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + 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.ofNullable(keys[index]); index = (int)links[index]; } - return null; + return Optional.empty(); } @Override @@ -987,7 +998,7 @@ public class ImmutableObject2ObjectOpenHashMap extends AbstractObject2Obje } @Override - public V reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); V state = null; boolean empty = true; @@ -1002,19 +1013,19 @@ public class ImmutableObject2ObjectOpenHashMap extends AbstractObject2Obje state = operator.apply(state, values[index]); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public V findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return Optional.ofNullable(values[index]); index = (int)links[index]; } - return null; + return Optional.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/objects/maps/impl/immutable/ImmutableObject2ShortOpenHashMap.java b/src/main/java/speiger/src/collections/objects/maps/impl/immutable/ImmutableObject2ShortOpenHashMap.java index 10cc1cc..a4ab19b 100644 --- a/src/main/java/speiger/src/collections/objects/maps/impl/immutable/ImmutableObject2ShortOpenHashMap.java +++ b/src/main/java/speiger/src/collections/objects/maps/impl/immutable/ImmutableObject2ShortOpenHashMap.java @@ -4,6 +4,7 @@ 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; @@ -21,6 +22,7 @@ import speiger.src.collections.objects.maps.interfaces.Object2ShortMap; import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.shorts.functions.function.ShortPredicate; +import speiger.src.collections.shorts.functions.OptionalShort; import speiger.src.collections.shorts.collections.AbstractShortCollection; import speiger.src.collections.shorts.collections.ShortOrderedCollection; import speiger.src.collections.shorts.collections.ShortIterator; @@ -508,6 +510,10 @@ public class ImmutableObject2ShortOpenHashMap extends AbstractObject2ShortMap } private class MapEntrySet extends AbstractObjectSet> implements Object2ShortOrderedMap.FastOrderedSet { + @Override + public void addFirst(Object2ShortMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Object2ShortMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Object2ShortMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -661,7 +667,7 @@ public class ImmutableObject2ShortOpenHashMap extends AbstractObject2ShortMap } @Override - public Object2ShortMap.Entry reduce(ObjectObjectUnaryOperator, Object2ShortMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Object2ShortMap.Entry> operator) { Objects.requireNonNull(operator); Object2ShortMap.Entry state = null; boolean empty = true; @@ -676,21 +682,21 @@ public class ImmutableObject2ShortOpenHashMap extends AbstractObject2ShortMap state = operator.apply(state, new BasicEntry<>(keys[index], values[index])); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Object2ShortMap.Entry findFirst(Predicate> filter) { + public Optional> findFirst(Predicate> filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry entry = new BasicEntry<>(); 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 @@ -754,6 +760,12 @@ public class ImmutableObject2ShortOpenHashMap extends AbstractObject2ShortMap throw new UnsupportedOperationException(); } + @Override + public void addFirst(T o) { throw new UnsupportedOperationException(); } + + @Override + public void addLast(T o) { throw new UnsupportedOperationException(); } + @Override public boolean addAndMoveToFirst(T o) { throw new UnsupportedOperationException(); } @@ -889,7 +901,7 @@ public class ImmutableObject2ShortOpenHashMap extends AbstractObject2ShortMap } @Override - public T reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); T state = null; boolean empty = true; @@ -904,19 +916,19 @@ public class ImmutableObject2ShortOpenHashMap extends AbstractObject2ShortMap state = operator.apply(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public T findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + 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.ofNullable(keys[index]); index = (int)links[index]; } - return null; + return Optional.empty(); } @Override @@ -1043,7 +1055,7 @@ public class ImmutableObject2ShortOpenHashMap extends AbstractObject2ShortMap } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -1058,19 +1070,19 @@ public class ImmutableObject2ShortOpenHashMap extends AbstractObject2ShortMap state = operator.applyAsShort(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (short)0; + if(size() <= 0) return OptionalShort.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalShort.of(values[index]); index = (int)links[index]; } - return (short)0; + return OptionalShort.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/objects/maps/impl/misc/LinkedEnum2BooleanMap.java b/src/main/java/speiger/src/collections/objects/maps/impl/misc/LinkedEnum2BooleanMap.java index 5198aeb..b72d904 100644 --- a/src/main/java/speiger/src/collections/objects/maps/impl/misc/LinkedEnum2BooleanMap.java +++ b/src/main/java/speiger/src/collections/objects/maps/impl/misc/LinkedEnum2BooleanMap.java @@ -458,6 +458,10 @@ public class LinkedEnum2BooleanMap> extends Enum2BooleanMap } private class MapEntrySet extends AbstractObjectSet> implements Object2BooleanOrderedMap.FastOrderedSet { + @Override + public void addFirst(Object2BooleanMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Object2BooleanMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Object2BooleanMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -607,7 +611,10 @@ public class LinkedEnum2BooleanMap> extends Enum2BooleanMap public boolean add(T o) { throw new UnsupportedOperationException(); } - + @Override + public void addFirst(T o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(T o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(T o) { throw new UnsupportedOperationException(); } diff --git a/src/main/java/speiger/src/collections/objects/maps/impl/misc/LinkedEnum2ByteMap.java b/src/main/java/speiger/src/collections/objects/maps/impl/misc/LinkedEnum2ByteMap.java index e6a865a..181d543 100644 --- a/src/main/java/speiger/src/collections/objects/maps/impl/misc/LinkedEnum2ByteMap.java +++ b/src/main/java/speiger/src/collections/objects/maps/impl/misc/LinkedEnum2ByteMap.java @@ -458,6 +458,10 @@ public class LinkedEnum2ByteMap> extends Enum2ByteMap imple } private class MapEntrySet extends AbstractObjectSet> implements Object2ByteOrderedMap.FastOrderedSet { + @Override + public void addFirst(Object2ByteMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Object2ByteMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Object2ByteMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -607,7 +611,10 @@ public class LinkedEnum2ByteMap> extends Enum2ByteMap imple public boolean add(T o) { throw new UnsupportedOperationException(); } - + @Override + public void addFirst(T o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(T o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(T o) { throw new UnsupportedOperationException(); } diff --git a/src/main/java/speiger/src/collections/objects/maps/impl/misc/LinkedEnum2CharMap.java b/src/main/java/speiger/src/collections/objects/maps/impl/misc/LinkedEnum2CharMap.java index dcae37e..2a53887 100644 --- a/src/main/java/speiger/src/collections/objects/maps/impl/misc/LinkedEnum2CharMap.java +++ b/src/main/java/speiger/src/collections/objects/maps/impl/misc/LinkedEnum2CharMap.java @@ -458,6 +458,10 @@ public class LinkedEnum2CharMap> extends Enum2CharMap imple } private class MapEntrySet extends AbstractObjectSet> implements Object2CharOrderedMap.FastOrderedSet { + @Override + public void addFirst(Object2CharMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Object2CharMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Object2CharMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -607,7 +611,10 @@ public class LinkedEnum2CharMap> extends Enum2CharMap imple public boolean add(T o) { throw new UnsupportedOperationException(); } - + @Override + public void addFirst(T o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(T o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(T o) { throw new UnsupportedOperationException(); } diff --git a/src/main/java/speiger/src/collections/objects/maps/impl/misc/LinkedEnum2DoubleMap.java b/src/main/java/speiger/src/collections/objects/maps/impl/misc/LinkedEnum2DoubleMap.java index 8811b18..28f524c 100644 --- a/src/main/java/speiger/src/collections/objects/maps/impl/misc/LinkedEnum2DoubleMap.java +++ b/src/main/java/speiger/src/collections/objects/maps/impl/misc/LinkedEnum2DoubleMap.java @@ -458,6 +458,10 @@ public class LinkedEnum2DoubleMap> extends Enum2DoubleMap i } private class MapEntrySet extends AbstractObjectSet> implements Object2DoubleOrderedMap.FastOrderedSet { + @Override + public void addFirst(Object2DoubleMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Object2DoubleMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Object2DoubleMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -607,7 +611,10 @@ public class LinkedEnum2DoubleMap> extends Enum2DoubleMap i public boolean add(T o) { throw new UnsupportedOperationException(); } - + @Override + public void addFirst(T o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(T o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(T o) { throw new UnsupportedOperationException(); } diff --git a/src/main/java/speiger/src/collections/objects/maps/impl/misc/LinkedEnum2FloatMap.java b/src/main/java/speiger/src/collections/objects/maps/impl/misc/LinkedEnum2FloatMap.java index 803655a..801b032 100644 --- a/src/main/java/speiger/src/collections/objects/maps/impl/misc/LinkedEnum2FloatMap.java +++ b/src/main/java/speiger/src/collections/objects/maps/impl/misc/LinkedEnum2FloatMap.java @@ -458,6 +458,10 @@ public class LinkedEnum2FloatMap> extends Enum2FloatMap imp } private class MapEntrySet extends AbstractObjectSet> implements Object2FloatOrderedMap.FastOrderedSet { + @Override + public void addFirst(Object2FloatMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Object2FloatMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Object2FloatMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -607,7 +611,10 @@ public class LinkedEnum2FloatMap> extends Enum2FloatMap imp public boolean add(T o) { throw new UnsupportedOperationException(); } - + @Override + public void addFirst(T o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(T o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(T o) { throw new UnsupportedOperationException(); } diff --git a/src/main/java/speiger/src/collections/objects/maps/impl/misc/LinkedEnum2IntMap.java b/src/main/java/speiger/src/collections/objects/maps/impl/misc/LinkedEnum2IntMap.java index 0d2447c..c1d993c 100644 --- a/src/main/java/speiger/src/collections/objects/maps/impl/misc/LinkedEnum2IntMap.java +++ b/src/main/java/speiger/src/collections/objects/maps/impl/misc/LinkedEnum2IntMap.java @@ -458,6 +458,10 @@ public class LinkedEnum2IntMap> extends Enum2IntMap impleme } private class MapEntrySet extends AbstractObjectSet> implements Object2IntOrderedMap.FastOrderedSet { + @Override + public void addFirst(Object2IntMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Object2IntMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Object2IntMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -607,7 +611,10 @@ public class LinkedEnum2IntMap> extends Enum2IntMap impleme public boolean add(T o) { throw new UnsupportedOperationException(); } - + @Override + public void addFirst(T o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(T o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(T o) { throw new UnsupportedOperationException(); } diff --git a/src/main/java/speiger/src/collections/objects/maps/impl/misc/LinkedEnum2LongMap.java b/src/main/java/speiger/src/collections/objects/maps/impl/misc/LinkedEnum2LongMap.java index fee536c..7f82fe5 100644 --- a/src/main/java/speiger/src/collections/objects/maps/impl/misc/LinkedEnum2LongMap.java +++ b/src/main/java/speiger/src/collections/objects/maps/impl/misc/LinkedEnum2LongMap.java @@ -458,6 +458,10 @@ public class LinkedEnum2LongMap> extends Enum2LongMap imple } private class MapEntrySet extends AbstractObjectSet> implements Object2LongOrderedMap.FastOrderedSet { + @Override + public void addFirst(Object2LongMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Object2LongMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Object2LongMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -607,7 +611,10 @@ public class LinkedEnum2LongMap> extends Enum2LongMap imple public boolean add(T o) { throw new UnsupportedOperationException(); } - + @Override + public void addFirst(T o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(T o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(T o) { throw new UnsupportedOperationException(); } diff --git a/src/main/java/speiger/src/collections/objects/maps/impl/misc/LinkedEnum2ObjectMap.java b/src/main/java/speiger/src/collections/objects/maps/impl/misc/LinkedEnum2ObjectMap.java index be46e75..3143102 100644 --- a/src/main/java/speiger/src/collections/objects/maps/impl/misc/LinkedEnum2ObjectMap.java +++ b/src/main/java/speiger/src/collections/objects/maps/impl/misc/LinkedEnum2ObjectMap.java @@ -440,6 +440,10 @@ public class LinkedEnum2ObjectMap, V> extends Enum2ObjectMap> implements Object2ObjectOrderedMap.FastOrderedSet { + @Override + public void addFirst(Object2ObjectMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Object2ObjectMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Object2ObjectMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -589,7 +593,10 @@ public class LinkedEnum2ObjectMap, V> extends Enum2ObjectMap> extends Enum2ShortMap imp } private class MapEntrySet extends AbstractObjectSet> implements Object2ShortOrderedMap.FastOrderedSet { + @Override + public void addFirst(Object2ShortMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Object2ShortMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Object2ShortMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -607,7 +611,10 @@ public class LinkedEnum2ShortMap> extends Enum2ShortMap imp public boolean add(T o) { throw new UnsupportedOperationException(); } - + @Override + public void addFirst(T o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(T o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(T o) { throw new UnsupportedOperationException(); } diff --git a/src/main/java/speiger/src/collections/objects/maps/impl/misc/Object2BooleanArrayMap.java b/src/main/java/speiger/src/collections/objects/maps/impl/misc/Object2BooleanArrayMap.java index 23aceff..10dee6d 100644 --- a/src/main/java/speiger/src/collections/objects/maps/impl/misc/Object2BooleanArrayMap.java +++ b/src/main/java/speiger/src/collections/objects/maps/impl/misc/Object2BooleanArrayMap.java @@ -4,6 +4,7 @@ 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; @@ -30,6 +31,7 @@ import speiger.src.collections.booleans.lists.BooleanListIterator; import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.booleans.functions.function.BooleanPredicate; +import speiger.src.collections.booleans.functions.OptionalBoolean; import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.utils.HashUtil; @@ -709,6 +711,10 @@ public class Object2BooleanArrayMap extends AbstractObject2BooleanMap impl } private class MapEntrySet extends AbstractObjectSet> implements Object2BooleanOrderedMap.FastOrderedSet { + @Override + public void addFirst(Object2BooleanMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Object2BooleanMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Object2BooleanMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -857,7 +863,7 @@ public class Object2BooleanArrayMap extends AbstractObject2BooleanMap impl } @Override - public Object2BooleanMap.Entry reduce(ObjectObjectUnaryOperator, Object2BooleanMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Object2BooleanMap.Entry> operator) { Objects.requireNonNull(operator); Object2BooleanMap.Entry state = null; boolean empty = true; @@ -869,19 +875,19 @@ public class Object2BooleanArrayMap extends AbstractObject2BooleanMap impl } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Object2BooleanMap.Entry 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 extends AbstractObject2BooleanMap impl @Override public boolean add(T o) { throw new UnsupportedOperationException(); } @Override + public void addFirst(T o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(T o) { throw new UnsupportedOperationException(); } + @Override public boolean addAndMoveToFirst(T o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToLast(T o) { throw new UnsupportedOperationException(); } @@ -1040,7 +1050,7 @@ public class Object2BooleanArrayMap extends AbstractObject2BooleanMap impl } @Override - public T reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); T state = null; boolean empty = true; @@ -1052,16 +1062,16 @@ public class Object2BooleanArrayMap extends AbstractObject2BooleanMap impl } state = operator.apply(state, keys[i]); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public T findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); for(int i = 0;i extends AbstractObject2BooleanMap impl } @Override - public boolean reduce(BooleanBooleanUnaryOperator operator) { + public OptionalBoolean reduce(BooleanBooleanUnaryOperator operator) { Objects.requireNonNull(operator); boolean state = false; boolean empty = true; @@ -1174,16 +1184,16 @@ public class Object2BooleanArrayMap extends AbstractObject2BooleanMap impl } state = operator.applyAsBoolean(state, values[i]); } - return state; + return empty ? OptionalBoolean.empty() : OptionalBoolean.of(state); } @Override - public boolean findFirst(BooleanPredicate filter) { + public OptionalBoolean findFirst(BooleanPredicate filter) { Objects.requireNonNull(filter); for(int i = 0;i extends AbstractObject2ByteMap implements } private class MapEntrySet extends AbstractObjectSet> implements Object2ByteOrderedMap.FastOrderedSet { + @Override + public void addFirst(Object2ByteMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Object2ByteMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Object2ByteMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -880,7 +886,7 @@ public class Object2ByteArrayMap extends AbstractObject2ByteMap implements } @Override - public Object2ByteMap.Entry reduce(ObjectObjectUnaryOperator, Object2ByteMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Object2ByteMap.Entry> operator) { Objects.requireNonNull(operator); Object2ByteMap.Entry state = null; boolean empty = true; @@ -892,19 +898,19 @@ public class Object2ByteArrayMap extends AbstractObject2ByteMap implements } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Object2ByteMap.Entry 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 extends AbstractObject2ByteMap implements @Override public boolean add(T o) { throw new UnsupportedOperationException(); } @Override + public void addFirst(T o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(T o) { throw new UnsupportedOperationException(); } + @Override public boolean addAndMoveToFirst(T o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToLast(T o) { throw new UnsupportedOperationException(); } @@ -1063,7 +1073,7 @@ public class Object2ByteArrayMap extends AbstractObject2ByteMap implements } @Override - public T reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); T state = null; boolean empty = true; @@ -1075,16 +1085,16 @@ public class Object2ByteArrayMap extends AbstractObject2ByteMap implements } state = operator.apply(state, keys[i]); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public T findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); for(int i = 0;i extends AbstractObject2ByteMap implements } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -1197,16 +1207,16 @@ public class Object2ByteArrayMap extends AbstractObject2ByteMap implements } state = operator.applyAsByte(state, values[i]); } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); for(int i = 0;i extends AbstractObject2CharMap implements } private class MapEntrySet extends AbstractObjectSet> implements Object2CharOrderedMap.FastOrderedSet { + @Override + public void addFirst(Object2CharMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Object2CharMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Object2CharMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -880,7 +886,7 @@ public class Object2CharArrayMap extends AbstractObject2CharMap implements } @Override - public Object2CharMap.Entry reduce(ObjectObjectUnaryOperator, Object2CharMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Object2CharMap.Entry> operator) { Objects.requireNonNull(operator); Object2CharMap.Entry state = null; boolean empty = true; @@ -892,19 +898,19 @@ public class Object2CharArrayMap extends AbstractObject2CharMap implements } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Object2CharMap.Entry 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 extends AbstractObject2CharMap implements @Override public boolean add(T o) { throw new UnsupportedOperationException(); } @Override + public void addFirst(T o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(T o) { throw new UnsupportedOperationException(); } + @Override public boolean addAndMoveToFirst(T o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToLast(T o) { throw new UnsupportedOperationException(); } @@ -1063,7 +1073,7 @@ public class Object2CharArrayMap extends AbstractObject2CharMap implements } @Override - public T reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); T state = null; boolean empty = true; @@ -1075,16 +1085,16 @@ public class Object2CharArrayMap extends AbstractObject2CharMap implements } state = operator.apply(state, keys[i]); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public T findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); for(int i = 0;i extends AbstractObject2CharMap implements } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -1197,16 +1207,16 @@ public class Object2CharArrayMap extends AbstractObject2CharMap implements } state = operator.applyAsChar(state, values[i]); } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); for(int i = 0;i extends AbstractObject2DoubleMap implem } private class MapEntrySet extends AbstractObjectSet> implements Object2DoubleOrderedMap.FastOrderedSet { + @Override + public void addFirst(Object2DoubleMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Object2DoubleMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Object2DoubleMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -880,7 +886,7 @@ public class Object2DoubleArrayMap extends AbstractObject2DoubleMap implem } @Override - public Object2DoubleMap.Entry reduce(ObjectObjectUnaryOperator, Object2DoubleMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Object2DoubleMap.Entry> operator) { Objects.requireNonNull(operator); Object2DoubleMap.Entry state = null; boolean empty = true; @@ -892,19 +898,19 @@ public class Object2DoubleArrayMap extends AbstractObject2DoubleMap implem } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Object2DoubleMap.Entry 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 extends AbstractObject2DoubleMap implem @Override public boolean add(T o) { throw new UnsupportedOperationException(); } @Override + public void addFirst(T o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(T o) { throw new UnsupportedOperationException(); } + @Override public boolean addAndMoveToFirst(T o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToLast(T o) { throw new UnsupportedOperationException(); } @@ -1063,7 +1073,7 @@ public class Object2DoubleArrayMap extends AbstractObject2DoubleMap implem } @Override - public T reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); T state = null; boolean empty = true; @@ -1075,16 +1085,16 @@ public class Object2DoubleArrayMap extends AbstractObject2DoubleMap implem } state = operator.apply(state, keys[i]); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public T findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); for(int i = 0;i extends AbstractObject2DoubleMap implem } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -1197,16 +1207,16 @@ public class Object2DoubleArrayMap extends AbstractObject2DoubleMap implem } state = operator.applyAsDouble(state, values[i]); } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); for(int i = 0;i extends AbstractObject2FloatMap implemen } private class MapEntrySet extends AbstractObjectSet> implements Object2FloatOrderedMap.FastOrderedSet { + @Override + public void addFirst(Object2FloatMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Object2FloatMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Object2FloatMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -880,7 +886,7 @@ public class Object2FloatArrayMap extends AbstractObject2FloatMap implemen } @Override - public Object2FloatMap.Entry reduce(ObjectObjectUnaryOperator, Object2FloatMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Object2FloatMap.Entry> operator) { Objects.requireNonNull(operator); Object2FloatMap.Entry state = null; boolean empty = true; @@ -892,19 +898,19 @@ public class Object2FloatArrayMap extends AbstractObject2FloatMap implemen } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Object2FloatMap.Entry 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 extends AbstractObject2FloatMap implemen @Override public boolean add(T o) { throw new UnsupportedOperationException(); } @Override + public void addFirst(T o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(T o) { throw new UnsupportedOperationException(); } + @Override public boolean addAndMoveToFirst(T o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToLast(T o) { throw new UnsupportedOperationException(); } @@ -1063,7 +1073,7 @@ public class Object2FloatArrayMap extends AbstractObject2FloatMap implemen } @Override - public T reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); T state = null; boolean empty = true; @@ -1075,16 +1085,16 @@ public class Object2FloatArrayMap extends AbstractObject2FloatMap implemen } state = operator.apply(state, keys[i]); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public T findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); for(int i = 0;i extends AbstractObject2FloatMap implemen } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -1197,16 +1207,16 @@ public class Object2FloatArrayMap extends AbstractObject2FloatMap implemen } state = operator.applyAsFloat(state, values[i]); } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); for(int i = 0;i extends AbstractObject2IntMap implements O } private class MapEntrySet extends AbstractObjectSet> implements Object2IntOrderedMap.FastOrderedSet { + @Override + public void addFirst(Object2IntMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Object2IntMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Object2IntMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -880,7 +886,7 @@ public class Object2IntArrayMap extends AbstractObject2IntMap implements O } @Override - public Object2IntMap.Entry reduce(ObjectObjectUnaryOperator, Object2IntMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Object2IntMap.Entry> operator) { Objects.requireNonNull(operator); Object2IntMap.Entry state = null; boolean empty = true; @@ -892,19 +898,19 @@ public class Object2IntArrayMap extends AbstractObject2IntMap implements O } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Object2IntMap.Entry 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 extends AbstractObject2IntMap implements O @Override public boolean add(T o) { throw new UnsupportedOperationException(); } @Override + public void addFirst(T o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(T o) { throw new UnsupportedOperationException(); } + @Override public boolean addAndMoveToFirst(T o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToLast(T o) { throw new UnsupportedOperationException(); } @@ -1063,7 +1073,7 @@ public class Object2IntArrayMap extends AbstractObject2IntMap implements O } @Override - public T reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); T state = null; boolean empty = true; @@ -1075,16 +1085,16 @@ public class Object2IntArrayMap extends AbstractObject2IntMap implements O } state = operator.apply(state, keys[i]); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public T findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); for(int i = 0;i extends AbstractObject2IntMap implements O } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -1197,16 +1207,16 @@ public class Object2IntArrayMap extends AbstractObject2IntMap implements O } state = operator.applyAsInt(state, values[i]); } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); for(int i = 0;i extends AbstractObject2LongMap implements } private class MapEntrySet extends AbstractObjectSet> implements Object2LongOrderedMap.FastOrderedSet { + @Override + public void addFirst(Object2LongMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Object2LongMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Object2LongMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -880,7 +886,7 @@ public class Object2LongArrayMap extends AbstractObject2LongMap implements } @Override - public Object2LongMap.Entry reduce(ObjectObjectUnaryOperator, Object2LongMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Object2LongMap.Entry> operator) { Objects.requireNonNull(operator); Object2LongMap.Entry state = null; boolean empty = true; @@ -892,19 +898,19 @@ public class Object2LongArrayMap extends AbstractObject2LongMap implements } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Object2LongMap.Entry 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 extends AbstractObject2LongMap implements @Override public boolean add(T o) { throw new UnsupportedOperationException(); } @Override + public void addFirst(T o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(T o) { throw new UnsupportedOperationException(); } + @Override public boolean addAndMoveToFirst(T o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToLast(T o) { throw new UnsupportedOperationException(); } @@ -1063,7 +1073,7 @@ public class Object2LongArrayMap extends AbstractObject2LongMap implements } @Override - public T reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); T state = null; boolean empty = true; @@ -1075,16 +1085,16 @@ public class Object2LongArrayMap extends AbstractObject2LongMap implements } state = operator.apply(state, keys[i]); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public T findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); for(int i = 0;i extends AbstractObject2LongMap implements } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -1197,16 +1207,16 @@ public class Object2LongArrayMap extends AbstractObject2LongMap implements } state = operator.applyAsLong(state, values[i]); } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); for(int i = 0;i extends AbstractObject2ObjectMap } private class MapEntrySet extends AbstractObjectSet> implements Object2ObjectOrderedMap.FastOrderedSet { + @Override + public void addFirst(Object2ObjectMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Object2ObjectMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Object2ObjectMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -753,7 +758,7 @@ public class Object2ObjectArrayMap extends AbstractObject2ObjectMap } @Override - public Object2ObjectMap.Entry reduce(ObjectObjectUnaryOperator, Object2ObjectMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Object2ObjectMap.Entry> operator) { Objects.requireNonNull(operator); Object2ObjectMap.Entry state = null; boolean empty = true; @@ -765,19 +770,19 @@ public class Object2ObjectArrayMap extends AbstractObject2ObjectMap } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Object2ObjectMap.Entry 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 extends AbstractObject2ObjectMap @Override public boolean add(T o) { throw new UnsupportedOperationException(); } @Override + public void addFirst(T o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(T o) { throw new UnsupportedOperationException(); } + @Override public boolean addAndMoveToFirst(T o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToLast(T o) { throw new UnsupportedOperationException(); } @@ -936,7 +945,7 @@ public class Object2ObjectArrayMap extends AbstractObject2ObjectMap } @Override - public T reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); T state = null; boolean empty = true; @@ -948,16 +957,16 @@ public class Object2ObjectArrayMap extends AbstractObject2ObjectMap } state = operator.apply(state, keys[i]); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public T findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); for(int i = 0;i extends AbstractObject2ObjectMap } @Override - public V reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); V state = null; boolean empty = true; @@ -1070,16 +1079,16 @@ public class Object2ObjectArrayMap extends AbstractObject2ObjectMap } state = operator.apply(state, values[i]); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public V findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); for(int i = 0;i extends AbstractObject2ShortMap implemen } private class MapEntrySet extends AbstractObjectSet> implements Object2ShortOrderedMap.FastOrderedSet { + @Override + public void addFirst(Object2ShortMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Object2ShortMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Object2ShortMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -880,7 +886,7 @@ public class Object2ShortArrayMap extends AbstractObject2ShortMap implemen } @Override - public Object2ShortMap.Entry reduce(ObjectObjectUnaryOperator, Object2ShortMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Object2ShortMap.Entry> operator) { Objects.requireNonNull(operator); Object2ShortMap.Entry state = null; boolean empty = true; @@ -892,19 +898,19 @@ public class Object2ShortArrayMap extends AbstractObject2ShortMap implemen } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Object2ShortMap.Entry 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 extends AbstractObject2ShortMap implemen @Override public boolean add(T o) { throw new UnsupportedOperationException(); } @Override + public void addFirst(T o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(T o) { throw new UnsupportedOperationException(); } + @Override public boolean addAndMoveToFirst(T o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToLast(T o) { throw new UnsupportedOperationException(); } @@ -1063,7 +1073,7 @@ public class Object2ShortArrayMap extends AbstractObject2ShortMap implemen } @Override - public T reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); T state = null; boolean empty = true; @@ -1075,16 +1085,16 @@ public class Object2ShortArrayMap extends AbstractObject2ShortMap implemen } state = operator.apply(state, keys[i]); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public T findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); for(int i = 0;i extends AbstractObject2ShortMap implemen } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -1197,16 +1207,16 @@ public class Object2ShortArrayMap extends AbstractObject2ShortMap implemen } state = operator.applyAsShort(state, values[i]); } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); for(int i = 0;i extends AbstractObject2BooleanMap im } @Override - public T reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); T state = null; boolean empty = true; @@ -1050,15 +1052,15 @@ public class Object2BooleanAVLTreeMap extends AbstractObject2BooleanMap im } state = operator.apply(state, entry.key); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public T findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); for(Node entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.test(entry.key)) return entry.key; - return null; + if(filter.test(entry.key)) return Optional.ofNullable(entry.key); + return Optional.empty(); } @Override @@ -1675,7 +1677,7 @@ public class Object2BooleanAVLTreeMap extends AbstractObject2BooleanMap im } @Override - public Object2BooleanMap.Entry reduce(ObjectObjectUnaryOperator, Object2BooleanMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Object2BooleanMap.Entry> operator) { Objects.requireNonNull(operator); Object2BooleanMap.Entry state = null; boolean empty = true; @@ -1687,19 +1689,19 @@ public class Object2BooleanAVLTreeMap extends AbstractObject2BooleanMap im } state = operator.apply(state, new BasicEntry<>(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Object2BooleanMap.Entry findFirst(Predicate> filter) { + public Optional> findFirst(Predicate> filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry<>(); for(Node 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 @@ -1794,7 +1796,7 @@ public class Object2BooleanAVLTreeMap extends AbstractObject2BooleanMap im } @Override - public boolean reduce(BooleanBooleanUnaryOperator operator) { + public OptionalBoolean reduce(BooleanBooleanUnaryOperator operator) { Objects.requireNonNull(operator); boolean state = false; boolean empty = true; @@ -1806,15 +1808,15 @@ public class Object2BooleanAVLTreeMap extends AbstractObject2BooleanMap im } state = operator.applyAsBoolean(state, entry.value); } - return state; + return empty ? OptionalBoolean.empty() : OptionalBoolean.of(state); } @Override - public boolean findFirst(BooleanPredicate filter) { + public OptionalBoolean findFirst(BooleanPredicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return false; + if(filter.test(entry.value)) return OptionalBoolean.of(entry.value); + return OptionalBoolean.empty(); } @Override @@ -2130,7 +2132,7 @@ public class Object2BooleanAVLTreeMap extends AbstractObject2BooleanMap im } @Override - public boolean reduce(BooleanBooleanUnaryOperator operator) { + public OptionalBoolean reduce(BooleanBooleanUnaryOperator operator) { Objects.requireNonNull(operator); boolean state = false; boolean empty = true; @@ -2142,15 +2144,15 @@ public class Object2BooleanAVLTreeMap extends AbstractObject2BooleanMap im } state = operator.applyAsBoolean(state, entry.value); } - return state; + return empty ? OptionalBoolean.empty() : OptionalBoolean.of(state); } @Override - public boolean findFirst(BooleanPredicate filter) { + public OptionalBoolean findFirst(BooleanPredicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return false; + if(filter.test(entry.value)) return OptionalBoolean.of(entry.value); + return OptionalBoolean.empty(); } @Override @@ -2291,7 +2293,7 @@ public class Object2BooleanAVLTreeMap extends AbstractObject2BooleanMap im } @Override - public Object2BooleanMap.Entry reduce(ObjectObjectUnaryOperator, Object2BooleanMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Object2BooleanMap.Entry> operator) { Objects.requireNonNull(operator); Object2BooleanMap.Entry state = null; boolean empty = true; @@ -2303,19 +2305,19 @@ public class Object2BooleanAVLTreeMap extends AbstractObject2BooleanMap im } state = operator.apply(state, new BasicEntry<>(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Object2BooleanMap.Entry findFirst(Predicate> filter) { + public Optional> findFirst(Predicate> filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry<>(); for(Node 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/main/java/speiger/src/collections/objects/maps/impl/tree/Object2BooleanRBTreeMap.java b/src/main/java/speiger/src/collections/objects/maps/impl/tree/Object2BooleanRBTreeMap.java index 5cb91c1..72bf250 100644 --- a/src/main/java/speiger/src/collections/objects/maps/impl/tree/Object2BooleanRBTreeMap.java +++ b/src/main/java/speiger/src/collections/objects/maps/impl/tree/Object2BooleanRBTreeMap.java @@ -4,6 +4,7 @@ import java.util.Collections; import java.util.Map; import java.util.Comparator; import java.util.Objects; +import java.util.Optional; import java.util.NoSuchElementException; import java.util.function.Consumer; import java.util.function.BiFunction; @@ -31,6 +32,7 @@ import speiger.src.collections.booleans.functions.BooleanConsumer; import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.booleans.functions.function.BooleanPredicate; +import speiger.src.collections.booleans.functions.OptionalBoolean; import speiger.src.collections.objects.collections.ObjectIterator; /** @@ -1091,7 +1093,7 @@ public class Object2BooleanRBTreeMap extends AbstractObject2BooleanMap imp } @Override - public T reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); T state = null; boolean empty = true; @@ -1103,15 +1105,15 @@ public class Object2BooleanRBTreeMap extends AbstractObject2BooleanMap imp } state = operator.apply(state, entry.key); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public T findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); for(Node entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.test(entry.key)) return entry.key; - return null; + if(filter.test(entry.key)) return Optional.ofNullable(entry.key); + return Optional.empty(); } @Override @@ -1738,7 +1740,7 @@ public class Object2BooleanRBTreeMap extends AbstractObject2BooleanMap imp } @Override - public Object2BooleanMap.Entry reduce(ObjectObjectUnaryOperator, Object2BooleanMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Object2BooleanMap.Entry> operator) { Objects.requireNonNull(operator); Object2BooleanMap.Entry state = null; boolean empty = true; @@ -1750,19 +1752,19 @@ public class Object2BooleanRBTreeMap extends AbstractObject2BooleanMap imp } state = operator.apply(state, new BasicEntry<>(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Object2BooleanMap.Entry findFirst(Predicate> filter) { + public Optional> findFirst(Predicate> filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry<>(); for(Node 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 @@ -1857,7 +1859,7 @@ public class Object2BooleanRBTreeMap extends AbstractObject2BooleanMap imp } @Override - public boolean reduce(BooleanBooleanUnaryOperator operator) { + public OptionalBoolean reduce(BooleanBooleanUnaryOperator operator) { Objects.requireNonNull(operator); boolean state = false; boolean empty = true; @@ -1869,15 +1871,15 @@ public class Object2BooleanRBTreeMap extends AbstractObject2BooleanMap imp } state = operator.applyAsBoolean(state, entry.value); } - return state; + return empty ? OptionalBoolean.empty() : OptionalBoolean.of(state); } @Override - public boolean findFirst(BooleanPredicate filter) { + public OptionalBoolean findFirst(BooleanPredicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return false; + if(filter.test(entry.value)) return OptionalBoolean.of(entry.value); + return OptionalBoolean.empty(); } @Override @@ -2193,7 +2195,7 @@ public class Object2BooleanRBTreeMap extends AbstractObject2BooleanMap imp } @Override - public boolean reduce(BooleanBooleanUnaryOperator operator) { + public OptionalBoolean reduce(BooleanBooleanUnaryOperator operator) { Objects.requireNonNull(operator); boolean state = false; boolean empty = true; @@ -2205,15 +2207,15 @@ public class Object2BooleanRBTreeMap extends AbstractObject2BooleanMap imp } state = operator.applyAsBoolean(state, entry.value); } - return state; + return empty ? OptionalBoolean.empty() : OptionalBoolean.of(state); } @Override - public boolean findFirst(BooleanPredicate filter) { + public OptionalBoolean findFirst(BooleanPredicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return false; + if(filter.test(entry.value)) return OptionalBoolean.of(entry.value); + return OptionalBoolean.empty(); } @Override @@ -2354,7 +2356,7 @@ public class Object2BooleanRBTreeMap extends AbstractObject2BooleanMap imp } @Override - public Object2BooleanMap.Entry reduce(ObjectObjectUnaryOperator, Object2BooleanMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Object2BooleanMap.Entry> operator) { Objects.requireNonNull(operator); Object2BooleanMap.Entry state = null; boolean empty = true; @@ -2366,19 +2368,19 @@ public class Object2BooleanRBTreeMap extends AbstractObject2BooleanMap imp } state = operator.apply(state, new BasicEntry<>(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Object2BooleanMap.Entry findFirst(Predicate> filter) { + public Optional> findFirst(Predicate> filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry<>(); for(Node 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/main/java/speiger/src/collections/objects/maps/impl/tree/Object2ByteAVLTreeMap.java b/src/main/java/speiger/src/collections/objects/maps/impl/tree/Object2ByteAVLTreeMap.java index c3afcf3..0bc498a 100644 --- a/src/main/java/speiger/src/collections/objects/maps/impl/tree/Object2ByteAVLTreeMap.java +++ b/src/main/java/speiger/src/collections/objects/maps/impl/tree/Object2ByteAVLTreeMap.java @@ -4,6 +4,7 @@ import java.util.Collections; import java.util.Map; import java.util.Comparator; import java.util.Objects; +import java.util.Optional; import java.util.NoSuchElementException; import java.util.function.Consumer; import java.util.function.BiFunction; @@ -33,6 +34,7 @@ import speiger.src.collections.bytes.functions.ByteConsumer; import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.bytes.functions.function.BytePredicate; +import speiger.src.collections.bytes.functions.OptionalByte; import speiger.src.collections.objects.collections.ObjectIterator; /** @@ -1097,7 +1099,7 @@ public class Object2ByteAVLTreeMap extends AbstractObject2ByteMap implemen } @Override - public T reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); T state = null; boolean empty = true; @@ -1109,15 +1111,15 @@ public class Object2ByteAVLTreeMap extends AbstractObject2ByteMap implemen } state = operator.apply(state, entry.key); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public T findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); for(Node entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.test(entry.key)) return entry.key; - return null; + if(filter.test(entry.key)) return Optional.ofNullable(entry.key); + return Optional.empty(); } @Override @@ -1746,7 +1748,7 @@ public class Object2ByteAVLTreeMap extends AbstractObject2ByteMap implemen } @Override - public Object2ByteMap.Entry reduce(ObjectObjectUnaryOperator, Object2ByteMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Object2ByteMap.Entry> operator) { Objects.requireNonNull(operator); Object2ByteMap.Entry state = null; boolean empty = true; @@ -1758,19 +1760,19 @@ public class Object2ByteAVLTreeMap extends AbstractObject2ByteMap implemen } state = operator.apply(state, new BasicEntry<>(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Object2ByteMap.Entry findFirst(Predicate> filter) { + public Optional> findFirst(Predicate> filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry<>(); for(Node 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 @@ -1865,7 +1867,7 @@ public class Object2ByteAVLTreeMap extends AbstractObject2ByteMap implemen } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -1877,15 +1879,15 @@ public class Object2ByteAVLTreeMap extends AbstractObject2ByteMap implemen } state = operator.applyAsByte(state, entry.value); } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return (byte)0; + if(filter.test(entry.value)) return OptionalByte.of(entry.value); + return OptionalByte.empty(); } @Override @@ -2201,7 +2203,7 @@ public class Object2ByteAVLTreeMap extends AbstractObject2ByteMap implemen } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -2213,15 +2215,15 @@ public class Object2ByteAVLTreeMap extends AbstractObject2ByteMap implemen } state = operator.applyAsByte(state, entry.value); } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return (byte)0; + if(filter.test(entry.value)) return OptionalByte.of(entry.value); + return OptionalByte.empty(); } @Override @@ -2362,7 +2364,7 @@ public class Object2ByteAVLTreeMap extends AbstractObject2ByteMap implemen } @Override - public Object2ByteMap.Entry reduce(ObjectObjectUnaryOperator, Object2ByteMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Object2ByteMap.Entry> operator) { Objects.requireNonNull(operator); Object2ByteMap.Entry state = null; boolean empty = true; @@ -2374,19 +2376,19 @@ public class Object2ByteAVLTreeMap extends AbstractObject2ByteMap implemen } state = operator.apply(state, new BasicEntry<>(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Object2ByteMap.Entry findFirst(Predicate> filter) { + public Optional> findFirst(Predicate> filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry<>(); for(Node 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/main/java/speiger/src/collections/objects/maps/impl/tree/Object2ByteRBTreeMap.java b/src/main/java/speiger/src/collections/objects/maps/impl/tree/Object2ByteRBTreeMap.java index 425bb43..14d3276 100644 --- a/src/main/java/speiger/src/collections/objects/maps/impl/tree/Object2ByteRBTreeMap.java +++ b/src/main/java/speiger/src/collections/objects/maps/impl/tree/Object2ByteRBTreeMap.java @@ -4,6 +4,7 @@ import java.util.Collections; import java.util.Map; import java.util.Comparator; import java.util.Objects; +import java.util.Optional; import java.util.NoSuchElementException; import java.util.function.Consumer; import java.util.function.BiFunction; @@ -32,6 +33,7 @@ import speiger.src.collections.bytes.functions.ByteConsumer; import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.bytes.functions.function.BytePredicate; +import speiger.src.collections.bytes.functions.OptionalByte; import speiger.src.collections.objects.collections.ObjectIterator; /** @@ -1151,7 +1153,7 @@ public class Object2ByteRBTreeMap extends AbstractObject2ByteMap implement } @Override - public T reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); T state = null; boolean empty = true; @@ -1163,15 +1165,15 @@ public class Object2ByteRBTreeMap extends AbstractObject2ByteMap implement } state = operator.apply(state, entry.key); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public T findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); for(Node entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.test(entry.key)) return entry.key; - return null; + if(filter.test(entry.key)) return Optional.ofNullable(entry.key); + return Optional.empty(); } @Override @@ -1810,7 +1812,7 @@ public class Object2ByteRBTreeMap extends AbstractObject2ByteMap implement } @Override - public Object2ByteMap.Entry reduce(ObjectObjectUnaryOperator, Object2ByteMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Object2ByteMap.Entry> operator) { Objects.requireNonNull(operator); Object2ByteMap.Entry state = null; boolean empty = true; @@ -1822,19 +1824,19 @@ public class Object2ByteRBTreeMap extends AbstractObject2ByteMap implement } state = operator.apply(state, new BasicEntry<>(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Object2ByteMap.Entry findFirst(Predicate> filter) { + public Optional> findFirst(Predicate> filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry<>(); for(Node 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 @@ -1929,7 +1931,7 @@ public class Object2ByteRBTreeMap extends AbstractObject2ByteMap implement } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -1941,15 +1943,15 @@ public class Object2ByteRBTreeMap extends AbstractObject2ByteMap implement } state = operator.applyAsByte(state, entry.value); } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return (byte)0; + if(filter.test(entry.value)) return OptionalByte.of(entry.value); + return OptionalByte.empty(); } @Override @@ -2265,7 +2267,7 @@ public class Object2ByteRBTreeMap extends AbstractObject2ByteMap implement } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -2277,15 +2279,15 @@ public class Object2ByteRBTreeMap extends AbstractObject2ByteMap implement } state = operator.applyAsByte(state, entry.value); } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return (byte)0; + if(filter.test(entry.value)) return OptionalByte.of(entry.value); + return OptionalByte.empty(); } @Override @@ -2426,7 +2428,7 @@ public class Object2ByteRBTreeMap extends AbstractObject2ByteMap implement } @Override - public Object2ByteMap.Entry reduce(ObjectObjectUnaryOperator, Object2ByteMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Object2ByteMap.Entry> operator) { Objects.requireNonNull(operator); Object2ByteMap.Entry state = null; boolean empty = true; @@ -2438,19 +2440,19 @@ public class Object2ByteRBTreeMap extends AbstractObject2ByteMap implement } state = operator.apply(state, new BasicEntry<>(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Object2ByteMap.Entry findFirst(Predicate> filter) { + public Optional> findFirst(Predicate> filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry<>(); for(Node 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/main/java/speiger/src/collections/objects/maps/impl/tree/Object2CharAVLTreeMap.java b/src/main/java/speiger/src/collections/objects/maps/impl/tree/Object2CharAVLTreeMap.java index 19ec9d4..3bb2944 100644 --- a/src/main/java/speiger/src/collections/objects/maps/impl/tree/Object2CharAVLTreeMap.java +++ b/src/main/java/speiger/src/collections/objects/maps/impl/tree/Object2CharAVLTreeMap.java @@ -4,6 +4,7 @@ import java.util.Collections; import java.util.Map; import java.util.Comparator; import java.util.Objects; +import java.util.Optional; import java.util.NoSuchElementException; import java.util.function.Consumer; import java.util.function.BiFunction; @@ -33,6 +34,7 @@ import speiger.src.collections.chars.functions.CharConsumer; import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.chars.functions.function.CharPredicate; +import speiger.src.collections.chars.functions.OptionalChar; import speiger.src.collections.objects.collections.ObjectIterator; /** @@ -1097,7 +1099,7 @@ public class Object2CharAVLTreeMap extends AbstractObject2CharMap implemen } @Override - public T reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); T state = null; boolean empty = true; @@ -1109,15 +1111,15 @@ public class Object2CharAVLTreeMap extends AbstractObject2CharMap implemen } state = operator.apply(state, entry.key); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public T findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); for(Node entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.test(entry.key)) return entry.key; - return null; + if(filter.test(entry.key)) return Optional.ofNullable(entry.key); + return Optional.empty(); } @Override @@ -1746,7 +1748,7 @@ public class Object2CharAVLTreeMap extends AbstractObject2CharMap implemen } @Override - public Object2CharMap.Entry reduce(ObjectObjectUnaryOperator, Object2CharMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Object2CharMap.Entry> operator) { Objects.requireNonNull(operator); Object2CharMap.Entry state = null; boolean empty = true; @@ -1758,19 +1760,19 @@ public class Object2CharAVLTreeMap extends AbstractObject2CharMap implemen } state = operator.apply(state, new BasicEntry<>(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Object2CharMap.Entry findFirst(Predicate> filter) { + public Optional> findFirst(Predicate> filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry<>(); for(Node 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 @@ -1865,7 +1867,7 @@ public class Object2CharAVLTreeMap extends AbstractObject2CharMap implemen } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -1877,15 +1879,15 @@ public class Object2CharAVLTreeMap extends AbstractObject2CharMap implemen } state = operator.applyAsChar(state, entry.value); } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return (char)0; + if(filter.test(entry.value)) return OptionalChar.of(entry.value); + return OptionalChar.empty(); } @Override @@ -2201,7 +2203,7 @@ public class Object2CharAVLTreeMap extends AbstractObject2CharMap implemen } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -2213,15 +2215,15 @@ public class Object2CharAVLTreeMap extends AbstractObject2CharMap implemen } state = operator.applyAsChar(state, entry.value); } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return (char)0; + if(filter.test(entry.value)) return OptionalChar.of(entry.value); + return OptionalChar.empty(); } @Override @@ -2362,7 +2364,7 @@ public class Object2CharAVLTreeMap extends AbstractObject2CharMap implemen } @Override - public Object2CharMap.Entry reduce(ObjectObjectUnaryOperator, Object2CharMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Object2CharMap.Entry> operator) { Objects.requireNonNull(operator); Object2CharMap.Entry state = null; boolean empty = true; @@ -2374,19 +2376,19 @@ public class Object2CharAVLTreeMap extends AbstractObject2CharMap implemen } state = operator.apply(state, new BasicEntry<>(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Object2CharMap.Entry findFirst(Predicate> filter) { + public Optional> findFirst(Predicate> filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry<>(); for(Node 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/main/java/speiger/src/collections/objects/maps/impl/tree/Object2CharRBTreeMap.java b/src/main/java/speiger/src/collections/objects/maps/impl/tree/Object2CharRBTreeMap.java index 5cc3c50..a925d6a 100644 --- a/src/main/java/speiger/src/collections/objects/maps/impl/tree/Object2CharRBTreeMap.java +++ b/src/main/java/speiger/src/collections/objects/maps/impl/tree/Object2CharRBTreeMap.java @@ -4,6 +4,7 @@ import java.util.Collections; import java.util.Map; import java.util.Comparator; import java.util.Objects; +import java.util.Optional; import java.util.NoSuchElementException; import java.util.function.Consumer; import java.util.function.BiFunction; @@ -32,6 +33,7 @@ import speiger.src.collections.chars.functions.CharConsumer; import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.chars.functions.function.CharPredicate; +import speiger.src.collections.chars.functions.OptionalChar; import speiger.src.collections.objects.collections.ObjectIterator; /** @@ -1151,7 +1153,7 @@ public class Object2CharRBTreeMap extends AbstractObject2CharMap implement } @Override - public T reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); T state = null; boolean empty = true; @@ -1163,15 +1165,15 @@ public class Object2CharRBTreeMap extends AbstractObject2CharMap implement } state = operator.apply(state, entry.key); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public T findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); for(Node entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.test(entry.key)) return entry.key; - return null; + if(filter.test(entry.key)) return Optional.ofNullable(entry.key); + return Optional.empty(); } @Override @@ -1810,7 +1812,7 @@ public class Object2CharRBTreeMap extends AbstractObject2CharMap implement } @Override - public Object2CharMap.Entry reduce(ObjectObjectUnaryOperator, Object2CharMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Object2CharMap.Entry> operator) { Objects.requireNonNull(operator); Object2CharMap.Entry state = null; boolean empty = true; @@ -1822,19 +1824,19 @@ public class Object2CharRBTreeMap extends AbstractObject2CharMap implement } state = operator.apply(state, new BasicEntry<>(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Object2CharMap.Entry findFirst(Predicate> filter) { + public Optional> findFirst(Predicate> filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry<>(); for(Node 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 @@ -1929,7 +1931,7 @@ public class Object2CharRBTreeMap extends AbstractObject2CharMap implement } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -1941,15 +1943,15 @@ public class Object2CharRBTreeMap extends AbstractObject2CharMap implement } state = operator.applyAsChar(state, entry.value); } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return (char)0; + if(filter.test(entry.value)) return OptionalChar.of(entry.value); + return OptionalChar.empty(); } @Override @@ -2265,7 +2267,7 @@ public class Object2CharRBTreeMap extends AbstractObject2CharMap implement } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -2277,15 +2279,15 @@ public class Object2CharRBTreeMap extends AbstractObject2CharMap implement } state = operator.applyAsChar(state, entry.value); } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return (char)0; + if(filter.test(entry.value)) return OptionalChar.of(entry.value); + return OptionalChar.empty(); } @Override @@ -2426,7 +2428,7 @@ public class Object2CharRBTreeMap extends AbstractObject2CharMap implement } @Override - public Object2CharMap.Entry reduce(ObjectObjectUnaryOperator, Object2CharMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Object2CharMap.Entry> operator) { Objects.requireNonNull(operator); Object2CharMap.Entry state = null; boolean empty = true; @@ -2438,19 +2440,19 @@ public class Object2CharRBTreeMap extends AbstractObject2CharMap implement } state = operator.apply(state, new BasicEntry<>(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Object2CharMap.Entry findFirst(Predicate> filter) { + public Optional> findFirst(Predicate> filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry<>(); for(Node 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/main/java/speiger/src/collections/objects/maps/impl/tree/Object2DoubleAVLTreeMap.java b/src/main/java/speiger/src/collections/objects/maps/impl/tree/Object2DoubleAVLTreeMap.java index b90675f..ebe4b1e 100644 --- a/src/main/java/speiger/src/collections/objects/maps/impl/tree/Object2DoubleAVLTreeMap.java +++ b/src/main/java/speiger/src/collections/objects/maps/impl/tree/Object2DoubleAVLTreeMap.java @@ -4,11 +4,13 @@ import java.util.Collections; import java.util.Map; import java.util.Comparator; 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; import java.util.function.DoublePredicate; +import java.util.OptionalDouble; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; @@ -1097,7 +1099,7 @@ public class Object2DoubleAVLTreeMap extends AbstractObject2DoubleMap impl } @Override - public T reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); T state = null; boolean empty = true; @@ -1109,15 +1111,15 @@ public class Object2DoubleAVLTreeMap extends AbstractObject2DoubleMap impl } state = operator.apply(state, entry.key); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public T findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); for(Node entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.test(entry.key)) return entry.key; - return null; + if(filter.test(entry.key)) return Optional.ofNullable(entry.key); + return Optional.empty(); } @Override @@ -1746,7 +1748,7 @@ public class Object2DoubleAVLTreeMap extends AbstractObject2DoubleMap impl } @Override - public Object2DoubleMap.Entry reduce(ObjectObjectUnaryOperator, Object2DoubleMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Object2DoubleMap.Entry> operator) { Objects.requireNonNull(operator); Object2DoubleMap.Entry state = null; boolean empty = true; @@ -1758,19 +1760,19 @@ public class Object2DoubleAVLTreeMap extends AbstractObject2DoubleMap impl } state = operator.apply(state, new BasicEntry<>(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Object2DoubleMap.Entry findFirst(Predicate> filter) { + public Optional> findFirst(Predicate> filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry<>(); for(Node 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 @@ -1865,7 +1867,7 @@ public class Object2DoubleAVLTreeMap extends AbstractObject2DoubleMap impl } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -1877,15 +1879,15 @@ public class Object2DoubleAVLTreeMap extends AbstractObject2DoubleMap impl } state = operator.applyAsDouble(state, entry.value); } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return 0D; + if(filter.test(entry.value)) return OptionalDouble.of(entry.value); + return OptionalDouble.empty(); } @Override @@ -2201,7 +2203,7 @@ public class Object2DoubleAVLTreeMap extends AbstractObject2DoubleMap impl } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -2213,15 +2215,15 @@ public class Object2DoubleAVLTreeMap extends AbstractObject2DoubleMap impl } state = operator.applyAsDouble(state, entry.value); } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return 0D; + if(filter.test(entry.value)) return OptionalDouble.of(entry.value); + return OptionalDouble.empty(); } @Override @@ -2362,7 +2364,7 @@ public class Object2DoubleAVLTreeMap extends AbstractObject2DoubleMap impl } @Override - public Object2DoubleMap.Entry reduce(ObjectObjectUnaryOperator, Object2DoubleMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Object2DoubleMap.Entry> operator) { Objects.requireNonNull(operator); Object2DoubleMap.Entry state = null; boolean empty = true; @@ -2374,19 +2376,19 @@ public class Object2DoubleAVLTreeMap extends AbstractObject2DoubleMap impl } state = operator.apply(state, new BasicEntry<>(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Object2DoubleMap.Entry findFirst(Predicate> filter) { + public Optional> findFirst(Predicate> filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry<>(); for(Node 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/main/java/speiger/src/collections/objects/maps/impl/tree/Object2DoubleRBTreeMap.java b/src/main/java/speiger/src/collections/objects/maps/impl/tree/Object2DoubleRBTreeMap.java index c66ed1e..71cc589 100644 --- a/src/main/java/speiger/src/collections/objects/maps/impl/tree/Object2DoubleRBTreeMap.java +++ b/src/main/java/speiger/src/collections/objects/maps/impl/tree/Object2DoubleRBTreeMap.java @@ -4,11 +4,13 @@ import java.util.Collections; import java.util.Map; import java.util.Comparator; 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; import java.util.function.DoublePredicate; +import java.util.OptionalDouble; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; @@ -1151,7 +1153,7 @@ public class Object2DoubleRBTreeMap extends AbstractObject2DoubleMap imple } @Override - public T reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); T state = null; boolean empty = true; @@ -1163,15 +1165,15 @@ public class Object2DoubleRBTreeMap extends AbstractObject2DoubleMap imple } state = operator.apply(state, entry.key); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public T findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); for(Node entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.test(entry.key)) return entry.key; - return null; + if(filter.test(entry.key)) return Optional.ofNullable(entry.key); + return Optional.empty(); } @Override @@ -1810,7 +1812,7 @@ public class Object2DoubleRBTreeMap extends AbstractObject2DoubleMap imple } @Override - public Object2DoubleMap.Entry reduce(ObjectObjectUnaryOperator, Object2DoubleMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Object2DoubleMap.Entry> operator) { Objects.requireNonNull(operator); Object2DoubleMap.Entry state = null; boolean empty = true; @@ -1822,19 +1824,19 @@ public class Object2DoubleRBTreeMap extends AbstractObject2DoubleMap imple } state = operator.apply(state, new BasicEntry<>(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Object2DoubleMap.Entry findFirst(Predicate> filter) { + public Optional> findFirst(Predicate> filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry<>(); for(Node 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 @@ -1929,7 +1931,7 @@ public class Object2DoubleRBTreeMap extends AbstractObject2DoubleMap imple } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -1941,15 +1943,15 @@ public class Object2DoubleRBTreeMap extends AbstractObject2DoubleMap imple } state = operator.applyAsDouble(state, entry.value); } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return 0D; + if(filter.test(entry.value)) return OptionalDouble.of(entry.value); + return OptionalDouble.empty(); } @Override @@ -2265,7 +2267,7 @@ public class Object2DoubleRBTreeMap extends AbstractObject2DoubleMap imple } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -2277,15 +2279,15 @@ public class Object2DoubleRBTreeMap extends AbstractObject2DoubleMap imple } state = operator.applyAsDouble(state, entry.value); } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return 0D; + if(filter.test(entry.value)) return OptionalDouble.of(entry.value); + return OptionalDouble.empty(); } @Override @@ -2426,7 +2428,7 @@ public class Object2DoubleRBTreeMap extends AbstractObject2DoubleMap imple } @Override - public Object2DoubleMap.Entry reduce(ObjectObjectUnaryOperator, Object2DoubleMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Object2DoubleMap.Entry> operator) { Objects.requireNonNull(operator); Object2DoubleMap.Entry state = null; boolean empty = true; @@ -2438,19 +2440,19 @@ public class Object2DoubleRBTreeMap extends AbstractObject2DoubleMap imple } state = operator.apply(state, new BasicEntry<>(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Object2DoubleMap.Entry findFirst(Predicate> filter) { + public Optional> findFirst(Predicate> filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry<>(); for(Node 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/main/java/speiger/src/collections/objects/maps/impl/tree/Object2FloatAVLTreeMap.java b/src/main/java/speiger/src/collections/objects/maps/impl/tree/Object2FloatAVLTreeMap.java index 63e27a0..daa6bc8 100644 --- a/src/main/java/speiger/src/collections/objects/maps/impl/tree/Object2FloatAVLTreeMap.java +++ b/src/main/java/speiger/src/collections/objects/maps/impl/tree/Object2FloatAVLTreeMap.java @@ -4,6 +4,7 @@ import java.util.Collections; import java.util.Map; import java.util.Comparator; import java.util.Objects; +import java.util.Optional; import java.util.NoSuchElementException; import java.util.function.Consumer; import java.util.function.BiFunction; @@ -33,6 +34,7 @@ import speiger.src.collections.floats.functions.FloatConsumer; import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.floats.functions.function.FloatPredicate; +import speiger.src.collections.floats.functions.OptionalFloat; import speiger.src.collections.objects.collections.ObjectIterator; /** @@ -1097,7 +1099,7 @@ public class Object2FloatAVLTreeMap extends AbstractObject2FloatMap implem } @Override - public T reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); T state = null; boolean empty = true; @@ -1109,15 +1111,15 @@ public class Object2FloatAVLTreeMap extends AbstractObject2FloatMap implem } state = operator.apply(state, entry.key); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public T findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); for(Node entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.test(entry.key)) return entry.key; - return null; + if(filter.test(entry.key)) return Optional.ofNullable(entry.key); + return Optional.empty(); } @Override @@ -1746,7 +1748,7 @@ public class Object2FloatAVLTreeMap extends AbstractObject2FloatMap implem } @Override - public Object2FloatMap.Entry reduce(ObjectObjectUnaryOperator, Object2FloatMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Object2FloatMap.Entry> operator) { Objects.requireNonNull(operator); Object2FloatMap.Entry state = null; boolean empty = true; @@ -1758,19 +1760,19 @@ public class Object2FloatAVLTreeMap extends AbstractObject2FloatMap implem } state = operator.apply(state, new BasicEntry<>(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Object2FloatMap.Entry findFirst(Predicate> filter) { + public Optional> findFirst(Predicate> filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry<>(); for(Node 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 @@ -1865,7 +1867,7 @@ public class Object2FloatAVLTreeMap extends AbstractObject2FloatMap implem } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -1877,15 +1879,15 @@ public class Object2FloatAVLTreeMap extends AbstractObject2FloatMap implem } state = operator.applyAsFloat(state, entry.value); } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return 0F; + if(filter.test(entry.value)) return OptionalFloat.of(entry.value); + return OptionalFloat.empty(); } @Override @@ -2201,7 +2203,7 @@ public class Object2FloatAVLTreeMap extends AbstractObject2FloatMap implem } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -2213,15 +2215,15 @@ public class Object2FloatAVLTreeMap extends AbstractObject2FloatMap implem } state = operator.applyAsFloat(state, entry.value); } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return 0F; + if(filter.test(entry.value)) return OptionalFloat.of(entry.value); + return OptionalFloat.empty(); } @Override @@ -2362,7 +2364,7 @@ public class Object2FloatAVLTreeMap extends AbstractObject2FloatMap implem } @Override - public Object2FloatMap.Entry reduce(ObjectObjectUnaryOperator, Object2FloatMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Object2FloatMap.Entry> operator) { Objects.requireNonNull(operator); Object2FloatMap.Entry state = null; boolean empty = true; @@ -2374,19 +2376,19 @@ public class Object2FloatAVLTreeMap extends AbstractObject2FloatMap implem } state = operator.apply(state, new BasicEntry<>(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Object2FloatMap.Entry findFirst(Predicate> filter) { + public Optional> findFirst(Predicate> filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry<>(); for(Node 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/main/java/speiger/src/collections/objects/maps/impl/tree/Object2FloatRBTreeMap.java b/src/main/java/speiger/src/collections/objects/maps/impl/tree/Object2FloatRBTreeMap.java index 6b20aa9..4fa8bbd 100644 --- a/src/main/java/speiger/src/collections/objects/maps/impl/tree/Object2FloatRBTreeMap.java +++ b/src/main/java/speiger/src/collections/objects/maps/impl/tree/Object2FloatRBTreeMap.java @@ -4,6 +4,7 @@ import java.util.Collections; import java.util.Map; import java.util.Comparator; import java.util.Objects; +import java.util.Optional; import java.util.NoSuchElementException; import java.util.function.Consumer; import java.util.function.BiFunction; @@ -32,6 +33,7 @@ import speiger.src.collections.floats.functions.FloatConsumer; import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.floats.functions.function.FloatPredicate; +import speiger.src.collections.floats.functions.OptionalFloat; import speiger.src.collections.objects.collections.ObjectIterator; /** @@ -1151,7 +1153,7 @@ public class Object2FloatRBTreeMap extends AbstractObject2FloatMap impleme } @Override - public T reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); T state = null; boolean empty = true; @@ -1163,15 +1165,15 @@ public class Object2FloatRBTreeMap extends AbstractObject2FloatMap impleme } state = operator.apply(state, entry.key); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public T findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); for(Node entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.test(entry.key)) return entry.key; - return null; + if(filter.test(entry.key)) return Optional.ofNullable(entry.key); + return Optional.empty(); } @Override @@ -1810,7 +1812,7 @@ public class Object2FloatRBTreeMap extends AbstractObject2FloatMap impleme } @Override - public Object2FloatMap.Entry reduce(ObjectObjectUnaryOperator, Object2FloatMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Object2FloatMap.Entry> operator) { Objects.requireNonNull(operator); Object2FloatMap.Entry state = null; boolean empty = true; @@ -1822,19 +1824,19 @@ public class Object2FloatRBTreeMap extends AbstractObject2FloatMap impleme } state = operator.apply(state, new BasicEntry<>(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Object2FloatMap.Entry findFirst(Predicate> filter) { + public Optional> findFirst(Predicate> filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry<>(); for(Node 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 @@ -1929,7 +1931,7 @@ public class Object2FloatRBTreeMap extends AbstractObject2FloatMap impleme } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -1941,15 +1943,15 @@ public class Object2FloatRBTreeMap extends AbstractObject2FloatMap impleme } state = operator.applyAsFloat(state, entry.value); } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return 0F; + if(filter.test(entry.value)) return OptionalFloat.of(entry.value); + return OptionalFloat.empty(); } @Override @@ -2265,7 +2267,7 @@ public class Object2FloatRBTreeMap extends AbstractObject2FloatMap impleme } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -2277,15 +2279,15 @@ public class Object2FloatRBTreeMap extends AbstractObject2FloatMap impleme } state = operator.applyAsFloat(state, entry.value); } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return 0F; + if(filter.test(entry.value)) return OptionalFloat.of(entry.value); + return OptionalFloat.empty(); } @Override @@ -2426,7 +2428,7 @@ public class Object2FloatRBTreeMap extends AbstractObject2FloatMap impleme } @Override - public Object2FloatMap.Entry reduce(ObjectObjectUnaryOperator, Object2FloatMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Object2FloatMap.Entry> operator) { Objects.requireNonNull(operator); Object2FloatMap.Entry state = null; boolean empty = true; @@ -2438,19 +2440,19 @@ public class Object2FloatRBTreeMap extends AbstractObject2FloatMap impleme } state = operator.apply(state, new BasicEntry<>(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Object2FloatMap.Entry findFirst(Predicate> filter) { + public Optional> findFirst(Predicate> filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry<>(); for(Node 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/main/java/speiger/src/collections/objects/maps/impl/tree/Object2IntAVLTreeMap.java b/src/main/java/speiger/src/collections/objects/maps/impl/tree/Object2IntAVLTreeMap.java index 854fa01..07517c0 100644 --- a/src/main/java/speiger/src/collections/objects/maps/impl/tree/Object2IntAVLTreeMap.java +++ b/src/main/java/speiger/src/collections/objects/maps/impl/tree/Object2IntAVLTreeMap.java @@ -4,11 +4,13 @@ import java.util.Collections; import java.util.Map; import java.util.Comparator; 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; import java.util.function.IntPredicate; +import java.util.OptionalInt; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; @@ -1097,7 +1099,7 @@ public class Object2IntAVLTreeMap extends AbstractObject2IntMap implements } @Override - public T reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); T state = null; boolean empty = true; @@ -1109,15 +1111,15 @@ public class Object2IntAVLTreeMap extends AbstractObject2IntMap implements } state = operator.apply(state, entry.key); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public T findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); for(Node entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.test(entry.key)) return entry.key; - return null; + if(filter.test(entry.key)) return Optional.ofNullable(entry.key); + return Optional.empty(); } @Override @@ -1746,7 +1748,7 @@ public class Object2IntAVLTreeMap extends AbstractObject2IntMap implements } @Override - public Object2IntMap.Entry reduce(ObjectObjectUnaryOperator, Object2IntMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Object2IntMap.Entry> operator) { Objects.requireNonNull(operator); Object2IntMap.Entry state = null; boolean empty = true; @@ -1758,19 +1760,19 @@ public class Object2IntAVLTreeMap extends AbstractObject2IntMap implements } state = operator.apply(state, new BasicEntry<>(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Object2IntMap.Entry findFirst(Predicate> filter) { + public Optional> findFirst(Predicate> filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry<>(); for(Node 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 @@ -1865,7 +1867,7 @@ public class Object2IntAVLTreeMap extends AbstractObject2IntMap implements } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -1877,15 +1879,15 @@ public class Object2IntAVLTreeMap extends AbstractObject2IntMap implements } state = operator.applyAsInt(state, entry.value); } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return 0; + if(filter.test(entry.value)) return OptionalInt.of(entry.value); + return OptionalInt.empty(); } @Override @@ -2201,7 +2203,7 @@ public class Object2IntAVLTreeMap extends AbstractObject2IntMap implements } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -2213,15 +2215,15 @@ public class Object2IntAVLTreeMap extends AbstractObject2IntMap implements } state = operator.applyAsInt(state, entry.value); } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return 0; + if(filter.test(entry.value)) return OptionalInt.of(entry.value); + return OptionalInt.empty(); } @Override @@ -2362,7 +2364,7 @@ public class Object2IntAVLTreeMap extends AbstractObject2IntMap implements } @Override - public Object2IntMap.Entry reduce(ObjectObjectUnaryOperator, Object2IntMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Object2IntMap.Entry> operator) { Objects.requireNonNull(operator); Object2IntMap.Entry state = null; boolean empty = true; @@ -2374,19 +2376,19 @@ public class Object2IntAVLTreeMap extends AbstractObject2IntMap implements } state = operator.apply(state, new BasicEntry<>(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Object2IntMap.Entry findFirst(Predicate> filter) { + public Optional> findFirst(Predicate> filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry<>(); for(Node 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/main/java/speiger/src/collections/objects/maps/impl/tree/Object2IntRBTreeMap.java b/src/main/java/speiger/src/collections/objects/maps/impl/tree/Object2IntRBTreeMap.java index 8479046..b090924 100644 --- a/src/main/java/speiger/src/collections/objects/maps/impl/tree/Object2IntRBTreeMap.java +++ b/src/main/java/speiger/src/collections/objects/maps/impl/tree/Object2IntRBTreeMap.java @@ -4,11 +4,13 @@ import java.util.Collections; import java.util.Map; import java.util.Comparator; 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; import java.util.function.IntPredicate; +import java.util.OptionalInt; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; @@ -1151,7 +1153,7 @@ public class Object2IntRBTreeMap extends AbstractObject2IntMap implements } @Override - public T reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); T state = null; boolean empty = true; @@ -1163,15 +1165,15 @@ public class Object2IntRBTreeMap extends AbstractObject2IntMap implements } state = operator.apply(state, entry.key); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public T findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); for(Node entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.test(entry.key)) return entry.key; - return null; + if(filter.test(entry.key)) return Optional.ofNullable(entry.key); + return Optional.empty(); } @Override @@ -1810,7 +1812,7 @@ public class Object2IntRBTreeMap extends AbstractObject2IntMap implements } @Override - public Object2IntMap.Entry reduce(ObjectObjectUnaryOperator, Object2IntMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Object2IntMap.Entry> operator) { Objects.requireNonNull(operator); Object2IntMap.Entry state = null; boolean empty = true; @@ -1822,19 +1824,19 @@ public class Object2IntRBTreeMap extends AbstractObject2IntMap implements } state = operator.apply(state, new BasicEntry<>(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Object2IntMap.Entry findFirst(Predicate> filter) { + public Optional> findFirst(Predicate> filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry<>(); for(Node 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 @@ -1929,7 +1931,7 @@ public class Object2IntRBTreeMap extends AbstractObject2IntMap implements } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -1941,15 +1943,15 @@ public class Object2IntRBTreeMap extends AbstractObject2IntMap implements } state = operator.applyAsInt(state, entry.value); } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return 0; + if(filter.test(entry.value)) return OptionalInt.of(entry.value); + return OptionalInt.empty(); } @Override @@ -2265,7 +2267,7 @@ public class Object2IntRBTreeMap extends AbstractObject2IntMap implements } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -2277,15 +2279,15 @@ public class Object2IntRBTreeMap extends AbstractObject2IntMap implements } state = operator.applyAsInt(state, entry.value); } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return 0; + if(filter.test(entry.value)) return OptionalInt.of(entry.value); + return OptionalInt.empty(); } @Override @@ -2426,7 +2428,7 @@ public class Object2IntRBTreeMap extends AbstractObject2IntMap implements } @Override - public Object2IntMap.Entry reduce(ObjectObjectUnaryOperator, Object2IntMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Object2IntMap.Entry> operator) { Objects.requireNonNull(operator); Object2IntMap.Entry state = null; boolean empty = true; @@ -2438,19 +2440,19 @@ public class Object2IntRBTreeMap extends AbstractObject2IntMap implements } state = operator.apply(state, new BasicEntry<>(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Object2IntMap.Entry findFirst(Predicate> filter) { + public Optional> findFirst(Predicate> filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry<>(); for(Node 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/main/java/speiger/src/collections/objects/maps/impl/tree/Object2LongAVLTreeMap.java b/src/main/java/speiger/src/collections/objects/maps/impl/tree/Object2LongAVLTreeMap.java index 1d863fb..8dc3e7a 100644 --- a/src/main/java/speiger/src/collections/objects/maps/impl/tree/Object2LongAVLTreeMap.java +++ b/src/main/java/speiger/src/collections/objects/maps/impl/tree/Object2LongAVLTreeMap.java @@ -4,11 +4,13 @@ import java.util.Collections; import java.util.Map; import java.util.Comparator; 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; import java.util.function.LongPredicate; +import java.util.OptionalLong; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; @@ -1097,7 +1099,7 @@ public class Object2LongAVLTreeMap extends AbstractObject2LongMap implemen } @Override - public T reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); T state = null; boolean empty = true; @@ -1109,15 +1111,15 @@ public class Object2LongAVLTreeMap extends AbstractObject2LongMap implemen } state = operator.apply(state, entry.key); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public T findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); for(Node entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.test(entry.key)) return entry.key; - return null; + if(filter.test(entry.key)) return Optional.ofNullable(entry.key); + return Optional.empty(); } @Override @@ -1746,7 +1748,7 @@ public class Object2LongAVLTreeMap extends AbstractObject2LongMap implemen } @Override - public Object2LongMap.Entry reduce(ObjectObjectUnaryOperator, Object2LongMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Object2LongMap.Entry> operator) { Objects.requireNonNull(operator); Object2LongMap.Entry state = null; boolean empty = true; @@ -1758,19 +1760,19 @@ public class Object2LongAVLTreeMap extends AbstractObject2LongMap implemen } state = operator.apply(state, new BasicEntry<>(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Object2LongMap.Entry findFirst(Predicate> filter) { + public Optional> findFirst(Predicate> filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry<>(); for(Node 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 @@ -1865,7 +1867,7 @@ public class Object2LongAVLTreeMap extends AbstractObject2LongMap implemen } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -1877,15 +1879,15 @@ public class Object2LongAVLTreeMap extends AbstractObject2LongMap implemen } state = operator.applyAsLong(state, entry.value); } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return 0L; + if(filter.test(entry.value)) return OptionalLong.of(entry.value); + return OptionalLong.empty(); } @Override @@ -2201,7 +2203,7 @@ public class Object2LongAVLTreeMap extends AbstractObject2LongMap implemen } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -2213,15 +2215,15 @@ public class Object2LongAVLTreeMap extends AbstractObject2LongMap implemen } state = operator.applyAsLong(state, entry.value); } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return 0L; + if(filter.test(entry.value)) return OptionalLong.of(entry.value); + return OptionalLong.empty(); } @Override @@ -2362,7 +2364,7 @@ public class Object2LongAVLTreeMap extends AbstractObject2LongMap implemen } @Override - public Object2LongMap.Entry reduce(ObjectObjectUnaryOperator, Object2LongMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Object2LongMap.Entry> operator) { Objects.requireNonNull(operator); Object2LongMap.Entry state = null; boolean empty = true; @@ -2374,19 +2376,19 @@ public class Object2LongAVLTreeMap extends AbstractObject2LongMap implemen } state = operator.apply(state, new BasicEntry<>(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Object2LongMap.Entry findFirst(Predicate> filter) { + public Optional> findFirst(Predicate> filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry<>(); for(Node 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/main/java/speiger/src/collections/objects/maps/impl/tree/Object2LongRBTreeMap.java b/src/main/java/speiger/src/collections/objects/maps/impl/tree/Object2LongRBTreeMap.java index a26d87f..e34fc51 100644 --- a/src/main/java/speiger/src/collections/objects/maps/impl/tree/Object2LongRBTreeMap.java +++ b/src/main/java/speiger/src/collections/objects/maps/impl/tree/Object2LongRBTreeMap.java @@ -4,11 +4,13 @@ import java.util.Collections; import java.util.Map; import java.util.Comparator; 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; import java.util.function.LongPredicate; +import java.util.OptionalLong; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; @@ -1151,7 +1153,7 @@ public class Object2LongRBTreeMap extends AbstractObject2LongMap implement } @Override - public T reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); T state = null; boolean empty = true; @@ -1163,15 +1165,15 @@ public class Object2LongRBTreeMap extends AbstractObject2LongMap implement } state = operator.apply(state, entry.key); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public T findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); for(Node entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.test(entry.key)) return entry.key; - return null; + if(filter.test(entry.key)) return Optional.ofNullable(entry.key); + return Optional.empty(); } @Override @@ -1810,7 +1812,7 @@ public class Object2LongRBTreeMap extends AbstractObject2LongMap implement } @Override - public Object2LongMap.Entry reduce(ObjectObjectUnaryOperator, Object2LongMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Object2LongMap.Entry> operator) { Objects.requireNonNull(operator); Object2LongMap.Entry state = null; boolean empty = true; @@ -1822,19 +1824,19 @@ public class Object2LongRBTreeMap extends AbstractObject2LongMap implement } state = operator.apply(state, new BasicEntry<>(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Object2LongMap.Entry findFirst(Predicate> filter) { + public Optional> findFirst(Predicate> filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry<>(); for(Node 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 @@ -1929,7 +1931,7 @@ public class Object2LongRBTreeMap extends AbstractObject2LongMap implement } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -1941,15 +1943,15 @@ public class Object2LongRBTreeMap extends AbstractObject2LongMap implement } state = operator.applyAsLong(state, entry.value); } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return 0L; + if(filter.test(entry.value)) return OptionalLong.of(entry.value); + return OptionalLong.empty(); } @Override @@ -2265,7 +2267,7 @@ public class Object2LongRBTreeMap extends AbstractObject2LongMap implement } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -2277,15 +2279,15 @@ public class Object2LongRBTreeMap extends AbstractObject2LongMap implement } state = operator.applyAsLong(state, entry.value); } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return 0L; + if(filter.test(entry.value)) return OptionalLong.of(entry.value); + return OptionalLong.empty(); } @Override @@ -2426,7 +2428,7 @@ public class Object2LongRBTreeMap extends AbstractObject2LongMap implement } @Override - public Object2LongMap.Entry reduce(ObjectObjectUnaryOperator, Object2LongMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Object2LongMap.Entry> operator) { Objects.requireNonNull(operator); Object2LongMap.Entry state = null; boolean empty = true; @@ -2438,19 +2440,19 @@ public class Object2LongRBTreeMap extends AbstractObject2LongMap implement } state = operator.apply(state, new BasicEntry<>(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Object2LongMap.Entry findFirst(Predicate> filter) { + public Optional> findFirst(Predicate> filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry<>(); for(Node 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/main/java/speiger/src/collections/objects/maps/impl/tree/Object2ObjectAVLTreeMap.java b/src/main/java/speiger/src/collections/objects/maps/impl/tree/Object2ObjectAVLTreeMap.java index 4f43612..fb563cd 100644 --- a/src/main/java/speiger/src/collections/objects/maps/impl/tree/Object2ObjectAVLTreeMap.java +++ b/src/main/java/speiger/src/collections/objects/maps/impl/tree/Object2ObjectAVLTreeMap.java @@ -4,6 +4,7 @@ import java.util.Collections; import java.util.Map; import java.util.Comparator; import java.util.Objects; +import java.util.Optional; import java.util.NoSuchElementException; import java.util.function.Consumer; import java.util.function.BiFunction; @@ -956,7 +957,7 @@ public class Object2ObjectAVLTreeMap extends AbstractObject2ObjectMap operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); T state = null; boolean empty = true; @@ -968,15 +969,15 @@ public class Object2ObjectAVLTreeMap extends AbstractObject2ObjectMap filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); for(Node entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.test(entry.key)) return entry.key; - return null; + if(filter.test(entry.key)) return Optional.ofNullable(entry.key); + return Optional.empty(); } @Override @@ -1598,7 +1599,7 @@ public class Object2ObjectAVLTreeMap extends AbstractObject2ObjectMap reduce(ObjectObjectUnaryOperator, Object2ObjectMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Object2ObjectMap.Entry> operator) { Objects.requireNonNull(operator); Object2ObjectMap.Entry state = null; boolean empty = true; @@ -1610,19 +1611,19 @@ public class Object2ObjectAVLTreeMap extends AbstractObject2ObjectMap(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Object2ObjectMap.Entry findFirst(Predicate> filter) { + public Optional> findFirst(Predicate> filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry<>(); for(Node 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 @@ -1717,7 +1718,7 @@ public class Object2ObjectAVLTreeMap extends AbstractObject2ObjectMap operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); V state = null; boolean empty = true; @@ -1729,15 +1730,15 @@ public class Object2ObjectAVLTreeMap extends AbstractObject2ObjectMap filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return null; + if(filter.test(entry.value)) return Optional.ofNullable(entry.value); + return Optional.empty(); } @Override @@ -2053,7 +2054,7 @@ public class Object2ObjectAVLTreeMap extends AbstractObject2ObjectMap operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); V state = null; boolean empty = true; @@ -2065,15 +2066,15 @@ public class Object2ObjectAVLTreeMap extends AbstractObject2ObjectMap filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return null; + if(filter.test(entry.value)) return Optional.ofNullable(entry.value); + return Optional.empty(); } @Override @@ -2214,7 +2215,7 @@ public class Object2ObjectAVLTreeMap extends AbstractObject2ObjectMap reduce(ObjectObjectUnaryOperator, Object2ObjectMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Object2ObjectMap.Entry> operator) { Objects.requireNonNull(operator); Object2ObjectMap.Entry state = null; boolean empty = true; @@ -2226,19 +2227,19 @@ public class Object2ObjectAVLTreeMap extends AbstractObject2ObjectMap(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Object2ObjectMap.Entry findFirst(Predicate> filter) { + public Optional> findFirst(Predicate> filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry<>(); for(Node 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/main/java/speiger/src/collections/objects/maps/impl/tree/Object2ObjectRBTreeMap.java b/src/main/java/speiger/src/collections/objects/maps/impl/tree/Object2ObjectRBTreeMap.java index b940756..9faa560 100644 --- a/src/main/java/speiger/src/collections/objects/maps/impl/tree/Object2ObjectRBTreeMap.java +++ b/src/main/java/speiger/src/collections/objects/maps/impl/tree/Object2ObjectRBTreeMap.java @@ -4,6 +4,7 @@ import java.util.Collections; import java.util.Map; import java.util.Comparator; import java.util.Objects; +import java.util.Optional; import java.util.NoSuchElementException; import java.util.function.Consumer; import java.util.function.BiFunction; @@ -1009,7 +1010,7 @@ public class Object2ObjectRBTreeMap extends AbstractObject2ObjectMap } @Override - public T reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); T state = null; boolean empty = true; @@ -1021,15 +1022,15 @@ public class Object2ObjectRBTreeMap extends AbstractObject2ObjectMap } state = operator.apply(state, entry.key); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public T findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); for(Node entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.test(entry.key)) return entry.key; - return null; + if(filter.test(entry.key)) return Optional.ofNullable(entry.key); + return Optional.empty(); } @Override @@ -1661,7 +1662,7 @@ public class Object2ObjectRBTreeMap extends AbstractObject2ObjectMap } @Override - public Object2ObjectMap.Entry reduce(ObjectObjectUnaryOperator, Object2ObjectMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Object2ObjectMap.Entry> operator) { Objects.requireNonNull(operator); Object2ObjectMap.Entry state = null; boolean empty = true; @@ -1673,19 +1674,19 @@ public class Object2ObjectRBTreeMap extends AbstractObject2ObjectMap } state = operator.apply(state, new BasicEntry<>(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Object2ObjectMap.Entry findFirst(Predicate> filter) { + public Optional> findFirst(Predicate> filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry<>(); for(Node 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 @@ -1780,7 +1781,7 @@ public class Object2ObjectRBTreeMap extends AbstractObject2ObjectMap } @Override - public V reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); V state = null; boolean empty = true; @@ -1792,15 +1793,15 @@ public class Object2ObjectRBTreeMap extends AbstractObject2ObjectMap } state = operator.apply(state, entry.value); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public V findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return null; + if(filter.test(entry.value)) return Optional.ofNullable(entry.value); + return Optional.empty(); } @Override @@ -2116,7 +2117,7 @@ public class Object2ObjectRBTreeMap extends AbstractObject2ObjectMap } @Override - public V reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); V state = null; boolean empty = true; @@ -2128,15 +2129,15 @@ public class Object2ObjectRBTreeMap extends AbstractObject2ObjectMap } state = operator.apply(state, entry.value); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public V findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return null; + if(filter.test(entry.value)) return Optional.ofNullable(entry.value); + return Optional.empty(); } @Override @@ -2277,7 +2278,7 @@ public class Object2ObjectRBTreeMap extends AbstractObject2ObjectMap } @Override - public Object2ObjectMap.Entry reduce(ObjectObjectUnaryOperator, Object2ObjectMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Object2ObjectMap.Entry> operator) { Objects.requireNonNull(operator); Object2ObjectMap.Entry state = null; boolean empty = true; @@ -2289,19 +2290,19 @@ public class Object2ObjectRBTreeMap extends AbstractObject2ObjectMap } state = operator.apply(state, new BasicEntry<>(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Object2ObjectMap.Entry findFirst(Predicate> filter) { + public Optional> findFirst(Predicate> filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry<>(); for(Node 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/main/java/speiger/src/collections/objects/maps/impl/tree/Object2ShortAVLTreeMap.java b/src/main/java/speiger/src/collections/objects/maps/impl/tree/Object2ShortAVLTreeMap.java index b24f665..d620ba5 100644 --- a/src/main/java/speiger/src/collections/objects/maps/impl/tree/Object2ShortAVLTreeMap.java +++ b/src/main/java/speiger/src/collections/objects/maps/impl/tree/Object2ShortAVLTreeMap.java @@ -4,6 +4,7 @@ import java.util.Collections; import java.util.Map; import java.util.Comparator; import java.util.Objects; +import java.util.Optional; import java.util.NoSuchElementException; import java.util.function.Consumer; import java.util.function.BiFunction; @@ -33,6 +34,7 @@ import speiger.src.collections.shorts.functions.ShortConsumer; import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.shorts.functions.function.ShortPredicate; +import speiger.src.collections.shorts.functions.OptionalShort; import speiger.src.collections.objects.collections.ObjectIterator; /** @@ -1097,7 +1099,7 @@ public class Object2ShortAVLTreeMap extends AbstractObject2ShortMap implem } @Override - public T reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); T state = null; boolean empty = true; @@ -1109,15 +1111,15 @@ public class Object2ShortAVLTreeMap extends AbstractObject2ShortMap implem } state = operator.apply(state, entry.key); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public T findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); for(Node entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.test(entry.key)) return entry.key; - return null; + if(filter.test(entry.key)) return Optional.ofNullable(entry.key); + return Optional.empty(); } @Override @@ -1746,7 +1748,7 @@ public class Object2ShortAVLTreeMap extends AbstractObject2ShortMap implem } @Override - public Object2ShortMap.Entry reduce(ObjectObjectUnaryOperator, Object2ShortMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Object2ShortMap.Entry> operator) { Objects.requireNonNull(operator); Object2ShortMap.Entry state = null; boolean empty = true; @@ -1758,19 +1760,19 @@ public class Object2ShortAVLTreeMap extends AbstractObject2ShortMap implem } state = operator.apply(state, new BasicEntry<>(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Object2ShortMap.Entry findFirst(Predicate> filter) { + public Optional> findFirst(Predicate> filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry<>(); for(Node 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 @@ -1865,7 +1867,7 @@ public class Object2ShortAVLTreeMap extends AbstractObject2ShortMap implem } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -1877,15 +1879,15 @@ public class Object2ShortAVLTreeMap extends AbstractObject2ShortMap implem } state = operator.applyAsShort(state, entry.value); } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return (short)0; + if(filter.test(entry.value)) return OptionalShort.of(entry.value); + return OptionalShort.empty(); } @Override @@ -2201,7 +2203,7 @@ public class Object2ShortAVLTreeMap extends AbstractObject2ShortMap implem } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -2213,15 +2215,15 @@ public class Object2ShortAVLTreeMap extends AbstractObject2ShortMap implem } state = operator.applyAsShort(state, entry.value); } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return (short)0; + if(filter.test(entry.value)) return OptionalShort.of(entry.value); + return OptionalShort.empty(); } @Override @@ -2362,7 +2364,7 @@ public class Object2ShortAVLTreeMap extends AbstractObject2ShortMap implem } @Override - public Object2ShortMap.Entry reduce(ObjectObjectUnaryOperator, Object2ShortMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Object2ShortMap.Entry> operator) { Objects.requireNonNull(operator); Object2ShortMap.Entry state = null; boolean empty = true; @@ -2374,19 +2376,19 @@ public class Object2ShortAVLTreeMap extends AbstractObject2ShortMap implem } state = operator.apply(state, new BasicEntry<>(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Object2ShortMap.Entry findFirst(Predicate> filter) { + public Optional> findFirst(Predicate> filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry<>(); for(Node 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/main/java/speiger/src/collections/objects/maps/impl/tree/Object2ShortRBTreeMap.java b/src/main/java/speiger/src/collections/objects/maps/impl/tree/Object2ShortRBTreeMap.java index d6b647e..2936acc 100644 --- a/src/main/java/speiger/src/collections/objects/maps/impl/tree/Object2ShortRBTreeMap.java +++ b/src/main/java/speiger/src/collections/objects/maps/impl/tree/Object2ShortRBTreeMap.java @@ -4,6 +4,7 @@ import java.util.Collections; import java.util.Map; import java.util.Comparator; import java.util.Objects; +import java.util.Optional; import java.util.NoSuchElementException; import java.util.function.Consumer; import java.util.function.BiFunction; @@ -32,6 +33,7 @@ import speiger.src.collections.shorts.functions.ShortConsumer; import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.shorts.functions.function.ShortPredicate; +import speiger.src.collections.shorts.functions.OptionalShort; import speiger.src.collections.objects.collections.ObjectIterator; /** @@ -1151,7 +1153,7 @@ public class Object2ShortRBTreeMap extends AbstractObject2ShortMap impleme } @Override - public T reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); T state = null; boolean empty = true; @@ -1163,15 +1165,15 @@ public class Object2ShortRBTreeMap extends AbstractObject2ShortMap impleme } state = operator.apply(state, entry.key); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public T findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); for(Node entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.test(entry.key)) return entry.key; - return null; + if(filter.test(entry.key)) return Optional.ofNullable(entry.key); + return Optional.empty(); } @Override @@ -1810,7 +1812,7 @@ public class Object2ShortRBTreeMap extends AbstractObject2ShortMap impleme } @Override - public Object2ShortMap.Entry reduce(ObjectObjectUnaryOperator, Object2ShortMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Object2ShortMap.Entry> operator) { Objects.requireNonNull(operator); Object2ShortMap.Entry state = null; boolean empty = true; @@ -1822,19 +1824,19 @@ public class Object2ShortRBTreeMap extends AbstractObject2ShortMap impleme } state = operator.apply(state, new BasicEntry<>(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Object2ShortMap.Entry findFirst(Predicate> filter) { + public Optional> findFirst(Predicate> filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry<>(); for(Node 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 @@ -1929,7 +1931,7 @@ public class Object2ShortRBTreeMap extends AbstractObject2ShortMap impleme } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -1941,15 +1943,15 @@ public class Object2ShortRBTreeMap extends AbstractObject2ShortMap impleme } state = operator.applyAsShort(state, entry.value); } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return (short)0; + if(filter.test(entry.value)) return OptionalShort.of(entry.value); + return OptionalShort.empty(); } @Override @@ -2265,7 +2267,7 @@ public class Object2ShortRBTreeMap extends AbstractObject2ShortMap impleme } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -2277,15 +2279,15 @@ public class Object2ShortRBTreeMap extends AbstractObject2ShortMap impleme } state = operator.applyAsShort(state, entry.value); } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return (short)0; + if(filter.test(entry.value)) return OptionalShort.of(entry.value); + return OptionalShort.empty(); } @Override @@ -2426,7 +2428,7 @@ public class Object2ShortRBTreeMap extends AbstractObject2ShortMap impleme } @Override - public Object2ShortMap.Entry reduce(ObjectObjectUnaryOperator, Object2ShortMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Object2ShortMap.Entry> operator) { Objects.requireNonNull(operator); Object2ShortMap.Entry state = null; boolean empty = true; @@ -2438,19 +2440,19 @@ public class Object2ShortRBTreeMap extends AbstractObject2ShortMap impleme } state = operator.apply(state, new BasicEntry<>(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Object2ShortMap.Entry findFirst(Predicate> filter) { + public Optional> findFirst(Predicate> filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry<>(); for(Node 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/main/java/speiger/src/collections/objects/queues/ObjectArrayFIFOQueue.java b/src/main/java/speiger/src/collections/objects/queues/ObjectArrayFIFOQueue.java index c72376f..b52aff2 100644 --- a/src/main/java/speiger/src/collections/objects/queues/ObjectArrayFIFOQueue.java +++ b/src/main/java/speiger/src/collections/objects/queues/ObjectArrayFIFOQueue.java @@ -5,6 +5,7 @@ import java.util.Comparator; import java.util.function.Consumer; import java.util.function.BiFunction; import java.util.Objects; +import java.util.Optional; import java.util.NoSuchElementException; import java.util.function.Predicate; @@ -289,17 +290,17 @@ public class ObjectArrayFIFOQueue extends AbstractObjectPriorityQueue impl } @Override - public T findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); for(int i = 0,m=size();i extends AbstractObjectPriorityQueue impl } @Override - public T reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); T state = null; boolean empty = true; @@ -325,7 +326,7 @@ public class ObjectArrayFIFOQueue extends AbstractObjectPriorityQueue impl } state = operator.apply(state, array[(first + i) % array.length]); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override diff --git a/src/main/java/speiger/src/collections/objects/queues/ObjectArrayPriorityQueue.java b/src/main/java/speiger/src/collections/objects/queues/ObjectArrayPriorityQueue.java index 675a996..f2d17bb 100644 --- a/src/main/java/speiger/src/collections/objects/queues/ObjectArrayPriorityQueue.java +++ b/src/main/java/speiger/src/collections/objects/queues/ObjectArrayPriorityQueue.java @@ -6,6 +6,7 @@ import java.util.Comparator; import java.util.function.Consumer; import java.util.function.BiFunction; import java.util.Objects; +import java.util.Optional; import java.util.function.Predicate; import speiger.src.collections.objects.collections.ObjectCollection; @@ -316,7 +317,7 @@ public class ObjectArrayPriorityQueue extends AbstractObjectPriorityQueue } @Override - public T reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); T state = null; boolean empty = true; @@ -328,20 +329,20 @@ public class ObjectArrayPriorityQueue extends AbstractObjectPriorityQueue } state = operator.apply(state, array[i]); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public T findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); for(int i = 0;i extends AbstractObjectPriorityQueue } @Override - public T reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); T state = null; boolean empty = true; @@ -310,20 +311,20 @@ public class ObjectHeapPriorityQueue extends AbstractObjectPriorityQueue } state = operator.apply(state, array[i]); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public T findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); for(int i = 0;i extends AbstractObjectCollection i public ObjectBidirectionalIterator reverseIterator() { return set.iterator(); } - + + @Override + public void addFirst(T o) { + set.addLast(o); + } + + @Override + public void addLast(T o) { + set.addFirst(o); + } + @Override public boolean remove(Object o) { return set.remove(o); diff --git a/src/main/java/speiger/src/collections/objects/sets/ImmutableObjectOpenHashSet.java b/src/main/java/speiger/src/collections/objects/sets/ImmutableObjectOpenHashSet.java index ec9949b..808e0de 100644 --- a/src/main/java/speiger/src/collections/objects/sets/ImmutableObjectOpenHashSet.java +++ b/src/main/java/speiger/src/collections/objects/sets/ImmutableObjectOpenHashSet.java @@ -7,6 +7,7 @@ import java.util.Collection; import java.util.Iterator; import java.util.NoSuchElementException; import java.util.Objects; +import java.util.Optional; import java.util.function.Predicate; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; @@ -236,6 +237,10 @@ public class ImmutableObjectOpenHashSet extends AbstractObjectSet implemen @Override public boolean addAll(ObjectCollection c) { throw new UnsupportedOperationException(); } @Override + public void addFirst(T o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(T o) { throw new UnsupportedOperationException(); } + @Override public boolean addAndMoveToFirst(T o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToLast(T o) { throw new UnsupportedOperationException(); } @@ -354,7 +359,7 @@ public class ImmutableObjectOpenHashSet extends AbstractObjectSet implemen } @Override - public T reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); T state = null; boolean empty = true; @@ -367,18 +372,18 @@ public class ImmutableObjectOpenHashSet extends AbstractObjectSet implemen else state = operator.apply(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public T findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); int index = firstIndex; while(index != -1) { - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return Optional.ofNullable(keys[index]); index = (int)links[index]; } - return null; + return Optional.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/objects/sets/ObjectAVLTreeSet.java b/src/main/java/speiger/src/collections/objects/sets/ObjectAVLTreeSet.java index d6725e0..0a0ebd3 100644 --- a/src/main/java/speiger/src/collections/objects/sets/ObjectAVLTreeSet.java +++ b/src/main/java/speiger/src/collections/objects/sets/ObjectAVLTreeSet.java @@ -8,6 +8,7 @@ import java.util.function.BiFunction; import java.util.Iterator; import java.util.NoSuchElementException; import java.util.Objects; +import java.util.Optional; import java.util.function.Predicate; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; @@ -333,12 +334,12 @@ public class ObjectAVLTreeSet extends AbstractObjectSet implements ObjectN } @Override - public T findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); for(Entry entry = first;entry != null;entry = entry.next()) { - if(filter.test(entry.key)) return entry.key; + if(filter.test(entry.key)) return Optional.ofNullable(entry.key); } - return null; + return Optional.empty(); } @Override @@ -352,7 +353,7 @@ public class ObjectAVLTreeSet extends AbstractObjectSet implements ObjectN } @Override - public T reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); T state = null; boolean empty = true; @@ -364,7 +365,7 @@ public class ObjectAVLTreeSet extends AbstractObjectSet implements ObjectN } state = operator.apply(state, entry.key); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override @@ -1117,7 +1118,7 @@ public class ObjectAVLTreeSet extends AbstractObjectSet implements ObjectN } @Override - public T reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); T state = null; boolean empty = true; @@ -1129,16 +1130,16 @@ public class ObjectAVLTreeSet extends AbstractObjectSet implements ObjectN } state = operator.apply(state, entry.key); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public T findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); for(Entry 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.ofNullable(entry.key); } - return null; + return Optional.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/objects/sets/ObjectArraySet.java b/src/main/java/speiger/src/collections/objects/sets/ObjectArraySet.java index b5c9c6f..580e062 100644 --- a/src/main/java/speiger/src/collections/objects/sets/ObjectArraySet.java +++ b/src/main/java/speiger/src/collections/objects/sets/ObjectArraySet.java @@ -6,12 +6,12 @@ import java.util.function.Consumer; import java.util.function.BiFunction; import java.util.NoSuchElementException; import java.util.Objects; +import java.util.Optional; import java.util.Set; import java.util.function.Predicate; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.objects.collections.ObjectCollection; import speiger.src.collections.objects.collections.ObjectIterator; - import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOperator; @@ -130,6 +130,25 @@ public class ObjectArraySet extends AbstractObjectSet implements ObjectOrd return o; } + @Override + public void addFirst(T o) { + int index = findIndex(o); + if(index == -1) { + if(data.length == size) data = Arrays.copyOf(data, size == 0 ? 2 : size * 2); + System.arraycopy(data, 0, data, 1, size++); + data[0] = o; + } + } + + @Override + public void addLast(T o) { + int index = findIndex(o); + if(index == -1) { + if(data.length == size) data = Arrays.copyOf(data, size == 0 ? 2 : size * 2); + data[size++] = o; + } + } + @Override public boolean addAndMoveToFirst(T o) { int index = findIndex(o); @@ -358,12 +377,12 @@ public class ObjectArraySet extends AbstractObjectSet implements ObjectOrd } @Override - public T findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); for(int i = 0;i extends AbstractObjectSet implements ObjectOrd } @Override - public T reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); T state = null; boolean empty = true; @@ -389,7 +408,7 @@ public class ObjectArraySet extends AbstractObjectSet implements ObjectOrd } state = operator.apply(state, data[i]); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override diff --git a/src/main/java/speiger/src/collections/objects/sets/ObjectLinkedOpenCustomHashSet.java b/src/main/java/speiger/src/collections/objects/sets/ObjectLinkedOpenCustomHashSet.java index 6835972..2cd42c7 100644 --- a/src/main/java/speiger/src/collections/objects/sets/ObjectLinkedOpenCustomHashSet.java +++ b/src/main/java/speiger/src/collections/objects/sets/ObjectLinkedOpenCustomHashSet.java @@ -7,6 +7,7 @@ import java.util.Objects; import java.util.Collection; import java.util.Iterator; import java.util.NoSuchElementException; +import java.util.Optional; import java.util.function.Predicate; import speiger.src.collections.objects.collections.ObjectCollection; @@ -190,6 +191,46 @@ public class ObjectLinkedOpenCustomHashSet extends ObjectOpenCustomHashSet while(iterator.hasNext()) add(iterator.next()); } + @Override + public void addFirst(T o) { + if(strategy.equals(o, null)) { + if(containsNull) return; + containsNull = true; + onNodeAdded(nullIndex); + moveToFirstIndex(nullIndex); + } + else { + int pos = HashUtil.mix(strategy.hashCode(o)) & mask; + while(!strategy.equals(keys[pos], null)) { + if(strategy.equals(keys[pos], o)) return; + pos = ++pos & mask; + } + keys[pos] = o; + onNodeAdded(pos); + moveToFirstIndex(pos); + } + if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor)); + } + + @Override + public void addLast(T o) { + if(strategy.equals(o, null)) { + if(containsNull) return; + containsNull = true; + onNodeAdded(nullIndex); + } + else { + int pos = HashUtil.mix(strategy.hashCode(o)) & mask; + while(!strategy.equals(keys[pos], null)) { + if(strategy.equals(keys[pos], o)) return; + pos = ++pos & mask; + } + keys[pos] = o; + onNodeAdded(pos); + } + if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor)); + } + @Override public boolean addAndMoveToFirst(T o) { if(strategy.equals(o, null)) { @@ -583,7 +624,7 @@ public class ObjectLinkedOpenCustomHashSet extends ObjectOpenCustomHashSet } @Override - public T reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); T state = null; boolean empty = true; @@ -596,18 +637,18 @@ public class ObjectLinkedOpenCustomHashSet extends ObjectOpenCustomHashSet else state = operator.apply(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public T findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); int index = firstIndex; while(index != -1) { - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return Optional.ofNullable(keys[index]); index = (int)links[index]; } - return null; + return Optional.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/objects/sets/ObjectLinkedOpenHashSet.java b/src/main/java/speiger/src/collections/objects/sets/ObjectLinkedOpenHashSet.java index 19103cd..fd988ee 100644 --- a/src/main/java/speiger/src/collections/objects/sets/ObjectLinkedOpenHashSet.java +++ b/src/main/java/speiger/src/collections/objects/sets/ObjectLinkedOpenHashSet.java @@ -5,6 +5,7 @@ import java.util.Collection; import java.util.Iterator; 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; @@ -163,6 +164,46 @@ public class ObjectLinkedOpenHashSet extends ObjectOpenHashSet implements while(iterator.hasNext()) add(iterator.next()); } + @Override + public void addFirst(T o) { + if(o == null) { + if(containsNull) return; + containsNull = true; + onNodeAdded(nullIndex); + moveToFirstIndex(nullIndex); + } + else { + int pos = HashUtil.mix(Objects.hashCode(o)) & mask; + while(keys[pos] != null) { + if(Objects.equals(keys[pos], o)) return; + pos = ++pos & mask; + } + keys[pos] = o; + onNodeAdded(pos); + moveToFirstIndex(pos); + } + if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor)); + } + + @Override + public void addLast(T o) { + if(o == null) { + if(containsNull) return; + containsNull = true; + onNodeAdded(nullIndex); + } + else { + int pos = HashUtil.mix(Objects.hashCode(o)) & mask; + while(keys[pos] != null) { + if(Objects.equals(keys[pos], o)) return; + pos = ++pos & mask; + } + keys[pos] = o; + onNodeAdded(pos); + } + if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor)); + } + @Override public boolean addAndMoveToFirst(T o) { if(o == null) { @@ -440,7 +481,7 @@ public class ObjectLinkedOpenHashSet extends ObjectOpenHashSet implements } @Override - public T reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); T state = null; boolean empty = true; @@ -453,18 +494,18 @@ public class ObjectLinkedOpenHashSet extends ObjectOpenHashSet implements else state = operator.apply(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public T findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); int index = firstIndex; while(index != -1) { - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return Optional.ofNullable(keys[index]); index = (int)links[index]; } - return null; + return Optional.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/objects/sets/ObjectOpenCustomHashSet.java b/src/main/java/speiger/src/collections/objects/sets/ObjectOpenCustomHashSet.java index dd0468b..7115958 100644 --- a/src/main/java/speiger/src/collections/objects/sets/ObjectOpenCustomHashSet.java +++ b/src/main/java/speiger/src/collections/objects/sets/ObjectOpenCustomHashSet.java @@ -6,6 +6,7 @@ import java.util.ConcurrentModificationException; import java.util.Iterator; 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; @@ -532,7 +533,7 @@ public class ObjectOpenCustomHashSet extends AbstractObjectSet implements } @Override - public T reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); T state = null; boolean empty = true; @@ -549,18 +550,18 @@ public class ObjectOpenCustomHashSet extends AbstractObjectSet implements } state = operator.apply(state, keys[i]); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public T findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return Optional.empty(); + if(containsNull && filter.test(keys[nullIndex])) return Optional.ofNullable(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], null) && filter.test(keys[i])) return keys[i]; + if(!strategy.equals(keys[i], null) && filter.test(keys[i])) return Optional.ofNullable(keys[i]); } - return null; + return Optional.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/objects/sets/ObjectOpenHashSet.java b/src/main/java/speiger/src/collections/objects/sets/ObjectOpenHashSet.java index 95b461c..da3e31b 100644 --- a/src/main/java/speiger/src/collections/objects/sets/ObjectOpenHashSet.java +++ b/src/main/java/speiger/src/collections/objects/sets/ObjectOpenHashSet.java @@ -6,6 +6,7 @@ import java.util.ConcurrentModificationException; import java.util.Iterator; 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; @@ -390,7 +391,7 @@ public class ObjectOpenHashSet extends AbstractObjectSet implements ITrimm } @Override - public T reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); T state = null; boolean empty = true; @@ -407,18 +408,18 @@ public class ObjectOpenHashSet extends AbstractObjectSet implements ITrimm } state = operator.apply(state, keys[i]); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public T findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return Optional.empty(); + if(containsNull && filter.test(keys[nullIndex])) return Optional.ofNullable(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(keys[i] != null && filter.test(keys[i])) return keys[i]; + if(keys[i] != null && filter.test(keys[i])) return Optional.ofNullable(keys[i]); } - return null; + return Optional.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/objects/sets/ObjectOrderedSet.java b/src/main/java/speiger/src/collections/objects/sets/ObjectOrderedSet.java index 8d816a2..e745311 100644 --- a/src/main/java/speiger/src/collections/objects/sets/ObjectOrderedSet.java +++ b/src/main/java/speiger/src/collections/objects/sets/ObjectOrderedSet.java @@ -1,6 +1,7 @@ package speiger.src.collections.objects.sets; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; +import speiger.src.collections.objects.collections.ObjectOrderedCollection; import speiger.src.collections.objects.collections.ObjectSplititerator; import speiger.src.collections.objects.utils.ObjectSplititerators; import speiger.src.collections.objects.sets.AbstractObjectSet.ReversedObjectOrderedSet; @@ -14,7 +15,7 @@ import speiger.src.collections.objects.utils.ObjectSets; * * @param the keyType of elements maintained by this Collection */ -public interface ObjectOrderedSet extends ObjectSet +public interface ObjectOrderedSet extends ObjectSet, ObjectOrderedCollection { /** * A customized add method that allows you to insert into the first index. diff --git a/src/main/java/speiger/src/collections/objects/sets/ObjectRBTreeSet.java b/src/main/java/speiger/src/collections/objects/sets/ObjectRBTreeSet.java index b8520f0..6f9ad61 100644 --- a/src/main/java/speiger/src/collections/objects/sets/ObjectRBTreeSet.java +++ b/src/main/java/speiger/src/collections/objects/sets/ObjectRBTreeSet.java @@ -2,6 +2,7 @@ package speiger.src.collections.objects.sets; import java.util.Collection; import java.util.Collections; +import java.util.Optional; import java.util.Comparator; import java.util.function.Consumer; import java.util.function.BiFunction; @@ -342,7 +343,7 @@ public class ObjectRBTreeSet extends AbstractObjectSet implements ObjectNa } @Override - public T reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); T state = null; boolean empty = true; @@ -354,16 +355,16 @@ public class ObjectRBTreeSet extends AbstractObjectSet implements ObjectNa } state = operator.apply(state, entry.key); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public T findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); for(Entry entry = first;entry != null;entry = entry.next()) { - if(filter.test(entry.key)) return entry.key; + if(filter.test(entry.key)) return Optional.ofNullable(entry.key); } - return null; + return Optional.empty(); } @Override @@ -1177,7 +1178,7 @@ public class ObjectRBTreeSet extends AbstractObjectSet implements ObjectNa } @Override - public T reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); T state = null; boolean empty = true; @@ -1189,16 +1190,16 @@ public class ObjectRBTreeSet extends AbstractObjectSet implements ObjectNa } state = operator.apply(state, entry.key); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public T findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); for(Entry 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.ofNullable(entry.key); } - return null; + return Optional.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/objects/utils/ObjectCollections.java b/src/main/java/speiger/src/collections/objects/utils/ObjectCollections.java index 0dd2171..b3e940a 100644 --- a/src/main/java/speiger/src/collections/objects/utils/ObjectCollections.java +++ b/src/main/java/speiger/src/collections/objects/utils/ObjectCollections.java @@ -6,6 +6,7 @@ import java.util.ConcurrentModificationException; import java.util.Iterator; import java.util.NoSuchElementException; import java.util.Objects; +import java.util.Optional; import java.util.Comparator; import java.util.function.BiFunction; import java.util.function.Predicate; @@ -709,9 +710,9 @@ public class ObjectCollections public boolean matchesAll(Predicate filter) { synchronized(mutex) { return c.matchesAll(filter); } } public E reduce(E identity, BiFunction operator) { synchronized(mutex) { return c.reduce(identity, operator); } } @Override - public T reduce(ObjectObjectUnaryOperator operator) { synchronized(mutex) { return c.reduce(operator); } } + public Optional reduce(ObjectObjectUnaryOperator operator) { synchronized(mutex) { return c.reduce(operator); } } @Override - public T findFirst(Predicate filter) { synchronized(mutex) { return c.findFirst(filter); } } + public Optional findFirst(Predicate filter) { synchronized(mutex) { return c.findFirst(filter); } } @Override public int count(Predicate filter) { synchronized(mutex) { return c.count(filter); } } } @@ -824,9 +825,9 @@ public class ObjectCollections public boolean matchesAll(Predicate filter) { return c.matchesAll(filter); } public E reduce(E identity, BiFunction operator) { return c.reduce(identity, operator); } @Override - public T reduce(ObjectObjectUnaryOperator operator) { return c.reduce(operator); } + public Optional reduce(ObjectObjectUnaryOperator operator) { return c.reduce(operator); } @Override - public T findFirst(Predicate filter) { return c.findFirst(filter); } + public Optional findFirst(Predicate filter) { return c.findFirst(filter); } @Override public int count(Predicate filter) { return c.count(filter); } } diff --git a/src/main/java/speiger/src/collections/objects/utils/ObjectPriorityQueues.java b/src/main/java/speiger/src/collections/objects/utils/ObjectPriorityQueues.java index 76e24e5..e86e4e9 100644 --- a/src/main/java/speiger/src/collections/objects/utils/ObjectPriorityQueues.java +++ b/src/main/java/speiger/src/collections/objects/utils/ObjectPriorityQueues.java @@ -3,12 +3,14 @@ package speiger.src.collections.objects.utils; import java.util.Collection; import java.util.Comparator; import java.util.function.Consumer; +import java.util.function.BiFunction; import java.util.function.Predicate; - +import java.util.Optional; import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.collections.ObjectCollection; import speiger.src.collections.objects.queues.ObjectPriorityDequeue; import speiger.src.collections.objects.queues.ObjectPriorityQueue; +import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOperator; import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; /** @@ -121,7 +123,10 @@ public class ObjectPriorityQueues @Override public boolean matchesAll(Predicate filter) { synchronized(mutex) { return queue.matchesAll(filter); } } @Override - public T findFirst(Predicate filter) { synchronized(mutex) { return queue.findFirst(filter); } } + public Optional findFirst(Predicate filter) { synchronized(mutex) { return queue.findFirst(filter); } } + public E reduce(E identity, BiFunction operator) { synchronized(mutex) { return queue.reduce(identity, operator); } } + @Override + public Optional reduce(ObjectObjectUnaryOperator operator) { synchronized(mutex) { return queue.reduce(operator); } } @Override public int count(Predicate filter) { synchronized(mutex) { return queue.count(filter); } } } diff --git a/src/main/java/speiger/src/collections/objects/utils/ObjectSets.java b/src/main/java/speiger/src/collections/objects/utils/ObjectSets.java index 5219dd9..76a8854 100644 --- a/src/main/java/speiger/src/collections/objects/utils/ObjectSets.java +++ b/src/main/java/speiger/src/collections/objects/utils/ObjectSets.java @@ -283,6 +283,10 @@ public class ObjectSets s = c; } + @Override + public void addFirst(T o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(T o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(T o) { throw new UnsupportedOperationException(); } @Override @@ -533,7 +537,11 @@ public class ObjectSets super(c, mutex); s = c; } - + + @Override + public void addFirst(T o) { synchronized(mutex) { s.addFirst(o); } } + @Override + public void addLast(T o) { synchronized(mutex) { s.addLast(o); } } @Override public boolean addAndMoveToFirst(T o) { synchronized(mutex) { return s.addAndMoveToFirst(o); } } @Override diff --git a/src/main/java/speiger/src/collections/objects/utils/maps/Object2BooleanMaps.java b/src/main/java/speiger/src/collections/objects/utils/maps/Object2BooleanMaps.java index fa5b0be..2d195b9 100644 --- a/src/main/java/speiger/src/collections/objects/utils/maps/Object2BooleanMaps.java +++ b/src/main/java/speiger/src/collections/objects/utils/maps/Object2BooleanMaps.java @@ -621,7 +621,11 @@ public class Object2BooleanMaps super(c); set = c; } - + + @Override + public void addFirst(Object2BooleanMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Object2BooleanMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Object2BooleanMap.Entry o) { throw new UnsupportedOperationException(); } @Override diff --git a/src/main/java/speiger/src/collections/objects/utils/maps/Object2ByteMaps.java b/src/main/java/speiger/src/collections/objects/utils/maps/Object2ByteMaps.java index ffb55aa..2e6221f 100644 --- a/src/main/java/speiger/src/collections/objects/utils/maps/Object2ByteMaps.java +++ b/src/main/java/speiger/src/collections/objects/utils/maps/Object2ByteMaps.java @@ -633,7 +633,11 @@ public class Object2ByteMaps super(c); set = c; } - + + @Override + public void addFirst(Object2ByteMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Object2ByteMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Object2ByteMap.Entry o) { throw new UnsupportedOperationException(); } @Override diff --git a/src/main/java/speiger/src/collections/objects/utils/maps/Object2CharMaps.java b/src/main/java/speiger/src/collections/objects/utils/maps/Object2CharMaps.java index eb35cf8..7b705b8 100644 --- a/src/main/java/speiger/src/collections/objects/utils/maps/Object2CharMaps.java +++ b/src/main/java/speiger/src/collections/objects/utils/maps/Object2CharMaps.java @@ -633,7 +633,11 @@ public class Object2CharMaps super(c); set = c; } - + + @Override + public void addFirst(Object2CharMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Object2CharMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Object2CharMap.Entry o) { throw new UnsupportedOperationException(); } @Override diff --git a/src/main/java/speiger/src/collections/objects/utils/maps/Object2DoubleMaps.java b/src/main/java/speiger/src/collections/objects/utils/maps/Object2DoubleMaps.java index ad7a0b4..b393ac1 100644 --- a/src/main/java/speiger/src/collections/objects/utils/maps/Object2DoubleMaps.java +++ b/src/main/java/speiger/src/collections/objects/utils/maps/Object2DoubleMaps.java @@ -633,7 +633,11 @@ public class Object2DoubleMaps super(c); set = c; } - + + @Override + public void addFirst(Object2DoubleMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Object2DoubleMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Object2DoubleMap.Entry o) { throw new UnsupportedOperationException(); } @Override diff --git a/src/main/java/speiger/src/collections/objects/utils/maps/Object2FloatMaps.java b/src/main/java/speiger/src/collections/objects/utils/maps/Object2FloatMaps.java index 0156c2e..9a4b182 100644 --- a/src/main/java/speiger/src/collections/objects/utils/maps/Object2FloatMaps.java +++ b/src/main/java/speiger/src/collections/objects/utils/maps/Object2FloatMaps.java @@ -633,7 +633,11 @@ public class Object2FloatMaps super(c); set = c; } - + + @Override + public void addFirst(Object2FloatMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Object2FloatMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Object2FloatMap.Entry o) { throw new UnsupportedOperationException(); } @Override diff --git a/src/main/java/speiger/src/collections/objects/utils/maps/Object2IntMaps.java b/src/main/java/speiger/src/collections/objects/utils/maps/Object2IntMaps.java index ffdd514..2ec6991 100644 --- a/src/main/java/speiger/src/collections/objects/utils/maps/Object2IntMaps.java +++ b/src/main/java/speiger/src/collections/objects/utils/maps/Object2IntMaps.java @@ -633,7 +633,11 @@ public class Object2IntMaps super(c); set = c; } - + + @Override + public void addFirst(Object2IntMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Object2IntMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Object2IntMap.Entry o) { throw new UnsupportedOperationException(); } @Override diff --git a/src/main/java/speiger/src/collections/objects/utils/maps/Object2LongMaps.java b/src/main/java/speiger/src/collections/objects/utils/maps/Object2LongMaps.java index 6010ce4..30c9d9e 100644 --- a/src/main/java/speiger/src/collections/objects/utils/maps/Object2LongMaps.java +++ b/src/main/java/speiger/src/collections/objects/utils/maps/Object2LongMaps.java @@ -633,7 +633,11 @@ public class Object2LongMaps super(c); set = c; } - + + @Override + public void addFirst(Object2LongMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Object2LongMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Object2LongMap.Entry o) { throw new UnsupportedOperationException(); } @Override diff --git a/src/main/java/speiger/src/collections/objects/utils/maps/Object2ObjectMaps.java b/src/main/java/speiger/src/collections/objects/utils/maps/Object2ObjectMaps.java index 7faff62..68d2081 100644 --- a/src/main/java/speiger/src/collections/objects/utils/maps/Object2ObjectMaps.java +++ b/src/main/java/speiger/src/collections/objects/utils/maps/Object2ObjectMaps.java @@ -611,7 +611,11 @@ public class Object2ObjectMaps super(c); set = c; } - + + @Override + public void addFirst(Object2ObjectMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Object2ObjectMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Object2ObjectMap.Entry o) { throw new UnsupportedOperationException(); } @Override diff --git a/src/main/java/speiger/src/collections/objects/utils/maps/Object2ShortMaps.java b/src/main/java/speiger/src/collections/objects/utils/maps/Object2ShortMaps.java index cfc7517..230ffd2 100644 --- a/src/main/java/speiger/src/collections/objects/utils/maps/Object2ShortMaps.java +++ b/src/main/java/speiger/src/collections/objects/utils/maps/Object2ShortMaps.java @@ -633,7 +633,11 @@ public class Object2ShortMaps super(c); set = c; } - + + @Override + public void addFirst(Object2ShortMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Object2ShortMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Object2ShortMap.Entry o) { throw new UnsupportedOperationException(); } @Override diff --git a/src/main/java/speiger/src/collections/shorts/collections/ShortIterable.java b/src/main/java/speiger/src/collections/shorts/collections/ShortIterable.java index 2c64cdc..b5eff54 100644 --- a/src/main/java/speiger/src/collections/shorts/collections/ShortIterable.java +++ b/src/main/java/speiger/src/collections/shorts/collections/ShortIterable.java @@ -7,6 +7,7 @@ import java.util.function.Consumer; import speiger.src.collections.shorts.functions.ShortConsumer; import speiger.src.collections.shorts.functions.ShortComparator; import speiger.src.collections.objects.collections.ObjectIterable; +import speiger.src.collections.shorts.functions.OptionalShort; import speiger.src.collections.shorts.functions.function.ShortFunction; import speiger.src.collections.ints.functions.consumer.IntShortConsumer; import speiger.src.collections.objects.functions.consumer.ObjectShortConsumer; @@ -281,13 +282,13 @@ public interface ShortIterable extends Iterable * @param filter that should be applied * @return the found value or the null equivalent variant. */ - default short findFirst(ShortPredicate filter) { + default OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); for(ShortIterator iter = iterator();iter.hasNext();) { short entry = iter.nextShort(); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return OptionalShort.of(entry); } - return (short)0; + return OptionalShort.empty(); } /** @@ -312,7 +313,7 @@ public interface ShortIterable extends Iterable * @param operator the operation that should be applied * @return the reduction result, returns null value if nothing was found */ - default short reduce(ShortShortUnaryOperator operator) { + default OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -324,7 +325,7 @@ public interface ShortIterable extends Iterable } state = operator.applyAsShort(state, iter.nextShort()); } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } /** diff --git a/src/main/java/speiger/src/collections/shorts/lists/CopyOnWriteShortArrayList.java b/src/main/java/speiger/src/collections/shorts/lists/CopyOnWriteShortArrayList.java index f1691b9..93a9cc6 100644 --- a/src/main/java/speiger/src/collections/shorts/lists/CopyOnWriteShortArrayList.java +++ b/src/main/java/speiger/src/collections/shorts/lists/CopyOnWriteShortArrayList.java @@ -16,6 +16,7 @@ import java.nio.ShortBuffer; import speiger.src.collections.shorts.collections.ShortCollection; import speiger.src.collections.shorts.collections.ShortStack; import speiger.src.collections.shorts.collections.ShortIterator; +import speiger.src.collections.shorts.functions.OptionalShort; import speiger.src.collections.shorts.functions.ShortComparator; import speiger.src.collections.shorts.functions.ShortConsumer; import speiger.src.collections.objects.functions.consumer.ObjectShortConsumer; @@ -654,13 +655,13 @@ public class CopyOnWriteShortArrayList extends AbstractShortList implements ITri } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); short[] data = this.data; for(int i = 0,m=data.length;i operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Short2BooleanMap.Entry state = null; boolean empty = true; @@ -779,11 +782,11 @@ public class Short2BooleanConcurrentOpenHashMap extends AbstractShort2BooleanMap seg.unlockRead(stamp); } } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Short2BooleanMap.Entry 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); Short2ByteMap.Entry state = null; boolean empty = true; @@ -792,11 +795,11 @@ public class Short2ByteConcurrentOpenHashMap extends AbstractShort2ByteMap imple seg.unlockRead(stamp); } } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Short2ByteMap.Entry 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); Short2CharMap.Entry state = null; boolean empty = true; @@ -792,11 +795,11 @@ public class Short2CharConcurrentOpenHashMap extends AbstractShort2CharMap imple seg.unlockRead(stamp); } } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Short2CharMap.Entry 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); Short2DoubleMap.Entry state = null; boolean empty = true; @@ -792,11 +795,11 @@ public class Short2DoubleConcurrentOpenHashMap extends AbstractShort2DoubleMap i seg.unlockRead(stamp); } } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Short2DoubleMap.Entry 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); Short2FloatMap.Entry state = null; boolean empty = true; @@ -792,11 +795,11 @@ public class Short2FloatConcurrentOpenHashMap extends AbstractShort2FloatMap imp seg.unlockRead(stamp); } } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Short2FloatMap.Entry 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); Short2IntMap.Entry state = null; boolean empty = true; @@ -792,11 +795,11 @@ public class Short2IntConcurrentOpenHashMap extends AbstractShort2IntMap impleme seg.unlockRead(stamp); } } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Short2IntMap.Entry 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); Short2LongMap.Entry state = null; boolean empty = true; @@ -792,11 +795,11 @@ public class Short2LongConcurrentOpenHashMap extends AbstractShort2LongMap imple seg.unlockRead(stamp); } } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Short2LongMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); MapEntry entry = new MapEntry(); for(int i = 0,m=segments.length;i extends AbstractShort2ObjectMa } @Override - public Short2ObjectMap.Entry reduce(ObjectObjectUnaryOperator, Short2ObjectMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Short2ObjectMap.Entry> operator) { Objects.requireNonNull(operator); Short2ObjectMap.Entry state = null; boolean empty = true; @@ -739,11 +741,11 @@ public class Short2ObjectConcurrentOpenHashMap extends AbstractShort2ObjectMa seg.unlockRead(stamp); } } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Short2ObjectMap.Entry findFirst(Predicate> filter) { + public Optional> findFirst(Predicate> filter) { Objects.requireNonNull(filter); MapEntry entry = new MapEntry(); for(int i = 0,m=segments.length;i extends AbstractShort2ObjectMa try { while(index != -1) { entry.set(index, i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); index = (int)seg.links[index]; } } @@ -761,7 +763,7 @@ public class Short2ObjectConcurrentOpenHashMap extends AbstractShort2ObjectMa seg.unlockRead(stamp); } } - return null; + return Optional.empty(); } @Override @@ -1022,7 +1024,7 @@ public class Short2ObjectConcurrentOpenHashMap extends AbstractShort2ObjectMa } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -1046,11 +1048,11 @@ public class Short2ObjectConcurrentOpenHashMap extends AbstractShort2ObjectMa seg.unlockRead(stamp); } } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); for(int i = 0,m=segments.length;i seg = segments[i]; @@ -1058,7 +1060,7 @@ public class Short2ObjectConcurrentOpenHashMap extends AbstractShort2ObjectMa try { int index = seg.firstIndex; while(index != -1){ - if(filter.test(seg.keys[index])) return seg.keys[index]; + if(filter.test(seg.keys[index])) return OptionalShort.of(seg.keys[index]); index = (int)seg.links[index]; } } @@ -1066,7 +1068,7 @@ public class Short2ObjectConcurrentOpenHashMap extends AbstractShort2ObjectMa seg.unlockRead(stamp); } } - return (short)0; + return OptionalShort.empty(); } @Override @@ -1258,7 +1260,7 @@ public class Short2ObjectConcurrentOpenHashMap extends AbstractShort2ObjectMa } @Override - public V reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); V state = null; boolean empty = true; @@ -1282,20 +1284,20 @@ public class Short2ObjectConcurrentOpenHashMap extends AbstractShort2ObjectMa seg.unlockRead(stamp); } } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public V findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); for(int i = 0,m=segments.length;i seg = segments[i]; long stamp = seg.readLock(); try { int index = seg.firstIndex; while(index != -1){ - if(filter.test(seg.values[index])) return seg.values[index]; + if(filter.test(seg.values[index])) return Optional.ofNullable(seg.values[index]); index = (int)seg.links[index]; } } @@ -1303,7 +1305,7 @@ public class Short2ObjectConcurrentOpenHashMap extends AbstractShort2ObjectMa seg.unlockRead(stamp); } } - return null; + return Optional.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/shorts/maps/impl/concurrent/Short2ShortConcurrentOpenHashMap.java b/src/main/java/speiger/src/collections/shorts/maps/impl/concurrent/Short2ShortConcurrentOpenHashMap.java index 22467b6..5a7cdad 100644 --- a/src/main/java/speiger/src/collections/shorts/maps/impl/concurrent/Short2ShortConcurrentOpenHashMap.java +++ b/src/main/java/speiger/src/collections/shorts/maps/impl/concurrent/Short2ShortConcurrentOpenHashMap.java @@ -4,6 +4,7 @@ import java.util.Arrays; import java.util.Map; import java.util.NoSuchElementException; import java.util.Objects; +import java.util.Optional; import java.util.concurrent.locks.StampedLock; import java.util.function.Consumer; import java.util.function.Predicate; @@ -18,6 +19,7 @@ import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.shorts.functions.consumer.ShortShortConsumer; import speiger.src.collections.shorts.functions.function.ShortUnaryOperator; import speiger.src.collections.shorts.functions.function.ShortShortUnaryOperator; +import speiger.src.collections.shorts.functions.OptionalShort; import speiger.src.collections.shorts.functions.function.ShortPredicate; import speiger.src.collections.shorts.maps.abstracts.AbstractShort2ShortMap; import speiger.src.collections.shorts.maps.interfaces.Short2ShortMap; @@ -761,7 +763,7 @@ public class Short2ShortConcurrentOpenHashMap extends AbstractShort2ShortMap imp } @Override - public Short2ShortMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Short2ShortMap.Entry state = null; boolean empty = true; @@ -785,11 +787,11 @@ public class Short2ShortConcurrentOpenHashMap extends AbstractShort2ShortMap imp seg.unlockRead(stamp); } } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Short2ShortMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); MapEntry entry = new MapEntry(); for(int i = 0,m=segments.length;i implements Short2BooleanOrderedMap.FastOrderedSet { + @Override + public void addFirst(Short2BooleanMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Short2BooleanMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Short2BooleanMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -836,7 +843,7 @@ public class Short2BooleanLinkedOpenCustomHashMap extends Short2BooleanOpenCusto } @Override - public Short2BooleanMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Short2BooleanMap.Entry state = null; boolean empty = true; @@ -851,21 +858,21 @@ public class Short2BooleanLinkedOpenCustomHashMap extends Short2BooleanOpenCusto state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Short2BooleanMap.Entry 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 @@ -944,10 +951,13 @@ public class Short2BooleanLinkedOpenCustomHashMap extends Short2BooleanOpenCusto public boolean add(short o) { throw new UnsupportedOperationException(); } - + + @Override + public void addFirst(short o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(short o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(short o) { throw new UnsupportedOperationException(); } - @Override public boolean addAndMoveToLast(short o) { throw new UnsupportedOperationException(); } @@ -1090,7 +1100,7 @@ public class Short2BooleanLinkedOpenCustomHashMap extends Short2BooleanOpenCusto } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -1105,19 +1115,19 @@ public class Short2BooleanLinkedOpenCustomHashMap extends Short2BooleanOpenCusto state = operator.applyAsShort(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (short)0; + if(size() <= 0) return OptionalShort.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalShort.of(keys[index]); index = (int)links[index]; } - return (short)0; + return OptionalShort.empty(); } @Override @@ -1252,7 +1262,7 @@ public class Short2BooleanLinkedOpenCustomHashMap extends Short2BooleanOpenCusto } @Override - public boolean reduce(BooleanBooleanUnaryOperator operator) { + public OptionalBoolean reduce(BooleanBooleanUnaryOperator operator) { Objects.requireNonNull(operator); boolean state = false; boolean empty = true; @@ -1267,19 +1277,19 @@ public class Short2BooleanLinkedOpenCustomHashMap extends Short2BooleanOpenCusto state = operator.applyAsBoolean(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalBoolean.empty() : OptionalBoolean.of(state); } @Override - public boolean findFirst(BooleanPredicate filter) { + public OptionalBoolean findFirst(BooleanPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return false; + if(size() <= 0) return OptionalBoolean.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalBoolean.of(values[index]); index = (int)links[index]; } - return false; + return OptionalBoolean.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/shorts/maps/impl/customHash/Short2BooleanOpenCustomHashMap.java b/src/main/java/speiger/src/collections/shorts/maps/impl/customHash/Short2BooleanOpenCustomHashMap.java index d4919f3..495b8b8 100644 --- a/src/main/java/speiger/src/collections/shorts/maps/impl/customHash/Short2BooleanOpenCustomHashMap.java +++ b/src/main/java/speiger/src/collections/shorts/maps/impl/customHash/Short2BooleanOpenCustomHashMap.java @@ -5,6 +5,7 @@ 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; @@ -19,6 +20,7 @@ import speiger.src.collections.shorts.functions.consumer.ShortBooleanConsumer; import speiger.src.collections.shorts.functions.function.ShortPredicate; import speiger.src.collections.shorts.functions.function.ShortBooleanUnaryOperator; import speiger.src.collections.shorts.functions.function.ShortShortUnaryOperator; +import speiger.src.collections.shorts.functions.OptionalShort; import speiger.src.collections.shorts.maps.abstracts.AbstractShort2BooleanMap; import speiger.src.collections.shorts.maps.interfaces.Short2BooleanMap; import speiger.src.collections.shorts.sets.AbstractShortSet; @@ -36,6 +38,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectBooleanConsumer; import speiger.src.collections.booleans.functions.function.BooleanPredicate; +import speiger.src.collections.booleans.functions.OptionalBoolean; import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.sets.AbstractObjectSet; import speiger.src.collections.objects.sets.ObjectSet; @@ -930,7 +933,7 @@ public class Short2BooleanOpenCustomHashMap extends AbstractShort2BooleanMap imp } @Override - public Short2BooleanMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Short2BooleanMap.Entry state = null; boolean empty = true; @@ -947,25 +950,25 @@ public class Short2BooleanOpenCustomHashMap extends AbstractShort2BooleanMap imp } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Short2BooleanMap.Entry 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], (short)0)) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1139,7 +1142,7 @@ public class Short2BooleanOpenCustomHashMap extends AbstractShort2BooleanMap imp } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -1156,18 +1159,18 @@ public class Short2BooleanOpenCustomHashMap extends AbstractShort2BooleanMap imp } state = operator.applyAsShort(state, keys[i]); } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (short)0; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OptionalShort.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OptionalShort.of(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], (short)0) && filter.test(keys[i])) return keys[i]; + if(!strategy.equals(keys[i], (short)0) && filter.test(keys[i])) return OptionalShort.of(keys[i]); } - return (short)0; + return OptionalShort.empty(); } @Override @@ -1282,7 +1285,7 @@ public class Short2BooleanOpenCustomHashMap extends AbstractShort2BooleanMap imp } @Override - public boolean reduce(BooleanBooleanUnaryOperator operator) { + public OptionalBoolean reduce(BooleanBooleanUnaryOperator operator) { Objects.requireNonNull(operator); boolean state = false; boolean empty = true; @@ -1299,18 +1302,18 @@ public class Short2BooleanOpenCustomHashMap extends AbstractShort2BooleanMap imp } state = operator.applyAsBoolean(state, values[i]); } - return state; + return empty ? OptionalBoolean.empty() : OptionalBoolean.of(state); } @Override - public boolean findFirst(BooleanPredicate filter) { + public OptionalBoolean findFirst(BooleanPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return false; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return OptionalBoolean.empty(); + if(containsNull && filter.test(values[nullIndex])) return OptionalBoolean.of(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], (short)0) && filter.test(values[i])) return values[i]; + if(!strategy.equals(keys[i], (short)0) && filter.test(values[i])) return OptionalBoolean.of(values[i]); } - return false; + return OptionalBoolean.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/shorts/maps/impl/customHash/Short2ByteLinkedOpenCustomHashMap.java b/src/main/java/speiger/src/collections/shorts/maps/impl/customHash/Short2ByteLinkedOpenCustomHashMap.java index 55080df..4fe7585 100644 --- a/src/main/java/speiger/src/collections/shorts/maps/impl/customHash/Short2ByteLinkedOpenCustomHashMap.java +++ b/src/main/java/speiger/src/collections/shorts/maps/impl/customHash/Short2ByteLinkedOpenCustomHashMap.java @@ -4,6 +4,7 @@ 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; @@ -14,6 +15,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectShortConsumer; import speiger.src.collections.ints.functions.consumer.IntShortConsumer; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntByteConsumer; +import speiger.src.collections.shorts.functions.OptionalShort; import speiger.src.collections.shorts.functions.function.ShortPredicate; import speiger.src.collections.shorts.functions.consumer.ShortByteConsumer; import speiger.src.collections.shorts.functions.function.ShortShortUnaryOperator; @@ -33,6 +35,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectByteConsumer; import speiger.src.collections.bytes.functions.function.BytePredicate; +import speiger.src.collections.bytes.functions.OptionalByte; import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOperator; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.objects.lists.ObjectListIterator; @@ -671,6 +674,10 @@ public class Short2ByteLinkedOpenCustomHashMap extends Short2ByteOpenCustomHashM } private class MapEntrySet extends AbstractObjectSet implements Short2ByteOrderedMap.FastOrderedSet { + @Override + public void addFirst(Short2ByteMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Short2ByteMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Short2ByteMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -836,7 +843,7 @@ public class Short2ByteLinkedOpenCustomHashMap extends Short2ByteOpenCustomHashM } @Override - public Short2ByteMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Short2ByteMap.Entry state = null; boolean empty = true; @@ -851,21 +858,21 @@ public class Short2ByteLinkedOpenCustomHashMap extends Short2ByteOpenCustomHashM state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Short2ByteMap.Entry 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 @@ -944,10 +951,13 @@ public class Short2ByteLinkedOpenCustomHashMap extends Short2ByteOpenCustomHashM public boolean add(short o) { throw new UnsupportedOperationException(); } - + + @Override + public void addFirst(short o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(short o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(short o) { throw new UnsupportedOperationException(); } - @Override public boolean addAndMoveToLast(short o) { throw new UnsupportedOperationException(); } @@ -1090,7 +1100,7 @@ public class Short2ByteLinkedOpenCustomHashMap extends Short2ByteOpenCustomHashM } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -1105,19 +1115,19 @@ public class Short2ByteLinkedOpenCustomHashMap extends Short2ByteOpenCustomHashM state = operator.applyAsShort(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (short)0; + if(size() <= 0) return OptionalShort.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalShort.of(keys[index]); index = (int)links[index]; } - return (short)0; + return OptionalShort.empty(); } @Override @@ -1252,7 +1262,7 @@ public class Short2ByteLinkedOpenCustomHashMap extends Short2ByteOpenCustomHashM } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -1267,19 +1277,19 @@ public class Short2ByteLinkedOpenCustomHashMap extends Short2ByteOpenCustomHashM state = operator.applyAsByte(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (byte)0; + if(size() <= 0) return OptionalByte.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalByte.of(values[index]); index = (int)links[index]; } - return (byte)0; + return OptionalByte.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/shorts/maps/impl/customHash/Short2ByteOpenCustomHashMap.java b/src/main/java/speiger/src/collections/shorts/maps/impl/customHash/Short2ByteOpenCustomHashMap.java index da20618..3109eed 100644 --- a/src/main/java/speiger/src/collections/shorts/maps/impl/customHash/Short2ByteOpenCustomHashMap.java +++ b/src/main/java/speiger/src/collections/shorts/maps/impl/customHash/Short2ByteOpenCustomHashMap.java @@ -5,6 +5,7 @@ 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; @@ -19,6 +20,7 @@ import speiger.src.collections.shorts.functions.consumer.ShortByteConsumer; import speiger.src.collections.shorts.functions.function.Short2ByteFunction; import speiger.src.collections.shorts.functions.function.ShortByteUnaryOperator; import speiger.src.collections.shorts.functions.function.ShortShortUnaryOperator; +import speiger.src.collections.shorts.functions.OptionalShort; import speiger.src.collections.shorts.functions.function.ShortPredicate; import speiger.src.collections.shorts.maps.abstracts.AbstractShort2ByteMap; import speiger.src.collections.shorts.maps.interfaces.Short2ByteMap; @@ -37,6 +39,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectByteConsumer; import speiger.src.collections.bytes.functions.function.BytePredicate; +import speiger.src.collections.bytes.functions.OptionalByte; import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.sets.AbstractObjectSet; import speiger.src.collections.objects.sets.ObjectSet; @@ -952,7 +955,7 @@ public class Short2ByteOpenCustomHashMap extends AbstractShort2ByteMap implement } @Override - public Short2ByteMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Short2ByteMap.Entry state = null; boolean empty = true; @@ -969,25 +972,25 @@ public class Short2ByteOpenCustomHashMap extends AbstractShort2ByteMap implement } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Short2ByteMap.Entry 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], (short)0)) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1161,7 +1164,7 @@ public class Short2ByteOpenCustomHashMap extends AbstractShort2ByteMap implement } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -1178,18 +1181,18 @@ public class Short2ByteOpenCustomHashMap extends AbstractShort2ByteMap implement } state = operator.applyAsShort(state, keys[i]); } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (short)0; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OptionalShort.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OptionalShort.of(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], (short)0) && filter.test(keys[i])) return keys[i]; + if(!strategy.equals(keys[i], (short)0) && filter.test(keys[i])) return OptionalShort.of(keys[i]); } - return (short)0; + return OptionalShort.empty(); } @Override @@ -1304,7 +1307,7 @@ public class Short2ByteOpenCustomHashMap extends AbstractShort2ByteMap implement } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -1321,18 +1324,18 @@ public class Short2ByteOpenCustomHashMap extends AbstractShort2ByteMap implement } state = operator.applyAsByte(state, values[i]); } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (byte)0; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return OptionalByte.empty(); + if(containsNull && filter.test(values[nullIndex])) return OptionalByte.of(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], (short)0) && filter.test(values[i])) return values[i]; + if(!strategy.equals(keys[i], (short)0) && filter.test(values[i])) return OptionalByte.of(values[i]); } - return (byte)0; + return OptionalByte.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/shorts/maps/impl/customHash/Short2CharLinkedOpenCustomHashMap.java b/src/main/java/speiger/src/collections/shorts/maps/impl/customHash/Short2CharLinkedOpenCustomHashMap.java index ccd92e0..2f4abf2 100644 --- a/src/main/java/speiger/src/collections/shorts/maps/impl/customHash/Short2CharLinkedOpenCustomHashMap.java +++ b/src/main/java/speiger/src/collections/shorts/maps/impl/customHash/Short2CharLinkedOpenCustomHashMap.java @@ -4,6 +4,7 @@ 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; @@ -14,6 +15,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectShortConsumer; import speiger.src.collections.ints.functions.consumer.IntShortConsumer; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntCharConsumer; +import speiger.src.collections.shorts.functions.OptionalShort; import speiger.src.collections.shorts.functions.function.ShortPredicate; import speiger.src.collections.shorts.functions.consumer.ShortCharConsumer; import speiger.src.collections.shorts.functions.function.ShortShortUnaryOperator; @@ -33,6 +35,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectCharConsumer; import speiger.src.collections.chars.functions.function.CharPredicate; +import speiger.src.collections.chars.functions.OptionalChar; import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOperator; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.objects.lists.ObjectListIterator; @@ -671,6 +674,10 @@ public class Short2CharLinkedOpenCustomHashMap extends Short2CharOpenCustomHashM } private class MapEntrySet extends AbstractObjectSet implements Short2CharOrderedMap.FastOrderedSet { + @Override + public void addFirst(Short2CharMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Short2CharMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Short2CharMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -836,7 +843,7 @@ public class Short2CharLinkedOpenCustomHashMap extends Short2CharOpenCustomHashM } @Override - public Short2CharMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Short2CharMap.Entry state = null; boolean empty = true; @@ -851,21 +858,21 @@ public class Short2CharLinkedOpenCustomHashMap extends Short2CharOpenCustomHashM state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Short2CharMap.Entry 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 @@ -944,10 +951,13 @@ public class Short2CharLinkedOpenCustomHashMap extends Short2CharOpenCustomHashM public boolean add(short o) { throw new UnsupportedOperationException(); } - + + @Override + public void addFirst(short o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(short o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(short o) { throw new UnsupportedOperationException(); } - @Override public boolean addAndMoveToLast(short o) { throw new UnsupportedOperationException(); } @@ -1090,7 +1100,7 @@ public class Short2CharLinkedOpenCustomHashMap extends Short2CharOpenCustomHashM } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -1105,19 +1115,19 @@ public class Short2CharLinkedOpenCustomHashMap extends Short2CharOpenCustomHashM state = operator.applyAsShort(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (short)0; + if(size() <= 0) return OptionalShort.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalShort.of(keys[index]); index = (int)links[index]; } - return (short)0; + return OptionalShort.empty(); } @Override @@ -1252,7 +1262,7 @@ public class Short2CharLinkedOpenCustomHashMap extends Short2CharOpenCustomHashM } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -1267,19 +1277,19 @@ public class Short2CharLinkedOpenCustomHashMap extends Short2CharOpenCustomHashM state = operator.applyAsChar(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (char)0; + if(size() <= 0) return OptionalChar.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalChar.of(values[index]); index = (int)links[index]; } - return (char)0; + return OptionalChar.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/shorts/maps/impl/customHash/Short2CharOpenCustomHashMap.java b/src/main/java/speiger/src/collections/shorts/maps/impl/customHash/Short2CharOpenCustomHashMap.java index 20889e7..c03d5c9 100644 --- a/src/main/java/speiger/src/collections/shorts/maps/impl/customHash/Short2CharOpenCustomHashMap.java +++ b/src/main/java/speiger/src/collections/shorts/maps/impl/customHash/Short2CharOpenCustomHashMap.java @@ -5,6 +5,7 @@ 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; @@ -19,6 +20,7 @@ import speiger.src.collections.shorts.functions.consumer.ShortCharConsumer; import speiger.src.collections.shorts.functions.function.Short2CharFunction; import speiger.src.collections.shorts.functions.function.ShortCharUnaryOperator; import speiger.src.collections.shorts.functions.function.ShortShortUnaryOperator; +import speiger.src.collections.shorts.functions.OptionalShort; import speiger.src.collections.shorts.functions.function.ShortPredicate; import speiger.src.collections.shorts.maps.abstracts.AbstractShort2CharMap; import speiger.src.collections.shorts.maps.interfaces.Short2CharMap; @@ -37,6 +39,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectCharConsumer; import speiger.src.collections.chars.functions.function.CharPredicate; +import speiger.src.collections.chars.functions.OptionalChar; import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.sets.AbstractObjectSet; import speiger.src.collections.objects.sets.ObjectSet; @@ -952,7 +955,7 @@ public class Short2CharOpenCustomHashMap extends AbstractShort2CharMap implement } @Override - public Short2CharMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Short2CharMap.Entry state = null; boolean empty = true; @@ -969,25 +972,25 @@ public class Short2CharOpenCustomHashMap extends AbstractShort2CharMap implement } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Short2CharMap.Entry 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], (short)0)) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1161,7 +1164,7 @@ public class Short2CharOpenCustomHashMap extends AbstractShort2CharMap implement } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -1178,18 +1181,18 @@ public class Short2CharOpenCustomHashMap extends AbstractShort2CharMap implement } state = operator.applyAsShort(state, keys[i]); } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (short)0; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OptionalShort.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OptionalShort.of(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], (short)0) && filter.test(keys[i])) return keys[i]; + if(!strategy.equals(keys[i], (short)0) && filter.test(keys[i])) return OptionalShort.of(keys[i]); } - return (short)0; + return OptionalShort.empty(); } @Override @@ -1304,7 +1307,7 @@ public class Short2CharOpenCustomHashMap extends AbstractShort2CharMap implement } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -1321,18 +1324,18 @@ public class Short2CharOpenCustomHashMap extends AbstractShort2CharMap implement } state = operator.applyAsChar(state, values[i]); } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (char)0; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return OptionalChar.empty(); + if(containsNull && filter.test(values[nullIndex])) return OptionalChar.of(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], (short)0) && filter.test(values[i])) return values[i]; + if(!strategy.equals(keys[i], (short)0) && filter.test(values[i])) return OptionalChar.of(values[i]); } - return (char)0; + return OptionalChar.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/shorts/maps/impl/customHash/Short2DoubleLinkedOpenCustomHashMap.java b/src/main/java/speiger/src/collections/shorts/maps/impl/customHash/Short2DoubleLinkedOpenCustomHashMap.java index 0cb6b2d..db11bb0 100644 --- a/src/main/java/speiger/src/collections/shorts/maps/impl/customHash/Short2DoubleLinkedOpenCustomHashMap.java +++ b/src/main/java/speiger/src/collections/shorts/maps/impl/customHash/Short2DoubleLinkedOpenCustomHashMap.java @@ -4,10 +4,12 @@ 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; import java.util.function.DoublePredicate; +import java.util.OptionalDouble; import speiger.src.collections.shorts.collections.ShortBidirectionalIterator; import speiger.src.collections.shorts.functions.ShortConsumer; @@ -15,6 +17,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectShortConsumer; import speiger.src.collections.ints.functions.consumer.IntShortConsumer; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntDoubleConsumer; +import speiger.src.collections.shorts.functions.OptionalShort; import speiger.src.collections.shorts.functions.function.ShortPredicate; import speiger.src.collections.shorts.functions.consumer.ShortDoubleConsumer; import speiger.src.collections.shorts.functions.function.ShortShortUnaryOperator; @@ -671,6 +674,10 @@ public class Short2DoubleLinkedOpenCustomHashMap extends Short2DoubleOpenCustomH } private class MapEntrySet extends AbstractObjectSet implements Short2DoubleOrderedMap.FastOrderedSet { + @Override + public void addFirst(Short2DoubleMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Short2DoubleMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Short2DoubleMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -836,7 +843,7 @@ public class Short2DoubleLinkedOpenCustomHashMap extends Short2DoubleOpenCustomH } @Override - public Short2DoubleMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Short2DoubleMap.Entry state = null; boolean empty = true; @@ -851,21 +858,21 @@ public class Short2DoubleLinkedOpenCustomHashMap extends Short2DoubleOpenCustomH state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Short2DoubleMap.Entry 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 @@ -944,10 +951,13 @@ public class Short2DoubleLinkedOpenCustomHashMap extends Short2DoubleOpenCustomH public boolean add(short o) { throw new UnsupportedOperationException(); } - + + @Override + public void addFirst(short o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(short o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(short o) { throw new UnsupportedOperationException(); } - @Override public boolean addAndMoveToLast(short o) { throw new UnsupportedOperationException(); } @@ -1090,7 +1100,7 @@ public class Short2DoubleLinkedOpenCustomHashMap extends Short2DoubleOpenCustomH } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -1105,19 +1115,19 @@ public class Short2DoubleLinkedOpenCustomHashMap extends Short2DoubleOpenCustomH state = operator.applyAsShort(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (short)0; + if(size() <= 0) return OptionalShort.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalShort.of(keys[index]); index = (int)links[index]; } - return (short)0; + return OptionalShort.empty(); } @Override @@ -1252,7 +1262,7 @@ public class Short2DoubleLinkedOpenCustomHashMap extends Short2DoubleOpenCustomH } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -1267,19 +1277,19 @@ public class Short2DoubleLinkedOpenCustomHashMap extends Short2DoubleOpenCustomH state = operator.applyAsDouble(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0D; + if(size() <= 0) return OptionalDouble.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalDouble.of(values[index]); index = (int)links[index]; } - return 0D; + return OptionalDouble.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/shorts/maps/impl/customHash/Short2DoubleOpenCustomHashMap.java b/src/main/java/speiger/src/collections/shorts/maps/impl/customHash/Short2DoubleOpenCustomHashMap.java index 622c748..e257900 100644 --- a/src/main/java/speiger/src/collections/shorts/maps/impl/customHash/Short2DoubleOpenCustomHashMap.java +++ b/src/main/java/speiger/src/collections/shorts/maps/impl/customHash/Short2DoubleOpenCustomHashMap.java @@ -5,10 +5,12 @@ 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; import java.util.function.DoublePredicate; +import java.util.OptionalDouble; import speiger.src.collections.shorts.collections.ShortIterator; import speiger.src.collections.shorts.functions.ShortConsumer; @@ -20,6 +22,7 @@ import speiger.src.collections.shorts.functions.consumer.ShortDoubleConsumer; import speiger.src.collections.shorts.functions.function.Short2DoubleFunction; import speiger.src.collections.shorts.functions.function.ShortDoubleUnaryOperator; import speiger.src.collections.shorts.functions.function.ShortShortUnaryOperator; +import speiger.src.collections.shorts.functions.OptionalShort; import speiger.src.collections.shorts.functions.function.ShortPredicate; import speiger.src.collections.shorts.maps.abstracts.AbstractShort2DoubleMap; import speiger.src.collections.shorts.maps.interfaces.Short2DoubleMap; @@ -952,7 +955,7 @@ public class Short2DoubleOpenCustomHashMap extends AbstractShort2DoubleMap imple } @Override - public Short2DoubleMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Short2DoubleMap.Entry state = null; boolean empty = true; @@ -969,25 +972,25 @@ public class Short2DoubleOpenCustomHashMap extends AbstractShort2DoubleMap imple } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Short2DoubleMap.Entry 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], (short)0)) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1161,7 +1164,7 @@ public class Short2DoubleOpenCustomHashMap extends AbstractShort2DoubleMap imple } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -1178,18 +1181,18 @@ public class Short2DoubleOpenCustomHashMap extends AbstractShort2DoubleMap imple } state = operator.applyAsShort(state, keys[i]); } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (short)0; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OptionalShort.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OptionalShort.of(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], (short)0) && filter.test(keys[i])) return keys[i]; + if(!strategy.equals(keys[i], (short)0) && filter.test(keys[i])) return OptionalShort.of(keys[i]); } - return (short)0; + return OptionalShort.empty(); } @Override @@ -1304,7 +1307,7 @@ public class Short2DoubleOpenCustomHashMap extends AbstractShort2DoubleMap imple } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -1321,18 +1324,18 @@ public class Short2DoubleOpenCustomHashMap extends AbstractShort2DoubleMap imple } state = operator.applyAsDouble(state, values[i]); } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0D; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return OptionalDouble.empty(); + if(containsNull && filter.test(values[nullIndex])) return OptionalDouble.of(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], (short)0) && filter.test(values[i])) return values[i]; + if(!strategy.equals(keys[i], (short)0) && filter.test(values[i])) return OptionalDouble.of(values[i]); } - return 0D; + return OptionalDouble.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/shorts/maps/impl/customHash/Short2FloatLinkedOpenCustomHashMap.java b/src/main/java/speiger/src/collections/shorts/maps/impl/customHash/Short2FloatLinkedOpenCustomHashMap.java index e670006..6c5d143 100644 --- a/src/main/java/speiger/src/collections/shorts/maps/impl/customHash/Short2FloatLinkedOpenCustomHashMap.java +++ b/src/main/java/speiger/src/collections/shorts/maps/impl/customHash/Short2FloatLinkedOpenCustomHashMap.java @@ -4,6 +4,7 @@ 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; @@ -14,6 +15,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectShortConsumer; import speiger.src.collections.ints.functions.consumer.IntShortConsumer; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntFloatConsumer; +import speiger.src.collections.shorts.functions.OptionalShort; import speiger.src.collections.shorts.functions.function.ShortPredicate; import speiger.src.collections.shorts.functions.consumer.ShortFloatConsumer; import speiger.src.collections.shorts.functions.function.ShortShortUnaryOperator; @@ -33,6 +35,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectFloatConsumer; import speiger.src.collections.floats.functions.function.FloatPredicate; +import speiger.src.collections.floats.functions.OptionalFloat; import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOperator; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.objects.lists.ObjectListIterator; @@ -671,6 +674,10 @@ public class Short2FloatLinkedOpenCustomHashMap extends Short2FloatOpenCustomHas } private class MapEntrySet extends AbstractObjectSet implements Short2FloatOrderedMap.FastOrderedSet { + @Override + public void addFirst(Short2FloatMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Short2FloatMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Short2FloatMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -836,7 +843,7 @@ public class Short2FloatLinkedOpenCustomHashMap extends Short2FloatOpenCustomHas } @Override - public Short2FloatMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Short2FloatMap.Entry state = null; boolean empty = true; @@ -851,21 +858,21 @@ public class Short2FloatLinkedOpenCustomHashMap extends Short2FloatOpenCustomHas state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Short2FloatMap.Entry 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 @@ -944,10 +951,13 @@ public class Short2FloatLinkedOpenCustomHashMap extends Short2FloatOpenCustomHas public boolean add(short o) { throw new UnsupportedOperationException(); } - + + @Override + public void addFirst(short o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(short o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(short o) { throw new UnsupportedOperationException(); } - @Override public boolean addAndMoveToLast(short o) { throw new UnsupportedOperationException(); } @@ -1090,7 +1100,7 @@ public class Short2FloatLinkedOpenCustomHashMap extends Short2FloatOpenCustomHas } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -1105,19 +1115,19 @@ public class Short2FloatLinkedOpenCustomHashMap extends Short2FloatOpenCustomHas state = operator.applyAsShort(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (short)0; + if(size() <= 0) return OptionalShort.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalShort.of(keys[index]); index = (int)links[index]; } - return (short)0; + return OptionalShort.empty(); } @Override @@ -1252,7 +1262,7 @@ public class Short2FloatLinkedOpenCustomHashMap extends Short2FloatOpenCustomHas } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -1267,19 +1277,19 @@ public class Short2FloatLinkedOpenCustomHashMap extends Short2FloatOpenCustomHas state = operator.applyAsFloat(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0F; + if(size() <= 0) return OptionalFloat.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalFloat.of(values[index]); index = (int)links[index]; } - return 0F; + return OptionalFloat.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/shorts/maps/impl/customHash/Short2FloatOpenCustomHashMap.java b/src/main/java/speiger/src/collections/shorts/maps/impl/customHash/Short2FloatOpenCustomHashMap.java index a1e1bfd..1a7ba11 100644 --- a/src/main/java/speiger/src/collections/shorts/maps/impl/customHash/Short2FloatOpenCustomHashMap.java +++ b/src/main/java/speiger/src/collections/shorts/maps/impl/customHash/Short2FloatOpenCustomHashMap.java @@ -5,6 +5,7 @@ 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; @@ -19,6 +20,7 @@ import speiger.src.collections.shorts.functions.consumer.ShortFloatConsumer; import speiger.src.collections.shorts.functions.function.Short2FloatFunction; import speiger.src.collections.shorts.functions.function.ShortFloatUnaryOperator; import speiger.src.collections.shorts.functions.function.ShortShortUnaryOperator; +import speiger.src.collections.shorts.functions.OptionalShort; import speiger.src.collections.shorts.functions.function.ShortPredicate; import speiger.src.collections.shorts.maps.abstracts.AbstractShort2FloatMap; import speiger.src.collections.shorts.maps.interfaces.Short2FloatMap; @@ -37,6 +39,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectFloatConsumer; import speiger.src.collections.floats.functions.function.FloatPredicate; +import speiger.src.collections.floats.functions.OptionalFloat; import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.sets.AbstractObjectSet; import speiger.src.collections.objects.sets.ObjectSet; @@ -952,7 +955,7 @@ public class Short2FloatOpenCustomHashMap extends AbstractShort2FloatMap impleme } @Override - public Short2FloatMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Short2FloatMap.Entry state = null; boolean empty = true; @@ -969,25 +972,25 @@ public class Short2FloatOpenCustomHashMap extends AbstractShort2FloatMap impleme } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Short2FloatMap.Entry 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], (short)0)) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1161,7 +1164,7 @@ public class Short2FloatOpenCustomHashMap extends AbstractShort2FloatMap impleme } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -1178,18 +1181,18 @@ public class Short2FloatOpenCustomHashMap extends AbstractShort2FloatMap impleme } state = operator.applyAsShort(state, keys[i]); } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (short)0; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OptionalShort.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OptionalShort.of(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], (short)0) && filter.test(keys[i])) return keys[i]; + if(!strategy.equals(keys[i], (short)0) && filter.test(keys[i])) return OptionalShort.of(keys[i]); } - return (short)0; + return OptionalShort.empty(); } @Override @@ -1304,7 +1307,7 @@ public class Short2FloatOpenCustomHashMap extends AbstractShort2FloatMap impleme } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -1321,18 +1324,18 @@ public class Short2FloatOpenCustomHashMap extends AbstractShort2FloatMap impleme } state = operator.applyAsFloat(state, values[i]); } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0F; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return OptionalFloat.empty(); + if(containsNull && filter.test(values[nullIndex])) return OptionalFloat.of(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], (short)0) && filter.test(values[i])) return values[i]; + if(!strategy.equals(keys[i], (short)0) && filter.test(values[i])) return OptionalFloat.of(values[i]); } - return 0F; + return OptionalFloat.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/shorts/maps/impl/customHash/Short2IntLinkedOpenCustomHashMap.java b/src/main/java/speiger/src/collections/shorts/maps/impl/customHash/Short2IntLinkedOpenCustomHashMap.java index eb2e79f..b4d832b 100644 --- a/src/main/java/speiger/src/collections/shorts/maps/impl/customHash/Short2IntLinkedOpenCustomHashMap.java +++ b/src/main/java/speiger/src/collections/shorts/maps/impl/customHash/Short2IntLinkedOpenCustomHashMap.java @@ -4,10 +4,12 @@ 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; import java.util.function.IntPredicate; +import java.util.OptionalInt; import speiger.src.collections.shorts.collections.ShortBidirectionalIterator; import speiger.src.collections.shorts.functions.ShortConsumer; @@ -15,6 +17,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectShortConsumer; import speiger.src.collections.ints.functions.consumer.IntShortConsumer; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntIntConsumer; +import speiger.src.collections.shorts.functions.OptionalShort; import speiger.src.collections.shorts.functions.function.ShortPredicate; import speiger.src.collections.shorts.functions.consumer.ShortIntConsumer; import speiger.src.collections.shorts.functions.function.ShortShortUnaryOperator; @@ -671,6 +674,10 @@ public class Short2IntLinkedOpenCustomHashMap extends Short2IntOpenCustomHashMap } private class MapEntrySet extends AbstractObjectSet implements Short2IntOrderedMap.FastOrderedSet { + @Override + public void addFirst(Short2IntMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Short2IntMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Short2IntMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -836,7 +843,7 @@ public class Short2IntLinkedOpenCustomHashMap extends Short2IntOpenCustomHashMap } @Override - public Short2IntMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Short2IntMap.Entry state = null; boolean empty = true; @@ -851,21 +858,21 @@ public class Short2IntLinkedOpenCustomHashMap extends Short2IntOpenCustomHashMap state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Short2IntMap.Entry 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 @@ -944,10 +951,13 @@ public class Short2IntLinkedOpenCustomHashMap extends Short2IntOpenCustomHashMap public boolean add(short o) { throw new UnsupportedOperationException(); } - + + @Override + public void addFirst(short o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(short o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(short o) { throw new UnsupportedOperationException(); } - @Override public boolean addAndMoveToLast(short o) { throw new UnsupportedOperationException(); } @@ -1090,7 +1100,7 @@ public class Short2IntLinkedOpenCustomHashMap extends Short2IntOpenCustomHashMap } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -1105,19 +1115,19 @@ public class Short2IntLinkedOpenCustomHashMap extends Short2IntOpenCustomHashMap state = operator.applyAsShort(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (short)0; + if(size() <= 0) return OptionalShort.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalShort.of(keys[index]); index = (int)links[index]; } - return (short)0; + return OptionalShort.empty(); } @Override @@ -1252,7 +1262,7 @@ public class Short2IntLinkedOpenCustomHashMap extends Short2IntOpenCustomHashMap } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -1267,19 +1277,19 @@ public class Short2IntLinkedOpenCustomHashMap extends Short2IntOpenCustomHashMap state = operator.applyAsInt(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0; + if(size() <= 0) return OptionalInt.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalInt.of(values[index]); index = (int)links[index]; } - return 0; + return OptionalInt.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/shorts/maps/impl/customHash/Short2IntOpenCustomHashMap.java b/src/main/java/speiger/src/collections/shorts/maps/impl/customHash/Short2IntOpenCustomHashMap.java index 6f13f38..3440a25 100644 --- a/src/main/java/speiger/src/collections/shorts/maps/impl/customHash/Short2IntOpenCustomHashMap.java +++ b/src/main/java/speiger/src/collections/shorts/maps/impl/customHash/Short2IntOpenCustomHashMap.java @@ -5,10 +5,12 @@ 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; import java.util.function.IntPredicate; +import java.util.OptionalInt; import speiger.src.collections.shorts.collections.ShortIterator; import speiger.src.collections.shorts.functions.ShortConsumer; @@ -20,6 +22,7 @@ import speiger.src.collections.shorts.functions.consumer.ShortIntConsumer; import speiger.src.collections.shorts.functions.function.Short2IntFunction; import speiger.src.collections.shorts.functions.function.ShortIntUnaryOperator; import speiger.src.collections.shorts.functions.function.ShortShortUnaryOperator; +import speiger.src.collections.shorts.functions.OptionalShort; import speiger.src.collections.shorts.functions.function.ShortPredicate; import speiger.src.collections.shorts.maps.abstracts.AbstractShort2IntMap; import speiger.src.collections.shorts.maps.interfaces.Short2IntMap; @@ -952,7 +955,7 @@ public class Short2IntOpenCustomHashMap extends AbstractShort2IntMap implements } @Override - public Short2IntMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Short2IntMap.Entry state = null; boolean empty = true; @@ -969,25 +972,25 @@ public class Short2IntOpenCustomHashMap extends AbstractShort2IntMap implements } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Short2IntMap.Entry 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], (short)0)) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1161,7 +1164,7 @@ public class Short2IntOpenCustomHashMap extends AbstractShort2IntMap implements } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -1178,18 +1181,18 @@ public class Short2IntOpenCustomHashMap extends AbstractShort2IntMap implements } state = operator.applyAsShort(state, keys[i]); } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (short)0; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OptionalShort.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OptionalShort.of(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], (short)0) && filter.test(keys[i])) return keys[i]; + if(!strategy.equals(keys[i], (short)0) && filter.test(keys[i])) return OptionalShort.of(keys[i]); } - return (short)0; + return OptionalShort.empty(); } @Override @@ -1304,7 +1307,7 @@ public class Short2IntOpenCustomHashMap extends AbstractShort2IntMap implements } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -1321,18 +1324,18 @@ public class Short2IntOpenCustomHashMap extends AbstractShort2IntMap implements } state = operator.applyAsInt(state, values[i]); } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return OptionalInt.empty(); + if(containsNull && filter.test(values[nullIndex])) return OptionalInt.of(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], (short)0) && filter.test(values[i])) return values[i]; + if(!strategy.equals(keys[i], (short)0) && filter.test(values[i])) return OptionalInt.of(values[i]); } - return 0; + return OptionalInt.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/shorts/maps/impl/customHash/Short2LongLinkedOpenCustomHashMap.java b/src/main/java/speiger/src/collections/shorts/maps/impl/customHash/Short2LongLinkedOpenCustomHashMap.java index 42dc1b8..939e4dd 100644 --- a/src/main/java/speiger/src/collections/shorts/maps/impl/customHash/Short2LongLinkedOpenCustomHashMap.java +++ b/src/main/java/speiger/src/collections/shorts/maps/impl/customHash/Short2LongLinkedOpenCustomHashMap.java @@ -4,10 +4,12 @@ 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; import java.util.function.LongPredicate; +import java.util.OptionalLong; import speiger.src.collections.shorts.collections.ShortBidirectionalIterator; import speiger.src.collections.shorts.functions.ShortConsumer; @@ -15,6 +17,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectShortConsumer; import speiger.src.collections.ints.functions.consumer.IntShortConsumer; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntLongConsumer; +import speiger.src.collections.shorts.functions.OptionalShort; import speiger.src.collections.shorts.functions.function.ShortPredicate; import speiger.src.collections.shorts.functions.consumer.ShortLongConsumer; import speiger.src.collections.shorts.functions.function.ShortShortUnaryOperator; @@ -671,6 +674,10 @@ public class Short2LongLinkedOpenCustomHashMap extends Short2LongOpenCustomHashM } private class MapEntrySet extends AbstractObjectSet implements Short2LongOrderedMap.FastOrderedSet { + @Override + public void addFirst(Short2LongMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Short2LongMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Short2LongMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -836,7 +843,7 @@ public class Short2LongLinkedOpenCustomHashMap extends Short2LongOpenCustomHashM } @Override - public Short2LongMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Short2LongMap.Entry state = null; boolean empty = true; @@ -851,21 +858,21 @@ public class Short2LongLinkedOpenCustomHashMap extends Short2LongOpenCustomHashM state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Short2LongMap.Entry 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 @@ -944,10 +951,13 @@ public class Short2LongLinkedOpenCustomHashMap extends Short2LongOpenCustomHashM public boolean add(short o) { throw new UnsupportedOperationException(); } - + + @Override + public void addFirst(short o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(short o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(short o) { throw new UnsupportedOperationException(); } - @Override public boolean addAndMoveToLast(short o) { throw new UnsupportedOperationException(); } @@ -1090,7 +1100,7 @@ public class Short2LongLinkedOpenCustomHashMap extends Short2LongOpenCustomHashM } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -1105,19 +1115,19 @@ public class Short2LongLinkedOpenCustomHashMap extends Short2LongOpenCustomHashM state = operator.applyAsShort(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (short)0; + if(size() <= 0) return OptionalShort.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalShort.of(keys[index]); index = (int)links[index]; } - return (short)0; + return OptionalShort.empty(); } @Override @@ -1252,7 +1262,7 @@ public class Short2LongLinkedOpenCustomHashMap extends Short2LongOpenCustomHashM } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -1267,19 +1277,19 @@ public class Short2LongLinkedOpenCustomHashMap extends Short2LongOpenCustomHashM state = operator.applyAsLong(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0L; + if(size() <= 0) return OptionalLong.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalLong.of(values[index]); index = (int)links[index]; } - return 0L; + return OptionalLong.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/shorts/maps/impl/customHash/Short2LongOpenCustomHashMap.java b/src/main/java/speiger/src/collections/shorts/maps/impl/customHash/Short2LongOpenCustomHashMap.java index 8bc0edd..465d947 100644 --- a/src/main/java/speiger/src/collections/shorts/maps/impl/customHash/Short2LongOpenCustomHashMap.java +++ b/src/main/java/speiger/src/collections/shorts/maps/impl/customHash/Short2LongOpenCustomHashMap.java @@ -5,10 +5,12 @@ 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; import java.util.function.LongPredicate; +import java.util.OptionalLong; import speiger.src.collections.shorts.collections.ShortIterator; import speiger.src.collections.shorts.functions.ShortConsumer; @@ -20,6 +22,7 @@ import speiger.src.collections.shorts.functions.consumer.ShortLongConsumer; import speiger.src.collections.shorts.functions.function.Short2LongFunction; import speiger.src.collections.shorts.functions.function.ShortLongUnaryOperator; import speiger.src.collections.shorts.functions.function.ShortShortUnaryOperator; +import speiger.src.collections.shorts.functions.OptionalShort; import speiger.src.collections.shorts.functions.function.ShortPredicate; import speiger.src.collections.shorts.maps.abstracts.AbstractShort2LongMap; import speiger.src.collections.shorts.maps.interfaces.Short2LongMap; @@ -952,7 +955,7 @@ public class Short2LongOpenCustomHashMap extends AbstractShort2LongMap implement } @Override - public Short2LongMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Short2LongMap.Entry state = null; boolean empty = true; @@ -969,25 +972,25 @@ public class Short2LongOpenCustomHashMap extends AbstractShort2LongMap implement } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Short2LongMap.Entry 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], (short)0)) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1161,7 +1164,7 @@ public class Short2LongOpenCustomHashMap extends AbstractShort2LongMap implement } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -1178,18 +1181,18 @@ public class Short2LongOpenCustomHashMap extends AbstractShort2LongMap implement } state = operator.applyAsShort(state, keys[i]); } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (short)0; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OptionalShort.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OptionalShort.of(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], (short)0) && filter.test(keys[i])) return keys[i]; + if(!strategy.equals(keys[i], (short)0) && filter.test(keys[i])) return OptionalShort.of(keys[i]); } - return (short)0; + return OptionalShort.empty(); } @Override @@ -1304,7 +1307,7 @@ public class Short2LongOpenCustomHashMap extends AbstractShort2LongMap implement } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -1321,18 +1324,18 @@ public class Short2LongOpenCustomHashMap extends AbstractShort2LongMap implement } state = operator.applyAsLong(state, values[i]); } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0L; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return OptionalLong.empty(); + if(containsNull && filter.test(values[nullIndex])) return OptionalLong.of(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], (short)0) && filter.test(values[i])) return values[i]; + if(!strategy.equals(keys[i], (short)0) && filter.test(values[i])) return OptionalLong.of(values[i]); } - return 0L; + return OptionalLong.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/shorts/maps/impl/customHash/Short2ObjectLinkedOpenCustomHashMap.java b/src/main/java/speiger/src/collections/shorts/maps/impl/customHash/Short2ObjectLinkedOpenCustomHashMap.java index d081ed7..bd9469d 100644 --- a/src/main/java/speiger/src/collections/shorts/maps/impl/customHash/Short2ObjectLinkedOpenCustomHashMap.java +++ b/src/main/java/speiger/src/collections/shorts/maps/impl/customHash/Short2ObjectLinkedOpenCustomHashMap.java @@ -4,6 +4,7 @@ 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; @@ -13,6 +14,7 @@ import speiger.src.collections.shorts.functions.ShortConsumer; import speiger.src.collections.objects.functions.consumer.ObjectShortConsumer; import speiger.src.collections.ints.functions.consumer.IntShortConsumer; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; +import speiger.src.collections.shorts.functions.OptionalShort; import speiger.src.collections.shorts.functions.function.ShortPredicate; import speiger.src.collections.shorts.functions.consumer.ShortObjectConsumer; import speiger.src.collections.shorts.functions.function.ShortShortUnaryOperator; @@ -666,6 +668,10 @@ public class Short2ObjectLinkedOpenCustomHashMap extends Short2ObjectOpenCust } private class MapEntrySet extends AbstractObjectSet> implements Short2ObjectOrderedMap.FastOrderedSet { + @Override + public void addFirst(Short2ObjectMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Short2ObjectMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Short2ObjectMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -831,7 +837,7 @@ public class Short2ObjectLinkedOpenCustomHashMap extends Short2ObjectOpenCust } @Override - public Short2ObjectMap.Entry reduce(ObjectObjectUnaryOperator, Short2ObjectMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Short2ObjectMap.Entry> operator) { Objects.requireNonNull(operator); Short2ObjectMap.Entry state = null; boolean empty = true; @@ -846,21 +852,21 @@ public class Short2ObjectLinkedOpenCustomHashMap extends Short2ObjectOpenCust state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Short2ObjectMap.Entry 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 @@ -939,10 +945,13 @@ public class Short2ObjectLinkedOpenCustomHashMap extends Short2ObjectOpenCust public boolean add(short o) { throw new UnsupportedOperationException(); } - + + @Override + public void addFirst(short o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(short o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(short o) { throw new UnsupportedOperationException(); } - @Override public boolean addAndMoveToLast(short o) { throw new UnsupportedOperationException(); } @@ -1085,7 +1094,7 @@ public class Short2ObjectLinkedOpenCustomHashMap extends Short2ObjectOpenCust } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -1100,19 +1109,19 @@ public class Short2ObjectLinkedOpenCustomHashMap extends Short2ObjectOpenCust state = operator.applyAsShort(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (short)0; + if(size() <= 0) return OptionalShort.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalShort.of(keys[index]); index = (int)links[index]; } - return (short)0; + return OptionalShort.empty(); } @Override @@ -1248,7 +1257,7 @@ public class Short2ObjectLinkedOpenCustomHashMap extends Short2ObjectOpenCust } @Override - public V reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); V state = null; boolean empty = true; @@ -1263,19 +1272,19 @@ public class Short2ObjectLinkedOpenCustomHashMap extends Short2ObjectOpenCust state = operator.apply(state, values[index]); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public V findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return Optional.ofNullable(values[index]); index = (int)links[index]; } - return null; + return Optional.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/shorts/maps/impl/customHash/Short2ObjectOpenCustomHashMap.java b/src/main/java/speiger/src/collections/shorts/maps/impl/customHash/Short2ObjectOpenCustomHashMap.java index 98e7a19..ad60978 100644 --- a/src/main/java/speiger/src/collections/shorts/maps/impl/customHash/Short2ObjectOpenCustomHashMap.java +++ b/src/main/java/speiger/src/collections/shorts/maps/impl/customHash/Short2ObjectOpenCustomHashMap.java @@ -5,6 +5,7 @@ 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; @@ -18,6 +19,7 @@ import speiger.src.collections.shorts.functions.consumer.ShortObjectConsumer; import speiger.src.collections.shorts.functions.function.ShortFunction; import speiger.src.collections.shorts.functions.function.ShortObjectUnaryOperator; import speiger.src.collections.shorts.functions.function.ShortShortUnaryOperator; +import speiger.src.collections.shorts.functions.OptionalShort; import speiger.src.collections.shorts.functions.function.ShortPredicate; import speiger.src.collections.shorts.maps.abstracts.AbstractShort2ObjectMap; import speiger.src.collections.shorts.maps.interfaces.Short2ObjectMap; @@ -868,7 +870,7 @@ public class Short2ObjectOpenCustomHashMap extends AbstractShort2ObjectMap } @Override - public Short2ObjectMap.Entry reduce(ObjectObjectUnaryOperator, Short2ObjectMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Short2ObjectMap.Entry> operator) { Objects.requireNonNull(operator); Short2ObjectMap.Entry state = null; boolean empty = true; @@ -885,25 +887,25 @@ public class Short2ObjectOpenCustomHashMap extends AbstractShort2ObjectMap } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Short2ObjectMap.Entry 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], (short)0)) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1077,7 +1079,7 @@ public class Short2ObjectOpenCustomHashMap extends AbstractShort2ObjectMap } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -1094,18 +1096,18 @@ public class Short2ObjectOpenCustomHashMap extends AbstractShort2ObjectMap } state = operator.applyAsShort(state, keys[i]); } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (short)0; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OptionalShort.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OptionalShort.of(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], (short)0) && filter.test(keys[i])) return keys[i]; + if(!strategy.equals(keys[i], (short)0) && filter.test(keys[i])) return OptionalShort.of(keys[i]); } - return (short)0; + return OptionalShort.empty(); } @Override @@ -1220,7 +1222,7 @@ public class Short2ObjectOpenCustomHashMap extends AbstractShort2ObjectMap } @Override - public V reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); V state = null; boolean empty = true; @@ -1237,18 +1239,18 @@ public class Short2ObjectOpenCustomHashMap extends AbstractShort2ObjectMap } state = operator.apply(state, values[i]); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public V findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return Optional.empty(); + if(containsNull && filter.test(values[nullIndex])) return Optional.ofNullable(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], (short)0) && filter.test(values[i])) return values[i]; + if(!strategy.equals(keys[i], (short)0) && filter.test(values[i])) return Optional.ofNullable(values[i]); } - return null; + return Optional.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/shorts/maps/impl/customHash/Short2ShortLinkedOpenCustomHashMap.java b/src/main/java/speiger/src/collections/shorts/maps/impl/customHash/Short2ShortLinkedOpenCustomHashMap.java index 6245a6d..03fabb3 100644 --- a/src/main/java/speiger/src/collections/shorts/maps/impl/customHash/Short2ShortLinkedOpenCustomHashMap.java +++ b/src/main/java/speiger/src/collections/shorts/maps/impl/customHash/Short2ShortLinkedOpenCustomHashMap.java @@ -4,6 +4,7 @@ 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; @@ -13,6 +14,7 @@ import speiger.src.collections.shorts.functions.ShortConsumer; import speiger.src.collections.objects.functions.consumer.ObjectShortConsumer; import speiger.src.collections.ints.functions.consumer.IntShortConsumer; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; +import speiger.src.collections.shorts.functions.OptionalShort; import speiger.src.collections.shorts.functions.function.ShortPredicate; import speiger.src.collections.shorts.functions.consumer.ShortShortConsumer; import speiger.src.collections.shorts.functions.function.ShortShortUnaryOperator; @@ -664,6 +666,10 @@ public class Short2ShortLinkedOpenCustomHashMap extends Short2ShortOpenCustomHas } private class MapEntrySet extends AbstractObjectSet implements Short2ShortOrderedMap.FastOrderedSet { + @Override + public void addFirst(Short2ShortMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Short2ShortMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Short2ShortMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -829,7 +835,7 @@ public class Short2ShortLinkedOpenCustomHashMap extends Short2ShortOpenCustomHas } @Override - public Short2ShortMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Short2ShortMap.Entry state = null; boolean empty = true; @@ -844,21 +850,21 @@ public class Short2ShortLinkedOpenCustomHashMap extends Short2ShortOpenCustomHas state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Short2ShortMap.Entry 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 @@ -937,10 +943,13 @@ public class Short2ShortLinkedOpenCustomHashMap extends Short2ShortOpenCustomHas public boolean add(short o) { throw new UnsupportedOperationException(); } - + + @Override + public void addFirst(short o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(short o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(short o) { throw new UnsupportedOperationException(); } - @Override public boolean addAndMoveToLast(short o) { throw new UnsupportedOperationException(); } @@ -1083,7 +1092,7 @@ public class Short2ShortLinkedOpenCustomHashMap extends Short2ShortOpenCustomHas } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -1098,19 +1107,19 @@ public class Short2ShortLinkedOpenCustomHashMap extends Short2ShortOpenCustomHas state = operator.applyAsShort(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (short)0; + if(size() <= 0) return OptionalShort.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalShort.of(keys[index]); index = (int)links[index]; } - return (short)0; + return OptionalShort.empty(); } @Override @@ -1245,7 +1254,7 @@ public class Short2ShortLinkedOpenCustomHashMap extends Short2ShortOpenCustomHas } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -1260,19 +1269,19 @@ public class Short2ShortLinkedOpenCustomHashMap extends Short2ShortOpenCustomHas state = operator.applyAsShort(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (short)0; + if(size() <= 0) return OptionalShort.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalShort.of(values[index]); index = (int)links[index]; } - return (short)0; + return OptionalShort.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/shorts/maps/impl/customHash/Short2ShortOpenCustomHashMap.java b/src/main/java/speiger/src/collections/shorts/maps/impl/customHash/Short2ShortOpenCustomHashMap.java index 61bf567..2313f10 100644 --- a/src/main/java/speiger/src/collections/shorts/maps/impl/customHash/Short2ShortOpenCustomHashMap.java +++ b/src/main/java/speiger/src/collections/shorts/maps/impl/customHash/Short2ShortOpenCustomHashMap.java @@ -5,6 +5,7 @@ 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; @@ -17,6 +18,7 @@ import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.shorts.functions.consumer.ShortShortConsumer; import speiger.src.collections.shorts.functions.function.ShortUnaryOperator; import speiger.src.collections.shorts.functions.function.ShortShortUnaryOperator; +import speiger.src.collections.shorts.functions.OptionalShort; import speiger.src.collections.shorts.functions.function.ShortPredicate; import speiger.src.collections.shorts.maps.abstracts.AbstractShort2ShortMap; import speiger.src.collections.shorts.maps.interfaces.Short2ShortMap; @@ -944,7 +946,7 @@ public class Short2ShortOpenCustomHashMap extends AbstractShort2ShortMap impleme } @Override - public Short2ShortMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Short2ShortMap.Entry state = null; boolean empty = true; @@ -961,25 +963,25 @@ public class Short2ShortOpenCustomHashMap extends AbstractShort2ShortMap impleme } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Short2ShortMap.Entry 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], (short)0)) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1153,7 +1155,7 @@ public class Short2ShortOpenCustomHashMap extends AbstractShort2ShortMap impleme } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -1170,18 +1172,18 @@ public class Short2ShortOpenCustomHashMap extends AbstractShort2ShortMap impleme } state = operator.applyAsShort(state, keys[i]); } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (short)0; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OptionalShort.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OptionalShort.of(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], (short)0) && filter.test(keys[i])) return keys[i]; + if(!strategy.equals(keys[i], (short)0) && filter.test(keys[i])) return OptionalShort.of(keys[i]); } - return (short)0; + return OptionalShort.empty(); } @Override @@ -1296,7 +1298,7 @@ public class Short2ShortOpenCustomHashMap extends AbstractShort2ShortMap impleme } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -1313,18 +1315,18 @@ public class Short2ShortOpenCustomHashMap extends AbstractShort2ShortMap impleme } state = operator.applyAsShort(state, values[i]); } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (short)0; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return OptionalShort.empty(); + if(containsNull && filter.test(values[nullIndex])) return OptionalShort.of(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], (short)0) && filter.test(values[i])) return values[i]; + if(!strategy.equals(keys[i], (short)0) && filter.test(values[i])) return OptionalShort.of(values[i]); } - return (short)0; + return OptionalShort.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/shorts/maps/impl/hash/Short2BooleanLinkedOpenHashMap.java b/src/main/java/speiger/src/collections/shorts/maps/impl/hash/Short2BooleanLinkedOpenHashMap.java index a05824e..eab1c13 100644 --- a/src/main/java/speiger/src/collections/shorts/maps/impl/hash/Short2BooleanLinkedOpenHashMap.java +++ b/src/main/java/speiger/src/collections/shorts/maps/impl/hash/Short2BooleanLinkedOpenHashMap.java @@ -4,6 +4,7 @@ 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; @@ -14,6 +15,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectShortConsumer; import speiger.src.collections.ints.functions.consumer.IntShortConsumer; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntBooleanConsumer; +import speiger.src.collections.shorts.functions.OptionalShort; import speiger.src.collections.shorts.functions.function.ShortPredicate; import speiger.src.collections.shorts.functions.consumer.ShortBooleanConsumer; import speiger.src.collections.shorts.functions.function.ShortShortUnaryOperator; @@ -32,6 +34,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectBooleanConsumer; import speiger.src.collections.booleans.functions.function.BooleanPredicate; +import speiger.src.collections.booleans.functions.OptionalBoolean; import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOperator; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.objects.lists.ObjectListIterator; @@ -669,6 +672,10 @@ public class Short2BooleanLinkedOpenHashMap extends Short2BooleanOpenHashMap imp } private class MapEntrySet extends AbstractObjectSet implements Short2BooleanOrderedMap.FastOrderedSet { + @Override + public void addFirst(Short2BooleanMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Short2BooleanMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Short2BooleanMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -834,7 +841,7 @@ public class Short2BooleanLinkedOpenHashMap extends Short2BooleanOpenHashMap imp } @Override - public Short2BooleanMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Short2BooleanMap.Entry state = null; boolean empty = true; @@ -849,21 +856,21 @@ public class Short2BooleanLinkedOpenHashMap extends Short2BooleanOpenHashMap imp state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Short2BooleanMap.Entry 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 @@ -939,6 +946,12 @@ public class Short2BooleanLinkedOpenHashMap extends Short2BooleanOpenHashMap imp @Override public boolean add(short o) { throw new UnsupportedOperationException(); } + + @Override + public void addFirst(short o) { throw new UnsupportedOperationException(); } + + @Override + public void addLast(short o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(short o) { throw new UnsupportedOperationException(); } @@ -1085,7 +1098,7 @@ public class Short2BooleanLinkedOpenHashMap extends Short2BooleanOpenHashMap imp } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -1100,19 +1113,19 @@ public class Short2BooleanLinkedOpenHashMap extends Short2BooleanOpenHashMap imp state = operator.applyAsShort(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (short)0; + if(size() <= 0) return OptionalShort.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalShort.of(keys[index]); index = (int)links[index]; } - return (short)0; + return OptionalShort.empty(); } @Override @@ -1248,7 +1261,7 @@ public class Short2BooleanLinkedOpenHashMap extends Short2BooleanOpenHashMap imp } @Override - public boolean reduce(BooleanBooleanUnaryOperator operator) { + public OptionalBoolean reduce(BooleanBooleanUnaryOperator operator) { Objects.requireNonNull(operator); boolean state = false; boolean empty = true; @@ -1263,19 +1276,19 @@ public class Short2BooleanLinkedOpenHashMap extends Short2BooleanOpenHashMap imp state = operator.applyAsBoolean(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalBoolean.empty() : OptionalBoolean.of(state); } @Override - public boolean findFirst(BooleanPredicate filter) { + public OptionalBoolean findFirst(BooleanPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return false; + if(size() <= 0) return OptionalBoolean.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalBoolean.of(values[index]); index = (int)links[index]; } - return false; + return OptionalBoolean.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/shorts/maps/impl/hash/Short2BooleanOpenHashMap.java b/src/main/java/speiger/src/collections/shorts/maps/impl/hash/Short2BooleanOpenHashMap.java index 051f03f..f134abc 100644 --- a/src/main/java/speiger/src/collections/shorts/maps/impl/hash/Short2BooleanOpenHashMap.java +++ b/src/main/java/speiger/src/collections/shorts/maps/impl/hash/Short2BooleanOpenHashMap.java @@ -5,6 +5,7 @@ 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; @@ -19,6 +20,7 @@ import speiger.src.collections.shorts.functions.consumer.ShortBooleanConsumer; import speiger.src.collections.shorts.functions.function.ShortPredicate; import speiger.src.collections.shorts.functions.function.ShortBooleanUnaryOperator; import speiger.src.collections.shorts.functions.function.ShortShortUnaryOperator; +import speiger.src.collections.shorts.functions.OptionalShort; import speiger.src.collections.shorts.maps.abstracts.AbstractShort2BooleanMap; import speiger.src.collections.shorts.maps.interfaces.Short2BooleanMap; import speiger.src.collections.shorts.sets.AbstractShortSet; @@ -33,6 +35,7 @@ import speiger.src.collections.booleans.functions.BooleanConsumer; import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectBooleanConsumer; import speiger.src.collections.booleans.functions.function.BooleanPredicate; +import speiger.src.collections.booleans.functions.OptionalBoolean; import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.sets.AbstractObjectSet; import speiger.src.collections.objects.sets.ObjectSet; @@ -898,7 +901,7 @@ public class Short2BooleanOpenHashMap extends AbstractShort2BooleanMap implement } @Override - public Short2BooleanMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Short2BooleanMap.Entry state = null; boolean empty = true; @@ -915,25 +918,25 @@ public class Short2BooleanOpenHashMap extends AbstractShort2BooleanMap implement } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Short2BooleanMap.Entry 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(keys[i] != (short)0) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1105,7 +1108,7 @@ public class Short2BooleanOpenHashMap extends AbstractShort2BooleanMap implement } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -1122,18 +1125,18 @@ public class Short2BooleanOpenHashMap extends AbstractShort2BooleanMap implement } state = operator.applyAsShort(state, keys[i]); } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (short)0; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OptionalShort.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OptionalShort.of(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(keys[i] != (short)0 && filter.test(keys[i])) return keys[i]; + if(keys[i] != (short)0 && filter.test(keys[i])) return OptionalShort.of(keys[i]); } - return (short)0; + return OptionalShort.empty(); } @Override @@ -1248,7 +1251,7 @@ public class Short2BooleanOpenHashMap extends AbstractShort2BooleanMap implement } @Override - public boolean reduce(BooleanBooleanUnaryOperator operator) { + public OptionalBoolean reduce(BooleanBooleanUnaryOperator operator) { Objects.requireNonNull(operator); boolean state = false; boolean empty = true; @@ -1265,18 +1268,18 @@ public class Short2BooleanOpenHashMap extends AbstractShort2BooleanMap implement } state = operator.applyAsBoolean(state, values[i]); } - return state; + return empty ? OptionalBoolean.empty() : OptionalBoolean.of(state); } @Override - public boolean findFirst(BooleanPredicate filter) { + public OptionalBoolean findFirst(BooleanPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return false; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return OptionalBoolean.empty(); + if(containsNull && filter.test(values[nullIndex])) return OptionalBoolean.of(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(keys[i] != (short)0 && filter.test(values[i])) return values[i]; + if(keys[i] != (short)0 && filter.test(values[i])) return OptionalBoolean.of(values[i]); } - return false; + return OptionalBoolean.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/shorts/maps/impl/hash/Short2ByteLinkedOpenHashMap.java b/src/main/java/speiger/src/collections/shorts/maps/impl/hash/Short2ByteLinkedOpenHashMap.java index 08934a6..1184410 100644 --- a/src/main/java/speiger/src/collections/shorts/maps/impl/hash/Short2ByteLinkedOpenHashMap.java +++ b/src/main/java/speiger/src/collections/shorts/maps/impl/hash/Short2ByteLinkedOpenHashMap.java @@ -4,6 +4,7 @@ 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; @@ -14,6 +15,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectShortConsumer; import speiger.src.collections.ints.functions.consumer.IntShortConsumer; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntByteConsumer; +import speiger.src.collections.shorts.functions.OptionalShort; import speiger.src.collections.shorts.functions.function.ShortPredicate; import speiger.src.collections.shorts.functions.consumer.ShortByteConsumer; import speiger.src.collections.shorts.functions.function.ShortShortUnaryOperator; @@ -32,6 +34,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectByteConsumer; import speiger.src.collections.bytes.functions.function.BytePredicate; +import speiger.src.collections.bytes.functions.OptionalByte; import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOperator; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.objects.lists.ObjectListIterator; @@ -669,6 +672,10 @@ public class Short2ByteLinkedOpenHashMap extends Short2ByteOpenHashMap implement } private class MapEntrySet extends AbstractObjectSet implements Short2ByteOrderedMap.FastOrderedSet { + @Override + public void addFirst(Short2ByteMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Short2ByteMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Short2ByteMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -834,7 +841,7 @@ public class Short2ByteLinkedOpenHashMap extends Short2ByteOpenHashMap implement } @Override - public Short2ByteMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Short2ByteMap.Entry state = null; boolean empty = true; @@ -849,21 +856,21 @@ public class Short2ByteLinkedOpenHashMap extends Short2ByteOpenHashMap implement state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Short2ByteMap.Entry 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 @@ -939,6 +946,12 @@ public class Short2ByteLinkedOpenHashMap extends Short2ByteOpenHashMap implement @Override public boolean add(short o) { throw new UnsupportedOperationException(); } + + @Override + public void addFirst(short o) { throw new UnsupportedOperationException(); } + + @Override + public void addLast(short o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(short o) { throw new UnsupportedOperationException(); } @@ -1085,7 +1098,7 @@ public class Short2ByteLinkedOpenHashMap extends Short2ByteOpenHashMap implement } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -1100,19 +1113,19 @@ public class Short2ByteLinkedOpenHashMap extends Short2ByteOpenHashMap implement state = operator.applyAsShort(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (short)0; + if(size() <= 0) return OptionalShort.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalShort.of(keys[index]); index = (int)links[index]; } - return (short)0; + return OptionalShort.empty(); } @Override @@ -1248,7 +1261,7 @@ public class Short2ByteLinkedOpenHashMap extends Short2ByteOpenHashMap implement } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -1263,19 +1276,19 @@ public class Short2ByteLinkedOpenHashMap extends Short2ByteOpenHashMap implement state = operator.applyAsByte(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (byte)0; + if(size() <= 0) return OptionalByte.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalByte.of(values[index]); index = (int)links[index]; } - return (byte)0; + return OptionalByte.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/shorts/maps/impl/hash/Short2ByteOpenHashMap.java b/src/main/java/speiger/src/collections/shorts/maps/impl/hash/Short2ByteOpenHashMap.java index 9bc56e1..1cc11c2 100644 --- a/src/main/java/speiger/src/collections/shorts/maps/impl/hash/Short2ByteOpenHashMap.java +++ b/src/main/java/speiger/src/collections/shorts/maps/impl/hash/Short2ByteOpenHashMap.java @@ -5,6 +5,7 @@ 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; @@ -19,6 +20,7 @@ import speiger.src.collections.shorts.functions.consumer.ShortByteConsumer; import speiger.src.collections.shorts.functions.function.Short2ByteFunction; import speiger.src.collections.shorts.functions.function.ShortByteUnaryOperator; import speiger.src.collections.shorts.functions.function.ShortShortUnaryOperator; +import speiger.src.collections.shorts.functions.OptionalShort; import speiger.src.collections.shorts.functions.function.ShortPredicate; import speiger.src.collections.shorts.maps.abstracts.AbstractShort2ByteMap; import speiger.src.collections.shorts.maps.interfaces.Short2ByteMap; @@ -34,6 +36,7 @@ import speiger.src.collections.bytes.functions.ByteConsumer; import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectByteConsumer; import speiger.src.collections.bytes.functions.function.BytePredicate; +import speiger.src.collections.bytes.functions.OptionalByte; import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.sets.AbstractObjectSet; import speiger.src.collections.objects.sets.ObjectSet; @@ -920,7 +923,7 @@ public class Short2ByteOpenHashMap extends AbstractShort2ByteMap implements ITri } @Override - public Short2ByteMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Short2ByteMap.Entry state = null; boolean empty = true; @@ -937,25 +940,25 @@ public class Short2ByteOpenHashMap extends AbstractShort2ByteMap implements ITri } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Short2ByteMap.Entry 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(keys[i] != (short)0) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1127,7 +1130,7 @@ public class Short2ByteOpenHashMap extends AbstractShort2ByteMap implements ITri } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -1144,18 +1147,18 @@ public class Short2ByteOpenHashMap extends AbstractShort2ByteMap implements ITri } state = operator.applyAsShort(state, keys[i]); } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (short)0; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OptionalShort.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OptionalShort.of(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(keys[i] != (short)0 && filter.test(keys[i])) return keys[i]; + if(keys[i] != (short)0 && filter.test(keys[i])) return OptionalShort.of(keys[i]); } - return (short)0; + return OptionalShort.empty(); } @Override @@ -1270,7 +1273,7 @@ public class Short2ByteOpenHashMap extends AbstractShort2ByteMap implements ITri } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -1287,18 +1290,18 @@ public class Short2ByteOpenHashMap extends AbstractShort2ByteMap implements ITri } state = operator.applyAsByte(state, values[i]); } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (byte)0; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return OptionalByte.empty(); + if(containsNull && filter.test(values[nullIndex])) return OptionalByte.of(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(keys[i] != (short)0 && filter.test(values[i])) return values[i]; + if(keys[i] != (short)0 && filter.test(values[i])) return OptionalByte.of(values[i]); } - return (byte)0; + return OptionalByte.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/shorts/maps/impl/hash/Short2CharLinkedOpenHashMap.java b/src/main/java/speiger/src/collections/shorts/maps/impl/hash/Short2CharLinkedOpenHashMap.java index 3a07f32..b786dce 100644 --- a/src/main/java/speiger/src/collections/shorts/maps/impl/hash/Short2CharLinkedOpenHashMap.java +++ b/src/main/java/speiger/src/collections/shorts/maps/impl/hash/Short2CharLinkedOpenHashMap.java @@ -4,6 +4,7 @@ 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; @@ -14,6 +15,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectShortConsumer; import speiger.src.collections.ints.functions.consumer.IntShortConsumer; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntCharConsumer; +import speiger.src.collections.shorts.functions.OptionalShort; import speiger.src.collections.shorts.functions.function.ShortPredicate; import speiger.src.collections.shorts.functions.consumer.ShortCharConsumer; import speiger.src.collections.shorts.functions.function.ShortShortUnaryOperator; @@ -32,6 +34,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectCharConsumer; import speiger.src.collections.chars.functions.function.CharPredicate; +import speiger.src.collections.chars.functions.OptionalChar; import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOperator; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.objects.lists.ObjectListIterator; @@ -669,6 +672,10 @@ public class Short2CharLinkedOpenHashMap extends Short2CharOpenHashMap implement } private class MapEntrySet extends AbstractObjectSet implements Short2CharOrderedMap.FastOrderedSet { + @Override + public void addFirst(Short2CharMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Short2CharMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Short2CharMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -834,7 +841,7 @@ public class Short2CharLinkedOpenHashMap extends Short2CharOpenHashMap implement } @Override - public Short2CharMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Short2CharMap.Entry state = null; boolean empty = true; @@ -849,21 +856,21 @@ public class Short2CharLinkedOpenHashMap extends Short2CharOpenHashMap implement state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Short2CharMap.Entry 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 @@ -939,6 +946,12 @@ public class Short2CharLinkedOpenHashMap extends Short2CharOpenHashMap implement @Override public boolean add(short o) { throw new UnsupportedOperationException(); } + + @Override + public void addFirst(short o) { throw new UnsupportedOperationException(); } + + @Override + public void addLast(short o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(short o) { throw new UnsupportedOperationException(); } @@ -1085,7 +1098,7 @@ public class Short2CharLinkedOpenHashMap extends Short2CharOpenHashMap implement } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -1100,19 +1113,19 @@ public class Short2CharLinkedOpenHashMap extends Short2CharOpenHashMap implement state = operator.applyAsShort(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (short)0; + if(size() <= 0) return OptionalShort.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalShort.of(keys[index]); index = (int)links[index]; } - return (short)0; + return OptionalShort.empty(); } @Override @@ -1248,7 +1261,7 @@ public class Short2CharLinkedOpenHashMap extends Short2CharOpenHashMap implement } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -1263,19 +1276,19 @@ public class Short2CharLinkedOpenHashMap extends Short2CharOpenHashMap implement state = operator.applyAsChar(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (char)0; + if(size() <= 0) return OptionalChar.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalChar.of(values[index]); index = (int)links[index]; } - return (char)0; + return OptionalChar.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/shorts/maps/impl/hash/Short2CharOpenHashMap.java b/src/main/java/speiger/src/collections/shorts/maps/impl/hash/Short2CharOpenHashMap.java index 589f37a..8db514a 100644 --- a/src/main/java/speiger/src/collections/shorts/maps/impl/hash/Short2CharOpenHashMap.java +++ b/src/main/java/speiger/src/collections/shorts/maps/impl/hash/Short2CharOpenHashMap.java @@ -5,6 +5,7 @@ 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; @@ -19,6 +20,7 @@ import speiger.src.collections.shorts.functions.consumer.ShortCharConsumer; import speiger.src.collections.shorts.functions.function.Short2CharFunction; import speiger.src.collections.shorts.functions.function.ShortCharUnaryOperator; import speiger.src.collections.shorts.functions.function.ShortShortUnaryOperator; +import speiger.src.collections.shorts.functions.OptionalShort; import speiger.src.collections.shorts.functions.function.ShortPredicate; import speiger.src.collections.shorts.maps.abstracts.AbstractShort2CharMap; import speiger.src.collections.shorts.maps.interfaces.Short2CharMap; @@ -34,6 +36,7 @@ import speiger.src.collections.chars.functions.CharConsumer; import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectCharConsumer; import speiger.src.collections.chars.functions.function.CharPredicate; +import speiger.src.collections.chars.functions.OptionalChar; import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.sets.AbstractObjectSet; import speiger.src.collections.objects.sets.ObjectSet; @@ -920,7 +923,7 @@ public class Short2CharOpenHashMap extends AbstractShort2CharMap implements ITri } @Override - public Short2CharMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Short2CharMap.Entry state = null; boolean empty = true; @@ -937,25 +940,25 @@ public class Short2CharOpenHashMap extends AbstractShort2CharMap implements ITri } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Short2CharMap.Entry 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(keys[i] != (short)0) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1127,7 +1130,7 @@ public class Short2CharOpenHashMap extends AbstractShort2CharMap implements ITri } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -1144,18 +1147,18 @@ public class Short2CharOpenHashMap extends AbstractShort2CharMap implements ITri } state = operator.applyAsShort(state, keys[i]); } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (short)0; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OptionalShort.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OptionalShort.of(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(keys[i] != (short)0 && filter.test(keys[i])) return keys[i]; + if(keys[i] != (short)0 && filter.test(keys[i])) return OptionalShort.of(keys[i]); } - return (short)0; + return OptionalShort.empty(); } @Override @@ -1270,7 +1273,7 @@ public class Short2CharOpenHashMap extends AbstractShort2CharMap implements ITri } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -1287,18 +1290,18 @@ public class Short2CharOpenHashMap extends AbstractShort2CharMap implements ITri } state = operator.applyAsChar(state, values[i]); } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (char)0; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return OptionalChar.empty(); + if(containsNull && filter.test(values[nullIndex])) return OptionalChar.of(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(keys[i] != (short)0 && filter.test(values[i])) return values[i]; + if(keys[i] != (short)0 && filter.test(values[i])) return OptionalChar.of(values[i]); } - return (char)0; + return OptionalChar.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/shorts/maps/impl/hash/Short2DoubleLinkedOpenHashMap.java b/src/main/java/speiger/src/collections/shorts/maps/impl/hash/Short2DoubleLinkedOpenHashMap.java index 8366677..97cb633 100644 --- a/src/main/java/speiger/src/collections/shorts/maps/impl/hash/Short2DoubleLinkedOpenHashMap.java +++ b/src/main/java/speiger/src/collections/shorts/maps/impl/hash/Short2DoubleLinkedOpenHashMap.java @@ -4,10 +4,12 @@ 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; import java.util.function.DoublePredicate; +import java.util.OptionalDouble; import speiger.src.collections.shorts.collections.ShortBidirectionalIterator; import speiger.src.collections.shorts.functions.ShortConsumer; @@ -15,6 +17,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectShortConsumer; import speiger.src.collections.ints.functions.consumer.IntShortConsumer; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntDoubleConsumer; +import speiger.src.collections.shorts.functions.OptionalShort; import speiger.src.collections.shorts.functions.function.ShortPredicate; import speiger.src.collections.shorts.functions.consumer.ShortDoubleConsumer; import speiger.src.collections.shorts.functions.function.ShortShortUnaryOperator; @@ -669,6 +672,10 @@ public class Short2DoubleLinkedOpenHashMap extends Short2DoubleOpenHashMap imple } private class MapEntrySet extends AbstractObjectSet implements Short2DoubleOrderedMap.FastOrderedSet { + @Override + public void addFirst(Short2DoubleMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Short2DoubleMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Short2DoubleMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -834,7 +841,7 @@ public class Short2DoubleLinkedOpenHashMap extends Short2DoubleOpenHashMap imple } @Override - public Short2DoubleMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Short2DoubleMap.Entry state = null; boolean empty = true; @@ -849,21 +856,21 @@ public class Short2DoubleLinkedOpenHashMap extends Short2DoubleOpenHashMap imple state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Short2DoubleMap.Entry 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 @@ -939,6 +946,12 @@ public class Short2DoubleLinkedOpenHashMap extends Short2DoubleOpenHashMap imple @Override public boolean add(short o) { throw new UnsupportedOperationException(); } + + @Override + public void addFirst(short o) { throw new UnsupportedOperationException(); } + + @Override + public void addLast(short o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(short o) { throw new UnsupportedOperationException(); } @@ -1085,7 +1098,7 @@ public class Short2DoubleLinkedOpenHashMap extends Short2DoubleOpenHashMap imple } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -1100,19 +1113,19 @@ public class Short2DoubleLinkedOpenHashMap extends Short2DoubleOpenHashMap imple state = operator.applyAsShort(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (short)0; + if(size() <= 0) return OptionalShort.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalShort.of(keys[index]); index = (int)links[index]; } - return (short)0; + return OptionalShort.empty(); } @Override @@ -1248,7 +1261,7 @@ public class Short2DoubleLinkedOpenHashMap extends Short2DoubleOpenHashMap imple } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -1263,19 +1276,19 @@ public class Short2DoubleLinkedOpenHashMap extends Short2DoubleOpenHashMap imple state = operator.applyAsDouble(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0D; + if(size() <= 0) return OptionalDouble.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalDouble.of(values[index]); index = (int)links[index]; } - return 0D; + return OptionalDouble.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/shorts/maps/impl/hash/Short2DoubleOpenHashMap.java b/src/main/java/speiger/src/collections/shorts/maps/impl/hash/Short2DoubleOpenHashMap.java index 88ad5da..729c5ec 100644 --- a/src/main/java/speiger/src/collections/shorts/maps/impl/hash/Short2DoubleOpenHashMap.java +++ b/src/main/java/speiger/src/collections/shorts/maps/impl/hash/Short2DoubleOpenHashMap.java @@ -5,10 +5,12 @@ 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; import java.util.function.DoublePredicate; +import java.util.OptionalDouble; import speiger.src.collections.shorts.collections.ShortIterator; import speiger.src.collections.shorts.functions.ShortConsumer; @@ -20,6 +22,7 @@ import speiger.src.collections.shorts.functions.consumer.ShortDoubleConsumer; import speiger.src.collections.shorts.functions.function.Short2DoubleFunction; import speiger.src.collections.shorts.functions.function.ShortDoubleUnaryOperator; import speiger.src.collections.shorts.functions.function.ShortShortUnaryOperator; +import speiger.src.collections.shorts.functions.OptionalShort; import speiger.src.collections.shorts.functions.function.ShortPredicate; import speiger.src.collections.shorts.maps.abstracts.AbstractShort2DoubleMap; import speiger.src.collections.shorts.maps.interfaces.Short2DoubleMap; @@ -920,7 +923,7 @@ public class Short2DoubleOpenHashMap extends AbstractShort2DoubleMap implements } @Override - public Short2DoubleMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Short2DoubleMap.Entry state = null; boolean empty = true; @@ -937,25 +940,25 @@ public class Short2DoubleOpenHashMap extends AbstractShort2DoubleMap implements } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Short2DoubleMap.Entry 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(keys[i] != (short)0) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1127,7 +1130,7 @@ public class Short2DoubleOpenHashMap extends AbstractShort2DoubleMap implements } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -1144,18 +1147,18 @@ public class Short2DoubleOpenHashMap extends AbstractShort2DoubleMap implements } state = operator.applyAsShort(state, keys[i]); } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (short)0; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OptionalShort.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OptionalShort.of(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(keys[i] != (short)0 && filter.test(keys[i])) return keys[i]; + if(keys[i] != (short)0 && filter.test(keys[i])) return OptionalShort.of(keys[i]); } - return (short)0; + return OptionalShort.empty(); } @Override @@ -1270,7 +1273,7 @@ public class Short2DoubleOpenHashMap extends AbstractShort2DoubleMap implements } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -1287,18 +1290,18 @@ public class Short2DoubleOpenHashMap extends AbstractShort2DoubleMap implements } state = operator.applyAsDouble(state, values[i]); } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0D; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return OptionalDouble.empty(); + if(containsNull && filter.test(values[nullIndex])) return OptionalDouble.of(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(keys[i] != (short)0 && filter.test(values[i])) return values[i]; + if(keys[i] != (short)0 && filter.test(values[i])) return OptionalDouble.of(values[i]); } - return 0D; + return OptionalDouble.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/shorts/maps/impl/hash/Short2FloatLinkedOpenHashMap.java b/src/main/java/speiger/src/collections/shorts/maps/impl/hash/Short2FloatLinkedOpenHashMap.java index ba05d9a..d48b268 100644 --- a/src/main/java/speiger/src/collections/shorts/maps/impl/hash/Short2FloatLinkedOpenHashMap.java +++ b/src/main/java/speiger/src/collections/shorts/maps/impl/hash/Short2FloatLinkedOpenHashMap.java @@ -4,6 +4,7 @@ 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; @@ -14,6 +15,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectShortConsumer; import speiger.src.collections.ints.functions.consumer.IntShortConsumer; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntFloatConsumer; +import speiger.src.collections.shorts.functions.OptionalShort; import speiger.src.collections.shorts.functions.function.ShortPredicate; import speiger.src.collections.shorts.functions.consumer.ShortFloatConsumer; import speiger.src.collections.shorts.functions.function.ShortShortUnaryOperator; @@ -32,6 +34,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectFloatConsumer; import speiger.src.collections.floats.functions.function.FloatPredicate; +import speiger.src.collections.floats.functions.OptionalFloat; import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOperator; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.objects.lists.ObjectListIterator; @@ -669,6 +672,10 @@ public class Short2FloatLinkedOpenHashMap extends Short2FloatOpenHashMap impleme } private class MapEntrySet extends AbstractObjectSet implements Short2FloatOrderedMap.FastOrderedSet { + @Override + public void addFirst(Short2FloatMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Short2FloatMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Short2FloatMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -834,7 +841,7 @@ public class Short2FloatLinkedOpenHashMap extends Short2FloatOpenHashMap impleme } @Override - public Short2FloatMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Short2FloatMap.Entry state = null; boolean empty = true; @@ -849,21 +856,21 @@ public class Short2FloatLinkedOpenHashMap extends Short2FloatOpenHashMap impleme state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Short2FloatMap.Entry 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 @@ -939,6 +946,12 @@ public class Short2FloatLinkedOpenHashMap extends Short2FloatOpenHashMap impleme @Override public boolean add(short o) { throw new UnsupportedOperationException(); } + + @Override + public void addFirst(short o) { throw new UnsupportedOperationException(); } + + @Override + public void addLast(short o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(short o) { throw new UnsupportedOperationException(); } @@ -1085,7 +1098,7 @@ public class Short2FloatLinkedOpenHashMap extends Short2FloatOpenHashMap impleme } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -1100,19 +1113,19 @@ public class Short2FloatLinkedOpenHashMap extends Short2FloatOpenHashMap impleme state = operator.applyAsShort(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (short)0; + if(size() <= 0) return OptionalShort.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalShort.of(keys[index]); index = (int)links[index]; } - return (short)0; + return OptionalShort.empty(); } @Override @@ -1248,7 +1261,7 @@ public class Short2FloatLinkedOpenHashMap extends Short2FloatOpenHashMap impleme } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -1263,19 +1276,19 @@ public class Short2FloatLinkedOpenHashMap extends Short2FloatOpenHashMap impleme state = operator.applyAsFloat(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0F; + if(size() <= 0) return OptionalFloat.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalFloat.of(values[index]); index = (int)links[index]; } - return 0F; + return OptionalFloat.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/shorts/maps/impl/hash/Short2FloatOpenHashMap.java b/src/main/java/speiger/src/collections/shorts/maps/impl/hash/Short2FloatOpenHashMap.java index 3069940..8d8179f 100644 --- a/src/main/java/speiger/src/collections/shorts/maps/impl/hash/Short2FloatOpenHashMap.java +++ b/src/main/java/speiger/src/collections/shorts/maps/impl/hash/Short2FloatOpenHashMap.java @@ -5,6 +5,7 @@ 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; @@ -19,6 +20,7 @@ import speiger.src.collections.shorts.functions.consumer.ShortFloatConsumer; import speiger.src.collections.shorts.functions.function.Short2FloatFunction; import speiger.src.collections.shorts.functions.function.ShortFloatUnaryOperator; import speiger.src.collections.shorts.functions.function.ShortShortUnaryOperator; +import speiger.src.collections.shorts.functions.OptionalShort; import speiger.src.collections.shorts.functions.function.ShortPredicate; import speiger.src.collections.shorts.maps.abstracts.AbstractShort2FloatMap; import speiger.src.collections.shorts.maps.interfaces.Short2FloatMap; @@ -34,6 +36,7 @@ import speiger.src.collections.floats.functions.FloatConsumer; import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectFloatConsumer; import speiger.src.collections.floats.functions.function.FloatPredicate; +import speiger.src.collections.floats.functions.OptionalFloat; import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.sets.AbstractObjectSet; import speiger.src.collections.objects.sets.ObjectSet; @@ -920,7 +923,7 @@ public class Short2FloatOpenHashMap extends AbstractShort2FloatMap implements IT } @Override - public Short2FloatMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Short2FloatMap.Entry state = null; boolean empty = true; @@ -937,25 +940,25 @@ public class Short2FloatOpenHashMap extends AbstractShort2FloatMap implements IT } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Short2FloatMap.Entry 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(keys[i] != (short)0) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1127,7 +1130,7 @@ public class Short2FloatOpenHashMap extends AbstractShort2FloatMap implements IT } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -1144,18 +1147,18 @@ public class Short2FloatOpenHashMap extends AbstractShort2FloatMap implements IT } state = operator.applyAsShort(state, keys[i]); } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (short)0; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OptionalShort.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OptionalShort.of(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(keys[i] != (short)0 && filter.test(keys[i])) return keys[i]; + if(keys[i] != (short)0 && filter.test(keys[i])) return OptionalShort.of(keys[i]); } - return (short)0; + return OptionalShort.empty(); } @Override @@ -1270,7 +1273,7 @@ public class Short2FloatOpenHashMap extends AbstractShort2FloatMap implements IT } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -1287,18 +1290,18 @@ public class Short2FloatOpenHashMap extends AbstractShort2FloatMap implements IT } state = operator.applyAsFloat(state, values[i]); } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0F; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return OptionalFloat.empty(); + if(containsNull && filter.test(values[nullIndex])) return OptionalFloat.of(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(keys[i] != (short)0 && filter.test(values[i])) return values[i]; + if(keys[i] != (short)0 && filter.test(values[i])) return OptionalFloat.of(values[i]); } - return 0F; + return OptionalFloat.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/shorts/maps/impl/hash/Short2IntLinkedOpenHashMap.java b/src/main/java/speiger/src/collections/shorts/maps/impl/hash/Short2IntLinkedOpenHashMap.java index 974f07f..c81efb4 100644 --- a/src/main/java/speiger/src/collections/shorts/maps/impl/hash/Short2IntLinkedOpenHashMap.java +++ b/src/main/java/speiger/src/collections/shorts/maps/impl/hash/Short2IntLinkedOpenHashMap.java @@ -4,10 +4,12 @@ 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; import java.util.function.IntPredicate; +import java.util.OptionalInt; import speiger.src.collections.shorts.collections.ShortBidirectionalIterator; import speiger.src.collections.shorts.functions.ShortConsumer; @@ -15,6 +17,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectShortConsumer; import speiger.src.collections.ints.functions.consumer.IntShortConsumer; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntIntConsumer; +import speiger.src.collections.shorts.functions.OptionalShort; import speiger.src.collections.shorts.functions.function.ShortPredicate; import speiger.src.collections.shorts.functions.consumer.ShortIntConsumer; import speiger.src.collections.shorts.functions.function.ShortShortUnaryOperator; @@ -669,6 +672,10 @@ public class Short2IntLinkedOpenHashMap extends Short2IntOpenHashMap implements } private class MapEntrySet extends AbstractObjectSet implements Short2IntOrderedMap.FastOrderedSet { + @Override + public void addFirst(Short2IntMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Short2IntMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Short2IntMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -834,7 +841,7 @@ public class Short2IntLinkedOpenHashMap extends Short2IntOpenHashMap implements } @Override - public Short2IntMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Short2IntMap.Entry state = null; boolean empty = true; @@ -849,21 +856,21 @@ public class Short2IntLinkedOpenHashMap extends Short2IntOpenHashMap implements state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Short2IntMap.Entry 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 @@ -939,6 +946,12 @@ public class Short2IntLinkedOpenHashMap extends Short2IntOpenHashMap implements @Override public boolean add(short o) { throw new UnsupportedOperationException(); } + + @Override + public void addFirst(short o) { throw new UnsupportedOperationException(); } + + @Override + public void addLast(short o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(short o) { throw new UnsupportedOperationException(); } @@ -1085,7 +1098,7 @@ public class Short2IntLinkedOpenHashMap extends Short2IntOpenHashMap implements } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -1100,19 +1113,19 @@ public class Short2IntLinkedOpenHashMap extends Short2IntOpenHashMap implements state = operator.applyAsShort(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (short)0; + if(size() <= 0) return OptionalShort.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalShort.of(keys[index]); index = (int)links[index]; } - return (short)0; + return OptionalShort.empty(); } @Override @@ -1248,7 +1261,7 @@ public class Short2IntLinkedOpenHashMap extends Short2IntOpenHashMap implements } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -1263,19 +1276,19 @@ public class Short2IntLinkedOpenHashMap extends Short2IntOpenHashMap implements state = operator.applyAsInt(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0; + if(size() <= 0) return OptionalInt.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalInt.of(values[index]); index = (int)links[index]; } - return 0; + return OptionalInt.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/shorts/maps/impl/hash/Short2IntOpenHashMap.java b/src/main/java/speiger/src/collections/shorts/maps/impl/hash/Short2IntOpenHashMap.java index 9a0012b..baf01ea 100644 --- a/src/main/java/speiger/src/collections/shorts/maps/impl/hash/Short2IntOpenHashMap.java +++ b/src/main/java/speiger/src/collections/shorts/maps/impl/hash/Short2IntOpenHashMap.java @@ -5,10 +5,12 @@ 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; import java.util.function.IntPredicate; +import java.util.OptionalInt; import speiger.src.collections.shorts.collections.ShortIterator; import speiger.src.collections.shorts.functions.ShortConsumer; @@ -20,6 +22,7 @@ import speiger.src.collections.shorts.functions.consumer.ShortIntConsumer; import speiger.src.collections.shorts.functions.function.Short2IntFunction; import speiger.src.collections.shorts.functions.function.ShortIntUnaryOperator; import speiger.src.collections.shorts.functions.function.ShortShortUnaryOperator; +import speiger.src.collections.shorts.functions.OptionalShort; import speiger.src.collections.shorts.functions.function.ShortPredicate; import speiger.src.collections.shorts.maps.abstracts.AbstractShort2IntMap; import speiger.src.collections.shorts.maps.interfaces.Short2IntMap; @@ -920,7 +923,7 @@ public class Short2IntOpenHashMap extends AbstractShort2IntMap implements ITrimm } @Override - public Short2IntMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Short2IntMap.Entry state = null; boolean empty = true; @@ -937,25 +940,25 @@ public class Short2IntOpenHashMap extends AbstractShort2IntMap implements ITrimm } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Short2IntMap.Entry 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(keys[i] != (short)0) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1127,7 +1130,7 @@ public class Short2IntOpenHashMap extends AbstractShort2IntMap implements ITrimm } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -1144,18 +1147,18 @@ public class Short2IntOpenHashMap extends AbstractShort2IntMap implements ITrimm } state = operator.applyAsShort(state, keys[i]); } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (short)0; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OptionalShort.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OptionalShort.of(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(keys[i] != (short)0 && filter.test(keys[i])) return keys[i]; + if(keys[i] != (short)0 && filter.test(keys[i])) return OptionalShort.of(keys[i]); } - return (short)0; + return OptionalShort.empty(); } @Override @@ -1270,7 +1273,7 @@ public class Short2IntOpenHashMap extends AbstractShort2IntMap implements ITrimm } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -1287,18 +1290,18 @@ public class Short2IntOpenHashMap extends AbstractShort2IntMap implements ITrimm } state = operator.applyAsInt(state, values[i]); } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return OptionalInt.empty(); + if(containsNull && filter.test(values[nullIndex])) return OptionalInt.of(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(keys[i] != (short)0 && filter.test(values[i])) return values[i]; + if(keys[i] != (short)0 && filter.test(values[i])) return OptionalInt.of(values[i]); } - return 0; + return OptionalInt.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/shorts/maps/impl/hash/Short2LongLinkedOpenHashMap.java b/src/main/java/speiger/src/collections/shorts/maps/impl/hash/Short2LongLinkedOpenHashMap.java index 051fd55..04abf02 100644 --- a/src/main/java/speiger/src/collections/shorts/maps/impl/hash/Short2LongLinkedOpenHashMap.java +++ b/src/main/java/speiger/src/collections/shorts/maps/impl/hash/Short2LongLinkedOpenHashMap.java @@ -4,10 +4,12 @@ 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; import java.util.function.LongPredicate; +import java.util.OptionalLong; import speiger.src.collections.shorts.collections.ShortBidirectionalIterator; import speiger.src.collections.shorts.functions.ShortConsumer; @@ -15,6 +17,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectShortConsumer; import speiger.src.collections.ints.functions.consumer.IntShortConsumer; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntLongConsumer; +import speiger.src.collections.shorts.functions.OptionalShort; import speiger.src.collections.shorts.functions.function.ShortPredicate; import speiger.src.collections.shorts.functions.consumer.ShortLongConsumer; import speiger.src.collections.shorts.functions.function.ShortShortUnaryOperator; @@ -669,6 +672,10 @@ public class Short2LongLinkedOpenHashMap extends Short2LongOpenHashMap implement } private class MapEntrySet extends AbstractObjectSet implements Short2LongOrderedMap.FastOrderedSet { + @Override + public void addFirst(Short2LongMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Short2LongMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Short2LongMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -834,7 +841,7 @@ public class Short2LongLinkedOpenHashMap extends Short2LongOpenHashMap implement } @Override - public Short2LongMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Short2LongMap.Entry state = null; boolean empty = true; @@ -849,21 +856,21 @@ public class Short2LongLinkedOpenHashMap extends Short2LongOpenHashMap implement state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Short2LongMap.Entry 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 @@ -939,6 +946,12 @@ public class Short2LongLinkedOpenHashMap extends Short2LongOpenHashMap implement @Override public boolean add(short o) { throw new UnsupportedOperationException(); } + + @Override + public void addFirst(short o) { throw new UnsupportedOperationException(); } + + @Override + public void addLast(short o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(short o) { throw new UnsupportedOperationException(); } @@ -1085,7 +1098,7 @@ public class Short2LongLinkedOpenHashMap extends Short2LongOpenHashMap implement } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -1100,19 +1113,19 @@ public class Short2LongLinkedOpenHashMap extends Short2LongOpenHashMap implement state = operator.applyAsShort(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (short)0; + if(size() <= 0) return OptionalShort.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalShort.of(keys[index]); index = (int)links[index]; } - return (short)0; + return OptionalShort.empty(); } @Override @@ -1248,7 +1261,7 @@ public class Short2LongLinkedOpenHashMap extends Short2LongOpenHashMap implement } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -1263,19 +1276,19 @@ public class Short2LongLinkedOpenHashMap extends Short2LongOpenHashMap implement state = operator.applyAsLong(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0L; + if(size() <= 0) return OptionalLong.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalLong.of(values[index]); index = (int)links[index]; } - return 0L; + return OptionalLong.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/shorts/maps/impl/hash/Short2LongOpenHashMap.java b/src/main/java/speiger/src/collections/shorts/maps/impl/hash/Short2LongOpenHashMap.java index 2603732..f204d01 100644 --- a/src/main/java/speiger/src/collections/shorts/maps/impl/hash/Short2LongOpenHashMap.java +++ b/src/main/java/speiger/src/collections/shorts/maps/impl/hash/Short2LongOpenHashMap.java @@ -5,10 +5,12 @@ 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; import java.util.function.LongPredicate; +import java.util.OptionalLong; import speiger.src.collections.shorts.collections.ShortIterator; import speiger.src.collections.shorts.functions.ShortConsumer; @@ -20,6 +22,7 @@ import speiger.src.collections.shorts.functions.consumer.ShortLongConsumer; import speiger.src.collections.shorts.functions.function.Short2LongFunction; import speiger.src.collections.shorts.functions.function.ShortLongUnaryOperator; import speiger.src.collections.shorts.functions.function.ShortShortUnaryOperator; +import speiger.src.collections.shorts.functions.OptionalShort; import speiger.src.collections.shorts.functions.function.ShortPredicate; import speiger.src.collections.shorts.maps.abstracts.AbstractShort2LongMap; import speiger.src.collections.shorts.maps.interfaces.Short2LongMap; @@ -920,7 +923,7 @@ public class Short2LongOpenHashMap extends AbstractShort2LongMap implements ITri } @Override - public Short2LongMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Short2LongMap.Entry state = null; boolean empty = true; @@ -937,25 +940,25 @@ public class Short2LongOpenHashMap extends AbstractShort2LongMap implements ITri } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Short2LongMap.Entry 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(keys[i] != (short)0) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1127,7 +1130,7 @@ public class Short2LongOpenHashMap extends AbstractShort2LongMap implements ITri } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -1144,18 +1147,18 @@ public class Short2LongOpenHashMap extends AbstractShort2LongMap implements ITri } state = operator.applyAsShort(state, keys[i]); } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (short)0; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OptionalShort.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OptionalShort.of(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(keys[i] != (short)0 && filter.test(keys[i])) return keys[i]; + if(keys[i] != (short)0 && filter.test(keys[i])) return OptionalShort.of(keys[i]); } - return (short)0; + return OptionalShort.empty(); } @Override @@ -1270,7 +1273,7 @@ public class Short2LongOpenHashMap extends AbstractShort2LongMap implements ITri } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -1287,18 +1290,18 @@ public class Short2LongOpenHashMap extends AbstractShort2LongMap implements ITri } state = operator.applyAsLong(state, values[i]); } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0L; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return OptionalLong.empty(); + if(containsNull && filter.test(values[nullIndex])) return OptionalLong.of(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(keys[i] != (short)0 && filter.test(values[i])) return values[i]; + if(keys[i] != (short)0 && filter.test(values[i])) return OptionalLong.of(values[i]); } - return 0L; + return OptionalLong.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/shorts/maps/impl/hash/Short2ObjectLinkedOpenHashMap.java b/src/main/java/speiger/src/collections/shorts/maps/impl/hash/Short2ObjectLinkedOpenHashMap.java index 7ce1faa..c18efd4 100644 --- a/src/main/java/speiger/src/collections/shorts/maps/impl/hash/Short2ObjectLinkedOpenHashMap.java +++ b/src/main/java/speiger/src/collections/shorts/maps/impl/hash/Short2ObjectLinkedOpenHashMap.java @@ -4,6 +4,7 @@ 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; @@ -13,6 +14,7 @@ import speiger.src.collections.shorts.functions.ShortConsumer; import speiger.src.collections.objects.functions.consumer.ObjectShortConsumer; import speiger.src.collections.ints.functions.consumer.IntShortConsumer; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; +import speiger.src.collections.shorts.functions.OptionalShort; import speiger.src.collections.shorts.functions.function.ShortPredicate; import speiger.src.collections.shorts.functions.consumer.ShortObjectConsumer; import speiger.src.collections.shorts.functions.function.ShortShortUnaryOperator; @@ -653,6 +655,10 @@ public class Short2ObjectLinkedOpenHashMap extends Short2ObjectOpenHashMap } private class MapEntrySet extends AbstractObjectSet> implements Short2ObjectOrderedMap.FastOrderedSet { + @Override + public void addFirst(Short2ObjectMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Short2ObjectMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Short2ObjectMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -818,7 +824,7 @@ public class Short2ObjectLinkedOpenHashMap extends Short2ObjectOpenHashMap } @Override - public Short2ObjectMap.Entry reduce(ObjectObjectUnaryOperator, Short2ObjectMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Short2ObjectMap.Entry> operator) { Objects.requireNonNull(operator); Short2ObjectMap.Entry state = null; boolean empty = true; @@ -833,21 +839,21 @@ public class Short2ObjectLinkedOpenHashMap extends Short2ObjectOpenHashMap state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Short2ObjectMap.Entry 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 @@ -923,6 +929,12 @@ public class Short2ObjectLinkedOpenHashMap extends Short2ObjectOpenHashMap @Override public boolean add(short o) { throw new UnsupportedOperationException(); } + + @Override + public void addFirst(short o) { throw new UnsupportedOperationException(); } + + @Override + public void addLast(short o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(short o) { throw new UnsupportedOperationException(); } @@ -1069,7 +1081,7 @@ public class Short2ObjectLinkedOpenHashMap extends Short2ObjectOpenHashMap } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -1084,19 +1096,19 @@ public class Short2ObjectLinkedOpenHashMap extends Short2ObjectOpenHashMap state = operator.applyAsShort(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (short)0; + if(size() <= 0) return OptionalShort.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalShort.of(keys[index]); index = (int)links[index]; } - return (short)0; + return OptionalShort.empty(); } @Override @@ -1233,7 +1245,7 @@ public class Short2ObjectLinkedOpenHashMap extends Short2ObjectOpenHashMap } @Override - public V reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); V state = null; boolean empty = true; @@ -1248,19 +1260,19 @@ public class Short2ObjectLinkedOpenHashMap extends Short2ObjectOpenHashMap state = operator.apply(state, values[index]); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public V findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return Optional.ofNullable(values[index]); index = (int)links[index]; } - return null; + return Optional.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/shorts/maps/impl/hash/Short2ObjectOpenHashMap.java b/src/main/java/speiger/src/collections/shorts/maps/impl/hash/Short2ObjectOpenHashMap.java index 86d1a5b..ece37a8 100644 --- a/src/main/java/speiger/src/collections/shorts/maps/impl/hash/Short2ObjectOpenHashMap.java +++ b/src/main/java/speiger/src/collections/shorts/maps/impl/hash/Short2ObjectOpenHashMap.java @@ -5,6 +5,7 @@ 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; @@ -18,6 +19,7 @@ import speiger.src.collections.shorts.functions.consumer.ShortObjectConsumer; import speiger.src.collections.shorts.functions.function.ShortFunction; import speiger.src.collections.shorts.functions.function.ShortObjectUnaryOperator; import speiger.src.collections.shorts.functions.function.ShortShortUnaryOperator; +import speiger.src.collections.shorts.functions.OptionalShort; import speiger.src.collections.shorts.functions.function.ShortPredicate; import speiger.src.collections.shorts.maps.abstracts.AbstractShort2ObjectMap; import speiger.src.collections.shorts.maps.interfaces.Short2ObjectMap; @@ -837,7 +839,7 @@ public class Short2ObjectOpenHashMap extends AbstractShort2ObjectMap imple } @Override - public Short2ObjectMap.Entry reduce(ObjectObjectUnaryOperator, Short2ObjectMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Short2ObjectMap.Entry> operator) { Objects.requireNonNull(operator); Short2ObjectMap.Entry state = null; boolean empty = true; @@ -854,25 +856,25 @@ public class Short2ObjectOpenHashMap extends AbstractShort2ObjectMap imple } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Short2ObjectMap.Entry 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(keys[i] != (short)0) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1044,7 +1046,7 @@ public class Short2ObjectOpenHashMap extends AbstractShort2ObjectMap imple } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -1061,18 +1063,18 @@ public class Short2ObjectOpenHashMap extends AbstractShort2ObjectMap imple } state = operator.applyAsShort(state, keys[i]); } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (short)0; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OptionalShort.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OptionalShort.of(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(keys[i] != (short)0 && filter.test(keys[i])) return keys[i]; + if(keys[i] != (short)0 && filter.test(keys[i])) return OptionalShort.of(keys[i]); } - return (short)0; + return OptionalShort.empty(); } @Override @@ -1187,7 +1189,7 @@ public class Short2ObjectOpenHashMap extends AbstractShort2ObjectMap imple } @Override - public V reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); V state = null; boolean empty = true; @@ -1204,18 +1206,18 @@ public class Short2ObjectOpenHashMap extends AbstractShort2ObjectMap imple } state = operator.apply(state, values[i]); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public V findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return Optional.empty(); + if(containsNull && filter.test(values[nullIndex])) return Optional.ofNullable(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(keys[i] != (short)0 && filter.test(values[i])) return values[i]; + if(keys[i] != (short)0 && filter.test(values[i])) return Optional.ofNullable(values[i]); } - return null; + return Optional.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/shorts/maps/impl/hash/Short2ShortLinkedOpenHashMap.java b/src/main/java/speiger/src/collections/shorts/maps/impl/hash/Short2ShortLinkedOpenHashMap.java index 2acd083..5f429a1 100644 --- a/src/main/java/speiger/src/collections/shorts/maps/impl/hash/Short2ShortLinkedOpenHashMap.java +++ b/src/main/java/speiger/src/collections/shorts/maps/impl/hash/Short2ShortLinkedOpenHashMap.java @@ -4,6 +4,7 @@ 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; @@ -13,6 +14,7 @@ import speiger.src.collections.shorts.functions.ShortConsumer; import speiger.src.collections.objects.functions.consumer.ObjectShortConsumer; import speiger.src.collections.ints.functions.consumer.IntShortConsumer; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; +import speiger.src.collections.shorts.functions.OptionalShort; import speiger.src.collections.shorts.functions.function.ShortPredicate; import speiger.src.collections.shorts.functions.consumer.ShortShortConsumer; import speiger.src.collections.shorts.functions.function.ShortShortUnaryOperator; @@ -662,6 +664,10 @@ public class Short2ShortLinkedOpenHashMap extends Short2ShortOpenHashMap impleme } private class MapEntrySet extends AbstractObjectSet implements Short2ShortOrderedMap.FastOrderedSet { + @Override + public void addFirst(Short2ShortMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Short2ShortMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Short2ShortMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -827,7 +833,7 @@ public class Short2ShortLinkedOpenHashMap extends Short2ShortOpenHashMap impleme } @Override - public Short2ShortMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Short2ShortMap.Entry state = null; boolean empty = true; @@ -842,21 +848,21 @@ public class Short2ShortLinkedOpenHashMap extends Short2ShortOpenHashMap impleme state = operator.apply(state, new ValueMapEntry(index)); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Short2ShortMap.Entry 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 @@ -932,6 +938,12 @@ public class Short2ShortLinkedOpenHashMap extends Short2ShortOpenHashMap impleme @Override public boolean add(short o) { throw new UnsupportedOperationException(); } + + @Override + public void addFirst(short o) { throw new UnsupportedOperationException(); } + + @Override + public void addLast(short o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(short o) { throw new UnsupportedOperationException(); } @@ -1078,7 +1090,7 @@ public class Short2ShortLinkedOpenHashMap extends Short2ShortOpenHashMap impleme } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -1093,19 +1105,19 @@ public class Short2ShortLinkedOpenHashMap extends Short2ShortOpenHashMap impleme state = operator.applyAsShort(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (short)0; + if(size() <= 0) return OptionalShort.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalShort.of(keys[index]); index = (int)links[index]; } - return (short)0; + return OptionalShort.empty(); } @Override @@ -1241,7 +1253,7 @@ public class Short2ShortLinkedOpenHashMap extends Short2ShortOpenHashMap impleme } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -1256,19 +1268,19 @@ public class Short2ShortLinkedOpenHashMap extends Short2ShortOpenHashMap impleme state = operator.applyAsShort(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (short)0; + if(size() <= 0) return OptionalShort.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalShort.of(values[index]); index = (int)links[index]; } - return (short)0; + return OptionalShort.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/shorts/maps/impl/hash/Short2ShortOpenHashMap.java b/src/main/java/speiger/src/collections/shorts/maps/impl/hash/Short2ShortOpenHashMap.java index 5c532aa..26fae47 100644 --- a/src/main/java/speiger/src/collections/shorts/maps/impl/hash/Short2ShortOpenHashMap.java +++ b/src/main/java/speiger/src/collections/shorts/maps/impl/hash/Short2ShortOpenHashMap.java @@ -5,6 +5,7 @@ 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; @@ -17,6 +18,7 @@ import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.shorts.functions.consumer.ShortShortConsumer; import speiger.src.collections.shorts.functions.function.ShortUnaryOperator; import speiger.src.collections.shorts.functions.function.ShortShortUnaryOperator; +import speiger.src.collections.shorts.functions.OptionalShort; import speiger.src.collections.shorts.functions.function.ShortPredicate; import speiger.src.collections.shorts.maps.abstracts.AbstractShort2ShortMap; import speiger.src.collections.shorts.maps.interfaces.Short2ShortMap; @@ -914,7 +916,7 @@ public class Short2ShortOpenHashMap extends AbstractShort2ShortMap implements IT } @Override - public Short2ShortMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Short2ShortMap.Entry state = null; boolean empty = true; @@ -931,25 +933,25 @@ public class Short2ShortOpenHashMap extends AbstractShort2ShortMap implements IT } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Short2ShortMap.Entry 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(keys[i] != (short)0) { entry.set(i); - if(filter.test(entry)) return entry; + if(filter.test(entry)) return Optional.ofNullable(entry); } } - return null; + return Optional.empty(); } @Override @@ -1121,7 +1123,7 @@ public class Short2ShortOpenHashMap extends AbstractShort2ShortMap implements IT } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -1138,18 +1140,18 @@ public class Short2ShortOpenHashMap extends AbstractShort2ShortMap implements IT } state = operator.applyAsShort(state, keys[i]); } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (short)0; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OptionalShort.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OptionalShort.of(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(keys[i] != (short)0 && filter.test(keys[i])) return keys[i]; + if(keys[i] != (short)0 && filter.test(keys[i])) return OptionalShort.of(keys[i]); } - return (short)0; + return OptionalShort.empty(); } @Override @@ -1264,7 +1266,7 @@ public class Short2ShortOpenHashMap extends AbstractShort2ShortMap implements IT } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -1281,18 +1283,18 @@ public class Short2ShortOpenHashMap extends AbstractShort2ShortMap implements IT } state = operator.applyAsShort(state, values[i]); } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (short)0; - if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; + if(size() <= 0) return OptionalShort.empty(); + if(containsNull && filter.test(values[nullIndex])) return OptionalShort.of(values[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(keys[i] != (short)0 && filter.test(values[i])) return values[i]; + if(keys[i] != (short)0 && filter.test(values[i])) return OptionalShort.of(values[i]); } - return (short)0; + return OptionalShort.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/shorts/maps/impl/immutable/ImmutableShort2BooleanOpenHashMap.java b/src/main/java/speiger/src/collections/shorts/maps/impl/immutable/ImmutableShort2BooleanOpenHashMap.java index 242bb05..b155a9e 100644 --- a/src/main/java/speiger/src/collections/shorts/maps/impl/immutable/ImmutableShort2BooleanOpenHashMap.java +++ b/src/main/java/speiger/src/collections/shorts/maps/impl/immutable/ImmutableShort2BooleanOpenHashMap.java @@ -4,6 +4,7 @@ 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; @@ -14,6 +15,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectShortConsumer; import speiger.src.collections.ints.functions.consumer.IntShortConsumer; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntBooleanConsumer; +import speiger.src.collections.shorts.functions.OptionalShort; import speiger.src.collections.shorts.functions.consumer.ShortBooleanConsumer; import speiger.src.collections.shorts.functions.function.ShortPredicate; import speiger.src.collections.shorts.functions.function.ShortBooleanUnaryOperator; @@ -28,6 +30,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectBooleanConsumer; import speiger.src.collections.booleans.functions.function.BooleanPredicate; +import speiger.src.collections.booleans.functions.OptionalBoolean; import speiger.src.collections.shorts.sets.AbstractShortSet; import speiger.src.collections.booleans.collections.AbstractBooleanCollection; import speiger.src.collections.booleans.collections.BooleanOrderedCollection; @@ -533,6 +536,10 @@ public class ImmutableShort2BooleanOpenHashMap extends AbstractShort2BooleanMap } private class MapEntrySet extends AbstractObjectSet implements Short2BooleanOrderedMap.FastOrderedSet { + @Override + public void addFirst(Short2BooleanMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Short2BooleanMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Short2BooleanMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -686,7 +693,7 @@ public class ImmutableShort2BooleanOpenHashMap extends AbstractShort2BooleanMap } @Override - public Short2BooleanMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Short2BooleanMap.Entry state = null; boolean empty = true; @@ -701,21 +708,21 @@ public class ImmutableShort2BooleanOpenHashMap extends AbstractShort2BooleanMap state = operator.apply(state, new BasicEntry(keys[index], values[index])); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Short2BooleanMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry entry = new BasicEntry(); 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 @@ -778,6 +785,12 @@ public class ImmutableShort2BooleanOpenHashMap extends AbstractShort2BooleanMap throw new UnsupportedOperationException(); } + @Override + public void addFirst(short o) { throw new UnsupportedOperationException(); } + + @Override + public void addLast(short o) { throw new UnsupportedOperationException(); } + @Override public boolean addAndMoveToFirst(short o) { throw new UnsupportedOperationException(); } @@ -913,7 +926,7 @@ public class ImmutableShort2BooleanOpenHashMap extends AbstractShort2BooleanMap } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -928,19 +941,19 @@ public class ImmutableShort2BooleanOpenHashMap extends AbstractShort2BooleanMap state = operator.applyAsShort(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (short)0; + if(size() <= 0) return OptionalShort.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalShort.of(keys[index]); index = (int)links[index]; } - return (short)0; + return OptionalShort.empty(); } @Override @@ -1067,7 +1080,7 @@ public class ImmutableShort2BooleanOpenHashMap extends AbstractShort2BooleanMap } @Override - public boolean reduce(BooleanBooleanUnaryOperator operator) { + public OptionalBoolean reduce(BooleanBooleanUnaryOperator operator) { Objects.requireNonNull(operator); boolean state = false; boolean empty = true; @@ -1082,19 +1095,19 @@ public class ImmutableShort2BooleanOpenHashMap extends AbstractShort2BooleanMap state = operator.applyAsBoolean(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalBoolean.empty() : OptionalBoolean.of(state); } @Override - public boolean findFirst(BooleanPredicate filter) { + public OptionalBoolean findFirst(BooleanPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return false; + if(size() <= 0) return OptionalBoolean.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalBoolean.of(values[index]); index = (int)links[index]; } - return false; + return OptionalBoolean.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/shorts/maps/impl/immutable/ImmutableShort2ByteOpenHashMap.java b/src/main/java/speiger/src/collections/shorts/maps/impl/immutable/ImmutableShort2ByteOpenHashMap.java index 628423a..c5ce3aa 100644 --- a/src/main/java/speiger/src/collections/shorts/maps/impl/immutable/ImmutableShort2ByteOpenHashMap.java +++ b/src/main/java/speiger/src/collections/shorts/maps/impl/immutable/ImmutableShort2ByteOpenHashMap.java @@ -4,6 +4,7 @@ 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; @@ -14,6 +15,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectShortConsumer; import speiger.src.collections.ints.functions.consumer.IntShortConsumer; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntByteConsumer; +import speiger.src.collections.shorts.functions.OptionalShort; import speiger.src.collections.shorts.functions.function.ShortPredicate; import speiger.src.collections.shorts.functions.consumer.ShortByteConsumer; import speiger.src.collections.shorts.functions.function.Short2ByteFunction; @@ -29,6 +31,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectByteConsumer; import speiger.src.collections.bytes.functions.function.BytePredicate; +import speiger.src.collections.bytes.functions.OptionalByte; import speiger.src.collections.shorts.sets.AbstractShortSet; import speiger.src.collections.bytes.collections.AbstractByteCollection; import speiger.src.collections.bytes.collections.ByteOrderedCollection; @@ -538,6 +541,10 @@ public class ImmutableShort2ByteOpenHashMap extends AbstractShort2ByteMap implem } private class MapEntrySet extends AbstractObjectSet implements Short2ByteOrderedMap.FastOrderedSet { + @Override + public void addFirst(Short2ByteMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Short2ByteMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Short2ByteMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -691,7 +698,7 @@ public class ImmutableShort2ByteOpenHashMap extends AbstractShort2ByteMap implem } @Override - public Short2ByteMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Short2ByteMap.Entry state = null; boolean empty = true; @@ -706,21 +713,21 @@ public class ImmutableShort2ByteOpenHashMap extends AbstractShort2ByteMap implem state = operator.apply(state, new BasicEntry(keys[index], values[index])); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Short2ByteMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry entry = new BasicEntry(); 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 @@ -783,6 +790,12 @@ public class ImmutableShort2ByteOpenHashMap extends AbstractShort2ByteMap implem throw new UnsupportedOperationException(); } + @Override + public void addFirst(short o) { throw new UnsupportedOperationException(); } + + @Override + public void addLast(short o) { throw new UnsupportedOperationException(); } + @Override public boolean addAndMoveToFirst(short o) { throw new UnsupportedOperationException(); } @@ -918,7 +931,7 @@ public class ImmutableShort2ByteOpenHashMap extends AbstractShort2ByteMap implem } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -933,19 +946,19 @@ public class ImmutableShort2ByteOpenHashMap extends AbstractShort2ByteMap implem state = operator.applyAsShort(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (short)0; + if(size() <= 0) return OptionalShort.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalShort.of(keys[index]); index = (int)links[index]; } - return (short)0; + return OptionalShort.empty(); } @Override @@ -1072,7 +1085,7 @@ public class ImmutableShort2ByteOpenHashMap extends AbstractShort2ByteMap implem } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -1087,19 +1100,19 @@ public class ImmutableShort2ByteOpenHashMap extends AbstractShort2ByteMap implem state = operator.applyAsByte(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (byte)0; + if(size() <= 0) return OptionalByte.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalByte.of(values[index]); index = (int)links[index]; } - return (byte)0; + return OptionalByte.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/shorts/maps/impl/immutable/ImmutableShort2CharOpenHashMap.java b/src/main/java/speiger/src/collections/shorts/maps/impl/immutable/ImmutableShort2CharOpenHashMap.java index 804f35b..2bf2d8a 100644 --- a/src/main/java/speiger/src/collections/shorts/maps/impl/immutable/ImmutableShort2CharOpenHashMap.java +++ b/src/main/java/speiger/src/collections/shorts/maps/impl/immutable/ImmutableShort2CharOpenHashMap.java @@ -4,6 +4,7 @@ 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; @@ -14,6 +15,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectShortConsumer; import speiger.src.collections.ints.functions.consumer.IntShortConsumer; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntCharConsumer; +import speiger.src.collections.shorts.functions.OptionalShort; import speiger.src.collections.shorts.functions.function.ShortPredicate; import speiger.src.collections.shorts.functions.consumer.ShortCharConsumer; import speiger.src.collections.shorts.functions.function.Short2CharFunction; @@ -29,6 +31,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectCharConsumer; import speiger.src.collections.chars.functions.function.CharPredicate; +import speiger.src.collections.chars.functions.OptionalChar; import speiger.src.collections.shorts.sets.AbstractShortSet; import speiger.src.collections.chars.collections.AbstractCharCollection; import speiger.src.collections.chars.collections.CharOrderedCollection; @@ -538,6 +541,10 @@ public class ImmutableShort2CharOpenHashMap extends AbstractShort2CharMap implem } private class MapEntrySet extends AbstractObjectSet implements Short2CharOrderedMap.FastOrderedSet { + @Override + public void addFirst(Short2CharMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Short2CharMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Short2CharMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -691,7 +698,7 @@ public class ImmutableShort2CharOpenHashMap extends AbstractShort2CharMap implem } @Override - public Short2CharMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Short2CharMap.Entry state = null; boolean empty = true; @@ -706,21 +713,21 @@ public class ImmutableShort2CharOpenHashMap extends AbstractShort2CharMap implem state = operator.apply(state, new BasicEntry(keys[index], values[index])); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Short2CharMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry entry = new BasicEntry(); 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 @@ -783,6 +790,12 @@ public class ImmutableShort2CharOpenHashMap extends AbstractShort2CharMap implem throw new UnsupportedOperationException(); } + @Override + public void addFirst(short o) { throw new UnsupportedOperationException(); } + + @Override + public void addLast(short o) { throw new UnsupportedOperationException(); } + @Override public boolean addAndMoveToFirst(short o) { throw new UnsupportedOperationException(); } @@ -918,7 +931,7 @@ public class ImmutableShort2CharOpenHashMap extends AbstractShort2CharMap implem } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -933,19 +946,19 @@ public class ImmutableShort2CharOpenHashMap extends AbstractShort2CharMap implem state = operator.applyAsShort(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (short)0; + if(size() <= 0) return OptionalShort.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalShort.of(keys[index]); index = (int)links[index]; } - return (short)0; + return OptionalShort.empty(); } @Override @@ -1072,7 +1085,7 @@ public class ImmutableShort2CharOpenHashMap extends AbstractShort2CharMap implem } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -1087,19 +1100,19 @@ public class ImmutableShort2CharOpenHashMap extends AbstractShort2CharMap implem state = operator.applyAsChar(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (char)0; + if(size() <= 0) return OptionalChar.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalChar.of(values[index]); index = (int)links[index]; } - return (char)0; + return OptionalChar.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/shorts/maps/impl/immutable/ImmutableShort2DoubleOpenHashMap.java b/src/main/java/speiger/src/collections/shorts/maps/impl/immutable/ImmutableShort2DoubleOpenHashMap.java index 31b50ec..6a7b3d8 100644 --- a/src/main/java/speiger/src/collections/shorts/maps/impl/immutable/ImmutableShort2DoubleOpenHashMap.java +++ b/src/main/java/speiger/src/collections/shorts/maps/impl/immutable/ImmutableShort2DoubleOpenHashMap.java @@ -4,10 +4,12 @@ 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; import java.util.function.DoublePredicate; +import java.util.OptionalDouble; import speiger.src.collections.shorts.collections.ShortBidirectionalIterator; import speiger.src.collections.shorts.functions.ShortConsumer; @@ -15,6 +17,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectShortConsumer; import speiger.src.collections.ints.functions.consumer.IntShortConsumer; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntDoubleConsumer; +import speiger.src.collections.shorts.functions.OptionalShort; import speiger.src.collections.shorts.functions.function.ShortPredicate; import speiger.src.collections.shorts.functions.consumer.ShortDoubleConsumer; import speiger.src.collections.shorts.functions.function.Short2DoubleFunction; @@ -538,6 +541,10 @@ public class ImmutableShort2DoubleOpenHashMap extends AbstractShort2DoubleMap im } private class MapEntrySet extends AbstractObjectSet implements Short2DoubleOrderedMap.FastOrderedSet { + @Override + public void addFirst(Short2DoubleMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Short2DoubleMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Short2DoubleMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -691,7 +698,7 @@ public class ImmutableShort2DoubleOpenHashMap extends AbstractShort2DoubleMap im } @Override - public Short2DoubleMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Short2DoubleMap.Entry state = null; boolean empty = true; @@ -706,21 +713,21 @@ public class ImmutableShort2DoubleOpenHashMap extends AbstractShort2DoubleMap im state = operator.apply(state, new BasicEntry(keys[index], values[index])); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Short2DoubleMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry entry = new BasicEntry(); 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 @@ -783,6 +790,12 @@ public class ImmutableShort2DoubleOpenHashMap extends AbstractShort2DoubleMap im throw new UnsupportedOperationException(); } + @Override + public void addFirst(short o) { throw new UnsupportedOperationException(); } + + @Override + public void addLast(short o) { throw new UnsupportedOperationException(); } + @Override public boolean addAndMoveToFirst(short o) { throw new UnsupportedOperationException(); } @@ -918,7 +931,7 @@ public class ImmutableShort2DoubleOpenHashMap extends AbstractShort2DoubleMap im } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -933,19 +946,19 @@ public class ImmutableShort2DoubleOpenHashMap extends AbstractShort2DoubleMap im state = operator.applyAsShort(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (short)0; + if(size() <= 0) return OptionalShort.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalShort.of(keys[index]); index = (int)links[index]; } - return (short)0; + return OptionalShort.empty(); } @Override @@ -1072,7 +1085,7 @@ public class ImmutableShort2DoubleOpenHashMap extends AbstractShort2DoubleMap im } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -1087,19 +1100,19 @@ public class ImmutableShort2DoubleOpenHashMap extends AbstractShort2DoubleMap im state = operator.applyAsDouble(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0D; + if(size() <= 0) return OptionalDouble.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalDouble.of(values[index]); index = (int)links[index]; } - return 0D; + return OptionalDouble.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/shorts/maps/impl/immutable/ImmutableShort2FloatOpenHashMap.java b/src/main/java/speiger/src/collections/shorts/maps/impl/immutable/ImmutableShort2FloatOpenHashMap.java index 739efde..2166112 100644 --- a/src/main/java/speiger/src/collections/shorts/maps/impl/immutable/ImmutableShort2FloatOpenHashMap.java +++ b/src/main/java/speiger/src/collections/shorts/maps/impl/immutable/ImmutableShort2FloatOpenHashMap.java @@ -4,6 +4,7 @@ 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; @@ -14,6 +15,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectShortConsumer; import speiger.src.collections.ints.functions.consumer.IntShortConsumer; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntFloatConsumer; +import speiger.src.collections.shorts.functions.OptionalShort; import speiger.src.collections.shorts.functions.function.ShortPredicate; import speiger.src.collections.shorts.functions.consumer.ShortFloatConsumer; import speiger.src.collections.shorts.functions.function.Short2FloatFunction; @@ -29,6 +31,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectFloatConsumer; import speiger.src.collections.floats.functions.function.FloatPredicate; +import speiger.src.collections.floats.functions.OptionalFloat; import speiger.src.collections.shorts.sets.AbstractShortSet; import speiger.src.collections.floats.collections.AbstractFloatCollection; import speiger.src.collections.floats.collections.FloatOrderedCollection; @@ -538,6 +541,10 @@ public class ImmutableShort2FloatOpenHashMap extends AbstractShort2FloatMap impl } private class MapEntrySet extends AbstractObjectSet implements Short2FloatOrderedMap.FastOrderedSet { + @Override + public void addFirst(Short2FloatMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Short2FloatMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Short2FloatMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -691,7 +698,7 @@ public class ImmutableShort2FloatOpenHashMap extends AbstractShort2FloatMap impl } @Override - public Short2FloatMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Short2FloatMap.Entry state = null; boolean empty = true; @@ -706,21 +713,21 @@ public class ImmutableShort2FloatOpenHashMap extends AbstractShort2FloatMap impl state = operator.apply(state, new BasicEntry(keys[index], values[index])); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Short2FloatMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry entry = new BasicEntry(); 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 @@ -783,6 +790,12 @@ public class ImmutableShort2FloatOpenHashMap extends AbstractShort2FloatMap impl throw new UnsupportedOperationException(); } + @Override + public void addFirst(short o) { throw new UnsupportedOperationException(); } + + @Override + public void addLast(short o) { throw new UnsupportedOperationException(); } + @Override public boolean addAndMoveToFirst(short o) { throw new UnsupportedOperationException(); } @@ -918,7 +931,7 @@ public class ImmutableShort2FloatOpenHashMap extends AbstractShort2FloatMap impl } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -933,19 +946,19 @@ public class ImmutableShort2FloatOpenHashMap extends AbstractShort2FloatMap impl state = operator.applyAsShort(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (short)0; + if(size() <= 0) return OptionalShort.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalShort.of(keys[index]); index = (int)links[index]; } - return (short)0; + return OptionalShort.empty(); } @Override @@ -1072,7 +1085,7 @@ public class ImmutableShort2FloatOpenHashMap extends AbstractShort2FloatMap impl } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -1087,19 +1100,19 @@ public class ImmutableShort2FloatOpenHashMap extends AbstractShort2FloatMap impl state = operator.applyAsFloat(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0F; + if(size() <= 0) return OptionalFloat.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalFloat.of(values[index]); index = (int)links[index]; } - return 0F; + return OptionalFloat.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/shorts/maps/impl/immutable/ImmutableShort2IntOpenHashMap.java b/src/main/java/speiger/src/collections/shorts/maps/impl/immutable/ImmutableShort2IntOpenHashMap.java index f2c6634..93a77f0 100644 --- a/src/main/java/speiger/src/collections/shorts/maps/impl/immutable/ImmutableShort2IntOpenHashMap.java +++ b/src/main/java/speiger/src/collections/shorts/maps/impl/immutable/ImmutableShort2IntOpenHashMap.java @@ -4,10 +4,12 @@ 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; import java.util.function.IntPredicate; +import java.util.OptionalInt; import speiger.src.collections.shorts.collections.ShortBidirectionalIterator; import speiger.src.collections.shorts.functions.ShortConsumer; @@ -15,6 +17,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectShortConsumer; import speiger.src.collections.ints.functions.consumer.IntShortConsumer; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntIntConsumer; +import speiger.src.collections.shorts.functions.OptionalShort; import speiger.src.collections.shorts.functions.function.ShortPredicate; import speiger.src.collections.shorts.functions.consumer.ShortIntConsumer; import speiger.src.collections.shorts.functions.function.Short2IntFunction; @@ -538,6 +541,10 @@ public class ImmutableShort2IntOpenHashMap extends AbstractShort2IntMap implemen } private class MapEntrySet extends AbstractObjectSet implements Short2IntOrderedMap.FastOrderedSet { + @Override + public void addFirst(Short2IntMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Short2IntMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Short2IntMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -691,7 +698,7 @@ public class ImmutableShort2IntOpenHashMap extends AbstractShort2IntMap implemen } @Override - public Short2IntMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Short2IntMap.Entry state = null; boolean empty = true; @@ -706,21 +713,21 @@ public class ImmutableShort2IntOpenHashMap extends AbstractShort2IntMap implemen state = operator.apply(state, new BasicEntry(keys[index], values[index])); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Short2IntMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry entry = new BasicEntry(); 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 @@ -783,6 +790,12 @@ public class ImmutableShort2IntOpenHashMap extends AbstractShort2IntMap implemen throw new UnsupportedOperationException(); } + @Override + public void addFirst(short o) { throw new UnsupportedOperationException(); } + + @Override + public void addLast(short o) { throw new UnsupportedOperationException(); } + @Override public boolean addAndMoveToFirst(short o) { throw new UnsupportedOperationException(); } @@ -918,7 +931,7 @@ public class ImmutableShort2IntOpenHashMap extends AbstractShort2IntMap implemen } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -933,19 +946,19 @@ public class ImmutableShort2IntOpenHashMap extends AbstractShort2IntMap implemen state = operator.applyAsShort(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (short)0; + if(size() <= 0) return OptionalShort.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalShort.of(keys[index]); index = (int)links[index]; } - return (short)0; + return OptionalShort.empty(); } @Override @@ -1072,7 +1085,7 @@ public class ImmutableShort2IntOpenHashMap extends AbstractShort2IntMap implemen } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -1087,19 +1100,19 @@ public class ImmutableShort2IntOpenHashMap extends AbstractShort2IntMap implemen state = operator.applyAsInt(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0; + if(size() <= 0) return OptionalInt.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalInt.of(values[index]); index = (int)links[index]; } - return 0; + return OptionalInt.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/shorts/maps/impl/immutable/ImmutableShort2LongOpenHashMap.java b/src/main/java/speiger/src/collections/shorts/maps/impl/immutable/ImmutableShort2LongOpenHashMap.java index f920cf7..91f425b 100644 --- a/src/main/java/speiger/src/collections/shorts/maps/impl/immutable/ImmutableShort2LongOpenHashMap.java +++ b/src/main/java/speiger/src/collections/shorts/maps/impl/immutable/ImmutableShort2LongOpenHashMap.java @@ -4,10 +4,12 @@ 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; import java.util.function.LongPredicate; +import java.util.OptionalLong; import speiger.src.collections.shorts.collections.ShortBidirectionalIterator; import speiger.src.collections.shorts.functions.ShortConsumer; @@ -15,6 +17,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectShortConsumer; import speiger.src.collections.ints.functions.consumer.IntShortConsumer; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntLongConsumer; +import speiger.src.collections.shorts.functions.OptionalShort; import speiger.src.collections.shorts.functions.function.ShortPredicate; import speiger.src.collections.shorts.functions.consumer.ShortLongConsumer; import speiger.src.collections.shorts.functions.function.Short2LongFunction; @@ -538,6 +541,10 @@ public class ImmutableShort2LongOpenHashMap extends AbstractShort2LongMap implem } private class MapEntrySet extends AbstractObjectSet implements Short2LongOrderedMap.FastOrderedSet { + @Override + public void addFirst(Short2LongMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Short2LongMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Short2LongMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -691,7 +698,7 @@ public class ImmutableShort2LongOpenHashMap extends AbstractShort2LongMap implem } @Override - public Short2LongMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Short2LongMap.Entry state = null; boolean empty = true; @@ -706,21 +713,21 @@ public class ImmutableShort2LongOpenHashMap extends AbstractShort2LongMap implem state = operator.apply(state, new BasicEntry(keys[index], values[index])); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Short2LongMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry entry = new BasicEntry(); 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 @@ -783,6 +790,12 @@ public class ImmutableShort2LongOpenHashMap extends AbstractShort2LongMap implem throw new UnsupportedOperationException(); } + @Override + public void addFirst(short o) { throw new UnsupportedOperationException(); } + + @Override + public void addLast(short o) { throw new UnsupportedOperationException(); } + @Override public boolean addAndMoveToFirst(short o) { throw new UnsupportedOperationException(); } @@ -918,7 +931,7 @@ public class ImmutableShort2LongOpenHashMap extends AbstractShort2LongMap implem } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -933,19 +946,19 @@ public class ImmutableShort2LongOpenHashMap extends AbstractShort2LongMap implem state = operator.applyAsShort(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (short)0; + if(size() <= 0) return OptionalShort.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalShort.of(keys[index]); index = (int)links[index]; } - return (short)0; + return OptionalShort.empty(); } @Override @@ -1072,7 +1085,7 @@ public class ImmutableShort2LongOpenHashMap extends AbstractShort2LongMap implem } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -1087,19 +1100,19 @@ public class ImmutableShort2LongOpenHashMap extends AbstractShort2LongMap implem state = operator.applyAsLong(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return 0L; + if(size() <= 0) return OptionalLong.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalLong.of(values[index]); index = (int)links[index]; } - return 0L; + return OptionalLong.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/shorts/maps/impl/immutable/ImmutableShort2ObjectOpenHashMap.java b/src/main/java/speiger/src/collections/shorts/maps/impl/immutable/ImmutableShort2ObjectOpenHashMap.java index b9b8c60..1152af6 100644 --- a/src/main/java/speiger/src/collections/shorts/maps/impl/immutable/ImmutableShort2ObjectOpenHashMap.java +++ b/src/main/java/speiger/src/collections/shorts/maps/impl/immutable/ImmutableShort2ObjectOpenHashMap.java @@ -4,6 +4,7 @@ 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; @@ -13,6 +14,7 @@ import speiger.src.collections.shorts.functions.ShortConsumer; import speiger.src.collections.objects.functions.consumer.ObjectShortConsumer; import speiger.src.collections.ints.functions.consumer.IntShortConsumer; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; +import speiger.src.collections.shorts.functions.OptionalShort; import speiger.src.collections.shorts.functions.function.ShortPredicate; import speiger.src.collections.shorts.functions.consumer.ShortObjectConsumer; import speiger.src.collections.shorts.functions.function.ShortFunction; @@ -509,6 +511,10 @@ public class ImmutableShort2ObjectOpenHashMap extends AbstractShort2ObjectMap } private class MapEntrySet extends AbstractObjectSet> implements Short2ObjectOrderedMap.FastOrderedSet { + @Override + public void addFirst(Short2ObjectMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Short2ObjectMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Short2ObjectMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -662,7 +668,7 @@ public class ImmutableShort2ObjectOpenHashMap extends AbstractShort2ObjectMap } @Override - public Short2ObjectMap.Entry reduce(ObjectObjectUnaryOperator, Short2ObjectMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Short2ObjectMap.Entry> operator) { Objects.requireNonNull(operator); Short2ObjectMap.Entry state = null; boolean empty = true; @@ -677,21 +683,21 @@ public class ImmutableShort2ObjectOpenHashMap extends AbstractShort2ObjectMap state = operator.apply(state, new BasicEntry<>(keys[index], values[index])); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Short2ObjectMap.Entry findFirst(Predicate> filter) { + public Optional> findFirst(Predicate> filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry entry = new BasicEntry<>(); 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 @@ -754,6 +760,12 @@ public class ImmutableShort2ObjectOpenHashMap extends AbstractShort2ObjectMap throw new UnsupportedOperationException(); } + @Override + public void addFirst(short o) { throw new UnsupportedOperationException(); } + + @Override + public void addLast(short o) { throw new UnsupportedOperationException(); } + @Override public boolean addAndMoveToFirst(short o) { throw new UnsupportedOperationException(); } @@ -889,7 +901,7 @@ public class ImmutableShort2ObjectOpenHashMap extends AbstractShort2ObjectMap } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -904,19 +916,19 @@ public class ImmutableShort2ObjectOpenHashMap extends AbstractShort2ObjectMap state = operator.applyAsShort(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (short)0; + if(size() <= 0) return OptionalShort.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalShort.of(keys[index]); index = (int)links[index]; } - return (short)0; + return OptionalShort.empty(); } @Override @@ -1044,7 +1056,7 @@ public class ImmutableShort2ObjectOpenHashMap extends AbstractShort2ObjectMap } @Override - public V reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); V state = null; boolean empty = true; @@ -1059,19 +1071,19 @@ public class ImmutableShort2ObjectOpenHashMap extends AbstractShort2ObjectMap state = operator.apply(state, values[index]); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public V findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return Optional.ofNullable(values[index]); index = (int)links[index]; } - return null; + return Optional.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/shorts/maps/impl/immutable/ImmutableShort2ShortOpenHashMap.java b/src/main/java/speiger/src/collections/shorts/maps/impl/immutable/ImmutableShort2ShortOpenHashMap.java index 11612f8..af0f0e6 100644 --- a/src/main/java/speiger/src/collections/shorts/maps/impl/immutable/ImmutableShort2ShortOpenHashMap.java +++ b/src/main/java/speiger/src/collections/shorts/maps/impl/immutable/ImmutableShort2ShortOpenHashMap.java @@ -4,6 +4,7 @@ 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; @@ -13,6 +14,7 @@ import speiger.src.collections.shorts.functions.ShortConsumer; import speiger.src.collections.objects.functions.consumer.ObjectShortConsumer; import speiger.src.collections.ints.functions.consumer.IntShortConsumer; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; +import speiger.src.collections.shorts.functions.OptionalShort; import speiger.src.collections.shorts.functions.function.ShortPredicate; import speiger.src.collections.shorts.functions.consumer.ShortShortConsumer; import speiger.src.collections.shorts.functions.function.ShortUnaryOperator; @@ -529,6 +531,10 @@ public class ImmutableShort2ShortOpenHashMap extends AbstractShort2ShortMap impl } private class MapEntrySet extends AbstractObjectSet implements Short2ShortOrderedMap.FastOrderedSet { + @Override + public void addFirst(Short2ShortMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Short2ShortMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Short2ShortMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -682,7 +688,7 @@ public class ImmutableShort2ShortOpenHashMap extends AbstractShort2ShortMap impl } @Override - public Short2ShortMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Short2ShortMap.Entry state = null; boolean empty = true; @@ -697,21 +703,21 @@ public class ImmutableShort2ShortOpenHashMap extends AbstractShort2ShortMap impl state = operator.apply(state, new BasicEntry(keys[index], values[index])); index = (int)links[index]; } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Short2ShortMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry entry = new BasicEntry(); 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 @@ -774,6 +780,12 @@ public class ImmutableShort2ShortOpenHashMap extends AbstractShort2ShortMap impl throw new UnsupportedOperationException(); } + @Override + public void addFirst(short o) { throw new UnsupportedOperationException(); } + + @Override + public void addLast(short o) { throw new UnsupportedOperationException(); } + @Override public boolean addAndMoveToFirst(short o) { throw new UnsupportedOperationException(); } @@ -909,7 +921,7 @@ public class ImmutableShort2ShortOpenHashMap extends AbstractShort2ShortMap impl } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -924,19 +936,19 @@ public class ImmutableShort2ShortOpenHashMap extends AbstractShort2ShortMap impl state = operator.applyAsShort(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (short)0; + if(size() <= 0) return OptionalShort.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalShort.of(keys[index]); index = (int)links[index]; } - return (short)0; + return OptionalShort.empty(); } @Override @@ -1063,7 +1075,7 @@ public class ImmutableShort2ShortOpenHashMap extends AbstractShort2ShortMap impl } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -1078,19 +1090,19 @@ public class ImmutableShort2ShortOpenHashMap extends AbstractShort2ShortMap impl state = operator.applyAsShort(state, values[index]); index = (int)links[index]; } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (short)0; + if(size() <= 0) return OptionalShort.empty(); int index = firstIndex; while(index != -1){ - if(filter.test(values[index])) return values[index]; + if(filter.test(values[index])) return OptionalShort.of(values[index]); index = (int)links[index]; } - return (short)0; + return OptionalShort.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/shorts/maps/impl/misc/Short2BooleanArrayMap.java b/src/main/java/speiger/src/collections/shorts/maps/impl/misc/Short2BooleanArrayMap.java index 78a681c..bdc1315 100644 --- a/src/main/java/speiger/src/collections/shorts/maps/impl/misc/Short2BooleanArrayMap.java +++ b/src/main/java/speiger/src/collections/shorts/maps/impl/misc/Short2BooleanArrayMap.java @@ -4,6 +4,7 @@ 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; @@ -14,6 +15,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectShortConsumer; import speiger.src.collections.ints.functions.consumer.IntShortConsumer; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntBooleanConsumer; +import speiger.src.collections.shorts.functions.OptionalShort; import speiger.src.collections.shorts.functions.consumer.ShortBooleanConsumer; import speiger.src.collections.shorts.functions.function.ShortPredicate; import speiger.src.collections.shorts.functions.function.ShortBooleanUnaryOperator; @@ -34,6 +36,7 @@ import speiger.src.collections.booleans.lists.BooleanListIterator; import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.booleans.functions.function.BooleanPredicate; +import speiger.src.collections.booleans.functions.OptionalBoolean; import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOperator; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; @@ -728,6 +731,10 @@ public class Short2BooleanArrayMap extends AbstractShort2BooleanMap implements S } private class MapEntrySet extends AbstractObjectSet implements Short2BooleanOrderedMap.FastOrderedSet { + @Override + public void addFirst(Short2BooleanMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Short2BooleanMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Short2BooleanMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -876,7 +883,7 @@ public class Short2BooleanArrayMap extends AbstractShort2BooleanMap implements S } @Override - public Short2BooleanMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Short2BooleanMap.Entry state = null; boolean empty = true; @@ -888,19 +895,19 @@ public class Short2BooleanArrayMap extends AbstractShort2BooleanMap implements S } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Short2BooleanMap.Entry 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 implements Short2ByteOrderedMap.FastOrderedSet { + @Override + public void addFirst(Short2ByteMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Short2ByteMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Short2ByteMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -899,7 +906,7 @@ public class Short2ByteArrayMap extends AbstractShort2ByteMap implements Short2B } @Override - public Short2ByteMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Short2ByteMap.Entry state = null; boolean empty = true; @@ -911,19 +918,19 @@ public class Short2ByteArrayMap extends AbstractShort2ByteMap implements Short2B } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Short2ByteMap.Entry 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 implements Short2CharOrderedMap.FastOrderedSet { + @Override + public void addFirst(Short2CharMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Short2CharMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Short2CharMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -899,7 +906,7 @@ public class Short2CharArrayMap extends AbstractShort2CharMap implements Short2C } @Override - public Short2CharMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Short2CharMap.Entry state = null; boolean empty = true; @@ -911,19 +918,19 @@ public class Short2CharArrayMap extends AbstractShort2CharMap implements Short2C } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Short2CharMap.Entry 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 implements Short2DoubleOrderedMap.FastOrderedSet { + @Override + public void addFirst(Short2DoubleMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Short2DoubleMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Short2DoubleMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -899,7 +906,7 @@ public class Short2DoubleArrayMap extends AbstractShort2DoubleMap implements Sho } @Override - public Short2DoubleMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Short2DoubleMap.Entry state = null; boolean empty = true; @@ -911,19 +918,19 @@ public class Short2DoubleArrayMap extends AbstractShort2DoubleMap implements Sho } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Short2DoubleMap.Entry 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 implements Short2FloatOrderedMap.FastOrderedSet { + @Override + public void addFirst(Short2FloatMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Short2FloatMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Short2FloatMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -899,7 +906,7 @@ public class Short2FloatArrayMap extends AbstractShort2FloatMap implements Short } @Override - public Short2FloatMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Short2FloatMap.Entry state = null; boolean empty = true; @@ -911,19 +918,19 @@ public class Short2FloatArrayMap extends AbstractShort2FloatMap implements Short } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Short2FloatMap.Entry 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 implements Short2IntOrderedMap.FastOrderedSet { + @Override + public void addFirst(Short2IntMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Short2IntMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Short2IntMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -899,7 +906,7 @@ public class Short2IntArrayMap extends AbstractShort2IntMap implements Short2Int } @Override - public Short2IntMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Short2IntMap.Entry state = null; boolean empty = true; @@ -911,19 +918,19 @@ public class Short2IntArrayMap extends AbstractShort2IntMap implements Short2Int } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Short2IntMap.Entry 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 implements Short2LongOrderedMap.FastOrderedSet { + @Override + public void addFirst(Short2LongMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Short2LongMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Short2LongMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -899,7 +906,7 @@ public class Short2LongArrayMap extends AbstractShort2LongMap implements Short2L } @Override - public Short2LongMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Short2LongMap.Entry state = null; boolean empty = true; @@ -911,19 +918,19 @@ public class Short2LongArrayMap extends AbstractShort2LongMap implements Short2L } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Short2LongMap.Entry 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 extends AbstractShort2ObjectMap implemen } private class MapEntrySet extends AbstractObjectSet> implements Short2ObjectOrderedMap.FastOrderedSet { + @Override + public void addFirst(Short2ObjectMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Short2ObjectMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Short2ObjectMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -812,7 +818,7 @@ public class Short2ObjectArrayMap extends AbstractShort2ObjectMap implemen } @Override - public Short2ObjectMap.Entry reduce(ObjectObjectUnaryOperator, Short2ObjectMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Short2ObjectMap.Entry> operator) { Objects.requireNonNull(operator); Short2ObjectMap.Entry state = null; boolean empty = true; @@ -824,19 +830,19 @@ public class Short2ObjectArrayMap extends AbstractShort2ObjectMap implemen } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Short2ObjectMap.Entry 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 extends AbstractShort2ObjectMap implemen @Override public boolean add(short o) { throw new UnsupportedOperationException(); } @Override + public void addFirst(short o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(short o) { throw new UnsupportedOperationException(); } + @Override public boolean addAndMoveToFirst(short o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToLast(short o) { throw new UnsupportedOperationException(); } @@ -995,7 +1005,7 @@ public class Short2ObjectArrayMap extends AbstractShort2ObjectMap implemen } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -1007,16 +1017,16 @@ public class Short2ObjectArrayMap extends AbstractShort2ObjectMap implemen } state = operator.applyAsShort(state, keys[i]); } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); for(int i = 0;i extends AbstractShort2ObjectMap implemen } @Override - public V reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); V state = null; boolean empty = true; @@ -1129,16 +1139,16 @@ public class Short2ObjectArrayMap extends AbstractShort2ObjectMap implemen } state = operator.apply(state, values[i]); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public V findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); for(int i = 0;i implements Short2ShortOrderedMap.FastOrderedSet { + @Override + public void addFirst(Short2ShortMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Short2ShortMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Short2ShortMap.Entry o) { throw new UnsupportedOperationException(); } @Override @@ -892,7 +898,7 @@ public class Short2ShortArrayMap extends AbstractShort2ShortMap implements Short } @Override - public Short2ShortMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Short2ShortMap.Entry state = null; boolean empty = true; @@ -904,19 +910,19 @@ public class Short2ShortArrayMap extends AbstractShort2ShortMap implements Short } state = operator.apply(state, new ValueMapEntry(i)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Short2ShortMap.Entry 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); Short2BooleanMap.Entry state = null; boolean empty = true; @@ -1724,19 +1727,19 @@ public class Short2BooleanAVLTreeMap extends AbstractShort2BooleanMap implements } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Short2BooleanMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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 @@ -1831,7 +1834,7 @@ public class Short2BooleanAVLTreeMap extends AbstractShort2BooleanMap implements } @Override - public boolean reduce(BooleanBooleanUnaryOperator operator) { + public OptionalBoolean reduce(BooleanBooleanUnaryOperator operator) { Objects.requireNonNull(operator); boolean state = false; boolean empty = true; @@ -1843,15 +1846,15 @@ public class Short2BooleanAVLTreeMap extends AbstractShort2BooleanMap implements } state = operator.applyAsBoolean(state, entry.value); } - return state; + return empty ? OptionalBoolean.empty() : OptionalBoolean.of(state); } @Override - public boolean findFirst(BooleanPredicate filter) { + public OptionalBoolean findFirst(BooleanPredicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return false; + if(filter.test(entry.value)) return OptionalBoolean.of(entry.value); + return OptionalBoolean.empty(); } @Override @@ -2167,7 +2170,7 @@ public class Short2BooleanAVLTreeMap extends AbstractShort2BooleanMap implements } @Override - public boolean reduce(BooleanBooleanUnaryOperator operator) { + public OptionalBoolean reduce(BooleanBooleanUnaryOperator operator) { Objects.requireNonNull(operator); boolean state = false; boolean empty = true; @@ -2179,15 +2182,15 @@ public class Short2BooleanAVLTreeMap extends AbstractShort2BooleanMap implements } state = operator.applyAsBoolean(state, entry.value); } - return state; + return empty ? OptionalBoolean.empty() : OptionalBoolean.of(state); } @Override - public boolean findFirst(BooleanPredicate filter) { + public OptionalBoolean findFirst(BooleanPredicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return false; + if(filter.test(entry.value)) return OptionalBoolean.of(entry.value); + return OptionalBoolean.empty(); } @Override @@ -2328,7 +2331,7 @@ public class Short2BooleanAVLTreeMap extends AbstractShort2BooleanMap implements } @Override - public Short2BooleanMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Short2BooleanMap.Entry state = null; boolean empty = true; @@ -2340,19 +2343,19 @@ public class Short2BooleanAVLTreeMap extends AbstractShort2BooleanMap implements } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Short2BooleanMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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/main/java/speiger/src/collections/shorts/maps/impl/tree/Short2BooleanRBTreeMap.java b/src/main/java/speiger/src/collections/shorts/maps/impl/tree/Short2BooleanRBTreeMap.java index 14bdd49..763ddbc 100644 --- a/src/main/java/speiger/src/collections/shorts/maps/impl/tree/Short2BooleanRBTreeMap.java +++ b/src/main/java/speiger/src/collections/shorts/maps/impl/tree/Short2BooleanRBTreeMap.java @@ -3,6 +3,7 @@ package speiger.src.collections.shorts.maps.impl.tree; import java.util.Collections; import java.util.Map; import java.util.Objects; +import java.util.Optional; import java.util.NoSuchElementException; import java.util.function.Consumer; import java.util.function.BiFunction; @@ -17,6 +18,7 @@ import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntBooleanConsumer; import speiger.src.collections.shorts.functions.function.ShortPredicate; import speiger.src.collections.shorts.functions.consumer.ShortBooleanConsumer; +import speiger.src.collections.shorts.functions.OptionalShort; import speiger.src.collections.shorts.functions.function.ShortBooleanUnaryOperator; import speiger.src.collections.shorts.functions.function.ShortShortUnaryOperator; import speiger.src.collections.shorts.maps.abstracts.AbstractShort2BooleanMap; @@ -35,6 +37,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectBooleanConsumer; import speiger.src.collections.booleans.functions.function.BooleanPredicate; +import speiger.src.collections.booleans.functions.OptionalBoolean; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOperator; @@ -1123,7 +1126,7 @@ public class Short2BooleanRBTreeMap extends AbstractShort2BooleanMap implements } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -1135,15 +1138,15 @@ public class Short2BooleanRBTreeMap extends AbstractShort2BooleanMap implements } state = operator.applyAsShort(state, entry.key); } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); for(Node entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.test(entry.key)) return entry.key; - return (short)0; + if(filter.test(entry.key)) return OptionalShort.of(entry.key); + return OptionalShort.empty(); } @Override @@ -1775,7 +1778,7 @@ public class Short2BooleanRBTreeMap extends AbstractShort2BooleanMap implements } @Override - public Short2BooleanMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Short2BooleanMap.Entry state = null; boolean empty = true; @@ -1787,19 +1790,19 @@ public class Short2BooleanRBTreeMap extends AbstractShort2BooleanMap implements } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Short2BooleanMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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 @@ -1894,7 +1897,7 @@ public class Short2BooleanRBTreeMap extends AbstractShort2BooleanMap implements } @Override - public boolean reduce(BooleanBooleanUnaryOperator operator) { + public OptionalBoolean reduce(BooleanBooleanUnaryOperator operator) { Objects.requireNonNull(operator); boolean state = false; boolean empty = true; @@ -1906,15 +1909,15 @@ public class Short2BooleanRBTreeMap extends AbstractShort2BooleanMap implements } state = operator.applyAsBoolean(state, entry.value); } - return state; + return empty ? OptionalBoolean.empty() : OptionalBoolean.of(state); } @Override - public boolean findFirst(BooleanPredicate filter) { + public OptionalBoolean findFirst(BooleanPredicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return false; + if(filter.test(entry.value)) return OptionalBoolean.of(entry.value); + return OptionalBoolean.empty(); } @Override @@ -2230,7 +2233,7 @@ public class Short2BooleanRBTreeMap extends AbstractShort2BooleanMap implements } @Override - public boolean reduce(BooleanBooleanUnaryOperator operator) { + public OptionalBoolean reduce(BooleanBooleanUnaryOperator operator) { Objects.requireNonNull(operator); boolean state = false; boolean empty = true; @@ -2242,15 +2245,15 @@ public class Short2BooleanRBTreeMap extends AbstractShort2BooleanMap implements } state = operator.applyAsBoolean(state, entry.value); } - return state; + return empty ? OptionalBoolean.empty() : OptionalBoolean.of(state); } @Override - public boolean findFirst(BooleanPredicate filter) { + public OptionalBoolean findFirst(BooleanPredicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return false; + if(filter.test(entry.value)) return OptionalBoolean.of(entry.value); + return OptionalBoolean.empty(); } @Override @@ -2391,7 +2394,7 @@ public class Short2BooleanRBTreeMap extends AbstractShort2BooleanMap implements } @Override - public Short2BooleanMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Short2BooleanMap.Entry state = null; boolean empty = true; @@ -2403,19 +2406,19 @@ public class Short2BooleanRBTreeMap extends AbstractShort2BooleanMap implements } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Short2BooleanMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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/main/java/speiger/src/collections/shorts/maps/impl/tree/Short2ByteAVLTreeMap.java b/src/main/java/speiger/src/collections/shorts/maps/impl/tree/Short2ByteAVLTreeMap.java index 64c54a3..2b1b7a7 100644 --- a/src/main/java/speiger/src/collections/shorts/maps/impl/tree/Short2ByteAVLTreeMap.java +++ b/src/main/java/speiger/src/collections/shorts/maps/impl/tree/Short2ByteAVLTreeMap.java @@ -3,6 +3,7 @@ package speiger.src.collections.shorts.maps.impl.tree; import java.util.Collections; import java.util.Map; import java.util.Objects; +import java.util.Optional; import java.util.NoSuchElementException; import java.util.function.Consumer; import java.util.function.BiFunction; @@ -18,6 +19,7 @@ import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntByteConsumer; import speiger.src.collections.shorts.functions.function.Short2ByteFunction; import speiger.src.collections.shorts.functions.consumer.ShortByteConsumer; +import speiger.src.collections.shorts.functions.OptionalShort; import speiger.src.collections.shorts.functions.function.ShortPredicate; import speiger.src.collections.shorts.functions.function.ShortByteUnaryOperator; import speiger.src.collections.shorts.functions.function.ShortShortUnaryOperator; @@ -37,6 +39,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectByteConsumer; import speiger.src.collections.bytes.functions.function.BytePredicate; +import speiger.src.collections.bytes.functions.OptionalByte; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOperator; @@ -1129,7 +1132,7 @@ public class Short2ByteAVLTreeMap extends AbstractShort2ByteMap implements Short } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -1141,15 +1144,15 @@ public class Short2ByteAVLTreeMap extends AbstractShort2ByteMap implements Short } state = operator.applyAsShort(state, entry.key); } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); for(Node entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.test(entry.key)) return entry.key; - return (short)0; + if(filter.test(entry.key)) return OptionalShort.of(entry.key); + return OptionalShort.empty(); } @Override @@ -1783,7 +1786,7 @@ public class Short2ByteAVLTreeMap extends AbstractShort2ByteMap implements Short } @Override - public Short2ByteMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Short2ByteMap.Entry state = null; boolean empty = true; @@ -1795,19 +1798,19 @@ public class Short2ByteAVLTreeMap extends AbstractShort2ByteMap implements Short } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Short2ByteMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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 @@ -1902,7 +1905,7 @@ public class Short2ByteAVLTreeMap extends AbstractShort2ByteMap implements Short } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -1914,15 +1917,15 @@ public class Short2ByteAVLTreeMap extends AbstractShort2ByteMap implements Short } state = operator.applyAsByte(state, entry.value); } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return (byte)0; + if(filter.test(entry.value)) return OptionalByte.of(entry.value); + return OptionalByte.empty(); } @Override @@ -2238,7 +2241,7 @@ public class Short2ByteAVLTreeMap extends AbstractShort2ByteMap implements Short } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -2250,15 +2253,15 @@ public class Short2ByteAVLTreeMap extends AbstractShort2ByteMap implements Short } state = operator.applyAsByte(state, entry.value); } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return (byte)0; + if(filter.test(entry.value)) return OptionalByte.of(entry.value); + return OptionalByte.empty(); } @Override @@ -2399,7 +2402,7 @@ public class Short2ByteAVLTreeMap extends AbstractShort2ByteMap implements Short } @Override - public Short2ByteMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Short2ByteMap.Entry state = null; boolean empty = true; @@ -2411,19 +2414,19 @@ public class Short2ByteAVLTreeMap extends AbstractShort2ByteMap implements Short } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Short2ByteMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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/main/java/speiger/src/collections/shorts/maps/impl/tree/Short2ByteRBTreeMap.java b/src/main/java/speiger/src/collections/shorts/maps/impl/tree/Short2ByteRBTreeMap.java index fbd6d93..349daa8 100644 --- a/src/main/java/speiger/src/collections/shorts/maps/impl/tree/Short2ByteRBTreeMap.java +++ b/src/main/java/speiger/src/collections/shorts/maps/impl/tree/Short2ByteRBTreeMap.java @@ -3,6 +3,7 @@ package speiger.src.collections.shorts.maps.impl.tree; import java.util.Collections; import java.util.Map; import java.util.Objects; +import java.util.Optional; import java.util.NoSuchElementException; import java.util.function.Consumer; import java.util.function.BiFunction; @@ -17,6 +18,7 @@ import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntByteConsumer; import speiger.src.collections.shorts.functions.function.Short2ByteFunction; import speiger.src.collections.shorts.functions.consumer.ShortByteConsumer; +import speiger.src.collections.shorts.functions.OptionalShort; import speiger.src.collections.shorts.functions.function.ShortPredicate; import speiger.src.collections.shorts.functions.function.ShortByteUnaryOperator; import speiger.src.collections.shorts.functions.function.ShortShortUnaryOperator; @@ -36,6 +38,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectByteConsumer; import speiger.src.collections.bytes.functions.function.BytePredicate; +import speiger.src.collections.bytes.functions.OptionalByte; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOperator; @@ -1182,7 +1185,7 @@ public class Short2ByteRBTreeMap extends AbstractShort2ByteMap implements Short2 } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -1194,15 +1197,15 @@ public class Short2ByteRBTreeMap extends AbstractShort2ByteMap implements Short2 } state = operator.applyAsShort(state, entry.key); } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); for(Node entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.test(entry.key)) return entry.key; - return (short)0; + if(filter.test(entry.key)) return OptionalShort.of(entry.key); + return OptionalShort.empty(); } @Override @@ -1846,7 +1849,7 @@ public class Short2ByteRBTreeMap extends AbstractShort2ByteMap implements Short2 } @Override - public Short2ByteMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Short2ByteMap.Entry state = null; boolean empty = true; @@ -1858,19 +1861,19 @@ public class Short2ByteRBTreeMap extends AbstractShort2ByteMap implements Short2 } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Short2ByteMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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 @@ -1965,7 +1968,7 @@ public class Short2ByteRBTreeMap extends AbstractShort2ByteMap implements Short2 } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -1977,15 +1980,15 @@ public class Short2ByteRBTreeMap extends AbstractShort2ByteMap implements Short2 } state = operator.applyAsByte(state, entry.value); } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return (byte)0; + if(filter.test(entry.value)) return OptionalByte.of(entry.value); + return OptionalByte.empty(); } @Override @@ -2301,7 +2304,7 @@ public class Short2ByteRBTreeMap extends AbstractShort2ByteMap implements Short2 } @Override - public byte reduce(ByteByteUnaryOperator operator) { + public OptionalByte reduce(ByteByteUnaryOperator operator) { Objects.requireNonNull(operator); byte state = (byte)0; boolean empty = true; @@ -2313,15 +2316,15 @@ public class Short2ByteRBTreeMap extends AbstractShort2ByteMap implements Short2 } state = operator.applyAsByte(state, entry.value); } - return state; + return empty ? OptionalByte.empty() : OptionalByte.of(state); } @Override - public byte findFirst(BytePredicate filter) { + public OptionalByte findFirst(BytePredicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return (byte)0; + if(filter.test(entry.value)) return OptionalByte.of(entry.value); + return OptionalByte.empty(); } @Override @@ -2462,7 +2465,7 @@ public class Short2ByteRBTreeMap extends AbstractShort2ByteMap implements Short2 } @Override - public Short2ByteMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Short2ByteMap.Entry state = null; boolean empty = true; @@ -2474,19 +2477,19 @@ public class Short2ByteRBTreeMap extends AbstractShort2ByteMap implements Short2 } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Short2ByteMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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/main/java/speiger/src/collections/shorts/maps/impl/tree/Short2CharAVLTreeMap.java b/src/main/java/speiger/src/collections/shorts/maps/impl/tree/Short2CharAVLTreeMap.java index b4e0b48..6af2661 100644 --- a/src/main/java/speiger/src/collections/shorts/maps/impl/tree/Short2CharAVLTreeMap.java +++ b/src/main/java/speiger/src/collections/shorts/maps/impl/tree/Short2CharAVLTreeMap.java @@ -3,6 +3,7 @@ package speiger.src.collections.shorts.maps.impl.tree; import java.util.Collections; import java.util.Map; import java.util.Objects; +import java.util.Optional; import java.util.NoSuchElementException; import java.util.function.Consumer; import java.util.function.BiFunction; @@ -18,6 +19,7 @@ import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntCharConsumer; import speiger.src.collections.shorts.functions.function.Short2CharFunction; import speiger.src.collections.shorts.functions.consumer.ShortCharConsumer; +import speiger.src.collections.shorts.functions.OptionalShort; import speiger.src.collections.shorts.functions.function.ShortPredicate; import speiger.src.collections.shorts.functions.function.ShortCharUnaryOperator; import speiger.src.collections.shorts.functions.function.ShortShortUnaryOperator; @@ -37,6 +39,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectCharConsumer; import speiger.src.collections.chars.functions.function.CharPredicate; +import speiger.src.collections.chars.functions.OptionalChar; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOperator; @@ -1129,7 +1132,7 @@ public class Short2CharAVLTreeMap extends AbstractShort2CharMap implements Short } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -1141,15 +1144,15 @@ public class Short2CharAVLTreeMap extends AbstractShort2CharMap implements Short } state = operator.applyAsShort(state, entry.key); } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); for(Node entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.test(entry.key)) return entry.key; - return (short)0; + if(filter.test(entry.key)) return OptionalShort.of(entry.key); + return OptionalShort.empty(); } @Override @@ -1783,7 +1786,7 @@ public class Short2CharAVLTreeMap extends AbstractShort2CharMap implements Short } @Override - public Short2CharMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Short2CharMap.Entry state = null; boolean empty = true; @@ -1795,19 +1798,19 @@ public class Short2CharAVLTreeMap extends AbstractShort2CharMap implements Short } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Short2CharMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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 @@ -1902,7 +1905,7 @@ public class Short2CharAVLTreeMap extends AbstractShort2CharMap implements Short } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -1914,15 +1917,15 @@ public class Short2CharAVLTreeMap extends AbstractShort2CharMap implements Short } state = operator.applyAsChar(state, entry.value); } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return (char)0; + if(filter.test(entry.value)) return OptionalChar.of(entry.value); + return OptionalChar.empty(); } @Override @@ -2238,7 +2241,7 @@ public class Short2CharAVLTreeMap extends AbstractShort2CharMap implements Short } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -2250,15 +2253,15 @@ public class Short2CharAVLTreeMap extends AbstractShort2CharMap implements Short } state = operator.applyAsChar(state, entry.value); } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return (char)0; + if(filter.test(entry.value)) return OptionalChar.of(entry.value); + return OptionalChar.empty(); } @Override @@ -2399,7 +2402,7 @@ public class Short2CharAVLTreeMap extends AbstractShort2CharMap implements Short } @Override - public Short2CharMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Short2CharMap.Entry state = null; boolean empty = true; @@ -2411,19 +2414,19 @@ public class Short2CharAVLTreeMap extends AbstractShort2CharMap implements Short } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Short2CharMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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/main/java/speiger/src/collections/shorts/maps/impl/tree/Short2CharRBTreeMap.java b/src/main/java/speiger/src/collections/shorts/maps/impl/tree/Short2CharRBTreeMap.java index fa5e1d8..f29fdd8 100644 --- a/src/main/java/speiger/src/collections/shorts/maps/impl/tree/Short2CharRBTreeMap.java +++ b/src/main/java/speiger/src/collections/shorts/maps/impl/tree/Short2CharRBTreeMap.java @@ -3,6 +3,7 @@ package speiger.src.collections.shorts.maps.impl.tree; import java.util.Collections; import java.util.Map; import java.util.Objects; +import java.util.Optional; import java.util.NoSuchElementException; import java.util.function.Consumer; import java.util.function.BiFunction; @@ -17,6 +18,7 @@ import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntCharConsumer; import speiger.src.collections.shorts.functions.function.Short2CharFunction; import speiger.src.collections.shorts.functions.consumer.ShortCharConsumer; +import speiger.src.collections.shorts.functions.OptionalShort; import speiger.src.collections.shorts.functions.function.ShortPredicate; import speiger.src.collections.shorts.functions.function.ShortCharUnaryOperator; import speiger.src.collections.shorts.functions.function.ShortShortUnaryOperator; @@ -36,6 +38,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectCharConsumer; import speiger.src.collections.chars.functions.function.CharPredicate; +import speiger.src.collections.chars.functions.OptionalChar; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOperator; @@ -1182,7 +1185,7 @@ public class Short2CharRBTreeMap extends AbstractShort2CharMap implements Short2 } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -1194,15 +1197,15 @@ public class Short2CharRBTreeMap extends AbstractShort2CharMap implements Short2 } state = operator.applyAsShort(state, entry.key); } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); for(Node entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.test(entry.key)) return entry.key; - return (short)0; + if(filter.test(entry.key)) return OptionalShort.of(entry.key); + return OptionalShort.empty(); } @Override @@ -1846,7 +1849,7 @@ public class Short2CharRBTreeMap extends AbstractShort2CharMap implements Short2 } @Override - public Short2CharMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Short2CharMap.Entry state = null; boolean empty = true; @@ -1858,19 +1861,19 @@ public class Short2CharRBTreeMap extends AbstractShort2CharMap implements Short2 } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Short2CharMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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 @@ -1965,7 +1968,7 @@ public class Short2CharRBTreeMap extends AbstractShort2CharMap implements Short2 } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -1977,15 +1980,15 @@ public class Short2CharRBTreeMap extends AbstractShort2CharMap implements Short2 } state = operator.applyAsChar(state, entry.value); } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return (char)0; + if(filter.test(entry.value)) return OptionalChar.of(entry.value); + return OptionalChar.empty(); } @Override @@ -2301,7 +2304,7 @@ public class Short2CharRBTreeMap extends AbstractShort2CharMap implements Short2 } @Override - public char reduce(CharCharUnaryOperator operator) { + public OptionalChar reduce(CharCharUnaryOperator operator) { Objects.requireNonNull(operator); char state = (char)0; boolean empty = true; @@ -2313,15 +2316,15 @@ public class Short2CharRBTreeMap extends AbstractShort2CharMap implements Short2 } state = operator.applyAsChar(state, entry.value); } - return state; + return empty ? OptionalChar.empty() : OptionalChar.of(state); } @Override - public char findFirst(CharPredicate filter) { + public OptionalChar findFirst(CharPredicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return (char)0; + if(filter.test(entry.value)) return OptionalChar.of(entry.value); + return OptionalChar.empty(); } @Override @@ -2462,7 +2465,7 @@ public class Short2CharRBTreeMap extends AbstractShort2CharMap implements Short2 } @Override - public Short2CharMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Short2CharMap.Entry state = null; boolean empty = true; @@ -2474,19 +2477,19 @@ public class Short2CharRBTreeMap extends AbstractShort2CharMap implements Short2 } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Short2CharMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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/main/java/speiger/src/collections/shorts/maps/impl/tree/Short2DoubleAVLTreeMap.java b/src/main/java/speiger/src/collections/shorts/maps/impl/tree/Short2DoubleAVLTreeMap.java index cee365c..2c8dda1 100644 --- a/src/main/java/speiger/src/collections/shorts/maps/impl/tree/Short2DoubleAVLTreeMap.java +++ b/src/main/java/speiger/src/collections/shorts/maps/impl/tree/Short2DoubleAVLTreeMap.java @@ -3,11 +3,13 @@ package speiger.src.collections.shorts.maps.impl.tree; import java.util.Collections; import java.util.Map; 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; import java.util.function.DoublePredicate; +import java.util.OptionalDouble; import speiger.src.collections.shorts.collections.ShortBidirectionalIterator; @@ -19,6 +21,7 @@ import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntDoubleConsumer; import speiger.src.collections.shorts.functions.function.Short2DoubleFunction; import speiger.src.collections.shorts.functions.consumer.ShortDoubleConsumer; +import speiger.src.collections.shorts.functions.OptionalShort; import speiger.src.collections.shorts.functions.function.ShortPredicate; import speiger.src.collections.shorts.functions.function.ShortDoubleUnaryOperator; import speiger.src.collections.shorts.functions.function.ShortShortUnaryOperator; @@ -1129,7 +1132,7 @@ public class Short2DoubleAVLTreeMap extends AbstractShort2DoubleMap implements S } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -1141,15 +1144,15 @@ public class Short2DoubleAVLTreeMap extends AbstractShort2DoubleMap implements S } state = operator.applyAsShort(state, entry.key); } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); for(Node entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.test(entry.key)) return entry.key; - return (short)0; + if(filter.test(entry.key)) return OptionalShort.of(entry.key); + return OptionalShort.empty(); } @Override @@ -1783,7 +1786,7 @@ public class Short2DoubleAVLTreeMap extends AbstractShort2DoubleMap implements S } @Override - public Short2DoubleMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Short2DoubleMap.Entry state = null; boolean empty = true; @@ -1795,19 +1798,19 @@ public class Short2DoubleAVLTreeMap extends AbstractShort2DoubleMap implements S } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Short2DoubleMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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 @@ -1902,7 +1905,7 @@ public class Short2DoubleAVLTreeMap extends AbstractShort2DoubleMap implements S } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -1914,15 +1917,15 @@ public class Short2DoubleAVLTreeMap extends AbstractShort2DoubleMap implements S } state = operator.applyAsDouble(state, entry.value); } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return 0D; + if(filter.test(entry.value)) return OptionalDouble.of(entry.value); + return OptionalDouble.empty(); } @Override @@ -2238,7 +2241,7 @@ public class Short2DoubleAVLTreeMap extends AbstractShort2DoubleMap implements S } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -2250,15 +2253,15 @@ public class Short2DoubleAVLTreeMap extends AbstractShort2DoubleMap implements S } state = operator.applyAsDouble(state, entry.value); } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return 0D; + if(filter.test(entry.value)) return OptionalDouble.of(entry.value); + return OptionalDouble.empty(); } @Override @@ -2399,7 +2402,7 @@ public class Short2DoubleAVLTreeMap extends AbstractShort2DoubleMap implements S } @Override - public Short2DoubleMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Short2DoubleMap.Entry state = null; boolean empty = true; @@ -2411,19 +2414,19 @@ public class Short2DoubleAVLTreeMap extends AbstractShort2DoubleMap implements S } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Short2DoubleMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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/main/java/speiger/src/collections/shorts/maps/impl/tree/Short2DoubleRBTreeMap.java b/src/main/java/speiger/src/collections/shorts/maps/impl/tree/Short2DoubleRBTreeMap.java index 2ad3ac9..d7443d2 100644 --- a/src/main/java/speiger/src/collections/shorts/maps/impl/tree/Short2DoubleRBTreeMap.java +++ b/src/main/java/speiger/src/collections/shorts/maps/impl/tree/Short2DoubleRBTreeMap.java @@ -3,11 +3,13 @@ package speiger.src.collections.shorts.maps.impl.tree; import java.util.Collections; import java.util.Map; 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; import java.util.function.DoublePredicate; +import java.util.OptionalDouble; import speiger.src.collections.shorts.collections.ShortBidirectionalIterator; import speiger.src.collections.shorts.functions.ShortComparator; @@ -18,6 +20,7 @@ import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntDoubleConsumer; import speiger.src.collections.shorts.functions.function.Short2DoubleFunction; import speiger.src.collections.shorts.functions.consumer.ShortDoubleConsumer; +import speiger.src.collections.shorts.functions.OptionalShort; import speiger.src.collections.shorts.functions.function.ShortPredicate; import speiger.src.collections.shorts.functions.function.ShortDoubleUnaryOperator; import speiger.src.collections.shorts.functions.function.ShortShortUnaryOperator; @@ -1182,7 +1185,7 @@ public class Short2DoubleRBTreeMap extends AbstractShort2DoubleMap implements Sh } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -1194,15 +1197,15 @@ public class Short2DoubleRBTreeMap extends AbstractShort2DoubleMap implements Sh } state = operator.applyAsShort(state, entry.key); } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); for(Node entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.test(entry.key)) return entry.key; - return (short)0; + if(filter.test(entry.key)) return OptionalShort.of(entry.key); + return OptionalShort.empty(); } @Override @@ -1846,7 +1849,7 @@ public class Short2DoubleRBTreeMap extends AbstractShort2DoubleMap implements Sh } @Override - public Short2DoubleMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Short2DoubleMap.Entry state = null; boolean empty = true; @@ -1858,19 +1861,19 @@ public class Short2DoubleRBTreeMap extends AbstractShort2DoubleMap implements Sh } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Short2DoubleMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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 @@ -1965,7 +1968,7 @@ public class Short2DoubleRBTreeMap extends AbstractShort2DoubleMap implements Sh } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -1977,15 +1980,15 @@ public class Short2DoubleRBTreeMap extends AbstractShort2DoubleMap implements Sh } state = operator.applyAsDouble(state, entry.value); } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return 0D; + if(filter.test(entry.value)) return OptionalDouble.of(entry.value); + return OptionalDouble.empty(); } @Override @@ -2301,7 +2304,7 @@ public class Short2DoubleRBTreeMap extends AbstractShort2DoubleMap implements Sh } @Override - public double reduce(DoubleDoubleUnaryOperator operator) { + public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) { Objects.requireNonNull(operator); double state = 0D; boolean empty = true; @@ -2313,15 +2316,15 @@ public class Short2DoubleRBTreeMap extends AbstractShort2DoubleMap implements Sh } state = operator.applyAsDouble(state, entry.value); } - return state; + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } @Override - public double findFirst(DoublePredicate filter) { + public OptionalDouble findFirst(DoublePredicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return 0D; + if(filter.test(entry.value)) return OptionalDouble.of(entry.value); + return OptionalDouble.empty(); } @Override @@ -2462,7 +2465,7 @@ public class Short2DoubleRBTreeMap extends AbstractShort2DoubleMap implements Sh } @Override - public Short2DoubleMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Short2DoubleMap.Entry state = null; boolean empty = true; @@ -2474,19 +2477,19 @@ public class Short2DoubleRBTreeMap extends AbstractShort2DoubleMap implements Sh } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Short2DoubleMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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/main/java/speiger/src/collections/shorts/maps/impl/tree/Short2FloatAVLTreeMap.java b/src/main/java/speiger/src/collections/shorts/maps/impl/tree/Short2FloatAVLTreeMap.java index 7a51fdf..8a85407 100644 --- a/src/main/java/speiger/src/collections/shorts/maps/impl/tree/Short2FloatAVLTreeMap.java +++ b/src/main/java/speiger/src/collections/shorts/maps/impl/tree/Short2FloatAVLTreeMap.java @@ -3,6 +3,7 @@ package speiger.src.collections.shorts.maps.impl.tree; import java.util.Collections; import java.util.Map; import java.util.Objects; +import java.util.Optional; import java.util.NoSuchElementException; import java.util.function.Consumer; import java.util.function.BiFunction; @@ -18,6 +19,7 @@ import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntFloatConsumer; import speiger.src.collections.shorts.functions.function.Short2FloatFunction; import speiger.src.collections.shorts.functions.consumer.ShortFloatConsumer; +import speiger.src.collections.shorts.functions.OptionalShort; import speiger.src.collections.shorts.functions.function.ShortPredicate; import speiger.src.collections.shorts.functions.function.ShortFloatUnaryOperator; import speiger.src.collections.shorts.functions.function.ShortShortUnaryOperator; @@ -37,6 +39,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectFloatConsumer; import speiger.src.collections.floats.functions.function.FloatPredicate; +import speiger.src.collections.floats.functions.OptionalFloat; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOperator; @@ -1129,7 +1132,7 @@ public class Short2FloatAVLTreeMap extends AbstractShort2FloatMap implements Sho } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -1141,15 +1144,15 @@ public class Short2FloatAVLTreeMap extends AbstractShort2FloatMap implements Sho } state = operator.applyAsShort(state, entry.key); } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); for(Node entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.test(entry.key)) return entry.key; - return (short)0; + if(filter.test(entry.key)) return OptionalShort.of(entry.key); + return OptionalShort.empty(); } @Override @@ -1783,7 +1786,7 @@ public class Short2FloatAVLTreeMap extends AbstractShort2FloatMap implements Sho } @Override - public Short2FloatMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Short2FloatMap.Entry state = null; boolean empty = true; @@ -1795,19 +1798,19 @@ public class Short2FloatAVLTreeMap extends AbstractShort2FloatMap implements Sho } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Short2FloatMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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 @@ -1902,7 +1905,7 @@ public class Short2FloatAVLTreeMap extends AbstractShort2FloatMap implements Sho } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -1914,15 +1917,15 @@ public class Short2FloatAVLTreeMap extends AbstractShort2FloatMap implements Sho } state = operator.applyAsFloat(state, entry.value); } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return 0F; + if(filter.test(entry.value)) return OptionalFloat.of(entry.value); + return OptionalFloat.empty(); } @Override @@ -2238,7 +2241,7 @@ public class Short2FloatAVLTreeMap extends AbstractShort2FloatMap implements Sho } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -2250,15 +2253,15 @@ public class Short2FloatAVLTreeMap extends AbstractShort2FloatMap implements Sho } state = operator.applyAsFloat(state, entry.value); } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return 0F; + if(filter.test(entry.value)) return OptionalFloat.of(entry.value); + return OptionalFloat.empty(); } @Override @@ -2399,7 +2402,7 @@ public class Short2FloatAVLTreeMap extends AbstractShort2FloatMap implements Sho } @Override - public Short2FloatMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Short2FloatMap.Entry state = null; boolean empty = true; @@ -2411,19 +2414,19 @@ public class Short2FloatAVLTreeMap extends AbstractShort2FloatMap implements Sho } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Short2FloatMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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/main/java/speiger/src/collections/shorts/maps/impl/tree/Short2FloatRBTreeMap.java b/src/main/java/speiger/src/collections/shorts/maps/impl/tree/Short2FloatRBTreeMap.java index 9c98a92..5d18362 100644 --- a/src/main/java/speiger/src/collections/shorts/maps/impl/tree/Short2FloatRBTreeMap.java +++ b/src/main/java/speiger/src/collections/shorts/maps/impl/tree/Short2FloatRBTreeMap.java @@ -3,6 +3,7 @@ package speiger.src.collections.shorts.maps.impl.tree; import java.util.Collections; import java.util.Map; import java.util.Objects; +import java.util.Optional; import java.util.NoSuchElementException; import java.util.function.Consumer; import java.util.function.BiFunction; @@ -17,6 +18,7 @@ import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntFloatConsumer; import speiger.src.collections.shorts.functions.function.Short2FloatFunction; import speiger.src.collections.shorts.functions.consumer.ShortFloatConsumer; +import speiger.src.collections.shorts.functions.OptionalShort; import speiger.src.collections.shorts.functions.function.ShortPredicate; import speiger.src.collections.shorts.functions.function.ShortFloatUnaryOperator; import speiger.src.collections.shorts.functions.function.ShortShortUnaryOperator; @@ -36,6 +38,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectFloatConsumer; import speiger.src.collections.floats.functions.function.FloatPredicate; +import speiger.src.collections.floats.functions.OptionalFloat; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOperator; @@ -1182,7 +1185,7 @@ public class Short2FloatRBTreeMap extends AbstractShort2FloatMap implements Shor } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -1194,15 +1197,15 @@ public class Short2FloatRBTreeMap extends AbstractShort2FloatMap implements Shor } state = operator.applyAsShort(state, entry.key); } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); for(Node entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.test(entry.key)) return entry.key; - return (short)0; + if(filter.test(entry.key)) return OptionalShort.of(entry.key); + return OptionalShort.empty(); } @Override @@ -1846,7 +1849,7 @@ public class Short2FloatRBTreeMap extends AbstractShort2FloatMap implements Shor } @Override - public Short2FloatMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Short2FloatMap.Entry state = null; boolean empty = true; @@ -1858,19 +1861,19 @@ public class Short2FloatRBTreeMap extends AbstractShort2FloatMap implements Shor } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Short2FloatMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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 @@ -1965,7 +1968,7 @@ public class Short2FloatRBTreeMap extends AbstractShort2FloatMap implements Shor } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -1977,15 +1980,15 @@ public class Short2FloatRBTreeMap extends AbstractShort2FloatMap implements Shor } state = operator.applyAsFloat(state, entry.value); } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return 0F; + if(filter.test(entry.value)) return OptionalFloat.of(entry.value); + return OptionalFloat.empty(); } @Override @@ -2301,7 +2304,7 @@ public class Short2FloatRBTreeMap extends AbstractShort2FloatMap implements Shor } @Override - public float reduce(FloatFloatUnaryOperator operator) { + public OptionalFloat reduce(FloatFloatUnaryOperator operator) { Objects.requireNonNull(operator); float state = 0F; boolean empty = true; @@ -2313,15 +2316,15 @@ public class Short2FloatRBTreeMap extends AbstractShort2FloatMap implements Shor } state = operator.applyAsFloat(state, entry.value); } - return state; + return empty ? OptionalFloat.empty() : OptionalFloat.of(state); } @Override - public float findFirst(FloatPredicate filter) { + public OptionalFloat findFirst(FloatPredicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return 0F; + if(filter.test(entry.value)) return OptionalFloat.of(entry.value); + return OptionalFloat.empty(); } @Override @@ -2462,7 +2465,7 @@ public class Short2FloatRBTreeMap extends AbstractShort2FloatMap implements Shor } @Override - public Short2FloatMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Short2FloatMap.Entry state = null; boolean empty = true; @@ -2474,19 +2477,19 @@ public class Short2FloatRBTreeMap extends AbstractShort2FloatMap implements Shor } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Short2FloatMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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/main/java/speiger/src/collections/shorts/maps/impl/tree/Short2IntAVLTreeMap.java b/src/main/java/speiger/src/collections/shorts/maps/impl/tree/Short2IntAVLTreeMap.java index b1b561b..55d6bd0 100644 --- a/src/main/java/speiger/src/collections/shorts/maps/impl/tree/Short2IntAVLTreeMap.java +++ b/src/main/java/speiger/src/collections/shorts/maps/impl/tree/Short2IntAVLTreeMap.java @@ -3,11 +3,13 @@ package speiger.src.collections.shorts.maps.impl.tree; import java.util.Collections; import java.util.Map; 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; import java.util.function.IntPredicate; +import java.util.OptionalInt; import speiger.src.collections.shorts.collections.ShortBidirectionalIterator; @@ -19,6 +21,7 @@ import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntIntConsumer; import speiger.src.collections.shorts.functions.function.Short2IntFunction; import speiger.src.collections.shorts.functions.consumer.ShortIntConsumer; +import speiger.src.collections.shorts.functions.OptionalShort; import speiger.src.collections.shorts.functions.function.ShortPredicate; import speiger.src.collections.shorts.functions.function.ShortIntUnaryOperator; import speiger.src.collections.shorts.functions.function.ShortShortUnaryOperator; @@ -1129,7 +1132,7 @@ public class Short2IntAVLTreeMap extends AbstractShort2IntMap implements Short2I } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -1141,15 +1144,15 @@ public class Short2IntAVLTreeMap extends AbstractShort2IntMap implements Short2I } state = operator.applyAsShort(state, entry.key); } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); for(Node entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.test(entry.key)) return entry.key; - return (short)0; + if(filter.test(entry.key)) return OptionalShort.of(entry.key); + return OptionalShort.empty(); } @Override @@ -1783,7 +1786,7 @@ public class Short2IntAVLTreeMap extends AbstractShort2IntMap implements Short2I } @Override - public Short2IntMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Short2IntMap.Entry state = null; boolean empty = true; @@ -1795,19 +1798,19 @@ public class Short2IntAVLTreeMap extends AbstractShort2IntMap implements Short2I } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Short2IntMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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 @@ -1902,7 +1905,7 @@ public class Short2IntAVLTreeMap extends AbstractShort2IntMap implements Short2I } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -1914,15 +1917,15 @@ public class Short2IntAVLTreeMap extends AbstractShort2IntMap implements Short2I } state = operator.applyAsInt(state, entry.value); } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return 0; + if(filter.test(entry.value)) return OptionalInt.of(entry.value); + return OptionalInt.empty(); } @Override @@ -2238,7 +2241,7 @@ public class Short2IntAVLTreeMap extends AbstractShort2IntMap implements Short2I } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -2250,15 +2253,15 @@ public class Short2IntAVLTreeMap extends AbstractShort2IntMap implements Short2I } state = operator.applyAsInt(state, entry.value); } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return 0; + if(filter.test(entry.value)) return OptionalInt.of(entry.value); + return OptionalInt.empty(); } @Override @@ -2399,7 +2402,7 @@ public class Short2IntAVLTreeMap extends AbstractShort2IntMap implements Short2I } @Override - public Short2IntMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Short2IntMap.Entry state = null; boolean empty = true; @@ -2411,19 +2414,19 @@ public class Short2IntAVLTreeMap extends AbstractShort2IntMap implements Short2I } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Short2IntMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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/main/java/speiger/src/collections/shorts/maps/impl/tree/Short2IntRBTreeMap.java b/src/main/java/speiger/src/collections/shorts/maps/impl/tree/Short2IntRBTreeMap.java index b3206ac..b48918b 100644 --- a/src/main/java/speiger/src/collections/shorts/maps/impl/tree/Short2IntRBTreeMap.java +++ b/src/main/java/speiger/src/collections/shorts/maps/impl/tree/Short2IntRBTreeMap.java @@ -3,11 +3,13 @@ package speiger.src.collections.shorts.maps.impl.tree; import java.util.Collections; import java.util.Map; 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; import java.util.function.IntPredicate; +import java.util.OptionalInt; import speiger.src.collections.shorts.collections.ShortBidirectionalIterator; import speiger.src.collections.shorts.functions.ShortComparator; @@ -18,6 +20,7 @@ import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntIntConsumer; import speiger.src.collections.shorts.functions.function.Short2IntFunction; import speiger.src.collections.shorts.functions.consumer.ShortIntConsumer; +import speiger.src.collections.shorts.functions.OptionalShort; import speiger.src.collections.shorts.functions.function.ShortPredicate; import speiger.src.collections.shorts.functions.function.ShortIntUnaryOperator; import speiger.src.collections.shorts.functions.function.ShortShortUnaryOperator; @@ -1182,7 +1185,7 @@ public class Short2IntRBTreeMap extends AbstractShort2IntMap implements Short2In } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -1194,15 +1197,15 @@ public class Short2IntRBTreeMap extends AbstractShort2IntMap implements Short2In } state = operator.applyAsShort(state, entry.key); } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); for(Node entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.test(entry.key)) return entry.key; - return (short)0; + if(filter.test(entry.key)) return OptionalShort.of(entry.key); + return OptionalShort.empty(); } @Override @@ -1846,7 +1849,7 @@ public class Short2IntRBTreeMap extends AbstractShort2IntMap implements Short2In } @Override - public Short2IntMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Short2IntMap.Entry state = null; boolean empty = true; @@ -1858,19 +1861,19 @@ public class Short2IntRBTreeMap extends AbstractShort2IntMap implements Short2In } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Short2IntMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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 @@ -1965,7 +1968,7 @@ public class Short2IntRBTreeMap extends AbstractShort2IntMap implements Short2In } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -1977,15 +1980,15 @@ public class Short2IntRBTreeMap extends AbstractShort2IntMap implements Short2In } state = operator.applyAsInt(state, entry.value); } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return 0; + if(filter.test(entry.value)) return OptionalInt.of(entry.value); + return OptionalInt.empty(); } @Override @@ -2301,7 +2304,7 @@ public class Short2IntRBTreeMap extends AbstractShort2IntMap implements Short2In } @Override - public int reduce(IntIntUnaryOperator operator) { + public OptionalInt reduce(IntIntUnaryOperator operator) { Objects.requireNonNull(operator); int state = 0; boolean empty = true; @@ -2313,15 +2316,15 @@ public class Short2IntRBTreeMap extends AbstractShort2IntMap implements Short2In } state = operator.applyAsInt(state, entry.value); } - return state; + return empty ? OptionalInt.empty() : OptionalInt.of(state); } @Override - public int findFirst(IntPredicate filter) { + public OptionalInt findFirst(IntPredicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return 0; + if(filter.test(entry.value)) return OptionalInt.of(entry.value); + return OptionalInt.empty(); } @Override @@ -2462,7 +2465,7 @@ public class Short2IntRBTreeMap extends AbstractShort2IntMap implements Short2In } @Override - public Short2IntMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Short2IntMap.Entry state = null; boolean empty = true; @@ -2474,19 +2477,19 @@ public class Short2IntRBTreeMap extends AbstractShort2IntMap implements Short2In } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Short2IntMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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/main/java/speiger/src/collections/shorts/maps/impl/tree/Short2LongAVLTreeMap.java b/src/main/java/speiger/src/collections/shorts/maps/impl/tree/Short2LongAVLTreeMap.java index 4d4f814..45273f7 100644 --- a/src/main/java/speiger/src/collections/shorts/maps/impl/tree/Short2LongAVLTreeMap.java +++ b/src/main/java/speiger/src/collections/shorts/maps/impl/tree/Short2LongAVLTreeMap.java @@ -3,11 +3,13 @@ package speiger.src.collections.shorts.maps.impl.tree; import java.util.Collections; import java.util.Map; 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; import java.util.function.LongPredicate; +import java.util.OptionalLong; import speiger.src.collections.shorts.collections.ShortBidirectionalIterator; @@ -19,6 +21,7 @@ import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntLongConsumer; import speiger.src.collections.shorts.functions.function.Short2LongFunction; import speiger.src.collections.shorts.functions.consumer.ShortLongConsumer; +import speiger.src.collections.shorts.functions.OptionalShort; import speiger.src.collections.shorts.functions.function.ShortPredicate; import speiger.src.collections.shorts.functions.function.ShortLongUnaryOperator; import speiger.src.collections.shorts.functions.function.ShortShortUnaryOperator; @@ -1129,7 +1132,7 @@ public class Short2LongAVLTreeMap extends AbstractShort2LongMap implements Short } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -1141,15 +1144,15 @@ public class Short2LongAVLTreeMap extends AbstractShort2LongMap implements Short } state = operator.applyAsShort(state, entry.key); } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); for(Node entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.test(entry.key)) return entry.key; - return (short)0; + if(filter.test(entry.key)) return OptionalShort.of(entry.key); + return OptionalShort.empty(); } @Override @@ -1783,7 +1786,7 @@ public class Short2LongAVLTreeMap extends AbstractShort2LongMap implements Short } @Override - public Short2LongMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Short2LongMap.Entry state = null; boolean empty = true; @@ -1795,19 +1798,19 @@ public class Short2LongAVLTreeMap extends AbstractShort2LongMap implements Short } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Short2LongMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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 @@ -1902,7 +1905,7 @@ public class Short2LongAVLTreeMap extends AbstractShort2LongMap implements Short } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -1914,15 +1917,15 @@ public class Short2LongAVLTreeMap extends AbstractShort2LongMap implements Short } state = operator.applyAsLong(state, entry.value); } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return 0L; + if(filter.test(entry.value)) return OptionalLong.of(entry.value); + return OptionalLong.empty(); } @Override @@ -2238,7 +2241,7 @@ public class Short2LongAVLTreeMap extends AbstractShort2LongMap implements Short } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -2250,15 +2253,15 @@ public class Short2LongAVLTreeMap extends AbstractShort2LongMap implements Short } state = operator.applyAsLong(state, entry.value); } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return 0L; + if(filter.test(entry.value)) return OptionalLong.of(entry.value); + return OptionalLong.empty(); } @Override @@ -2399,7 +2402,7 @@ public class Short2LongAVLTreeMap extends AbstractShort2LongMap implements Short } @Override - public Short2LongMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Short2LongMap.Entry state = null; boolean empty = true; @@ -2411,19 +2414,19 @@ public class Short2LongAVLTreeMap extends AbstractShort2LongMap implements Short } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Short2LongMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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/main/java/speiger/src/collections/shorts/maps/impl/tree/Short2LongRBTreeMap.java b/src/main/java/speiger/src/collections/shorts/maps/impl/tree/Short2LongRBTreeMap.java index f9804f0..cf9c28f 100644 --- a/src/main/java/speiger/src/collections/shorts/maps/impl/tree/Short2LongRBTreeMap.java +++ b/src/main/java/speiger/src/collections/shorts/maps/impl/tree/Short2LongRBTreeMap.java @@ -3,11 +3,13 @@ package speiger.src.collections.shorts.maps.impl.tree; import java.util.Collections; import java.util.Map; 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; import java.util.function.LongPredicate; +import java.util.OptionalLong; import speiger.src.collections.shorts.collections.ShortBidirectionalIterator; import speiger.src.collections.shorts.functions.ShortComparator; @@ -18,6 +20,7 @@ import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntLongConsumer; import speiger.src.collections.shorts.functions.function.Short2LongFunction; import speiger.src.collections.shorts.functions.consumer.ShortLongConsumer; +import speiger.src.collections.shorts.functions.OptionalShort; import speiger.src.collections.shorts.functions.function.ShortPredicate; import speiger.src.collections.shorts.functions.function.ShortLongUnaryOperator; import speiger.src.collections.shorts.functions.function.ShortShortUnaryOperator; @@ -1182,7 +1185,7 @@ public class Short2LongRBTreeMap extends AbstractShort2LongMap implements Short2 } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -1194,15 +1197,15 @@ public class Short2LongRBTreeMap extends AbstractShort2LongMap implements Short2 } state = operator.applyAsShort(state, entry.key); } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); for(Node entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.test(entry.key)) return entry.key; - return (short)0; + if(filter.test(entry.key)) return OptionalShort.of(entry.key); + return OptionalShort.empty(); } @Override @@ -1846,7 +1849,7 @@ public class Short2LongRBTreeMap extends AbstractShort2LongMap implements Short2 } @Override - public Short2LongMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Short2LongMap.Entry state = null; boolean empty = true; @@ -1858,19 +1861,19 @@ public class Short2LongRBTreeMap extends AbstractShort2LongMap implements Short2 } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Short2LongMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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 @@ -1965,7 +1968,7 @@ public class Short2LongRBTreeMap extends AbstractShort2LongMap implements Short2 } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -1977,15 +1980,15 @@ public class Short2LongRBTreeMap extends AbstractShort2LongMap implements Short2 } state = operator.applyAsLong(state, entry.value); } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return 0L; + if(filter.test(entry.value)) return OptionalLong.of(entry.value); + return OptionalLong.empty(); } @Override @@ -2301,7 +2304,7 @@ public class Short2LongRBTreeMap extends AbstractShort2LongMap implements Short2 } @Override - public long reduce(LongLongUnaryOperator operator) { + public OptionalLong reduce(LongLongUnaryOperator operator) { Objects.requireNonNull(operator); long state = 0L; boolean empty = true; @@ -2313,15 +2316,15 @@ public class Short2LongRBTreeMap extends AbstractShort2LongMap implements Short2 } state = operator.applyAsLong(state, entry.value); } - return state; + return empty ? OptionalLong.empty() : OptionalLong.of(state); } @Override - public long findFirst(LongPredicate filter) { + public OptionalLong findFirst(LongPredicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return 0L; + if(filter.test(entry.value)) return OptionalLong.of(entry.value); + return OptionalLong.empty(); } @Override @@ -2462,7 +2465,7 @@ public class Short2LongRBTreeMap extends AbstractShort2LongMap implements Short2 } @Override - public Short2LongMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Short2LongMap.Entry state = null; boolean empty = true; @@ -2474,19 +2477,19 @@ public class Short2LongRBTreeMap extends AbstractShort2LongMap implements Short2 } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Short2LongMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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/main/java/speiger/src/collections/shorts/maps/impl/tree/Short2ObjectAVLTreeMap.java b/src/main/java/speiger/src/collections/shorts/maps/impl/tree/Short2ObjectAVLTreeMap.java index 0fdfe9c..42ebec3 100644 --- a/src/main/java/speiger/src/collections/shorts/maps/impl/tree/Short2ObjectAVLTreeMap.java +++ b/src/main/java/speiger/src/collections/shorts/maps/impl/tree/Short2ObjectAVLTreeMap.java @@ -3,6 +3,7 @@ package speiger.src.collections.shorts.maps.impl.tree; import java.util.Collections; import java.util.Map; import java.util.Objects; +import java.util.Optional; import java.util.NoSuchElementException; import java.util.function.Consumer; import java.util.function.BiFunction; @@ -17,6 +18,7 @@ import speiger.src.collections.ints.functions.consumer.IntShortConsumer; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.shorts.functions.function.ShortFunction; import speiger.src.collections.shorts.functions.consumer.ShortObjectConsumer; +import speiger.src.collections.shorts.functions.OptionalShort; import speiger.src.collections.shorts.functions.function.ShortPredicate; import speiger.src.collections.shorts.functions.function.ShortObjectUnaryOperator; import speiger.src.collections.shorts.functions.function.ShortShortUnaryOperator; @@ -1017,7 +1019,7 @@ public class Short2ObjectAVLTreeMap extends AbstractShort2ObjectMap implem } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -1029,15 +1031,15 @@ public class Short2ObjectAVLTreeMap extends AbstractShort2ObjectMap implem } state = operator.applyAsShort(state, entry.key); } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); for(Node entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.test(entry.key)) return entry.key; - return (short)0; + if(filter.test(entry.key)) return OptionalShort.of(entry.key); + return OptionalShort.empty(); } @Override @@ -1664,7 +1666,7 @@ public class Short2ObjectAVLTreeMap extends AbstractShort2ObjectMap implem } @Override - public Short2ObjectMap.Entry reduce(ObjectObjectUnaryOperator, Short2ObjectMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Short2ObjectMap.Entry> operator) { Objects.requireNonNull(operator); Short2ObjectMap.Entry state = null; boolean empty = true; @@ -1676,19 +1678,19 @@ public class Short2ObjectAVLTreeMap extends AbstractShort2ObjectMap implem } state = operator.apply(state, new BasicEntry<>(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Short2ObjectMap.Entry findFirst(Predicate> filter) { + public Optional> findFirst(Predicate> filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry<>(); for(Node 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 @@ -1783,7 +1785,7 @@ public class Short2ObjectAVLTreeMap extends AbstractShort2ObjectMap implem } @Override - public V reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); V state = null; boolean empty = true; @@ -1795,15 +1797,15 @@ public class Short2ObjectAVLTreeMap extends AbstractShort2ObjectMap implem } state = operator.apply(state, entry.value); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public V findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return null; + if(filter.test(entry.value)) return Optional.ofNullable(entry.value); + return Optional.empty(); } @Override @@ -2119,7 +2121,7 @@ public class Short2ObjectAVLTreeMap extends AbstractShort2ObjectMap implem } @Override - public V reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); V state = null; boolean empty = true; @@ -2131,15 +2133,15 @@ public class Short2ObjectAVLTreeMap extends AbstractShort2ObjectMap implem } state = operator.apply(state, entry.value); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public V findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return null; + if(filter.test(entry.value)) return Optional.ofNullable(entry.value); + return Optional.empty(); } @Override @@ -2280,7 +2282,7 @@ public class Short2ObjectAVLTreeMap extends AbstractShort2ObjectMap implem } @Override - public Short2ObjectMap.Entry reduce(ObjectObjectUnaryOperator, Short2ObjectMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Short2ObjectMap.Entry> operator) { Objects.requireNonNull(operator); Short2ObjectMap.Entry state = null; boolean empty = true; @@ -2292,19 +2294,19 @@ public class Short2ObjectAVLTreeMap extends AbstractShort2ObjectMap implem } state = operator.apply(state, new BasicEntry<>(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Short2ObjectMap.Entry findFirst(Predicate> filter) { + public Optional> findFirst(Predicate> filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry<>(); for(Node 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/main/java/speiger/src/collections/shorts/maps/impl/tree/Short2ObjectRBTreeMap.java b/src/main/java/speiger/src/collections/shorts/maps/impl/tree/Short2ObjectRBTreeMap.java index 7a845fc..9ee54d5 100644 --- a/src/main/java/speiger/src/collections/shorts/maps/impl/tree/Short2ObjectRBTreeMap.java +++ b/src/main/java/speiger/src/collections/shorts/maps/impl/tree/Short2ObjectRBTreeMap.java @@ -3,6 +3,7 @@ package speiger.src.collections.shorts.maps.impl.tree; import java.util.Collections; import java.util.Map; import java.util.Objects; +import java.util.Optional; import java.util.NoSuchElementException; import java.util.function.Consumer; import java.util.function.BiFunction; @@ -16,6 +17,7 @@ import speiger.src.collections.ints.functions.consumer.IntShortConsumer; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.shorts.functions.function.ShortFunction; import speiger.src.collections.shorts.functions.consumer.ShortObjectConsumer; +import speiger.src.collections.shorts.functions.OptionalShort; import speiger.src.collections.shorts.functions.function.ShortPredicate; import speiger.src.collections.shorts.functions.function.ShortObjectUnaryOperator; import speiger.src.collections.shorts.functions.function.ShortShortUnaryOperator; @@ -1070,7 +1072,7 @@ public class Short2ObjectRBTreeMap extends AbstractShort2ObjectMap impleme } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -1082,15 +1084,15 @@ public class Short2ObjectRBTreeMap extends AbstractShort2ObjectMap impleme } state = operator.applyAsShort(state, entry.key); } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); for(Node entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.test(entry.key)) return entry.key; - return (short)0; + if(filter.test(entry.key)) return OptionalShort.of(entry.key); + return OptionalShort.empty(); } @Override @@ -1727,7 +1729,7 @@ public class Short2ObjectRBTreeMap extends AbstractShort2ObjectMap impleme } @Override - public Short2ObjectMap.Entry reduce(ObjectObjectUnaryOperator, Short2ObjectMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Short2ObjectMap.Entry> operator) { Objects.requireNonNull(operator); Short2ObjectMap.Entry state = null; boolean empty = true; @@ -1739,19 +1741,19 @@ public class Short2ObjectRBTreeMap extends AbstractShort2ObjectMap impleme } state = operator.apply(state, new BasicEntry<>(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Short2ObjectMap.Entry findFirst(Predicate> filter) { + public Optional> findFirst(Predicate> filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry<>(); for(Node 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 @@ -1846,7 +1848,7 @@ public class Short2ObjectRBTreeMap extends AbstractShort2ObjectMap impleme } @Override - public V reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); V state = null; boolean empty = true; @@ -1858,15 +1860,15 @@ public class Short2ObjectRBTreeMap extends AbstractShort2ObjectMap impleme } state = operator.apply(state, entry.value); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public V findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return null; + if(filter.test(entry.value)) return Optional.ofNullable(entry.value); + return Optional.empty(); } @Override @@ -2182,7 +2184,7 @@ public class Short2ObjectRBTreeMap extends AbstractShort2ObjectMap impleme } @Override - public V reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); V state = null; boolean empty = true; @@ -2194,15 +2196,15 @@ public class Short2ObjectRBTreeMap extends AbstractShort2ObjectMap impleme } state = operator.apply(state, entry.value); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public V findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return null; + if(filter.test(entry.value)) return Optional.ofNullable(entry.value); + return Optional.empty(); } @Override @@ -2343,7 +2345,7 @@ public class Short2ObjectRBTreeMap extends AbstractShort2ObjectMap impleme } @Override - public Short2ObjectMap.Entry reduce(ObjectObjectUnaryOperator, Short2ObjectMap.Entry> operator) { + public Optional> reduce(ObjectObjectUnaryOperator, Short2ObjectMap.Entry> operator) { Objects.requireNonNull(operator); Short2ObjectMap.Entry state = null; boolean empty = true; @@ -2355,19 +2357,19 @@ public class Short2ObjectRBTreeMap extends AbstractShort2ObjectMap impleme } state = operator.apply(state, new BasicEntry<>(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Short2ObjectMap.Entry findFirst(Predicate> filter) { + public Optional> findFirst(Predicate> filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry<>(); for(Node 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/main/java/speiger/src/collections/shorts/maps/impl/tree/Short2ShortAVLTreeMap.java b/src/main/java/speiger/src/collections/shorts/maps/impl/tree/Short2ShortAVLTreeMap.java index 84dd0f5..59d5b32 100644 --- a/src/main/java/speiger/src/collections/shorts/maps/impl/tree/Short2ShortAVLTreeMap.java +++ b/src/main/java/speiger/src/collections/shorts/maps/impl/tree/Short2ShortAVLTreeMap.java @@ -3,6 +3,7 @@ package speiger.src.collections.shorts.maps.impl.tree; import java.util.Collections; import java.util.Map; import java.util.Objects; +import java.util.Optional; import java.util.NoSuchElementException; import java.util.function.Consumer; import java.util.function.BiFunction; @@ -17,6 +18,7 @@ import speiger.src.collections.ints.functions.consumer.IntShortConsumer; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.shorts.functions.function.ShortUnaryOperator; import speiger.src.collections.shorts.functions.consumer.ShortShortConsumer; +import speiger.src.collections.shorts.functions.OptionalShort; import speiger.src.collections.shorts.functions.function.ShortPredicate; import speiger.src.collections.shorts.functions.function.ShortShortUnaryOperator; import speiger.src.collections.shorts.maps.abstracts.AbstractShort2ShortMap; @@ -1121,7 +1123,7 @@ public class Short2ShortAVLTreeMap extends AbstractShort2ShortMap implements Sho } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -1133,15 +1135,15 @@ public class Short2ShortAVLTreeMap extends AbstractShort2ShortMap implements Sho } state = operator.applyAsShort(state, entry.key); } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); for(Node entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.test(entry.key)) return entry.key; - return (short)0; + if(filter.test(entry.key)) return OptionalShort.of(entry.key); + return OptionalShort.empty(); } @Override @@ -1775,7 +1777,7 @@ public class Short2ShortAVLTreeMap extends AbstractShort2ShortMap implements Sho } @Override - public Short2ShortMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Short2ShortMap.Entry state = null; boolean empty = true; @@ -1787,19 +1789,19 @@ public class Short2ShortAVLTreeMap extends AbstractShort2ShortMap implements Sho } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Short2ShortMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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 @@ -1894,7 +1896,7 @@ public class Short2ShortAVLTreeMap extends AbstractShort2ShortMap implements Sho } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -1906,15 +1908,15 @@ public class Short2ShortAVLTreeMap extends AbstractShort2ShortMap implements Sho } state = operator.applyAsShort(state, entry.value); } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return (short)0; + if(filter.test(entry.value)) return OptionalShort.of(entry.value); + return OptionalShort.empty(); } @Override @@ -2230,7 +2232,7 @@ public class Short2ShortAVLTreeMap extends AbstractShort2ShortMap implements Sho } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -2242,15 +2244,15 @@ public class Short2ShortAVLTreeMap extends AbstractShort2ShortMap implements Sho } state = operator.applyAsShort(state, entry.value); } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return (short)0; + if(filter.test(entry.value)) return OptionalShort.of(entry.value); + return OptionalShort.empty(); } @Override @@ -2391,7 +2393,7 @@ public class Short2ShortAVLTreeMap extends AbstractShort2ShortMap implements Sho } @Override - public Short2ShortMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Short2ShortMap.Entry state = null; boolean empty = true; @@ -2403,19 +2405,19 @@ public class Short2ShortAVLTreeMap extends AbstractShort2ShortMap implements Sho } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Short2ShortMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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/main/java/speiger/src/collections/shorts/maps/impl/tree/Short2ShortRBTreeMap.java b/src/main/java/speiger/src/collections/shorts/maps/impl/tree/Short2ShortRBTreeMap.java index 71544d0..13af7e2 100644 --- a/src/main/java/speiger/src/collections/shorts/maps/impl/tree/Short2ShortRBTreeMap.java +++ b/src/main/java/speiger/src/collections/shorts/maps/impl/tree/Short2ShortRBTreeMap.java @@ -3,6 +3,7 @@ package speiger.src.collections.shorts.maps.impl.tree; import java.util.Collections; import java.util.Map; import java.util.Objects; +import java.util.Optional; import java.util.NoSuchElementException; import java.util.function.Consumer; import java.util.function.BiFunction; @@ -16,6 +17,7 @@ import speiger.src.collections.ints.functions.consumer.IntShortConsumer; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.shorts.functions.function.ShortUnaryOperator; import speiger.src.collections.shorts.functions.consumer.ShortShortConsumer; +import speiger.src.collections.shorts.functions.OptionalShort; import speiger.src.collections.shorts.functions.function.ShortPredicate; import speiger.src.collections.shorts.functions.function.ShortShortUnaryOperator; import speiger.src.collections.shorts.maps.abstracts.AbstractShort2ShortMap; @@ -1174,7 +1176,7 @@ public class Short2ShortRBTreeMap extends AbstractShort2ShortMap implements Shor } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -1186,15 +1188,15 @@ public class Short2ShortRBTreeMap extends AbstractShort2ShortMap implements Shor } state = operator.applyAsShort(state, entry.key); } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); for(Node entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.test(entry.key)) return entry.key; - return (short)0; + if(filter.test(entry.key)) return OptionalShort.of(entry.key); + return OptionalShort.empty(); } @Override @@ -1838,7 +1840,7 @@ public class Short2ShortRBTreeMap extends AbstractShort2ShortMap implements Shor } @Override - public Short2ShortMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Short2ShortMap.Entry state = null; boolean empty = true; @@ -1850,19 +1852,19 @@ public class Short2ShortRBTreeMap extends AbstractShort2ShortMap implements Shor } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Short2ShortMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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 @@ -1957,7 +1959,7 @@ public class Short2ShortRBTreeMap extends AbstractShort2ShortMap implements Shor } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -1969,15 +1971,15 @@ public class Short2ShortRBTreeMap extends AbstractShort2ShortMap implements Shor } state = operator.applyAsShort(state, entry.value); } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); for(Node entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - if(filter.test(entry.value)) return entry.value; - return (short)0; + if(filter.test(entry.value)) return OptionalShort.of(entry.value); + return OptionalShort.empty(); } @Override @@ -2293,7 +2295,7 @@ public class Short2ShortRBTreeMap extends AbstractShort2ShortMap implements Shor } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -2305,15 +2307,15 @@ public class Short2ShortRBTreeMap extends AbstractShort2ShortMap implements Shor } state = operator.applyAsShort(state, entry.value); } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); for(Node entry = first;entry != null;entry = entry.next()) - if(filter.test(entry.value)) return entry.value; - return (short)0; + if(filter.test(entry.value)) return OptionalShort.of(entry.value); + return OptionalShort.empty(); } @Override @@ -2454,7 +2456,7 @@ public class Short2ShortRBTreeMap extends AbstractShort2ShortMap implements Shor } @Override - public Short2ShortMap.Entry reduce(ObjectObjectUnaryOperator operator) { + public Optional reduce(ObjectObjectUnaryOperator operator) { Objects.requireNonNull(operator); Short2ShortMap.Entry state = null; boolean empty = true; @@ -2466,19 +2468,19 @@ public class Short2ShortRBTreeMap extends AbstractShort2ShortMap implements Shor } state = operator.apply(state, new BasicEntry(entry.key, entry.value)); } - return state; + return empty ? Optional.empty() : Optional.ofNullable(state); } @Override - public Short2ShortMap.Entry findFirst(Predicate filter) { + public Optional findFirst(Predicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return null; + if(size() <= 0) return Optional.empty(); BasicEntry subEntry = new BasicEntry(); for(Node 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/main/java/speiger/src/collections/shorts/queues/ShortArrayFIFOQueue.java b/src/main/java/speiger/src/collections/shorts/queues/ShortArrayFIFOQueue.java index 65b8816..f8c291b 100644 --- a/src/main/java/speiger/src/collections/shorts/queues/ShortArrayFIFOQueue.java +++ b/src/main/java/speiger/src/collections/shorts/queues/ShortArrayFIFOQueue.java @@ -5,6 +5,7 @@ import java.util.Objects; import java.util.NoSuchElementException; import speiger.src.collections.shorts.collections.ShortIterator; +import speiger.src.collections.shorts.functions.OptionalShort; import speiger.src.collections.shorts.functions.ShortComparator; import speiger.src.collections.shorts.functions.ShortConsumer; import speiger.src.collections.ints.functions.consumer.IntShortConsumer; @@ -278,17 +279,17 @@ public class ShortArrayFIFOQueue extends AbstractShortPriorityQueue implements S } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); for(int i = 0,m=size();i= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor)); + } + + @Override + public void addLast(short o) { + if(strategy.equals(o, (short)0)) { + if(containsNull) return; + containsNull = true; + onNodeAdded(nullIndex); + } + else { + int pos = HashUtil.mix(strategy.hashCode(o)) & mask; + while(!strategy.equals(keys[pos], (short)0)) { + if(strategy.equals(keys[pos], o)) return; + pos = ++pos & mask; + } + keys[pos] = o; + onNodeAdded(pos); + } + if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor)); + } + @Override public boolean addAndMoveToFirst(short o) { if(strategy.equals(o, (short)0)) { @@ -618,7 +659,7 @@ public class ShortLinkedOpenCustomHashSet extends ShortOpenCustomHashSet impleme } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -631,18 +672,18 @@ public class ShortLinkedOpenCustomHashSet extends ShortOpenCustomHashSet impleme else state = operator.applyAsShort(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); int index = firstIndex; while(index != -1) { - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalShort.of(keys[index]); index = (int)links[index]; } - return (short)0; + return OptionalShort.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/shorts/sets/ShortLinkedOpenHashSet.java b/src/main/java/speiger/src/collections/shorts/sets/ShortLinkedOpenHashSet.java index bf1a1b7..71e40b0 100644 --- a/src/main/java/speiger/src/collections/shorts/sets/ShortLinkedOpenHashSet.java +++ b/src/main/java/speiger/src/collections/shorts/sets/ShortLinkedOpenHashSet.java @@ -10,6 +10,7 @@ import speiger.src.collections.shorts.collections.ShortCollection; import speiger.src.collections.shorts.collections.ShortIterator; import speiger.src.collections.shorts.collections.ShortBidirectionalIterator; import speiger.src.collections.shorts.functions.ShortConsumer; +import speiger.src.collections.shorts.functions.OptionalShort; import speiger.src.collections.ints.functions.consumer.IntShortConsumer; import speiger.src.collections.objects.functions.consumer.ObjectShortConsumer; import speiger.src.collections.shorts.functions.function.ShortPredicate; @@ -183,6 +184,46 @@ public class ShortLinkedOpenHashSet extends ShortOpenHashSet implements ShortOrd while(iterator.hasNext()) add(iterator.nextShort()); } + @Override + public void addFirst(short o) { + if(o == (short)0) { + if(containsNull) return; + containsNull = true; + onNodeAdded(nullIndex); + moveToFirstIndex(nullIndex); + } + else { + int pos = HashUtil.mix(Short.hashCode(o)) & mask; + while(keys[pos] != (short)0) { + if(keys[pos] == o) return; + pos = ++pos & mask; + } + keys[pos] = o; + onNodeAdded(pos); + moveToFirstIndex(pos); + } + if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor)); + } + + @Override + public void addLast(short o) { + if(o == (short)0) { + if(containsNull) return; + containsNull = true; + onNodeAdded(nullIndex); + } + else { + int pos = HashUtil.mix(Short.hashCode(o)) & mask; + while(keys[pos] != (short)0) { + if(keys[pos] == o) return; + pos = ++pos & mask; + } + keys[pos] = o; + onNodeAdded(pos); + } + if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor)); + } + @Override public boolean addAndMoveToFirst(short o) { if(o == (short)0) { @@ -471,7 +512,7 @@ public class ShortLinkedOpenHashSet extends ShortOpenHashSet implements ShortOrd } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -484,18 +525,18 @@ public class ShortLinkedOpenHashSet extends ShortOpenHashSet implements ShortOrd else state = operator.applyAsShort(state, keys[index]); index = (int)links[index]; } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); int index = firstIndex; while(index != -1) { - if(filter.test(keys[index])) return keys[index]; + if(filter.test(keys[index])) return OptionalShort.of(keys[index]); index = (int)links[index]; } - return (short)0; + return OptionalShort.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/shorts/sets/ShortOpenCustomHashSet.java b/src/main/java/speiger/src/collections/shorts/sets/ShortOpenCustomHashSet.java index 4a64b8b..2a543b1 100644 --- a/src/main/java/speiger/src/collections/shorts/sets/ShortOpenCustomHashSet.java +++ b/src/main/java/speiger/src/collections/shorts/sets/ShortOpenCustomHashSet.java @@ -9,6 +9,7 @@ import java.util.Objects; import speiger.src.collections.shorts.collections.ShortCollection; import speiger.src.collections.shorts.collections.ShortIterator; +import speiger.src.collections.shorts.functions.OptionalShort; import speiger.src.collections.shorts.utils.ShortIterators; import speiger.src.collections.shorts.functions.ShortConsumer; import speiger.src.collections.ints.functions.consumer.IntShortConsumer; @@ -546,7 +547,7 @@ public class ShortOpenCustomHashSet extends AbstractShortSet implements ITrimmab } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -563,18 +564,18 @@ public class ShortOpenCustomHashSet extends AbstractShortSet implements ITrimmab } state = operator.applyAsShort(state, keys[i]); } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (short)0; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OptionalShort.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OptionalShort.of(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(!strategy.equals(keys[i], (short)0) && filter.test(keys[i])) return keys[i]; + if(!strategy.equals(keys[i], (short)0) && filter.test(keys[i])) return OptionalShort.of(keys[i]); } - return (short)0; + return OptionalShort.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/shorts/sets/ShortOpenHashSet.java b/src/main/java/speiger/src/collections/shorts/sets/ShortOpenHashSet.java index 006177f..c574f16 100644 --- a/src/main/java/speiger/src/collections/shorts/sets/ShortOpenHashSet.java +++ b/src/main/java/speiger/src/collections/shorts/sets/ShortOpenHashSet.java @@ -11,6 +11,7 @@ import speiger.src.collections.shorts.collections.ShortCollection; import speiger.src.collections.shorts.collections.ShortIterator; import speiger.src.collections.shorts.utils.ShortIterators; import speiger.src.collections.shorts.functions.ShortConsumer; +import speiger.src.collections.shorts.functions.OptionalShort; import speiger.src.collections.ints.functions.consumer.IntShortConsumer; import speiger.src.collections.objects.functions.consumer.ObjectShortConsumer; import speiger.src.collections.shorts.functions.function.ShortPredicate; @@ -428,7 +429,7 @@ public class ShortOpenHashSet extends AbstractShortSet implements ITrimmable } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -445,18 +446,18 @@ public class ShortOpenHashSet extends AbstractShortSet implements ITrimmable } state = operator.applyAsShort(state, keys[i]); } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); - if(size() <= 0) return (short)0; - if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; + if(size() <= 0) return OptionalShort.empty(); + if(containsNull && filter.test(keys[nullIndex])) return OptionalShort.of(keys[nullIndex]); for(int i = nullIndex-1;i>=0;i--) { - if(keys[i] != (short)0 && filter.test(keys[i])) return keys[i]; + if(keys[i] != (short)0 && filter.test(keys[i])) return OptionalShort.of(keys[i]); } - return (short)0; + return OptionalShort.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/shorts/sets/ShortOrderedSet.java b/src/main/java/speiger/src/collections/shorts/sets/ShortOrderedSet.java index 11edaed..248498d 100644 --- a/src/main/java/speiger/src/collections/shorts/sets/ShortOrderedSet.java +++ b/src/main/java/speiger/src/collections/shorts/sets/ShortOrderedSet.java @@ -1,6 +1,7 @@ package speiger.src.collections.shorts.sets; import speiger.src.collections.shorts.collections.ShortBidirectionalIterator; +import speiger.src.collections.shorts.collections.ShortOrderedCollection; import speiger.src.collections.shorts.collections.ShortSplititerator; import speiger.src.collections.shorts.utils.ShortSplititerators; import speiger.src.collections.shorts.sets.AbstractShortSet.ReversedShortOrderedSet; @@ -13,7 +14,7 @@ import speiger.src.collections.shorts.utils.ShortSets; * This interface provides basic access to such functions while also providing some Sorted/NaivgableSet implementations that still fit into here. * */ -public interface ShortOrderedSet extends ShortSet +public interface ShortOrderedSet extends ShortSet, ShortOrderedCollection { /** * A customized add method that allows you to insert into the first index. diff --git a/src/main/java/speiger/src/collections/shorts/sets/ShortRBTreeSet.java b/src/main/java/speiger/src/collections/shorts/sets/ShortRBTreeSet.java index 3ae742e..c2dce6f 100644 --- a/src/main/java/speiger/src/collections/shorts/sets/ShortRBTreeSet.java +++ b/src/main/java/speiger/src/collections/shorts/sets/ShortRBTreeSet.java @@ -9,6 +9,7 @@ import java.util.Objects; import speiger.src.collections.shorts.collections.ShortBidirectionalIterator; import speiger.src.collections.shorts.functions.ShortComparator; import speiger.src.collections.shorts.functions.ShortConsumer; +import speiger.src.collections.shorts.functions.OptionalShort; import speiger.src.collections.ints.functions.consumer.IntShortConsumer; import speiger.src.collections.objects.functions.consumer.ObjectShortConsumer; import speiger.src.collections.shorts.functions.function.ShortPredicate; @@ -334,7 +335,7 @@ public class ShortRBTreeSet extends AbstractShortSet implements ShortNavigableSe } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -346,16 +347,16 @@ public class ShortRBTreeSet extends AbstractShortSet implements ShortNavigableSe } state = operator.applyAsShort(state, entry.key); } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); for(Entry entry = first;entry != null;entry = entry.next()) { - if(filter.test(entry.key)) return entry.key; + if(filter.test(entry.key)) return OptionalShort.of(entry.key); } - return (short)0; + return OptionalShort.empty(); } @Override @@ -1241,7 +1242,7 @@ public class ShortRBTreeSet extends AbstractShortSet implements ShortNavigableSe } @Override - public short reduce(ShortShortUnaryOperator operator) { + public OptionalShort reduce(ShortShortUnaryOperator operator) { Objects.requireNonNull(operator); short state = (short)0; boolean empty = true; @@ -1253,16 +1254,16 @@ public class ShortRBTreeSet extends AbstractShortSet implements ShortNavigableSe } state = operator.applyAsShort(state, entry.key); } - return state; + return empty ? OptionalShort.empty() : OptionalShort.of(state); } @Override - public short findFirst(ShortPredicate filter) { + public OptionalShort findFirst(ShortPredicate filter) { Objects.requireNonNull(filter); for(Entry entry = start();entry != null && inRange(entry.key);entry = next(entry)) { - if(filter.test(entry.key)) return entry.key; + if(filter.test(entry.key)) return OptionalShort.of(entry.key); } - return (short)0; + return OptionalShort.empty(); } @Override diff --git a/src/main/java/speiger/src/collections/shorts/utils/ShortCollections.java b/src/main/java/speiger/src/collections/shorts/utils/ShortCollections.java index 25906ae..794057c 100644 --- a/src/main/java/speiger/src/collections/shorts/utils/ShortCollections.java +++ b/src/main/java/speiger/src/collections/shorts/utils/ShortCollections.java @@ -15,6 +15,7 @@ import speiger.src.collections.shorts.collections.ShortCollection; import speiger.src.collections.shorts.collections.ShortOrderedCollection; import speiger.src.collections.shorts.collections.ShortIterator; import speiger.src.collections.shorts.functions.ShortComparator; +import speiger.src.collections.shorts.functions.OptionalShort; import speiger.src.collections.objects.utils.ObjectArrays; import speiger.src.collections.shorts.functions.ShortConsumer; import speiger.src.collections.shorts.functions.function.ShortPredicate; @@ -758,9 +759,9 @@ public class ShortCollections @Override public short reduce(short identity, ShortShortUnaryOperator operator) { synchronized(mutex) { return c.reduce(identity, operator); } } @Override - public short reduce(ShortShortUnaryOperator operator) { synchronized(mutex) { return c.reduce(operator); } } + public OptionalShort reduce(ShortShortUnaryOperator operator) { synchronized(mutex) { return c.reduce(operator); } } @Override - public short findFirst(ShortPredicate filter) { synchronized(mutex) { return c.findFirst(filter); } } + public OptionalShort findFirst(ShortPredicate filter) { synchronized(mutex) { return c.findFirst(filter); } } @Override public int count(ShortPredicate filter) { synchronized(mutex) { return c.count(filter); } } } @@ -888,9 +889,9 @@ public class ShortCollections @Override public short reduce(short identity, ShortShortUnaryOperator operator) { return c.reduce(identity, operator); } @Override - public short reduce(ShortShortUnaryOperator operator) { return c.reduce(operator); } + public OptionalShort reduce(ShortShortUnaryOperator operator) { return c.reduce(operator); } @Override - public short findFirst(ShortPredicate filter) { return c.findFirst(filter); } + public OptionalShort findFirst(ShortPredicate filter) { return c.findFirst(filter); } @Override public int count(ShortPredicate filter) { return c.count(filter); } } diff --git a/src/main/java/speiger/src/collections/shorts/utils/ShortPriorityQueues.java b/src/main/java/speiger/src/collections/shorts/utils/ShortPriorityQueues.java index e060c64..387335b 100644 --- a/src/main/java/speiger/src/collections/shorts/utils/ShortPriorityQueues.java +++ b/src/main/java/speiger/src/collections/shorts/utils/ShortPriorityQueues.java @@ -1,13 +1,14 @@ package speiger.src.collections.shorts.utils; - import speiger.src.collections.shorts.collections.ShortIterator; import speiger.src.collections.shorts.collections.ShortCollection; import speiger.src.collections.shorts.functions.ShortComparator; +import speiger.src.collections.shorts.functions.OptionalShort; import speiger.src.collections.shorts.queues.ShortPriorityDequeue; import speiger.src.collections.shorts.queues.ShortPriorityQueue; import speiger.src.collections.shorts.functions.ShortConsumer; import speiger.src.collections.shorts.functions.function.ShortPredicate; +import speiger.src.collections.shorts.functions.function.ShortShortUnaryOperator; import speiger.src.collections.objects.functions.consumer.ObjectShortConsumer; /** @@ -113,7 +114,11 @@ public class ShortPriorityQueues @Override public boolean matchesAll(ShortPredicate filter) { synchronized(mutex) { return queue.matchesAll(filter); } } @Override - public short findFirst(ShortPredicate filter) { synchronized(mutex) { return queue.findFirst(filter); } } + public OptionalShort findFirst(ShortPredicate filter) { synchronized(mutex) { return queue.findFirst(filter); } } + @Override + public short reduce(short identity, ShortShortUnaryOperator operator) { synchronized(mutex) { return queue.reduce(identity, operator); } } + @Override + public OptionalShort reduce(ShortShortUnaryOperator operator) { synchronized(mutex) { return queue.reduce(operator); } } @Override public int count(ShortPredicate filter) { synchronized(mutex) { return queue.count(filter); } } } diff --git a/src/main/java/speiger/src/collections/shorts/utils/ShortSets.java b/src/main/java/speiger/src/collections/shorts/utils/ShortSets.java index a243453..c468d22 100644 --- a/src/main/java/speiger/src/collections/shorts/utils/ShortSets.java +++ b/src/main/java/speiger/src/collections/shorts/utils/ShortSets.java @@ -301,6 +301,10 @@ public class ShortSets s = c; } + @Override + public void addFirst(short o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(short o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(short o) { throw new UnsupportedOperationException(); } @Override @@ -573,7 +577,11 @@ public class ShortSets super(c, mutex); s = c; } - + + @Override + public void addFirst(short o) { synchronized(mutex) { s.addFirst(o); } } + @Override + public void addLast(short o) { synchronized(mutex) { s.addLast(o); } } @Override public boolean addAndMoveToFirst(short o) { synchronized(mutex) { return s.addAndMoveToFirst(o); } } @Override diff --git a/src/main/java/speiger/src/collections/shorts/utils/maps/Short2BooleanMaps.java b/src/main/java/speiger/src/collections/shorts/utils/maps/Short2BooleanMaps.java index d69af44..693fda3 100644 --- a/src/main/java/speiger/src/collections/shorts/utils/maps/Short2BooleanMaps.java +++ b/src/main/java/speiger/src/collections/shorts/utils/maps/Short2BooleanMaps.java @@ -608,7 +608,11 @@ public class Short2BooleanMaps super(c); set = c; } - + + @Override + public void addFirst(Short2BooleanMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Short2BooleanMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Short2BooleanMap.Entry o) { throw new UnsupportedOperationException(); } @Override diff --git a/src/main/java/speiger/src/collections/shorts/utils/maps/Short2ByteMaps.java b/src/main/java/speiger/src/collections/shorts/utils/maps/Short2ByteMaps.java index e94e144..f987e90 100644 --- a/src/main/java/speiger/src/collections/shorts/utils/maps/Short2ByteMaps.java +++ b/src/main/java/speiger/src/collections/shorts/utils/maps/Short2ByteMaps.java @@ -620,7 +620,11 @@ public class Short2ByteMaps super(c); set = c; } - + + @Override + public void addFirst(Short2ByteMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Short2ByteMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Short2ByteMap.Entry o) { throw new UnsupportedOperationException(); } @Override diff --git a/src/main/java/speiger/src/collections/shorts/utils/maps/Short2CharMaps.java b/src/main/java/speiger/src/collections/shorts/utils/maps/Short2CharMaps.java index 98c91ed..8741806 100644 --- a/src/main/java/speiger/src/collections/shorts/utils/maps/Short2CharMaps.java +++ b/src/main/java/speiger/src/collections/shorts/utils/maps/Short2CharMaps.java @@ -620,7 +620,11 @@ public class Short2CharMaps super(c); set = c; } - + + @Override + public void addFirst(Short2CharMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Short2CharMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Short2CharMap.Entry o) { throw new UnsupportedOperationException(); } @Override diff --git a/src/main/java/speiger/src/collections/shorts/utils/maps/Short2DoubleMaps.java b/src/main/java/speiger/src/collections/shorts/utils/maps/Short2DoubleMaps.java index e99f55f..977895f 100644 --- a/src/main/java/speiger/src/collections/shorts/utils/maps/Short2DoubleMaps.java +++ b/src/main/java/speiger/src/collections/shorts/utils/maps/Short2DoubleMaps.java @@ -620,7 +620,11 @@ public class Short2DoubleMaps super(c); set = c; } - + + @Override + public void addFirst(Short2DoubleMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Short2DoubleMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Short2DoubleMap.Entry o) { throw new UnsupportedOperationException(); } @Override diff --git a/src/main/java/speiger/src/collections/shorts/utils/maps/Short2FloatMaps.java b/src/main/java/speiger/src/collections/shorts/utils/maps/Short2FloatMaps.java index e278ffb..6816750 100644 --- a/src/main/java/speiger/src/collections/shorts/utils/maps/Short2FloatMaps.java +++ b/src/main/java/speiger/src/collections/shorts/utils/maps/Short2FloatMaps.java @@ -620,7 +620,11 @@ public class Short2FloatMaps super(c); set = c; } - + + @Override + public void addFirst(Short2FloatMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Short2FloatMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Short2FloatMap.Entry o) { throw new UnsupportedOperationException(); } @Override diff --git a/src/main/java/speiger/src/collections/shorts/utils/maps/Short2IntMaps.java b/src/main/java/speiger/src/collections/shorts/utils/maps/Short2IntMaps.java index 055e52f..d8f345f 100644 --- a/src/main/java/speiger/src/collections/shorts/utils/maps/Short2IntMaps.java +++ b/src/main/java/speiger/src/collections/shorts/utils/maps/Short2IntMaps.java @@ -620,7 +620,11 @@ public class Short2IntMaps super(c); set = c; } - + + @Override + public void addFirst(Short2IntMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Short2IntMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Short2IntMap.Entry o) { throw new UnsupportedOperationException(); } @Override diff --git a/src/main/java/speiger/src/collections/shorts/utils/maps/Short2LongMaps.java b/src/main/java/speiger/src/collections/shorts/utils/maps/Short2LongMaps.java index b785ef4..01e6a79 100644 --- a/src/main/java/speiger/src/collections/shorts/utils/maps/Short2LongMaps.java +++ b/src/main/java/speiger/src/collections/shorts/utils/maps/Short2LongMaps.java @@ -620,7 +620,11 @@ public class Short2LongMaps super(c); set = c; } - + + @Override + public void addFirst(Short2LongMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Short2LongMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Short2LongMap.Entry o) { throw new UnsupportedOperationException(); } @Override diff --git a/src/main/java/speiger/src/collections/shorts/utils/maps/Short2ObjectMaps.java b/src/main/java/speiger/src/collections/shorts/utils/maps/Short2ObjectMaps.java index 2a85e4d..76a5474 100644 --- a/src/main/java/speiger/src/collections/shorts/utils/maps/Short2ObjectMaps.java +++ b/src/main/java/speiger/src/collections/shorts/utils/maps/Short2ObjectMaps.java @@ -611,7 +611,11 @@ public class Short2ObjectMaps super(c); set = c; } - + + @Override + public void addFirst(Short2ObjectMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Short2ObjectMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Short2ObjectMap.Entry o) { throw new UnsupportedOperationException(); } @Override diff --git a/src/main/java/speiger/src/collections/shorts/utils/maps/Short2ShortMaps.java b/src/main/java/speiger/src/collections/shorts/utils/maps/Short2ShortMaps.java index 4996041..78b198b 100644 --- a/src/main/java/speiger/src/collections/shorts/utils/maps/Short2ShortMaps.java +++ b/src/main/java/speiger/src/collections/shorts/utils/maps/Short2ShortMaps.java @@ -619,7 +619,11 @@ public class Short2ShortMaps super(c); set = c; } - + + @Override + public void addFirst(Short2ShortMap.Entry o) { throw new UnsupportedOperationException(); } + @Override + public void addLast(Short2ShortMap.Entry o) { throw new UnsupportedOperationException(); } @Override public boolean addAndMoveToFirst(Short2ShortMap.Entry o) { throw new UnsupportedOperationException(); } @Override diff --git a/src/test/java/speiger/src/collections/ints/base/BaseInt2IntNavigableMapTest.java b/src/test/java/speiger/src/collections/ints/base/BaseInt2IntNavigableMapTest.java index 1772330..d8973a3 100644 --- a/src/test/java/speiger/src/collections/ints/base/BaseInt2IntNavigableMapTest.java +++ b/src/test/java/speiger/src/collections/ints/base/BaseInt2IntNavigableMapTest.java @@ -9,8 +9,7 @@ import speiger.src.collections.ints.maps.interfaces.Int2IntNavigableMap; import speiger.src.collections.tests.NavigableSetTest; @SuppressWarnings("javadoc") -public abstract class BaseInt2IntNavigableMapTest extends BaseInt2IntSortedMapTest -{ +public abstract class BaseInt2IntNavigableMapTest extends BaseInt2IntSortedMapTest { @Override public abstract Int2IntNavigableMap createMap(int[] keys, int[] values); @Override @@ -29,30 +28,22 @@ public abstract class BaseInt2IntNavigableMapTest extends BaseInt2IntSortedMapTe @Test public void lowerTest() { - if(getValidNavigableMapTests().contains(NavigableSetTest.LOWER)) { - Assert.assertTrue(createMap(TEST_ARRAY, TEST_ARRAY).lowerKey(50) < 50); - } + if(getValidNavigableMapTests().contains(NavigableSetTest.LOWER)) { Assert.assertTrue(createMap(TEST_ARRAY, TEST_ARRAY).lowerKey(50) < 50); } } @Test public void higherTest() { - if(getValidNavigableMapTests().contains(NavigableSetTest.HIGHER)) { - Assert.assertTrue(createMap(TEST_ARRAY, TEST_ARRAY).higherKey(50) > 50); - } + if(getValidNavigableMapTests().contains(NavigableSetTest.HIGHER)) { Assert.assertTrue(createMap(TEST_ARRAY, TEST_ARRAY).higherKey(50) > 50); } } @Test public void ceilTest() { - if(getValidNavigableMapTests().contains(NavigableSetTest.CEILING)) { - Assert.assertTrue(createMap(TEST_ARRAY, TEST_ARRAY).ceilingKey(50) >= 50); - } + if(getValidNavigableMapTests().contains(NavigableSetTest.CEILING)) { Assert.assertTrue(createMap(TEST_ARRAY, TEST_ARRAY).ceilingKey(50) >= 50); } } @Test public void floorTest() { - if(getValidNavigableMapTests().contains(NavigableSetTest.FLOOR)) { - Assert.assertTrue(createMap(TEST_ARRAY, TEST_ARRAY).floorKey(50) <= 50); - } + if(getValidNavigableMapTests().contains(NavigableSetTest.FLOOR)) { Assert.assertTrue(createMap(TEST_ARRAY, TEST_ARRAY).floorKey(50) <= 50); } } @Test @@ -75,7 +66,7 @@ public abstract class BaseInt2IntNavigableMapTest extends BaseInt2IntSortedMapTe Assert.assertTrue(subSet.lowerKey(50) < 50); Assert.assertTrue(subSet.higherKey(50) > 50); Assert.assertTrue(subSet.ceilingKey(50) >= 50); - Assert.assertTrue(subSet.floorKey(50) <= 50); + Assert.assertTrue(subSet.floorKey(50) <= 50); } }