KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jscience > physics > quantities > Duration


1 /*
2  * JScience - Java(TM) Tools and Libraries for the Advancement of Sciences.
3  * Copyright (C) 2005 - JScience (http://jscience.org/)
4  * All rights reserved.
5  *
6  * Permission to use, copy, modify, and distribute this software is
7  * freely granted, provided that this notice is preserved.
8  */

9 package org.jscience.physics.quantities;
10
11 import java.util.Date JavaDoc;
12
13 import org.jscience.physics.units.SI;
14 import org.jscience.physics.units.Unit;
15
16 /**
17  * This class represents a period of existence or persistence. The system
18  * unit for this quantity is "s" (Système International d'Unités).
19  *
20  * @author <a HREF="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a>
21  * @version 1.0, October 24, 2004
22  */

23 public class Duration extends Quantity {
24
25     /**
26      * Holds the associated unit.
27      */

28     private final static Unit<Duration> UNIT = SI.SECOND;
29
30     /**
31      * Holds the factory for this class.
32      */

33     private final static Factory<Duration> FACTORY = new Factory<Duration>(UNIT) {
34         protected Duration create() {
35             return new Duration();
36         }
37     };
38
39     /**
40      * Represents a {@link Duration} amounting to nothing.
41      */

42     public final static Duration ZERO = Quantity.valueOf(0, UNIT);
43
44     /**
45      * Default constructor (allows for derivation).
46      */

47     protected Duration() {
48     }
49
50     /**
51      * Shows {@link Duration} instances in the specified unit.
52      *
53      * @param unit the display unit for {@link Duration} instances.
54      */

55     public static void showAs(Unit unit) {
56         QuantityFormat.show(Duration.class, unit);
57     }
58
59     ///////////////////////
60
// DURATION SPECIFIC //
61
///////////////////////
62

63     /**
64      * Returns the {@link Duration} from the ellapsed time between two dates.
65      *
66      * @param from the departure date.
67      * @param to the arrival date.
68      * @return the ellapsed time between the specified dates.
69      */

70     public static Duration between(Date JavaDoc from, Date JavaDoc to) {
71         return Quantity.valueOf(from.getTime() - to.getTime(), 0.5, SI
72                 .MILLI(SI.SECOND));
73     }
74
75     /**
76      * Returns the date after the specified date. How long after being
77      * specified by this {@link Duration}.
78      *
79      * @param date the date of origin.
80      * @return <code>date + this</code>.
81      */

82     public Date JavaDoc addTo(Date JavaDoc date) {
83         return new Date JavaDoc(date.getTime()
84                 + this.to(SI.MILLI(SI.SECOND)).longValue());
85     }
86
87     /**
88      * Returns the date before the specified date. How long before being
89      * specified by this {@link Duration}.
90      *
91      * @param date the date of origin.
92      * @return <code>date - this</code>
93      */

94     public Date JavaDoc subtractFrom(Date JavaDoc date) {
95         return new Date JavaDoc(date.getTime()
96                 - this.to(SI.MILLI(SI.SECOND)).longValue());
97     }
98
99     private static final long serialVersionUID = 1L;
100 }
101
Popular Tags