Skip to content

Commit

Permalink
Merge pull request eclipse#437 from donraab/master
Browse files Browse the repository at this point in the history
Implement toStringOfItemToCount on primitive Bags.
  • Loading branch information
nikhilnanivadekar committed Jan 19, 2018
2 parents 17ec1c9 + 39c8b1f commit 5d67a88
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ body(type, name) ::= <<

package org.eclipse.collections.api.bag.primitive;

import java.util.StringJoiner;

import org.eclipse.collections.api.<name>Iterable;
import org.eclipse.collections.api.bag.Bag;
import org.eclipse.collections.api.block.function.primitive.<name>ToObjectFunction;
Expand Down Expand Up @@ -104,6 +106,16 @@ public interface <name>Bag extends <name>Iterable
* Returns an immutable copy of this bag. If the bag is immutable, it returns itself.
*/
Immutable<name>Bag toImmutable();

/**
* @since 9.2
*/
default String toStringOfItemToCount()
{
StringJoiner joiner = new StringJoiner(", ", "{", "}");
this.forEachWithOccurrences((each, occurrences) -> joiner.add(each + "=" + occurrences));
return joiner.toString();
}
}

>>
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import "copyright.stg"
import "copyrightAndOthers.stg"
import "primitiveEquals.stg"
import "primitiveHashCode.stg"
import "primitiveLiteral.stg"
Expand All @@ -15,7 +15,7 @@ class(primitive) ::= <<
>>

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

package org.eclipse.collections.impl.bag.immutable.primitive;

Expand Down Expand Up @@ -276,6 +276,16 @@ public abstract class AbstractImmutable<name>BagTestCase extends AbstractImmutab
Immutable<name>Bag expected = this.classUnderTest();
Assert.assertSame(expected, expected.toImmutable());
}

@Test
public void toStringOfItemToCount()
{
Immutable<name>Bag empty = this.newWith();
Assert.assertEquals("{}", empty.toStringOfItemToCount());
Assert.assertEquals("{" + <(literal.(type))("100")> + "=3}", this.newWith(<["100", "100", "100"]:(literal.(type))(); separator=", ">).toStringOfItemToCount());
String actual = this.newWith(<["100", "101", "101"]:(literal.(type))(); separator=", ">).toStringOfItemToCount();
Assert.assertTrue(("{" + <(literal.(type))("100")> + "=1, " + <(literal.(type))("101")> + "=2}").equals(actual) || ("{" + <(literal.(type))("101")> + "=2, " + <(literal.(type))("100")> + "=1}").equals(actual));
}
}

>>
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import "copyright.stg"
import "copyrightAndOthers.stg"
import "primitiveEquals.stg"
import "primitiveHashCode.stg"
import "primitiveLiteral.stg"
Expand All @@ -14,7 +14,7 @@ class(primitive) ::= <<
>>

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

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

Expand Down Expand Up @@ -374,6 +374,16 @@ public abstract class AbstractMutable<name>BagTestCase extends AbstractMutable<n
Assert.assertNotSame(this.classUnderTest(), this.classUnderTest().toImmutable());
Verify.assertInstanceOf(Immutable<name>Bag.class, this.classUnderTest().toImmutable());
}

@Test
public void toStringOfItemToCount()
{
Mutable<name>Bag empty = this.newWith();
Assert.assertEquals("{}", empty.toStringOfItemToCount());
Assert.assertEquals("{" + <(literal.(type))("100")> + "=3}", this.newWith(<["100", "100", "100"]:(literal.(type))(); separator=", ">).toStringOfItemToCount());
String actual = this.newWith(<["100", "101", "101"]:(literal.(type))(); separator=", ">).toStringOfItemToCount();
Assert.assertTrue(("{" + <(literal.(type))("100")> + "=1, " + <(literal.(type))("101")> + "=2}").equals(actual) || ("{" + <(literal.(type))("101")> + "=2, " + <(literal.(type))("100")> + "=1}").equals(actual));
}
}

>>
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2016 Goldman Sachs.
* Copyright (c) 2018 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 All @@ -13,6 +13,7 @@
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.StringJoiner;

import org.eclipse.collections.api.bag.Bag;
import org.eclipse.collections.api.bag.MutableBag;
Expand Down Expand Up @@ -643,20 +644,9 @@ public <IV, P> IV injectIntoWith(IV injectedValue, Function3<? super IV, ? super
@Override
public String toStringOfItemToCount()
{
if (this.isEmpty())
{
return "{}";
}
StringBuilder builder = new StringBuilder().append('{');
this.forEachWithOccurrences((each, occurrences) -> {
builder.append(each);
builder.append('=');
builder.append(occurrences);
builder.append(", ");
});
builder.deleteCharAt(builder.length() - 1);
builder.deleteCharAt(builder.length() - 1);
return builder.append('}').toString();
StringJoiner joiner = new StringJoiner(", ", "{", "}");
this.forEachWithOccurrences((each, occurrences) -> joiner.add(each + "=" + occurrences));
return joiner.toString();
}

protected MutableList<ObjectIntPair<T>> toListWithOccurrences()
Expand Down

0 comments on commit 5d67a88

Please sign in to comment.