Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/metacosm/framework/world/Date.java


1   /*
2       Metacosm, an object-oriented network game framework
3       Copyright (C) 1999-2001 Metacosm Development Team
4   
5       This program is free software; you can redistribute it and/or modify
6       it under the terms of the GNU General Public License as published by
7       the Free Software Foundation; either version 2 of the License, or
8       (at your option) any later version.
9   
10      This program is distributed in the hope that it will be useful,
11      but WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13      GNU General Public License for more details.
14  
15      You should have received a copy of the GNU General Public License
16      along with this program; if not, write to the Free Software
17      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  */
19  
20  package org.metacosm.framework.world;
21  
22  import org.metacosm.framework.server.GameClock;
23  
24  /**
25   * Date is a common representation for time in all the worlds.<br> This has
26   * nothing to do with seconds, minutes, hours, days, weeks, months, seasons,
27   * years, decades, centuries, millenaries, eras, eons, etc. These notions
28   * suppose a planet with one sun, a week of 7 days, etc. Each world should
29   * provide its own Date converter.
30   *
31   * Date contains a timestamp given by the game clock at its creation.
32   *
33   * BE CAREFUL: java.util.Date and org.metacosm.framework.world.Date should not
34   * be mixed up.
35   */
36  
37  public class Date implements java.lang.Cloneable,
38                               java.io.Serializable,
39                               java.lang.Comparable {
40    /**
41     * Allocates a Date object and initializes it so that it represents the time
42     *  (given by the game clock) at which it was allocated.
43     */
44    public Date() {
45      timestamp = GameClock.getInstance().getTime();
46    }
47  
48    /**
49     * Allocates a Date object and initializes it so that it represents the time
50     * after <i>timestamp</i> ticks.
51     */
52    public Date( long timestamp) {
53      if (timestamp < 0) {
54        throw new java.lang.IllegalArgumentException();
55      }
56      this.timestamp = timestamp;
57    }
58  
59    /**
60     * Tests if this date is after the specified date.
61     */
62    public final boolean after( Date when) {
63      return (this.timestamp > when.timestamp);
64    }
65  
66    /**
67     * Tests if this date is before the specified date.
68     */
69    public final boolean before( Date when) {
70      return (this.timestamp < when.timestamp);
71    }
72  
73    /**
74     * Compares this Date to another Object.<br>
75     * If the Object is a Date, this function behaves like compareTo(Date).
76     * Otherwise, it throws a ClassCastException (as Dates are comparable only to
77     * other Dates).
78     */
79    public final int compareTo( Object o) {
80      Date d = (Date)o;
81      return (this.timestamp < d.timestamp ? -1 : (this.timestamp == d.timestamp ? 0 : 1));
82    }
83  
84    /**
85     * Compares two Dates for ordering.
86     */
87    public final int compareTo( Date date) {
88      return (this.timestamp < date.timestamp ? -1 : (this.timestamp == date.timestamp ? 0 : 1));
89    }
90  
91    /**
92     * Compares two dates for equality.
93     */
94    public final boolean equals(Object obj) {
95      return (obj != null && obj instanceof Date
96          && this.timestamp == ((Date) obj).timestamp);
97    }
98  
99    /**
100    * @return the number of ticks since clock has started
101    */
102   public final long getTicks() {
103     return timestamp;
104   }
105 
106   /**
107    * Sets this Date object to represent a point in time that is <i>ticks</i>
108    * ticks after game clock started
109    */
110   public void setTicks( long ticks) {
111     if (ticks < 0)
112     {
113       throw new java.lang.IllegalArgumentException();
114     }
115     timestamp = ticks;
116   }
117 
118   /**
119    * @return a string representation of this date.
120    */
121   public String toString() {
122     return new String("Tick " + timestamp);
123   }
124 
125   private long timestamp;
126 }