aboutsummaryrefslogtreecommitdiffstats
path: root/document/src/main/java/com/yahoo/document/annotation/AnnotationReferenceDataType.java
blob: 50c69dd59e35cefcc3515f3bbf2e32e1d2a28355 (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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.document.annotation;

import com.yahoo.document.DataType;
import com.yahoo.document.datatypes.FieldValue;

/**
 * A data type describing a field value having a reference to an annotation of a given type.
 *
 * @author Einar M R Rosenvinge
 */
public class AnnotationReferenceDataType extends DataType {

    private AnnotationType aType;

    /**
     * Creates an AnnotationReferenceDataType with a generated id.
     *
     * @param aType the annotation type that AnnotationRefs shall refer to
     */
    public AnnotationReferenceDataType(AnnotationType aType) {
        super("annotationreference<" + ((aType == null) ? "" : aType.getName()) + ">", 0);
        setAnnotationType(aType);
    }

    /**
     * Creates an AnnotationReferenceDataType with a given id.
     *
     * @param aType the annotation type that AnnotationRefs shall refer to
     * @param id    the id to use
     */
    public AnnotationReferenceDataType(AnnotationType aType, int id) {
        super("annotationreference<" + ((aType == null) ? "" : aType.getName()) + ">", id);
        this.aType = aType;
    }

    /**
     * Creates an AnnotationReferenceDataType. WARNING! Do not use!
     */
    protected AnnotationReferenceDataType() {
        super("annotationreference<>", 0);
    }

    private int createId() {
        //TODO: This should be Java's hashCode(), since all other data types use it, and using something else here will probably lead to collisions
        return getName().toLowerCase().hashCode();
    }

    @Override
    public FieldValue createFieldValue() {
        return new AnnotationReference(this);
    }

    @Override
    public Class getValueClass() {
        return AnnotationReference.class;
    }

    @Override
    public boolean isValueCompatible(FieldValue value) {
        if (!(value instanceof AnnotationReference)) {
            return false;
        }
        AnnotationReference reference = (AnnotationReference) value;
        if (equals(reference.getDataType())) {
            return true;
        }
        return false;
    }

    /**
     * Returns the annotation type of this AnnotationReferenceDataType.
     *
     * @return the annotation type of this AnnotationReferenceDataType.
     */
    public AnnotationType getAnnotationType() {
        return aType;
    }

    /**
     * Sets the annotation type that this AnnotationReferenceDataType points to. WARNING! Do not use.
     * @param type the annotation type of this AnnotationReferenceDataType.
     */
    protected void setAnnotationType(AnnotationType type) {
        this.aType = type;
        setId(createId());
    }

}