diff src/clojure/lang/Atom.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/Atom.java	Sat Aug 21 06:25:44 2010 -0400
     1.3 @@ -0,0 +1,104 @@
     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 Jan 1, 2009 */
    1.15 +
    1.16 +package clojure.lang;
    1.17 +
    1.18 +import java.util.concurrent.atomic.AtomicReference;
    1.19 +
    1.20 +final public class Atom extends ARef{
    1.21 +final AtomicReference state;
    1.22 +
    1.23 +public Atom(Object state){
    1.24 +	this.state = new AtomicReference(state);
    1.25 +}
    1.26 +
    1.27 +public Atom(Object state, IPersistentMap meta){
    1.28 +	super(meta);
    1.29 +	this.state = new AtomicReference(state);
    1.30 +}
    1.31 +
    1.32 +public Object deref(){
    1.33 +	return state.get();
    1.34 +}
    1.35 +
    1.36 +public Object swap(IFn f) throws Exception{
    1.37 +	for(; ;)
    1.38 +		{
    1.39 +		Object v = deref();
    1.40 +		Object newv = f.invoke(v);
    1.41 +		validate(newv);
    1.42 +		if(state.compareAndSet(v, newv))
    1.43 +			{
    1.44 +			notifyWatches(v, newv);
    1.45 +			return newv;
    1.46 +			}
    1.47 +		}
    1.48 +}
    1.49 +
    1.50 +public Object swap(IFn f, Object arg) throws Exception{
    1.51 +	for(; ;)
    1.52 +		{
    1.53 +		Object v = deref();
    1.54 +		Object newv = f.invoke(v, arg);
    1.55 +		validate(newv);
    1.56 +		if(state.compareAndSet(v, newv))
    1.57 +			{
    1.58 +			notifyWatches(v, newv);
    1.59 +			return newv;
    1.60 +			}
    1.61 +		}
    1.62 +}
    1.63 +
    1.64 +public Object swap(IFn f, Object arg1, Object arg2) throws Exception{
    1.65 +	for(; ;)
    1.66 +		{
    1.67 +		Object v = deref();
    1.68 +		Object newv = f.invoke(v, arg1, arg2);
    1.69 +		validate(newv);
    1.70 +		if(state.compareAndSet(v, newv))
    1.71 +			{
    1.72 +			notifyWatches(v, newv);
    1.73 +			return newv;
    1.74 +			}
    1.75 +		}
    1.76 +}
    1.77 +
    1.78 +public Object swap(IFn f, Object x, Object y, ISeq args) throws Exception{
    1.79 +	for(; ;)
    1.80 +		{
    1.81 +		Object v = deref();
    1.82 +		Object newv = f.applyTo(RT.listStar(v, x, y, args));
    1.83 +		validate(newv);
    1.84 +		if(state.compareAndSet(v, newv))
    1.85 +			{
    1.86 +			notifyWatches(v, newv);
    1.87 +			return newv;
    1.88 +			}
    1.89 +		}
    1.90 +}
    1.91 +
    1.92 +public boolean compareAndSet(Object oldv, Object newv){
    1.93 +	validate(newv);
    1.94 +	boolean ret = state.compareAndSet(oldv, newv);
    1.95 +	if(ret)
    1.96 +		notifyWatches(oldv, newv);
    1.97 +	return ret;
    1.98 +}
    1.99 +
   1.100 +public Object reset(Object newval){
   1.101 +	Object oldval = state.get();
   1.102 +	validate(newval);
   1.103 +	state.set(newval);
   1.104 +	notifyWatches(oldval, newval);
   1.105 +	return newval;
   1.106 +}
   1.107 +}