diff --git a/eclipse-collections-code-generator/src/main/resources/impl/bag/immutable/immutablePrimitiveBagFactoryImpl.stg b/eclipse-collections-code-generator/src/main/resources/impl/bag/immutable/immutablePrimitiveBagFactoryImpl.stg index 87cd974e5f..6641ae1b6f 100644 --- a/eclipse-collections-code-generator/src/main/resources/impl/bag/immutable/immutablePrimitiveBagFactoryImpl.stg +++ b/eclipse-collections-code-generator/src/main/resources/impl/bag/immutable/immutablePrimitiveBagFactoryImpl.stg @@ -15,7 +15,9 @@ package org.eclipse.collections.impl.bag.immutable.primitive; import org.eclipse.collections.api.Iterable; import org.eclipse.collections.api.bag.primitive.ImmutableBag; +import org.eclipse.collections.api.bag.primitive.MutableBag; import org.eclipse.collections.api.factory.bag.primitive.ImmutableBagFactory; +import org.eclipse.collections.impl.bag.mutable.primitive.HashBag; import org.eclipse.collections.impl.factory.primitive.Bags; <(wideStreamImport.(type))> @@ -92,7 +94,21 @@ public class ImmutableBagFactoryImpl implements ImmutableBagFactory { return (ImmutableBag) items; } - return this.with(items.toArray()); + if (items == null) + { + return this.with(); + } + MutableBag bag = new HashBag(); + items.forEach(bag::add); + if (bag.size() == 0) + { + return this.with(); + } + if (bag.size() == 1) + { + return this.with(bag.toArray()); + } + return ImmutableHashBag.newBagWith(bag); } /** @@ -145,7 +161,21 @@ public ImmutableBag ofAll(<(wideStream.(type))> items) @Override public ImmutableBag withAll(<(wideStream.(type))> items) { - return this.with(items.toArray()); + if (items == null) + { + return this.with(); + } + MutableBag bag = new HashBag(); + items.forEach(bag::add); + if (bag.size() == 0) + { + return this.with(); + } + if (bag.size() == 1) + { + return this.with(bag.toArray()); + } + return ImmutableHashBag.newBagWith(bag); } >> diff --git a/eclipse-collections-code-generator/src/main/resources/impl/bag/immutable/immutablePrimitiveHashBag.stg b/eclipse-collections-code-generator/src/main/resources/impl/bag/immutable/immutablePrimitiveHashBag.stg index c574bfd217..4b4b4d72d8 100644 --- a/eclipse-collections-code-generator/src/main/resources/impl/bag/immutable/immutablePrimitiveHashBag.stg +++ b/eclipse-collections-code-generator/src/main/resources/impl/bag/immutable/immutablePrimitiveHashBag.stg @@ -66,6 +66,13 @@ final class ImmutableHashBag implements ImmutableBag, Serializable this.delegate = HashBag.newBagWith(newElements); } + private ImmutableHashBag(MutableBag delegate) + { + MutableBag defensiveCopy = new HashBag(delegate); + this.checkOptimizedSize(defensiveCopy.size()); + this.delegate = defensiveCopy; + } + private void checkOptimizedSize(int length) { if (length \<= 1) @@ -79,6 +86,11 @@ final class ImmutableHashBag implements ImmutableBag, Serializable return new ImmutableHashBag(elements); } + public static ImmutableHashBag newBagWith(MutableBag elements) + { + return new ImmutableHashBag(elements); + } + @Override public ImmutableBag newWith( element) { diff --git a/eclipse-collections-code-generator/src/main/resources/impl/bag/mutable/mutablePrimitiveBagFactoryImpl.stg b/eclipse-collections-code-generator/src/main/resources/impl/bag/mutable/mutablePrimitiveBagFactoryImpl.stg index 40cee5a321..1dabdb7519 100644 --- a/eclipse-collections-code-generator/src/main/resources/impl/bag/mutable/mutablePrimitiveBagFactoryImpl.stg +++ b/eclipse-collections-code-generator/src/main/resources/impl/bag/mutable/mutablePrimitiveBagFactoryImpl.stg @@ -126,7 +126,9 @@ public MutableBag ofAll(<(wideStream.(type))> items) @Override public MutableBag withAll(<(wideStream.(type))> items) { - return this.with(items.toArray()); + MutableBag bag = new HashBag(); + items.forEach(bag::add); + return bag; } >> diff --git a/eclipse-collections-code-generator/src/main/resources/test/bag/immutable/immutablePrimitiveHashBagTest.stg b/eclipse-collections-code-generator/src/main/resources/test/bag/immutable/immutablePrimitiveHashBagTest.stg index 7b7be22296..34a33327dc 100644 --- a/eclipse-collections-code-generator/src/main/resources/test/bag/immutable/immutablePrimitiveHashBagTest.stg +++ b/eclipse-collections-code-generator/src/main/resources/test/bag/immutable/immutablePrimitiveHashBagTest.stg @@ -19,6 +19,8 @@ body(type, name, wrapperName) ::= << package org.eclipse.collections.impl.bag.immutable.primitive; import org.eclipse.collections.api.bag.primitive.ImmutableBag; +import org.eclipse.collections.api.bag.primitive.MutableBag; +import org.eclipse.collections.impl.bag.mutable.primitive.HashBag; import org.eclipse.collections.impl.factory.primitive.Bags; import org.eclipse.collections.impl.math.Mutable; import org.junit.Assert; @@ -60,6 +62,21 @@ public class ImmutableHashBagTest extends AbstractImmutableBagTestCa ImmutableSet actual = bag.selectUnique(); Assert.assertEquals(expected, actual); } + + @Test + public void newBagWithMutable() + { + HashBag mutable3 = HashBag.newBagWith(<["1", "2", "3"]:(literal.(type))(); separator=", ">); + ImmutableBag immutable3 = mutable3.toImmutable(); + HashBag mutable4 = HashBag.newBagWith(<["1", "2", "3", "4"]:(literal.(type))(); separator=", ">); + MutableBag mutableBag = new HashBag(<["1", "2", "3"]:(literal.(type))(); separator=", ">); + ImmutableHashBag immutableBag = ImmutableHashBag.newBagWith(mutableBag); + Assert.assertEquals(mutable3, mutableBag); + Assert.assertEquals(immutable3, immutableBag); + mutableBag.add(<(literal.(type))("4")>); + Assert.assertEquals(mutable4, mutableBag); + Assert.assertEquals(immutable3, immutableBag); + } } >> diff --git a/unit-tests/src/test/java/org/eclipse/collections/impl/stream/PrimitiveStreamsTest.java b/unit-tests/src/test/java/org/eclipse/collections/impl/stream/PrimitiveStreamsTest.java index 87f8c7985e..c81b04fac3 100644 --- a/unit-tests/src/test/java/org/eclipse/collections/impl/stream/PrimitiveStreamsTest.java +++ b/unit-tests/src/test/java/org/eclipse/collections/impl/stream/PrimitiveStreamsTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 The Bank of New York Mellon. + * Copyright (c) 2022 The Bank of New York Mellon and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * and Eclipse Distribution License v. 1.0 which accompany this distribution. @@ -51,6 +51,7 @@ import org.eclipse.collections.impl.factory.primitive.LongSets; import org.eclipse.collections.impl.factory.primitive.LongStacks; import org.eclipse.collections.impl.list.primitive.IntInterval; +import org.eclipse.collections.impl.list.primitive.LongInterval; import org.junit.Assert; import org.junit.Test; @@ -107,6 +108,21 @@ public void toImmutableIntBag() Assert.assertEquals(IntBags.mutable.ofAll(IntStream.rangeClosed(1, 10)), bag); } + @Test + public void toEmptyImmutableIntBag() + { + ImmutableIntBag bag = PrimitiveStreams.iIntBag(IntStream.empty()); + Assert.assertEquals(IntBags.immutable.empty(), bag); + } + + @Test + public void toImmutableIntBagWithOneElement() + { + ImmutableIntBag bag = PrimitiveStreams.iIntBag(IntStream.rangeClosed(1, 1)); + Assert.assertEquals(IntInterval.oneTo(1).toBag(), bag); + Assert.assertEquals(IntBags.mutable.ofAll(IntStream.rangeClosed(1, 1)), bag); + } + @Test public void toIntStack() { @@ -171,6 +187,21 @@ public void toImmutableLongBag() Assert.assertEquals(LongBags.mutable.ofAll(LongStream.rangeClosed(1, 10)), bag); } + @Test + public void toEmptyImmutableLongBag() + { + ImmutableLongBag bag = PrimitiveStreams.iLongBag(LongStream.empty()); + Assert.assertEquals(LongBags.immutable.empty(), bag); + } + + @Test + public void toImmutableLongBagWithOneElement() + { + ImmutableLongBag bag = PrimitiveStreams.iLongBag(LongStream.rangeClosed(1, 1)); + Assert.assertEquals(LongInterval.oneTo(1).toBag(), bag); + Assert.assertEquals(LongBags.mutable.ofAll(LongStream.rangeClosed(1, 1)), bag); + } + @Test public void toLongStack() { @@ -235,6 +266,20 @@ public void toImmutableDoubleBag() Assert.assertEquals(DoubleBags.mutable.ofAll(DoubleStream.of(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0)), bag); } + @Test + public void toEmptyImmutableDoubleBag() + { + ImmutableDoubleBag bag = PrimitiveStreams.iDoubleBag(DoubleStream.empty()); + Assert.assertEquals(DoubleBags.immutable.empty(), bag); + } + + @Test + public void toImmutableDoubleBagWithOneElement() + { + ImmutableDoubleBag bag = PrimitiveStreams.iDoubleBag(DoubleStream.of(1.0)); + Assert.assertEquals(DoubleBags.mutable.ofAll(DoubleStream.of(1.0)), bag); + } + @Test public void toDoubleStack() {