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 javolution.context.ObjectFactory;
012: import javolution.xml.XMLFormat;
013: import javolution.xml.stream.XMLStreamException;
014:
015: import org.jscience.geography.coordinates.crs.CompoundCRS;
016: import org.jscience.geography.coordinates.crs.CoordinateReferenceSystem;
017:
018: /**
019: * This class represents a coordinates made up by combining
020: * two coordinates objects together.
021: *
022: * @author <a href="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a>
023: * @version 3.0, February 13, 2006
024: */
025: public final class CompoundCoordinates<C1 extends Coordinates<?>, C2 extends Coordinates<?>>
026: extends Coordinates<CompoundCRS<C1, C2>> {
027:
028: /**
029: * Holds the first coordinates.
030: */
031: private C1 _first;
032:
033: /**
034: * Holds the next coordinates.
035: */
036: private C2 _next;
037:
038: /**
039: * Returns a compound coordinates made up of the specified coordinates.
040: *
041: * @param first the first coordinates.
042: * @param next the next coordinates.
043: */
044: @SuppressWarnings("unchecked")
045: public static <T1 extends Coordinates<?>, T2 extends Coordinates<?>> CompoundCoordinates<T1, T2> valueOf(
046: T1 first, T2 next) {
047: CompoundCoordinates coord = FACTORY.object();
048: coord._first = first;
049: coord._next = next;
050: return coord;
051: }
052:
053: @SuppressWarnings("unchecked")
054: private static final ObjectFactory<CompoundCoordinates> FACTORY = new ObjectFactory<CompoundCoordinates>() {
055:
056: @Override
057: protected CompoundCoordinates create() {
058: return new CompoundCoordinates();
059: }
060:
061: };
062:
063: private CompoundCoordinates() {
064: }
065:
066: /**
067: * Returns the first coordinates.
068: *
069: * @return the first coordinates.
070: */
071: public C1 getFirst() {
072: return _first;
073: }
074:
075: /**
076: * Returns the next coordinates.
077: *
078: * @return the next coordinates.
079: */
080: public C2 getNext() {
081: return _next;
082: }
083:
084: @SuppressWarnings("unchecked")
085: @Override
086: public CompoundCRS<C1, C2> getCoordinateReferenceSystem() {
087: return new CompoundCRS<C1, C2>(
088: (CoordinateReferenceSystem<C1>) _first
089: .getCoordinateReferenceSystem(),
090: (CoordinateReferenceSystem<C2>) _next
091: .getCoordinateReferenceSystem());
092: }
093:
094: // OpenGIS Interface.
095: public int getDimension() {
096: return _first.getDimension() + _next.getDimension();
097: }
098:
099: // OpenGIS Interface.
100: public double getOrdinate(int dimension)
101: throws IndexOutOfBoundsException {
102: final int firstDimension = _first.getDimension();
103: if (dimension < firstDimension) {
104: return _first.getOrdinate(dimension);
105: } else {
106: return _next.getOrdinate(dimension - firstDimension);
107: }
108: }
109:
110: @Override
111: public CompoundCoordinates<?, ?> copy() {
112: return CompoundCoordinates.valueOf(_first, _next);
113: }
114:
115: // Default serialization.
116: //
117:
118: @SuppressWarnings("unchecked")
119: static final XMLFormat<CompoundCoordinates> XML = new XMLFormat<CompoundCoordinates>(
120: CompoundCoordinates.class) {
121:
122: @Override
123: public CompoundCoordinates newInstance(
124: Class<CompoundCoordinates> cls, InputElement xml)
125: throws XMLStreamException {
126: return FACTORY.object();
127: }
128:
129: @SuppressWarnings("unchecked")
130: @Override
131: public void read(InputElement xml, CompoundCoordinates coord)
132: throws XMLStreamException {
133: coord._first = xml.getNext();
134: coord._next = xml.getNext();
135: }
136:
137: @Override
138: public void write(CompoundCoordinates coord, OutputElement xml)
139: throws XMLStreamException {
140: xml.add(coord._first);
141: xml.add(coord._next);
142: }
143: };
144:
145: private static final long serialVersionUID = 1L;
146:
147: }
|