view src/clojure/lang/Delay.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 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 **/
11 /* rich Jun 28, 2007 */
13 package clojure.lang;
15 public class Delay implements IDeref{
16 Object val;
17 IFn fn;
19 public Delay(IFn fn){
20 this.fn = fn;
21 this.val = null;
22 }
24 static public Object force(Object x) throws Exception{
25 return (x instanceof Delay) ?
26 ((Delay) x).deref()
27 : x;
28 }
30 synchronized public Object deref() throws Exception{
31 if(fn != null)
32 {
33 val = fn.invoke();
34 fn = null;
35 }
36 return val;
37 }
38 }