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

com.idefav.context.Context Maven / Gradle / Ivy

package com.idefav.context;

import java.util.Map;
import java.util.concurrent.Callable;

/**
 * the Context description.
 *
 * @author wuzishu
 */
public interface Context extends Map {
	static Context current() {
		return ContextStorage.defaultStorage().current();
	}

	static void reset() {
		ContextStorage.defaultStorage().reset();
	}

	default Scope makeCurrent() {
		return ContextStorage.defaultStorage().attach(this);
	}

	Context clone();

	Context merge(Context target);

	default Runnable wrap(Runnable runnable) {
		Context current = current().clone();
		return () -> {
			try (Scope ignored = ContextStorage.defaultStorage().attach(current)) {
				runnable.run();
			}

		};
	}

	default  Callable wrap(Callable callable) {
		Context current = current().clone();
		return () -> {
			try (Scope ignored = ContextStorage.defaultStorage().attach(current)) {
				return callable.call();
			}
		};
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy