-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathMultiProviderEvaluation.java
More file actions
123 lines (108 loc) · 3.9 KB
/
MultiProviderEvaluation.java
File metadata and controls
123 lines (108 loc) · 3.9 KB
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
package dev.openfeature.sdk.multiprovider;
import dev.openfeature.sdk.ErrorCode;
import dev.openfeature.sdk.ImmutableMetadata;
import dev.openfeature.sdk.ProviderEvaluation;
import java.util.Collections;
import java.util.List;
/**
* A {@link ProviderEvaluation} subtype returned by multi-provider strategies that carries
* per-provider error details.
*
* <p>This type can represent both successful and failed evaluations. When a strategy exhausts
* all providers without a successful result, the per-provider errors describe why each provider
* failed. Custom strategies may also use this type for successful results to surface information
* about providers that were skipped or failed before the successful one.
*
* <p>Usage:
* <pre>{@code
* ProviderEvaluation<String> result = strategy.evaluate(...);
* if (result instanceof MultiProviderEvaluation<String> multiResult) {
* for (ProviderError error : multiResult.getProviderErrors()) {
* log.warn("Provider {} failed: {} - {}",
* error.getProviderName(), error.getErrorCode(), error.getErrorMessage());
* }
* }
* }</pre>
*
* @param <T> the type of the flag being evaluated
*/
public class MultiProviderEvaluation<T> extends ProviderEvaluation<T> {
private final List<ProviderError> providerErrors;
private MultiProviderEvaluation(
T value,
String variant,
String reason,
ErrorCode errorCode,
String errorMessage,
ImmutableMetadata flagMetadata,
List<ProviderError> providerErrors) {
super(value, variant, reason, errorCode, errorMessage, flagMetadata);
this.providerErrors =
providerErrors != null ? Collections.unmodifiableList(providerErrors) : Collections.emptyList();
}
/**
* Returns the per-provider error details.
*
* <p>Each entry describes why a specific provider failed during multi-provider evaluation.
*
* @return an unmodifiable list of per-provider errors, never {@code null}
*/
public List<ProviderError> getProviderErrors() {
return providerErrors;
}
/**
* Create a new builder for {@link MultiProviderEvaluation}.
*
* @param <T> the flag value type
* @return a new builder
*/
public static <T> Builder<T> multiProviderBuilder() {
return new Builder<>();
}
/**
* Builder for {@link MultiProviderEvaluation}.
*
* @param <T> the flag value type
*/
public static class Builder<T> {
private T value;
private String variant;
private String reason;
private ErrorCode errorCode;
private String errorMessage;
private ImmutableMetadata flagMetadata;
private List<ProviderError> providerErrors;
public Builder<T> value(T value) {
this.value = value;
return this;
}
public Builder<T> variant(String variant) {
this.variant = variant;
return this;
}
public Builder<T> reason(String reason) {
this.reason = reason;
return this;
}
public Builder<T> errorCode(ErrorCode errorCode) {
this.errorCode = errorCode;
return this;
}
public Builder<T> errorMessage(String errorMessage) {
this.errorMessage = errorMessage;
return this;
}
public Builder<T> flagMetadata(ImmutableMetadata flagMetadata) {
this.flagMetadata = flagMetadata;
return this;
}
public Builder<T> providerErrors(List<ProviderError> providerErrors) {
this.providerErrors = providerErrors;
return this;
}
public MultiProviderEvaluation<T> build() {
return new MultiProviderEvaluation<>(
value, variant, reason, errorCode, errorMessage, flagMetadata, providerErrors);
}
}
}