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