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

me.lucko.helper.event.functional.SubscriptionBuilder Maven / Gradle / Ivy

There is a newer version: 5.6.14
Show newest version
/*
 * This file is part of helper, licensed under the MIT License.
 *
 *  Copyright (c) lucko (Luck) 
 *  Copyright (c) contributors
 *
 *  Permission is hereby granted, free of charge, to any person obtaining a copy
 *  of this software and associated documentation files (the "Software"), to deal
 *  in the Software without restriction, including without limitation the rights
 *  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 *  copies of the Software, and to permit persons to whom the Software is
 *  furnished to do so, subject to the following conditions:
 *
 *  The above copyright notice and this permission notice shall be included in all
 *  copies or substantial portions of the Software.
 *
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 *  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 *  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 *  SOFTWARE.
 */

package me.lucko.helper.event.functional;

import me.lucko.helper.utils.Log;

import java.util.concurrent.TimeUnit;
import java.util.function.BiConsumer;
import java.util.function.Predicate;

import javax.annotation.Nonnull;

/**
 * Abstract {@link me.lucko.helper.event.Subscription} builder.
 *
 * @param  the handled / event type
 */
public interface SubscriptionBuilder {

    BiConsumer DEFAULT_EXCEPTION_CONSUMER = (event, throwable) -> {
        Log.severe("[EVENTS] Exception thrown whilst handling event: " + event.getClass().getName());
        throwable.printStackTrace();
    };

    /**
     * Add a expiry predicate.
     *
     * @param predicate the expiry test
     * @return ths builder instance
     */
    @Nonnull
    SubscriptionBuilder expireIf(@Nonnull Predicate predicate);

    /**
     * Sets the expiry time on the handler
     *
     * @param duration the duration until expiry
     * @param unit     the unit for the duration
     * @return the builder instance
     * @throws IllegalArgumentException if duration is not greater than or equal to 1
     */
    @Nonnull
    SubscriptionBuilder expireAfter(long duration, @Nonnull TimeUnit unit);

    /**
     * Sets the number of calls until the handler will automatically be unregistered
     *
     * 

The call counter is only incremented if the event call passes all filters and if the handler completes * without throwing an exception. * * @param maxCalls the number of times the handler will be called until being unregistered. * @return the builder instance * @throws IllegalArgumentException if maxCalls is not greater than or equal to 1 */ @Nonnull SubscriptionBuilder expireAfter(long maxCalls); /** * Adds a filter to the handler. * *

An event will only be handled if it passes all filters. Filters are evaluated in the order they are * registered. * * @param predicate the filter * @return the builder instance */ @Nonnull SubscriptionBuilder filter(@Nonnull Predicate predicate); }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy