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

domino.java.capsule.DynamicCapsuleContext Maven / Gradle / Ivy

There is a newer version: 0.3.1
Show newest version
package domino.java.capsule;

import java.util.LinkedHashSet;

import de.tototec.utils.functional.Optional;

/**
 * A {@link CapsuleContext} implementation based on {@link DynamicVariable} and
 * {@link DefaultCapsuleScope}.
 */
public class DynamicCapsuleContext implements CapsuleContext {
	/**
	 * A Set representing the current scope.
	 */
	private DynamicVariable>> dynamicCapsuleSet = new DynamicVariable>>(
			Optional.none());

	//
	@Override
	public void addCapsule(final Capsule capsule) {
		// Start the capsule immediately
		capsule.start();

		// Add capsule to the current set if there is one
		dynamicCapsuleSet.value().foreach(p -> p.add(capsule));
	}

	@Override
	public CapsuleScope executeWithinNewCapsuleScope(final Runnable f) {
		// Create the new set of capsules
		final LinkedHashSet newCapsuleSet = new LinkedHashSet();

		// // Execute the function in the new set
		dynamicCapsuleSet.withValue(Optional.some(newCapsuleSet), () -> {
			f.run();
			return null;
		});

		// Returns the set wrapped in the scope interface
		return new DefaultCapsuleScope(newCapsuleSet);
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy