![JAR search and dependency download from the Maven repository](/logo.png)
net.kemitix.mon.combinator.Around Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of mon Show documentation
Show all versions of mon Show documentation
TypeAlias, Result and Maybe for Java
The newest version!
/**
* The MIT License (MIT)
*
* Copyright (c) 2021 Paul Campbell
*
* 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 net.kemitix.mon.combinator;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.BiConsumer;
import java.util.function.Function;
/**
* Around pattern combinator.
*
* Original from http://boundsofjava.com/newsletter/003-introducing-combinators-part1
*
* @param the argument type
* @param the result type
*
* @author Federico Peralta Schaffner ([email protected])
*/
@FunctionalInterface
public interface Around extends
Function<
Function,
Function<
BiConsumer, T>,
Function>> {
/**
* Decorates a function with an BiConsumer that will be supplier with an executable to perform the function, and the
* argument that will be applied when executed.
*
* @param function the function to apply the argument to and return the result value of
* @param around the bi-consumer that will supplied with the executable and the argument
* @param the argument type
* @param the result type
*
* @return a partially applied Function that will take an argument, and the result of applying it to function
*/
static Function decorate(
final Function function,
final BiConsumer, T> around
) {
return Around.create().apply(function)
.apply(around);
}
/**
* Create an Around curried function.
*
* @param the argument type
* @param the result type
*
* @return a curried function that will execute the around function, passing an executable and the invocations
* argument. The around function must {@code execute()} the executable and may capture the result.
*/
static Around create() {
return function -> around -> argument -> {
final AtomicReference result = new AtomicReference<>();
final Executable callback = () -> {
result.set(function.apply(argument));
return result.get();
};
around.accept(callback, argument);
return result.get();
};
}
/**
* The executable that will be supplied to the around function to trigger the surrounded function.
*
* @param the return type of the function
*/
@FunctionalInterface
interface Executable {
/**
* Executes the function.
*
* @return the result of applying the function
*/
R execute();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy