Skip to content

Introduce Vector abstraction #3194

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-commons</artifactId>
<version>3.5.0-SNAPSHOT</version>
<version>3.5.0-GH-3193-SNAPSHOT</version>

<name>Spring Data Core</name>
<description>Core Spring concepts underpinning every Spring Data module.</description>
Expand Down
119 changes: 119 additions & 0 deletions src/main/java/org/springframework/data/domain/DoubleVector.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/*
* Copyright 2025 the original author or authors.
*
* Licensed 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
*
* https://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.springframework.data.domain;

import java.util.Arrays;
import java.util.Collection;

import org.springframework.util.ObjectUtils;

/**
* {@link Vector} implementation based on {@code double} array.
*
* @author Mark Paluch
* @since 3.5
*/
class DoubleVector implements Vector {

private final double[] v;

DoubleVector(double[] v) {
this.v = v;
}

/**
* Copy the given {@code double} array and wrap it within a Vector.
*/
static Vector copy(double[] v) {

if (v.length == 0) {
return new DoubleVector(new double[0]);
}

return new DoubleVector(Arrays.copyOf(v, v.length));
}

/**
* Copy the given numeric values and wrap within a Vector.
*/
static Vector copy(Collection<? extends Number> v) {

if (v.isEmpty()) {
return new DoubleVector(new double[0]);
}

double[] copy = new double[v.size()];
int i = 0;
for (Number number : v) {
copy[i++] = number.doubleValue();
}

return new DoubleVector(copy);
}

@Override
public Class<Double> getType() {
return Double.TYPE;
}

@Override
public Object getSource() {
return v;
}

@Override
public int size() {
return v.length;
}

@Override
public float[] toFloatArray() {

float[] copy = new float[this.v.length];
for (int i = 0; i < this.v.length; i++) {
copy[i] = (float) this.v[i];
}

return copy;
}

@Override
public double[] toDoubleArray() {
return Arrays.copyOf(this.v, this.v.length);
}

@Override
public boolean equals(Object o) {

if (this == o) {
return true;
}
if (!(o instanceof DoubleVector that)) {
return false;
}
return ObjectUtils.nullSafeEquals(v, that.v);
}

@Override
public int hashCode() {
return Arrays.hashCode(v);
}

@Override
public String toString() {
return "D" + Arrays.toString(v);
}
}
119 changes: 119 additions & 0 deletions src/main/java/org/springframework/data/domain/FloatVector.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/*
* Copyright 2025 the original author or authors.
*
* Licensed 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
*
* https://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.springframework.data.domain;

import java.util.Arrays;
import java.util.Collection;

import org.springframework.util.ObjectUtils;

/**
* {@link Vector} implementation based on {@code float} array.
*
* @author Mark Paluch
* @since 3.5
*/
class FloatVector implements Vector {

private final float[] v;

FloatVector(float[] v) {
this.v = v;
}

/**
* Copy the given {@code float} array and wrap it within a Vector.
*/
static Vector copy(float[] v) {

if (v.length == 0) {
return new FloatVector(new float[0]);
}

return new FloatVector(Arrays.copyOf(v, v.length));
}

/**
* Copy the given numeric values and wrap within a Vector.
*/
static Vector copy(Collection<? extends Number> v) {

if (v.isEmpty()) {
return new FloatVector(new float[0]);
}

float[] copy = new float[v.size()];
int i = 0;
for (Number number : v) {
copy[i++] = number.floatValue();
}

return new FloatVector(copy);
}

@Override
public Class<Float> getType() {
return Float.TYPE;
}

@Override
public Object getSource() {
return v;
}

@Override
public int size() {
return v.length;
}

@Override
public float[] toFloatArray() {
return Arrays.copyOf(this.v, this.v.length);
}

@Override
public double[] toDoubleArray() {

double[] copy = new double[this.v.length];
for (int i = 0; i < this.v.length; i++) {
copy[i] = this.v[i];
}

return copy;
}

@Override
public boolean equals(Object o) {

if (this == o) {
return true;
}
if (!(o instanceof FloatVector that)) {
return false;
}
return ObjectUtils.nullSafeEquals(v, that.v);
}

@Override
public int hashCode() {
return Arrays.hashCode(v);
}

@Override
public String toString() {
return "F" + Arrays.toString(v);
}
}
133 changes: 133 additions & 0 deletions src/main/java/org/springframework/data/domain/NumberVector.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
/*
* Copyright 2025 the original author or authors.
*
* Licensed 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
*
* https://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.springframework.data.domain;

import java.util.Arrays;
import java.util.Collection;

import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;

/**
* {@link Vector} implementation based on {@link Number} array.
*
* @author Mark Paluch
* @since 3.5
*/
class NumberVector implements Vector {

private final Number[] v;

NumberVector(Number[] v) {

Assert.noNullElements(v, "Vector [v] must not contain null elements");
this.v = v;
}

/**
* Copy the given {@link Number} array and wrap it within a Vector.
*/
static Vector copy(Number[] v) {

if (v.length == 0) {
return new NumberVector(new Number[0]);
}

return new NumberVector(Arrays.copyOf(v, v.length));
}

/**
* Copy the given {@link Number} and wrap it within a Vector.
*/
static Vector copy(Collection<? extends Number> v) {

if (v.isEmpty()) {
return new NumberVector(new Number[0]);
}

return new NumberVector(v.toArray(Number[]::new));
}

@Override
public Class<? extends Number> getType() {

if (this.v.length == 0) {
return Number.class;
}

Class<? extends Number> candidate = this.v[0].getClass();
for (int i = 1; i < this.v.length; i++) {
if (candidate != this.v[i].getClass()) {
return Number.class;
}
}
return candidate;
}

@Override
public Object getSource() {
return v;
}

@Override
public int size() {
return v.length;
}

@Override
public float[] toFloatArray() {

float[] copy = new float[this.v.length];
for (int i = 0; i < this.v.length; i++) {
copy[i] = this.v[i].floatValue();
}

return copy;
}

@Override
public double[] toDoubleArray() {

double[] copy = new double[this.v.length];
for (int i = 0; i < this.v.length; i++) {
copy[i] = this.v[i].doubleValue();
}

return copy;
}

@Override
public boolean equals(Object o) {

if (this == o) {
return true;
}
if (!(o instanceof NumberVector that)) {
return false;
}
return ObjectUtils.nullSafeEquals(v, that.v);
}

@Override
public int hashCode() {
return Arrays.hashCode(v);
}

@Override
public String toString() {
return "N" + Arrays.toString(v);
}
}
Loading