Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#3951 - Joiner implements Collector #5287

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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<>();
}
}