Skip to content

Commit 745bec8

Browse files
committed
chore: improve Vectors::toString representation
1 parent ce15aa1 commit 745bec8

2 files changed

Lines changed: 44 additions & 12 deletions

File tree

src/main/java/io/weaviate/client6/v1/api/collections/Vectors.java

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.weaviate.client6.v1.api.collections;
22

33
import java.io.IOException;
4+
import java.util.Arrays;
45
import java.util.Collections;
56
import java.util.HashMap;
67
import java.util.Map;
@@ -15,17 +16,13 @@
1516
import com.google.gson.stream.JsonReader;
1617
import com.google.gson.stream.JsonWriter;
1718

18-
import io.weaviate.client6.v1.internal.ObjectBuilder;
19-
import lombok.ToString;
20-
2119
/**
2220
* Vectors is an abstraction over named vectors, which can store
2321
* both 1-dimensional and 2-dimensional vectors.
2422
*/
25-
@ToString
2623
public class Vectors {
2724
/** Elements of this map must only be {@code float[]} or {@code float[][]}. */
28-
private final Map<String, Object> namedVectors;
25+
private final Map<String, Object> vectorsMap;
2926

3027
/** Create a 1-dimensional vector. */
3128
public static Vectors of(float[] vector) {
@@ -58,7 +55,7 @@ public static Vectors of(String name, float[][] vector) {
5855
* @param vector {@code float[]} or {@code float[][]} vector.
5956
*/
6057
private Vectors(String name, Object vector) {
61-
this.namedVectors = Collections.singletonMap(name, vector);
58+
this.vectorsMap = Collections.singletonMap(name, vector);
6259
}
6360

6461
/**
@@ -72,7 +69,7 @@ private Vectors(String name, Object vector) {
7269
* @param vector Map of named vectors.
7370
*/
7471
private Vectors(Map<String, Object> namedVectors) {
75-
this.namedVectors = namedVectors;
72+
this.vectorsMap = namedVectors;
7673
}
7774

7875
/** Merge all vectors in a single vector map. */
@@ -81,7 +78,7 @@ public Vectors(Vectors... vectors) {
8178
for (var vec : vectors) {
8279
namedVectors.putAll(vec.asMap());
8380
}
84-
this.namedVectors = namedVectors;
81+
this.vectorsMap = namedVectors;
8582
}
8683

8784
/**
@@ -91,7 +88,7 @@ public Vectors(Vectors... vectors) {
9188
* @throws ClassCastException The underlying vector is not a {@code float[]}.
9289
*/
9390
public float[] getSingle(String name) {
94-
return (float[]) namedVectors.get(name);
91+
return (float[]) vectorsMap.get(name);
9592
}
9693

9794
/**
@@ -112,7 +109,7 @@ public float[] getDefaultSingle() {
112109
* {@code float[][]}.
113110
*/
114111
public float[][] getMulti(String name) {
115-
return (float[][]) namedVectors.get(name);
112+
return (float[][]) vectorsMap.get(name);
116113
}
117114

118115
/**
@@ -134,7 +131,22 @@ public float[][] getDefaultMulti() {
134131
* @return Map of name-vector pairs. The returned map is immutable.
135132
*/
136133
public Map<String, Object> asMap() {
137-
return Map.copyOf(namedVectors);
134+
return Map.copyOf(vectorsMap);
135+
}
136+
137+
@Override
138+
public String toString() {
139+
var vectorStrings = vectorsMap.entrySet().stream()
140+
.map(v -> {
141+
var name = v.getKey();
142+
var value = v.getValue();
143+
var array = (value instanceof float[] f)
144+
? Arrays.toString((float[]) value)
145+
: Arrays.deepToString((float[][]) value);
146+
return "%s=%s".formatted(name, array);
147+
})
148+
.toList();
149+
return "Vectors(%s)".formatted(String.join(", ", vectorStrings));
138150
}
139151

140152
public static enum CustomTypeAdapterFactory implements TypeAdapterFactory {
@@ -154,7 +166,7 @@ public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
154166

155167
@Override
156168
public void write(JsonWriter out, Vectors value) throws IOException {
157-
mapAdapter.write(out, value.namedVectors);
169+
mapAdapter.write(out, value.vectorsMap);
158170
}
159171

160172
@Override
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package io.weaviate.client6.v1.api.collections;
2+
3+
import org.assertj.core.api.Assertions;
4+
import org.junit.Test;
5+
6+
public class VectorsTest {
7+
@Test
8+
public void testToString_1d() {
9+
var vector = Vectors.of(new float[] { 1, 2, 3 });
10+
var got = vector.toString();
11+
Assertions.assertThat(got).isEqualTo("Vectors(default=[1.0, 2.0, 3.0])");
12+
}
13+
14+
@Test
15+
public void testToString_2d() {
16+
var vector = Vectors.of(new float[][] { { 1, 2, 3 }, { 1, 2, 3 } });
17+
var got = vector.toString();
18+
Assertions.assertThat(got).isEqualTo("Vectors(default=[[1.0, 2.0, 3.0], [1.0, 2.0, 3.0]])");
19+
}
20+
}

0 commit comments

Comments
 (0)