|
1 | 1 | package com.github.switcherapi.client.model; |
2 | 2 |
|
3 | 3 | import com.github.switcherapi.client.SwitcherContext; |
4 | | -import com.github.switcherapi.client.SwitcherExecutor; |
5 | 4 | import com.github.switcherapi.client.exception.SwitcherException; |
6 | 5 |
|
7 | | -import java.util.*; |
| 6 | +import java.util.List; |
8 | 7 |
|
9 | 8 | /** |
10 | | - * Switchers are the entry point to evaluate criteria and return the result. |
11 | | - * <br>To execute a criteria evaluation, use one of the available methods: {@link #isItOn()}. |
12 | | - * |
13 | | - * @author Roger Floriano (petruki) |
14 | | - * @since 2019-12-24 |
15 | | - * |
16 | | - * @see #isItOn() |
17 | | - * @see #submit() |
| 9 | + * The API that handles the switcher operations. |
| 10 | + * |
| 11 | + * <ul> |
| 12 | + * <li>Switcher creation</li> |
| 13 | + * <li>Switcher execution</li> |
| 14 | + * <li>Switcher get input/output</li> |
| 15 | + * </ul> |
18 | 16 | */ |
19 | | -public final class Switcher extends SwitcherBuilder { |
20 | | - |
21 | | - public static final String KEY = "key"; |
22 | | - |
23 | | - public static final String SHOW_REASON = "showReason"; |
24 | | - |
25 | | - public static final String BYPASS_METRIC = "bypassMetric"; |
| 17 | +public interface Switcher { |
26 | 18 |
|
27 | | - private final SwitcherExecutor context; |
28 | | - |
29 | | - private final String switcherKey; |
30 | | - |
31 | | - private final Set<SwitcherResult> historyExecution; |
32 | | - |
33 | | - private AsyncSwitcher<Switcher> asyncSwitcher; |
34 | | - |
35 | 19 | /** |
36 | | - * Use {@link SwitcherContext#getSwitcher(String)} to create this object. |
37 | | - * |
38 | | - * @param switcherKey name of the key created |
39 | | - * @param context client context in which the switcher will be executed (local/remote) |
| 20 | + * This method builds the Switcher object.<br> |
| 21 | + * Uses to isolate Switcher creation from the execution.<br> |
| 22 | + * |
| 23 | + * For example: |
| 24 | + * <pre> |
| 25 | + * Switcher switcher = SwitcherContext |
| 26 | + * .getSwitcher(MY_SWITCHER) |
| 27 | + * .remote(true) |
| 28 | + * .throttle(1000) |
| 29 | + * .checkValue("value") |
| 30 | + * .build(); |
| 31 | + * </pre> |
| 32 | + * |
| 33 | + * @return instance of SwitcherInterface |
| 34 | + * @see SwitcherRequest |
40 | 35 | */ |
41 | | - public Switcher(final String switcherKey, final SwitcherExecutor context) { |
42 | | - super(context.getSwitcherProperties()); |
43 | | - this.context = context; |
44 | | - this.switcherKey = switcherKey; |
45 | | - this.historyExecution = new HashSet<>(); |
46 | | - this.entry = new ArrayList<>(); |
47 | | - } |
48 | | - |
49 | | - @Override |
50 | | - public Switcher build() { |
51 | | - return this; |
52 | | - } |
53 | | - |
54 | | - @Override |
55 | | - public Switcher prepareEntry(final List<Entry> entry) { |
56 | | - this.entry = Optional.ofNullable(entry).orElse(new ArrayList<>()); |
57 | | - return this; |
58 | | - } |
59 | | - |
60 | | - @Override |
61 | | - public Switcher prepareEntry(final Entry entry, final boolean add) { |
62 | | - if (!add) { |
63 | | - this.entry.clear(); |
64 | | - } |
65 | | - |
66 | | - if (!this.entry.contains(entry)) { |
67 | | - this.entry.add(entry); |
68 | | - } |
69 | | - |
70 | | - return this; |
71 | | - } |
72 | | - |
73 | | - @Override |
74 | | - public Switcher prepareEntry(final Entry entry) { |
75 | | - return this.prepareEntry(entry, false); |
76 | | - } |
77 | | - |
78 | | - @Override |
79 | | - public boolean isItOn() throws SwitcherException { |
80 | | - final SwitcherResult response = submit(); |
81 | | - return response.isItOn(); |
82 | | - } |
83 | | - |
84 | | - @Override |
85 | | - public SwitcherResult submit() throws SwitcherException { |
86 | | - if (SwitcherExecutor.getBypass().containsKey(switcherKey)) { |
87 | | - return SwitcherExecutor.getBypass().get(switcherKey).buildFromSwitcher(switcherKey, entry); |
88 | | - } |
89 | | - |
90 | | - if (canUseAsync()) { |
91 | | - if (Objects.isNull(asyncSwitcher)) { |
92 | | - asyncSwitcher = new AsyncSwitcher<>(this, super.delay); |
93 | | - } |
94 | | - |
95 | | - asyncSwitcher.execute(); |
96 | | - final Optional<SwitcherResult> response = getFromHistory(); |
97 | | - if (response.isPresent()) { |
98 | | - return response.get(); |
99 | | - } |
100 | | - } |
101 | | - |
102 | | - final SwitcherResult response = this.context.executeCriteria(this); |
103 | | - this.updateHistoryExecution(response); |
104 | | - return response; |
105 | | - } |
| 36 | + Switcher build(); |
106 | 37 |
|
107 | | - @Override |
108 | | - public SwitcherResult executeCriteria() { |
109 | | - return this.context.executeCriteria(this); |
110 | | - } |
| 38 | + /** |
| 39 | + * Prepare the Switcher including a list of inputs necessary to run the criteria afterward. |
| 40 | + * |
| 41 | + * @param entry input object |
| 42 | + * @return instance of SwitcherInterface |
| 43 | + */ |
| 44 | + Switcher prepareEntry(final List<Entry> entry); |
111 | 45 |
|
112 | | - @Override |
113 | | - public void updateHistoryExecution(final SwitcherResult response) { |
114 | | - this.historyExecution.removeIf(item -> |
115 | | - this.switcherKey.equals(item.getSwitcherKey()) && this.entry.equals(item.getEntry())); |
| 46 | + /** |
| 47 | + * Prepare the Switcher including a list of inputs necessary to run the criteria afterward. |
| 48 | + * |
| 49 | + * @param entry input object |
| 50 | + * @param add if false, the list will be cleaned and the entry provided will be the only input for this Switcher. |
| 51 | + * @return instance of SwitcherInterface |
| 52 | + */ |
| 53 | + Switcher prepareEntry(final Entry entry, final boolean add); |
116 | 54 |
|
117 | | - this.historyExecution.add(response); |
118 | | - } |
| 55 | + /** |
| 56 | + * It adds an input to the list of inputs. |
| 57 | + * <br>Under the table it calls {@link #prepareEntry(Entry, boolean)} passing true to the second argument. |
| 58 | + * |
| 59 | + * @param entry input object |
| 60 | + * @return instance of SwitcherInterface |
| 61 | + */ |
| 62 | + Switcher prepareEntry(final Entry entry); |
119 | 63 |
|
120 | | - @Override |
121 | | - public String getSwitcherKey() { |
122 | | - return this.switcherKey; |
123 | | - } |
| 64 | + /** |
| 65 | + * Execute criteria based on a given switcher key provided via {@link SwitcherContext#getSwitcher(String)}. |
| 66 | + * <br>The detailed result is available in list of {@link SwitcherResult}. |
| 67 | + * |
| 68 | + * @return criteria result |
| 69 | + * @throws SwitcherException connectivity or criteria errors regarding reading malformed snapshots |
| 70 | + */ |
| 71 | + boolean isItOn() throws SwitcherException; |
124 | 72 |
|
125 | | - @Override |
126 | | - public List<Entry> getEntry() { |
127 | | - return this.entry; |
128 | | - } |
| 73 | + /** |
| 74 | + * Execute criteria based on a given switcher key provided via {@link SwitcherContext#getSwitcher(String)}. |
| 75 | + * <br>The detailed result is available in list of {@link SwitcherResult}. |
| 76 | + * |
| 77 | + * @return {@link SwitcherResult} |
| 78 | + * @throws SwitcherException connectivity or criteria errors regarding reading malformed snapshots |
| 79 | + */ |
| 80 | + SwitcherResult submit() throws SwitcherException; |
129 | 81 |
|
130 | | - public boolean isBypassMetrics() { |
131 | | - return bypassMetrics; |
132 | | - } |
133 | | - |
134 | | - public void resetEntry() { |
135 | | - this.entry = new ArrayList<>(); |
136 | | - } |
| 82 | + /** |
| 83 | + * Execute the criteria evaluation. |
| 84 | + * |
| 85 | + * @return the switcher result |
| 86 | + */ |
| 87 | + SwitcherResult executeCriteria(); |
137 | 88 |
|
138 | | - private boolean canUseAsync() { |
139 | | - return super.delay > 0 && !this.historyExecution.isEmpty(); |
140 | | - } |
| 89 | + /** |
| 90 | + * Update the history of executions. |
| 91 | + * |
| 92 | + * @param response the response to be updated |
| 93 | + */ |
| 94 | + void updateHistoryExecution(SwitcherResult response); |
141 | 95 |
|
142 | | - private Optional<SwitcherResult> getFromHistory() { |
143 | | - for (SwitcherResult switcherResult : historyExecution) { |
144 | | - if (switcherResult.getEntry().equals(getEntry())) { |
145 | | - return Optional.of(switcherResult); |
146 | | - } |
147 | | - } |
148 | | - return Optional.empty(); |
149 | | - } |
| 96 | + /** |
| 97 | + * Get the key of the switcher. |
| 98 | + * |
| 99 | + * @return the key of the switcher |
| 100 | + */ |
| 101 | + String getSwitcherKey(); |
150 | 102 |
|
151 | | - @Override |
152 | | - public String toString() { |
153 | | - return String.format("Switcher [switcherKey= %s, entry= %s, bypassMetrics= %s]", |
154 | | - switcherKey, entry, bypassMetrics); |
155 | | - } |
| 103 | + /** |
| 104 | + * Get the entry input list for the switcher. |
| 105 | + * |
| 106 | + * @return the entry of the switcher |
| 107 | + */ |
| 108 | + List<Entry> getEntry(); |
156 | 109 |
|
157 | 110 | } |
0 commit comments