Refactor betweenTest and distinctIgnoreCase by wyyl1 · Pull Request #1602 · apitable/apitable

Submit a pull request for this project.

@Nested
class BetweenTest {

    private final LocalDateTime startDateTime = LocalDateTime.of(2022, 7, 29, 18, 34, 20);

    @TestFactory
    Stream<DynamicTest> endDateTimeLessStartDateTime() {
        return Stream.of(
            new DifferenceTime(0, 2022, 7, 29, 18, 34, 19),
            new DifferenceTime(0, 2022, 7, 29, 0, 0, 0),
            new DifferenceTime(-1, 2022, 7, 28, 18, 34, 20),
            new DifferenceTime(-2, 2022, 7, 27, 18, 34, 20)
        ).map(differenceTime -> DynamicTest.dynamicTest(String.format("%s natural time difference between %s and %s", differenceTime.expectDifferenceNaturalTime, startDateTime, differenceTime.endDateTime()),
            () -> Assertions.assertEquals(differenceTime.expectDifferenceNaturalTime(), DateTimeUtil.between(startDateTime, differenceTime.endDateTime(), ChronoField.EPOCH_DAY))));
    }

    @Test
    void return0WhenStartDateTimeEqualsEndDateTime() {
        Assertions.assertEquals(0, DateTimeUtil.between(startDateTime, startDateTime, ChronoField.EPOCH_DAY));
    }

    @TestFactory
    Stream<DynamicTest> endDateTimeGreaterStartDateTime() {
        return Stream.of(
            new DifferenceTime(0, 2022, 7, 29, 18, 34, 21),
            new DifferenceTime(1, 2022, 7, 30, 0, 0, 0),
            new DifferenceTime(1, 2022, 7, 30, 18, 34, 19),
            new DifferenceTime(1, 2022, 7, 30, 18, 34, 20),
            new DifferenceTime(1, 2022, 7, 30, 18, 34, 21),
            new DifferenceTime(2, 2022, 7, 31, 0, 0, 0),
            new DifferenceTime(3, 2022, 8, 1, 0, 0, 0)
        ).map(differenceTime -> DynamicTest.dynamicTest(String.format("%s natural time difference between %s and %s", differenceTime.expectDifferenceNaturalTime, startDateTime, differenceTime.endDateTime()),
            () -> Assertions.assertEquals(differenceTime.expectDifferenceNaturalTime(), DateTimeUtil.between(startDateTime, differenceTime.endDateTime(), ChronoField.EPOCH_DAY))));
    }

    private record DifferenceTime(int expectDifferenceNaturalTime, int year, int month, int dayOfMonth, int hour,
                                  int minute, int second) {
        LocalDateTime endDateTime(){
            return LocalDateTime.of(year, month, dayOfMonth, hour, minute, second);
        }

    }
}
public static List<String> distinctIgnoreCase(Collection<String> collection) {
    if (CollectionUtils.isEmpty(collection)) {
        return new ArrayList<>();
    }

    Set<String> caseInsensitiveSet = caseInsensitiveSetOf(collection);
    return filterAndCollect(collection, caseInsensitiveSet);
}

private static Set<String> caseInsensitiveSetOf(Collection<String> collection) {
    Set<String> caseInsensitiveSet = new TreeSet<>(String.CASE_INSENSITIVE_ORDER);
    caseInsensitiveSet.addAll(collection);
    return caseInsensitiveSet;
}

private static boolean containsSearchStr(String coll, Set<String> sets) {
    return sets.stream().anyMatch(coll::equals);
}

private static List<String> filterAndCollect(Collection<String> collection, Set<String> caseInsensitiveSet) {
    return collection.stream()
        .filter(coll -> containsSearchStr(coll, caseInsensitiveSet))
        .toList();
}