aboutsummaryrefslogtreecommitdiffstats
path: root/document/src/main/java/com/yahoo/document/CollectionDataType.java
blob: 1aeccb7e2fae9601cdd53ec06e3a8e6437f587f9 (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
// 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.CollectionFieldValue;
import com.yahoo.document.datatypes.FieldValue;
import com.yahoo.vespa.objects.Ids;
import com.yahoo.vespa.objects.ObjectVisitor;

import java.util.List;

/**
 * @author Einar M R Rosenvinge
 */
public abstract class CollectionDataType extends DataType {

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

    private DataType nestedType;

    protected CollectionDataType(String name, int code, DataType nestedType) {
        super(name, code);
        this.nestedType = nestedType;
    }

    @Override
    public abstract CollectionFieldValue<?> createFieldValue();

    @Override
    public CollectionDataType clone() {
        CollectionDataType type = (CollectionDataType) super.clone();
        type.nestedType = nestedType.clone();
        return type;
    }

    public DataType getNestedType() {
        return nestedType;
    }

    @Override
    protected FieldValue createByReflection(Object arg) { return null; }

    @Override
    public PrimitiveDataType getPrimitiveType() {
        return nestedType.getPrimitiveType();
    }

    @Override
    public boolean isValueCompatible(FieldValue value) {
        if (!(value instanceof CollectionFieldValue)) {
            return false;
        }
        CollectionFieldValue<?> cfv = (CollectionFieldValue<?>) value;
        return equals(cfv.getDataType());
    }

    @Override
    protected void register(DocumentTypeManager manager, List<DataType> seenTypes) {
        seenTypes.add(this);
        if (!seenTypes.contains(getNestedType())) {
            //we haven't seen this one before, register it:
            getNestedType().register(manager, seenTypes);
        }
        super.register(manager, seenTypes);
    }

    @Override
    public void visitMembers(ObjectVisitor visitor) {
        super.visitMembers(visitor);
        visitor.visit("nestedType", nestedType);
    }

    @Override
    public boolean isMultivalue() { return true; }

}