How to Bind Methods or Constructors to Functional Interfaces
How to bind methods or constructors to functional interfaces in a general way, with the FunctionalInterfaceFactory component.
Join the DZone community and get the full member experience.
Join For FreeTo bind methods or constructors to functional interfaces, we are going to use the FunctionalInterfaceFactory of Burningwave Core library. FunctionalInterfaceFactory component uses to cache all generated functional interfaces for faster access. Before we start coding, we must add the following dependency to our pom.xml:
<dependency>
<groupId>org.burningwave</groupId>
<artifactId>core</artifactId>
<version>8.12.6</version>
</dependency>
Constructors Binding
To bind a constructors to a functional interface, we will use the following constructor:
xxxxxxxxxx
private Service(String name, String... items) {
this.id = UUID.randomUUID().toString();
this.name = name;
this.items = items;
logInfo("\nMultiparameter constructor:\n\tid: {}\n\tname: {} \n\titems: {}",
this.id, this.name, String.join(", ", this.items));
}
... And now let's see the code needed to bind and call the generated functional interface:
ComponentContainer componentContainer = ComponentContainer.getInstance();
FunctionalInterfaceFactory functionalInterfaceFactory =
componentContainer.getFunctionalInterfaceFactory();
BiFunction<String, String[], Service> serviceInstantiator =
functionalInterfaceFactory.getOrCreate(Service.class, String.class, String[].class);
Service service = serviceInstantiator.apply("Service name", new String[] {"item 1", "item 2"});
Methods Binding
To bind a method to a functional interface, we will use the following method:
xxxxxxxxxx
private Long reset(String name, String... items) {
this.id = UUID.randomUUID().toString();
this.name = name;
this.items = items;
logInfo("\nMultiparameter method:\n\tid: {}\n\tname: {} \n\titems: {}", this.id,
this.name, String.join(", ", this.items));
return System.currentTimeMillis();
}
... And now let's see the code needed to bind and call the generated functional interface:
xxxxxxxxxx
ComponentContainer componentContainer = ComponentContainer.getInstance();
FunctionalInterfaceFactory functionalInterfaceFactory =
componentContainer.getFunctionalInterfaceFactory();
MultiParamsFunction<Long> methodInvoker =
functionalInterfaceFactory.getOrCreate(Service.class, "reset", String.class, String[].class);
Long currentTimeMillis = methodInvoker.apply(service, "Service One", new String[] {"item 1", "item 2"});
Void Methods Binding
To bind a void methods to a functional interface, we will use the following method:
x
private void voidReset(String... name) {
this.id = UUID.randomUUID().toString();
this.name = name[0];
this.items = null;
logInfo("\nSingle parameter void varargs method:\n\tname: {}", this.name);
}
... And now let's see the code needed to bind and call the generated functional interface:
xxxxxxxxxx
ComponentContainer componentContainer = ComponentContainer.getInstance();
FunctionalInterfaceFactory functionalInterfaceFactory =
componentContainer.getFunctionalInterfaceFactory();
BiConsumer<Service, String[]> methodInvoker =
functionalInterfaceFactory.getOrCreate(Service.class, "voidReset", String[].class);
methodInvoker.accept(service, new String[] {"Service Two"});
Binding to Methods with Boolean Return
To bind a method with a boolean return to a functional interface, we will use the following method:
x
private boolean resetWithBooleanReturn(String... name) {
this.id = UUID.randomUUID().toString();
this.name = name[0];
this.items = null;
logInfo("\nSingle parameter varargs method with boolean return:\n\tname: {}", this.name);
return true;
}
... And now let's see the code needed to bind and call the generated functional interface:
xxxxxxxxxx
ComponentContainer componentContainer = ComponentContainer.getInstance();
FunctionalInterfaceFactory functionalInterfaceFactory =
componentContainer.getFunctionalInterfaceFactory();
BiPredicate<Service, String[]> methodInvoker =
functionalInterfaceFactory.getOrCreate(Service.class, "resetWithBooleanReturn", String[].class);
boolean executed = methodInvoker.test(service, new String[] {"Service Two"});
Conclusion
In this article, we learned how to bind methods and constructors to functional interfaces, and the complete source code of the examples above is available here.
Published at DZone with permission of Jonathan Burton. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments