sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject svn commit: r1422228 - in /sis/branches/JDK7/sis-referencing/src/main/java/org/apache/sis/geometry: AbstractDirectPosition.java DirectPosition1D.java DirectPosition2D.java GeneralDirectPosition.java
Date Sat, 15 Dec 2012 12:04:12 GMT
Author: desruisseaux
Date: Sat Dec 15 12:04:11 2012
New Revision: 1422228

URL: http://svn.apache.org/viewvc?rev=1422228&view=rev
Log:
Added DirectPosition implementations.

Added:
    sis/branches/JDK7/sis-referencing/src/main/java/org/apache/sis/geometry/DirectPosition1D.java
  (with props)
    sis/branches/JDK7/sis-referencing/src/main/java/org/apache/sis/geometry/DirectPosition2D.java
  (with props)
    sis/branches/JDK7/sis-referencing/src/main/java/org/apache/sis/geometry/GeneralDirectPosition.java
  (with props)
Modified:
    sis/branches/JDK7/sis-referencing/src/main/java/org/apache/sis/geometry/AbstractDirectPosition.java

Modified: sis/branches/JDK7/sis-referencing/src/main/java/org/apache/sis/geometry/AbstractDirectPosition.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/sis-referencing/src/main/java/org/apache/sis/geometry/AbstractDirectPosition.java?rev=1422228&r1=1422227&r2=1422228&view=diff
==============================================================================
--- sis/branches/JDK7/sis-referencing/src/main/java/org/apache/sis/geometry/AbstractDirectPosition.java
(original)
+++ sis/branches/JDK7/sis-referencing/src/main/java/org/apache/sis/geometry/AbstractDirectPosition.java
Sat Dec 15 12:04:11 2012
@@ -56,7 +56,7 @@ public abstract class AbstractDirectPosi
      * {@linkplain org.opengis.geometry.coordinate.Position position}.
      */
     @Override
-    public DirectPosition getDirectPosition() {
+    public final DirectPosition getDirectPosition() {
         return this;
     }
 
@@ -200,7 +200,7 @@ public abstract class AbstractDirectPosi
      * Parses the given WKT.
      *
      * @param  wkt The WKT to parse.
-     * @return The ordinates, or {@code null) if none.
+     * @return The ordinates, or {@code null} if none.
      * @throws NumberFormatException If a number can not be parsed.
      * @throws IllegalArgumentException If the parenthesis are not balanced.
      */
@@ -295,20 +295,13 @@ parse:  while (i < length) {
      */
     @Override
     public int hashCode() {
-        return hashCode(this);
-    }
-
-    /**
-     * Returns a hash value for the given coordinate.
-     */
-    static int hashCode(final DirectPosition position) {
-        final int dimension = position.getDimension();
+        final int dimension = getDimension();
         int code = 1;
         for (int i=0; i<dimension; i++) {
-            final long bits = doubleToLongBits(position.getOrdinate(i));
+            final long bits = doubleToLongBits(getOrdinate(i));
             code = 31 * code + (((int) bits) ^ (int) (bits >>> 32));
         }
-        final CoordinateReferenceSystem crs = position.getCoordinateReferenceSystem();
+        final CoordinateReferenceSystem crs = getCoordinateReferenceSystem();
         if (crs != null) {
             code += crs.hashCode();
         }

Added: sis/branches/JDK7/sis-referencing/src/main/java/org/apache/sis/geometry/DirectPosition1D.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/sis-referencing/src/main/java/org/apache/sis/geometry/DirectPosition1D.java?rev=1422228&view=auto
==============================================================================
--- sis/branches/JDK7/sis-referencing/src/main/java/org/apache/sis/geometry/DirectPosition1D.java
(added)
+++ sis/branches/JDK7/sis-referencing/src/main/java/org/apache/sis/geometry/DirectPosition1D.java
Sat Dec 15 12:04:11 2012
@@ -0,0 +1,228 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sis.geometry;
+
+import java.io.Serializable;
+import org.opengis.referencing.crs.CoordinateReferenceSystem;
+import org.opengis.geometry.DirectPosition;
+import org.opengis.geometry.MismatchedDimensionException;
+import org.apache.sis.util.resources.Errors;
+
+
+/**
+ * Holds the coordinates for a one-dimensional position within some coordinate reference
system.
+ *
+ * @author Martin Desruisseaux (IRD, Geomatys)
+ * @since   0.3 (derived from geotk-2.0)
+ * @version 0.3
+ * @module
+ *
+ * @see DirectPosition2D
+ * @see GeneralDirectPosition
+ */
+public class DirectPosition1D extends AbstractDirectPosition implements Serializable, Cloneable
{
+    /**
+     * Serial number for inter-operability with different versions.
+     */
+    private static final long serialVersionUID = 3235094562875693710L;
+
+    /**
+     * The coordinate reference system for this position;
+     */
+    private CoordinateReferenceSystem crs;
+
+    /**
+     * The ordinate value.
+     */
+    public double ordinate;
+
+    /**
+     * Constructs a position initialized to (0) with a {@code null}
+     * coordinate reference system.
+     */
+    public DirectPosition1D() {
+    }
+
+    /**
+     * Constructs a position with the specified coordinate reference system.
+     *
+     * @param crs The coordinate reference system.
+     */
+    public DirectPosition1D(final CoordinateReferenceSystem crs) {
+        ensureDimensionMatch(crs, 1);
+        this.crs = crs;
+    }
+
+    /**
+     * Constructs a 1D position from the specified ordinate.
+     *
+     * @param ordinate The ordinate value.
+     */
+    public DirectPosition1D(final double ordinate) {
+        this.ordinate = ordinate;
+    }
+
+    /**
+     * Constructs a position initialized to the values parsed from the given string in
+     * <cite>Well Known Text</cite> (WKT) format. The given string is typically
a {@code POINT}
+     * element like below:
+     *
+     * {@preformat wkt
+     *     POINT(6)
+     * }
+     *
+     * @param  wkt The {@code POINT} or other kind of element to parse.
+     * @throws IllegalArgumentException If the given string can not be parsed.
+     * @throws MismatchedDimensionException If the given point is not one-dimensional.
+     *
+     * @see #toString(DirectPosition)
+     * @see org.geotoolkit.measure.CoordinateFormat
+     */
+    public DirectPosition1D(final String wkt) throws IllegalArgumentException {
+        final double[] ordinates = parse(wkt);
+        if (ordinates == null) {
+            throw new IllegalArgumentException(Errors.format(
+                    Errors.Keys.UnparsableStringForClass_2, "POINT", wkt));
+        }
+        ensureDimensionMatch("wkt", ordinates.length, 1);
+        ordinate = ordinates[0];
+    }
+
+    /**
+     * The length of coordinate sequence (the number of entries).
+     * This is always 1 for {@code DirectPosition1D} objects.
+     *
+     * @return The dimensionality of this position.
+     */
+    @Override
+    public final int getDimension() {
+        return 1;
+    }
+
+    /**
+     * Returns the coordinate reference system in which the coordinate is given.
+     * May be {@code null} if this particular {@code DirectPosition} is included
+     * in a larger object with such a reference to a CRS.
+     *
+     * @return The coordinate reference system, or {@code null}.
+     */
+    @Override
+    public final CoordinateReferenceSystem getCoordinateReferenceSystem() {
+        return crs;
+    }
+
+    /**
+     * Sets the coordinate reference system in which the coordinate is given.
+     *
+     * @param crs The new coordinate reference system, or {@code null}.
+     */
+    public void setCoordinateReferenceSystem(final CoordinateReferenceSystem crs) {
+        ensureDimensionMatch(crs, 1);
+        this.crs = crs;
+    }
+
+    /**
+     * Returns a sequence of numbers that hold the coordinate of this position in its
+     * reference system.
+     *
+     * {@note This method is final for ensuring consistency with the <code>ordinate</code>
field,
+     *        which is public.}
+     *
+     * @return The coordinates.
+     */
+    @Override
+    public final double[] getCoordinate() {
+        return new double[] {ordinate};
+    }
+
+    /**
+     * Returns the ordinate at the specified dimension.
+     *
+     * {@note This method is final for ensuring consistency with the <code>ordinate</code>
field,
+     *        which is public.}
+     *
+     * @param  dimension The dimension, which must be 0.
+     * @return The {@linkplain #ordinate}.
+     * @throws IndexOutOfBoundsException if the specified dimension is out of bounds.
+     */
+    @Override
+    public final double getOrdinate(final int dimension) throws IndexOutOfBoundsException
{
+        if (dimension == 0) {
+            return ordinate;
+        } else {
+            throw new IndexOutOfBoundsException(Errors.format(Errors.Keys.IndexOutOfBounds_1,
dimension));
+        }
+    }
+
+    /**
+     * Sets the ordinate value along the specified dimension.
+     *
+     * @param  dimension The dimension, which must be 0.
+     * @param  value the ordinate value.
+     * @throws IndexOutOfBoundsException if the specified dimension is out of bounds.
+     */
+    @Override
+    public void setOrdinate(int dimension, double value) throws IndexOutOfBoundsException
{
+        if (dimension == 0) {
+            ordinate = value;
+        } else {
+            throw new IndexOutOfBoundsException(Errors.format(Errors.Keys.IndexOutOfBounds_1,
dimension));
+        }
+    }
+
+    /**
+     * Sets this coordinate to the specified direct position. If the specified position
+     * contains a coordinate reference system (CRS), then the CRS for this position will
+     * be set to the CRS of the specified position.
+     *
+     * @param  position The new position for this point.
+     * @throws MismatchedDimensionException if this point doesn't have the expected dimension.
+     */
+    @Override
+    public void setLocation(final DirectPosition position) throws MismatchedDimensionException
{
+        ensureDimensionMatch("position", position.getDimension(), 1);
+        setCoordinateReferenceSystem(position.getCoordinateReferenceSystem());
+        ordinate = position.getOrdinate(0);
+    }
+
+    /**
+     * Returns a copy of this position.
+     */
+    @Override
+    public DirectPosition1D clone() {
+        try {
+            return (DirectPosition1D) super.clone();
+        } catch (CloneNotSupportedException exception) {
+            // Should not happen, since we are cloneable.
+            throw new AssertionError(exception);
+        }
+    }
+
+    /**
+     * Returns a hash value for this coordinate.
+     */
+    @Override
+    public int hashCode() {
+        final long value = Double.doubleToLongBits(ordinate);
+        int code = 31 + (((int) value) ^ (int) (value >>> 32));
+        if (crs != null) {
+            code += crs.hashCode();
+        }
+        assert code == super.hashCode();
+        return code;
+    }
+}

Propchange: sis/branches/JDK7/sis-referencing/src/main/java/org/apache/sis/geometry/DirectPosition1D.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sis/branches/JDK7/sis-referencing/src/main/java/org/apache/sis/geometry/DirectPosition1D.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: sis/branches/JDK7/sis-referencing/src/main/java/org/apache/sis/geometry/DirectPosition2D.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/sis-referencing/src/main/java/org/apache/sis/geometry/DirectPosition2D.java?rev=1422228&view=auto
==============================================================================
--- sis/branches/JDK7/sis-referencing/src/main/java/org/apache/sis/geometry/DirectPosition2D.java
(added)
+++ sis/branches/JDK7/sis-referencing/src/main/java/org/apache/sis/geometry/DirectPosition2D.java
Sat Dec 15 12:04:11 2012
@@ -0,0 +1,350 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sis.geometry;
+
+import java.util.Objects;
+import java.awt.geom.Point2D;
+import org.opengis.geometry.DirectPosition;
+import org.opengis.geometry.MismatchedDimensionException;
+import org.opengis.referencing.crs.CoordinateReferenceSystem;
+import org.opengis.referencing.cs.AxisDirection;
+import org.apache.sis.util.resources.Errors;
+import static java.lang.Double.doubleToLongBits;
+
+
+/**
+ * Holds the coordinates for a two-dimensional position within some coordinate reference
system.
+ * This class inherits {@linkplain #x x} and {@linkplain #y y} fields. But despite their
names,
+ * they don't need to be oriented toward {@linkplain AxisDirection#EAST East} and {@linkplain
+ * AxisDirection#NORTH North}. The (<var>x</var>,<var>y</var>) axis
can have any orientation and
+ * should be understood as "<cite>ordinate 0</cite>" and "<cite>ordinate
1</cite>" values instead.
+ * This is not specific to this implementation; in Java2D too, the visual axis orientation
depend
+ * on the {@linkplain java.awt.Graphics2D#getTransform() affine transform in the graphics
context}.
+ *
+ * {@note The rational for avoiding axis orientation restriction is that other <code>DirectPosition</code>
+ *        implementations do not have such restriction, and it would be hard to generalize.
+ *        For example there is no clear "x" or "y" classification for North-East direction.}
+ *
+ * {@section Caution when used in collections}
+ * <strong>Do not mix instances of this class with ordinary {@link Point2D} instances
in a
+ * {@link java.util.HashSet} or as {@link java.util.HashMap} keys.</strong>
+ * It is not possible to meet both {@link Point2D#hashCode} and {@link DirectPosition#hashCode}
+ * contracts, and this class chooses to implements the later. Consequently, {@link #hashCode()}
+ * is inconsistent with {@link Point2D#equals(Object)} but is consistent with
+ * {@link DirectPosition#equals(Object)}.
+ *
+ * <p>In other words, it is safe to add instances of {@code DirectPosition2D} in a
+ * {@code HashSet<DirectPosition>}, but it is unsafe to add them in a {@code HashSet<Point2D>}.
+ * Collections that do not rely on {@link Object#hashCode()}, like {@link java.util.ArrayList},
+ * are safe in all cases.</p>
+ *
+ * @author Martin Desruisseaux (IRD, Geomatys)
+ * @since   0.3 (derived from geotk-2.0)
+ * @version 0.3
+ * @module
+ *
+ * @see DirectPosition1D
+ * @see GeneralDirectPosition
+ * @see Point2D
+ */
+public class DirectPosition2D extends Point2D.Double implements DirectPosition, Cloneable
{
+    /**
+     * Serial number for inter-operability with different versions.
+     */
+    private static final long serialVersionUID = 835130287438466996L;
+
+    /**
+     * The coordinate reference system for this position;
+     */
+    private CoordinateReferenceSystem crs;
+
+    /**
+     * Constructs a position initialized to (0,0) with a {@code null} coordinate reference
system.
+     */
+    public DirectPosition2D() {
+    }
+
+    /**
+     * Constructs a position with the specified coordinate reference system.
+     *
+     * @param crs The coordinate reference system, or {@code null}.
+     */
+    public DirectPosition2D(final CoordinateReferenceSystem crs) {
+        this.crs = crs;
+    }
+
+    /**
+     * Constructs a 2D position from the specified ordinates. Despite their names,
+     * the (<var>x</var>,<var>y</var>) coordinates don't need to
be oriented toward
+     * ({@linkplain AxisDirection#EAST East}, {@linkplain AxisDirection#NORTH North}).
+     * Those parameter names simply match the {@linkplain #x x} and {@linkplain #y y} fields.
+     * See the <a href="#skip-navbar_top">class javadoc</a> for details.
+     *
+     * @param x The first ordinate value (not necessarily horizontal).
+     * @param y The second ordinate value (not necessarily vertical).
+     */
+    public DirectPosition2D(final double x, final double y) {
+        super(x, y);
+    }
+
+    /**
+     * Constructs a 2D position from the specified ordinates in the specified CRS.
+     * Despite their names, the (<var>x</var>,<var>y</var>) coordinates
don't need to be oriented
+     * toward ({@linkplain AxisDirection#EAST East}, {@linkplain AxisDirection#NORTH North}).
+     * Those parameter names simply match the {@linkplain #x x} and {@linkplain #y y} fields.
+     * The actual axis orientations are determined by the specified CRS.
+     * See the <a href="#skip-navbar_top">class javadoc</a> for details.
+     *
+     * @param crs The coordinate reference system, or {@code null}.
+     * @param x The first ordinate value (not necessarily horizontal).
+     * @param y The second ordinate value (not necessarily vertical).
+     */
+    public DirectPosition2D(final CoordinateReferenceSystem crs,
+                            final double x, final double y)
+    {
+        super(x, y);
+        AbstractDirectPosition.ensureDimensionMatch(crs, 2);
+        this.crs = crs;
+    }
+
+    /**
+     * Constructs a position from the specified {@link Point2D}.
+     * If the given point implements also the {@code DirectPosition} interface, then this
+     * constructor will set the CRS of this {@code DirectPosition2D} to the CRS of the given
point.
+     * Otherwise the CRS of this {@code DirectPosition2D} will be initially null.
+     *
+     * @param point The point to copy.
+     */
+    public DirectPosition2D(final Point2D point) {
+        super(point.getX(), point.getY());
+        if (point instanceof DirectPosition) {
+            crs = ((DirectPosition) point).getCoordinateReferenceSystem();
+            AbstractDirectPosition.ensureDimensionMatch(crs, 2);
+        }
+    }
+
+    /**
+     * Constructs a position initialized to the values parsed from the given string in
+     * <cite>Well Known Text</cite> (WKT) format. The given string is typically
a {@code POINT}
+     * element like below:
+     *
+     * {@preformat wkt
+     *     POINT(6 10)
+     * }
+     *
+     * @param  wkt The {@code POINT} or other kind of element to parse.
+     * @throws IllegalArgumentException If the given string can not be parsed.
+     * @throws MismatchedDimensionException If the given point is not two-dimensional.
+     *
+     * @see AbstractDirectPosition#toString(DirectPosition)
+     * @see org.geotoolkit.measure.CoordinateFormat
+     */
+    public DirectPosition2D(final String wkt) throws IllegalArgumentException {
+        final double[] ordinates = AbstractDirectPosition.parse(wkt);
+        if (ordinates == null) {
+            throw new IllegalArgumentException(Errors.format(
+                    Errors.Keys.UnparsableStringForClass_2, "POINT", wkt));
+        }
+        AbstractDirectPosition.ensureDimensionMatch("wkt", ordinates.length, 2);
+        x = ordinates[0];
+        y = ordinates[1];
+    }
+
+    /**
+     * Returns always {@code this}, the direct position for this
+     * {@linkplain org.opengis.geometry.coordinate.Position position}.
+     */
+    @Override
+    public final DirectPosition getDirectPosition() {
+        return this;
+    }
+
+    /**
+     * The length of coordinate sequence (the number of entries).
+     * This is always 2 for {@code DirectPosition2D} objects.
+     *
+     * @return The dimensionality of this position.
+     */
+    @Override
+    public final int getDimension() {
+        return 2;
+    }
+
+    /**
+     * Returns the coordinate reference system in which the coordinate is given.
+     * May be {@code null} if this particular {@code DirectPosition} is included
+     * in a larger object with such a reference to a CRS.
+     *
+     * @return The coordinate reference system, or {@code null}.
+     */
+    @Override
+    public final CoordinateReferenceSystem getCoordinateReferenceSystem() {
+        return crs;
+    }
+
+    /**
+     * Sets the coordinate reference system in which the coordinate is given.
+     *
+     * @param crs The new coordinate reference system, or {@code null}.
+     */
+    public void setCoordinateReferenceSystem(final CoordinateReferenceSystem crs) {
+        AbstractDirectPosition.ensureDimensionMatch(crs, 2);
+        this.crs = crs;
+    }
+
+    /**
+     * Returns a sequence of numbers that hold the coordinate of this position in its
+     * reference system.
+     *
+     * {@note This method is final for ensuring consistency with the <code>x</code>
+     *        and <code>y</code> fields, which are public.}
+     *
+     * @return The coordinate.
+     */
+    @Override
+    public final double[] getCoordinate() {
+        return new double[] {x,y};
+    }
+
+    /**
+     * Returns the ordinate at the specified dimension.
+     *
+     * {@note This method is final for ensuring consistency with the <code>x</code>
+     *        and <code>y</code> fields, which are public.}
+     *
+     * @param  dimension The dimension in the range 0 to 1 inclusive.
+     * @return The coordinate at the specified dimension.
+     * @throws IndexOutOfBoundsException if the specified dimension is out of bounds.
+     */
+    @Override
+    public final double getOrdinate(final int dimension) throws IndexOutOfBoundsException
{
+        switch (dimension) {
+            case 0:  return x;
+            case 1:  return y;
+            default: throw new IndexOutOfBoundsException(Errors.format(Errors.Keys.IndexOutOfBounds_1,
dimension));
+        }
+    }
+
+    /**
+     * Sets the ordinate value along the specified dimension.
+     *
+     * @param  dimension the dimension for the ordinate of interest.
+     * @param  value the ordinate value of interest.
+     * @throws IndexOutOfBoundsException if the specified dimension is out of bounds.
+     */
+    @Override
+    public void setOrdinate(int dimension, double value) throws IndexOutOfBoundsException
{
+        switch (dimension) {
+            case 0:  x = value; break;
+            case 1:  y = value; break;
+            default: throw new IndexOutOfBoundsException(Errors.format(Errors.Keys.IndexOutOfBounds_1,
dimension));
+        }
+    }
+
+    /**
+     * Sets this coordinate to the specified direct position. If the specified position
+     * contains a coordinate reference system (CRS), then the CRS for this position will
+     * be set to the CRS of the specified position.
+     *
+     * @param  position The new position for this point.
+     * @throws MismatchedDimensionException if this point doesn't have the expected dimension.
+     */
+    public void setLocation(final DirectPosition position) throws MismatchedDimensionException
{
+        AbstractDirectPosition.ensureDimensionMatch("position", position.getDimension(),
2);
+        setCoordinateReferenceSystem(position.getCoordinateReferenceSystem());
+        x = position.getOrdinate(0);
+        y = position.getOrdinate(1);
+    }
+
+    /**
+     * Formats this position in the <cite>Well Known Text</cite> (WKT) format.
+     * The output is like below:
+     *
+     * {@preformat wkt
+     *   POINT(x y)
+     * }
+     *
+     * The output of this method can be parsed by the by the {@link GeneralDirectPosition}
constructor.
+     */
+    @Override
+    public String toString() {
+        return AbstractDirectPosition.toString(this);
+    }
+
+    /**
+     * Returns a hash value for this coordinate. This method implements the
+     * {@link DirectPosition#hashCode()} contract, not the {@link Point2D#hashCode()} contract.
+     *
+     * @return A hash code value for this position.
+     */
+    @Override
+    public int hashCode() {
+        int code;
+        long bits;
+        bits = doubleToLongBits(x); code = 31      + (((int) bits) ^ (int) (bits >>>
32));
+        bits = doubleToLongBits(y); code = 31*code + (((int) bits) ^ (int) (bits >>>
32));
+        if (crs != null) {
+            code += crs.hashCode();
+        }
+        return code;
+    }
+
+    /**
+     * Compares this point with the specified object for equality. If the given object implements
+     * the {@link DirectPosition} interface, then the comparison is performed as specified
in the
+     * {@link DirectPosition#equals(Object)} contract. Otherwise the comparison is performed
as
+     * specified in the {@link Point2D#equals(Object)} contract.
+     *
+     * @param object The object to compare with this position.
+     * @return {@code true} if the given object is equal to this position.
+     */
+    @Override
+    public boolean equals(final Object object) {
+        /*
+         * If the other object implements the DirectPosition interface, performs
+         * the comparison as specified in DirectPosition.equals(Object) contract.
+         */
+        if (object instanceof DirectPosition) {
+            final DirectPosition other = (DirectPosition) object;
+            if (other.getDimension() == 2 &&
+                doubleToLongBits(other.getOrdinate(0)) == doubleToLongBits(x) &&
+                doubleToLongBits(other.getOrdinate(1)) == doubleToLongBits(y) &&
+                Objects.equals(other.getCoordinateReferenceSystem(), crs))
+            {
+                assert hashCode() == other.hashCode() : this;
+                return true;
+            }
+            return false;
+        }
+        /*
+         * Otherwise performs the comparison as in Point2D.equals(Object).
+         * Do NOT check the CRS if the given object is an ordinary Point2D.
+         * This is necessary in order to respect the contract defined in Point2D.
+         */
+        return super.equals(object);
+    }
+
+    /**
+     * Returns a clone of this point.
+     *
+     * @return A clone of this position.
+     */
+    @Override
+    public DirectPosition2D clone() {
+        return (DirectPosition2D) super.clone();
+    }
+}

Propchange: sis/branches/JDK7/sis-referencing/src/main/java/org/apache/sis/geometry/DirectPosition2D.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sis/branches/JDK7/sis-referencing/src/main/java/org/apache/sis/geometry/DirectPosition2D.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: sis/branches/JDK7/sis-referencing/src/main/java/org/apache/sis/geometry/GeneralDirectPosition.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/sis-referencing/src/main/java/org/apache/sis/geometry/GeneralDirectPosition.java?rev=1422228&view=auto
==============================================================================
--- sis/branches/JDK7/sis-referencing/src/main/java/org/apache/sis/geometry/GeneralDirectPosition.java
(added)
+++ sis/branches/JDK7/sis-referencing/src/main/java/org/apache/sis/geometry/GeneralDirectPosition.java
Sat Dec 15 12:04:11 2012
@@ -0,0 +1,281 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sis.geometry;
+
+import java.util.Arrays;
+import java.io.Serializable;
+import java.lang.reflect.Field;
+import org.opengis.geometry.DirectPosition;
+import org.opengis.geometry.MismatchedDimensionException;
+import org.opengis.referencing.crs.CoordinateReferenceSystem;
+import org.apache.sis.util.resources.Errors;
+
+
+/**
+ * Holds the coordinates for a position within some coordinate reference system.
+ * Since {@code DirectPosition}s, as data types, will often be included in larger objects
+ * (such as {@linkplain org.opengis.geometry.Geometry geometries}) that have references
+ * to {@code CoordinateReferenceSystem}, the {@link #getCoordinateReferenceSystem()} method
+ * may returns {@code null} if this particular {@code DirectPosition} is included in such
+ * larger object. In this case, the coordinate reference system is implicitly assumed to
take
+ * on the value of the containing object's {@code CoordinateReferenceSystem}.
+ *
+ * <p>This particular implementation of {@code DirectPosition} is said "General" because
it
+ * uses an {@linkplain #ordinates array of ordinates} of an arbitrary length. If the direct
+ * position is known to be always two-dimensional, then {@link DirectPosition2D} provides
+ * a more efficient implementation.</p>
+ *
+ * @author Martin Desruisseaux (IRD, Geomatys)
+ * @since   0.3 (derived from geotk-1.2)
+ * @version 0.3
+ * @module
+ *
+ * @see DirectPosition1D
+ * @see DirectPosition2D
+ */
+public class GeneralDirectPosition extends AbstractDirectPosition implements Serializable,
Cloneable {
+    /**
+     * Serial number for inter-operability with different versions.
+     */
+    private static final long serialVersionUID = 9071833698385715524L;
+
+    /**
+     * The ordinates of the direct position. The length of this array is the
+     * {@linkplain #getDimension() dimension} of this direct position.
+     */
+    public final double[] ordinates;
+
+    /**
+     * The coordinate reference system for this position, or {@code null}.
+     */
+    private CoordinateReferenceSystem crs;
+
+    /**
+     * Constructs a position using the specified coordinate reference system.
+     * The number of dimensions is inferred from the coordinate reference system.
+     *
+     * @param crs The coordinate reference system to be given to this position.
+     */
+    public GeneralDirectPosition(final CoordinateReferenceSystem crs) {
+        this(crs.getCoordinateSystem().getDimension());
+        this.crs = crs;
+    }
+
+    /**
+     * Constructs a position with the specified number of dimensions.
+     *
+     * @param  dimension Number of dimensions.
+     * @throws NegativeArraySizeException if {@code dimension} is negative.
+     */
+    public GeneralDirectPosition(final int dimension) throws NegativeArraySizeException {
+        ordinates = new double[dimension];
+    }
+
+    /**
+     * Constructs a position with the specified ordinates.
+     * The {@code ordinates} array will be copied.
+     *
+     * @param ordinates The ordinate values to copy.
+     */
+    public GeneralDirectPosition(final double... ordinates) {
+        this.ordinates = ordinates.clone();
+    }
+
+    /**
+     * Constructs a position initialized to the same values than the specified point.
+     *
+     * @param point The position to copy.
+     */
+    public GeneralDirectPosition(final DirectPosition point) {
+        ordinates = point.getCoordinate(); // Should already be cloned.
+        crs = point.getCoordinateReferenceSystem();
+        ensureDimensionMatch(crs, ordinates.length);
+    }
+
+    /**
+     * Constructs a position initialized to the values parsed
+     * from the given string in <cite>Well Known Text</cite> (WKT) format.
+     * The given string is typically a {@code POINT} element like below:
+     *
+     * {@preformat wkt
+     *     POINT(6 10)
+     * }
+     *
+     * However this constructor is lenient to other types like {@code POINT ZM}.
+     *
+     * @param  wkt The {@code POINT} or other kind of element to parse.
+     * @throws IllegalArgumentException If the given string can not be parsed.
+     *
+     * @see #toString(DirectPosition)
+     * @see org.apache.sis.measure.CoordinateFormat
+     */
+    public GeneralDirectPosition(final String wkt) throws IllegalArgumentException {
+        if ((ordinates = parse(wkt)) == null) {
+            throw new IllegalArgumentException(Errors.format(
+                    Errors.Keys.UnparsableStringForClass_2, "POINT", wkt));
+        }
+    }
+
+    /**
+     * The length of ordinate sequence (the number of entries).
+     * This is always equals to the length of the {@link #ordinates} array.
+     *
+     * @return The dimensionality of this position.
+     */
+    @Override
+    public final int getDimension() {
+        return ordinates.length;
+    }
+
+    /**
+     * Returns the coordinate reference system in which the coordinate is given.
+     * May be {@code null} if this particular {@code DirectPosition} is included
+     * in a larger object with such a reference to a CRS.
+     *
+     * @return The coordinate reference system, or {@code null}.
+     */
+    @Override
+    public final CoordinateReferenceSystem getCoordinateReferenceSystem() {
+        return crs;
+    }
+
+    /**
+     * Sets the coordinate reference system in which the coordinate is given.
+     *
+     * @param crs The new coordinate reference system, or {@code null}.
+     * @throws MismatchedDimensionException if the specified CRS doesn't have the expected
+     *         number of dimensions.
+     */
+    public void setCoordinateReferenceSystem(final CoordinateReferenceSystem crs)
+            throws MismatchedDimensionException
+    {
+        ensureDimensionMatch(crs, getDimension());
+        this.crs = crs;
+    }
+
+    /**
+     * Returns a sequence of numbers that hold the coordinate of this position in its
+     * reference system.
+     *
+     * {@note This method is final for ensuring consistency with the <code>ordinates</code>,
+     *        array field, which is public.}
+     *
+     * @return A copy of the {@linkplain #ordinates ordinates} array.
+     */
+    @Override
+    public final double[] getCoordinate() {
+        return ordinates.clone();
+    }
+
+    /**
+     * Sets the ordinate values along all dimensions.
+     *
+     * @param  ordinates The new ordinates values, or a {@code null} array
+     *         for setting all ordinate values to {@link Double#NaN NaN}.
+     * @throws MismatchedDimensionException If the length of the specified array is not
+     *         equals to the {@linkplain #getDimension() dimension} of this position.
+     */
+    public void setCoordinate(final double... ordinates) throws MismatchedDimensionException
{
+        if (ordinates == null) {
+            Arrays.fill(this.ordinates, Double.NaN);
+        } else {
+            ensureDimensionMatch("ordinates", ordinates.length, this.ordinates.length);
+            System.arraycopy(ordinates, 0, this.ordinates, 0, ordinates.length);
+        }
+    }
+
+    /**
+     * Returns the ordinate at the specified dimension.
+     *
+     * {@note This method is final for ensuring consistency with the <code>ordinates</code>,
+     *        array field, which is public.}
+     *
+     * @param  dimension The dimension in the range 0 to {@linkplain #getDimension() dimension}-1.
+     * @return The ordinate at the specified dimension.
+     * @throws IndexOutOfBoundsException if the specified dimension is out of bounds.
+     */
+    @Override
+    public final double getOrdinate(final int dimension) throws IndexOutOfBoundsException
{
+        return ordinates[dimension];
+    }
+
+    /**
+     * Sets the ordinate value along the specified dimension.
+     *
+     * @param dimension The dimension for the ordinate of interest.
+     * @param value The ordinate value of interest.
+     * @throws IndexOutOfBoundsException if the specified dimension is out of bounds.
+     */
+    @Override
+    public void setOrdinate(final int dimension, final double value) throws IndexOutOfBoundsException
{
+        ordinates[dimension] = value;
+    }
+
+    /**
+     * Sets this coordinate to the specified direct position. If the specified position
+     * contains a coordinate reference system (CRS), then the CRS for this position will
+     * be set to the CRS of the specified position.
+     *
+     * @param  position The new position for this point, or {@code null} for setting all
ordinate
+     *         values to {@link Double#NaN NaN}.
+     * @throws MismatchedDimensionException if the given position doesn't have the expected
dimension.
+     */
+    @Override
+    public void setLocation(final DirectPosition position) throws MismatchedDimensionException
{
+        if (position == null) {
+            Arrays.fill(ordinates, Double.NaN);
+        } else {
+            ensureDimensionMatch("position", position.getDimension(), ordinates.length);
+            setCoordinateReferenceSystem(position.getCoordinateReferenceSystem());
+            for (int i=0; i<ordinates.length; i++) {
+                ordinates[i] = position.getOrdinate(i);
+            }
+        }
+    }
+
+    /**
+     * Returns a deep copy of this position.
+     */
+    @Override
+    public GeneralDirectPosition clone() {
+        try {
+            GeneralDirectPosition e = (GeneralDirectPosition) super.clone();
+            final Field field = GeneralDirectPosition.class.getDeclaredField("ordinates");
+            field.setAccessible(true);
+            field.set(e, ordinates.clone());
+            return e;
+        } catch (ReflectiveOperationException | CloneNotSupportedException exception) {
+            // Should not happen, since we are cloneable.
+            // Should not happen, since the "ordinates" field exists.
+            // etc...
+            throw new AssertionError(exception);
+        }
+    }
+
+    /**
+     * Returns a hash value for this coordinate.
+     */
+    @Override
+    public int hashCode() {
+        int code = Arrays.hashCode(ordinates);
+        if (crs != null) {
+            code += crs.hashCode();
+        }
+        assert code == super.hashCode();
+        return code;
+    }
+}

Propchange: sis/branches/JDK7/sis-referencing/src/main/java/org/apache/sis/geometry/GeneralDirectPosition.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sis/branches/JDK7/sis-referencing/src/main/java/org/apache/sis/geometry/GeneralDirectPosition.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain



Mime
View raw message