Time.java
Go to the documentation of this file.
00001 /*
00002  * Software License Agreement (BSD License)
00003  * 
00004  * Copyright (c) 2008, Willow Garage, Inc. All rights reserved.
00005  * 
00006  * Redistribution and use in source and binary forms, with or without
00007  * modification, are permitted provided that the following conditions are met:
00008  * 
00009  * * Redistributions of source code must retain the above copyright notice, this
00010  * list of conditions and the following disclaimer. * Redistributions in binary
00011  * form must reproduce the above copyright notice, this list of conditions and
00012  * the following disclaimer in the documentation and/or other materials provided
00013  * with the distribution. * Neither the name of Willow Garage, Inc. nor the
00014  * names of its contributors may be used to endorse or promote products derived
00015  * from this software without specific prior written permission.
00016  * 
00017  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00018  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00021  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00022  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00023  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00024  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00025  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00026  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00027  * POSSIBILITY OF SUCH DAMAGE.
00028  */
00029 
00030 package org.ros.message;
00031 
00041 public class Time implements Comparable<Time> {
00042 
00043   public int secs;
00044   public int nsecs;
00045 
00046   public Time() {
00047     secs = 0;
00048     nsecs = 0;
00049   }
00050 
00051   public Time(int secs, int nsecs) {
00052     this.secs = secs;
00053     this.nsecs = nsecs;
00054     normalize();
00055   }
00056 
00057   public Time(double secs) {
00058     this.secs = (int) secs;
00059     this.nsecs = (int) ((secs - this.secs) * 1000000000);
00060     normalize();
00061   }
00062 
00063   public Time(Time t) {
00064     this.secs = t.secs;
00065     this.nsecs = t.nsecs;
00066   }
00067 
00068   public Time add(Duration d) {
00069     return new Time(secs + d.secs, nsecs + d.nsecs);
00070   }
00071 
00072   public Time subtract(Duration d) {
00073     return new Time(secs - d.secs, nsecs - d.nsecs);
00074   }
00075 
00076   public Duration subtract(Time t) {
00077     return new Duration(secs - t.secs, nsecs - t.nsecs);
00078   }
00079 
00080   public static Time fromMillis(long timeInMillis) {
00081     int secs = (int) (timeInMillis / 1000);
00082     int nsecs = (int) (timeInMillis % 1000) * 1000000;
00083     return new Time(secs, nsecs);
00084   }
00085 
00086   public static Time fromNano(long timeInNs) {
00087     int secs = (int) (timeInNs / 1000000000);
00088     int nsecs = (int) (timeInNs % 1000000000);
00089     return new Time(secs, nsecs);
00090   }
00091 
00092   @Override
00093   public String toString() {
00094     return secs + ":" + nsecs;
00095   }
00096 
00097   public double toSeconds() {
00098     return totalNsecs() / 1e9;
00099   }
00100 
00101   public long totalNsecs() {
00102     return ((long) secs) * 1000000000 + nsecs;
00103   }
00104 
00105   public boolean isZero() {
00106     return totalNsecs() == 0;
00107   }
00108 
00109   public void normalize() {
00110     while (nsecs < 0) {
00111       nsecs += 1000000000;
00112       secs -= 1;
00113     }
00114     while (nsecs >= 1000000000) {
00115       nsecs -= 1000000000;
00116       secs += 1;
00117     }
00118   }
00119 
00120   @Override
00121   public int hashCode() {
00122     final int prime = 31;
00123     int result = 1;
00124     result = prime * result + nsecs;
00125     result = prime * result + secs;
00126     return result;
00127   }
00128 
00135   @Override
00136   public boolean equals(Object obj) {
00137     if (this == obj) return true;
00138     if (obj == null) return false;
00139     if (getClass() != obj.getClass()) return false;
00140     Time other = (Time) obj;
00141     if (nsecs != other.nsecs) return false;
00142     if (secs != other.secs) return false;
00143     return true;
00144   }
00145 
00146   @Override
00147   public int compareTo(Time t) {
00148     if ((secs > t.secs) || ((secs == t.secs) && nsecs > t.nsecs)) {
00149       return 1;
00150     }
00151     if ((secs == t.secs) && (nsecs == t.nsecs)) {
00152       return 0;
00153     }
00154     return -1;
00155   }
00156 }


rosjava_bootstrap
Author(s): Daniel Stonier , Damon Kohler
autogenerated on Fri Aug 28 2015 12:41:44