00001 /* 00002 * Copyright (c) 2008, AIST, the University of Tokyo and General Robotix Inc. 00003 * All rights reserved. This program is made available under the terms of the 00004 * Eclipse Public License v1.0 which accompanies this distribution, and is 00005 * available at http://www.eclipse.org/legal/epl-v10.html 00006 * Contributors: 00007 * General Robotix Inc. 00008 * National Institute of Advanced Industrial Science and Technology (AIST) 00009 */ 00017 package com.generalrobotix.ui.view.graph; 00018 00019 import java.text.DecimalFormat; 00020 00021 public class Time { 00022 int msec_; 00023 int usec_; 00024 DecimalFormat format_ = new DecimalFormat("000"); 00025 00029 public Time() { 00030 msec_ = 0; 00031 usec_ = 0; 00032 } 00033 00039 public Time(int m, int u) { 00040 msec_ = m; 00041 usec_ = u; 00042 } 00043 00048 public Time(double time) { 00049 set(time); 00050 } 00051 00052 public Time(float time) { 00053 set(time); 00054 } 00055 00056 public Time(Time time) { 00057 set(time); 00058 } 00059 00064 public Time(long time) { 00065 setUtime(time); 00066 } 00067 00072 public void set(Time time) { 00073 msec_ = time.msec_; 00074 usec_ = time.usec_; 00075 } 00076 00081 public void set(double time) { 00082 long utime = (long)Math.round(time * 1000000.0); 00083 usec_ = (int)(utime % 1000); 00084 msec_ = (int)(utime / 1000); 00085 } 00086 00087 public void set(float time) { 00088 long utime = (long)Math.round((double)time * 1000000.0); 00089 usec_ = (int)(utime % 1000); 00090 msec_ = (int)(utime / 1000); 00091 } 00092 00098 public void set(int m, int u) { 00099 msec_ = m; 00100 usec_ = u; 00101 } 00102 00107 public void add(Time time) { 00108 msec_ += time.msec_; 00109 usec_ += time.usec_; 00110 if (usec_ >= 1000) { 00111 msec_ += usec_ / 1000; 00112 usec_ = usec_ % 1000; 00113 } 00114 } 00119 public void sub(Time time) { 00120 msec_ -= time.msec_; 00121 usec_ -= time.usec_; 00122 if (usec_ < 0) { 00123 msec_ += usec_ / 1000 - 1 ; 00124 usec_ = usec_ % 1000; 00125 } 00126 } 00127 00128 public void addUtime(int utime) { 00129 usec_ += utime; 00130 if (usec_ >= 1000) { 00131 msec_ += usec_ / 1000; 00132 usec_ = usec_ % 1000; 00133 } 00134 } 00135 00140 public double getDouble() { 00141 return (double)msec_ * 0.001 + (double)usec_ * 0.000001; 00142 } 00143 00147 public float getFloat() { 00148 return (float)msec_ * 0.001f + (float)usec_ * 0.000001f; 00149 } 00150 00151 public boolean compare(Time time) { 00152 if ((msec_ == time.msec_) && (usec_ == time.usec_)) return true; 00153 else return false; 00154 } 00155 00156 public long getUtime() { 00157 return (long)msec_ * 1000L + (long)usec_; 00158 } 00159 00160 public void setUtime(long utime) { 00161 msec_ = (int)(utime / 1000); 00162 usec_ = (int)utime % 1000; 00163 } 00164 00165 public String toString() { 00166 StringBuffer buf = new StringBuffer(); 00167 buf.append(String.valueOf(msec_ / 1000)); 00168 buf.append('.'); 00169 buf.append(format_.format(msec_ % 1000)); 00170 buf.append(format_.format(usec_ % 1000)); 00171 return buf.toString(); 00172 } 00173 }