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

io.polyglotted.pgmodel.search.query.Expression Maven / Gradle / Ivy

Go to download

Standard set of models for geo coding, access control and elastic search abstraction

The newest version!
package io.polyglotted.pgmodel.search.query;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import lombok.RequiredArgsConstructor;

import java.util.List;
import java.util.Objects;

import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Strings.isNullOrEmpty;
import static io.polyglotted.pgmodel.util.ModelUtil.jsonEquals;

@RequiredArgsConstructor
public final class Expression {
    public static final Expression NilExpression = new Expression("_nil_", "_nil_", ImmutableMap.of(), ImmutableList.of());
    public static final String ValueKey = "_val";
    public final String operation;
    public final String label;
    public final ImmutableMap args;
    public final ImmutableList children;

    public static Expression withMap(String operation, String label, java.util.Map args) {
        return new Expression(checkNotNull(operation), checkNotNull(label), ImmutableMap.copyOf(args), ImmutableList.of());
    }

    public static Expression withValue(ExpressionType expressionType, String label, Object valueArg) {
        return new Expression(expressionType.name(), checkNotNull(label), ImmutableMap.of(ValueKey, valueArg), ImmutableList.of());
    }

    public static > Expression withArray(ExpressionType expressionType, String label, List valueArg) {
        return new Expression(expressionType.name(), checkNotNull(label), ImmutableMap.of(ValueKey, valueArg), ImmutableList.of());
    }

    public static Expression withOnlyChildren(ExpressionType expressionType, String label, Iterable list) {
        return new Expression(expressionType.name(), checkNotNull(label), ImmutableMap.of(), ImmutableList.copyOf(list));
    }

    public static Expression withLabel(ExpressionType expressionType, String label) {
        return new Expression(expressionType.name(), checkNotNull(label), ImmutableMap.of(), ImmutableList.of());
    }

    public  T valueArg() {
        return argFor(ValueKey, null);
    }

    public List arrayArg() {
        return argFor(ValueKey, ImmutableList.of());
    }

    public String stringArg() {
        return stringArg(ValueKey);
    }

    public String stringArg(String key) {
        return argFor(key, null);
    }

    public boolean boolArg(String key) {
        return argFor(key, Boolean.FALSE);
    }

    public int intArg(String key, int defValue) {
        return argFor(key, defValue);
    }

    public  T argFor(String key) {
        return checkNotNull(argFor(key, null));
    }

    @SuppressWarnings("unchecked")
    private  T argFor(String key, T defValue) {
        return args.containsKey(key) ? (T) args.get(key) : defValue;
    }

    @Override
    public boolean equals(Object o) {
        return jsonEquals(this, o);
    }

    @Override
    public int hashCode() {
        return Objects.hash(operation, label, args, children);
    }

    @Override
    public String toString() {
        return (isNullOrEmpty(label) ? "" : label + " ") + operation + (args.isEmpty() ? "" : " " + args)
           + (children.isEmpty() ? "" : " " + children);
    }
}