/interpreter/tags/at2dist220411/src/edu/vub/at/trace/Anchor.java

http://ambienttalk.googlecode.com/ · Java · 102 lines · 36 code · 11 blank · 55 comment · 9 complexity · 85b8c83a3ee15ec1190e5da7596aa004 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.IOException;
  33. import java.io.Serializable;
  34. /**
  35. * A marker for a point in an event loop turn where an event originated.
  36. */
  37. public class Anchor implements Comparable, Serializable {
  38. static private final long serialVersionUID = 1L;
  39. /**
  40. * event loop turn in which the event originated
  41. */
  42. public final Turn turn;
  43. /**
  44. * intra-{@linkplain #turn turn} event number
  45. */
  46. public final long number;
  47. /**
  48. * Constructs an instance.
  49. * @param turn {@link #turn}
  50. * @param number {@link #number}
  51. */
  52. public Anchor(final Turn turn, final long number) {
  53. this.turn = turn;
  54. this.number = number;
  55. }
  56. // org.joe_e.Selfless interface
  57. public boolean equals(final Object o) {
  58. return null != o && Anchor.class == o.getClass() &&
  59. number == ((Anchor)o).number &&
  60. (null!=turn?turn.equals(((Anchor)o).turn):null==((Anchor)o).turn);
  61. }
  62. public int
  63. hashCode() {
  64. return (null != turn ? turn.hashCode() : 0) +
  65. (int)(number ^ (number >>> 32)) +
  66. 0x7C42A2C4;
  67. }
  68. // java.lang.Comparable interface
  69. public int compareTo(final Object o) {
  70. if (!(o instanceof Anchor)) { throw new IllegalArgumentException(); };
  71. final int major = turn.compareTo(((Anchor)o).turn);
  72. if (0 != major) { return major; }
  73. final long minor = number - ((Anchor)o).number;
  74. return minor < 0L ? -1 : minor == 0L ? 0 : 1;
  75. }
  76. /**
  77. * "anchor" : {
  78. * "number" : n,
  79. * "turn" : {
  80. * "loop" : "event-loop-name",
  81. * "number" : n
  82. * }
  83. * }
  84. */
  85. public void toJSON(JSONWriter json) throws IOException {
  86. JSONWriter.ObjectWriter anchor = json.startObject();
  87. anchor.startMember("number").writeLong(number);
  88. turn.toJSON(anchor.startMember("turn"));
  89. anchor.finish();
  90. }
  91. }