All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.cassite.pure.ioc.Scope Maven / Gradle / Ivy

The newest version!
package net.cassite.pure.ioc;

import net.cassite.style.Style;
import net.cassite.style.interfaces.RFunc1;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * A scope of dependency injection
* A scope can store variables and bind classes. */ public class Scope { private static ThreadLocal scopeThreadLocal = new ThreadLocal<>(); /** * retrieve scope in current thread * * @return current thread scope */ public static Scope currentThreadScope() { Scope scope = scopeThreadLocal.get(); if (scope != null) return scope; scope = new Scope(IOCController.rootScope); scopeThreadLocal.set(scope); return scope; } /** * parent scope */ protected Scope parent; /** * container for constants, variables and properties. */ protected Map> dataMap = new LinkedHashMap<>(); /** * container for bond classes -> objects
* values that the functions return would be used to fill parameters of their bond classes */ protected Map, RFunc1> bondMap = new LinkedHashMap<>(); /** * create a scope without parent scope */ public Scope() { this(null); } /** * create a scope with parent scope * * @param parent parent scope */ public Scope(Scope parent) { this.parent = parent; } /** * get instance stored data map * * @param name name of the data * @param data type * @return retrieved data */ @SuppressWarnings("unchecked") public T get(String name) { try { return (T) (dataMap.containsKey(name) ? (dataMap.get(name).apply(this)) : (getParent() == null ? null : getParent().get(name))); } catch (Throwable t) { throw Style.$(t); } } /** * determine whether the scope contain instance provider with given name * * @param name name * @return true if contains, false otherwise */ public boolean containsKey(String name) { return dataMap.containsKey(name) || (getParent() != null && getParent().containsKey(name)); } /** * register a function into data map, the function will be called when retrieving the object * * @param name name of the data * @param func function that produces data */ public void register(String name, RFunc1 func) { dataMap.put(name, func); } /** * get parent scope * * @return parent scope, or null if no parent scope */ public Scope getParent() { return parent; } /** * register a constant instance into data map * * @param name name of the constant * @param constant constant object */ public void registerInstance(String name, Object constant) { register(name, scope -> constant); } /** * register properties(Map[Object,Object]) into data map * * @param name name * @param properties properties */ public void registerProperties(String name, Map properties) { registerInstance(name, properties); } /** * bind class to function that produces instance * * @param cls class * @param instanceFunc function */ public void bind(Class cls, RFunc1 instanceFunc) { bondMap.put(cls, instanceFunc); } /** * get bond instance * * @param cls class * @return instance retrieved */ public Object getBondInstance(Class cls) { try { return bondMap.containsKey(cls) ? bondMap.get(cls).apply(this) : (getParent() == null ? null : getParent().getBondInstance(cls)); } catch (Throwable e) { throw Style.$(e); } } /** * determine whether the class has been bond to an instance provider * * @param cls class * @return true if bond, false otherwise */ public boolean isBond(Class cls) { return bondMap.containsKey(cls) || (getParent() != null && getParent().isBond(cls)); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy