blob: 537d242374dede1823b85612411bd7e39101882f (
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
|
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.yolean.chain;
import com.google.common.collect.ImmutableList;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import static java.util.Objects.requireNonNull;
/**
* An immutable and ordered list of components
*
* @author tonytv
*/
public final class Chain<T> implements Iterable<T> {
private final String id;
private final Collection<T> components;
@SafeVarargs
public Chain(String id, T... components) {
this(id, Arrays.asList(components));
}
public Chain(String id, List<? extends T> components) {
requireNonNull(id, "id must be non-null.");
requireNonNull(components, "components must be non-null");
this.components = ImmutableList.copyOf(components);
this.id = id;
}
public String id() {
return id;
}
public boolean isEmpty() {
return components.isEmpty();
}
@Override
public Iterator<T> iterator() {
return components.iterator();
}
@Override
public String toString() {
StringBuilder b = new StringBuilder();
b.append("chain '").append(id).append("'{");
boolean first = true;
for (T component : components) {
if (!first) {
b.append("->");
} else {
first = false;
}
b.append(" ").append(component.getClass().getSimpleName()).append(" ");
}
b.append("}");
return b.toString();
}
@Override
public int hashCode() {
return id.hashCode();
}
@Override
public boolean equals(Object other) {
return other instanceof Chain && equals((Chain<?>)other);
}
public boolean equals(Chain<?> other) {
return id.equals(other.id) && componentsIdentical(components, other.components);
}
private boolean componentsIdentical(Collection<T> components1, Collection<?> components2) {
if (components1.size() != components2.size()) {
return false;
}
Iterator<T> iterator1 = components1.iterator();
Iterator<?> iterator2 = components2.iterator();
while (iterator1.hasNext()) {
T c1 = iterator1.next();
Object c2 = iterator2.next();
if (c1 != c2) {
return false;
}
}
return true;
}
}
|