com.sap.cds.impl.sql.SpaceSeparatedCollector Maven / Gradle / Ivy
/*******************************************************************
* © 2019 SAP SE or an SAP affiliate company. All rights reserved. *
*******************************************************************/
package com.sap.cds.impl.sql;
import java.util.function.BiConsumer;
import java.util.function.BinaryOperator;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collector;
public class SpaceSeparatedCollector {
private SpaceSeparatedCollector() {
}
public static Collector joining() {
Supplier supplier = StringBuilder::new;
BiConsumer accumulator = (sb, s) -> {
if (sb.length() > 0) {
if (!",".equals(s) && !")".equals(s) && !endsWithParenthesis(sb)) {
sb.append(" ");
}
}
sb.append(s);
};
BinaryOperator combiner = StringBuilder::append;
Function finisher = StringBuilder::toString;
return Collector.of(supplier, accumulator, combiner, finisher);
}
private static boolean endsWithParenthesis(StringBuilder sb) {
return sb.charAt(sb.length() - 1) == '(';
}
}