Skip to content

Commit

Permalink
Add fused method for collect + makeString
Browse files Browse the repository at this point in the history
  • Loading branch information
Desislav-Petrov committed Jan 15, 2022
1 parent b13a6bc commit 6fddda8
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 0 deletions.
Expand Up @@ -248,6 +248,11 @@ default String makeString(String start, String separator, String end)
return stringBuilder.toString();
}

default String makeString(Function<? super T, Object> function, String start, String separator, String end)
{
return this.collect(function).makeString(start, separator, end);
}

default void appendString(Appendable appendable)
{
this.appendString(appendable, ", ");
Expand Down
Expand Up @@ -2298,6 +2298,17 @@ default String makeString(String start, String separator, String end)
return stringBuilder.toString();
}

/**
* Returns a string representation of the collection, created by applying the function supplied to each element,
* with the elements separated by the specified separator and enclosed between the start and end strings.
*
* @return a string representation of the mapped collection
*/
default String makeString(Function<? super T, Object> function, String start, String separator, String end)
{
return this.asLazy().collect(function).makeString(start, separator, end);
}

/**
* Prints a string representation of this collection onto the given {@code Appendable}. Prints the string returned
* by {@link #makeString()}.
Expand Down
Expand Up @@ -1231,6 +1231,15 @@ public String makeString(String start, String separator, String end)
}
}

@Override
public String makeString(Function<? super T, Object> function, String start, String separator, String end)
{
synchronized (this.lock)
{
return this.delegate.makeString(function, start, separator, end);
}
}

@Override
public void appendString(Appendable appendable)
{
Expand Down
Expand Up @@ -1371,6 +1371,15 @@ public String makeString(String start, String separator, String end)
}
}

@Override
public String makeString(Function<? super T, Object> function, String start, String separator, String end)
{
try (LockWrapper wrapper = this.lockWrapper.acquireReadLock())
{
return this.getDelegate().makeString(function, start, separator, end);
}
}

@Override
public void appendString(Appendable appendable)
{
Expand Down
Expand Up @@ -680,6 +680,20 @@ public String makeString(String start, String separator, String end)
}
}

@Override
public String makeString(Function<? super T, Object> function, String start, String separator, String end)
{
this.lock.readLock().lock();
try
{
return this.delegate.makeString(function, start, separator, end);
}
finally
{
this.lock.readLock().unlock();
}
}

@Override
public void appendString(Appendable appendable)
{
Expand Down
Expand Up @@ -1997,6 +1997,16 @@ default void RichIterable_injectInto_primitive()
Assert.assertEquals(30.0f, iterable.injectInto(0, AddFunction.INTEGER_TO_FLOAT), 0.001f);
}

@Test
default void RichIterable_fused_collectMakeString()
{
RichIterable<Integer> iterable = this.newWith(0, 1, 8);

assertEquals(
iterable.asLazy().collect(Integer::toUnsignedString).makeString("[", ",", "]"),
iterable.makeString(Integer::toUnsignedString, "[", ",", "]"));
}

@Test
default void RichIterable_makeString_appendString()
{
Expand Down
Expand Up @@ -94,6 +94,12 @@ public void RichIterable_getLast_empty_null()
// Not applicable
}

@Override
public void RichIterable_fused_collectMakeString()
{
// Not applicable
}

@Override
public void MutableMapIterable_updateValue()
{
Expand Down
Expand Up @@ -95,6 +95,12 @@ public void RichIterable_getLast_empty_null()
// Not applicable
}

@Override
public void RichIterable_fused_collectMakeString()
{
// Not applicable
}

@Override
public void RichIterable_anySatisfy_allSatisfy_noneSatisfy()
{
Expand Down
Expand Up @@ -1841,6 +1841,15 @@ public void makeStringWithSeparatorAndStartAndEnd()
Assert.assertEquals(collection.toString(), collection.makeString("[", ", ", "]"));
}

@Test
public void fusedCollectMakeString()
{
RichIterable<Integer> collection = this.newWith(1, 2, 3);
Assert.assertEquals(
collection.asLazy().collect(Integer::toUnsignedString).makeString("[", ", ", "]"),
collection.makeString(Integer::toUnsignedString, "[", ", ", "]"));
}

@Test
public void appendString()
{
Expand Down

0 comments on commit 6fddda8

Please sign in to comment.