An approach I take is to override implementations for an enum which implements an interface
From https://github.com/OpenHFT/Chronicle-Engine/blob/master/src/main/java/net/openhft/chronicle/engine/map/remote/MapFunction.java
public enum MapFunction implements SerializableBiFunction<MapView, Object, Object> {
CONTAINS_VALUE {
@Override
public Boolean apply(@NotNull MapView map, Object value) {
Class vClass = map.valueType();
return map.containsValue(convertTo(vClass, value));
}
},
REMOVE {
@Override
public Object apply(@NotNull MapView map, Object o) {
Class kClass = map.keyType();
Class vClass = map.valueType();
KeyValuePair kf = (KeyValuePair) o;
return map.remove(convertTo(kClass, kf.key), convertTo(vClass, kf.value));
}
},
REPLACE {
@Override
public Object apply(@NotNull MapView map, Object o) {
Class kClass = map.keyType();
Class vClass = map.valueType();
if (o instanceof KeyValuePair) {
KeyValuePair kf = (KeyValuePair) o;
return map.replace(convertTo(kClass, kf.key), convertTo(vClass, kf.value));
}
KeyValuesTuple kf = (KeyValuesTuple) o;
return map.replace(convertTo(kClass, kf.key), convertTo(vClass, kf.oldValue), convertTo(vClass, kf.value));
}
},
This allows me to have many implementations of the same method in a single .java file. Note: this still creates multiple .class files.