You can create an anonymous class with a function of the desired signature that forwards calls to the original function.
Let's say you have the following functions:
public class StringPrinter { public void printStringInt( String s, int n ) { System.out.println( s + ", " + n ); } public void printStringString( String s, String s2 ) { System.out.println( s + ", " + s2 ); } }
You can then create anonymous classes that efficiently bind arguments to these functions.
public static void main( String[] args ) { public interface Print1String { public void printString( String s ); } List<Print1String> funcs = new ArrayList<Print1String); funcs.add( new Print1String() { public void printString( String s ) { new StringPrinter( ).printStringInt( s, 42 ); }}); funcs.add( new Print1String() { public void printString( String s ) { new StringPrinter( ).printStringString( s, "bar" ); }}); for ( Print1String func : funcs ) { func.print1String("foo"); } }
source share