Skip to content

Commit

Permalink
Merge pull request #1013 from prathasirisha/master
Browse files Browse the repository at this point in the history
Implement union operation on primitive sets. Partially addresses issue #310.
  • Loading branch information
donraab committed Oct 9, 2020
2 parents 6b73937 + e1daaf9 commit 3dd6c76
Show file tree
Hide file tree
Showing 9 changed files with 263 additions and 8 deletions.
Expand Up @@ -43,6 +43,22 @@ public interface Immutable<name>Set extends Immutable<name>Collection, <name>Set
@Override
\<V> ImmutableSet\<V> collect(<name>ToObjectFunction\<? extends V> function);

/**
* @since 11.0.
*/
@Override
default Immutable<name>Set union(<name>Set set)
{
if (this.size() > set.size())
{
return this.toSet().withAll(set).toImmutable();
}
else
{
return set.toSet().withAll(this).toImmutable();
}
}

<immutableAPI(fileName(primitive), type, name)>
}

Expand Down
Expand Up @@ -72,6 +72,22 @@ public interface Mutable<name>Set extends Mutable<name>Collection, <name>Set
{
throw new UnsupportedOperationException("Implement in concrete classes.");
}

/**
* @since 11.0.
*/
@Override
default Mutable<name>Set union(<name>Set set)
{
if (this.size() > set.size())
{
return this.toSet().withAll(set);
}
else
{
return set.toSet().withAll(this);
}
}
}

>>
Expand Up @@ -39,6 +39,11 @@ public interface <name>Set extends <name>Iterable
return this;
}

/**
* @since 11.0.
*/
<name>Set union(<name>Set set);

/**
* Follows the same general contract as {@link Set#equals(Object)}.
*/
Expand Down
@@ -1,4 +1,4 @@
import "copyright.stg"
import "copyrightAndOthers.stg"
import "primitiveLiteral.stg"

skipBoolean() ::= "true"
Expand All @@ -14,7 +14,7 @@ class(primitive) ::= <<
>>

body(type, wrapperName, name) ::= <<
<copyright()>
<copyrightAndOthers()>

package org.eclipse.collections.impl.set.mutable.primitive;

Expand Down Expand Up @@ -263,6 +263,46 @@ public abstract class AbstractImmutable<name>HashSetTestCase extends AbstractImm
Assert.assertEquals(set.toSet(), set.asLazy().toSet());
Verify.assertInstanceOf(Lazy<name>Iterable.class, set.asLazy());
}

@Test
public void union()
{
this.assertUnion(
this.newWith(<["1", "2", "3"]:(literal.(type))(); separator=", ">),
this.newWith(<["3", "4", "5"]:(literal.(type))(); separator=", ">),
this.newWith(<["1", "2", "3", "4", "5"]:(literal.(type))(); separator=", ">));

this.assertUnion(
this.newWith(<["1", "2", "3", "6"]:(literal.(type))(); separator=", ">),
this.newWith(<["3", "4", "5"]:(literal.(type))(); separator=", ">),
this.newWith(<["1", "2", "3", "4", "5", "6"]:(literal.(type))(); separator=", ">));

this.assertUnion(
this.newWith(<["1", "2", "3"]:(literal.(type))(); separator=", ">),
this.newWith(<["3", "4", "5" , "6"]:(literal.(type))(); separator=", ">),
this.newWith(<["1", "2", "3", "4", "5", "6"]:(literal.(type))(); separator=", ">));

this.assertUnion(
this.newWith(),
this.newWith(),
this.newWith());

this.assertUnion(
this.newWith(),
this.newWith(<["3", "4", "5"]:(literal.(type))(); separator=", ">),
this.newWith(<["3", "4", "5"]:(literal.(type))(); separator=", ">));

this.assertUnion(
this.newWith(<["1", "2", "3"]:(literal.(type))(); separator=", ">),
this.newWith(),
this.newWith(<["1", "2", "3"]:(literal.(type))(); separator=", ">));
}

private void assertUnion(Immutable<name>Set set1, Immutable<name>Set set2, Immutable<name>Set expected)
{
Immutable<name>Set actual = set1.union(set2);
Assert.assertEquals(expected, actual);
}
}

>>
@@ -1,4 +1,4 @@
import "copyright.stg"
import "copyrightAndOthers.stg"
import "primitiveLiteral.stg"

isTest() ::= "true"
Expand All @@ -12,7 +12,7 @@ class(primitive) ::= <<
>>

body(type, wrapperName, name) ::= <<
<copyright()>
<copyrightAndOthers()>

package org.eclipse.collections.impl.set.mutable.primitive;

Expand Down Expand Up @@ -457,6 +457,46 @@ public abstract class Abstract<name>SetTestCase extends AbstractMutable<name>Col
Verify.assertInstanceOf(Unmodifiable<name>Set.class, set.asUnmodifiable());
Assert.assertEquals(new Unmodifiable<name>Set(set), set.asUnmodifiable());
}

@Test
public void union()
{
this.assertUnion(
this.newWith(<["1", "2", "3"]:(literal.(type))(); separator=", ">),
this.newWith(<["3", "4", "5"]:(literal.(type))(); separator=", ">),
this.newWith(<["1", "2", "3", "4", "5"]:(literal.(type))(); separator=", ">));

this.assertUnion(
this.newWith(<["1", "2", "3", "6"]:(literal.(type))(); separator=", ">),
this.newWith(<["3", "4", "5"]:(literal.(type))(); separator=", ">),
this.newWith(<["1", "2", "3", "4", "5", "6"]:(literal.(type))(); separator=", ">));

this.assertUnion(
this.newWith(<["1", "2", "3"]:(literal.(type))(); separator=", ">),
this.newWith(<["3", "4", "5" , "6"]:(literal.(type))(); separator=", ">),
this.newWith(<["1", "2", "3", "4", "5", "6"]:(literal.(type))(); separator=", ">));

this.assertUnion(
this.newWith(),
this.newWith(),
this.newWith());

this.assertUnion(
this.newWith(),
this.newWith(<["3", "4", "5"]:(literal.(type))(); separator=", ">),
this.newWith(<["3", "4", "5"]:(literal.(type))(); separator=", ">));

this.assertUnion(
this.newWith(<["1", "2", "3"]:(literal.(type))(); separator=", ">),
this.newWith(),
this.newWith(<["1", "2", "3"]:(literal.(type))(); separator=", ">));
}

private void assertUnion(Mutable<name>Set set1, Mutable<name>Set set2, Mutable<name>Set expected)
{
Mutable<name>Set actual = set1.union(set2);
Assert.assertEquals(expected, actual);
}
}

>>
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2015 Goldman Sachs.
* Copyright (c) 2020 Goldman Sachs 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.
Expand Down Expand Up @@ -835,4 +835,33 @@ public void asUnmodifiable()
Assert.assertEquals(new UnmodifiableBooleanSet(this.setWithTrue), this.setWithTrue.asUnmodifiable());
Assert.assertEquals(new UnmodifiableBooleanSet(this.setWithTrueFalse), this.setWithTrueFalse.asUnmodifiable());
}

@Test
public void union()
{
MutableBooleanSet set11 = this.newWith(true);
MutableBooleanSet set21 = this.newWith(false);
MutableBooleanSet actual = set11.union(set21);
Assert.assertEquals(this.setWithTrueFalse, actual);

MutableBooleanSet set12 = this.newWith(false);
MutableBooleanSet set22 = this.newWith(false);
MutableBooleanSet actual2 = set12.union(set22);
Assert.assertEquals(this.setWithFalse, actual2);

MutableBooleanSet set13 = this.newWith(true);
MutableBooleanSet set23 = this.newWith(true);
MutableBooleanSet actual3 = set13.union(set23);
Assert.assertEquals(this.setWithTrue, actual3);

MutableBooleanSet set14 = this.setWithTrueFalse;
MutableBooleanSet set24 = this.newWith();
MutableBooleanSet actual4 = set14.union(set24);
Assert.assertEquals(this.setWithTrueFalse, actual4);

MutableBooleanSet set15 = this.newWith();
MutableBooleanSet set25 = this.newWith();
MutableBooleanSet actual5 = set15.union(set25);
Assert.assertEquals(this.emptySet, actual5);
}
}
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2015 Goldman Sachs.
* Copyright (c) 2020 Goldman Sachs 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.
Expand Down Expand Up @@ -388,4 +388,44 @@ public void classIsNonInstantiable()
{
Verify.assertClassNonInstantiable(ByteSets.class);
}

@Test
public void union()
{
this.assertUnion(
this.newWith((byte) 1, (byte) 2, (byte) 3),
this.newWith((byte) 3, (byte) 4, (byte) 5),
this.newWith((byte) 1, (byte) 2, (byte) 3, (byte) 4, (byte) 5));

this.assertUnion(
this.newWith((byte) 1, (byte) 2, (byte) 3, (byte) 6),
this.newWith((byte) 3, (byte) 4, (byte) 5),
this.newWith((byte) 1, (byte) 2, (byte) 3, (byte) 4, (byte) 5, (byte) 6));

this.assertUnion(
this.newWith((byte) 1, (byte) 2, (byte) 3),
this.newWith((byte) 3, (byte) 4, (byte) 5, (byte) 6),
this.newWith((byte) 1, (byte) 2, (byte) 3, (byte) 4, (byte) 5, (byte) 6));

this.assertUnion(
this.newWith(),
this.newWith(),
this.newWith());

this.assertUnion(
this.newWith(),
this.newWith((byte) 3, (byte) 4, (byte) 5),
this.newWith((byte) 3, (byte) 4, (byte) 5));

this.assertUnion(
this.newWith((byte) 1, (byte) 2, (byte) 3),
this.newWith(),
this.newWith((byte) 1, (byte) 2, (byte) 3));
}

private void assertUnion(MutableByteSet set1, MutableByteSet set2, MutableByteSet expected)
{
MutableByteSet actual = set1.union(set2);
Assert.assertEquals(expected, actual);
}
}
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2015 Goldman Sachs.
* Copyright (c) 2020 Goldman Sachs 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.
Expand Down Expand Up @@ -228,4 +228,44 @@ public void toImmutable()
Assert.assertEquals(1, this.newWith((byte) 1).toImmutable().size());
Assert.assertEquals(3, this.newWith((byte) 1, (byte) 2, (byte) 3).toImmutable().size());
}

@Test
public void union()
{
this.assertUnion(
this.newWith((byte) 1, (byte) 2, (byte) 3),
this.newWith((byte) 3, (byte) 4, (byte) 5),
this.newWith((byte) 1, (byte) 2, (byte) 3, (byte) 4, (byte) 5));

this.assertUnion(
this.newWith((byte) 1, (byte) 2, (byte) 3, (byte) 6),
this.newWith((byte) 3, (byte) 4, (byte) 5),
this.newWith((byte) 1, (byte) 2, (byte) 3, (byte) 4, (byte) 5, (byte) 6));

this.assertUnion(
this.newWith((byte) 1, (byte) 2, (byte) 3),
this.newWith((byte) 3, (byte) 4, (byte) 5, (byte) 6),
this.newWith((byte) 1, (byte) 2, (byte) 3, (byte) 4, (byte) 5, (byte) 6));

this.assertUnion(
this.newWith(),
this.newWith(),
this.newWith());

this.assertUnion(
this.newWith(),
this.newWith((byte) 3, (byte) 4, (byte) 5),
this.newWith((byte) 3, (byte) 4, (byte) 5));

this.assertUnion(
this.newWith((byte) 1, (byte) 2, (byte) 3),
this.newWith(),
this.newWith((byte) 1, (byte) 2, (byte) 3));
}

private void assertUnion(ImmutableByteSet set1, ImmutableByteSet set2, ImmutableByteSet expected)
{
ImmutableByteSet actual = set1.union(set2);
Assert.assertEquals(expected, actual);
}
}
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2015 Goldman Sachs.
* Copyright (c) 2020 Goldman Sachs 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.
Expand Down Expand Up @@ -567,4 +567,33 @@ public void newWithoutAll()
this.assertSizeAndContains(collection1, true);
this.assertSizeAndContains(collection2, false);
}

@Test
public void union()
{
ImmutableBooleanSet set11 = this.newWith(true);
ImmutableBooleanSet set21 = this.newWith(false);
ImmutableBooleanSet actual = set11.union(set21);
Assert.assertEquals(this.trueFalseSet, actual);

ImmutableBooleanSet set12 = this.newWith(false);
ImmutableBooleanSet set22 = this.newWith(false);
ImmutableBooleanSet actual2 = set12.union(set22);
Assert.assertEquals(this.falseSet, actual2);

ImmutableBooleanSet set13 = this.newWith(true);
ImmutableBooleanSet set23 = this.newWith(true);
ImmutableBooleanSet actual3 = set13.union(set23);
Assert.assertEquals(this.trueSet, actual3);

ImmutableBooleanSet set14 = this.trueFalseSet;
ImmutableBooleanSet set24 = this.newWith();
ImmutableBooleanSet actual4 = set14.union(set24);
Assert.assertEquals(this.trueFalseSet, actual4);

ImmutableBooleanSet set15 = this.newWith();
ImmutableBooleanSet set25 = this.newWith();
ImmutableBooleanSet actual5 = set15.union(set25);
Assert.assertEquals(this.emptySet, actual5);
}
}

0 comments on commit 3dd6c76

Please sign in to comment.