Example usage for java.util StringJoiner StringJoiner

List of usage examples for java.util StringJoiner StringJoiner

Introduction

In this page you can find the example usage for java.util StringJoiner StringJoiner.

Prototype

public StringJoiner(CharSequence delimiter) 

Source Link

Document

Constructs a StringJoiner with no characters in it, with no prefix or suffix , and a copy of the supplied delimiter .

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    List<Person> persons = Arrays.asList(new Person("Max", 18), new Person("Peter", 23),
            new Person("Pamela", 23), new Person("David", 12));

    Collector<Person, StringJoiner, String> personNameCollector = Collector.of(() -> new StringJoiner(" | "), // supplier
            (j, p) -> j.add(p.name.toUpperCase()), // accumulator
            (j1, j2) -> j1.merge(j2), // combiner
            StringJoiner::toString); // finisher

    String names = persons.stream().collect(personNameCollector);

    System.out.println(names);//from   w  ww  .  j a  v a 2  s.  c o  m
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    List<Person> persons = Arrays.asList(new Person("Max", 18), new Person("Peter", 23),
            new Person("Pamela", 23), new Person("David", 12));

    Collector<Person, StringJoiner, String> personNameCollector = Collector.of(() -> {
        System.out.println("supplier");
        return new StringJoiner(" | ");
    }, (j, p) -> {/*from ww  w. j  a va 2  s .  c om*/
        System.out.format("accumulator: p=%s; j=%s\n", p, j);
        j.add(p.name.toUpperCase());
    }, (j1, j2) -> {
        System.out.println("merge");
        return j1.merge(j2);
    }, j -> {
        System.out.println("finisher");
        return j.toString();
    });

    String names = persons.stream().collect(personNameCollector);

    System.out.println(names);
}

From source file:Main.java

public static String getDeepNodeValue(Node node) {
    StringJoiner out = new StringJoiner(" ");

    if (node.hasChildNodes()) {
        for (int i = 0; i < node.getChildNodes().getLength(); i++) {
            String val = getDeepNodeValue(node.getChildNodes().item(i));
            if (val != null && val.trim().length() != 0)
                out.add(val.trim());
        }// ww  w  .  jav  a  2s . c  o  m
    } else if (node.getNodeValue() != null && node.getNodeValue().trim().length() != 0) {
        out.add(node.getNodeValue().trim());
    }

    return out.toString();
}

From source file:pcgen.core.display.VisionDisplay.java

@NotNull
public static String getVision(final PlayerCharacter aPC, CDOMObject cdo) {
    if (aPC == null) {
        return "";
    }//  w  ww.  j a v  a 2 s.  c  om
    Collection<CDOMReference<Vision>> mods = CollectionUtils.emptyIfNull(cdo.getListMods(Vision.VISIONLIST));

    StringJoiner visionString = new StringJoiner(";");
    mods.stream().flatMap(ref -> ref.getContainedObjects().stream()).map(v -> v.toString(aPC))
            .forEach(visionString::add);

    return visionString.toString();
}

From source file:org.trellisldp.rosid.file.FileUtils.java

/**
 * Partition an identifier into a directory structure
 * @param identifier the identifier// w  w  w  .  j  a  v a 2s . c o m
 * @return a string usable as a directory path
 */
public static String partition(final String identifier) {
    requireNonNull(identifier, "identifier must not be null!");

    final StringJoiner joiner = new StringJoiner(separator);
    final CRC32 hasher = new CRC32();
    hasher.update(identifier.getBytes(UTF_8));
    final String intermediate = Long.toHexString(hasher.getValue());

    range(0, intermediate.length() / LENGTH).limit(MAX)
            .forEach(i -> joiner.add(intermediate.substring(i * LENGTH, (i + 1) * LENGTH)));

    joiner.add(md5Hex(identifier));
    return joiner.toString();
}

From source file:uk.nhs.fhir.load.FileLoader.java

/**
  * @param file File to load content from
  * @return String containing content of specified file
  *//*ww w . jav a2 s. c o  m*/
public static String loadFile(final File file) {
    LOG.debug("Loading file: " + file.getAbsolutePath());

    String charsetName;
    try {
        charsetName = getCharset(file);
    } catch (IOException e) {
        throw new IllegalStateException("Unable to determine appropriate charset for file " + file.getName(),
                e);
    }

    LOG.debug("Loading file using encoding: " + charsetName);

    try (ByteArrayOutputStream bOutStream = new ByteArrayOutputStream();
            InputStream fis = new FileInputStream(file);
            InputStream bomInputStream = new BOMInputStream(fis);
            Reader streamReader = new InputStreamReader(bomInputStream, charsetName);
            BufferedReader in = new BufferedReader(streamReader);) {

        StringJoiner sj = new StringJoiner("\n");

        String str;
        while ((str = in.readLine()) != null) {
            sj.add(str);
        }

        return sj.toString();
    } catch (IOException e) {
        throw new IllegalStateException("Error reading file: " + file.getName(), e);
    }
}

From source file:com.streamsets.pipeline.stage.origin.spooldir.TestOffsetUtil.java

private static Map<String, String> removeAbsolutePaths(Map<String, String> offsetV2) {
    Map<String, String> resultMap = new HashMap<>();
    for (Map.Entry<String, String> entry : offsetV2.entrySet()) {
        if (entry.getKey().contains(PATH_SEPARATOR)) {
            String filename = new StringJoiner(".").add(FilenameUtils.getBaseName(entry.getKey()))
                    .add(FilenameUtils.getExtension(entry.getKey())).toString();
            resultMap.put(filename, entry.getValue());
        } else {/* www . ja  va  2 s  .c o  m*/
            resultMap.put(entry.getKey(), entry.getValue());
        }
    }
    return resultMap;
}

From source file:io.mapzone.arena.share.content.ArenaContentProvider.java

@Override
public ArenaContent get() {
    StringJoiner layers = new StringJoiner(",");
    for (SelectionDescriptor selection : context.selectionDescriptors.get()) {
        layers.add(selection.layer.get().label.get());
        // FIXME, if multilayers are working, remove this break!!!
        break;//  ww w . j a va2 s. co  m
    }

    Envelope envelope = context.boundingBox.get();
    StringJoiner extent = new StringJoiner(",");
    extent.add(String.valueOf((int) envelope.getMinX()));
    extent.add(String.valueOf((int) envelope.getMinY()));
    extent.add(String.valueOf((int) envelope.getMaxX()));
    extent.add(String.valueOf((int) envelope.getMaxY()));

    StringBuffer shareInfo = new StringBuffer(ArenaPlugin.instance().config().getProxyUrl());
    shareInfo.append(ShareServletsStarter.ALIAS_SHAREINFO).append("?");
    try {
        shareInfo.append(ShareInfoServlet.PARAMETER_LAYERS).append("=")
                .append(URLEncoder.encode(layers.toString(), "utf-8"));
        shareInfo.append("&").append(ShareInfoServlet.PARAMETER_BBOX).append("=")
                .append(URLEncoder.encode(extent.toString(), "utf-8"));
        if (!StringUtils.isBlank(ArenaPlugin.instance().config().getServiceAuthToken())) {
            shareInfo.append("&").append(ShareInfoServlet.PARAMETER_AUTHTOKEN).append("=")
                    .append(URLEncoder.encode(ArenaPlugin.instance().config().getServiceAuthToken(), "utf-8"));
        }
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException(e);
    }

    ArenaContent content = new ArenaContent();
    content.arena = ArenaPlugin.instance().config().getProxyUrl() + ArenaPlugin.ALIAS;
    content.shareInfo = shareInfo.toString();
    return content;
}

From source file:com.github.robozonky.integrations.zonkoid.ZonkoidConfirmationProvider.java

static String getAuthenticationString(final RequestId requestId, final int loanId) {
    final String auth = new StringJoiner("|").add(String.valueOf(requestId.getPassword())).add(CLIENT_APP)
            .add(requestId.getUserId()).add(String.valueOf(loanId)).toString();
    try {/*from  w  w  w. j  a v  a  2s .c  o m*/
        return md5(auth);
    } catch (final NoSuchAlgorithmException ex) {
        throw new IllegalStateException("Your Java Runtime Environment does not support MD5!", ex);
    }
}

From source file:info.archinnov.achilles.internals.strategy.naming.InternalNamingStrategy.java

default String toSnakeCase(String name) {
    final String[] tokens = splitByCharacterTypeCamelCase(name);
    final StringJoiner joiner = new StringJoiner("_");
    asList(tokens).stream().filter(x -> x != null).map(x -> x.toLowerCase()).forEach(joiner::add);

    return joiner.toString().replaceAll("_+", "_");
}