io.deephaven.api.Strings Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of deephaven-table-api Show documentation
Show all versions of deephaven-table-api Show documentation
The Deephaven table operations API
The newest version!
//
// Copyright (c) 2016-2024 Deephaven Data Labs and Patent Pending
//
package io.deephaven.api;
import io.deephaven.api.agg.Aggregation;
import io.deephaven.api.agg.AggregationDescriptions;
import io.deephaven.api.expression.Expression;
import io.deephaven.api.expression.Function;
import io.deephaven.api.expression.Method;
import io.deephaven.api.filter.Filter;
import io.deephaven.api.filter.FilterAnd;
import io.deephaven.api.filter.FilterComparison;
import io.deephaven.api.filter.FilterIn;
import io.deephaven.api.filter.FilterIsNull;
import io.deephaven.api.filter.FilterNot;
import io.deephaven.api.filter.FilterOr;
import io.deephaven.api.filter.FilterPattern;
import io.deephaven.api.literal.Literal;
import org.apache.commons.text.StringEscapeUtils;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import java.util.function.BiConsumer;
import java.util.stream.Collectors;
/**
* A set of static helpers to turn strongly-typed api arguments into their {@link String} counterparts.
*/
public class Strings {
public static String of(ColumnName columnName) {
return columnName.name();
}
public static String of(ColumnName columnName, boolean inverted) {
return (inverted ? "!" : "") + of(columnName);
}
public static String ofColumnNames(Collection extends ColumnName> columnNames) {
return columnNames.stream().map(Strings::of).collect(Collectors.joining(",", "[", "]"));
}
public static String of(RawString rawString) {
return rawString.value();
}
public static String of(RawString rawString, boolean invert) {
return of(rawString, false, invert);
}
private static String of(RawString rawString, boolean encapsulate, boolean invert) {
final String inner = of(rawString);
if (invert) {
return "!" + encapsulate(inner);
}
if (encapsulate) {
return encapsulate(inner);
}
return inner;
}
public static String of(FilterComparison comparison) {
final String lhs = ofEncapsulated(comparison.lhs());
final String rhs = ofEncapsulated(comparison.rhs());
return String.format("%s %s %s", lhs, comparison.operator().javaOperator(), rhs);
}
public static String of(FilterIn in) {
return in.toString();
}
private static String of(FilterComparison comparison, boolean encapsulate) {
final String inner = of(comparison);
return encapsulate ? encapsulate(inner) : inner;
}
public static String of(FilterIn in, boolean invert) {
final String inner = of(in);
return (invert ? "!" : "") + inner;
}
public static String of(FilterIsNull isNull) {
return String.format("isNull(%s)", of(isNull.expression()));
}
public static String of(FilterIsNull isNull, boolean inverted) {
return (inverted ? "!" : "") + of(isNull);
}
public static String of(FilterOr filterOr) {
return filterOr.filters().stream().map(Strings::ofEncapsulated).collect(Collectors.joining(" || "));
}
private static String of(FilterOr filterOr, boolean encapsulate) {
final String inner = of(filterOr);
return encapsulate ? encapsulate(inner) : inner;
}
public static String of(FilterAnd filterAnd) {
return filterAnd.filters().stream().map(Strings::ofEncapsulated).collect(Collectors.joining(" && "));
}
private static String of(FilterAnd filterAnd, boolean encapsulate) {
final String inner = of(filterAnd);
return encapsulate ? encapsulate(inner) : inner;
}
public static String of(FilterPattern pattern) {
return pattern.toString();
}
public static String of(FilterPattern pattern, boolean invert) {
final String inner = of(pattern);
return (invert ? "!" : "") + inner;
}
public static String ofPairs(Collection extends Pair> pairs) {
return pairs.stream().map(Strings::of).collect(Collectors.joining(",", "[", "]"));
}
public static String of(Pair pair) {
if (pair.input().equals(pair.output())) {
return of(pair.output());
}
return String.format("%s=%s", of(pair.output()), of(pair.input()));
}
public static String of(JoinMatch match) {
if (match.left().equals(match.right())) {
return of(match.left());
}
return String.format("%s==%s", of(match.left()), of(match.right()));
}
public static String ofJoinMatches(Collection extends JoinMatch> matches) {
return matches.stream().map(Strings::of).collect(Collectors.joining(",", "[", "]"));
}
public static String of(JoinAddition addition) {
if (addition.newColumn().equals(addition.existingColumn())) {
return of(addition.newColumn());
}
return String.format("%s=%s", of(addition.newColumn()), of(addition.existingColumn()));
}
public static String ofJoinAdditions(Collection extends JoinAddition> additions) {
return additions.stream().map(Strings::of).collect(Collectors.joining(",", "[", "]"));
}
public static String of(final AsOfJoinMatch joinMatch) {
return joinMatch.leftColumn().name() + joinMatch.joinRule().operatorString() + joinMatch.rightColumn().name();
}
public static String of(final RangeJoinMatch rangeMatch) {
return String.format("%s%s %s %s %s %s%s",
rangeMatch.rangeStartRule() == RangeStartRule.LESS_THAN_OR_EQUAL_ALLOW_PRECEDING ? "<- " : "",
rangeMatch.leftStartColumn().name(),
rangeMatch.rangeStartRule() == RangeStartRule.LESS_THAN ? "<" : "<=",
rangeMatch.rightRangeColumn().name(),
rangeMatch.rangeEndRule() == RangeEndRule.GREATER_THAN ? "<" : "<=",
rangeMatch.leftEndColumn().name(),
rangeMatch.rangeEndRule() == RangeEndRule.GREATER_THAN_OR_EQUAL_ALLOW_FOLLOWING ? " ->" : "");
}
public static String of(final Aggregation aggregation) {
return ofMap(AggregationDescriptions.of(aggregation));
}
public static String ofAggregations(final Collection extends Aggregation> aggregations) {
return ofMap(AggregationDescriptions.of(aggregations));
}
public static String ofMap(final Map map) {
final Iterator> entries = map.entrySet().iterator();
if (!entries.hasNext()) {
return "[]";
}
final BiConsumer> appender =
(sb, e) -> sb.append(e.getKey()).append(" = ").append(e.getValue());
final StringBuilder sb = new StringBuilder();
appender.accept(sb.append('['), entries.next());
while (entries.hasNext()) {
appender.accept(sb.append(", "), entries.next());
}
return sb.append(']').toString();
}
public static String of(Selectable selectable) {
String lhs = of(selectable.newColumn());
if (selectable.newColumn().equals(selectable.expression())) {
return lhs;
}
return String.format("%s=%s", lhs, of(selectable.expression()));
}
public static String of(Expression expression) {
return expression.walk(new ExpressionAdapter(false, false));
}
public static String of(Filter filter) {
return of(filter, false);
}
public static String of(FilterNot> not) {
return of(not.filter(), true);
}
public static String of(Filter filter, boolean invert) {
return filter.walk(new FilterAdapter(false, invert));
}
public static String of(Literal value) {
return value.walk(new LiteralAdapter(false));
}
public static String of(Function function) {
return of(function, false);
}
public static String of(Function function, boolean invert) {
// (, , ..., )
return (invert ? "!" : "") + function.name()
+ function.arguments().stream().map(Strings::of).collect(Collectors.joining(", ", "(", ")"));
}
public static String of(Method method) {
return of(method, false);
}
public static String of(Method method, boolean invert) {
//