aboutsummaryrefslogtreecommitdiffstats
path: root/document/src/main/java/com/yahoo/document/StructDataType.java
blob: 5a236d0742d6cec5c86ce8c858ff86ff95d270b4 (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
// Copyright Vespa.ai. 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.Struct;
import com.yahoo.vespa.objects.Ids;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
 * @author Einar M R Rosenvinge
 */
public class StructDataType extends BaseStructDataType {

    public static final int classId = registerClass(Ids.document + 57, StructDataType.class);
    private StructDataType superType = null;

    public StructDataType(String name) {
        super(name);
    }

    public StructDataType(int id, String name) {
        super(id, name);
    }

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

    @Override
    public FieldValue createFieldValue(Object o) {
        Struct struct;
        if (o.getClass().equals(Struct.class)) {
            struct = new Struct(this);
        } else {
            // This indicates for example that o is a generated struct subtype, try the empty constructor
            try {
                struct = (Struct) o.getClass().getConstructor().newInstance();
            } catch (Exception e) {
                // Fallback, let assign handle the error if o is completely bogus
                struct = new Struct(this);
            }
        }
        struct.assign(o);
        return struct;
    }

    @Override
    public StructDataType clone() {
        StructDataType type = (StructDataType) super.clone();
        type.superType = this.superType;
        return type;
    }

    public void assign(StructDataType type) {
        super.assign(type);
        superType = type.superType;
    }

    @Override
    public Field getField(String fieldName) {
        Field f = super.getField(fieldName);
        if (f == null && superType != null) {
            f = superType.getField(fieldName);
        }
        return f;
    }

    @Override
    public Field getField(int id) {
        Field f = super.getField(id);
        if (f == null && superType != null) {
            f = superType.getField(id);
        }
        return f;
    }

    @Override
    public void addField(Field field) {
        if (hasField(field)) {
            throw new IllegalArgumentException("Struct already has field " + field);
        }
        if ((superType != null) && superType.hasField(field)) {
            throw new IllegalArgumentException(field.toString() + " already present in inherited type '" + superType.toString() + "', " + this.toString() + " cannot override.");
        }
        super.addField(field);
    }

    @Override
    public Collection<Field> getFields() {
        if (superType == null) {
            return Collections.unmodifiableCollection(super.getFields());
        }
        Collection<Field> fieldsBuilder = new ArrayList<>();
        fieldsBuilder.addAll(super.getFields());
        fieldsBuilder.addAll(superType.getFields());
        return List.copyOf(fieldsBuilder);
    }

    public Collection<Field> getFieldsThisTypeOnly() {
        return Collections.unmodifiableCollection(super.getFields());
    }

    @Override
    public int getFieldCount() {
        return getFields().size();
    }

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

    @Override
    public boolean isValueCompatible(FieldValue value) {
        if (!(value instanceof Struct structValue)) {
            return false;
        }
        if (structValue.getDataType().inherits(this)) {
            //the value is of this type; or the supertype of the value is of this type, etc....
            return true;
        }
        return false;
    }

    public void inherit(StructDataType type) {
        if (superType != null) {
            throw new IllegalArgumentException("Already inherits type " + superType + ", multiple inheritance not currently supported.");
        }
        for (Field f : type.getFields()) {
            if (hasField(f)) {
                throw new IllegalArgumentException(f + " already present in " + type + ", " + this + " cannot inherit from it");
            }
        }
        superType = type;
    }

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

    public boolean inherits(StructDataType 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 StructDataType that)) return false;
        if (!super.equals(o)) return false;

        if (superType != null ? !superType.equals(that.superType) : that.superType != null) return false;
        return true;
    }

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