Skip to content

Commit 231f407

Browse files
committed
Polishing.
Refine object creation based on common element type to preserve comparison.
1 parent e1e2171 commit 231f407

File tree

4 files changed

+55
-26
lines changed

4 files changed

+55
-26
lines changed

src/main/java/org/springframework/data/domain/DoubleVector.java

+15
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package org.springframework.data.domain;
1717

1818
import java.util.Arrays;
19+
import java.util.Collection;
1920

2021
import org.springframework.util.ObjectUtils;
2122

@@ -44,6 +45,20 @@ static Vector copy(double[] v) {
4445
return new DoubleVector(copy);
4546
}
4647

48+
/**
49+
* Copy the given numeric values and wrap within a Vector.
50+
*/
51+
static Vector copy(Collection<Number> v) {
52+
53+
double[] copy = new double[v.size()];
54+
int i = 0;
55+
for (Number number : v) {
56+
copy[i++] = number.doubleValue();
57+
}
58+
59+
return new DoubleVector(copy);
60+
}
61+
4762
@Override
4863
public Class<Double> getType() {
4964
return Double.TYPE;

src/main/java/org/springframework/data/domain/FloatVector.java

+15
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package org.springframework.data.domain;
1717

1818
import java.util.Arrays;
19+
import java.util.Collection;
1920

2021
import org.springframework.util.ObjectUtils;
2122

@@ -44,6 +45,20 @@ static Vector copy(float[] v) {
4445
return new FloatVector(copy);
4546
}
4647

48+
/**
49+
* Copy the given numeric values and wrap within a Vector.
50+
*/
51+
static Vector copy(Collection<Number> v) {
52+
53+
float[] copy = new float[v.size()];
54+
int i = 0;
55+
for (Number number : v) {
56+
copy[i++] = number.floatValue();
57+
}
58+
59+
return new FloatVector(copy);
60+
}
61+
4762
@Override
4863
public Class<Float> getType() {
4964
return Float.TYPE;

src/main/java/org/springframework/data/domain/Vector.java

+10-13
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import java.util.Collection;
1919

2020
import org.springframework.util.Assert;
21+
import org.springframework.util.CollectionUtils;
2122

2223
/**
2324
* A vector is a fixed-length array of non-null numeric values. Vectors are represent a point in a multidimensional
@@ -66,28 +67,24 @@ static Vector of(double... values) {
6667

6768
/**
6869
* Creates a new {@link Vector} from the given number {@code values}. Vector values are duplicated to avoid capturing
69-
* a mutable array instance and to prevent mutability.
70+
* a mutable collection instance and to prevent mutability.
7071
*
7172
* @param values number vector values.
7273
* @return the {@link Vector} for the given vector values.
7374
*/
74-
static Vector of(Number... values) {
75+
static Vector of(Collection<Number> values) {
7576

7677
Assert.notNull(values, "Vector values must not be null");
7778

78-
return NumberVector.copy(values);
79-
}
79+
Class<?> cet = CollectionUtils.findCommonElementType(values);
8080

81-
/**
82-
* Creates a new {@link Vector} from the given number {@code values}. Vector values are duplicated to avoid capturing
83-
* a mutable collection instance and to prevent mutability.
84-
*
85-
* @param values number vector values.
86-
* @return the {@link Vector} for the given vector values.
87-
*/
88-
static Vector of(Collection<Number> values) {
81+
if (cet == Double.class) {
82+
return DoubleVector.copy(values);
83+
}
8984

90-
Assert.notNull(values, "Vector values must not be null");
85+
if (cet == Float.class) {
86+
return FloatVector.copy(values);
87+
}
9188

9289
return NumberVector.copy(values);
9390
}

src/test/java/org/springframework/data/domain/NumberVectorUnitTests.java

+15-13
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
import static org.assertj.core.api.Assertions.*;
1919

20+
import java.util.Arrays;
21+
2022
import org.junit.jupiter.api.Test;
2123

2224
/**
@@ -26,41 +28,41 @@
2628
*/
2729
class NumberVectorUnitTests {
2830

29-
Number[] values = new Number[] { 1.1, 2.2, 3.3, 4.4, 5.5 };
30-
Number[] floats = new Number[] { (float) 1.1d, (float) 2.2d, (float) 3.3d, (float) 4.4d, (float) 5.5d };
31+
Number[] values = new Number[] { 1.1, 2.2, 3.3, 4.4, 5.5, 6.6f };
32+
Number[] floats = new Number[] { (float) 1.1d, (float) 2.2d, (float) 3.3d, (float) 4.4d, (float) 5.5, 6.6 };
3133

3234
@Test // GH-3193
3335
void shouldCreateVector() {
3436

35-
Vector vector = Vector.of(values);
37+
Vector vector = Vector.of(Arrays.asList(values));
3638

37-
assertThat(vector.size()).isEqualTo(5);
38-
assertThat(vector.getType()).isEqualTo(Double.class);
39+
assertThat(vector.size()).isEqualTo(6);
40+
assertThat(vector.getType()).isEqualTo(Number.class);
3941
}
4042

4143
@Test // GH-3193
4244
void shouldCopyVectorValues() {
4345

44-
Vector vector = Vector.of(values);
46+
Vector vector = Vector.of(Arrays.asList(values));
4547

4648
assertThat(vector.getSource()).isNotSameAs(vector).isEqualTo(values);
4749
}
4850

4951
@Test // GH-3193
5052
void shouldRenderToString() {
5153

52-
Vector vector = Vector.of(values);
54+
Vector vector = Vector.of(Arrays.asList(values));
5355

54-
assertThat(vector).hasToString("N[1.1, 2.2, 3.3, 4.4, 5.5]");
56+
assertThat(vector).hasToString("N[1.1, 2.2, 3.3, 4.4, 5.5, 6.6]");
5557
}
5658

5759
@Test // GH-3193
5860
void shouldCompareVector() {
5961

60-
Vector vector = Vector.of(values);
62+
Vector vector = Vector.of(Arrays.asList(values));
6163

62-
assertThat(vector).isEqualTo(Vector.of(values));
63-
assertThat(vector).hasSameHashCodeAs(Vector.of(values));
64+
assertThat(vector).isEqualTo(Vector.of(Arrays.asList(values)));
65+
assertThat(vector).hasSameHashCodeAs(Vector.of(Arrays.asList(values)));
6466
}
6567

6668
@Test // GH-3193
@@ -74,7 +76,7 @@ void sourceShouldReturnSource() {
7476
@Test // GH-3193
7577
void shouldCreateFloatArray() {
7678

77-
Vector vector = Vector.of(values);
79+
Vector vector = Vector.of(Arrays.asList(values));
7880

7981
float[] values = new float[this.floats.length];
8082
for (int i = 0; i < values.length; i++) {
@@ -87,7 +89,7 @@ void shouldCreateFloatArray() {
8789
@Test // GH-3193
8890
void shouldCreateDoubleArray() {
8991

90-
Vector vector = Vector.of(values);
92+
Vector vector = Vector.of(Arrays.asList(values));
9193

9294
double[] values = new double[this.values.length];
9395
for (int i = 0; i < values.length; i++) {

0 commit comments

Comments
 (0)