PageRenderTime 26ms CodeModel.GetById 21ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/interpreter/tags/at2dist110511/src/edu/vub/at/trace/TurnCounter.java

http://ambienttalk.googlecode.com/
Java | 98 lines | 37 code | 15 blank | 46 comment | 0 complexity | d9b3a9368fbac4388c4c53b108810002 MD5 | raw file
 1/**
 2 * AmbientTalk/2 Project
 3 * (c) Software Languages Lab, 2006 - 2009
 4 * Authors: Ambient Group at SOFT
 5 * 
 6 * The source code in this file is based on source code from Tyler Close's
 7 * Waterken server, Copyright 2008 Waterken Inc. Waterken's code is published
 8 * under the MIT license.
 9 * 
10 * Permission is hereby granted, free of charge, to any person
11 * obtaining a copy of this software and associated documentation
12 * files (the "Software"), to deal in the Software without
13 * restriction, including without limitation the rights to use,
14 * copy, modify, merge, publish, distribute, sublicense, and/or
15 * sell copies of the Software, and to permit persons to whom the
16 * Software is furnished to do so, subject to the following
17 * conditions:
18 *
19 * The above copyright notice and this permission notice shall be
20 * included in all copies or substantial portions of the Software.
21 *
22 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
24 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
26 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
27 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
29 * OTHER DEALINGS IN THE SOFTWARE.
30 */
31package edu.vub.at.trace;
32
33import java.io.Serializable;
34
35/**
36 * An event loop turn counter.
37 */
38public class
39TurnCounter implements Serializable {
40	
41	public interface FlipI {
42        public void flip();
43	}
44	
45    static private final long serialVersionUID = 1L;
46    
47    /**
48     * URI for the event loop
49     */
50    public final String loop;
51
52    /**
53     * increment the turn counter
54     */
55    public final FlipI flip;
56    
57    /**
58     * increment the anchor counter
59     */
60    public final Marker mark;
61    
62    private TurnCounter(final String loop, final FlipI flip, final Marker mark) {
63        this.loop = loop;
64        this.flip = flip;
65        this.mark = mark;
66    }
67    
68    /**
69     * Constructs an instance.
70     * @param loop  {@link #loop}
71     */
72    static public TurnCounter make(final String loop) {
73    	
74        class State implements Serializable {
75            static private final long serialVersionUID = 1L;
76
77            long turns = 0;     // id of current turn
78            long anchors = 0;   // id of next anchor
79        }
80        
81        final State m = new State();
82        
83        class Flip implements FlipI, Serializable {
84            static private final long serialVersionUID = 1L;
85
86            public void flip() {
87                m.turns += 1;
88                m.anchors = 0;
89            }
90        }
91        class Mark implements Marker, Serializable {
92            static private final long serialVersionUID = 1L;
93
94            public Anchor apply() { return new Anchor(new Turn(loop, m.turns), m.anchors++); }
95        }
96        return new TurnCounter(loop, new Flip(), new Mark());
97    }
98}