aboutsummaryrefslogtreecommitdiffstats
path: root/messagebus/src/main/java/com/yahoo/messagebus/routing/HopBlueprint.java
blob: 3b7afa9cfe42017d01d28d3ce31b60813a1317c7 (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
// 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.*;

/**
 * A hop blueprint is a stored prototype of a hop that has been created from a {@link HopSpec} object. A map of these
 * are stored in a {@link RoutingTable}.
 *
 * @author bratseth
 * @author Simon Thoresen Hult
 */
public class HopBlueprint {

    private final List<HopDirective> selector = new ArrayList<>();
    private final List<Hop> recipients = new ArrayList<>();
    private boolean ignoreResult = false;

    /**
     * The default constructor requires valid arguments for all member variables.
     *
     * @param spec The spec of this rule.
     */
    HopBlueprint(HopSpec spec) {
        Hop hop = Hop.parse(spec.getSelector());
        for (int i = 0; i < hop.getNumDirectives(); ++i) {
            selector.add(hop.getDirective(i));
        }
        List<String> lst = new ArrayList<>();
        for (int i = 0; i < spec.getNumRecipients(); ++i) {
            lst.add(spec.getRecipient(i));
        }
        for (String recipient : lst) {
            recipients.add(Hop.parse(recipient));
        }
        ignoreResult = spec.getIgnoreResult();
    }

    /**
     * Creates a hop instance from thie blueprint.
     *
     * @return The created hop.
     */
    public Hop create() {
        return new Hop(selector, ignoreResult);
    }

    /**
     * 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);
    }

    /**
     * Returns whether or not there are any recipients that the selector can choose from.
     *
     * @return True if there is at least one recipient.
     */
    public boolean hasRecipients() {
        return !recipients.isEmpty();
    }

    /**
     * Returns the number of recipients that the selector can choose from.
     *
     * @return The number of recipients.
     */
    public int getNumRecipients() {
        return recipients.size();
    }

    /**
     * Returns the recipient at the given index.
     *
     * @param i The index of the recipient to return.
     * @return The recipient at the given index.
     */
    public Hop getRecipient(int i) {
        return recipients.get(i);
    }

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

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

    @Override
    public String toString() {
        StringBuilder ret = new StringBuilder("HopBlueprint(selector = { ");
        for (int i = 0; i < selector.size(); ++i) {
            ret.append("'").append(selector.get(i)).append("'");
            if (i < selector.size() - 1) {
                ret.append(", ");
            }
        }
        ret.append(" }, recipients = { ");
        for (int i = 0; i < recipients.size(); ++i) {
            ret.append("'").append(recipients.get(i)).append("'");
            if (i < recipients.size() - 1) {
                ret.append(", ");
            }
        }
        ret.append(" }, ignoreResult = ").append(ignoreResult).append(")");
        return ret.toString();
    }
}