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();                                                                                 

Methods binding

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

private Long reset(String id, String name, String... items) {                                                                  
    this.id = id;                                                                                                              
    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();                                                                                         
}                                                                                                                              
                                                                                                                               
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();                                                                                         
}                                                                                                                              
                                                                                                                               
private Long reset(String... name) {                                                                                           
    this.id = UUID.randomUUID().toString();                                                                                    
    this.name = name[0];                                                                                                       
    this.items = null;                                                                                                         
    logInfo("\nSingle parameter varargs method:\n\tname: {}", this.name);                                                      
    return System.currentTimeMillis();                                                                                         
}                                                                                                                              
                                                                                                                               
private Long reset(String name) {                                                                                              
    this.id = UUID.randomUUID().toString();                                                                                    
    this.name = name;                                                                                                          
    this.items = null;                                                                                                         
    logInfo("\nSingle parameter method:\n\tname: {}", this.name);                                                              
    return System.currentTimeMillis();                                                                                         
}                                                                                                                              
                                                                                                                               
private Long reset() {                                                                                                         
    this.id = UUID.randomUUID().toString();                                                                                    
    this.name = "no name";                                                                                                     
    this.items = null;                                                                                                         
    logInfo("\nNo parameter method:\n\tname: {}", this.name);                                                                  
    return System.currentTimeMillis();                                                                                         
}                                                                                                                              

... 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<Long> methodInvokerZero = fIF.getOrCreate(Service.class, "reset", String.class, String.class, String[].class);                  
Long currentTimeMillis = methodInvokerZero.apply(service, UUID.randomUUID().toString(), "Service Zero New Name", new String[] {"item 3", "item 4"});
                                                                                                                                                    
MultiParamsFunction<Long> methodInvokerOne = fIF.getOrCreate(Service.class, "reset", String.class, String[].class);                                 
currentTimeMillis = methodInvokerOne.apply(service, "Service One", new String[] {"item 1", "item 2"});                                              
                                                                                                                                                    
BiFunction<Service, String[], Long> methodInvokerTwo = fIF.getOrCreate(Service.class, "reset", String[].class);                                     
currentTimeMillis = methodInvokerTwo.apply(service, new String[] {"Service Two"});                                                                  
                                                                                                                                                    
BiFunction<Service, String, Long> methodInvokerThree = fIF.getOrCreate(Service.class, "reset", String.class);                                       
currentTimeMillis = methodInvokerThree.apply(service, "Service Three");                                                                             
                                                                                                                                                    
Function<Service, Long> methodInvokerFour = fIF.getOrCreate(Service.class, "reset");                                                                
currentTimeMillis = methodInvokerFour.apply(service);                                                                                               

Void methods binding

To bind void methods to functional interfaces we will use the following methods:

private void voidReset(String id, String name, String... items) {                                                                   
    this.id = id;                                                                                                                   
    this.name = name;                                                                                                               
    this.items = items;                                                                                                             
    logInfo("\nMultiparameter void method:\n\tid: {}\n\tname: {} \n\titems: {}", this.id, this.name, String.join(", ", this.items));
}                                                                                                                                   
                                                                                                                                    
private void voidReset(String name, String... items) {                                                                              
    this.id = UUID.randomUUID().toString();                                                                                         
    this.name = name;                                                                                                               
    this.items = items;                                                                                                             
    logInfo("\nMultiparameter void method:\n\tname: {} \n\titems: {}", this.name, String.join(", ", this.items));                   
}                                                                                                                                   
                                                                                                                                    
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);                                                      
}                                                                                                                                   
                                                                                                                                    
private void voidReset(String name) {                                                                                               
    this.id = UUID.randomUUID().toString();                                                                                         
    this.name = "no name";                                                                                                          
    this.items = null;                                                                                                              
    logInfo("\nSingle parameter void method:\n\tname: {}", this.name);                                                              
}                                                                                                                                   
                                                                                                                                    
private void voidReset() {                                                                                                          
    this.id = UUID.randomUUID().toString();                                                                                         
    this.name = "no name";                                                                                                          
    this.items = null;                                                                                                              
    logInfo("\nNo parameter void method:\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();                                            
                                                                                                                                
MultiParamsConsumer methodInvokerZero = fIF.getOrCreate(Service.class, "voidReset", String.class, String.class, String[].class);
methodInvokerZero.accept(service, UUID.randomUUID().toString(), "Service Zero New Name", new String[] {"item 3", "item 4"});    
                                                                                                                                
MultiParamsConsumer methodInvokerOne = fIF.getOrCreate(Service.class, "voidReset", String.class, String[].class);               
methodInvokerOne.accept(service, "Service One", new String[] {"item 1", "item 2"});                                             
                                                                                                                                
BiConsumer<Service, String[]> methodInvokerTwo = fIF.getOrCreate(Service.class, "voidReset", String[].class);                   
methodInvokerTwo.accept(service, new String[] {"Service Two"});                                                                 
                                                                                                                                
BiConsumer<Service, String> methodInvokerThree = fIF.getOrCreate(Service.class, "voidReset", String.class);                     
methodInvokerThree.accept(service, "Service Three");                                                                            
                                                                                                                                
Consumer<Service> methodInvokerFour = fIF.getOrCreate(Service.class, "voidReset");                                              
methodInvokerFour.accept(service);                                                                                              

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