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

com.softicar.platform.common.core.singleton.SingletonSetScope 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 java.util.ArrayList;

/**
 * This class can be used to control {@link CurrentSingletonSet}.
 * 

* This class should only be used in a try block to ensure proper restoration of * the original value of {@link CurrentSingletonSet}. * * @author Oliver Richers */ public class SingletonSetScope implements AutoCloseable { private final SingletonSet singletonSet; private final SingletonSet originalSet; /** * Activates a new {@link SingletonSet} which inherits its values from the * {@link SingletonSet} returned by {@link CurrentSingletonSet#get}. */ public SingletonSetScope() { this(new SingletonSet(CurrentSingletonSet.get())); } /** * Activates the given {@link SingletonSet}. * * @param singletonSet * the {@link SingletonSet} to activate (never null) */ public SingletonSetScope(SingletonSet singletonSet) { this.singletonSet = singletonSet; this.originalSet = CurrentSingletonSet.get(); CurrentSingletonSet.set(singletonSet); } /** * Restores the originally active {@link SingletonSet}. */ @Override public void close() { // copying singletons to prevent potential concurrent modification new ArrayList<>(singletonSet.getSingletons()).forEach(this::callCloseHandler); CurrentSingletonSet.set(originalSet); } private void callCloseHandler(Singleton singleton) { T value = singletonSet.getSingletonValue(singleton); if (value != null) { singleton.callScopeCloseHandler(value); } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy