Skip to content

Commit c6fdb63

Browse files
google-genai-botcopybara-github
authored andcommitted
feat: update State constructors to accept general Map types
PiperOrigin-RevId: 881890323
1 parent 20f863f commit c6fdb63

2 files changed

Lines changed: 67 additions & 7 deletions

File tree

core/src/main/java/com/google/adk/sessions/State.java

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.util.Set;
2525
import java.util.concurrent.ConcurrentHashMap;
2626
import java.util.concurrent.ConcurrentMap;
27+
import javax.annotation.Nullable;
2728

2829
/** A {@link State} object that also keeps track of the changes to the state. */
2930
@SuppressWarnings("ShouldNotSubclass")
@@ -39,13 +40,22 @@ public final class State implements ConcurrentMap<String, Object> {
3940
private final ConcurrentMap<String, Object> state;
4041
private final ConcurrentMap<String, Object> delta;
4142

42-
public State(ConcurrentMap<String, Object> state) {
43-
this(state, new ConcurrentHashMap<>());
44-
}
45-
46-
public State(ConcurrentMap<String, Object> state, ConcurrentMap<String, Object> delta) {
47-
this.state = Objects.requireNonNull(state);
48-
this.delta = delta;
43+
public State(Map<String, Object> state) {
44+
this(state, null);
45+
}
46+
47+
public State(Map<String, Object> state, @Nullable Map<String, Object> delta) {
48+
Objects.requireNonNull(state, "state is null");
49+
this.state =
50+
state instanceof ConcurrentMap
51+
? (ConcurrentMap<String, Object>) state
52+
: new ConcurrentHashMap<>(state);
53+
this.delta =
54+
delta == null
55+
? new ConcurrentHashMap<>()
56+
: delta instanceof ConcurrentMap
57+
? (ConcurrentMap<String, Object>) delta
58+
: new ConcurrentHashMap<>(delta);
4959
}
5060

5161
@Override
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.google.adk.sessions;
2+
3+
import static com.google.common.truth.Truth.assertThat;
4+
5+
import java.util.HashMap;
6+
import java.util.Map;
7+
import java.util.concurrent.ConcurrentHashMap;
8+
import java.util.concurrent.ConcurrentMap;
9+
import org.junit.Assert;
10+
import org.junit.Test;
11+
import org.junit.runner.RunWith;
12+
import org.junit.runners.JUnit4;
13+
14+
@RunWith(JUnit4.class)
15+
public class StateTest {
16+
@Test
17+
public void constructor_nullDelta_createsEmptyConcurrentHashMap() {
18+
ConcurrentMap<String, Object> stateMap = new ConcurrentHashMap<>();
19+
State state = new State(stateMap, null);
20+
assertThat(state.hasDelta()).isFalse();
21+
state.put("key", "value");
22+
assertThat(state.hasDelta()).isTrue();
23+
}
24+
25+
@Test
26+
public void constructor_nullState_throwsException() {
27+
Assert.assertThrows(NullPointerException.class, () -> new State(null, new HashMap<>()));
28+
}
29+
30+
@Test
31+
public void constructor_regularMapState() {
32+
Map<String, Object> stateMap = new HashMap<>();
33+
stateMap.put("initial", "val");
34+
State state = new State(stateMap, null);
35+
// It should have copied the contents
36+
assertThat(state).containsEntry("initial", "val");
37+
state.put("key", "value");
38+
// The original map should NOT be updated because a copy was created
39+
assertThat(stateMap).doesNotContainKey("key");
40+
}
41+
42+
@Test
43+
public void constructor_singleArgument() {
44+
ConcurrentMap<String, Object> stateMap = new ConcurrentHashMap<>();
45+
State state = new State(stateMap);
46+
assertThat(state.hasDelta()).isFalse();
47+
state.put("key", "value");
48+
assertThat(state.hasDelta()).isTrue();
49+
}
50+
}

0 commit comments

Comments
 (0)