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

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