I have a difficult problem. I am trying to create a generic callback manager class that uses a generic interface of any type. The plan is to do something like this:
public interface LocationListener {
void locationChanged();
void providerChanged();
}
public abstract class CallbackManager<T> implements T {
}
public class MyCallbackManager extends CallbackManager<LocationListener> {
@Override
public void locationChanged() {
}
@Override
public void providerChanged() {
}
}
Generally speaking, I try to avoid creating the MyCallbackManager class as follows:
public class MyCallbackManager extends CallbackManager<LocationListener> implements LocationListener {
@Override
public void locationChanged() {
}
@Override
public void providerChanged() {
}
}
Is there any way to achieve this? I look forward to your answers.
EDIT
Because you asked to clarify a precedent. This is the idea:
public abstract class CallbackManager<T> {
protected interface FunctionalInterface<T> {
void run(T type);
}
protected ArrayList<WeakReference<T>> eventQueue = new ArrayList<>();
protected void flush(@NonNull FunctionalInterface<T> functionalInterface) {
for (int i = eventQueue.size() - 1; i >= 0; i--) {
WeakReference<T> weakReference = eventQueue.get(i);
T type = weakReference.get();
if (type == null) {
unregister(i);
} else {
functionalInterface.run(type);
}
}
}
}
public class CallbackManagerLocation extends CallbackManager<LocationListener> implements LocationListener {
@Override
public void locationChanged() {
flush((ll) -> ll.locationChanged());
}
@Override
public void providerChanged() {
flush((ll) -> ll.providerChanged());
}
}
The embedded interface in CallbackManagerLocation is used only to have exactly the same naming convention as the interface that is held in CallbackManager.