Skip to content

Commit e1e2171

Browse files
committed
Introduce Vector abstraction.
1 parent fcac591 commit e1e2171

File tree

7 files changed

+802
-0
lines changed

7 files changed

+802
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/*
2+
* Copyright 2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.domain;
17+
18+
import java.util.Arrays;
19+
20+
import org.springframework.util.ObjectUtils;
21+
22+
/**
23+
* {@link Vector} implementation based on {@code double} array.
24+
*
25+
* @author Mark Paluch
26+
* @since 3.5
27+
*/
28+
class DoubleVector implements Vector {
29+
30+
private final double[] v;
31+
32+
public DoubleVector(double[] v) {
33+
this.v = v;
34+
}
35+
36+
/**
37+
* Copy the given {@code double} array and wrap it within a Vector.
38+
*/
39+
static Vector copy(double[] v) {
40+
41+
double[] copy = new double[v.length];
42+
System.arraycopy(v, 0, copy, 0, copy.length);
43+
44+
return new DoubleVector(copy);
45+
}
46+
47+
@Override
48+
public Class<Double> getType() {
49+
return Double.TYPE;
50+
}
51+
52+
@Override
53+
public Object getSource() {
54+
return v;
55+
}
56+
57+
@Override
58+
public int size() {
59+
return v.length;
60+
}
61+
62+
@Override
63+
public float[] toFloatArray() {
64+
65+
float[] copy = new float[this.v.length];
66+
for (int i = 0; i < this.v.length; i++) {
67+
copy[i] = (float) this.v[i];
68+
}
69+
70+
return copy;
71+
}
72+
73+
@Override
74+
public double[] toDoubleArray() {
75+
76+
double[] copy = new double[this.v.length];
77+
System.arraycopy(this.v, 0, copy, 0, copy.length);
78+
79+
return copy;
80+
}
81+
82+
@Override
83+
public boolean equals(Object o) {
84+
85+
if (this == o) {
86+
return true;
87+
}
88+
if (!(o instanceof DoubleVector that)) {
89+
return false;
90+
}
91+
return ObjectUtils.nullSafeEquals(v, that.v);
92+
}
93+
94+
@Override
95+
public int hashCode() {
96+
return Arrays.hashCode(v);
97+
}
98+
99+
@Override
100+
public String toString() {
101+
return "D" + Arrays.toString(v);
102+
}
103+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/*
2+
* Copyright 2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.domain;
17+
18+
import java.util.Arrays;
19+
20+
import org.springframework.util.ObjectUtils;
21+
22+
/**
23+
* {@link Vector} implementation based on {@code float} array.
24+
*
25+
* @author Mark Paluch
26+
* @since 3.5
27+
*/
28+
class FloatVector implements Vector {
29+
30+
private final float[] v;
31+
32+
public FloatVector(float[] v) {
33+
this.v = v;
34+
}
35+
36+
/**
37+
* Copy the given {@code float} array and wrap it within a Vector.
38+
*/
39+
static Vector copy(float[] v) {
40+
41+
float[] copy = new float[v.length];
42+
System.arraycopy(v, 0, copy, 0, copy.length);
43+
44+
return new FloatVector(copy);
45+
}
46+
47+
@Override
48+
public Class<Float> getType() {
49+
return Float.TYPE;
50+
}
51+
52+
@Override
53+
public Object getSource() {
54+
return v;
55+
}
56+
57+
@Override
58+
public int size() {
59+
return v.length;
60+
}
61+
62+
@Override
63+
public float[] toFloatArray() {
64+
65+
float[] copy = new float[this.v.length];
66+
System.arraycopy(this.v, 0, copy, 0, copy.length);
67+
68+
return copy;
69+
}
70+
71+
@Override
72+
public double[] toDoubleArray() {
73+
74+
double[] copy = new double[this.v.length];
75+
for (int i = 0; i < this.v.length; i++) {
76+
copy[i] = this.v[i];
77+
}
78+
79+
return copy;
80+
}
81+
82+
@Override
83+
public boolean equals(Object o) {
84+
85+
if (this == o) {
86+
return true;
87+
}
88+
if (!(o instanceof FloatVector that)) {
89+
return false;
90+
}
91+
return ObjectUtils.nullSafeEquals(v, that.v);
92+
}
93+
94+
@Override
95+
public int hashCode() {
96+
return Arrays.hashCode(v);
97+
}
98+
99+
@Override
100+
public String toString() {
101+
return "F" + Arrays.toString(v);
102+
}
103+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
/*
2+
* Copyright 2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.domain;
17+
18+
import java.util.Arrays;
19+
import java.util.Collection;
20+
21+
import org.springframework.util.ObjectUtils;
22+
23+
/**
24+
* {@link Vector} implementation based on {@link Number} array.
25+
*
26+
* @author Mark Paluch
27+
* @since 3.5
28+
*/
29+
class NumberVector implements Vector {
30+
31+
private final Number[] v;
32+
33+
public NumberVector(Number[] v) {
34+
this.v = v;
35+
}
36+
37+
/**
38+
* Copy the given {@link Number} array and wrap it within a Vector.
39+
*/
40+
static Vector copy(Number[] v) {
41+
42+
Number[] copy = new Number[v.length];
43+
System.arraycopy(v, 0, copy, 0, copy.length);
44+
45+
return new NumberVector(copy);
46+
}
47+
48+
/**
49+
* Copy the given {@link Number} and wrap it within a Vector.
50+
*/
51+
static Vector copy(Collection<Number> numbers) {
52+
53+
Number[] copy = new Number[numbers.size()];
54+
55+
int i = 0;
56+
for (Number number : numbers) {
57+
copy[i++] = number;
58+
}
59+
60+
return new NumberVector(copy);
61+
}
62+
63+
@Override
64+
public Class<? extends Number> getType() {
65+
66+
Class<?> candidate = null;
67+
for (Object val : v) {
68+
if (val != null) {
69+
if (candidate == null) {
70+
candidate = val.getClass();
71+
} else if (candidate != val.getClass()) {
72+
return Number.class;
73+
}
74+
}
75+
}
76+
return (Class<? extends Number>) candidate;
77+
}
78+
79+
@Override
80+
public Object getSource() {
81+
return v;
82+
}
83+
84+
@Override
85+
public int size() {
86+
return v.length;
87+
}
88+
89+
@Override
90+
public float[] toFloatArray() {
91+
92+
float[] copy = new float[this.v.length];
93+
for (int i = 0; i < this.v.length; i++) {
94+
copy[i] = this.v[i].floatValue();
95+
}
96+
97+
return copy;
98+
}
99+
100+
@Override
101+
public double[] toDoubleArray() {
102+
103+
double[] copy = new double[this.v.length];
104+
for (int i = 0; i < this.v.length; i++) {
105+
copy[i] = this.v[i].doubleValue();
106+
}
107+
108+
return copy;
109+
}
110+
111+
@Override
112+
public boolean equals(Object o) {
113+
114+
if (this == o) {
115+
return true;
116+
}
117+
if (!(o instanceof NumberVector that)) {
118+
return false;
119+
}
120+
return ObjectUtils.nullSafeEquals(v, that.v);
121+
}
122+
123+
@Override
124+
public int hashCode() {
125+
return Arrays.hashCode(v);
126+
}
127+
128+
@Override
129+
public String toString() {
130+
return "N" + Arrays.toString(v);
131+
}
132+
}

0 commit comments

Comments
 (0)