MATH-1284: Restore Vector1D class as an abstract implementation of Vector<Euclidean1D> and now Cartesian1D extends Vector1D.

Restore the public interface of Vector1DFormat to act on Vector1D.
This commit is contained in:
Ray DeCampo 2017-05-06 10:45:00 -04:00
parent a27ca511a5
commit 9be91f380c
4 changed files with 62 additions and 28 deletions

View File

@ -34,7 +34,7 @@ import org.apache.commons.math4.util.MathUtils;
* <p>Instances of this class are guaranteed to be immutable.</p>
* @since 4.0
*/
public class Cartesian1D implements Point<Euclidean1D>, Vector<Euclidean1D> {
public class Cartesian1D extends Vector1D implements Point<Euclidean1D> {
/** Origin (coordinates: 0). */
public static final Cartesian1D ZERO = new Cartesian1D(0.0);
@ -128,6 +128,7 @@ public class Cartesian1D implements Point<Euclidean1D>, Vector<Euclidean1D> {
* @return abscissa of the vector
* @see #Cartesian1D(double)
*/
@Override
public double getX() {
return x;
}

View File

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

View File

@ -105,26 +105,26 @@ public class Vector1DFormat extends VectorFormat<Euclidean1D> {
@Override
public StringBuffer format(final Vector<Euclidean1D> vector, final StringBuffer toAppendTo,
final FieldPosition pos) {
final Cartesian1D p1 = (Cartesian1D) vector;
final Vector1D p1 = (Vector1D) vector;
return format(toAppendTo, pos, p1.getX());
}
/** {@inheritDoc} */
@Override
public Cartesian1D parse(final String source) throws MathParseException {
public Vector1D parse(final String source) throws MathParseException {
ParsePosition parsePosition = new ParsePosition(0);
Cartesian1D result = parse(source, parsePosition);
Vector1D result = parse(source, parsePosition);
if (parsePosition.getIndex() == 0) {
throw new MathParseException(source,
parsePosition.getErrorIndex(),
Cartesian1D.class);
Vector1D.class);
}
return result;
}
/** {@inheritDoc} */
@Override
public Cartesian1D parse(final String source, final ParsePosition pos) {
public Vector1D parse(final String source, final ParsePosition pos) {
final double[] coordinates = parseCoordinates(1, source, pos);
if (coordinates == null) {
return null;

View File

@ -130,14 +130,14 @@ public abstract class Vector1DFormatAbstractTest {
@Test
public void testParseSimpleNoDecimals() throws MathParseException {
String source = "{1}";
Cartesian1D expected = new Cartesian1D(1);
Cartesian1D actual = vector1DFormat.parse(source);
Vector1D expected = new Cartesian1D(1);
Vector1D actual = vector1DFormat.parse(source);
Assert.assertEquals(expected, actual);
}
@Test
public void testParseIgnoredWhitespace() {
Cartesian1D expected = new Cartesian1D(1);
Vector1D expected = new Cartesian1D(1);
ParsePosition pos1 = new ParsePosition(0);
String source1 = "{1}";
Assert.assertEquals(expected, vector1DFormat.parse(source1, pos1));
@ -153,8 +153,8 @@ public abstract class Vector1DFormatAbstractTest {
String source =
"{1" + getDecimalCharacter() +
"23}";
Cartesian1D expected = new Cartesian1D(1.23);
Cartesian1D actual = vector1DFormat.parse(source);
Vector1D expected = new Cartesian1D(1.23);
Vector1D actual = vector1DFormat.parse(source);
Assert.assertEquals(expected, actual);
}
@ -163,8 +163,8 @@ public abstract class Vector1DFormatAbstractTest {
String source =
"{1" + getDecimalCharacter() +
"2323}";
Cartesian1D expected = new Cartesian1D(1.2323);
Cartesian1D actual = vector1DFormat.parse(source);
Vector1D expected = new Cartesian1D(1.2323);
Vector1D actual = vector1DFormat.parse(source);
Assert.assertEquals(expected, actual);
}
@ -173,8 +173,8 @@ public abstract class Vector1DFormatAbstractTest {
String source =
"{-1" + getDecimalCharacter() +
"2323}";
Cartesian1D expected = new Cartesian1D(-1.2323);
Cartesian1D actual = vector1DFormat.parse(source);
Vector1D expected = new Cartesian1D(-1.2323);
Vector1D actual = vector1DFormat.parse(source);
Assert.assertEquals(expected, actual);
}
@ -183,8 +183,8 @@ public abstract class Vector1DFormatAbstractTest {
String source =
"{1" + getDecimalCharacter() +
"2323}";
Cartesian1D expected = new Cartesian1D(1.2323);
Cartesian1D actual = vector1DFormat.parse(source);
Vector1D expected = new Cartesian1D(1.2323);
Vector1D actual = vector1DFormat.parse(source);
Assert.assertEquals(expected, actual);
}
@ -193,8 +193,8 @@ public abstract class Vector1DFormatAbstractTest {
String source =
"{1" + getDecimalCharacter() +
"2323}";
Cartesian1D expected = new Cartesian1D(1.2323);
Cartesian1D actual = vector1DFormat.parse(source);
Vector1D expected = new Cartesian1D(1.2323);
Vector1D actual = vector1DFormat.parse(source);
Assert.assertEquals(expected, actual);
}
@ -203,8 +203,8 @@ public abstract class Vector1DFormatAbstractTest {
String source =
"{-1" + getDecimalCharacter() +
"2323}";
Cartesian1D expected = new Cartesian1D(-1.2323);
Cartesian1D actual = vector1DFormat.parse(source);
Vector1D expected = new Cartesian1D(-1.2323);
Vector1D actual = vector1DFormat.parse(source);
Assert.assertEquals(expected, actual);
}
@ -213,8 +213,8 @@ public abstract class Vector1DFormatAbstractTest {
String source =
"{0" + getDecimalCharacter() +
"0}";
Cartesian1D expected = new Cartesian1D(0.0);
Cartesian1D actual = vector1DFormat.parse(source);
Vector1D expected = new Cartesian1D(0.0);
Vector1D actual = vector1DFormat.parse(source);
Assert.assertEquals(expected, actual);
}
@ -223,29 +223,29 @@ public abstract class Vector1DFormatAbstractTest {
String source =
"[1" + getDecimalCharacter() +
"2323]";
Cartesian1D expected = new Cartesian1D(1.2323);
Cartesian1D actual = vector1DFormatSquare.parse(source);
Vector1D expected = new Cartesian1D(1.2323);
Vector1D actual = vector1DFormatSquare.parse(source);
Assert.assertEquals(expected, actual);
}
@Test
public void testParseNan() throws MathParseException {
String source = "{(NaN)}";
Cartesian1D actual = vector1DFormat.parse(source);
Vector1D actual = vector1DFormat.parse(source);
Assert.assertEquals(Cartesian1D.NaN, actual);
}
@Test
public void testParsePositiveInfinity() throws MathParseException {
String source = "{(Infinity)}";
Cartesian1D actual = vector1DFormat.parse(source);
Vector1D actual = vector1DFormat.parse(source);
Assert.assertEquals(Cartesian1D.POSITIVE_INFINITY, actual);
}
@Test
public void testParseNegativeInfinity() throws MathParseException {
String source = "{(-Infinity)}";
Cartesian1D actual = vector1DFormat.parse(source);
Vector1D actual = vector1DFormat.parse(source);
Assert.assertEquals(Cartesian1D.NEGATIVE_INFINITY, actual);
}