Duration.java
001 /*
002  * Java Genetic Algorithm Library (jenetics-2.0.2).
003  * Copyright (c) 2007-2014 Franz Wilhelmstötter
004  *
005  * Licensed under the Apache License, Version 2.0 (the "License");
006  * you may not use this file except in compliance with the License.
007  * You may obtain a copy of the License at
008  *
009  *      http://www.apache.org/licenses/LICENSE-2.0
010  *
011  * Unless required by applicable law or agreed to in writing, software
012  * distributed under the License is distributed on an "AS IS" BASIS,
013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014  * See the License for the specific language governing permissions and
015  * limitations under the License.
016  *
017  * Author:
018  *    Franz Wilhelmstötter (franz.wilhelmstoetter@gmx.at)
019  */
020 package org.jenetics.util;
021 
022 import java.io.Serializable;
023 
024 /**
025  * This class models a quantity or amount of time, such as '34.5 seconds'.
026  <p>
027  <i>It is an (temporary) replacement for the the {@code Measurable<Duration>}
028  * class in the remove JScience library. Will be removed (and be replaced by the
029  * JDK version) when updated to Java 8.
030  </i>
031  *
032  @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
033  @version 2.0 &mdash; <em>$Date: 2014-04-02 $</em>
034  @since 2.0
035  */
036 public final class Duration implements Comparable<Duration>, Serializable {
037     private static final long serialVersionUID = 1L;
038 
039     private final long _nanos;
040 
041     private Duration(final long nanos) {
042         _nanos = nanos;
043     }
044 
045     /**
046      * Return the amount of this duration in nano seconds.
047      *
048      @return the amount of this duration in nano seconds.
049      */
050     public long toNanos() {
051         return _nanos;
052     }
053 
054     /**
055      * Return the amount of this duration in milli seconds.
056      *
057      @return the amount of this duration in milli seconds.
058      */
059     public long toMillis() {
060         return _nanos/1_000_000;
061     }
062 
063     /**
064      * Return the amount of this duration in seconds.
065      *
066      @return the amount of this duration in seconds.
067      */
068     public double toSeconds() {
069         return (double)_nanos/1_000_000_000.0;
070     }
071 
072     @Override
073     public int compareTo(final Duration other) {
074         int result = 0;
075         if (_nanos < other._nanos) {
076             result = -1;
077         else if (_nanos > other._nanos) {
078             result = 1;
079         }
080         return result;
081     }
082 
083     @Override
084     public int hashCode() {
085         return (int)_nanos;
086     }
087 
088     @Override
089     public boolean equals(final Object obj) {
090         if (obj == this) {
091             return true;
092         }
093         if (!(obj instanceof Duration)) {
094             return false;
095         }
096 
097         final Duration duration = (Duration)obj;
098         return duration._nanos == _nanos;
099     }
100 
101     @Override
102     public String toString() {
103         return String.format("%11.11f s", toSeconds());
104     }
105 
106     /**
107      * Create a new duration object from the given nano seconds.
108      *
109      @param nanos the amount of the new duration in nano seconds.
110      @return a new duration object from the given nano seconds
111      */
112     public static Duration ofNanos(final long nanos) {
113         return new Duration(nanos);
114     }
115 
116     /**
117      * Create a new duration object from the given milli seconds.
118      *
119      @param millis the amount of the new duration in milli seconds.
120      @return a new duration object from the given milli seconds
121      */
122     public static Duration ofMillis(final long millis) {
123         return new Duration(millis*1_000_000);
124     }
125 
126     /**
127      * Create a new duration object from the given seconds.
128      *
129      @param seconds the amount of the new duration in seconds.
130      @return a new duration object from the given seconds
131      */
132     public static Duration ofSeconds(final double seconds) {
133         return new Duration((long)(seconds*1_000_000_000));
134     }
135 
136 }