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

tech.ydb.yoj.repository.ydb.yql.YqlPredicateParam Maven / Gradle / Ivy

The newest version!
package tech.ydb.yoj.repository.ydb.yql;

import com.google.common.base.Preconditions;
import lombok.RequiredArgsConstructor;
import lombok.Value;
import tech.ydb.yoj.repository.ydb.statement.PredicateStatement;

import java.util.Collection;
import java.util.stream.Stream;

import static java.util.Arrays.stream;
import static java.util.stream.Collectors.toList;
import static java.util.stream.Stream.concat;
import static lombok.AccessLevel.PRIVATE;

@Value
@RequiredArgsConstructor(access = PRIVATE)
public class YqlPredicateParam {
    String fieldPath;
    T value;
    boolean optional;
    PredicateStatement.ComplexField complexField;
    PredicateStatement.CollectionKind collectionKind;

    /**
     * @return required predicate parameter
     */
    public static  YqlPredicateParam of(String fieldPath, T value) {
        return of(fieldPath, value, false, PredicateStatement.ComplexField.FLATTEN, PredicateStatement.CollectionKind.SINGLE);
    }

    /**
     * @return optional predicate parameter
     */
    public static  YqlPredicateParam optionalOf(String fieldPath, T value) {
        return of(fieldPath, value, true, PredicateStatement.ComplexField.FLATTEN, PredicateStatement.CollectionKind.SINGLE);
    }

    /**
     * @return required collection-valued predicate parameter
     */
    public static  YqlPredicateParam> of(String fieldPath, Collection coll) {
        return of(fieldPath, coll, false, PredicateStatement.ComplexField.FLATTEN, PredicateStatement.CollectionKind.DICT_SET);
    }

    /**
     * @return required collection-valued predicate parameter
     */
    @SafeVarargs
    public static  YqlPredicateParam> of(String fieldPath, V first, V... rest) {
        return of(fieldPath, concat(Stream.of(first), stream(rest)).collect(toList()));
    }

    public static  YqlPredicateParam of(String fieldPath, T value, boolean optional, PredicateStatement.ComplexField structKind, PredicateStatement.CollectionKind collectionKind) {
        if (value instanceof Collection) {
            Preconditions.checkArgument(collectionKind != PredicateStatement.CollectionKind.SINGLE, "Collection parameter cannot be used with SINGLE collection kind");
            Preconditions.checkArgument(!optional, "Collection parameters cannot be optional");
            Preconditions.checkArgument(!((Collection) value).isEmpty(), "Collection value must not be empty");
            return new YqlPredicateParam<>(fieldPath, value, false, structKind, collectionKind);
        } else {
            Preconditions.checkArgument(collectionKind == PredicateStatement.CollectionKind.SINGLE, "Non-collection parameters cannot be used with " + collectionKind + " collection kind");
            return new YqlPredicateParam<>(fieldPath, value, optional, structKind, collectionKind);
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy