/interpreter/tags/at2dist090708/src/edu/vub/at/actors/id/ATObjectID.java

http://ambienttalk.googlecode.com/ · Java · 122 lines · 53 code · 18 blank · 51 comment · 5 complexity · 26b3f71c6fba44baacc2f038a762ded5 MD5 · raw file

  1. /**
  2. * AmbientTalk/2 Project
  3. * ATObjectID.java created on 21-dec-2006 at 12:04:03
  4. * (c) Programming Technology Lab, 2006 - 2007
  5. * Authors: Tom Van Cutsem & Stijn Mostinckx
  6. *
  7. * Permission is hereby granted, free of charge, to any person
  8. * obtaining a copy of this software and associated documentation
  9. * files (the "Software"), to deal in the Software without
  10. * restriction, including without limitation the rights to use,
  11. * copy, modify, merge, publish, distribute, sublicense, and/or
  12. * sell copies of the Software, and to permit persons to whom the
  13. * Software is furnished to do so, subject to the following
  14. * conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be
  17. * included in all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  20. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  21. * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  22. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  23. * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  24. * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  25. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  26. * OTHER DEALINGS IN THE SOFTWARE.
  27. */
  28. package edu.vub.at.actors.id;
  29. import edu.vub.at.actors.natives.ELActor;
  30. import edu.vub.at.actors.natives.ELVirtualMachine;
  31. import edu.vub.at.exceptions.InterpreterException;
  32. import java.io.Serializable;
  33. import java.util.Random;
  34. /**
  35. * An ATObjectID instance represents a globally unique identifier denoting
  36. * an AmbientTalk/2 object. It is represented by a triplet:
  37. * - object ID (uniquely identifying an object within one actor)
  38. * - actor ID (uniquely identifying an object within
  39. * - virtual machine ID (uniquely identifying a virtual machine in the network)
  40. *
  41. * @author tvcutsem
  42. */
  43. public class ATObjectID implements Serializable {
  44. private static final long serialVersionUID = -2108704271907943887L;
  45. private static final Random generator_ = new Random(System.currentTimeMillis());
  46. private final VirtualMachineID virtualMachineId_;
  47. private final ActorID actorId_;
  48. private final long objectId_;
  49. private final String description_;
  50. /**
  51. * Creates a new unique object identifier for an object that lives on the given
  52. * VM and is hosted by the given actor. The description parameter is used in
  53. * the printed representation of far references.
  54. */
  55. public ATObjectID(VirtualMachineID vmId, ActorID actorId, String description) {
  56. virtualMachineId_ = vmId;
  57. actorId_ = actorId;
  58. objectId_ = generator_.nextLong();
  59. description_ = description;
  60. }
  61. /*
  62. * Three cases to consider:
  63. * an object can be:
  64. * - local to an actor (isFar -> false, isRemote -> false)
  65. * - hosted by another local actor (isFar -> true, isRemote -> false)
  66. * - hosted by a remote actor (isFar -> true, isRemote -> true)
  67. */
  68. public boolean isFar() throws InterpreterException {
  69. return (!actorId_.equals(ELActor.currentActor().getActorID()));
  70. }
  71. public boolean isRemote() {
  72. return (!virtualMachineId_.equals(ELVirtualMachine.currentVM().getGUID()));
  73. }
  74. /* -----------------------
  75. * -- Structural Access --
  76. * ----------------------- */
  77. public ActorID getActorId() {
  78. return actorId_;
  79. }
  80. public VirtualMachineID getVirtualMachineId() {
  81. return virtualMachineId_;
  82. }
  83. public int hashCode() {
  84. return virtualMachineId_.hashCode() |
  85. actorId_.hashCode() |
  86. (int) objectId_;
  87. }
  88. public boolean equals(Object other) {
  89. if (other instanceof ATObjectID) {
  90. ATObjectID id = (ATObjectID) other;
  91. return (id.getVirtualMachineId().equals(virtualMachineId_)
  92. && (id.getActorId().equals(actorId_))
  93. && (id.objectId_ == objectId_));
  94. } else {
  95. return false;
  96. }
  97. }
  98. public String getDescription() {
  99. return description_;
  100. }
  101. public String toString() {
  102. return virtualMachineId_ + "|" + actorId_ + "|" + objectId_;
  103. }
  104. }