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

run.halo.app.extension.WatcherPredicates Maven / Gradle / Ivy

The newest version!
package run.halo.app.extension;

import java.util.function.BiPredicate;
import java.util.function.Predicate;

public class WatcherPredicates {

    static final Predicate EMPTY_PREDICATE = (e) -> true;

    static final BiPredicate EMPTY_BI_PREDICATE = (oldExt, newExt) -> true;
    private final Predicate onAddPredicate;
    private final BiPredicate onUpdatePredicate;
    private final Predicate onDeletePredicate;

    public WatcherPredicates(Predicate onAddPredicate,
        BiPredicate onUpdatePredicate,
        Predicate onDeletePredicate) {
        this.onAddPredicate = onAddPredicate;
        this.onUpdatePredicate = onUpdatePredicate;
        this.onDeletePredicate = onDeletePredicate;
    }

    public Predicate onAddPredicate() {
        if (onAddPredicate == null) {
            return EMPTY_PREDICATE;
        }
        return onAddPredicate;
    }

    public BiPredicate onUpdatePredicate() {
        if (onUpdatePredicate == null) {
            return EMPTY_BI_PREDICATE;
        }
        return onUpdatePredicate;
    }

    public Predicate onDeletePredicate() {
        if (onDeletePredicate == null) {
            return EMPTY_PREDICATE;
        }
        return onDeletePredicate;
    }

    public static final class Builder {

        private Predicate onAddPredicate;
        private BiPredicate onUpdatePredicate;
        private Predicate onDeletePredicate;

        private GroupVersionKind gvk;

        public Builder withGroupVersionKind(GroupVersionKind gvk) {
            this.gvk = gvk;
            return this;
        }

        public Builder onAddPredicate(Predicate onAddPredicate) {
            this.onAddPredicate = onAddPredicate;
            return this;
        }

        public Builder onUpdatePredicate(
            BiPredicate onUpdatePredicate) {
            this.onUpdatePredicate = onUpdatePredicate;
            return this;
        }

        public Builder onDeletePredicate(Predicate onDeletePredicate) {
            this.onDeletePredicate = onDeletePredicate;
            return this;
        }

        public WatcherPredicates build() {
            Predicate gvkPredicate = EMPTY_PREDICATE;
            BiPredicate gvkBiPredicate = EMPTY_BI_PREDICATE;
            if (gvk != null) {
                gvkPredicate = e -> gvk.equals(e.groupVersionKind());
                gvkBiPredicate = (oldE, newE) -> oldE.groupVersionKind().equals(gvk)
                    && newE.groupVersionKind().equals(gvk);
            }
            if (onAddPredicate == null) {
                onAddPredicate = EMPTY_PREDICATE;
            }
            if (onUpdatePredicate == null) {
                onUpdatePredicate = EMPTY_BI_PREDICATE;
            }
            if (onDeletePredicate == null) {
                onDeletePredicate = EMPTY_PREDICATE;
            }

            onAddPredicate = gvkPredicate.and(onAddPredicate);
            onUpdatePredicate = gvkBiPredicate.and(onUpdatePredicate);
            onDeletePredicate = gvkPredicate.and(onDeletePredicate);

            return new WatcherPredicates(onAddPredicate, onUpdatePredicate, onDeletePredicate);
        }

    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy