public void clear() {
    checkState(
        !isClosed,
        "OrderedList user state is no longer usable because it is closed for %s",
        requestTemplate.getStateKey());
    isCleared = true;
    // Create a new object for pendingRemoves and clear the mappings in pendingAdds.
    // The entire tree range set of pendingRemoves and the old values in the pendingAdds are kept,
    // so that they will still be accessible in pre-existing iterables even after the state is
    // cleared.
    pendingRemoves = TreeRangeSet.create();
    pendingRemoves.add(
        Range.range(
            Instant.ofEpochMilli(Long.MIN_VALUE),
            BoundType.CLOSED,
            Instant.ofEpochMilli(Long.MAX_VALUE),
            BoundType.OPEN));
    pendingAdds.clear();
  }
@Test
  public void testClear() throws Exception {
    FakeBeamFnStateClient fakeClient =
        new FakeBeamFnStateClient(
            timestampedValueCoder,
            ImmutableMap.of(
                createOrderedListStateKey("A", 1),
                asList(A1, B1),
                createOrderedListStateKey("A", 4),
                Collections.singletonList(A4),
                createOrderedListStateKey("A", 2),
                asList(A2, B2),
                createOrderedListStateKey("A", 3),
                Collections.singletonList(A3)));

    OrderedListUserState<String> userState =
        new OrderedListUserState<>(
            Caches.noop(),
            fakeClient,
            "instructionId",
            createOrderedListStateKey("A"),
            StringUtf8Coder.of());

    Iterable<TimestampedValue<String>> stateBeforeClear = userState.read();
    userState.clear();
    assertArrayEquals(
        asList(A1, B1, A2, B2, A3, A4).toArray(),
        Iterables.toArray(stateBeforeClear, TimestampedValue.class));
    assertThat(userState.read(), is(emptyIterable()));

    userState.asyncClose();
    assertThrows(IllegalStateException.class, () -> userState.clear());
  }