Mercurial > lasercutter
view 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 source
1 /**2 * Copyright (c) Rich Hickey. All rights reserved.3 * The use and distribution terms for this software are covered by the4 * 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 by7 * the terms of this license.8 * You must not remove this notice, or any other, from this software.9 **/11 /* rich Nov 2, 2009 */13 package clojure.lang;15 public final class KeywordLookupSite implements ILookupSite, ILookupThunk{17 final int n;18 final Keyword k;20 public KeywordLookupSite(int n, Keyword k){21 this.n = n;22 this.k = k;23 }25 public Object fault(Object target, ILookupHost host){26 if(target instanceof IKeywordLookup)27 {28 return install(target, host);29 }30 else if(target instanceof ILookup)31 {32 host.swapThunk(n,ilookupThunk(target.getClass()));33 return ((ILookup) target).valAt(k);34 }35 host.swapThunk(n,this);36 return RT.get(target, k);37 }39 public Object get(Object target){40 if(target instanceof IKeywordLookup || target instanceof ILookup)41 return this;42 return RT.get(target,k);43 }45 private ILookupThunk ilookupThunk(final Class c){46 return new ILookupThunk(){47 public Object get(Object target){48 if(target != null && target.getClass() == c)49 return ((ILookup) target).valAt(k);50 return this;51 }52 };53 }55 private Object install(Object target, ILookupHost host){56 ILookupThunk t = ((IKeywordLookup)target).getLookupThunk(k);57 if(t != null)58 {59 host.swapThunk(n,t);60 return t.get(target);61 }62 host.swapThunk(n,ilookupThunk(target.getClass()));63 return ((ILookup) target).valAt(k);64 }65 }