Advertisement
Guest User

Untitled

a guest
Dec 6th, 2014
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.62 KB | None | 0 0
  1. /**
  2. * Copyright (c) Ralph Ritoch. All rights reserved.
  3. * The use and distribution terms for this software are covered by the
  4. * Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
  5. * which can be found in the file epl-v10.html at the root of this distribution.
  6. * By using this software in any fashion, you are agreeing to be bound by
  7. * the terms of this license.
  8. * You must not remove this notice, or any other, from this software.
  9. **/
  10.  
  11.  
  12. // Installation: in Namespaces.java change
  13. //final static ConcurrentHashMap<Symbol, Namespace> namespaces = new ConcurrentHashMap<Symbol, Namespace>();
  14. // TO
  15. //final static NamespaceContainer namespaces = new NamespaceContainer();
  16.  
  17. package clojure.lang;
  18.  
  19. import java.util.concurrent.ConcurrentHashMap;
  20. import java.io.Serializable;
  21. import java.util.Collection;
  22. import java.util.Iterator;
  23. import java.util.AbstractCollection;
  24.  
  25. public class NamespaceContainer implements Serializable {
  26.  
  27. final static ConcurrentHashMap<Symbol, Namespace> root = new ConcurrentHashMap<Symbol, Namespace>();
  28.  
  29. final static InheritableThreadLocal<ConcurrentHashMap<Symbol, Namespace>> current = new InheritableThreadLocal<ConcurrentHashMap<Symbol, Namespace>>() {
  30. protected ConcurrentHashMap<Symbol, Namespace> childValue(ConcurrentHashMap<Symbol, Namespace> parentValue) {
  31. if (parentValue == null) {
  32. this.set(root);
  33. return root;
  34. }
  35. this.set(parentValue);
  36. return parentValue;
  37. }
  38.  
  39. protected ConcurrentHashMap<Symbol, Namespace> initialValue() {
  40. return root;
  41. }
  42.  
  43. };
  44.  
  45. final static ThreadLocal<IPersistentList> prev = new ThreadLocal<IPersistentList>() {
  46. protected IPersistentList initialValue() {
  47. return PersistentList.EMPTY;
  48. }
  49. };
  50.  
  51. final static Symbol CLOJURE_NS = Symbol.create("clojure.core");
  52.  
  53. public Collection<Namespace> values() {
  54. return current.get().values();
  55. }
  56.  
  57. public Namespace get(Symbol name) {
  58. return current.get().get(name);
  59. }
  60.  
  61. public Namespace putIfAbsent (Symbol name, Namespace ns) {
  62. return current.get().putIfAbsent(name, ns);
  63. }
  64.  
  65. public Namespace remove(Symbol name) {
  66. return current.get().remove(name);
  67. }
  68.  
  69. public static NamespaceContainer.Ref enter(Ref r) {
  70.  
  71. // Verify current namespace in dest
  72. if (!r.value.containsKey(((Namespace) RT.CURRENT_NS.deref()).getName())) {
  73. throw new RuntimeException("Current namespace is missing in target container");
  74. } else {
  75.  
  76. // Verify deps in dest?
  77. // Assuming deps are already loaded if current namespace is there.
  78.  
  79. // Do enter....
  80. ConcurrentHashMap<Symbol, Namespace> c = current.get();
  81. if (c != root) prev.set((IPersistentList)((IPersistentCollection)prev.get()).cons(c));
  82. current.set(r.value);
  83. return r;
  84. }
  85. }
  86.  
  87. public static NamespaceContainer.Ref enter() {
  88. ConcurrentHashMap<Symbol, Namespace> c = new ConcurrentHashMap<Symbol, Namespace>();
  89. c.putIfAbsent(CLOJURE_NS,root.get(CLOJURE_NS));
  90. // Push current namespace and requires
  91. PersistentHashSet deps = depends((Namespace) RT.CURRENT_NS.deref(),true);
  92. Iterator i = deps.iterator();
  93. while(i.hasNext()) {
  94. Namespace n = (Namespace)i.next();
  95. c.putIfAbsent(n.getName(),n);
  96. }
  97. return enter(new Ref(c));
  98. }
  99.  
  100. public static void exit() {
  101. IPersistentList pq = prev.get();
  102.  
  103. if (((Counted)pq).count() > 0) {
  104. if (pq.peek() == null) {
  105. current.set(root);
  106. } else {
  107. current.set((ConcurrentHashMap<Symbol, Namespace>)pq.peek());
  108. }
  109. prev.set((IPersistentList)pq.pop());
  110. } else {
  111. current.set(root);
  112. }
  113. }
  114.  
  115. public static PersistentHashSet depends(Namespace ns, boolean deep) {
  116. PersistentHashSet out;
  117. PersistentHashSet in = (PersistentHashSet)PersistentHashSet.EMPTY.cons(ns);
  118. Collection vals = ((PersistentHashMap)ns.getMappings()).values();
  119. Iterator vals_i = vals.iterator();
  120. while(vals_i.hasNext()) {
  121. Object v = vals_i.next();
  122. if (v instanceof Var) {
  123. in = (PersistentHashSet)in.cons(((Var)v).ns);
  124. }
  125. }
  126. if (deep) {
  127. out = (PersistentHashSet)PersistentHashSet.EMPTY.cons(ns);
  128. while(in.size() > 0) {
  129. Namespace ns_c = (Namespace) in.iterator().next();
  130. if (!out.contains(ns_c)) {
  131. Iterator d_i = depends(ns_c,false).iterator();
  132. while(d_i.hasNext()) {
  133. in = (PersistentHashSet)in.cons(d_i.next());
  134. }
  135. }
  136. out = (PersistentHashSet)out.cons(ns_c);
  137. in = (PersistentHashSet)in.disjoin(ns_c);
  138. }
  139. } else {
  140. out = in;
  141. }
  142. return out;
  143. }
  144.  
  145. static class Ref {
  146. private final ConcurrentHashMap<Symbol, Namespace> value;
  147. Ref(ConcurrentHashMap<Symbol, Namespace> value) {
  148. this.value = value;
  149. }
  150. }
  151.  
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement