1 9 package org.jscience.geography.coordinates; 10 11 import java.util.Date ; 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 28 public class Time extends Coordinates<TemporalCRS> implements Quantity<Duration> { 29 30 33 public static final TemporalCRS<Time> CRS = new TemporalCRS<Time>() { 34 35 @Override 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 44 protected AbsolutePosition positionOf(Time coordinates, AbsolutePosition position) { 45 position.timeUTC = coordinates; 46 return position; 47 } 48 49 @Override 50 public CoordinateSystem getCoordinateSystem() { 51 return TemporalCRS.TIME_CS; 52 } 53 54 }; 55 56 59 private double _timeInSecond; 60 61 69 public static Time valueOf(double value, Unit<Duration> unit) { 70 return new Time(value, unit); 71 } 72 73 79 public static Time valueOf(Date date) { 80 return new Time(date.getTime(), SI.MILLI(SI.SECOND)); 81 } 82 83 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 100 public TemporalCRS getCoordinateReferenceSystem() { 101 return CRS; 102 } 103 104 public int getDimension() { 106 return 1; 107 } 108 109 public double getOrdinate(int dimension) throws IndexOutOfBoundsException { 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 (); 116 } 117 } 118 119 public final double doubleValue(Unit<Duration> unit) { 121 return unit.equals(SI.SECOND) ? _timeInSecond : SI.SECOND 122 .getConverterTo(unit).convert(_timeInSecond); 123 } 124 125 public final long longValue(Unit<Duration> unit) { 127 return Math.round(doubleValue(unit)); 128 } 129 130 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 |