Like this:
Proc is a functional interface type void.
public class MainViewPagerViewModel extends ViewModel {
private HashMap<Integer, Integer> quantities = new HashMap<>();
private WeakReference<Proc> weakProc;
public void put(Integer position, Integer quantity) {
quantities.put(position, quantity);
if (weakProc.get() != null) {
weakProc.get().run();
}
}
public void updatePieChart(Consumer<HashMap<Integer, Integer>> mapConsumer) {
weakProc = new WeakReference<>(
() -> mapConsumer.accept(quantities)
);
}
}
Will this work, or I'm I still holding reference to the Consumer's onwer? or... is this too much? maybe this is enough?
public void updatePieChart(Consumer<HashMap<Integer, Integer>> mapConsumer) {
proc = () -> mapConsumer.accept(quantities)
}
Thanks in advance.