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

com.softicar.platform.common.core.singleton.SingletonSet Maven / Gradle / Ivy

Go to download

The SoftiCAR Platform is a lightweight, Java-based library to create interactive business web applications.

There is a newer version: 50.0.0
Show newest version
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); } } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy