Before you mark as a dup, yes I've seen Function Pointers in Java, and no, it didn't really answer my question, basically because I'm pretty new to Java, so I didn't really understand allot of the answers.
This is kind of some mashed up Java / C++, is there any reasonable way to do this in Java?
public class Foo {
private int _data;
/* various other functions */
public boolean test1( Foo other ) { /* do test */ }
public boolean test2( Foo other ) { /* do test */ }
public boolean test3( Foo other ) { /* do test */ }
public boolean test4( Foo other ) { /* do test */ }
}
public class Bar {
private Foo[] _foos = { /* Init an array of Foos */ };
public Bar doSomething() {
_foos = new Foo[4];
_foos[0] = getTest(Foo::test1);
_foos[1] = getTest(Foo::test2);
_foos[2] = getTest(Foo::test3);
_foos[3] = getTest(Foo::test4);
}
/*
* Now we only have a single function which takes function pointer.
*/
private Foo _getTest(boolean Foo::*func()) {
Foo current = _foos[ 0 ];
for ( int i = 1; i != _foos.length; i++ )
current = current.*func( _foos[ i ] ) ? _foos[ i ] : current;
return current;
}
}