aboutsummaryrefslogtreecommitdiffstats
path: root/container-search/src/main/java/com/yahoo/search/query/textserialize/serializer/DispatchForm.java
blob: 62e943956702abca8ea42e2c0c264aee71fe7fdb (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.search.query.textserialize.serializer;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Tony Vaagenes
 */
public class DispatchForm {
    private final String name;
    public final Map<Object, Object> properties = new LinkedHashMap<>();
    public final List<Object> children = new ArrayList<>();

    public DispatchForm(String name) {
        this.name = name;
    }

    public void addChild(Object child) {
        children.add(child);
    }

    /**
     * Only public for the purpose of testing.
     */
    public String serialize(ItemIdMapper itemIdMapper) {
        StringBuilder builder = new StringBuilder();
        builder.append('(').append(name);

        serializeProperties(builder, itemIdMapper);
        serializeChildren(builder, itemIdMapper);

        builder.append(')');
        return builder.toString();
    }

    private void serializeProperties(StringBuilder builder, ItemIdMapper itemIdMapper) {
        if (properties.isEmpty())
            return;

        builder.append(' ').append(Serializer.serializeMap(properties, itemIdMapper));
    }


    private void serializeChildren(StringBuilder builder, ItemIdMapper itemIdMapper) {
        for (Object child : children) {
            builder.append(' ').append(Serializer.serialize(child, itemIdMapper));
        }
    }

    public void setProperty(Object key, Object value) {
        properties.put(key, value);
    }
}