aboutsummaryrefslogtreecommitdiffstats
path: root/document/src/main/java/com/yahoo/document/TensorDataType.java
blob: a5dc4a98dad94dc7905cffbf604d6d4c45bed489 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.document;

import com.yahoo.document.datatypes.FieldValue;
import com.yahoo.document.datatypes.TensorFieldValue;
import com.yahoo.tensor.TensorType;
import com.yahoo.vespa.objects.Ids;

import java.util.Objects;

/**
 * A DataType containing a tensor type
 *
 * @author bratseth
 */
public class TensorDataType extends DataType {

    private final TensorType tensorType;

    // The global class identifier shared with C++.
    public static int classId = registerClass(Ids.document + 59, TensorDataType.class);

    public TensorDataType(TensorType tensorType) {
        super(tensorType == null ? "tensor" : tensorType.toString(), DataType.tensorDataTypeCode);
        this.tensorType = tensorType;
    }

    @Override
    public TensorDataType clone() {
        return (TensorDataType)super.clone();
    }

    @Override
    public FieldValue createFieldValue() {
        return new TensorFieldValue(tensorType);
    }

    @Override
    public Class<? extends TensorFieldValue> getValueClass() {
        return TensorFieldValue.class;
    }

    @Override
    public boolean isValueCompatible(FieldValue value) {
        if (value == null) return false;
        if ( ! TensorFieldValue.class.isAssignableFrom(value.getClass())) return false;
        TensorFieldValue tensorValue = (TensorFieldValue)value;
        return tensorType.isConvertibleTo(tensorValue.getDataType().getTensorType());
    }

    /** Returns the type of the tensor this field can hold */
    public TensorType getTensorType() { return tensorType; }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        if (!super.equals(o)) return false;
        TensorDataType that = (TensorDataType) o;
        return Objects.equals(tensorType, that.tensorType);
    }

    @Override
    public int hashCode() {
        return Objects.hash(super.hashCode(), tensorType);
    }

}