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

io.trino.matching.Property Maven / Gradle / Ivy

There is a newer version: 465
Show newest version
/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.trino.matching;

import io.trino.matching.pattern.EqualsPattern;
import io.trino.matching.pattern.FilterPattern;

import java.util.Optional;
import java.util.function.BiFunction;
import java.util.function.BiPredicate;
import java.util.function.Function;
import java.util.function.Predicate;

import static java.util.Objects.requireNonNull;

public class Property
{
    private final String name;
    private final BiFunction> function;

    public static  Property property(String name, Function function)
    {
        return property(name, (source, context) -> function.apply(source));
    }

    public static  Property property(String name, BiFunction function)
    {
        return optionalProperty(name, (source, context) -> Optional.of(function.apply(source, context)));
    }

    public static  Property optionalProperty(String name, Function> function)
    {
        return new Property<>(name, (source, context) -> function.apply(source));
    }

    public static  Property optionalProperty(String name, BiFunction> function)
    {
        return new Property<>(name, function);
    }

    public Property(String name, BiFunction> function)
    {
        this.name = requireNonNull(name, "name is null");
        this.function = requireNonNull(function, "function is null");
    }

    public String getName()
    {
        return name;
    }

    public BiFunction> getFunction()
    {
        //without the ::apply below, the type system is unable to drop the R type from Optional
        return function::apply;
    }

    public  PropertyPattern matching(Pattern pattern)
    {
        return PropertyPattern.of(this, pattern);
    }

    public PropertyPattern capturedAs(Capture capture)
    {
        Pattern matchAll = (Pattern) Pattern.any();
        return matching(matchAll.capturedAs(capture));
    }

    public PropertyPattern equalTo(T expectedValue)
    {
        return matching(new EqualsPattern<>(expectedValue, Optional.empty()));
    }

    public PropertyPattern matching(Predicate predicate)
    {
        return matching((t, context) -> predicate.test(t));
    }

    public PropertyPattern matching(BiPredicate predicate)
    {
        return matching(new FilterPattern<>(predicate, Optional.empty()));
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy