Mercurial > lasercutter
diff src/clojure/lang/KeywordLookupSite.java @ 10:ef7dbbd6452c
added clojure source goodness
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Sat, 21 Aug 2010 06:25:44 -0400 |
parents | |
children |
line wrap: on
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/clojure/lang/KeywordLookupSite.java Sat Aug 21 06:25:44 2010 -0400 1.3 @@ -0,0 +1,65 @@ 1.4 +/** 1.5 + * Copyright (c) Rich Hickey. All rights reserved. 1.6 + * The use and distribution terms for this software are covered by the 1.7 + * Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) 1.8 + * which can be found in the file epl-v10.html at the root of this distribution. 1.9 + * By using this software in any fashion, you are agreeing to be bound by 1.10 + * the terms of this license. 1.11 + * You must not remove this notice, or any other, from this software. 1.12 + **/ 1.13 + 1.14 +/* rich Nov 2, 2009 */ 1.15 + 1.16 +package clojure.lang; 1.17 + 1.18 +public final class KeywordLookupSite implements ILookupSite, ILookupThunk{ 1.19 + 1.20 +final int n; 1.21 +final Keyword k; 1.22 + 1.23 +public KeywordLookupSite(int n, Keyword k){ 1.24 + this.n = n; 1.25 + this.k = k; 1.26 +} 1.27 + 1.28 +public Object fault(Object target, ILookupHost host){ 1.29 + if(target instanceof IKeywordLookup) 1.30 + { 1.31 + return install(target, host); 1.32 + } 1.33 + else if(target instanceof ILookup) 1.34 + { 1.35 + host.swapThunk(n,ilookupThunk(target.getClass())); 1.36 + return ((ILookup) target).valAt(k); 1.37 + } 1.38 + host.swapThunk(n,this); 1.39 + return RT.get(target, k); 1.40 +} 1.41 + 1.42 +public Object get(Object target){ 1.43 + if(target instanceof IKeywordLookup || target instanceof ILookup) 1.44 + return this; 1.45 + return RT.get(target,k); 1.46 +} 1.47 + 1.48 +private ILookupThunk ilookupThunk(final Class c){ 1.49 + return new ILookupThunk(){ 1.50 + public Object get(Object target){ 1.51 + if(target != null && target.getClass() == c) 1.52 + return ((ILookup) target).valAt(k); 1.53 + return this; 1.54 + } 1.55 + }; 1.56 +} 1.57 + 1.58 +private Object install(Object target, ILookupHost host){ 1.59 + ILookupThunk t = ((IKeywordLookup)target).getLookupThunk(k); 1.60 + if(t != null) 1.61 + { 1.62 + host.swapThunk(n,t); 1.63 + return t.get(target); 1.64 + } 1.65 + host.swapThunk(n,ilookupThunk(target.getClass())); 1.66 + return ((ILookup) target).valAt(k); 1.67 +} 1.68 +}