io.gsonfire.util.reflection.CachedReflectionFactory Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of gson-fire Show documentation
Show all versions of gson-fire Show documentation
A java library that adds some very useful features to Gson, like Date serializing to unix timestamp or RFC3339, method (getter) serialization, pre and post processors and many more. Check out the documentation to learn how to use it!
package io.gsonfire.util.reflection;
import java.lang.ref.SoftReference;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
/**
* Created by julio on 10/1/16.
*/
public class CachedReflectionFactory implements Factory {
private final ConcurrentMap> cache = new ConcurrentHashMap>();
@Override
public T get(Class clazz) {
SoftReference resultRef = (SoftReference) cache.get(clazz);
if(resultRef != null) {
T result = resultRef.get();
if (result != null) {
return result;
}
}
//We need to create the object
try {
T newObject = clazz.newInstance();
cache.putIfAbsent(clazz, new SoftReference