Skip to content

Commit

Permalink
google#3951 - Joiner implements Collector
Browse files Browse the repository at this point in the history
  • Loading branch information
Saucistophe committed Oct 17, 2020
1 parent 33f2b15 commit 7b5ce05
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 12 deletions.
14 changes: 10 additions & 4 deletions guava-tests/test/com/google/common/base/JoinerTest.java
Expand Up @@ -26,11 +26,10 @@
import com.google.common.collect.Maps;
import com.google.common.testing.NullPointerTester;
import java.io.IOException;
import java.util.Arrays;
import java.util.Iterator;
import java.util.Map;
import java.util.*;
import java.util.Map.Entry;
import java.util.Set;
import java.util.stream.Collectors;

import junit.framework.AssertionFailedError;
import junit.framework.TestCase;

Expand Down Expand Up @@ -307,6 +306,13 @@ public void test_skipNulls_onMap() {
}
}

public void testCollector() {
Joiner onChar = Joiner.on(' ');
List<String> input = Lists.newArrayList("This","works","as","a","stream","collector");
String result = input.stream().collect(onChar);
assertEquals("This works as a stream collector",result);
}

private static class DontStringMeBro implements CharSequence {
@Override
public int length() {
Expand Down
54 changes: 46 additions & 8 deletions guava/src/com/google/common/base/Joiner.java
Expand Up @@ -19,12 +19,16 @@
import com.google.common.annotations.Beta;
import com.google.common.annotations.GwtCompatible;
import com.google.errorprone.annotations.CanIgnoreReturnValue;

import java.io.IOException;
import java.util.AbstractList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.Map;
import java.util.*;
import java.util.Map.Entry;
import java.util.function.BiConsumer;
import java.util.function.BinaryOperator;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collector;

import org.checkerframework.checker.nullness.qual.Nullable;

/**
Expand Down Expand Up @@ -63,13 +67,17 @@
* @since 2.0
*/
@GwtCompatible
public class Joiner {
/** Returns a joiner which automatically places {@code separator} between consecutive elements. */
public class Joiner implements Collector<Object, List<Object>, String> {
/**
* Returns a joiner which automatically places {@code separator} between consecutive elements.
*/
public static Joiner on(String separator) {
return new Joiner(separator);
}

/** Returns a joiner which automatically places {@code separator} between consecutive elements. */
/**
* Returns a joiner which automatically places {@code separator} between consecutive elements.
*/
public static Joiner on(char separator) {
return new Joiner(String.valueOf(separator));
}
Expand Down Expand Up @@ -121,7 +129,9 @@ public final <A extends Appendable> A appendTo(A appendable, Object[] parts) thr
return appendTo(appendable, Arrays.asList(parts));
}

/** Appends to {@code appendable} the string representation of each of the remaining arguments. */
/**
* Appends to {@code appendable} the string representation of each of the remaining arguments.
*/
@CanIgnoreReturnValue
public final <A extends Appendable> A appendTo(
A appendable, @Nullable Object first, @Nullable Object second, Object... rest)
Expand Down Expand Up @@ -475,4 +485,32 @@ public Object get(int index) {
}
};
}

@Override
public Supplier<List<Object>> supplier() {
return ArrayList::new;
}

@Override
public BiConsumer<List<Object>, Object> accumulator() {
return List::add;
}

@Override
public BinaryOperator<List<Object>> combiner() {
return (l1, l2) -> {
l1.addAll(l2);
return l1;
};
}

@Override
public Function<List<Object>, String> finisher() {
return this::join;
}

@Override
public Set<Characteristics> characteristics() {
return new HashSet<>();
}
}

0 comments on commit 7b5ce05

Please sign in to comment.