summaryrefslogtreecommitdiffstats
path: root/config-model/src/main/java/com/yahoo/schema/fieldoperation/StructFieldOperation.java
blob: ac80f5023fc213e2489d14ea40de001002aed1cf (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
// 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.SDField;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;

/**
 * @author Einar M R Rosenvinge
 */
public class StructFieldOperation implements FieldOperation, FieldOperationContainer {

    private final String structFieldName;
    private final List<FieldOperation> pendingOperations = new LinkedList<>();

    public StructFieldOperation(String structFieldName) {
        this.structFieldName = structFieldName;
    }

    public void apply(SDField field) {
        SDField structField = field.getStructField(structFieldName);
        if (structField == null ) {
            throw new IllegalArgumentException("Struct field '" + structFieldName + "' has not been defined in struct " +
                                               "for field '" + field.getName() + "'.");
        }

        applyOperations(structField);
    }

    @Override
    public void addOperation(FieldOperation op) {
        pendingOperations.add(op);
    }

    @Override
    public void applyOperations(SDField field) {
        if (pendingOperations.isEmpty()) return;

        Collections.sort(pendingOperations);
        ListIterator<FieldOperation> ops = pendingOperations.listIterator();
        while (ops.hasNext()) {
            FieldOperation op = ops.next();
            ops.remove();
            op.apply(field);
        }
    }

    @Override
    public String getName() {
        return structFieldName;
    }

}