MATH-1284: Restore Vector2D class as an abstract implementation of Vector<Euclidean2D> and now Cartesian2D extends Vector2D.

Restore the public interface of Vector2DFormat to act on Vector2D.
This commit is contained in:
Ray DeCampo 2017-05-06 10:51:16 -04:00
parent 9be91f380c
commit 09c55eb812
4 changed files with 65 additions and 27 deletions

View File

@ -36,7 +36,7 @@ import org.apache.commons.math4.util.MathUtils;
* <p>Instances of this class are guaranteed to be immutable.</p>
* @since 4.0
*/
public class Cartesian2D implements Point<Euclidean2D>, Vector<Euclidean2D> {
public class Cartesian2D extends Vector2D implements Point<Euclidean2D> {
/** Origin (coordinates: 0, 0). */
public static final Cartesian2D ZERO = new Cartesian2D(0, 0);

View File

@ -0,0 +1,38 @@
/*
* 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.commons.math4.geometry.euclidean.twod;
import org.apache.commons.math4.geometry.Vector;
/** This class represents a 2D vector.
* @since 3.0
*/
public abstract class Vector2D implements Vector<Euclidean2D> {
/** Get the abscissa of the vector.
* @return abscissa of the vector
* @see #Vector2D(double, double)
*/
public abstract double getX();
/** Get the ordinate of the vector.
* @return ordinate of the vector
* @see #Vector2D(double, double)
*/
public abstract double getY();
}

View File

@ -108,26 +108,26 @@ public class Vector2DFormat extends VectorFormat<Euclidean2D> {
@Override
public StringBuffer format(final Vector<Euclidean2D> vector, final StringBuffer toAppendTo,
final FieldPosition pos) {
final Cartesian2D p2 = (Cartesian2D) vector;
final Vector2D p2 = (Vector2D) vector;
return format(toAppendTo, pos, p2.getX(), p2.getY());
}
/** {@inheritDoc} */
@Override
public Cartesian2D parse(final String source) throws MathParseException {
public Vector2D parse(final String source) throws MathParseException {
ParsePosition parsePosition = new ParsePosition(0);
Cartesian2D result = parse(source, parsePosition);
Vector2D result = parse(source, parsePosition);
if (parsePosition.getIndex() == 0) {
throw new MathParseException(source,
parsePosition.getErrorIndex(),
Cartesian2D.class);
Vector2D.class);
}
return result;
}
/** {@inheritDoc} */
@Override
public Cartesian2D parse(final String source, final ParsePosition pos) {
public Vector2D parse(final String source, final ParsePosition pos) {
final double[] coordinates = parseCoordinates(2, source, pos);
if (coordinates == null) {
return null;

View File

@ -157,8 +157,8 @@ public abstract class Vector2DFormatAbstractTest {
@Test
public void testParseSimpleNoDecimals() throws MathParseException {
String source = "{1; 1}";
Cartesian2D expected = new Cartesian2D(1, 1);
Cartesian2D actual = vector2DFormat.parse(source);
Vector2D expected = new Cartesian2D(1, 1);
Vector2D actual = vector2DFormat.parse(source);
Assert.assertEquals(expected, actual);
}
@ -181,8 +181,8 @@ public abstract class Vector2DFormatAbstractTest {
"{1" + getDecimalCharacter() +
"23; 1" + getDecimalCharacter() +
"43}";
Cartesian2D expected = new Cartesian2D(1.23, 1.43);
Cartesian2D actual = vector2DFormat.parse(source);
Vector2D expected = new Cartesian2D(1.23, 1.43);
Vector2D actual = vector2DFormat.parse(source);
Assert.assertEquals(expected, actual);
}
@ -192,8 +192,8 @@ public abstract class Vector2DFormatAbstractTest {
"{1" + getDecimalCharacter() +
"2323; 1" + getDecimalCharacter() +
"4343}";
Cartesian2D expected = new Cartesian2D(1.2323, 1.4343);
Cartesian2D actual = vector2DFormat.parse(source);
Vector2D expected = new Cartesian2D(1.2323, 1.4343);
Vector2D actual = vector2DFormat.parse(source);
Assert.assertEquals(expected, actual);
}
@ -203,8 +203,8 @@ public abstract class Vector2DFormatAbstractTest {
"{-1" + getDecimalCharacter() +
"2323; 1" + getDecimalCharacter() +
"4343}";
Cartesian2D expected = new Cartesian2D(-1.2323, 1.4343);
Cartesian2D actual = vector2DFormat.parse(source);
Vector2D expected = new Cartesian2D(-1.2323, 1.4343);
Vector2D actual = vector2DFormat.parse(source);
Assert.assertEquals(expected, actual);
}
@ -214,8 +214,8 @@ public abstract class Vector2DFormatAbstractTest {
"{1" + getDecimalCharacter() +
"2323; -1" + getDecimalCharacter() +
"4343}";
Cartesian2D expected = new Cartesian2D(1.2323, -1.4343);
Cartesian2D actual = vector2DFormat.parse(source);
Vector2D expected = new Cartesian2D(1.2323, -1.4343);
Vector2D actual = vector2DFormat.parse(source);
Assert.assertEquals(expected, actual);
}
@ -225,8 +225,8 @@ public abstract class Vector2DFormatAbstractTest {
"{1" + getDecimalCharacter() +
"2323; 1" + getDecimalCharacter() +
"4343}";
Cartesian2D expected = new Cartesian2D(1.2323, 1.4343);
Cartesian2D actual = vector2DFormat.parse(source);
Vector2D expected = new Cartesian2D(1.2323, 1.4343);
Vector2D actual = vector2DFormat.parse(source);
Assert.assertEquals(expected, actual);
}
@ -236,8 +236,8 @@ public abstract class Vector2DFormatAbstractTest {
"{-1" + getDecimalCharacter() +
"2323; -1" + getDecimalCharacter() +
"4343}";
Cartesian2D expected = new Cartesian2D(-1.2323, -1.4343);
Cartesian2D actual = vector2DFormat.parse(source);
Vector2D expected = new Cartesian2D(-1.2323, -1.4343);
Vector2D actual = vector2DFormat.parse(source);
Assert.assertEquals(expected, actual);
}
@ -247,8 +247,8 @@ public abstract class Vector2DFormatAbstractTest {
"{0" + getDecimalCharacter() +
"0; -1" + getDecimalCharacter() +
"4343}";
Cartesian2D expected = new Cartesian2D(0.0, -1.4343);
Cartesian2D actual = vector2DFormat.parse(source);
Vector2D expected = new Cartesian2D(0.0, -1.4343);
Vector2D actual = vector2DFormat.parse(source);
Assert.assertEquals(expected, actual);
}
@ -258,29 +258,29 @@ public abstract class Vector2DFormatAbstractTest {
"[1" + getDecimalCharacter() +
"2323 : 1" + getDecimalCharacter() +
"4343]";
Cartesian2D expected = new Cartesian2D(1.2323, 1.4343);
Cartesian2D actual = vector2DFormatSquare.parse(source);
Vector2D expected = new Cartesian2D(1.2323, 1.4343);
Vector2D actual = vector2DFormatSquare.parse(source);
Assert.assertEquals(expected, actual);
}
@Test
public void testParseNan() throws MathParseException {
String source = "{(NaN); (NaN)}";
Cartesian2D actual = vector2DFormat.parse(source);
Vector2D actual = vector2DFormat.parse(source);
Assert.assertEquals(Cartesian2D.NaN, actual);
}
@Test
public void testParsePositiveInfinity() throws MathParseException {
String source = "{(Infinity); (Infinity)}";
Cartesian2D actual = vector2DFormat.parse(source);
Vector2D actual = vector2DFormat.parse(source);
Assert.assertEquals(Cartesian2D.POSITIVE_INFINITY, actual);
}
@Test
public void testParseNegativeInfinity() throws MathParseException {
String source = "{(-Infinity); (-Infinity)}";
Cartesian2D actual = vector2DFormat.parse(source);
Vector2D actual = vector2DFormat.parse(source);
Assert.assertEquals(Cartesian2D.NEGATIVE_INFINITY, actual);
}