Java 7
I have the following interface:
public interface SqlOperator{
public String apply(Object o);
/**
* @return an operator representing the set defined
* by inversing the image of {@this operator}.
*/
public SqlOperator not();
//Some other methods
}
And I have a few its implementations that look like:
public class Foo implements SqlOperator{
public SqlOperator not(){
return new Foo(){
@Override
public String apply(Object o){
return String.format("NOT (%s)", super.apply(o));
}
};
}
//other methods implementation
}
and this
public class Bar implements SqlOperator{
public SqlOperator not(){
return new Bar(){
@Override
public String apply(Object o){
return String.format("NOT (%s)", super.apply(o));
}
};
}
//other methods implementation
}
The thing is the not() method is pretty much the same for all implementation for now (Currently I have 7 ones) except the class to be instantiated with the new operator. Is there a way to avoid wiritng such boilerplate code any time I need to implement the SqlOperator.
not(foo)would be a better API thanfoo.not()- it would be better to use composition to build up your expression. It also makes implementingandororoperators easier:and(foo, bar)rather thanfoo.and(bar)etc. This means that yourSqlOperatorinterface would contain simply theapplymethod.