Skip to content

Instantly share code, notes, and snippets.

@mskoroglu
Last active June 3, 2024 08:26
Show Gist options
  • Save mskoroglu/bac439524ad110d07bf5ec686dd71f45 to your computer and use it in GitHub Desktop.
Save mskoroglu/bac439524ad110d07bf5ec686dd71f45 to your computer and use it in GitHub Desktop.
Generic Use Case Interface with Functional Extensions in Java
/**
* A functional interface for use cases with an input of type {@code Q} and an output of type {@code S}.
* It extends {@code java.util.function.Function} to provide functional interface compatibility.
*
* @param <Q> the type of the request
* @param <S> the type of the response
*/
@FunctionalInterface
public interface UseCase<Q, S> extends java.util.function.Function<Q, S> {
/**
* Executes the use case with the given request and returns a response.
*
* @param request the request object
* @return the response object
*/
S execute(Q request);
/**
* Applies the use case function with the given request.
*
* @param request the request object
* @return the response object
*/
@Override
default S apply(final Q request) {
return execute(request);
}
/**
* A functional interface for use cases that consume an input of type {@code Q} and do not return a result.
* It extends {@code UseCase} and {@code java.util.function.Consumer}.
*
* @param <Q> the type of the request
*/
@FunctionalInterface
interface Consumer<Q> extends UseCase<Q, Object>, java.util.function.Consumer<Q> {
/**
* Consumes the request.
*
* @param request the request object
*/
void consume(Q request);
/**
* Executes the consumer use case with the given request.
*
* @param request the request object
* @return always {@code null}
*/
@Override
default Object execute(final Q request) {
consume(request);
return null;
}
/**
* Accepts the request and executes the consumer use case.
*
* @param request the request object
*/
@Override
default void accept(final Q request) {
execute(request);
}
}
/**
* A functional interface for use cases that supply a result of type {@code S} without any input.
* It extends {@code UseCase} and {@code java.util.function.Supplier}.
*
* @param <S> the type of the response
*/
@FunctionalInterface
interface Supplier<S> extends UseCase<Object, S>, java.util.function.Supplier<S> {
/**
* Supplies the response.
*
* @return the response object
*/
S supply();
/**
* Executes the supplier use case without any input.
*
* @param request always {@code null}
* @return the response object
*/
@Override
default S execute(final Object request) {
return supply();
}
/**
* Gets the response by executing the supplier use case.
*
* @return the response object
*/
@Override
default S get() {
return execute(null);
}
}
/**
* A functional interface for use cases that perform a task without any input or output.
* It extends {@code UseCase} and {@code java.lang.Runnable}.
*/
@FunctionalInterface
interface Runnable extends UseCase<Object, Object>, java.lang.Runnable {
/**
* Executes the runnable use case, performing a task.
*
* @param request always {@code null}
* @return always {@code null}
*/
@Override
default Object execute(final Object request) {
run();
return null;
}
}
}
@mskoroglu
Copy link
Author

mskoroglu commented Jun 3, 2024

This Gist presents a Java implementation of a generic UseCase interface designed to support common functional patterns. It extends the functionality of Java's built-in function interfaces, such as Function, Consumer, Supplier, and Runnable, to create a versatile framework for defining and executing various use cases.

Features

  • UseCase<Q, S> Interface:

    • A functional interface that extends java.util.function.Function.
    • Defines an execute(Q request) method to process a request and return a response.
    • Default apply(final Q request) method to delegate to execute.
  • UseCase.Consumer<Q> Interface:

    • Extends UseCase<Q, Object> and java.util.function.Consumer<Q>.
    • Defines a consume(Q request) method.
    • Default execute(final Q request) and accept(final Q request) methods to delegate to consume.
  • UseCase.Supplier<S> Interface:

    • Extends UseCase<Object, S> and java.util.function.Supplier<S>.
    • Defines a supply() method.
    • Default execute(final Object request) and get() methods to delegate to supply.
  • UseCase.Runnable Interface:

    • Extends UseCase<Object, Object> and java.lang.Runnable.
    • Default execute(final Object request) method to delegate to run.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment