Skip to content

Commit 9e2050e

Browse files
committed
Added List<Float> constructor
1 parent bca778f commit 9e2050e

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.1.4 (unreleased)
2+
3+
- Added `List<Float>` constructor
4+
15
## 0.1.3 (2023-07-18)
26

37
- Added support for binary representation

src/main/java/com/pgvector/PGvector.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.sql.Connection;
55
import java.sql.SQLException;
66
import java.util.Arrays;
7+
import java.util.List;
78
import org.postgresql.PGConnection;
89
import org.postgresql.util.ByteConverter;
910
import org.postgresql.util.PGBinaryObject;
@@ -30,6 +31,22 @@ public PGvector(float[] v) {
3031
vec = v;
3132
}
3233

34+
/**
35+
* Constructor
36+
*/
37+
public PGvector(List<Float> v) {
38+
this();
39+
if (v == null) {
40+
vec = null;
41+
} else {
42+
vec = new float[v.size()];
43+
int i = 0;
44+
for (Float f : v) {
45+
vec[i++] = f.floatValue();
46+
}
47+
}
48+
}
49+
3350
/**
3451
* Constructor
3552
*/

src/test/java/com/pgvector/PGvectorTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.pgvector;
22

33
import java.sql.SQLException;
4+
import java.util.Arrays;
45
import com.pgvector.PGvector;
56
import org.junit.jupiter.api.Test;
67

@@ -19,6 +20,13 @@ void testStringConstructor() throws SQLException {
1920
assertArrayEquals(new float[] {1, 2, 3}, vec.toArray());
2021
}
2122

23+
@Test
24+
void testListConstructor() {
25+
Float[] a = new Float[] {Float.valueOf(1), Float.valueOf(2), Float.valueOf(3)};
26+
PGvector vec = new PGvector(Arrays.asList(a));
27+
assertArrayEquals(new float[] {1, 2, 3}, vec.toArray());
28+
}
29+
2230
@Test
2331
void testGetValue() throws SQLException {
2432
PGvector vec = new PGvector(new float[] {1, 2, 3});

0 commit comments

Comments
 (0)