下面列出了java.util.stream.Collectors#joining ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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()
));
}
}
@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);
}
@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);
}
private static Collector<CharSequence, ?, String> collectionCollector() {
return Collectors.joining(", ", "[", "]");
}
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")));
}
@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;
}
private Collector<CharSequence, ?, String> joiner ()
{
return Collectors.joining ( "/" );
}