summaryrefslogtreecommitdiffstats
path: root/container-core/src/main/java/com/yahoo/component/chain/Chain.java
blob: 24c4a0a0e2e0bb4574f3c51bd46850fbbc639c12 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.component.chain;

import com.yahoo.component.ComponentId;
import com.yahoo.component.chain.dependencies.ordering.ChainBuilder;

import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
 * An immutable ordered list of components
 *
 * @author Tony Vaagenes
 */
public class Chain<COMPONENT extends ChainedComponent> {

    final private List<COMPONENT> componentList;
    private final ComponentId id;

    /** Create a chain directly. This will NOT order the chain by the ordering constraints. */
    public Chain(String id, List<COMPONENT> componentList) {
        this(new ComponentId(id), componentList);
    }

    /** Create a chain directly. This will NOT order the chain by the ordering constraints. */
    public Chain(ComponentId id, List<COMPONENT> componentList) {
        this.id = id;
        this.componentList = List.copyOf(componentList);
    }

    /** Create a chain directly. This will NOT order the chain by the ordering constraints. */
    public Chain(List<COMPONENT> componentList) {
        this(new ComponentId("anonymous chain"), componentList);
    }

    /** Create a chain directly. This will NOT order the chain by the ordering constraints. */
    @SafeVarargs
    public Chain(COMPONENT... components) {
        this("anonymous chain", components);
    }

    /** Create a chain directly. This will NOT order the chain by the ordering constraints. */
    @SafeVarargs
    public Chain(String id, COMPONENT... components) {
        this(new ComponentId(id), components);
    }

    /** Create a chain directly. This will NOT order the chain by the ordering constraints. */
    @SafeVarargs
    @SuppressWarnings("varargs")
    public Chain(ComponentId id, COMPONENT... components) {
        this(id, Arrays.<COMPONENT>asList(components));
    }

    /** Create a chain by using a builder. This will order the chain by the ordering constraints. */
    public Chain(ComponentId id, Collection<COMPONENT> components, Collection<Phase> phases) {
        this(id, buildChain(
                emptyListIfNull(components),
                emptyListIfNull(phases)).components());

    }

    public ComponentId getId() {
        return id;
    }

    private static <T> Collection<T> emptyListIfNull(Collection<T> collection) {
        return collection == null ? Collections.<T>emptyList() : collection;
    }

    private static <T extends ChainedComponent> Chain<T> buildChain(Collection<T> components, Collection<Phase> phases) {
        ChainBuilder<T> builder = new ChainBuilder<>(new ComponentId("temp"));
        for (Phase phase : phases) {
            builder.addPhase(phase);
        }

        for (T component : components) {
            builder.addComponent(component);
        }

        return builder.orderNodes();
    }

    public List<COMPONENT> components() {
        return componentList;
    }

    public
    @Override
    String toString() {
        StringBuilder b = new StringBuilder("chain '");
        b.append(getId().stringValue());
        b.append("' [");
        appendComponent(0, b);
        appendComponent(1, b);
        if (components().size() > 3)
            b.append("... -> ");
        if (components().size() > 2)
            appendComponent(components().size() - 1, b);
        b.append("]");
        return b.toString();
    }

    private void appendComponent(int i, StringBuilder b) {
        if (i >= components().size()) return;
        b.append(components().get(i).getId().stringValue());
        if (i < components().size() - 1)
            b.append(" -> ");
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Chain<?> chain = (Chain<?>) o;

        if (!componentList.equals(chain.componentList)) return false;
        if (!id.equals(chain.id)) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = componentList.hashCode();
        result = 31 * result + id.hashCode();
        return result;
    }
}