| 15 | import java.util.concurrent.atomic.AtomicReference; |
| 16 | |
| 17 | final public class Atom extends ARef implements IAtom2{ |
| 18 | final AtomicReference state; |
| 19 | |
| 20 | public Atom(Object state){ |
| 21 | this.state = new AtomicReference(state); |
| 22 | } |
| 23 | |
| 24 | public Atom(Object state, IPersistentMap meta){ |
| 25 | super(meta); |
| 26 | this.state = new AtomicReference(state); |
| 27 | } |
| 28 | |
| 29 | public Object deref(){ |
| 30 | return state.get(); |
| 31 | } |
| 32 | |
| 33 | public Object swap(IFn f) { |
| 34 | for(; ;) |
| 35 | { |
| 36 | Object v = deref(); |
| 37 | Object newv = f.invoke(v); |
| 38 | validate(newv); |
| 39 | if(state.compareAndSet(v, newv)) |
| 40 | { |
| 41 | notifyWatches(v, newv); |
| 42 | return newv; |
| 43 | } |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | public Object swap(IFn f, Object arg) { |
| 48 | for(; ;) |
| 49 | { |
| 50 | Object v = deref(); |
| 51 | Object newv = f.invoke(v, arg); |
| 52 | validate(newv); |
| 53 | if(state.compareAndSet(v, newv)) |
| 54 | { |
| 55 | notifyWatches(v, newv); |
| 56 | return newv; |
| 57 | } |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | public Object swap(IFn f, Object arg1, Object arg2) { |
| 62 | for(; ;) |
| 63 | { |
| 64 | Object v = deref(); |
| 65 | Object newv = f.invoke(v, arg1, arg2); |
| 66 | validate(newv); |
| 67 | if(state.compareAndSet(v, newv)) |
| 68 | { |
| 69 | notifyWatches(v, newv); |
| 70 | return newv; |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 |
nothing calls this directly
no outgoing calls
no test coverage detected