All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.sap.cds.impl.sql.SpaceSeparatedCollector Maven / Gradle / Ivy

There is a newer version: 3.4.0
Show newest version
/*******************************************************************
 * © 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) == '(';
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy