aboutsummaryrefslogtreecommitdiffstats
path: root/document/src/main/java/com/yahoo/document/annotation/AnnotationReference.java
blob: 924f401f8be2586d9e8c1a4c5f11dfb5c7b3c47c (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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// Copyright Yahoo. 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.Field;
import com.yahoo.document.datatypes.FieldValue;
import com.yahoo.document.serialization.FieldReader;
import com.yahoo.document.serialization.FieldWriter;
import com.yahoo.document.serialization.XmlStream;
import com.yahoo.vespa.objects.Ids;

/**
 * A FieldValue which holds a reference to an annotation of a specified type.
 *
 * @see Annotation#setFieldValue(com.yahoo.document.datatypes.FieldValue)
 * @author Einar M R Rosenvinge
 */
public class AnnotationReference extends FieldValue {

    public static int classId = registerClass(Ids.annotation + 2, AnnotationReference.class);
    private Annotation reference;
    private AnnotationReferenceDataType dataType;

    /**
     * Constructs a new AnnotationReference, with a reference to the given {@link Annotation}.
     *
     * @param type      the data type of this AnnotationReference
     * @param reference the reference to set
     * @throws IllegalArgumentException if the given annotation has a type that is not compatible with this reference
     */
    public AnnotationReference(AnnotationReferenceDataType type, Annotation reference) {
        this.dataType = type;
        setReference(reference);
    }

    /**
     * Constructs a new AnnotationReference.
     *
     * @param type the data type of this AnnotationReference
     */
    public AnnotationReference(AnnotationReferenceDataType type) {
        this(type, null);
    }

    /**
     * Clones this AnnotationReference. Note: No deep-copying, so the AnnotationReference returned
     * refers to the same Annotation as this AnnotationReference.
     *
     * @return a copy of this object, referring to the same Annotation instance.
     */
    @Override
    public AnnotationReference clone() {
        return (AnnotationReference) super.clone();
        //do not clone annotation that we're referring to. See wizardry in SpanTree for that.
    }

    /**
     * Returns the Annotation that this AnnotationReference refers to.
     *
     * @return the Annotation that this AnnotationReference refers to.
     */
    public Annotation getReference() {
        return reference;
    }

    @Override
    public void assign(Object o) {
        if (o != null && (!(o instanceof Annotation))) {
            throw new IllegalArgumentException("Cannot assign object of type " + o.getClass().getName() + " to an AnnotationReference, must be of type " + Annotation.class.getName());
        }
        setReference((Annotation) o);
    }

    /**
     * Set an {@link Annotation} that this AnnotationReference shall refer to.
     *
     * @param reference an Annotation that this AnnotationReference shall refer to.
     * @throws IllegalArgumentException if the given annotation has a type that is not compatible with this reference
     */
    public void setReference(Annotation reference) {
        if (reference == null) {
            this.reference = null;
            return;
        }
        AnnotationReferenceDataType type = getDataType();
        if (type.getAnnotationType().isValueCompatible(reference)
                // The case if concrete annotation type
                || reference.getType() instanceof AnnotationType) {
            this.reference = reference;
        } else {
            throw new IllegalArgumentException("Cannot set reference, must be of type " + type + " (was of type " + reference.getType() + ")");
        }
    }


    /**
     * WARNING! Only to be used by deserializers when reference is not fully deserialized yet! Sets
     * an {@link Annotation} that this AnnotationReference shall refer to.
     *
     * @param reference an Annotation that this AnnotationReference shall refer to.
     * @throws IllegalArgumentException if the given annotation has a type that is not compatible with this reference
     */
    public void setReferenceNoCompatibilityCheck(Annotation reference) {
        if (reference == null) {
            this.reference = null;
            return;
        }
        this.reference = reference;
    }

    @Override
    public AnnotationReferenceDataType getDataType() {
        return dataType;
    }

    public void setDataType(DataType dataType) {
        if (dataType instanceof AnnotationReferenceDataType) {
            this.dataType = (AnnotationReferenceDataType) dataType;
        } else {
            throw new IllegalArgumentException("Cannot set dataType to " + dataType + ", must be of type AnnotationReferenceDataType.");
        }
    }

    @Override
    @Deprecated
    public void printXml(XmlStream xml) {
        // TODO: Implement AnnotationReference.printXml()
    }

    @Override
    public void clear() {
        this.reference = null;
    }

    @Override
    public void serialize(Field field, FieldWriter writer) {
        writer.write(field, this);
    }

    @Override
    public void deserialize(Field field, FieldReader reader) {
        reader.read(field, this);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof AnnotationReference)) return false;
        if (!super.equals(o)) return false;

        AnnotationReference that = (AnnotationReference) o;

        if (reference != null ? !reference.toString().equals(that.reference.toString()) : that.reference != null) {
            return false;
        }

        return true;
    }

    @Override
    public int hashCode() {
        int result = super.hashCode();
        result = 31 * result + (reference != null ? reference.toString().hashCode() : 0);
        return result;
    }

    @Override
    public String toString() {
        return "AnnotationReference " + getDataType() + " referring to " + reference;
    }

    @Override
    public int compareTo(FieldValue fieldValue) {
        int comp = super.compareTo(fieldValue);
        if (comp == 0) {
            //types are equal, this must be of this type
            AnnotationReference value = (AnnotationReference) fieldValue;
            if (reference == null) {
                comp = (value.reference == null) ? 0 : -1;
            } else {
                comp = (value.reference == null) ? 1 : (reference.toString().compareTo(value.reference.toString()));
            }
        }
        return comp;
    }

}