PageRenderTime 14ms CodeModel.GetById 9ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

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