001: /*
002: * JScience - Java(TM) Tools and Libraries for the Advancement of Sciences.
003: * Copyright (C) 2006 - JScience (http://jscience.org/)
004: * All rights reserved.
005: *
006: * Permission to use, copy, modify, and distribute this software is
007: * freely granted, provided that this notice is preserved.
008: */
009: package org.jscience.geography.coordinates;
010:
011: import java.util.Date;
012:
013: import javax.measure.Measurable;
014: import javax.measure.converter.UnitConverter;
015: import javax.measure.quantity.Duration;
016: import static javax.measure.unit.SI.*;
017: import javax.measure.unit.Unit;
018:
019: import javolution.context.ObjectFactory;
020: import javolution.xml.XMLFormat;
021: import javolution.xml.stream.XMLStreamException;
022:
023: import org.jscience.geography.coordinates.crs.TemporalCRS;
024: import org.opengis.referencing.cs.CoordinateSystem;
025:
026: /**
027: * This class represents the {@link TemporalCRS temporal} UTC time coordinates.
028: *
029: * @author <a href="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a>
030: * @version 3.0, February 6, 2006
031: */
032: public final class Time extends Coordinates<TemporalCRS<?>> implements
033: Measurable<Duration> {
034:
035: /**
036: * Holds the coordinate reference system for all instances of this class.
037: */
038: public static final TemporalCRS<Time> CRS = new TemporalCRS<Time>() {
039:
040: @Override
041: protected Time coordinatesOf(AbsolutePosition position) {
042: if (position.timeUTC instanceof Time)
043: return (Time) position.timeUTC;
044: return Time.valueOf(position.timeUTC.doubleValue(SECOND),
045: SECOND);
046: }
047:
048: @Override
049: protected AbsolutePosition positionOf(Time coordinates,
050: AbsolutePosition position) {
051: position.timeUTC = coordinates;
052: return position;
053: }
054:
055: @Override
056: public CoordinateSystem getCoordinateSystem() {
057: return TemporalCRS.TIME_CS;
058: }
059:
060: };
061:
062: /**
063: * Holds the time in second since midnight, January 1, 1970 UTC.
064: */
065: private double _seconds;
066:
067: /**
068: * Returns the temporal position corresponding to the specified coordinates.
069: *
070: * @param value the time since midnight, January 1, 1970 UTC stated in the
071: * specified unit.
072: * @param unit the duration unit in which the time value is stated.
073: * @return the corresponding temporal position.
074: */
075: public static Time valueOf(double value, Unit<Duration> unit) {
076: Time time = FACTORY.object();
077: if (unit == SECOND) {
078: time._seconds = value;
079: } else {
080: UnitConverter toSecond = unit.getConverterTo(SECOND);
081: time._seconds = toSecond.convert(value);
082: }
083: return time;
084: }
085:
086: private static final ObjectFactory<Time> FACTORY = new ObjectFactory<Time>() {
087:
088: @Override
089: protected Time create() {
090: return new Time();
091: }
092: };
093:
094: private Time() {
095: }
096:
097: /**
098: * Returns the temporal position corresponding to the specified date.
099: *
100: * @param date the date.
101: * @return the corresponding temporal position.
102: */
103: public static Time valueOf(Date date) {
104: return Time.valueOf(date.getTime(), MILLI(SECOND));
105: }
106:
107: /**
108: * Creates the temporal position corresponding to the specified coordinates.
109: *
110: * @param value the time since midnight, January 1, 1970 UTC stated in the
111: * specified unit.
112: * @param unit the duration unit in which the time value is stated.
113: */
114: public Time(double value, Unit<Duration> unit) {
115: }
116:
117: @Override
118: public TemporalCRS<?> getCoordinateReferenceSystem() {
119: return CRS;
120: }
121:
122: // OpenGIS Interface.
123: public int getDimension() {
124: return 1;
125: }
126:
127: // OpenGIS Interface.
128: public double getOrdinate(int dimension)
129: throws IndexOutOfBoundsException {
130: if (dimension == 0) {
131: Unit<?> u = TemporalCRS.TIME_CS.getAxis(0).getUnit();
132: return SECOND.getConverterTo(u).convert(_seconds);
133: } else {
134: throw new IndexOutOfBoundsException();
135: }
136: }
137:
138: // Implements Scalar<Duration>
139: public final double doubleValue(Unit<Duration> unit) {
140: return unit.equals(SECOND) ? _seconds : SECOND.getConverterTo(
141: unit).convert(_seconds);
142: }
143:
144: // Implements Scalar<Duration>
145: public final long longValue(Unit<Duration> unit) {
146: return Math.round(doubleValue(unit));
147: }
148:
149: // Implements Scalar<Duration>
150: public int compareTo(Measurable<Duration> arg0) {
151: double arg0InSecond = arg0.doubleValue(SECOND);
152: return (_seconds > arg0InSecond) ? 1
153: : (_seconds < arg0InSecond) ? -1 : 0;
154: }
155:
156: // Implements Realtime.
157: public Time copy() {
158: return Time.valueOf(_seconds, SECOND);
159: }
160:
161: // Default serialization.
162: //
163:
164: static final XMLFormat<Time> XML = new XMLFormat<Time>(Time.class) {
165:
166: @Override
167: public Time newInstance(Class<Time> cls, InputElement xml)
168: throws XMLStreamException {
169: return FACTORY.object();
170: }
171:
172: public void write(Time time, OutputElement xml)
173: throws XMLStreamException {
174: xml.setAttribute("seconds", time._seconds);
175: }
176:
177: public void read(InputElement xml, Time time)
178: throws XMLStreamException {
179: time._seconds = xml.getAttribute("seconds", 0.0);
180: }
181: };
182:
183: private static final long serialVersionUID = 1L;
184:
185: }
|