Last active
June 3, 2024 08:26
-
-
Save mskoroglu/bac439524ad110d07bf5ec686dd71f45 to your computer and use it in GitHub Desktop.
Generic Use Case Interface with Functional Extensions in Java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 asFunction
,Consumer
,Supplier
, andRunnable
, to create a versatile framework for defining and executing various use cases.Features
UseCase<Q, S>
Interface:java.util.function.Function
.execute(Q request)
method to process a request and return a response.apply(final Q request)
method to delegate toexecute
.UseCase.Consumer<Q>
Interface:UseCase<Q, Object>
andjava.util.function.Consumer<Q>
.consume(Q request)
method.execute(final Q request)
andaccept(final Q request)
methods to delegate toconsume
.UseCase.Supplier<S>
Interface:UseCase<Object, S>
andjava.util.function.Supplier<S>
.supply()
method.execute(final Object request)
andget()
methods to delegate tosupply
.UseCase.Runnable
Interface:UseCase<Object, Object>
andjava.lang.Runnable
.execute(final Object request)
method to delegate torun
.