Skip to content

Commit 0dde54a

Browse files
authored
Make Context.NONE a Specialized Subclass (Azure#24140)
Make Context.NONE a Specialized Subclass
1 parent 7993d1d commit 0dde54a

File tree

1 file changed

+19
-4
lines changed
  • sdk/core/azure-core/src/main/java/com/azure/core/util

1 file changed

+19
-4
lines changed

sdk/core/azure-core/src/main/java/com/azure/core/util/Context.java

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.azure.core.annotation.Immutable;
77
import com.azure.core.util.logging.ClientLogger;
88

9+
import java.util.Collections;
910
import java.util.HashMap;
1011
import java.util.Map;
1112
import java.util.Objects;
@@ -21,14 +22,28 @@
2122
*/
2223
@Immutable
2324
public class Context {
24-
private final ClientLogger logger = new ClientLogger(Context.class);
25+
private static final ClientLogger LOGGER = new ClientLogger(Context.class);
2526

2627
// All fields must be immutable.
2728
//
2829
/**
2930
* Signifies that no data needs to be passed to the pipeline.
3031
*/
31-
public static final Context NONE = new Context(null, null, null);
32+
public static final Context NONE = new Context(null, null, null) {
33+
@Override
34+
public Optional<Object> getData(Object key) {
35+
if (key == null) {
36+
throw LOGGER.logExceptionAsError(new IllegalArgumentException("key cannot be null"));
37+
}
38+
39+
return Optional.empty();
40+
}
41+
42+
@Override
43+
public Map<Object, Object> getValues() {
44+
return Collections.emptyMap();
45+
}
46+
};
3247

3348
private final Context parent;
3449
private final Object key;
@@ -72,7 +87,7 @@ private Context(Context parent, Object key, Object value) {
7287
*/
7388
public Context addData(Object key, Object value) {
7489
if (key == null) {
75-
throw logger.logExceptionAsError(new IllegalArgumentException("key cannot be null"));
90+
throw LOGGER.logExceptionAsError(new IllegalArgumentException("key cannot be null"));
7691
}
7792
return new Context(this, key, value);
7893
}
@@ -119,7 +134,7 @@ public static Context of(Map<Object, Object> keyValues) {
119134
*/
120135
public Optional<Object> getData(Object key) {
121136
if (key == null) {
122-
throw logger.logExceptionAsError(new IllegalArgumentException("key cannot be null"));
137+
throw LOGGER.logExceptionAsError(new IllegalArgumentException("key cannot be null"));
123138
}
124139
for (Context c = this; c != null; c = c.parent) {
125140
if (key.equals(c.key)) {

0 commit comments

Comments
 (0)