aboutsummaryrefslogtreecommitdiffstats
path: root/container-core/src/main/java/com/yahoo/container/di/componentgraph/core/ComponentRegistryNode.java
blob: ae06275fd2b58f5017947254142f9d9a8ff8d993 (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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.container.di.componentgraph.core;

import com.google.inject.Key;
import com.google.inject.util.Types;
import com.yahoo.component.ComponentId;
import com.yahoo.component.provider.ComponentRegistry;
import com.yahoo.config.ConfigInstance;
import com.yahoo.vespa.config.ConfigKey;

import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author Tony Vaagenes
 * @author gjoranv
 * @author ollivir
 */
public class ComponentRegistryNode extends Node {

    private static ComponentId componentRegistryNamespace = ComponentId.fromString("ComponentRegistry");

    private final Class<?> componentClass;

    public ComponentRegistryNode(Class<?> componentClass) {
        super(componentId(componentClass));
        this.componentClass = componentClass;
    }

    @Override
    public List<Node> usedComponents() {
        return componentsToInject;
    }

    @Override
    protected Object newInstance() {
        ComponentRegistry<Object> registry = new ComponentRegistry<>();
        componentsToInject.forEach(component -> registry.register(component.componentId(), component.component()));

        return registry;
    }

    @Override
    public Key<?> instanceKey() {
        return Key.get(Types.newParameterizedType(ComponentRegistry.class, componentClass));
    }

    @Override
    public Class<?> instanceType() {
        return instanceKey().getTypeLiteral().getRawType();
    }

    @Override
    public Class<?> componentType() {
        return instanceType();
    }

    public Class<?> componentClass() {
        return componentClass;
    }

    @Override
    public Set<ConfigKey<ConfigInstance>> configKeys() {
        return Collections.emptySet();
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = super.hashCode();
        result = prime * result + ((componentClass == null) ? 0 : componentClass.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object other) {
        if (other instanceof ComponentRegistryNode) {
            ComponentRegistryNode that = (ComponentRegistryNode) other;
            return this.componentId().equals(that.componentId()) && this.instanceType().equals(that.instanceType())
                    && equalNodeEdges(this.usedComponents(), that.usedComponents());
        } else {
            return false;
        }
    }

    @Override
    public String label() {
        return String.format("{ComponentRegistry\\<%s\\>|%s}", componentClass.getSimpleName(), Node.packageName(componentClass));
    }

    private static ComponentId componentId(Class<?> componentClass) {
        return syntheticComponentId(componentClass.getName(), componentClass, componentRegistryNamespace);
    }

    public static boolean equalNodeEdges(List<Node> edges, List<Node> otherEdges) {
        if (edges.size() == otherEdges.size()) {
            List<ComponentId> left = edges.stream().map(Node::componentId).sorted().toList();
            List<ComponentId> right = otherEdges.stream().map(Node::componentId).sorted().toList();
            return left.equals(right);
        } else {
            return false;
        }
    }

}