Save This Page
Home » openjdk-7 » net » sf » rvpf » metadata » [javadoc | source]
    1   /**
    2    * Related Values Processing Framework.
    3    * 
    4    * Copyright (C) 2003 Serge Brisson.
    5    * 
    6    * This software is distributable under LGPL license.
    7    * See details at the bottom of this file.
    8    * 
    9    * $Header: /cvsroot/rvpf/RVPF/java/src/net/sf/rvpf/metadata/MJD.java,v 1.8 2003/11/13 14:46:09 sfb Exp $
   10    */
   11   
   12   package net.sf.rvpf.metadata;
   13   
   14   import java.io.Serializable;
   15   import java.sql.Timestamp;
   16   
   17   /** Modified Julian Day.
   18    * <p>
   19    * This class implements a time object based on the Modified Julian Day.
   20    * Its internal representation is a 64 bits integer (long) holding the
   21    * number of 100 nanoseconds since 1858-11-17 00:00.
   22    * <p>
   23    * That representation has been chosen to simplify time manipulations
   24    * in a timed event database.  The fondness of the author for OpenVMS may
   25    * also have something to do with it.
   26    * 
   27    * @author Serge Brisson
   28    * @version $Revision: 1.8 $
   29    */
   30   public class MJD implements Serializable, Comparable {
   31   
   32       // Constructors.
   33   
   34       /** Constructs a MJD for the current time.
   35        */
   36       public MJD() {
   37           this(new Timestamp(System.currentTimeMillis()));
   38       }
   39   
   40       /** Constructs a MJD for the specified time.
   41        * 
   42        * @param timestamp The specified time.
   43        */
   44       public MJD(Timestamp timestamp) {
   45           long time = timestamp.getTime() * 10000;
   46   
   47           time += (timestamp.getNanos() / 100) % 10000;
   48   
   49           this.stamp = time + EPOCH;
   50       }
   51   
   52       /** Constructs a MJD from an internal representation.
   53        * 
   54        * @param stamp The specified time.
   55        */
   56       public MJD(long stamp) {
   57           this.stamp = stamp;
   58       }
   59   
   60       // Instance Properties.
   61   
   62       /** Gets the internal representation of time.
   63        * 
   64        * @return
   65        *      A long holding the number of 100 nanoseconds
   66        *      since 1858-11-17 00:00.
   67        */
   68       public long getStamp() {
   69           return this.stamp;
   70       }
   71   
   72       /** Sets the internal representation of time.
   73        * 
   74        * @param stamp
   75        *      A long holding the number of 100 nanoseconds
   76        *      since 1858-11-17 00:00.
   77        */
   78       public void setStamp(long stamp) {
   79           this.stamp = stamp;
   80       }
   81   
   82       // Public Instance Methods.
   83   
   84       /** Creates a new MJD which would be just after this.
   85        *
   86        * @return The MJD after this.
   87        */
   88       public MJD after() {
   89           if (this.stamp == Long.MAX_VALUE)
   90               throw new RuntimeException("Cannot go after end of time");
   91   
   92           return new MJD(this.stamp + 1);
   93       }
   94   
   95       /** Creates a new MJD which would be just before this.
   96        *
   97        * @return The MJD before this.
   98        */
   99       public MJD before() {
  100           if (this.stamp == Long.MIN_VALUE)
  101               throw new RuntimeException("Cannot go before beginning of time");
  102   
  103           return new MJD(this.stamp - 1);
  104       }
  105   
  106       /** Implements Comparable.
  107        *
  108        * @param other An other PointEntity.
  109        * @return A negative integer, zero, or a positive integer.
  110        */    
  111       public int compareTo(Object other) {
  112   
  113           return new Long(this.stamp).compareTo(new Long(((MJD) other).stamp));
  114       }
  115   
  116       /** Gets the number of milliseconds since 1970-01-01 00:00:00.
  117        *
  118        * @return The number of milliseconds.
  119        */
  120       public long getMillis() {
  121           return (this.stamp - EPOCH) / 10000;
  122       }
  123   
  124       /** Gets the time in a Timestamp.
  125        * 
  126        * @return The requested Timestamp.
  127        */
  128       public Timestamp getTimestamp() {
  129           Timestamp timestamp = new Timestamp(getMillis());
  130           int nanos = (int) ((this.stamp % 10000) * 100);
  131   
  132           timestamp.setNanos(timestamp.getNanos() + nanos);
  133   
  134           return timestamp;
  135       }
  136   
  137       /** Provides a String representation.
  138        * 
  139        * @return A String in the format of {@link java.sql.Timestamp#toString}.
  140        */
  141       public String toString() {
  142           return getTimestamp().toString();
  143       }
  144   
  145       // Class Constants.
  146   
  147       public static final MJD BEGINNING_OF_TIME = new MJD(Long.MIN_VALUE);
  148       public static final MJD END_OF_TIME = new MJD(Long.MAX_VALUE);
  149   
  150       private static final long EPOCH = 0x007C95674BEB4000L;
  151   
  152       private static final long serialVersionUID = 4438419496397223075L;
  153   
  154       // Instance Attributes.
  155   
  156       /** Raw stamp: a long holding the number of 100 nanoseconds
  157        * since 1858-11-17 00:00.
  158        * 
  159        * @serial
  160        */
  161       private long stamp;
  162   }
  163   
  164   // $Log: MJD.java,v $
  165   // Revision 1.8  2003/11/13 14:46:09  sfb
  166   // Replaced 'successor' with 'after' and 'before'.
  167   //
  168   // Revision 1.7  2003/10/02 16:21:15  sfb
  169   // Added serialVersionUID.
  170   //
  171   
  172   /*
  173    * This is free software; you can redistribute it and/or modify
  174    * it under the terms of the GNU Lesser General Public License
  175    * as published by the Free Software Foundation; either version 2.1
  176    * of the License, or (at your option) any later version.
  177    *
  178    * This software is distributed in the hope that it will be useful,
  179    * but WITHOUT ANY WARRANTY; without even the implied warranty of
  180    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  181    * Lesser General Public License for more details.
  182    *
  183    * You should have received a copy of the GNU Lesser General Public
  184    * License along with this software; if not, write to the Free Software
  185    * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  186    */

Save This Page
Home » openjdk-7 » net » sf » rvpf » metadata » [javadoc | source]