Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 16 additions & 10 deletions core/src/main/java/com/google/adk/sessions/State.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,22 @@ public State(Map<String, Object> state) {

public State(Map<String, Object> state, @Nullable Map<String, Object> delta) {
Objects.requireNonNull(state, "state is null");
this.state =
state instanceof ConcurrentMap
? (ConcurrentMap<String, Object>) state
: new ConcurrentHashMap<>(state);
this.delta =
delta == null
? new ConcurrentHashMap<>()
: delta instanceof ConcurrentMap
? (ConcurrentMap<String, Object>) delta
: new ConcurrentHashMap<>(delta);
this.state = toConcurrentMap(state);
this.delta = toConcurrentMap(delta);
}

private static ConcurrentMap<String, Object> toConcurrentMap(Map<String, Object> map) {
if (map == null) {
return new ConcurrentHashMap<>();
}
if (map instanceof ConcurrentMap) {
return (ConcurrentMap<String, Object>) map;
}
ConcurrentMap<String, Object> concurrentMap = new ConcurrentHashMap<>();
for (Map.Entry<String, Object> entry : map.entrySet()) {
concurrentMap.put(entry.getKey(), entry.getValue() == null ? REMOVED : entry.getValue());
}
return concurrentMap;
}

@Override
Expand Down
16 changes: 14 additions & 2 deletions core/src/test/java/com/google/adk/sessions/StateTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ public void constructor_nullDelta_createsEmptyConcurrentHashMap() {
}

@Test
public void constructor_nullState_throwsException() {
Assert.assertThrows(NullPointerException.class, () -> new State(null, new HashMap<>()));
public void constructor_nullState_createsEmptyConcurrentHashMap() {
State state = new State(null, new HashMap<>());
assertThat(state.isEmpty()).isTrue();
assertThat(state.hasDelta()).isFalse();
}

@Test
Expand All @@ -47,4 +49,14 @@ public void constructor_singleArgument() {
state.put("key", "value");
assertThat(state.hasDelta()).isTrue();
}

@Test
public void constructor_stateMapWithNullValues_replacesWithRemoved() {
Map<String, Object> stateMap = new HashMap<>();
stateMap.put("key1", "value1");
stateMap.put("key2", null);
State state = new State(stateMap);
assertThat(state).containsEntry("key1", "value1");
assertThat(state).containsEntry("key2", State.REMOVED);
}
}
Loading