Skip to content

Commit

Permalink
core: add remove method for SessionContext (Netflix#1087)
Browse files Browse the repository at this point in the history
  • Loading branch information
carl-mastrangelo authored and argha-c committed Mar 18, 2022
1 parent bc68a1a commit 2e05ce2
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,23 @@ public <T> boolean remove(Key<T> key, T value) {
return res;
}

/**
* {@inheritDoc}
*
* <p>This method exists for static analysis.
*/
@Override
public Object remove(Object key) {
return super.remove(key);
}

public <T> T remove(Key<T> key) {
Objects.requireNonNull(key, "key");
@SuppressWarnings("unchecked") // sorry
T res = ((Map<Key<T>, T>) (Map) typedMap).remove(key);
return res;
}

public Set<Key<?>> keys() {
return Collections.unmodifiableSet(new HashSet<>(typedMap.keySet()));
}
Expand All @@ -206,6 +223,7 @@ public Set<Key<?>> keys() {
@Override
public SessionContext clone()
{
// TODO(carl-mastrangelo): copy over the type safe keys
return (SessionContext) super.clone();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,17 @@ public void getOrDefault_failsOnNullDefault() {

assertThrows(NullPointerException.class, () -> context.getOrDefault(key, null));
}

@Test
public void remove() {
SessionContext context = new SessionContext();
Key<String> key = SessionContext.newKey("foo");
context.put(key, "bar");

Truth.assertThat(context.get(key)).isEqualTo("bar");

String val = context.remove(key);
Truth.assertThat(context.get(key)).isNull();
Truth.assertThat(val).isEqualTo("bar");
}
}

0 comments on commit 2e05ce2

Please sign in to comment.