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

io.ayte.utility.predicate.kit.unary.iterable.Comparing Maven / Gradle / Ivy

The newest version!
package io.ayte.utility.predicate.kit.unary.iterable;

import io.ayte.utility.predicate.UnaryPredicate;
import io.ayte.utility.predicate.kit.unary.AugmentedUnaryPredicate;
import lombok.AccessLevel;
import lombok.EqualsAndHashCode;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.ToString;
import lombok.val;

import java.util.function.BiPredicate;

@EqualsAndHashCode
@ToString(includeFieldNames = false)
@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
public class Comparing implements AugmentedUnaryPredicate> {
    private final BiPredicate predicate;

    @Override
    public boolean test(Iterable subject) {
        return apply(subject, predicate);
    }

    public static  boolean apply(
            @NonNull Iterable subject,
            @NonNull BiPredicate predicate
    ) {
        val iterator = subject.iterator();
        if (!iterator.hasNext()) {
            return true;
        }
        T previous = iterator.next();
        while (iterator.hasNext()) {
            val current = iterator.next();
            if (!predicate.test(previous, current)) {
                return false;
            }
            previous = current;
        }
        return true;
    }

    public static  UnaryPredicate> create(@NonNull BiPredicate predicate) {
        return new Comparing<>(predicate);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy