/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. */
  31. package edu.vub.at.trace;
  32. import java.io.Serializable;
  33. /**
  34. * An event loop turn counter.
  35. */
  36. public class
  37. TurnCounter implements Serializable {
  38. public interface FlipI {
  39. public void flip();
  40. }
  41. static private final long serialVersionUID = 1L;
  42. /**
  43. * URI for the event loop
  44. */
  45. public final String loop;
  46. /**
  47. * increment the turn counter
  48. */
  49. public final FlipI flip;
  50. /**
  51. * increment the anchor counter
  52. */
  53. public final Marker mark;
  54. private TurnCounter(final String loop, final FlipI flip, final Marker mark) {
  55. this.loop = loop;
  56. this.flip = flip;
  57. this.mark = mark;
  58. }
  59. /**
  60. * Constructs an instance.
  61. * @param loop {@link #loop}
  62. */
  63. static public TurnCounter make(final String loop) {
  64. class State implements Serializable {
  65. static private final long serialVersionUID = 1L;
  66. long turns = 0; // id of current turn
  67. long anchors = 0; // id of next anchor
  68. }
  69. final State m = new State();
  70. class Flip implements FlipI, Serializable {
  71. static private final long serialVersionUID = 1L;
  72. public void flip() {
  73. m.turns += 1;
  74. m.anchors = 0;
  75. }
  76. }
  77. class Mark implements Marker, Serializable {
  78. static private final long serialVersionUID = 1L;
  79. public Anchor apply() { return new Anchor(new Turn(loop, m.turns), m.anchors++); }
  80. }
  81. return new TurnCounter(loop, new Flip(), new Mark());
  82. }
  83. }