PageRenderTime 13ms CodeModel.GetById 4ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

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