/*
 * 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 com.facebook.presto.matching;

import com.facebook.presto.matching.pattern.CapturePattern;
import com.facebook.presto.matching.pattern.FilterPattern;
import com.facebook.presto.matching.pattern.TypeOfPattern;
import com.facebook.presto.matching.pattern.WithPattern;

import java.util.function.Predicate;

public abstract class Pattern<T>
{
    private final Pattern<?> previous;

    public static Pattern<Object> any()
    {
        return typeOf(Object.class);
    }

    public static <T> Pattern<T> typeOf(Class<T> expectedClass)
    {
        return new TypeOfPattern<>(expectedClass);
    }

    protected Pattern()
    {
        this(null);
    }

    protected Pattern(Pattern<?> previous)
    {
        this.previous = previous;
    }

    public Pattern<T> capturedAs(Capture<T> capture)
    {
        return new CapturePattern<>(capture, this);
    }

    public Pattern<T> matching(Predicate<? super T> predicate)
    {
        return new FilterPattern<>(predicate, this);
    }

    public Pattern<T> with(PropertyPattern<? super T, ?> pattern)
    {
        return new WithPattern<>(pattern, this);
    }

    public Pattern<?> previous()
    {
        return previous;
    }

    public abstract Match<T> accept(Matcher matcher, Object object, Captures captures);

    public abstract void accept(PatternVisitor patternVisitor);

    @Override
    public String toString()
    {
        DefaultPrinter printer = new DefaultPrinter();
        accept(printer);
        return printer.result();
    }
}

@@ -833,44 +833,142 @@
 rn;%0A
-%0Aimport java.util.function.Predicate
+import com.google.common.collect.Iterables;%0A%0Aimport java.util.function.Predicate;%0A%0Aimport static com.google.common.base.Predicates.not
 ;%0A%0Ap
@@ -1398,32 +1398,571 @@
 revious;%0A    %7D%0A%0A
+    //FIXME make sure there's a proper toString,%0A    // like with(propName)%5Cn%5Ctfilter(isEmpty)%0A    // or with(propName) map(isEmpty) equalTo(true)%0A    public static %3CF, T extends Iterable%3CS%3E, S%3E PropertyPattern%3CF, T%3E empty(Property%3CF, T%3E property)%0A    %7B%0A        return PropertyPattern.upcast(property.matching(Iterables::isEmpty));%0A    %7D%0A%0A    public static %3CF, T extends Iterable%3CS%3E, S%3E PropertyPattern%3CF, T%3E nonEmpty(Property%3CF, T%3E property)%0A    %7B%0A        return PropertyPattern.upcast(property.matching(not(Iterables::isEmpty)));%0A    %7D%0A%0A
     public Patte

Add empty and nonEmpty combinators for properties returning Iterables