Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Copyright (c) Ralph Ritoch. All rights reserved.
- * The use and distribution terms for this software are covered by the
- * Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
- * which can be found in the file epl-v10.html at the root of this distribution.
- * By using this software in any fashion, you are agreeing to be bound by
- * the terms of this license.
- * You must not remove this notice, or any other, from this software.
- **/
- package clojure.lang;
- import java.util.concurrent.ConcurrentHashMap;
- import java.io.Serializable;
- import java.util.Collection;
- public class NamespaceContainer implements Serializable {
- final static ConcurrentHashMap<Symbol, Namespace> root = new ConcurrentHashMap<Symbol, Namespace>();
- final static InheritableThreadLocal<ConcurrentHashMap<Symbol, Namespace>> current = new InheritableThreadLocal<ConcurrentHashMap<Symbol, Namespace>>() {
- protected ConcurrentHashMap<Symbol, Namespace> childValue(ConcurrentHashMap<Symbol, Namespace> parentValue) {
- if (parentValue == null) {
- this.set(root);
- return root;
- }
- this.set(parentValue);
- return parentValue;
- }
- protected ConcurrentHashMap<Symbol, Namespace> initialValue() {
- return root;
- }
- };
- final static ThreadLocal<PersistentQueue> prev = new ThreadLocal<PersistentQueue>() {
- protected PersistentQueue initialValue() {
- return PersistentQueue.EMPTY;
- }
- };
- final static Symbol CLOJURE_NS = Symbol.create("clojure.core");
- public Collection<Namespace> values() {
- return current.get().values();
- }
- public Namespace get(Symbol name) {
- return current.get().get(name);
- }
- public Namespace putIfAbsent (Symbol name, Namespace ns) {
- return current.get().putIfAbsent(name, ns);
- }
- public Namespace remove(Symbol name) {
- return current.get().remove(name);
- }
- static NamespaceContainer.Ref enter(Ref r) {
- // Verify current namespace and requires are in dest?
- ConcurrentHashMap<Symbol, Namespace> c = current.get();
- if (c != root) prev.set(prev.get().cons(c));
- current.set(r.value);
- return r;
- }
- static NamespaceContainer.Ref enter() {
- ConcurrentHashMap<Symbol, Namespace> c = new ConcurrentHashMap<Symbol, Namespace>();
- c.putIfAbsent(CLOJURE_NS,root.get(CLOJURE_NS));
- // Push current namespace and requires?
- return enter(new Ref(c));
- }
- static void exit() {
- PersistentQueue pq = prev.get();
- if (pq.peek() == null) {
- current.set(root);
- } else {
- current.set((ConcurrentHashMap<Symbol, Namespace>)pq.peek());
- }
- prev.set(pq.pop());
- }
- static class Ref {
- private final ConcurrentHashMap<Symbol, Namespace> value;
- Ref(ConcurrentHashMap<Symbol, Namespace> value) {
- this.value = value;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement