@Nullable
    public static <T extends KeyedStateHandle> T chooseTheBestStateHandleForInitial(
            @Nonnull List<T> restoreStateHandles,
            @Nonnull KeyGroupRange targetKeyGroupRange,
            double overlapFractionThreshold) {

        int pos =
                findTheBestStateHandleForInitial(
                        restoreStateHandles, targetKeyGroupRange, overlapFractionThreshold);
        return pos >= 0 ? restoreStateHandles.get(pos) : null;
    }
@Test
    public void testChooseTheBestStateHandleForInitial() {

        List<KeyedStateHandle> keyedStateHandles = new ArrayList<>(3);

        KeyedStateHandle keyedStateHandle1 = mock(KeyedStateHandle.class);
        when(keyedStateHandle1.getKeyGroupRange()).thenReturn(new KeyGroupRange(0, 3));
        keyedStateHandles.add(keyedStateHandle1);

        KeyedStateHandle keyedStateHandle2 = mock(KeyedStateHandle.class);
        when(keyedStateHandle2.getKeyGroupRange()).thenReturn(new KeyGroupRange(4, 7));
        keyedStateHandles.add(keyedStateHandle2);

        KeyedStateHandle keyedStateHandle3 = mock(KeyedStateHandle.class);
        when(keyedStateHandle3.getKeyGroupRange()).thenReturn(new KeyGroupRange(8, 12));
        keyedStateHandles.add(keyedStateHandle3);

        // this should choose keyedStateHandle2, because keyedStateHandle2's key-group range
        // satisfies the overlap fraction demand.
        Assert.assertEquals(
                keyedStateHandle2,
                RocksDBIncrementalCheckpointUtils.chooseTheBestStateHandleForInitial(
                        keyedStateHandles,
                        new KeyGroupRange(3, 6),
                        RESTORE_OVERLAP_FRACTION_THRESHOLD.defaultValue()));

        // both keyedStateHandle2 & keyedStateHandle3's key-group range satisfies the overlap
        // fraction, but keyedStateHandle3's key group range is better.
        Assert.assertEquals(
                keyedStateHandle3,
                RocksDBIncrementalCheckpointUtils.chooseTheBestStateHandleForInitial(
                        keyedStateHandles,
                        new KeyGroupRange(5, 12),
                        RESTORE_OVERLAP_FRACTION_THRESHOLD.defaultValue()));

        // The intersect key group number of keyedStateHandle2 & keyedStateHandle3's with [4, 11]
        // are 4. But the over fraction of keyedStateHandle2 is better.
        Assert.assertEquals(
                keyedStateHandle2,
                RocksDBIncrementalCheckpointUtils.chooseTheBestStateHandleForInitial(
                        keyedStateHandles,
                        new KeyGroupRange(4, 11),
                        RESTORE_OVERLAP_FRACTION_THRESHOLD.defaultValue()));

        // both keyedStateHandle2 & keyedStateHandle3's key-group range are covered by [3, 12],
        // but this should choose the keyedStateHandle3, because keyedStateHandle3's key-group is
        // bigger than keyedStateHandle2.
        Assert.assertEquals(
                keyedStateHandle3,
                RocksDBIncrementalCheckpointUtils.chooseTheBestStateHandleForInitial(
                        keyedStateHandles,
                        new KeyGroupRange(3, 12),
                        RESTORE_OVERLAP_FRACTION_THRESHOLD.defaultValue()));
    }