| 13 | /* Alex Miller, Dec 5, 2014 */ |
| 14 | |
| 15 | public class Cycle extends ASeq implements IReduce, IPending { |
| 16 | |
| 17 | private static final long serialVersionUID = 4007270937279943908L; |
| 18 | |
| 19 | private final ISeq all; // never null |
| 20 | private final ISeq prev; |
| 21 | private volatile ISeq _current; // lazily realized |
| 22 | private volatile ISeq _next; // cached |
| 23 | |
| 24 | private Cycle(ISeq all, ISeq prev, ISeq current){ |
| 25 | this.all = all; |
| 26 | this.prev = prev; |
| 27 | this._current = current; |
| 28 | } |
| 29 | |
| 30 | private Cycle(IPersistentMap meta, ISeq all, ISeq prev, ISeq current, ISeq next){ |
| 31 | super(meta); |
| 32 | this.all = all; |
| 33 | this.prev = prev; |
| 34 | this._current = current; |
| 35 | this._next = next; |
| 36 | } |
| 37 | |
| 38 | public static ISeq create(ISeq vals){ |
| 39 | if(vals == null) |
| 40 | return PersistentList.EMPTY; |
| 41 | return new Cycle(vals, null, vals); |
| 42 | } |
| 43 | |
| 44 | // realization for use of current |
| 45 | private ISeq current() { |
| 46 | if(_current == null) { |
| 47 | ISeq current = prev.next(); |
| 48 | _current = (current == null) ? all : current; |
| 49 | } |
| 50 | return _current; |
| 51 | } |
| 52 | |
| 53 | public boolean isRealized() { |
| 54 | return _current != null; |
| 55 | } |
| 56 | |
| 57 | public Object first(){ |
| 58 | return current().first(); |
| 59 | } |
| 60 | |
| 61 | public ISeq next(){ |
| 62 | if(_next == null) |
| 63 | _next = new Cycle(all, current(), null); |
| 64 | return _next; |
| 65 | } |
| 66 | |
| 67 | public Cycle withMeta(IPersistentMap meta){ |
| 68 | if(meta() == meta) |
| 69 | return this; |
| 70 | return new Cycle(meta, all, prev, _current, _next); |
| 71 | } |
| 72 |
nothing calls this directly
no outgoing calls
no test coverage detected