com.softicar.platform.common.core.singleton.SingletonSet Maven / Gradle / Ivy
Show all versions of platform-common Show documentation
package com.softicar.platform.common.core.singleton;
import com.softicar.platform.common.core.utils.CastUtils;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
/**
* Represents a set of {@link Singleton} instances.
*
* At any given time, there is one current {@link SingletonSet} per
* {@link Thread}. It is defined by {@link CurrentSingletonSet}.
*
* @author Oliver Richers
*/
public class SingletonSet {
private final Map, Object> singletons;
/**
* Constructs a new {@link SingletonSet} with all {@link Singleton} values
* to be default.
*/
public SingletonSet() {
this.singletons = new HashMap<>();
}
/**
* Constructs a new {@link SingletonSet} and inherits the {@link Singleton}
* values from the given {@link SingletonSet}.
*
* @param parentSet
* the parent set (may be null)
*/
public SingletonSet(SingletonSet parentSet) {
this.singletons = new HashMap<>();
if (parentSet != null) {
inheritValues(parentSet);
}
}
T getSingletonValue(Singleton singleton) {
Object value = singletons.computeIfAbsent(singleton, Singleton::getInitalValue);
return CastUtils.cast(value);
}
void setSingletonValue(Singleton singleton, T instance) {
singletons.put(singleton, instance);
}
void resetSingletonValue(Singleton singleton) {
singletons.remove(singleton);
}
Collection> getSingletons() {
return singletons.keySet();
}
private void inheritValues(SingletonSet parentSet) {
parentSet.singletons.keySet().forEach(singleton -> inheritValue(parentSet, singleton));
}
private void inheritValue(SingletonSet parentSet, Singleton singleton) {
T parentValue = parentSet.getSingletonValue(singleton);
if (parentValue != null) {
T value = singleton.getInheritedValue(parentValue);
if (value != null) {
setSingletonValue(singleton, value);
}
}
}
}