/**
* Copyright (c) 2014 Lazu Ioan-Bogdan
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from ...
If you think the Android project InfuseFactory listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
Java Source Code
package com.factory.infuse.internal.scope;
//www.java2s.comimport java.lang.ref.WeakReference;
import java.util.HashMap;
import java.util.Map;
import com.factory.infuse.Scope;
publicclass LocalScope implements Scope {
protected Map<Class<?>, WeakReference<Object>> scoped = new HashMap<Class<?>, WeakReference<Object>>();
/**
* Marks a instance to this scope.
*
* @param clazz
* Class to mark as scoped.
* @param instance
* Instance linked to that class.
*/publicvoid markScoped(Class<?> clazz, Object instance) {
if(!clazz.isInstance(instance)) thrownew IllegalArgumentException("Instance not type of provided class!");
scoped.put(clazz, new WeakReference<Object>(instance));
}
/**
* Obtains a instance of the object from this scope or null if the class was not marked as scoped.
*/public Object obtainScoped(Class<?> clazz) {
return scoped.get(clazz).get();
}
@Override
publicboolean peekScoped(Class<?> clazz) {
return scoped.containsKey(clazz);
}
/** Careful with this one, it returns the real list. */public Map<Class<?>, WeakReference<Object>> obtainScope() {
return scoped;
}
publicvoid clearScope() {
scoped.clear();
}
}