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

org.wildfly.security.auth.server.Scoped Maven / Gradle / Ivy

/*
 * JBoss, Home of Professional Open Source.
 * Copyright 2017 Red Hat, Inc., and individual contributors
 * as indicated by the @author tags.
 *
 * 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 org.wildfly.security.auth.server;

import java.util.concurrent.Callable;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.ObjIntConsumer;
import java.util.function.Supplier;

import org.wildfly.common.function.ExceptionBiConsumer;
import org.wildfly.common.function.ExceptionBiFunction;
import org.wildfly.common.function.ExceptionConsumer;
import org.wildfly.common.function.ExceptionFunction;
import org.wildfly.common.function.ExceptionObjIntConsumer;
import org.wildfly.common.function.ExceptionSupplier;

/**
 * An identity configuration which can be applied on a scoped basis.
 *
 * @author David M. Lloyd
 */
public interface Scoped {
    /**
     * Run an action under this identity.
     *
     * @param action the action to run
     */
    default void runAs(Runnable action) {
        if (action == null) return;
        runAsConsumer(Runnable::run, action);
    }

    /**
     * Run an action under this identity.
     *
     * @param action the action to run
     * @param  the action return type
     * @return the action result (may be {@code null})
     * @throws Exception if the action fails
     */
    default  T runAs(Callable action) throws Exception {
        if (action == null) return null;
        return runAsFunctionEx(Callable::call, action);
    }

    /**
     * Run an action under this identity.
     *
     * @param parameter the parameter to pass to the action
     * @param action the action to run
     * @param  the action return type
     * @param  the action parameter type
     * @return the action result (may be {@code null})
     */
    default  R runAsFunction(Function action, T parameter) {
        if (action == null) return null;
        return runAsFunction(Function::apply, action, parameter);
    }

    /**
     * Run an action under this identity.
     *
     * @param parameter1 the first parameter to pass to the action
     * @param parameter2 the second parameter to pass to the action
     * @param action the action to run
     * @param  the action return type
     * @param  the action first parameter type
     * @param  the action second parameter type
     * @return the action result (may be {@code null})
     */
     R runAsFunction(BiFunction action, T parameter1, U parameter2);

    /**
     * Run an action under this identity.
     *
     * @param parameter the parameter to pass to the action
     * @param action the action to run
     * @param  the action parameter type
     */
    default  void runAsConsumer(Consumer action, T parameter) {
        if (action == null) return;
        runAsConsumer(Consumer::accept, action, parameter);
    }

    /**
     * Run an action under this identity.
     *
     * @param parameter1 the first parameter to pass to the action
     * @param parameter2 the second parameter to pass to the action
     * @param action the action to run
     * @param  the action first parameter type
     * @param  the action second parameter type
     */
     void runAsConsumer(BiConsumer action, T parameter1, U parameter2);

    /**
     * Run an action under this identity.
     *
     * @param parameter1 the first parameter to pass to the action
     * @param parameter2 the second parameter to pass to the action
     * @param action the action to run
     * @param  the action first parameter type
     */
     void runAsObjIntConsumer(ObjIntConsumer action, T parameter1, int parameter2);

    /**
     * Run an action under this identity.
     *
     * @param action the action to run
     * @param  the action return type
     * @return the action result (may be {@code null})
     */
    default  T runAsSupplier(Supplier action) {
        if (action == null) return null;
        return runAsFunction(Supplier::get, action);
    }

    /**
     * Run an action under this identity.
     *
     * @param parameter the parameter to pass to the action
     * @param action the action to run
     * @param  the action return type
     * @param  the action parameter type
     * @param  the action exception type
     * @return the action result (may be {@code null})
     * @throws E if the action throws this exception
     */
    default  R runAsFunctionEx(ExceptionFunction action, T parameter) throws E {
        if (action == null) return null;
        return runAsFunctionEx(ExceptionFunction::apply, action, parameter);
    }

    /**
     * Run an action under this identity.
     *
     * @param parameter1 the first parameter to pass to the action
     * @param parameter2 the second parameter to pass to the action
     * @param action the action to run
     * @param  the action return type
     * @param  the action first parameter type
     * @param  the action second parameter type
     * @param  the action exception type
     * @return the action result (may be {@code null})
     * @throws E if the action throws this exception
     */
     R runAsFunctionEx(ExceptionBiFunction action, T parameter1, U parameter2) throws E;

    /**
     * Run an action under this identity.
     *
     * @param parameter the parameter to pass to the action
     * @param action the action to run
     * @param  the action parameter type
     * @param  the action exception type
     * @throws E if the action throws this exception
     */
    default  void runAsConsumerEx(ExceptionConsumer action, T parameter) throws E {
        if (action == null) return;
        runAsConsumerEx(ExceptionConsumer::accept, action, parameter);
    }

    /**
     * Run an action under this identity.
     *
     * @param parameter1 the first parameter to pass to the action
     * @param parameter2 the second parameter to pass to the action
     * @param action the action to run
     * @param  the action first parameter type
     * @param  the action second parameter type
     * @param  the action exception type
     * @throws E if the action throws this exception
     */
     void runAsConsumerEx(ExceptionBiConsumer action, T parameter1, U parameter2) throws E;

    /**
     * Run an action under this identity.
     *
     * @param parameter1 the first parameter to pass to the action
     * @param parameter2 the second parameter to pass to the action
     * @param action the action to run
     * @param  the action first parameter type
     * @param  the action exception type
     * @throws E if the action throws this exception
     */
     void runAsObjIntConsumerEx(ExceptionObjIntConsumer action, T parameter1, int parameter2) throws E;

    /**
     * Run an action under this identity.
     *
     * @param action the action to run
     * @param  the action return type
     * @param  the action exception type
     * @return the action result (may be {@code null})
     * @throws E if the action throws this exception
     */
    default  T runAsSupplierEx(ExceptionSupplier action) throws E {
        if (action == null) return null;
        return runAsFunctionEx(ExceptionSupplier::get, action);
    }
}