@Test public void retain_creates_object() throws Exception { RetainState retainState = new RetainState(null); String result = retainState.retain(0, new RetainState.OnCreate<String>() { @Override public String onCreate() { return "test"; } }); assertEquals("test", result); }
@SuppressWarnings("unchecked") public <T> T retain(int id, OnCreate<T> onCreate) { if (state == null) { state = new SparseArray<>(); } T item = (T) state.get(id); if (item == null) { item = onCreate.onCreate(); if (item instanceof RetainState) { ((RetainState) item).setIsRetaining(isRetaining); } state.put(id, item); } return item; }
RetainState implements Iterable<Object> { @SuppressWarnings("unchecked") public <T> T retain(int id, OnCreate<T> onCreate) { if (state == null) { state = new SparseArray<>(); } T item = (T) state.get(id); if (item == null) { item = onCreate.onCreate(); if (item instanceof RetainState) { ((RetainState) item).setIsRetaining(isRetaining); } state.put(id, item); } return item; } }
RetainState implements Iterable<Object> { @SuppressWarnings("unchecked") public <T> T retain(int id, OnCreate<T> onCreate) { if (state == null) { state = new SparseArray<>(); } T item = (T) state.get(id); if (item == null) { item = onCreate.onCreate(); if (item instanceof RetainState) { ((RetainState) item).setIsRetaining(isRetaining); } state.put(id, item); } return item; } @SuppressWarnings("unchecked")  RetainState(Object retainedState); }
RetainState implements Iterable<Object> { @SuppressWarnings("unchecked") public <T> T retain(int id, OnCreate<T> onCreate) { if (state == null) { state = new SparseArray<>(); } T item = (T) state.get(id); if (item == null) { item = onCreate.onCreate(); if (item instanceof RetainState) { ((RetainState) item).setIsRetaining(isRetaining); } state.put(id, item); } return item; } @SuppressWarnings("unchecked")  RetainState(Object retainedState); static RetainState from(Object host); Object onRetain(); boolean isRetaining(); void destroy(); @SuppressWarnings("unchecked") T retain(int id, OnCreate<T> onCreate); @SuppressWarnings("unchecked") T get(int id); @SuppressWarnings("unchecked") T remove(int id); @Override @SuppressWarnings("unchecked") Iterator<Object> iterator(); }
RetainState implements Iterable<Object> { @SuppressWarnings("unchecked") public <T> T retain(int id, OnCreate<T> onCreate) { if (state == null) { state = new SparseArray<>(); } T item = (T) state.get(id); if (item == null) { item = onCreate.onCreate(); if (item instanceof RetainState) { ((RetainState) item).setIsRetaining(isRetaining); } state.put(id, item); } return item; } @SuppressWarnings("unchecked")  RetainState(Object retainedState); static RetainState from(Object host); Object onRetain(); boolean isRetaining(); void destroy(); @SuppressWarnings("unchecked") T retain(int id, OnCreate<T> onCreate); @SuppressWarnings("unchecked") T get(int id); @SuppressWarnings("unchecked") T remove(int id); @Override @SuppressWarnings("unchecked") Iterator<Object> iterator(); static final OnCreate<RetainState> CREATE; }