Advertisement
Guest User

Untitled

a guest
Dec 6th, 2014
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.76 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. package clojure.lang;
  12.  
  13. import java.util.concurrent.ConcurrentHashMap;
  14. import java.io.Serializable;
  15. import java.util.Collection;
  16.  
  17. public class NamespaceContainer implements Serializable {
  18.  
  19. final static ConcurrentHashMap<Symbol, Namespace> root = new ConcurrentHashMap<Symbol, Namespace>();
  20.  
  21. final static InheritableThreadLocal<ConcurrentHashMap<Symbol, Namespace>> current = new InheritableThreadLocal<ConcurrentHashMap<Symbol, Namespace>>() {
  22. protected ConcurrentHashMap<Symbol, Namespace> childValue(ConcurrentHashMap<Symbol, Namespace> parentValue) {
  23. if (parentValue == null) {
  24. this.set(root);
  25. return root;
  26. }
  27. this.set(parentValue);
  28. return parentValue;
  29. }
  30.  
  31. protected ConcurrentHashMap<Symbol, Namespace> initialValue() {
  32. return root;
  33. }
  34.  
  35. };
  36.  
  37. final static ThreadLocal<PersistentQueue> prev = new ThreadLocal<PersistentQueue>() {
  38. protected PersistentQueue initialValue() {
  39. return PersistentQueue.EMPTY;
  40. }
  41. };
  42.  
  43. final static Symbol CLOJURE_NS = Symbol.create("clojure.core");
  44.  
  45. public Collection<Namespace> values() {
  46. return current.get().values();
  47. }
  48.  
  49. public Namespace get(Symbol name) {
  50. return current.get().get(name);
  51. }
  52.  
  53. public Namespace putIfAbsent (Symbol name, Namespace ns) {
  54. return current.get().putIfAbsent(name, ns);
  55. }
  56.  
  57. public Namespace remove(Symbol name) {
  58. return current.get().remove(name);
  59. }
  60.  
  61. static NamespaceContainer.Ref enter(Ref r) {
  62. // Verify current namespace and requires are in dest?
  63. ConcurrentHashMap<Symbol, Namespace> c = current.get();
  64. if (c != root) prev.set(prev.get().cons(c));
  65. current.set(r.value);
  66. return r;
  67. }
  68.  
  69. static NamespaceContainer.Ref enter() {
  70. ConcurrentHashMap<Symbol, Namespace> c = new ConcurrentHashMap<Symbol, Namespace>();
  71. c.putIfAbsent(CLOJURE_NS,root.get(CLOJURE_NS));
  72. // Push current namespace and requires?
  73. return enter(new Ref(c));
  74. }
  75.  
  76. static void exit() {
  77. PersistentQueue pq = prev.get();
  78. if (pq.peek() == null) {
  79. current.set(root);
  80. } else {
  81. current.set((ConcurrentHashMap<Symbol, Namespace>)pq.peek());
  82. }
  83. prev.set(pq.pop());
  84. }
  85.  
  86. static class Ref {
  87. private final ConcurrentHashMap<Symbol, Namespace> value;
  88. Ref(ConcurrentHashMap<Symbol, Namespace> value) {
  89. this.value = value;
  90. }
  91. }
  92.  
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement