PageRenderTime 32ms CodeModel.GetById 18ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 1ms

/interpreter/tags/at2dist091109/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 */
 28package edu.vub.at.actors.id;
 29
 30
 31import edu.vub.at.actors.natives.ELActor;
 32import edu.vub.at.actors.natives.ELVirtualMachine;
 33import edu.vub.at.exceptions.InterpreterException;
 34
 35import java.io.Serializable;
 36import java.util.Random;
 37
 38/**
 39 * An ATObjectID instance represents a globally unique identifier denoting
 40 * an AmbientTalk/2 object. It is represented by a triplet:
 41 *  - object ID (uniquely identifying an object within one actor)
 42 *  - actor ID (uniquely identifying an object within 
 43 *  - virtual machine ID (uniquely identifying a virtual machine in the network)
 44 *
 45 * @author tvcutsem
 46 */
 47public class ATObjectID implements Serializable {
 48
 49	private static final long serialVersionUID = -2108704271907943887L;
 50
 51	private static final Random generator_ = new Random(System.currentTimeMillis());
 52	
 53	private final VirtualMachineID virtualMachineId_;
 54	private final ActorID actorId_;
 55	private final long objectId_;
 56	private final String description_;
 57	
 58	/**
 59	 * Creates a new unique object identifier for an object that lives on the given
 60	 * VM and is hosted by the given actor. The description parameter is used in
 61	 * the printed representation of far references.
 62	 */
 63	public ATObjectID(VirtualMachineID vmId, ActorID actorId, String description) {
 64		virtualMachineId_ = vmId;
 65		actorId_ = actorId;
 66		objectId_ = generator_.nextLong();
 67		description_ = description;
 68	}
 69	
 70	/*
 71	 * Three cases to consider:
 72	 *  an object can be:
 73	 *   - local to an actor (isFar -> false, isRemote -> false)
 74	 *   - hosted by another local actor (isFar -> true, isRemote -> false)
 75	 *   - hosted by a remote actor (isFar -> true, isRemote -> true)
 76	 */
 77	public boolean isFar() throws InterpreterException {
 78		return (!actorId_.equals(ELActor.currentActor().getActorID()));
 79	}
 80	
 81	public boolean isRemote() {
 82		return (!virtualMachineId_.equals(ELVirtualMachine.currentVM().getGUID()));
 83	}
 84	
 85	/* -----------------------
 86	 * -- Structural Access --
 87	 * ----------------------- */
 88	
 89	public ActorID getActorId() {
 90		return actorId_;
 91	}
 92
 93	public VirtualMachineID getVirtualMachineId() {
 94		return virtualMachineId_;
 95	}
 96	
 97	public int hashCode() {
 98		return virtualMachineId_.hashCode() |
 99		       actorId_.hashCode() |
100		       (int) objectId_;
101	}
102	
103	public boolean equals(Object other) {
104		if (other instanceof ATObjectID) {
105			ATObjectID id = (ATObjectID) other;
106			return (id.getVirtualMachineId().equals(virtualMachineId_)
107					&& (id.getActorId().equals(actorId_))
108					&& (id.objectId_ == objectId_));
109		} else {
110			return false;
111		}
112	}
113	
114	public String getDescription() {
115		return description_;
116	}
117	
118	public String toString() {
119		return virtualMachineId_ + "|" + actorId_ + "|" + objectId_;
120	}
121	
122}