/*
 *  Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you 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 org.codehaus.groovy.runtime;

import groovy.lang.Closure;

import java.util.function.Predicate;

/**
 * Adapts a {@link Closure} to a {@link Predicate}, converting the closure's
 * return value to a boolean using Groovy truth semantics.
 *
 * @since 6.0.0
 */
public class BooleanClosurePredicate<T> implements Predicate<T> {
    private final BooleanClosureWrapper bcw;

    /**
     * Constructs a BooleanClosurePredicate from a Closure.
     *
     * @param wrapped the {@link Closure} to adapt to a predicate
     */
    public BooleanClosurePredicate(Closure wrapped) {
        this.bcw = new BooleanClosureWrapper(wrapped);
    }

    /**
     * Tests the given value using the wrapped closure with Groovy truth conversion.
     *
     * @param arg the value to test
     * @return {@code true} if the closure result is truthy by Groovy standards, {@code false} otherwise
     */
    @Override
    public boolean test(T arg) {
        return bcw.call(arg);
    }
}
