PageRenderTime 29ms CodeModel.GetById 15ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://ambienttalk.googlecode.com/
Java | 114 lines | 47 code | 19 blank | 48 comment | 7 complexity | eb4fc0371376dbf560b20a6a584e073f 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.ELVirtualMachine;
 32import edu.vub.at.exceptions.InterpreterException;
 33import edu.vub.at.objects.natives.OBJLexicalRoot;
 34
 35import java.io.Serializable;
 36
 37/**
 38 * An ATObjectID instance represents a globally unique identifier denoting
 39 * an AmbientTalk/2 object. It is represented by a triplet:
 40 *  ( VM id, Host actor id, object id )
 41 *
 42 * @author tvcutsem
 43 */
 44public class ATObjectID implements Serializable {
 45
 46	private static final long serialVersionUID = -2108704271907943887L;
 47
 48	// Virtual Machines are identified by IP address + port
 49	private final GUID virtualMachineId_;
 50	
 51	// Inside a Virtual Machine actors (which accept the messages for this far object)
 52	// are denoted by their hashcode
 53	private final int actorId_;
 54	
 55	// Inside an NATActorMirror, objects are denoted by their hashcode
 56	private final int objectId_;
 57	
 58	public ATObjectID(GUID vmId, int actorId, int objectId) {
 59		virtualMachineId_ = vmId;
 60		actorId_ = actorId;
 61		objectId_ = objectId;
 62	}
 63	
 64	/*
 65	 * Three cases to consider:
 66	 *  an object can be:
 67	 *   - local to an actor (isFar -> false, isRemote -> false)
 68	 *   - hosted by another local actor (isFar -> true, isRemote -> false)
 69	 *   - hosted by a remote actor (isFar -> true, isRemote -> true)
 70	 */
 71	public boolean isFar() throws InterpreterException {
 72		return (actorId_ != OBJLexicalRoot._INSTANCE_.base_getActor().hashCode());
 73	}
 74	
 75	public boolean isRemote() {
 76		return (!virtualMachineId_.equals(ELVirtualMachine.currentVM().getGUID()));
 77	}
 78	
 79	/* -----------------------
 80	 * -- Structural Access --
 81	 * ----------------------- */
 82	
 83	public int getActorId() {
 84		return actorId_;
 85	}
 86
 87	public int getObjectId() {
 88		return objectId_;
 89	}
 90
 91	public GUID getVirtualMachineId() {
 92		return virtualMachineId_;
 93	}
 94	
 95	public int hashCode() {
 96		return virtualMachineId_.hashCode() | actorId_ | objectId_;
 97	}
 98	
 99	public boolean equals(Object other) {
100		if (other instanceof ATObjectID) {
101			ATObjectID id = (ATObjectID) other;
102			return (id.getVirtualMachineId().equals(virtualMachineId_)
103					&& (id.getActorId() == actorId_)
104					&& (id.getObjectId() == objectId_));
105		} else {
106			return false;
107		}
108	}
109	
110	public String toString() {
111		return virtualMachineId_.toString() + ":" + actorId_ + ":" + objectId_;
112	}
113	
114}