aboutsummaryrefslogtreecommitdiffstats
path: root/config-lib/src/main/java/com/yahoo/config/LeafNodeMaps.java
blob: 214d8c52caae1b38ad3fd0cdaa0161bfe87473bb (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.config;

import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author gjoranv
 */
public class LeafNodeMaps {

    /**
     * Converts a map of String→NODE to String→REAL, where REAL is the underlying value type.
     */
    public static <NODE extends LeafNode<REAL>, REAL>
    Map<String, REAL> asValueMap(Map<String, NODE> input)
    {
        Map<String, REAL> ret = new LinkedHashMap<>();
        for(String key : input.keySet()) {
            ret.put(key, input.get(key).value());
        }
        return Collections.unmodifiableMap(ret);
    }

    /**
     * Converts a map of String→REAL to String→NODE, where REAL is the underlying value type.
     */
    @SuppressWarnings("unchecked")
    public static <NODE extends LeafNode<REAL>, REAL>
    Map<String, NODE> asNodeMap(Map<String, REAL> input, NODE defaultNode)
    {
        Map<String, NODE> ret = new LinkedHashMap<>();
        for(String key : input.keySet()) {
            NODE node = (NODE)defaultNode.clone();
            node.value = input.get(key);
            ret.put(key, node);
        }
        return Collections.unmodifiableMap(ret);
    }


    /**
     * Special case for file type, since FileNode param type (FileReference) is not same as type (String) in config builder
     */
    public static Map<String, FileNode> asFileNodeMap(Map<String, String> stringMap) {
        Map<String, FileNode> fileNodeMap = new LinkedHashMap<>();
        for (Map.Entry<String, String> e : stringMap.entrySet()) {
            fileNodeMap.put(e.getKey(), new FileNode(e.getValue()));
        }
        return Collections.unmodifiableMap(fileNodeMap);
    }

    public static Map<String, PathNode> asPathNodeMap(Map<String, FileReference> fileReferenceMap) {
        Map<String, PathNode> pathNodeMap = new LinkedHashMap<>();
        for (Map.Entry<String, FileReference> e : fileReferenceMap.entrySet()) {
            pathNodeMap.put(e.getKey(), new PathNode(e.getValue()));
        }
        return Collections.unmodifiableMap(pathNodeMap);
    }

    public static Map<String, OptionalPathNode> asOptionalPathNodeMap(Map<String, Optional<FileReference>> fileReferenceMap) {
        Map<String, OptionalPathNode> pathNodeMap = new LinkedHashMap<>();
        for (Map.Entry<String, Optional<FileReference>> e : fileReferenceMap.entrySet()) {
            pathNodeMap.put(e.getKey(), new OptionalPathNode(e.getValue()));
        }
        return Collections.unmodifiableMap(pathNodeMap);
    }

    public static Map<String, UrlNode> asUrlNodeMap(Map<String, UrlReference> urlReferenceMap) {
        return Collections.unmodifiableMap(
                    urlReferenceMap.entrySet().stream().collect(
                        Collectors.toMap(Map.Entry::getKey, e -> new UrlNode(e.getValue()))
                    ));
    }

    public static Map<String, ModelNode> asModelNodeMap(Map<String, ModelReference> modelReferenceMap) {
        return Collections.unmodifiableMap(
                modelReferenceMap.entrySet().stream().collect(
                        Collectors.toMap(Map.Entry::getKey, e -> new ModelNode(e.getValue()))
                ));
    }

}