Java Code Examples for java.util.stream.Collectors#joining()

The following examples show how to use java.util.stream.Collectors#joining() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: InsertAnalyzer.java    From crate with Apache License 2.0 6 votes vote down vote up
private static void checkSourceAndTargetColsForLengthAndTypesCompatibility(
    List<Reference> targetColumns, List<Symbol> sources) {
    if (targetColumns.size() != sources.size()) {
        Collector<CharSequence, ?, String> commaJoiner = Collectors.joining(", ");
        throw new IllegalArgumentException(String.format(Locale.ENGLISH,
            "Number of target columns (%s) of insert statement doesn't match number of source columns (%s)",
            targetColumns.stream().map(r -> r.column().sqlFqn()).collect(commaJoiner),
            sources.stream().map(Symbol::toString).collect(commaJoiner)));
    }

    for (int i = 0; i < targetColumns.size(); i++) {
        Reference targetCol = targetColumns.get(i);
        Symbol source = sources.get(i);
        DataType<?> targetType = targetCol.valueType();
        if (targetType.id() == DataTypes.UNDEFINED.id() || source.valueType().isConvertableTo(targetType, false)) {
            continue;
        }
        throw new IllegalArgumentException(String.format(Locale.ENGLISH,
            "The type '%s' of the insert source '%s' is not convertible to the type '%s' of target column '%s'",
            source.valueType(),
            source,
            targetType,
            targetCol.column().fqn()
        ));
    }
}
 
Example 2
Source File: ResolverTuple.java    From monsoon with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public String toString() {
    final Collector<CharSequence, ?, String> joiner;
    if (fields.size() == 1)
        joiner = Collectors.joining(", ");
    else
        joiner = Collectors.joining(", ", "(", ")");

    return fields.stream()
            .map(ResolverTuple::fieldString)
            .collect(joiner);
}
 
Example 3
Source File: SimpleBoundNameResolver.java    From monsoon with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public String toString() {
    final Collector<CharSequence, ?, String> nameJoiner;
    if (getWidth() == 1)
        nameJoiner = Collectors.joining(", ");
    else
        nameJoiner = Collectors.joining(", ", "(", ")");

    return names.stream()
            .map(name -> name.mapCombine(
                    String::valueOf,
                    s -> maybeQuoteIdentifier(s).toString()))
            .collect(nameJoiner);
}
 
Example 4
Source File: Text.java    From jig with Apache License 2.0 4 votes vote down vote up
private static Collector<CharSequence, ?, String> collectionCollector() {
    return Collectors.joining(", ", "[", "]");
}
 
Example 5
Source File: BenchJdbcAvroJob.java    From dbeam with Apache License 2.0 4 votes vote down vote up
private String tsvMetrics() {
  final List<String> columns = newArrayList(
      "recordCount", "writeElapsedMs", "msPerMillionRows", "bytesWritten", "KbWritePerSec");
  final Collector<CharSequence, ?, String> tabJoining = Collectors.joining("\t");
  final Stream<String> lines =
      IntStream.range(0, this.metrics.size())
          .mapToObj(i ->
              String.format(
                  "run_%02d  \t%s",
                  i,
                  columns.stream()
                      .map(c -> String.format("% 10d",
                          Optional.of(this.metrics.get(i).get(c)).orElse(0L)))
                      .collect(tabJoining)));
  final List<Stats> stats =
      columns.stream()
          .map(c ->
              Stats.of(
                  (Iterable<Long>)
                      this.metrics.stream().map(m -> Optional.of(m.get(c)).orElse(0L))
                          ::iterator))
          .collect(Collectors.toList());
  final Map<String, Function<Stats, Double>> relevantStats =
      ImmutableMap.of(
          "max     ", Stats::max,
          "mean    ", Stats::mean,
          "min     ", Stats::min,
          "stddev  ", Stats::populationStandardDeviation);
  final Stream<String> statsSummary =
      relevantStats.entrySet().stream()
          .map(e ->
              String.format(
                  "%s\t%s",
                  e.getKey(),
                  stats.stream()
                      .map(e.getValue())
                      .map(v -> String.format("% 10.1f", v))
                      .collect(tabJoining)));
  return String.format("name    \t%12s\n%s",
      String.join("\t", columns),
      Stream.concat(lines, statsSummary)
          .collect(Collectors.joining("\n")));
}
 
Example 6
Source File: DerivedMetricStatement.java    From monsoon with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public StringBuilder configString() {
    final StringBuilder sb = new StringBuilder()
            .append("define ")
            .append(getGroup().configString())
            .append(' ');

    if (!getTags().isEmpty()) {
        final boolean multiLine = getTags().size() != 1;  // Don't do newlines for single tag.
        final Collector<CharSequence, ?, String> joiner;

        if (multiLine)
            joiner = Collectors.joining(",\n", "tag(\n", ") ");
        else
            joiner = Collectors.joining(", ", "tag(", ") ");

        sb.append(getTags().entrySet().stream()
                .map(tag_mapping -> {
                    final String tagIdent = ConfigSupport.maybeQuoteIdentifier(tag_mapping.getKey()).toString();
                    final String tagExpr = tag_mapping.getValue().configString().toString();
                    return (multiLine ? INDENT : "") + tagIdent + " = " + tagExpr;
                })
                .collect(joiner));
    }

    if (getMapping().size() == 1) {
        Map.Entry<NameResolver, TimeSeriesMetricExpression> entry = getMapping().entrySet().stream().findAny().get();
        sb
                .append(entry.getKey().configString())
                .append(" = ")
                .append(entry.getValue().configString())
                .append(";\n");
    } else if (getMapping().isEmpty()) {
        sb.append("{}\n");
    } else {
        sb.append(getMapping().entrySet().stream()
                .map(entry -> SimpleMapEntry.create(entry.getKey().configString().toString(), entry.getValue().configString().toString()))
                .sorted(Comparator.comparing(entry -> entry.getKey()))
                .map(entry -> {
                    final String metricName = entry.getKey();
                    final String metricExpr = entry.getValue();
                    return INDENT + metricName + " = " + metricExpr + ";";
                })
                .collect(Collectors.joining("\n", "{\n", "\n}\n")));
    }
    return sb;
}
 
Example 7
Source File: PathName.java    From packagedrone with Eclipse Public License 1.0 4 votes vote down vote up
private Collector<CharSequence, ?, String> joiner ()
{
    return Collectors.joining ( "/" );
}