KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jscience > geography > coordinates > Time


1 /*
2  * JScience - Java(TM) Tools and Libraries for the Advancement of Sciences.
3  * Copyright (C) 2006 - 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.geography.coordinates;
10
11 import java.util.Date JavaDoc;
12
13 import javax.measure.converters.UnitConverter;
14 import javax.measure.quantities.Duration;
15 import javax.measure.quantities.Quantity;
16 import javax.measure.units.SI;
17 import javax.measure.units.Unit;
18
19 import org.jscience.geography.coordinates.crs.TemporalCRS;
20 import org.opengis.referencing.cs.CoordinateSystem;
21
22 /**
23  * This class represents the {@link TemporalCRS temporal} UTC time coordinates.
24  *
25  * @author <a HREF="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a>
26  * @version 3.0, February 6, 2006
27  */

28 public class Time extends Coordinates<TemporalCRS> implements Quantity<Duration> {
29
30     /**
31      * Holds the coordinate reference system for all instances of this class.
32      */

33     public static final TemporalCRS<Time> CRS = new TemporalCRS<Time>() {
34
35         @Override JavaDoc
36         protected Time coordinatesOf(AbsolutePosition position) {
37             if (position.timeUTC instanceof Time)
38                 return (Time) position.timeUTC;
39             return Time.valueOf(position.timeUTC.doubleValue(SI.SECOND),
40                     SI.SECOND);
41         }
42
43         @Override JavaDoc
44         protected AbsolutePosition positionOf(Time coordinates, AbsolutePosition position) {
45             position.timeUTC = coordinates;
46             return position;
47         }
48
49         @Override JavaDoc
50         public CoordinateSystem getCoordinateSystem() {
51             return TemporalCRS.TIME_CS;
52         }
53         
54     };
55
56     /**
57      * Holds the time in second since midnight, January 1, 1970 UTC.
58      */

59     private double _timeInSecond;
60
61     /**
62      * Returns the temporal position corresponding to the specified coordinates.
63      *
64      * @param value the time since midnight, January 1, 1970 UTC stated in the
65      * specified unit.
66      * @param unit the duration unit in which the time value is stated.
67      * @return the corresponding temporal position.
68      */

69     public static Time valueOf(double value, Unit<Duration> unit) {
70         return new Time(value, unit);
71     }
72
73     /**
74      * Returns the temporal position corresponding to the specified date.
75      *
76      * @param date the date.
77      * @return the corresponding temporal position.
78      */

79     public static Time valueOf(Date JavaDoc date) {
80         return new Time(date.getTime(), SI.MILLI(SI.SECOND));
81     }
82
83     /**
84      * Creates the temporal position corresponding to the specified coordinates.
85      *
86      * @param value the time since midnight, January 1, 1970 UTC stated in the
87      * specified unit.
88      * @param unit the duration unit in which the time value is stated.
89      */

90     public Time(double value, Unit<Duration> unit) {
91         if (unit == SI.SECOND) {
92             _timeInSecond = value;
93         } else {
94             UnitConverter toSecond = unit.getConverterTo(SI.SECOND);
95             _timeInSecond = toSecond.convert(value);
96         }
97     }
98
99     @Override JavaDoc
100     public TemporalCRS getCoordinateReferenceSystem() {
101         return CRS;
102     }
103
104     // OpenGIS Interface.
105
public int getDimension() {
106         return 1;
107     }
108
109     // OpenGIS Interface.
110
public double getOrdinate(int dimension) throws IndexOutOfBoundsException JavaDoc {
111         if (dimension == 0) {
112             Unit u = TemporalCRS.TIME_CS.getAxis(0).getUnit();
113             return SI.SECOND.getConverterTo(u).convert(_timeInSecond);
114         } else {
115             throw new IndexOutOfBoundsException JavaDoc();
116         }
117     }
118
119     // Implements Quantity<Duration>
120
public final double doubleValue(Unit<Duration> unit) {
121         return unit.equals(SI.SECOND) ? _timeInSecond : SI.SECOND
122                 .getConverterTo(unit).convert(_timeInSecond);
123     }
124
125     // Implements Quantity<Duration>
126
public final long longValue(Unit<Duration> unit) {
127         return Math.round(doubleValue(unit));
128     }
129
130     // Implements Quantity<Duration>
131
public int compareTo(Quantity<Duration> arg0) {
132         double arg0InSecond = arg0.doubleValue(SI.SECOND);
133         return (_timeInSecond > arg0InSecond) ? 1
134                 : (_timeInSecond < arg0InSecond) ? -1 : 0;
135     }
136
137 }
Popular Tags