Search in sources :

Example 6 with StringJoiner

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

the class StringJoinerTest method addCharSequenceWithEmptyValue.

public void addCharSequenceWithEmptyValue() {
    StringJoiner sj = new StringJoiner(",").setEmptyValue(EMPTY);
    CharSequence cs_one = ONE;
    CharSequence cs_two = TWO;
    sj.add(cs_one);
    sj.add(cs_two);
    assertEquals(sj.toString(), ONE + "," + TWO);
    sj = new StringJoiner(DASH, "{", "}");
    sj.add(cs_one);
    sj.add(cs_two);
    assertEquals(sj.toString(), "{" + ONE + DASH + TWO + "}");
    sj = new StringJoiner(DASH, "{", "}");
    assertEquals(sj.toString(), "{}");
    sj = new StringJoiner("=", "{", "}").setEmptyValue("");
    assertEquals(sj.toString(), "");
    sj = new StringJoiner(DASH, "{", "}").setEmptyValue(EMPTY);
    assertEquals(sj.toString(), EMPTY);
    sj.add(cs_one);
    sj.add(cs_two);
    assertEquals(sj.toString(), "{" + ONE + DASH + TWO + "}");
}
Also used : StringJoiner(java8.util.StringJoiner)

Example 7 with StringJoiner

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

the class StringJoinerTest method testCombos.

private void testCombos(String infix, String prefix, String suffix) {
    StringJoiner sj = new StringJoiner(infix, prefix, suffix);
    assertEquals(sj.toString(), prefix + suffix);
    assertEquals(sj.toString().length(), sj.length());
    // EmptyValue
    sj = new StringJoiner(infix, prefix, suffix).setEmptyValue("<NONE>");
    assertEquals(sj.toString(), "<NONE>");
    assertEquals(sj.toString().length(), sj.length());
    // empty in front
    sj.add("");
    assertEquals(sj.toString(), prefix + suffix);
    // empty in middle
    sj.add("");
    assertEquals(sj.toString(), prefix + infix + suffix);
    sj.add("1");
    assertEquals(sj.toString(), prefix + infix + infix + "1" + suffix);
    // empty at end
    sj.add("");
    assertEquals(sj.toString(), prefix + infix + infix + "1" + infix + suffix);
    sj = new StringJoiner(infix, prefix, suffix).setEmptyValue("<NONE>");
    sj.add("1");
    assertEquals(sj.toString(), prefix + "1" + suffix);
    sj.add("2");
    assertEquals(sj.toString(), prefix + "1" + infix + "2" + suffix);
    sj.add("");
    assertEquals(sj.toString(), prefix + "1" + infix + "2" + infix + suffix);
    sj.add("3");
    assertEquals(sj.toString(), prefix + "1" + infix + "2" + infix + infix + "3" + suffix);
}
Also used : StringJoiner(java8.util.StringJoiner)

Example 8 with StringJoiner

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

the class StringJoinerTest method stringFromtoStringWithEmptyValue.

public void stringFromtoStringWithEmptyValue() {
    StringJoiner sj = new StringJoiner(" ", "", "");
    assertEquals(sj.toString(), "");
    sj = new StringJoiner(", ");
    assertEquals(sj.toString(), "");
    sj = new StringJoiner(",", "{", "}");
    assertEquals(sj.toString(), "{}");
    sj = new StringJoiner(",", "{", "}").setEmptyValue("");
    assertEquals(sj.toString(), "");
    sj = new StringJoiner(",");
    sj.add(ONE);
    assertEquals(sj.toString(), ONE);
    sj.add(TWO);
    assertEquals(sj.toString(), ONE + "," + TWO);
    sj = new StringJoiner(",", "{--", "--}");
    sj.add(ONE);
    assertEquals(sj.toString(), "{--" + ONE + "--}");
    sj.add(TWO);
    assertEquals(sj.toString(), "{--" + ONE + "," + TWO + "--}");
}
Also used : StringJoiner(java8.util.StringJoiner)

Example 9 with StringJoiner

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

the class StringJoinerTest method addAddAll.

public void addAddAll() {
    final StringJoiner sj = new StringJoiner(DASH, "{", "}");
    sj.add(ONE);
    ArrayList<String> nextOne = new ArrayList<String>();
    nextOne.add(TWO);
    nextOne.add(THREE);
    Stream<String> stream = StreamSupport.stream(nextOne);
    stream.forEachOrdered(new java8.util.function.Consumer<String>() {

        @Override
        public void accept(String t) {
            sj.add(t);
        }
    });
    String expected = "{" + ONE + DASH + TWO + DASH + THREE + "}";
    assertEquals(sj.toString(), expected);
}
Also used : ArrayList(java.util.ArrayList) StringJoiner(java8.util.StringJoiner)

Example 10 with StringJoiner

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

the class StringJoinerTest method addAlladd.

void addAlladd() {
    final StringJoiner sj = new StringJoiner(DASH, "{", "}");
    ArrayList<String> firstOne = new ArrayList<String>();
    firstOne.add(ONE);
    firstOne.add(TWO);
    Stream<String> stream = StreamSupport.stream(firstOne);
    stream.forEachOrdered(new java8.util.function.Consumer<String>() {

        @Override
        public void accept(String t) {
            sj.add(t);
        }
    });
    sj.add(THREE);
    String expected = "{" + ONE + DASH + TWO + DASH + THREE + "}";
    assertEquals(sj.toString(), expected);
}
Also used : ArrayList(java.util.ArrayList) StringJoiner(java8.util.StringJoiner)

Aggregations

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