001/*
002 * The contents of this file are subject to the terms of the Common Development and
003 * Distribution License (the License). You may not use this file except in compliance with the
004 * License.
005 *
006 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
007 * specific language governing permission and limitations under the License.
008 *
009 * When distributing Covered Software, include this CDDL Header Notice in each file and include
010 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
011 * Header, with the fields enclosed by brackets [] replaced by your own identifying
012 * information: "Portions copyright [year] [name of copyright owner]".
013 *
014 * Copyright 2014 ForgeRock Inc.
015 */
016package org.forgerock.util;
017
018
019/**
020 * A synchronous function which returns a result immediately.
021 * <p>
022 * <b>Exception handling</b>: implementations which do not throw any exceptions
023 * should declare that they throw an exception of type
024 * {@link org.forgerock.util.promise.NeverThrowsException}.
025 * <p>
026 * Example usage:
027 *
028 * <pre>
029 * public class IsPossiblePrime implements Function&lt;String, Boolean, IllegalArgumentException&gt; {
030 *     public Boolean apply(String value) throws IllegalArgumentException {
031 *         // Parse the parameter now and potentially immediately throw an
032 *         // exception.
033 *         final BigInteger possiblePrime = new BigInteger(value);
034 *
035 *         // Determine if the parameter is a prime number.
036 *         return possiblePrime.isProbablePrime(1000);
037 *     }
038 * }
039 * </pre>
040 *
041 * @param <VIN>
042 *            The type of the function parameter, or {@link Void} if the
043 *            function does not expect a parameter.
044 * @param <VOUT>
045 *            The type of the function result, or {@link Void} if the function
046 *            does not return anything (i.e. it only has side-effects).
047 * @param <E>
048 *            The type of the exception thrown by the function, or
049 *            {@link org.forgerock.util.promise.NeverThrowsException} if no exception is thrown by the
050 *            function.
051 * @see AsyncFunction
052 * @see org.forgerock.util.promise.NeverThrowsException
053 */
054// @FunctionalInterface
055public interface Function<VIN, VOUT, E extends Exception> {
056    /**
057     * Applies this function to the input parameter {@code value} and returns
058     * the result.
059     *
060     * @param value
061     *            The input parameter.
062     * @return The result of applying this function to {@code value}.
063     * @throws E
064     *             If this function cannot be applied to {@code value}.
065     */
066    VOUT apply(VIN value) throws E;
067}