001/*
002 *  Copyright (c) 2023-2026, Agents-Flex (fuhai999@gmail.com).
003 *  <p>
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *  <p>
008 *  http://www.apache.org/licenses/LICENSE-2.0
009 *  <p>
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016package com.agentsflex.core.store;
017
018import com.agentsflex.core.util.Metadata;
019
020import java.util.ArrayList;
021import java.util.Arrays;
022import java.util.Collection;
023import java.util.List;
024
025
026public class VectorData extends Metadata {
027
028    protected float[] vector;
029
030    /**
031     * 0 ~ 1, 数值越大,相似度越高
032     */
033    protected Float score;
034
035    public float[] getVector() {
036        return vector;
037    }
038
039    public void setVector(float[] vector) {
040        this.vector = vector;
041    }
042
043    public List<Float> getVectorAsList() {
044        if (vector == null) {
045            return null;
046        }
047        List<Float> result = new ArrayList<>(vector.length);
048        for (float v : vector) {
049            result.add(v);
050        }
051        return result;
052    }
053
054    public List<Double> getVectorAsDoubleList() {
055        if (vector == null) {
056            return null;
057        }
058        List<Double> result = new ArrayList<>(vector.length);
059        for (float v : vector) {
060            result.add((double) v);
061        }
062        return result;
063    }
064
065    public double[] getVectorAsDoubleArray() {
066        if (vector == null) {
067            return null;
068        }
069        double[] result = new double[vector.length];
070        for (int i = 0; i < vector.length; i++) {
071            result[i] = vector[i];
072        }
073        return result;
074    }
075
076    public void setVectorByNumbers(Collection<? extends Number> vector) {
077        if (vector == null || vector.isEmpty()) {
078            this.vector = null;
079        } else {
080            this.vector = new float[vector.size()];
081            int index = 0;
082            for (Number num : vector) {
083                this.vector[index++] = num.floatValue();
084            }
085        }
086    }
087
088    public Float getScore() {
089        return score;
090    }
091
092    public void setScore(Float score) {
093        this.score = score;
094    }
095
096    @Override
097    public String toString() {
098        return "VectorData{" +
099            "vector=" + Arrays.toString(vector) +
100            ", score=" + score +
101            ", metadataMap=" + metadataMap +
102            '}';
103    }
104}