summaryrefslogtreecommitdiffstats
path: root/config-model/src/main/java/com/yahoo/schema/fieldoperation/WeightedSetOperation.java
blob: 8fb0cc9fcdbd06821d96d485e94ae0afe5d0eed7 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.schema.fieldoperation;

import com.yahoo.schema.document.Attribute;
import com.yahoo.document.DataType;
import com.yahoo.schema.document.SDField;
import com.yahoo.document.WeightedSetDataType;

/**
 * @author Einar M R Rosenvinge
 */
public class WeightedSetOperation implements FieldOperation {

    private Boolean createIfNonExistent;
    private Boolean removeIfZero;

    public Boolean getCreateIfNonExistent() {
        return createIfNonExistent;
    }

    public void setCreateIfNonExistent(Boolean createIfNonExistent) {
        this.createIfNonExistent = createIfNonExistent;
    }

    public Boolean getRemoveIfZero() {
        return removeIfZero;
    }

    public void setRemoveIfZero(Boolean removeIfZero) {
        this.removeIfZero = removeIfZero;
    }

    public void apply(SDField field) {
        WeightedSetDataType ctype = (WeightedSetDataType) field.getDataType();

        if (createIfNonExistent != null) {
            field.setDataType(DataType.getWeightedSet(ctype.getNestedType(), createIfNonExistent,
                                                      ctype.removeIfZero()));
        }

        ctype = (WeightedSetDataType) field.getDataType();
        if (removeIfZero != null) {
            field.setDataType(DataType.getWeightedSet(ctype.getNestedType(),
                                                      ctype.createIfNonExistent(), removeIfZero));
        }

        ctype = (WeightedSetDataType) field.getDataType();
        for (Object o : field.getAttributes().values()) {
            Attribute attribute = (Attribute) o;
            attribute.setRemoveIfZero(ctype.removeIfZero());
            attribute.setCreateIfNonExistent(ctype.createIfNonExistent());
        }
    }

    @Override
    public int compareTo(FieldOperation other) {
        // this operation should be executed first because it modifies the type of weighted sets, and other
        // operation depends on the type of the weighted set
        return -1;
    }
    
    @Override
    public String toString() {
        return "WeightedSetOperation{" +
               "createIfNonExistent=" + createIfNonExistent +
               ", removeIfZero=" + removeIfZero +
               '}';
    }

}