MCPcopy Index your code
hub / github.com/clojure/clojure / MultiFn

Class MultiFn

src/jvm/clojure/lang/MultiFn.java:18–586  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

16import java.util.concurrent.locks.ReentrantReadWriteLock;
17
18public class MultiFn extends AFn{
19final public IFn dispatchFn;
20final public Object defaultDispatchVal;
21final public IRef hierarchy;
22final String name;
23final ReentrantReadWriteLock rw;
24volatile IPersistentMap methodTable;
25volatile IPersistentMap preferTable;
26volatile IPersistentMap methodCache;
27volatile Object cachedHierarchy;
28
29static final Var assoc = RT.var("clojure.core", "assoc");
30static final Var dissoc = RT.var("clojure.core", "dissoc");
31static final Var isa = RT.var("clojure.core", "isa?");
32static final Var parents = RT.var("clojure.core", "parents");
33
34public MultiFn(String name, IFn dispatchFn, Object defaultDispatchVal, IRef hierarchy) {
35 this.rw = new ReentrantReadWriteLock();
36 this.name = name;
37 this.dispatchFn = dispatchFn;
38 this.defaultDispatchVal = defaultDispatchVal;
39 this.methodTable = PersistentHashMap.EMPTY;
40 this.methodCache = getMethodTable();
41 this.preferTable = PersistentHashMap.EMPTY;
42 this.hierarchy = hierarchy;
43 cachedHierarchy = null;
44}
45
46public MultiFn reset(){
47 rw.writeLock().lock();
48 try{
49 methodTable = methodCache = preferTable = PersistentHashMap.EMPTY;
50 cachedHierarchy = null;
51 return this;
52 }
53 finally {
54 rw.writeLock().unlock();
55 }
56}
57
58public MultiFn addMethod(Object dispatchVal, IFn method) {
59 rw.writeLock().lock();
60 try{
61 methodTable = getMethodTable().assoc(dispatchVal, method);
62 resetCache();
63 return this;
64 }
65 finally {
66 rw.writeLock().unlock();
67 }
68}
69
70public MultiFn removeMethod(Object dispatchVal) {
71 rw.writeLock().lock();
72 try
73 {
74 methodTable = getMethodTable().without(dispatchVal);
75 resetCache();

Callers

nothing calls this directly

Calls 1

varMethod · 0.95

Tested by

no test coverage detected