diff src/clojure/lang/Ref.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/Ref.java	Sat Aug 21 06:25:44 2010 -0400
     1.3 @@ -0,0 +1,379 @@
     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 Jul 25, 2007 */
    1.15 +
    1.16 +package clojure.lang;
    1.17 +
    1.18 +import java.util.concurrent.atomic.AtomicInteger;
    1.19 +import java.util.concurrent.atomic.AtomicLong;
    1.20 +import java.util.concurrent.locks.ReentrantReadWriteLock;
    1.21 +
    1.22 +public class Ref extends ARef implements IFn, Comparable<Ref>, IRef{
    1.23 +    public int compareTo(Ref ref) {
    1.24 +        if(this.id == ref.id)
    1.25 +            return 0;
    1.26 +        else if(this.id < ref.id)
    1.27 +            return -1;
    1.28 +        else
    1.29 +            return 1;
    1.30 +    }
    1.31 +
    1.32 +public int getMinHistory(){
    1.33 +	return minHistory;
    1.34 +}
    1.35 +
    1.36 +public Ref setMinHistory(int minHistory){
    1.37 +	this.minHistory = minHistory;
    1.38 +	return this;
    1.39 +}
    1.40 +
    1.41 +public int getMaxHistory(){
    1.42 +	return maxHistory;
    1.43 +}
    1.44 +
    1.45 +public Ref setMaxHistory(int maxHistory){
    1.46 +	this.maxHistory = maxHistory;
    1.47 +	return this;
    1.48 +}
    1.49 +
    1.50 +public static class TVal{
    1.51 +	Object val;
    1.52 +	long point;
    1.53 +	long msecs;
    1.54 +	TVal prior;
    1.55 +	TVal next;
    1.56 +
    1.57 +	TVal(Object val, long point, long msecs, TVal prior){
    1.58 +		this.val = val;
    1.59 +		this.point = point;
    1.60 +		this.msecs = msecs;
    1.61 +		this.prior = prior;
    1.62 +		this.next = prior.next;
    1.63 +		this.prior.next = this;
    1.64 +		this.next.prior = this;
    1.65 +	}
    1.66 +
    1.67 +	TVal(Object val, long point, long msecs){
    1.68 +		this.val = val;
    1.69 +		this.point = point;
    1.70 +		this.msecs = msecs;
    1.71 +		this.next = this;
    1.72 +		this.prior = this;
    1.73 +	}
    1.74 +
    1.75 +}
    1.76 +
    1.77 +TVal tvals;
    1.78 +final AtomicInteger faults;
    1.79 +final ReentrantReadWriteLock lock;
    1.80 +LockingTransaction.Info tinfo;
    1.81 +//IFn validator;
    1.82 +final long id;
    1.83 +
    1.84 +volatile int minHistory = 0;
    1.85 +volatile int maxHistory = 10;
    1.86 +
    1.87 +static final AtomicLong ids = new AtomicLong();
    1.88 +
    1.89 +public Ref(Object initVal) throws Exception{
    1.90 +	this(initVal, null);
    1.91 +}
    1.92 +
    1.93 +public Ref(Object initVal,IPersistentMap meta) throws Exception{
    1.94 +    super(meta);
    1.95 +    this.id = ids.getAndIncrement();
    1.96 +	this.faults = new AtomicInteger();
    1.97 +	this.lock = new ReentrantReadWriteLock();
    1.98 +	tvals = new TVal(initVal, 0, System.currentTimeMillis());
    1.99 +}
   1.100 +
   1.101 +//the latest val
   1.102 +
   1.103 +// ok out of transaction
   1.104 +Object currentVal(){
   1.105 +	try
   1.106 +		{
   1.107 +		lock.readLock().lock();
   1.108 +		if(tvals != null)
   1.109 +			return tvals.val;
   1.110 +		throw new IllegalStateException(this.toString() + " is unbound.");
   1.111 +		}
   1.112 +	finally
   1.113 +		{
   1.114 +		lock.readLock().unlock();
   1.115 +		}
   1.116 +}
   1.117 +
   1.118 +//*
   1.119 +
   1.120 +public Object deref(){
   1.121 +	LockingTransaction t = LockingTransaction.getRunning();
   1.122 +	if(t == null)
   1.123 +		return currentVal();
   1.124 +	return t.doGet(this);
   1.125 +}
   1.126 +
   1.127 +//void validate(IFn vf, Object val){
   1.128 +//	try{
   1.129 +//		if(vf != null && !RT.booleanCast(vf.invoke(val)))
   1.130 +//            throw new IllegalStateException("Invalid ref state");
   1.131 +//		}
   1.132 +//    catch(RuntimeException re)
   1.133 +//        {
   1.134 +//        throw re;
   1.135 +//        }
   1.136 +//	catch(Exception e)
   1.137 +//		{
   1.138 +//		throw new IllegalStateException("Invalid ref state", e);
   1.139 +//		}
   1.140 +//}
   1.141 +//
   1.142 +//public void setValidator(IFn vf){
   1.143 +//	try
   1.144 +//		{
   1.145 +//		lock.writeLock().lock();
   1.146 +//		validate(vf,currentVal());
   1.147 +//		validator = vf;
   1.148 +//		}
   1.149 +//	finally
   1.150 +//		{
   1.151 +//		lock.writeLock().unlock();
   1.152 +//		}
   1.153 +//}
   1.154 +//
   1.155 +//public IFn getValidator(){
   1.156 +//	try
   1.157 +//		{
   1.158 +//		lock.readLock().lock();
   1.159 +//		return validator;
   1.160 +//		}
   1.161 +//	finally
   1.162 +//		{
   1.163 +//		lock.readLock().unlock();
   1.164 +//		}
   1.165 +//}
   1.166 +
   1.167 +public Object set(Object val){
   1.168 +	return LockingTransaction.getEx().doSet(this, val);
   1.169 +}
   1.170 +
   1.171 +public Object commute(IFn fn, ISeq args) throws Exception{
   1.172 +	return LockingTransaction.getEx().doCommute(this, fn, args);
   1.173 +}
   1.174 +
   1.175 +public Object alter(IFn fn, ISeq args) throws Exception{
   1.176 +	LockingTransaction t = LockingTransaction.getEx();
   1.177 +	return t.doSet(this, fn.applyTo(RT.cons(t.doGet(this), args)));
   1.178 +}
   1.179 +
   1.180 +public void touch(){
   1.181 +	LockingTransaction.getEx().doEnsure(this);
   1.182 +}
   1.183 +
   1.184 +//*/
   1.185 +boolean isBound(){
   1.186 +	try
   1.187 +		{
   1.188 +		lock.readLock().lock();
   1.189 +		return tvals != null;
   1.190 +		}
   1.191 +	finally
   1.192 +		{
   1.193 +		lock.readLock().unlock();
   1.194 +		}
   1.195 +}
   1.196 +
   1.197 +
   1.198 +public void trimHistory(){
   1.199 +	try
   1.200 +		{
   1.201 +		lock.writeLock().lock();
   1.202 +		if(tvals != null)
   1.203 +			{
   1.204 +			tvals.next = tvals;
   1.205 +			tvals.prior = tvals;
   1.206 +			}
   1.207 +		}
   1.208 +	finally
   1.209 +		{
   1.210 +		lock.writeLock().unlock();
   1.211 +		}
   1.212 +}
   1.213 +
   1.214 +public int getHistoryCount(){
   1.215 +	try
   1.216 +		{
   1.217 +		lock.writeLock().lock();
   1.218 +		return histCount();
   1.219 +		}
   1.220 +	finally
   1.221 +		{
   1.222 +		lock.writeLock().unlock();
   1.223 +		}	
   1.224 +}
   1.225 +
   1.226 +int histCount(){
   1.227 +	if(tvals == null)
   1.228 +		return 0;
   1.229 +	else
   1.230 +		{
   1.231 +		int count = 0;
   1.232 +		for(TVal tv = tvals.next;tv != tvals;tv = tv.next)
   1.233 +			count++;
   1.234 +		return count;
   1.235 +		}
   1.236 +}
   1.237 +
   1.238 +final public IFn fn(){
   1.239 +	return (IFn) deref();
   1.240 +}
   1.241 +
   1.242 +public Object call() throws Exception{
   1.243 +	return invoke();
   1.244 +}
   1.245 +
   1.246 +public void run(){
   1.247 +	try
   1.248 +		{
   1.249 +		invoke();
   1.250 +		}
   1.251 +	catch(Exception e)
   1.252 +		{
   1.253 +		throw new RuntimeException(e);
   1.254 +		}
   1.255 +}
   1.256 +
   1.257 +public Object invoke() throws Exception{
   1.258 +	return fn().invoke();
   1.259 +}
   1.260 +
   1.261 +public Object invoke(Object arg1) throws Exception{
   1.262 +	return fn().invoke(arg1);
   1.263 +}
   1.264 +
   1.265 +public Object invoke(Object arg1, Object arg2) throws Exception{
   1.266 +	return fn().invoke(arg1, arg2);
   1.267 +}
   1.268 +
   1.269 +public Object invoke(Object arg1, Object arg2, Object arg3) throws Exception{
   1.270 +	return fn().invoke(arg1, arg2, arg3);
   1.271 +}
   1.272 +
   1.273 +public Object invoke(Object arg1, Object arg2, Object arg3, Object arg4) throws Exception{
   1.274 +	return fn().invoke(arg1, arg2, arg3, arg4);
   1.275 +}
   1.276 +
   1.277 +public Object invoke(Object arg1, Object arg2, Object arg3, Object arg4, Object arg5) throws Exception{
   1.278 +	return fn().invoke(arg1, arg2, arg3, arg4, arg5);
   1.279 +}
   1.280 +
   1.281 +public Object invoke(Object arg1, Object arg2, Object arg3, Object arg4, Object arg5, Object arg6) throws Exception{
   1.282 +	return fn().invoke(arg1, arg2, arg3, arg4, arg5, arg6);
   1.283 +}
   1.284 +
   1.285 +public Object invoke(Object arg1, Object arg2, Object arg3, Object arg4, Object arg5, Object arg6, Object arg7)
   1.286 +		throws Exception{
   1.287 +	return fn().invoke(arg1, arg2, arg3, arg4, arg5, arg6, arg7);
   1.288 +}
   1.289 +
   1.290 +public Object invoke(Object arg1, Object arg2, Object arg3, Object arg4, Object arg5, Object arg6, Object arg7,
   1.291 +                     Object arg8) throws Exception{
   1.292 +	return fn().invoke(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8);
   1.293 +}
   1.294 +
   1.295 +public Object invoke(Object arg1, Object arg2, Object arg3, Object arg4, Object arg5, Object arg6, Object arg7,
   1.296 +                     Object arg8, Object arg9) throws Exception{
   1.297 +	return fn().invoke(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9);
   1.298 +}
   1.299 +
   1.300 +public Object invoke(Object arg1, Object arg2, Object arg3, Object arg4, Object arg5, Object arg6, Object arg7,
   1.301 +                     Object arg8, Object arg9, Object arg10) throws Exception{
   1.302 +	return fn().invoke(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10);
   1.303 +}
   1.304 +
   1.305 +public Object invoke(Object arg1, Object arg2, Object arg3, Object arg4, Object arg5, Object arg6, Object arg7,
   1.306 +                     Object arg8, Object arg9, Object arg10, Object arg11) throws Exception{
   1.307 +	return fn().invoke(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11);
   1.308 +}
   1.309 +
   1.310 +public Object invoke(Object arg1, Object arg2, Object arg3, Object arg4, Object arg5, Object arg6, Object arg7,
   1.311 +                     Object arg8, Object arg9, Object arg10, Object arg11, Object arg12) throws Exception{
   1.312 +	return fn().invoke(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12);
   1.313 +}
   1.314 +
   1.315 +public Object invoke(Object arg1, Object arg2, Object arg3, Object arg4, Object arg5, Object arg6, Object arg7,
   1.316 +                     Object arg8, Object arg9, Object arg10, Object arg11, Object arg12, Object arg13)
   1.317 +		throws Exception{
   1.318 +	return fn().invoke(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13);
   1.319 +}
   1.320 +
   1.321 +public Object invoke(Object arg1, Object arg2, Object arg3, Object arg4, Object arg5, Object arg6, Object arg7,
   1.322 +                     Object arg8, Object arg9, Object arg10, Object arg11, Object arg12, Object arg13, Object arg14)
   1.323 +		throws Exception{
   1.324 +	return fn().invoke(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13, arg14);
   1.325 +}
   1.326 +
   1.327 +public Object invoke(Object arg1, Object arg2, Object arg3, Object arg4, Object arg5, Object arg6, Object arg7,
   1.328 +                     Object arg8, Object arg9, Object arg10, Object arg11, Object arg12, Object arg13, Object arg14,
   1.329 +                     Object arg15) throws Exception{
   1.330 +	return fn().invoke(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13, arg14, arg15);
   1.331 +}
   1.332 +
   1.333 +public Object invoke(Object arg1, Object arg2, Object arg3, Object arg4, Object arg5, Object arg6, Object arg7,
   1.334 +                     Object arg8, Object arg9, Object arg10, Object arg11, Object arg12, Object arg13, Object arg14,
   1.335 +                     Object arg15, Object arg16) throws Exception{
   1.336 +	return fn().invoke(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13, arg14, arg15,
   1.337 +	                   arg16);
   1.338 +}
   1.339 +
   1.340 +public Object invoke(Object arg1, Object arg2, Object arg3, Object arg4, Object arg5, Object arg6, Object arg7,
   1.341 +                     Object arg8, Object arg9, Object arg10, Object arg11, Object arg12, Object arg13, Object arg14,
   1.342 +                     Object arg15, Object arg16, Object arg17) throws Exception{
   1.343 +	return fn().invoke(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13, arg14, arg15,
   1.344 +	                   arg16, arg17);
   1.345 +}
   1.346 +
   1.347 +public Object invoke(Object arg1, Object arg2, Object arg3, Object arg4, Object arg5, Object arg6, Object arg7,
   1.348 +                     Object arg8, Object arg9, Object arg10, Object arg11, Object arg12, Object arg13, Object arg14,
   1.349 +                     Object arg15, Object arg16, Object arg17, Object arg18) throws Exception{
   1.350 +	return fn().invoke(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13, arg14, arg15,
   1.351 +	                   arg16, arg17, arg18);
   1.352 +}
   1.353 +
   1.354 +public Object invoke(Object arg1, Object arg2, Object arg3, Object arg4, Object arg5, Object arg6, Object arg7,
   1.355 +                     Object arg8, Object arg9, Object arg10, Object arg11, Object arg12, Object arg13, Object arg14,
   1.356 +                     Object arg15, Object arg16, Object arg17, Object arg18, Object arg19) throws Exception{
   1.357 +	return fn().invoke(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13, arg14, arg15,
   1.358 +	                   arg16, arg17, arg18, arg19);
   1.359 +}
   1.360 +
   1.361 +public Object invoke(Object arg1, Object arg2, Object arg3, Object arg4, Object arg5, Object arg6, Object arg7,
   1.362 +                     Object arg8, Object arg9, Object arg10, Object arg11, Object arg12, Object arg13, Object arg14,
   1.363 +                     Object arg15, Object arg16, Object arg17, Object arg18, Object arg19, Object arg20)
   1.364 +		throws Exception{
   1.365 +	return fn().invoke(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13, arg14, arg15,
   1.366 +	                   arg16, arg17, arg18, arg19, arg20);
   1.367 +}
   1.368 +
   1.369 +public Object invoke(Object arg1, Object arg2, Object arg3, Object arg4, Object arg5, Object arg6, Object arg7,
   1.370 +                     Object arg8, Object arg9, Object arg10, Object arg11, Object arg12, Object arg13, Object arg14,
   1.371 +                     Object arg15, Object arg16, Object arg17, Object arg18, Object arg19, Object arg20,
   1.372 +                     Object... args)
   1.373 +		throws Exception{
   1.374 +	return fn().invoke(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13, arg14, arg15,
   1.375 +	                   arg16, arg17, arg18, arg19, arg20, args);
   1.376 +}
   1.377 +
   1.378 +public Object applyTo(ISeq arglist) throws Exception{
   1.379 +	return AFn.applyToHelper(this, arglist);
   1.380 +}
   1.381 +
   1.382 +}