Search in sources :

Example 11 with StringJoiner

use of java8.util.StringJoiner in project streamsupport by stefan-zobel.

the class StringJoinerTest method lengthWithCustomEmptyValue.

public void lengthWithCustomEmptyValue() {
    StringJoiner sj = new StringJoiner(DASH, "<", ">").setEmptyValue(EMPTY);
    assertEquals(sj.length(), EMPTY.length());
    sj.add("");
    assertEquals(sj.length(), "<>".length());
    sj.add("");
    assertEquals(sj.length(), "<->".length());
    sj.add(ONE);
    assertEquals(sj.length(), 4 + ONE_LEN);
    assertEquals(sj.toString().length(), sj.length());
    sj.add(TWO);
    assertEquals(sj.length(), 5 + ONE_LEN + TWO_LEN);
    assertEquals(sj.toString().length(), sj.length());
    sj = new StringJoiner("||", "<", "-->");
    assertEquals(sj.length(), 4);
    assertEquals(sj.toString().length(), sj.length());
    sj.add("abcdef");
    assertEquals(sj.length(), 10);
    assertEquals(sj.toString().length(), sj.length());
    sj.add("xyz");
    assertEquals(sj.length(), 15);
    assertEquals(sj.toString().length(), sj.length());
}
Also used : StringJoiner(java8.util.StringJoiner)

Example 12 with StringJoiner

use of java8.util.StringJoiner in project streamsupport by stefan-zobel.

the class CollectorsTest method testJoining.

@Test(dataProvider = "StreamTestData<Integer>", dataProviderClass = StreamTestDataProvider.class)
public void testJoining(String name, TestData.OfRef<Integer> data) throws Exception {
    withData(data).terminal(s -> s.map(Object::toString).collect(Collectors.joining())).expectedResult(join(data, "")).exercise();
    Collector<String, StringBuilder, String> likeJoining = Collectors.of(StringBuilder::new, StringBuilder::append, (sb1, sb2) -> sb1.append(sb2.toString()), StringBuilder::toString);
    withData(data).terminal(s -> s.map(Object::toString).collect(likeJoining)).expectedResult(join(data, "")).exercise();
    withData(data).terminal(s -> s.map(Object::toString).collect(Collectors.joining(","))).expectedResult(join(data, ",")).exercise();
    withData(data).terminal(s -> s.map(Object::toString).collect(Collectors.joining(",", "[", "]"))).expectedResult("[" + join(data, ",") + "]").exercise();
    withData(data).terminal(s -> s.map(Object::toString).collect(StringBuilder::new, StringBuilder::append, StringBuilder::append).toString()).expectedResult(join(data, "")).exercise();
    withData(data).terminal(s -> s.map(Object::toString).collect(() -> new StringJoiner(","), (sj, cs) -> sj.add(cs), (j1, j2) -> j1.merge(j2)).toString()).expectedResult(join(data, ",")).exercise();
    withData(data).terminal(s -> s.map(Object::toString).collect(() -> new StringJoiner(",", "[", "]"), (sj, cs) -> sj.add(cs), (j1, j2) -> j1.merge(j2)).toString()).expectedResult("[" + join(data, ",") + "]").exercise();
}
Also used : StringJoiner(java8.util.StringJoiner) Test(org.testng.annotations.Test)

Example 13 with StringJoiner

use of java8.util.StringJoiner in project streamsupport by stefan-zobel.

the class MergeTest method testEmptyBoth.

public void testEmptyBoth() {
    fixesStream().forEach(fixes -> {
        StringJoiner sj = new StringJoiner(",", fixes.pre0, fixes.suf0);
        StringJoiner other = new StringJoiner(":", fixes.pre1, fixes.suf1);
        sj.merge(other);
        assertEquals(sj.toString(), fixes.pre0 + fixes.suf0);
        other.setEmptyValue("NOTHING");
        sj.merge(other);
        assertEquals(sj.toString(), fixes.pre0 + fixes.suf0);
        sj = new StringJoiner(",", fixes.pre0, fixes.suf0).setEmptyValue("EMPTY");
        assertEquals(sj.toString(), "EMPTY");
        sj.merge(other);
        assertEquals(sj.toString(), "EMPTY");
    });
}
Also used : StringJoiner(java8.util.StringJoiner)

Example 14 with StringJoiner

use of java8.util.StringJoiner in project streamsupport by stefan-zobel.

the class MergeTest method testCascadeEmpty.

public void testCascadeEmpty() {
    fixesStream().forEach(fixes -> {
        StringJoiner sj = new StringJoiner(",", fixes.pre0, fixes.suf0);
        StringJoiner o1 = new StringJoiner(":", fixes.pre1, fixes.suf1).setEmptyValue("Empty1");
        StringJoiner o2 = new StringJoiner(",", "<", ">").setEmptyValue("Empty2");
        o1.merge(o2);
        assertEquals(o1.toString(), "Empty1");
        sj.merge(o1);
        assertEquals(sj.toString(), fixes.pre0 + fixes.suf0);
    });
}
Also used : StringJoiner(java8.util.StringJoiner)

Example 15 with StringJoiner

use of java8.util.StringJoiner in project streamsupport by stefan-zobel.

the class MergeTest method testSimple.

public void testSimple() {
    fixesStream().forEach(fixes -> {
        StringJoiner sj = new StringJoiner(",", fixes.pre0, fixes.suf0);
        StringJoiner other = new StringJoiner(",", fixes.pre1, fixes.suf1);
        RefStreams.of("a", "b", "c").forEachOrdered(sj::add);
        RefStreams.of("d", "e", "f").forEachOrdered(other::add);
        sj.merge(other);
        assertEquals(sj.toString(), fixes.pre0 + "a,b,c,d,e,f" + fixes.suf0);
    });
}
Also used : StringJoiner(java8.util.StringJoiner)

Aggregations

StringJoiner (java8.util.StringJoiner)22 ArrayList (java.util.ArrayList)3 Test (org.testng.annotations.Test)2