aboutsummaryrefslogtreecommitdiffstats
path: root/document/src/main/java/com/yahoo/document/annotation/AnnotationType.java
blob: 88e6bd7822a3432252833d08a04366cb3ea1b75a (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
// 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.collections.MD5;
import com.yahoo.document.DataType;

import java.util.Collection;
import java.util.List;

/**
 * An AnnotationType describes a certain type of annotations; they are
 * generally distinguished by a name, an id, and an optional data type.
 * <p>
 * If an AnnotationType has a {@link com.yahoo.document.DataType}, this means that {@link Annotation}s of
 * that type are allowed to have a {@link com.yahoo.document.datatypes.FieldValue} of the given
 * {@link com.yahoo.document.DataType} as an optional payload.
 *
 * @author Einar M R Rosenvinge
 */
public class AnnotationType implements Comparable<AnnotationType> {

    private final int id;
    private final String name;
    private DataType dataType;
    private AnnotationType superType = null;

    /**
     * Creates a new annotation type that cannot have values (hence no data type).
     *
     * @param name the name of the new annotation type
     */
    public AnnotationType(String name) {
        this(name, null);
    }

    /**
     * Creates a new annotation type that can have values of the specified type.
     *
     * @param name     the name of the new annotation type
     * @param dataType the data type of the annotation value
     */
    public AnnotationType(String name, DataType dataType) {
        this.name = name;
        this.dataType = dataType;
        //always keep this as last statement in here:
        this.id = computeHash();
    }

    /**
     * Creates a new annotation type that can have values of the specified type.
     *
     * @param name     the name of the new annotation type
     * @param dataType the data type of the annotation value
     * @param id   the ID of the new annotation type
     */
    public AnnotationType(String name, DataType dataType, int id) {
        this.name = name;
        this.dataType = dataType;
        this.id = id;
    }

    /**
     * Creates a new annotation type, with the specified ID. WARNING! Only to be used by configuration
     * system, do not use!!
     *
     * @param name the name of the new annotation type
     * @param id   the ID of the new annotation type
     */
    public AnnotationType(String name, int id) {
        this.id = id;
        this.name = name;
    }

    /** Returns the name of this annotation. */
    public String getName() {
        return name;
    }

    /** Returns the data type of this annotation, if any. */
    public DataType getDataType() {
        return dataType;
    }

    /**
     * Sets the data type of this annotation. WARNING! Only to be used by configuration
     * system, do not use!!
     *
     * @param dataType the data type of the annotation value
     */
    public void setDataType(DataType dataType) {
        this.dataType = dataType;
    }

    /** Returns the ID of this annotation. */
    public int getId() {
        return id;
    }

    private int computeHash() {
        return new MD5().hash(name);
    }

    public boolean isValueCompatible(Annotation structValue) {
        if (structValue.getType().inherits(this)) {
            //the value is of this type; or the supertype of the value is of this type, etc....
            return true;
        }
        return false;
    }

    /**
     * WARNING! Only to be used by the configuration system and in unit tests. Not to be used in production code.
     *
     * @param type the type to inherit from
     */
    public void inherit(AnnotationType type) {
        if (superType != null) {
            throw new IllegalArgumentException("Already inherits type " + superType +
                                               ", multiple inheritance not currently supported.");
        }
        superType = type;
    }

    public Collection<AnnotationType> getInheritedTypes() {
        if (superType == null) {
            return List.of();
        }
        return List.of(superType);
    }

    public boolean inherits(AnnotationType type) {
        if (equals(type)) return true;
        if (superType != null && superType.inherits(type)) return true;
        return false;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof AnnotationType that)) return false;

        return name.equals(that.name);
    }

    @Override
    public int hashCode() {
        return id;
    }

    @Override
    public String toString() {
        StringBuilder strb = new StringBuilder();
        strb.append(name).append(" (id ").append(id);
        if (dataType != null) {
            strb.append(", data type ").append(dataType);
        }
        strb.append(")");
        return strb.toString();
    }

    @Override
    public int compareTo(AnnotationType annotationType) {
        if (annotationType == null) {
            return 1;
        }
        if (id < annotationType.id) {
            return -1;
        } else if (id > annotationType.id) {
            return 1;
        }
        return 0;
    }

}