aboutsummaryrefslogtreecommitdiffstats
path: root/messagebus/src/main/java/com/yahoo/messagebus/routing/Hop.java
blob: 879b18679e9d4934e45a0d654c6ae345340a95a5 (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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.messagebus.routing;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>Hops are the components of routes.
 * They are instantiated from a {@link HopBlueprint} or
 * using the factory method {@link #parse(String)}. A hop is resolved to a recipient, from
 * a set of primitives, either a string primitive that is to be matched verbatim to a
 * service address, or a {@link RoutingPolicy} directive.</p>
 *
 * @author bratseth
 */
public class Hop {

    private final List<HopDirective> selector = new ArrayList<>();
    private boolean ignoreResult = false;
    private String cache = null;

    /**
     * Constructs an empty hop. You will need to add directives to the
     * selector to make this usable.
     */
    public Hop() {
        // empty
    }

    /**
     * Implements the copy constructor.
     *
     * @param hop The hop to copy.
     */
    public Hop(Hop hop) {
        selector.addAll(hop.selector);
        ignoreResult = hop.ignoreResult;
    }

    /**
     * Constructs a fully populated hop. This is package private and used by
     * the {@link HopBlueprint#create()} method.
     *
     * @param selector     The selector to copy.
     * @param ignoreResult Whether or not to ignore the result of this hop.
     */
    Hop(List<HopDirective> selector, boolean ignoreResult) {
        this.selector.addAll(selector);
        this.ignoreResult = ignoreResult;
    }

    /**
     * Parses the given string as a single hop. The {@link #toString()}
     * method is compatible with this parser.
     *
     * @param str The string to parse.
     * @return A hop that corresponds to the string.
     */
    public static Hop parse(String str) {
        Route route = Route.parse(str);
        if (route.getNumHops() > 1) {
            return new Hop().addDirective(new ErrorDirective("Failed to completely parse '" + str + "'."));
        }
        return route.getHop(0);
    }

    /**
     * Returns whether or not there are any directives contained in this hop.
     *
     * @return True if there is at least one directive.
     */
    public boolean hasDirectives() {
        return !selector.isEmpty();
    }

    /**
     * Returns the number of directives contained in this hop.
     *
     * @return The number of directives.
     */
    public int getNumDirectives() {
        return selector.size();
    }

    /**
     * Returns the directive at the given index.
     *
     * @param i The index of the directive to return.
     * @return The item.
     */
    public HopDirective getDirective(int i) {
        return selector.get(i);
    }

    /**
     * Adds a new directive to this hop.
     *
     * @param directive The directive to add.
     * @return This, to allow chaining.
     */
    public Hop addDirective(HopDirective directive) {
        cache = null;
        selector.add(directive);
        return this;
    }

    /**
     * Sets the directive at a given index.
     *
     * @param i         The index at which to set the directive.
     * @param directive The directive to set.
     * @return This, to allow chaining.
     */
    public Hop setDirective(int i, HopDirective directive) {
        selector.set(i, directive);
        return this;
    }

    /**
     * <p>Removes the directive at the given index.</p>
     *
     * @param i The index of the directive to remove.
     * @return The removed directive.
     */
    public HopDirective removeDirective(int i) {
        cache = null;
        return selector.remove(i);
    }

    /**
     * <p>Clears all directives from this hop.</p>
     *
     * @return This, to allow chaining.
     */
    public Hop clearDirectives() {
        cache = null;
        selector.clear();
        return this;
    }

    /**
     * <p>Returns whether or not to ignore the result when routing through this
     * hop.</p>
     *
     * @return True to ignore the result.
     */
    public boolean getIgnoreResult() {
        return ignoreResult;
    }

    /**
     * <p>Sets whether or not to ignore the result when routing through this
     * hop.</p>
     *
     * @param ignoreResult Whether or not to ignore the result.
     * @return This, to allow chaining.
     */
    public Hop setIgnoreResult(boolean ignoreResult) {
        this.ignoreResult = ignoreResult;
        return this;
    }

    /**
     * <p>Returns true whether this hop matches another. This respects policy
     * directives matching any other.</p>
     *
     * @param hop The hop to compare to.
     * @return True if this matches the argument, false otherwise.
     */
    public boolean matches(Hop hop) {
        if (hop == null || hop.getNumDirectives() != selector.size()) {
            return false;
        }
        for (int i = 0; i < hop.getNumDirectives(); ++i) {
            if (!selector.get(i).matches(hop.getDirective(i))) {
                return false;
            }
        }
        return true;
    }

    /**
     * <p>Returns a string representation of this that can be debugged but not
     * parsed.</p>
     *
     * @return The debug string.
     */
    public String toDebugString() {
        StringBuilder ret = new StringBuilder("Hop(selector = { ");
        for (int i = 0; i < selector.size(); ++i) {
            ret.append(selector.get(i).toDebugString());
            if (i < selector.size() - 1) {
                ret.append(", ");
            }
        }
        ret.append(" }, ignoreResult = ").append(ignoreResult).append(")");
        return ret.toString();
    }

    @Override
    public String toString() {
        return (ignoreResult ? "?" : "") + getServiceName();
    }

    @Override
    public boolean equals(Object obj) {
        if (!(obj instanceof Hop)) {
            return false;
        }
        Hop rhs = (Hop)obj;
        if (selector.size() != rhs.selector.size()) {
            return false;
        }
        for (int i = 0; i < selector.size(); ++i) {
            if (!selector.get(i).equals(rhs.selector.get(i))) {
                return false;
            }
        }
        return true;
    }

    /**
     * <p>Returns the service name referenced by this hop. This is the
     * concatenation of all selector primitives, but with no ignore-result
     * prefix.</p>
     *
     * @return The service name.
     */
    public String getServiceName() {
        if (cache == null) {
            cache = toString(0, selector.size());
        }
        return cache;
    }

    /**
     * <p>Returns a string concatenation of a subset of the selector primitives
     * contained in this.</p>
     *
     * @param fromIncluding  The index of the first primitive to include.
     * @param toNotIncluding The index after the last primitive to include.
     * @return The string concatenation.
     */
    public String toString(int fromIncluding, int toNotIncluding) {
        StringBuilder ret = new StringBuilder();
        for (int i = fromIncluding; i < toNotIncluding; ++i) {
            ret.append(selector.get(i));
            if (i < toNotIncluding - 1) {
                ret.append("/");
            }
        }
        return ret.toString();
    }

    /**
     * <p>Returns the prefix of this hop's selector to, but not including, the
     * given index.</p>
     *
     * @param toNotIncluding The index to which to generate prefix.
     * @return The prefix before the index.
     */
    public String getPrefix(int toNotIncluding) {
        if (toNotIncluding > 0) {
            return toString(0, toNotIncluding) + "/";
        }
        return "";
    }

    /**
     * <p>Returns the suffix of this hop's selector from, but not including, the
     * given index.</p>
     *
     * @param fromNotIncluding The index from which to generate suffix.
     * @return The suffix after the index.
     */
    public String getSuffix(int fromNotIncluding) {
        if (fromNotIncluding < selector.size() - 1) {
            return "/" + toString(fromNotIncluding + 1, selector.size());
        }
        return "";
    }

    @Override
    public int hashCode() {
        int result = selector.hashCode();
        result = 31 * result + (ignoreResult ? 1 : 0);
        result = 31 * result + (cache != null ? cache.hashCode() : 0);
        return result;
    }

}