summaryrefslogtreecommitdiffstats
path: root/container-di/src/main/java/com/yahoo/container/di/componentgraph/core/Node.java
blob: 6feac7a407824fe24bfdd0e8307d1780b2506f1e (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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// Copyright 2018 Yahoo Holdings. 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.yahoo.component.ComponentId;
import com.yahoo.config.ConfigInstance;
import com.yahoo.container.di.componentgraph.Provider;
import com.yahoo.vespa.config.ConfigKey;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.logging.Logger;

import static com.yahoo.log.LogLevel.DEBUG;
import static com.yahoo.log.LogLevel.SPAM;

/**
 * @author Tony Vaagenes
 * @author gjoranv
 * @author ollivir
 */
public abstract class Node {
    private final static Logger log = Logger.getLogger(Node.class.getName());

    private final ComponentId componentId;
    protected Optional<Object> instance = Optional.empty();
    List<Node> componentsToInject = new ArrayList<>();

    public Node(ComponentId componentId) {
        this.componentId = componentId;
    }

    public abstract Key<?> instanceKey();

    /**
     * The components actually used by this node. Consist of a subset of the injected nodes + subset of the global nodes.
     */
    public abstract List<Node> usedComponents();

    protected abstract Object newInstance();

    public Object newOrCachedInstance() {
        Object inst;
        if (instance.isPresent()) {
            inst = instance.get();
            log.log(SPAM, "Reusing instance for component with ID " + componentId);
        } else {
            log.log(DEBUG, "Creating new instance for component with ID " + componentId);
            inst = newInstance();
            instance = Optional.of(inst);
        }
        return component(inst);
    }

    private Object component(Object instance) {
        if (instance instanceof Provider) {
            Provider<?> provider = (Provider<?>) instance;
            return provider.get();
        } else {
            return instance;
        }
    }

    public abstract Set<ConfigKey<ConfigInstance>> configKeys();

    public void inject(Node component) {
        componentsToInject.add(component);
    }

    public void injectAll(Collection<ComponentNode> componentNodes) {
        componentNodes.forEach(this::inject);
    }

    public abstract Class<?> instanceType();

    public abstract Class<?> componentType();

    public abstract String label();

    public String idAndType() {
        String className = instanceType().getName();

        if (className.equals(componentId.getName())) {
            return "'" + componentId + "'";
        } else {
            return "'" + componentId + "' of type '" + className + "'";
        }
    }

    private static boolean equalNodes(Object a, Object b) {
        if (a instanceof Node && b instanceof Node) {
            Node l = (Node) a;
            Node r = (Node) b;
            return l.componentId.equals(r.componentId);
        } else {
            return a.equals(b);
        }
    }

    public static boolean equalEdges(List<?> edges1, List<?> edges2) {
        Iterator<?> right = edges2.iterator();
        for (Object l : edges1) {
            if (!right.hasNext()) {
                return false;
            }
            Object r = right.next();
            if (!equalNodes(l, r)) {
                return false;
            }
        }
        return !right.hasNext();
    }

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

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

    public ComponentId componentId() {
        return componentId;
    }

    public Optional<?> instance() {
        return instance;
    }

    /**
     * @param identityObject
     *            The identifying object that makes the Node unique
     */
    protected static ComponentId syntheticComponentId(String className, Object identityObject, ComponentId namespace) {
        String name = className + "_" + System.identityHashCode(identityObject);
        return ComponentId.fromString(name).nestInNamespace(namespace);
    }

    public static String packageName(Class<?> componentClass) {
        String fullClassName = componentClass.getName();
        int index = fullClassName.lastIndexOf('.');
        if (index < 0) {
            return "";
        } else {
            return fullClassName.substring(0, index);
        }
    }
}