Skip to content

Commit 4744ad5

Browse files
committed
Make Context::put calls chainable
1 parent 4ead4d8 commit 4744ad5

File tree

4 files changed

+10
-6
lines changed

4 files changed

+10
-6
lines changed

context/src/main/java/software/amazon/smithy/java/context/ArrayStorageContext.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public <T> T get(Key<T> key) {
3838
}
3939

4040
@Override
41-
public <T> void put(Key<T> key, T value) {
41+
public <T> Context put(Key<T> key, T value) {
4242
var idx = key.id;
4343
if (idx >= values.length) {
4444
resize();
@@ -50,6 +50,8 @@ public <T> void put(Key<T> key, T value) {
5050
if (idx >= Key.MAX_ARRAY_KEY_SPACE) {
5151
keys.put(key.id, key);
5252
}
53+
54+
return this;
5355
}
5456

5557
private void resize() {

context/src/main/java/software/amazon/smithy/java/context/Context.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ static <T> Key<T> key(String name, Function<T, T> copyFunction) {
100100
* @param value Value to set.
101101
* @param <T> Value type.
102102
*/
103-
<T> void put(Key<T> key, T value);
103+
<T> Context put(Key<T> key, T value);
104104

105105
/**
106106
* Set a Property if not already present.
@@ -109,10 +109,11 @@ static <T> Key<T> key(String name, Function<T, T> copyFunction) {
109109
* @param value Value to set.
110110
* @param <T> Value type.
111111
*/
112-
default <T> void putIfAbsent(Key<T> key, T value) {
112+
default <T> Context putIfAbsent(Key<T> key, T value) {
113113
if (get(key) == null) {
114114
put(key, value);
115115
}
116+
return this;
116117
}
117118

118119
/**

context/src/main/java/software/amazon/smithy/java/context/MapStorageContext.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ final class MapStorageContext implements Context {
1313
private final Map<Key<?>, Object> attributes = new HashMap<>();
1414

1515
@Override
16-
public <T> void put(Key<T> key, T value) {
16+
public <T> Context put(Key<T> key, T value) {
1717
attributes.put(key, value);
18+
return this;
1819
}
1920

2021
@Override

context/src/main/java/software/amazon/smithy/java/context/UnmodifiableContext.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ final class UnmodifiableContext implements Context {
1616
}
1717

1818
@Override
19-
public <T> void put(Key<T> key, T value) {
19+
public <T> Context put(Key<T> key, T value) {
2020
throw new UnsupportedOperationException();
2121
}
2222

2323
@Override
24-
public <T> void putIfAbsent(Key<T> key, T value) {
24+
public <T> Context putIfAbsent(Key<T> key, T value) {
2525
throw new UnsupportedOperationException();
2626
}
2727

0 commit comments

Comments
 (0)