Skip to content

Instantly share code, notes, and snippets.

@anggadarkprince
Created May 8, 2016 02:53
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anggadarkprince/2b6e1f71f854ad21e03d16e423fbdcc7 to your computer and use it in GitHub Desktop.
Save anggadarkprince/2b6e1f71f854ad21e03d16e423fbdcc7 to your computer and use it in GitHub Desktop.
Android Object Pooling (reservoir)
/**
* Sketch Project Studio
* Created by Angga 20/04/2016 19:32
*/
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Fragment fragment;
Object objectFragment = objectPooling.find("object1");
if (objectFragment == null) {
fragment = ArticleFragment.newInstanceFeatured(1, "object1");
objectPooling.pool(fragment, ArticleFragment.FEATURED_HEADLINE);
} else {
fragment = (ArticleFragment) objectFragment;
}
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container_body, fragment);
fragmentTransaction.commit();
}
}
package com.sketchproject.infogue.modules;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
/**
* Pooling object into list and retrieve if needed, this class for optimization
* and prevent object to recreate again.
*
* Sketch Project Studio
* Created by Angga on 25/04/2016 09.37.
*/
public class ObjectPooling {
public static final String KEY_LABEL = "label";
public static final String KEY_OBJECT = "object";
private List<HashMap<String, Object>> objects;
private boolean isFixedSize = false;
private int max = 0;
/**
* Default constructor
*/
public ObjectPooling() {
objects = new ArrayList<>();
}
/**
* Constructor with default object has been set.
*
* @param objectList objects with label.
*/
@SuppressWarnings("unused")
public ObjectPooling(List<HashMap<String, Object>> objectList) {
objects = objectList;
}
/**
* Create fixed object pooling.
*
* @param maxSize number of objects can handle.
*/
@SuppressWarnings("unused")
public ObjectPooling(int maxSize) {
isFixedSize = true;
max = maxSize;
objects = new ArrayList<>(max);
}
/**
* Registering object into list and populate by label.
*
* @param data object need to returned when needed
* @param label label related to data object as reference to identifying it
* @return status if the object has been there or not
*/
public boolean pool(Object data, String label) {
if (isObjectAvailable(label)) {
return false;
}
if (isFixedSize && getSize() > max) {
throw new IndexOutOfBoundsException(ObjectPooling.class.getSimpleName() +
" is out of max number of container pool object. Max size of object is " + max);
}
HashMap<String, Object> object = new HashMap<>();
object.put(KEY_LABEL, label);
object.put(KEY_OBJECT, data);
objects.add(object);
return true;
}
/**
* Check if an object exist by their label.
*
* @param label related identity to object
* @return boolean that indicate object found or not
*/
public boolean isObjectAvailable(String label) {
for (HashMap object : objects) {
if (object.get(KEY_LABEL).equals(label)) {
return true;
}
}
return false;
}
/**
* Delete object on list by label.
*
* @param label related label which object want to destroyed
* @return object which destroyed
*/
@SuppressWarnings("unused")
public Object unpool(String label) {
for (HashMap object : objects) {
if (object.get(KEY_LABEL).equals(label)) {
Object data = object.get(KEY_OBJECT);
objects.remove(object);
return data;
}
}
return null;
}
/**
* Find out if and object related by certain label exist.
*
* @param label related with object
* @return object if found and null if not found
*/
public Object find(String label) {
for (HashMap object : objects) {
if (object.get(KEY_LABEL).equals(label)) {
return object.get(KEY_OBJECT);
}
}
return null;
}
/**
* Check if current pooling is fixed.
*
* @return boolean
*/
@SuppressWarnings("unused")
public boolean isFixedSize() {
return isFixedSize;
}
/**
* Check if current pooling is empty.
*
* @return boolean
*/
public boolean isEmpty() {
return objects.isEmpty();
}
/**
* Count current size object has polled.
*
* @return number of object
*/
public int getSize() {
return objects.size();
}
/**
* Destroy all objects.
*/
public void clear() {
objects.clear();
}
}
@anggadarkprince
Copy link
Author

anggadarkprince commented May 8, 2016

This class help you to populate object into list, and retrieve if you needed without re-instantiate the new one, so your state and data will be keep,, this is very useful if you use in single class which contain many fragment and you often to switch form one to another like using Navigation Drawer to switch fragments or you could use from one activity to another activity by change the list object into static and implement singleton pattern on ObjectPooling.java

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment