2424import org .springframework .util .ObjectUtils ;
2525
2626/**
27- * MongoDB-specific extension to {@link Vector} based on Mongo's {@link BinaryVector}. Note that only float32 and int8
28- * variants can be represented as floating-point numbers. int1 returns an all-zero array for {@link #toFloatArray()} and
29- * {@link #toDoubleArray()}.
27+ * MongoDB-specific extension to {@link Vector} based on Mongo's {@link BinaryVector}. Note that only {@code float32}
28+ * and {@code int8} variants can be represented as floating-point numbers. {@code int1} throws
29+ * {@link UnsupportedOperationException} when calling {@link #toFloatArray()} and {@link #toDoubleArray()}.
3030 *
3131 * @author Mark Paluch
3232 * @since 4.5
@@ -40,15 +40,65 @@ public class MongoVector implements Vector {
4040 }
4141
4242 /**
43- * Creates a new {@link MongoVector} from the given {@link BinaryVector}.
43+ * Creates a new binary {@link MongoVector} using the given {@link BinaryVector}.
4444 *
4545 * @param v binary vector representation.
46- * @return the {@link MongoVector} for the given vector values .
46+ * @return the {@link MongoVector} wrapping {@link BinaryVector} .
4747 */
4848 public static MongoVector of (BinaryVector v ) {
4949 return new MongoVector (v );
5050 }
5151
52+ /**
53+ * Creates a new binary {@link MongoVector} using the given {@code data}.
54+ * <p>
55+ * A {@link BinaryVector.DataType#INT8} vector is a vector of 8-bit signed integers where each byte in the vector
56+ * represents an element of a vector, with values in the range {@code [-128, 127]}.
57+ * <p>
58+ * NOTE: The byte array is not copied; changes to the provided array will be referenced in the created
59+ * {@code MongoVector} instance.
60+ *
61+ * @param data the byte array representing the {@link BinaryVector.DataType#INT8} vector data.
62+ * @return the {@link MongoVector} containing the given vector values to be represented as binary {@code int8}.
63+ */
64+ public static MongoVector ofInt8 (byte [] data ) {
65+ return of (BinaryVector .int8Vector (data ));
66+ }
67+
68+ /**
69+ * Creates a new binary {@link MongoVector} using the given {@code data}.
70+ * <p>
71+ * A {@link BinaryVector.DataType#FLOAT32} vector is a vector of floating-point numbers, where each element in the
72+ * vector is a {@code float}.
73+ * <p>
74+ * NOTE: The float array is not copied; changes to the provided array will be referenced in the created
75+ * {@code MongoVector} instance.
76+ *
77+ * @param data the float array representing the {@link BinaryVector.DataType#FLOAT32} vector data.
78+ * @return the {@link MongoVector} containing the given vector values to be represented as binary {@code float32}.
79+ */
80+ public static MongoVector ofFloat (float ... data ) {
81+ return of (BinaryVector .floatVector (data ));
82+ }
83+
84+ /**
85+ * Creates a new binary {@link MongoVector} from the given {@link Vector}.
86+ * <p>
87+ * A {@link BinaryVector.DataType#FLOAT32} vector is a vector of floating-point numbers, where each element in the
88+ * vector is a {@code float}. The given {@link Vector} must be able to return a {@link Vector#toFloatArray() float}
89+ * array.
90+ * <p>
91+ * NOTE: The float array is not copied; changes to the provided array will be referenced in the created
92+ * {@code MongoVector} instance.
93+ *
94+ * @param v the
95+ * @return the {@link MongoVector} using vector values from the given {@link Vector} to be represented as binary
96+ * float32.
97+ */
98+ public static MongoVector fromFloat (Vector v ) {
99+ return of (BinaryVector .floatVector (v .toFloatArray ()));
100+ }
101+
52102 @ Override
53103 public Class <? extends Number > getType () {
54104
@@ -90,6 +140,11 @@ public int size() {
90140 return 0 ;
91141 }
92142
143+ /**
144+ * {@inheritDoc}
145+ *
146+ * @throws UnsupportedOperationException if the underlying data type is {@code int1} {@link PackedBitBinaryVector}.
147+ */
93148 @ Override
94149 public float [] toFloatArray () {
95150
@@ -102,14 +157,22 @@ public float[] toFloatArray() {
102157
103158 if (v instanceof Int8BinaryVector i ) {
104159
105- float [] result = new float [i .getData ().length ];
106- System .arraycopy (i .getData (), 0 , result , 0 , result .length );
160+ byte [] data = i .getData ();
161+ float [] result = new float [data .length ];
162+ for (int j = 0 ; j < data .length ; j ++) {
163+ result [j ] = data [j ];
164+ }
107165 return result ;
108166 }
109167
110- return new float [ size ()] ;
168+ throw new UnsupportedOperationException ( "Cannot return float array for " + v . getClass ()) ;
111169 }
112170
171+ /**
172+ * {@inheritDoc}
173+ *
174+ * @throws UnsupportedOperationException if the underlying data type is {@code int1} {@link PackedBitBinaryVector}.
175+ */
113176 @ Override
114177 public double [] toDoubleArray () {
115178
@@ -126,12 +189,15 @@ public double[] toDoubleArray() {
126189
127190 if (v instanceof Int8BinaryVector i ) {
128191
129- double [] result = new double [i .getData ().length ];
130- System .arraycopy (i .getData (), 0 , result , 0 , result .length );
192+ byte [] data = i .getData ();
193+ double [] result = new double [data .length ];
194+ for (int j = 0 ; j < data .length ; j ++) {
195+ result [j ] = data [j ];
196+ }
131197 return result ;
132198 }
133199
134- return new double [ size ()] ;
200+ throw new UnsupportedOperationException ( "Cannot return double array for " + v . getClass ()) ;
135201 }
136202
137203 @ Override
0 commit comments