Skip to content

How to bind methods or constructors to functional interfaces

Burningwave edited this page Dec 1, 2020 · 17 revisions

To bind methods or constructors to functional interfaces we are going to use the FunctionalInterfaceFactory.

Constructors binding

To bind constructors to functional interfaces we will use the following constructors:

private Service(String id, String name, String... items) {                                                                          
    this.id = id;                                                                                                                   
    this.name = name;                                                                                                               
    this.items = items;                                                                                                             
    logInfo("\nMultiparameter constructor:\n\tid: {}\n\tname: {} \n\titems: {}", this.id, this.name, String.join(", ", this.items));
}                                                                                                                                   
                                                                                                                                    
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));
}                                                                                                                                   
                                                                                                                                    
private Service(String... name) {                                                                                                   
    this.id = UUID.randomUUID().toString();                                                                                         
    this.name = name[0];                                                                                                            
    this.items = null;                                                                                                              
    logInfo("\nSingle parameter varargs constructor:\n\tname: {}", this.name);                                                      
}                                                                                                                                   
                                                                                                                                    
private Service(String name) {                                                                                                      
    this.id = UUID.randomUUID().toString();                                                                                         
    this.name = name;                                                                                                               
    this.items = null;                                                                                                              
    logInfo("\nSingle parameter constructor:\n\tname: {}", this.name);                                                              
}                                                                                                                                   
                                                                                                                                    
private Service() {                                                                                                                 
    this.id = UUID.randomUUID().toString();                                                                                         
    this.name = "no name";                                                                                                          
    this.items = null;                                                                                                              
    logInfo("\nNo parameter constructor:\n\tname: {}", this.name);                                                                  
}                                                                                                                                   

... And now let's see the code needed to bind and call the generated functional interfaces:

ComponentContainer componentContainer = ComponentContainer.getInstance();                                                            
FunctionalInterfaceFactory fIF = componentContainer.getFunctionalInterfaceFactory();                                                 
                                                                                                                                     
MultiParamsFunction<Service> serviceInstantiatorZero = fIF.getOrCreate(Service.class, String.class, String.class, String[].class);   
Service serviceZero = serviceInstantiatorZero.apply(UUID.randomUUID().toString(), "Service Zero", new String[] {"item 1", "item 2"});
                                                                                                                                     
BiFunction<String, String[], Service> serviceInstantiatorOne = fIF.getOrCreate(Service.class, String.class, String[].class);         
Service serviceOne = serviceInstantiatorOne.apply("Service One", new String[] {"item 1", "item 2"});                                 
                                                                                                                                     
Function<String[], Service> serviceInstantiatorTwo = fIF.getOrCreate(Service.class, String[].class);                                 
Service serviceTwo = serviceInstantiatorTwo.apply(new String[] {"Service Two"});                                                     
                                                                                                                                     
Function<String, Service> serviceInstantiatorThree = fIF.getOrCreate(Service.class, String.class);                                   
Service serviceThree = serviceInstantiatorThree.apply("Service Three");                                                              
                                                                                                                                     
Supplier<Service> serviceInstantiatorFour = fIF.getOrCreate(Service.class);                                                          
Service serviceFour = serviceInstantiatorFour.get();                                                                                 

Examples of use of some components:

BackgroundExecutor
ClassFactory
ClassHunter
ClassPathHunter
CodeExecutor
Constructors
Fields
FileSystemItem
FunctionalInterfaceFactory
IterableObjectHelper
JavaMemoryCompiler
Methods
PathHelper
PropertyAccessor
UnitSourceGenerator

HitCount

Clone this wiki locally