Skip to content

Commit

Permalink
Undid StringUtilities isEmpty changes
Browse files Browse the repository at this point in the history
  • Loading branch information
kpartlow committed Jan 26, 2024
1 parent b6bc1c0 commit f9976ef
Show file tree
Hide file tree
Showing 5 changed files with 186 additions and 24 deletions.
26 changes: 13 additions & 13 deletions src/main/java/com/cedarsoftware/util/StringUtilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -181,24 +181,25 @@ public static boolean equalsIgnoreCaseWithTrim(final String s1, final String s2)
}

/**
* Checks if a CharSequence is empty ("") or null.
* Checks if a CharSequence is empty (""), null, or only whitespace.
*
* @param cs the CharSequence to check, may be null
* @return {@code true} if the CharSequence is empty or null
* @since 3.0 Changed signature from isEmpty(String) to isEmpty(CharSequence)
*/
public static boolean isEmpty(final CharSequence cs) {
return cs == null || cs.length() == 0;
public static boolean isEmpty(CharSequence cs)
{
return isWhitespace(cs);
}

/**
* Checks if a CharSequence is not empty ("") and not null.
* Checks if a CharSequence is not empty (""), not null and not whitespace only.
*
* @param cs the CharSequence to check, may be null
* @return {@code true} if the CharSequence is not empty and not null
* @return {@code true} if the CharSequence is
* not empty and not null and not whitespace only
*/
public static boolean isNotEmpty(final CharSequence cs) {
return !isEmpty(cs);
public static boolean isNotWhitespace(final CharSequence cs) {
return !isWhitespace(cs);
}

/**
Expand All @@ -221,13 +222,12 @@ public static boolean isWhitespace(final CharSequence cs) {
}

/**
* Checks if a CharSequence is not empty (""), not null and not whitespace only.
* Checks if a CharSequence is not null, not empty (""), and not only whitespace.
*
* @param cs the CharSequence to check, may be null
* @return {@code true} if the CharSequence is
* not empty and not null and not whitespace only
* @return {@code true} if the CharSequence is not empty and not null
*/
public static boolean hasContent(final CharSequence cs) {
public static boolean isNotEmpty(final CharSequence cs) {
return !isWhitespace(cs);
}

Expand All @@ -238,7 +238,7 @@ public static boolean hasContent(final CharSequence cs) {
* @return {@code true} if the CharSequence is
* not empty and not null and not whitespace only
*/
public static boolean isNotWhitespace(final CharSequence cs) {
public static boolean hasContent(final CharSequence cs) {
return !isWhitespace(cs);
}

Expand Down
10 changes: 5 additions & 5 deletions src/main/java/com/cedarsoftware/util/convert/Converter.java
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ private static void buildFactoryConversions() {
DEFAULT_FACTORY.put(pair(Boolean.class, Float.class), BooleanConversions::toFloat);
DEFAULT_FACTORY.put(pair(Character.class, Float.class), CharacterConversions::toFloat);
DEFAULT_FACTORY.put(pair(Instant.class, Float.class), InstantConversions::toFloat);
DEFAULT_FACTORY.put(pair(LocalDate.class, Float.class), LocalDateConversions::toLong);
DEFAULT_FACTORY.put(pair(LocalDate.class, Float.class), LocalDateConversions::toFloat);
DEFAULT_FACTORY.put(pair(AtomicBoolean.class, Float.class), AtomicBooleanConversions::toFloat);
DEFAULT_FACTORY.put(pair(AtomicInteger.class, Float.class), NumberConversions::toFloat);
DEFAULT_FACTORY.put(pair(AtomicLong.class, Float.class), NumberConversions::toFloat);
Expand All @@ -231,7 +231,7 @@ private static void buildFactoryConversions() {
DEFAULT_FACTORY.put(pair(Boolean.class, Double.class), BooleanConversions::toDouble);
DEFAULT_FACTORY.put(pair(Character.class, Double.class), CharacterConversions::toDouble);
DEFAULT_FACTORY.put(pair(Instant.class, Double.class), InstantConversions::toDouble);
DEFAULT_FACTORY.put(pair(LocalDate.class, Double.class), LocalDateConversions::toLong);
DEFAULT_FACTORY.put(pair(LocalDate.class, Double.class), LocalDateConversions::toDouble);
DEFAULT_FACTORY.put(pair(LocalDateTime.class, Double.class), LocalDateTimeConversions::toLong);
DEFAULT_FACTORY.put(pair(ZonedDateTime.class, Double.class), ZonedDateTimeConversions::toLong);
DEFAULT_FACTORY.put(pair(Date.class, Double.class), DateConversions::toLong);
Expand Down Expand Up @@ -554,8 +554,8 @@ private static void buildFactoryConversions() {
DEFAULT_FACTORY.put(pair(Timestamp.class, LocalDate.class), DateConversions::toLocalDate);
DEFAULT_FACTORY.put(pair(Date.class, LocalDate.class), DateConversions::toLocalDate);
DEFAULT_FACTORY.put(pair(Instant.class, LocalDate.class), InstantConversions::toLocalDate);
DEFAULT_FACTORY.put(pair(LocalDate.class, LocalDate.class), Converter::identity);
DEFAULT_FACTORY.put(pair(LocalDateTime.class, LocalDate.class), (fromInstance, converter, options) -> ((LocalDateTime) fromInstance).toLocalDate());
DEFAULT_FACTORY.put(pair(LocalDate.class, LocalDate.class), LocalDateConversions::toLocalDate);
DEFAULT_FACTORY.put(pair(LocalDateTime.class, LocalDate.class), LocalDateTimeConversions::toLocalDate);
DEFAULT_FACTORY.put(pair(ZonedDateTime.class, LocalDate.class), ZonedDateTimeConversions::toLocalDate);
DEFAULT_FACTORY.put(pair(Calendar.class, LocalDate.class), CalendarConversions::toLocalDate);
DEFAULT_FACTORY.put(pair(Number.class, LocalDate.class), NumberConversions::toLocalDate);
Expand Down Expand Up @@ -590,7 +590,7 @@ private static void buildFactoryConversions() {
DEFAULT_FACTORY.put(pair(Timestamp.class, LocalDateTime.class), DateConversions::toLocalDateTime);
DEFAULT_FACTORY.put(pair(Date.class, LocalDateTime.class), DateConversions::toLocalDateTime);
DEFAULT_FACTORY.put(pair(Instant.class, LocalDateTime.class), InstantConversions::toLocalDateTime);
DEFAULT_FACTORY.put(pair(LocalDateTime.class, LocalDateTime.class), Converter::identity);
DEFAULT_FACTORY.put(pair(LocalDateTime.class, LocalDateTime.class), LocalDateTimeConversions::toLocalDateTime);
DEFAULT_FACTORY.put(pair(LocalDate.class, LocalDateTime.class), LocalDateConversions::toLocalDateTime);
DEFAULT_FACTORY.put(pair(ZonedDateTime.class, LocalDateTime.class), ZonedDateTimeConversions::toLocalDateTime);
DEFAULT_FACTORY.put(pair(Calendar.class, LocalDateTime.class), CalendarConversions::toLocalDateTime);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ static LocalDateTime toLocalDateTime(Object fromInstance, Converter converter, C
return toZonedDateTime(fromInstance, options).toLocalDateTime();
}

static LocalDate toLocalDate(Object fromInstance, Converter converter, ConverterOptions options) {
return toZonedDateTime(fromInstance, options).toLocalDate();
}

static LocalTime toLocalTime(Object fromInstance, Converter converter, ConverterOptions options) {
return toZonedDateTime(fromInstance, options).toLocalTime();
}
Expand Down
8 changes: 4 additions & 4 deletions src/test/java/com/cedarsoftware/util/TestStringUtilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ void testConstructorIsPrivate() throws Exception {

@ParameterizedTest
@MethodSource("stringsWithAllWhitespace")
void testIsEmpty_whenStringHasWhitespace_returnsFalse(String s)
void testIsEmpty_whenStringHasOnlyWhitespace_returnsTrue(String s)
{
assertFalse(StringUtilities.isEmpty(s));
assertTrue(StringUtilities.isEmpty(s));
}

@ParameterizedTest
Expand All @@ -83,9 +83,9 @@ void testIsEmpty_whenNullOrEmpty_returnsTrue(String s)

@ParameterizedTest
@MethodSource("stringsWithAllWhitespace")
void testIsNotEmpty_whenStringHasWhitespace_returnsTrue(String s)
void testIsNotEmpty_whenStringHasOnlyWhitespace_returnsFalse(String s)
{
assertTrue(StringUtilities.isNotEmpty(s));
assertFalse(StringUtilities.isNotEmpty(s));
}

@ParameterizedTest
Expand Down
162 changes: 160 additions & 2 deletions src/test/java/com/cedarsoftware/util/convert/ConverterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -795,15 +795,16 @@ void testCalendar_roundTrip(long epochMilli, ZoneId zoneId, LocalDateTime expect
}


private static Stream<Arguments> roundTrip_localDates() {
private static Stream<Arguments> roundTrip_tokyoTime() {
return Stream.of(
Arguments.of(946652400000L, TOKYO, LD_MILLINNIUM_TOKYO),
Arguments.of(946652400000L, NEW_YORK, LD_MILLINNIUM_NY),
Arguments.of(946652400000L, CHICAGO, LD_MILLENNIUM_CHICAGO)
);
}

@ParameterizedTest
@MethodSource("roundTrip_localDates")
@MethodSource("roundTrip_tokyoTime")
void testCalendar_roundTrip_withLocalDate(long epochMilli, ZoneId zoneId, LocalDate expected) {
Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone(zoneId));
calendar.setTimeInMillis(epochMilli);
Expand All @@ -825,6 +826,154 @@ void testCalendar_roundTrip_withLocalDate(long epochMilli, ZoneId zoneId, LocalD
assertThat(actual.getTimeInMillis()).isEqualTo(epochMilli);
}

private static Stream<Arguments> localDateToLong() {
return Stream.of(
Arguments.of(946616400000L, NEW_YORK, LD_MILLINNIUM_NY, TOKYO),
Arguments.of(946616400000L, NEW_YORK, LD_MILLINNIUM_NY, CHICAGO),
Arguments.of(946620000000L, CHICAGO, LD_MILLENNIUM_CHICAGO, TOKYO)
);
}
@ParameterizedTest
@MethodSource("localDateToLong")
void testConvertLocalDateToLongAndBack(long epochMilli, ZoneId zoneId, LocalDate expected, ZoneId targetZone) {

long intermediate = this.converter.convert(expected, long.class, createConvertOptions(zoneId, targetZone));

assertThat(intermediate).isEqualTo(epochMilli);

LocalDate actual = this.converter.convert(intermediate, LocalDate.class, createConvertOptions(targetZone, zoneId));

assertThat(actual).isEqualTo(expected);
}

@ParameterizedTest
@MethodSource("localDateToLong")
void testLocalDateToInstantAndBack(long epochMilli, ZoneId zoneId, LocalDate expected, ZoneId targetZone) {

Instant intermediate = this.converter.convert(expected, Instant.class, createConvertOptions(zoneId, targetZone));

assertThat(intermediate.toEpochMilli()).isEqualTo(epochMilli);

LocalDate actual = this.converter.convert(intermediate, LocalDate.class, createConvertOptions(targetZone, zoneId));

assertThat(actual).isEqualTo(expected);
}

@ParameterizedTest
@MethodSource("localDateToLong")
void testLocalDateToDoubleAndBack(long epochMilli, ZoneId zoneId, LocalDate expected, ZoneId targetZone) {

double intermediate = this.converter.convert(expected, double.class, createConvertOptions(zoneId, targetZone));

assertThat((long)intermediate).isEqualTo(epochMilli);

LocalDate actual = this.converter.convert(intermediate, LocalDate.class, createConvertOptions(targetZone, zoneId));

assertThat(actual).isEqualTo(expected);
}

@ParameterizedTest
@MethodSource("localDateToLong")
void testLocalDateToAtomicLongAndBack(long epochMilli, ZoneId zoneId, LocalDate expected, ZoneId targetZone) {

AtomicLong intermediate = this.converter.convert(expected, AtomicLong.class, createConvertOptions(zoneId, targetZone));

assertThat(intermediate.get()).isEqualTo(epochMilli);

LocalDate actual = this.converter.convert(intermediate, LocalDate.class, createConvertOptions(targetZone, zoneId));

assertThat(actual).isEqualTo(expected);
}

@ParameterizedTest
@MethodSource("localDateToLong")
void testLocalDateToDateAndBack(long epochMilli, ZoneId zoneId, LocalDate expected, ZoneId targetZone) {

Date intermediate = this.converter.convert(expected,Date.class, createConvertOptions(zoneId, targetZone));

assertThat(intermediate.getTime()).isEqualTo(epochMilli);

LocalDate actual = this.converter.convert(intermediate, LocalDate.class, createConvertOptions(targetZone, zoneId));

assertThat(actual).isEqualTo(expected);
}

@ParameterizedTest
@MethodSource("localDateToLong")
void testLocalDateSqlDateAndBack(long epochMilli, ZoneId zoneId, LocalDate expected, ZoneId targetZone) {

java.sql.Date intermediate = this.converter.convert(expected, java.sql.Date.class, createConvertOptions(zoneId, targetZone));

assertThat(intermediate.getTime()).isEqualTo(epochMilli);

LocalDate actual = this.converter.convert(intermediate, LocalDate.class, createConvertOptions(targetZone, zoneId));

assertThat(actual).isEqualTo(expected);
}

@ParameterizedTest
@MethodSource("localDateToLong")
void testLocalDateTimestampAndBack(long epochMilli, ZoneId zoneId, LocalDate expected, ZoneId targetZone) {

Timestamp intermediate = this.converter.convert(expected, Timestamp.class, createConvertOptions(zoneId, targetZone));

assertThat(intermediate.getTime()).isEqualTo(epochMilli);

LocalDate actual = this.converter.convert(intermediate, LocalDate.class, createConvertOptions(targetZone, zoneId));

assertThat(actual).isEqualTo(expected);
}

@ParameterizedTest
@MethodSource("localDateToLong")
void testLocalDateZonedDateTimeAndBack(long epochMilli, ZoneId zoneId, LocalDate expected, ZoneId targetZone) {

ZonedDateTime intermediate = this.converter.convert(expected, ZonedDateTime.class, createConvertOptions(zoneId, targetZone));

assertThat(intermediate.toInstant().toEpochMilli()).isEqualTo(epochMilli);

LocalDate actual = this.converter.convert(intermediate, LocalDate.class, createConvertOptions(targetZone, zoneId));

assertThat(actual).isEqualTo(expected);
}

@ParameterizedTest
@MethodSource("localDateToLong")
void testLocalDateToLocalDateTimeAndBack(long epochMilli, ZoneId zoneId, LocalDate expected, ZoneId targetZone) {

LocalDateTime intermediate = this.converter.convert(expected, LocalDateTime.class, createConvertOptions(zoneId, targetZone));

LocalDate actual = this.converter.convert(intermediate, LocalDate.class, createConvertOptions(targetZone, zoneId));

assertThat(actual).isEqualTo(expected);
}

@ParameterizedTest
@MethodSource("localDateToLong")
void testLocalDateBigIntegerAndBack(long epochMilli, ZoneId zoneId, LocalDate expected, ZoneId targetZone) {

BigInteger intermediate = this.converter.convert(expected, BigInteger.class, createConvertOptions(zoneId, targetZone));

assertThat(intermediate.longValue()).isEqualTo(epochMilli);

LocalDate actual = this.converter.convert(intermediate, LocalDate.class, createConvertOptions(targetZone, zoneId));

assertThat(actual).isEqualTo(expected);
}

@ParameterizedTest
@MethodSource("localDateToLong")
void testLocalDateBigDecimalAndBack(long epochMilli, ZoneId zoneId, LocalDate expected, ZoneId targetZone) {

BigDecimal intermediate = this.converter.convert(expected, BigDecimal.class, createConvertOptions(zoneId, targetZone));

assertThat(intermediate.longValue()).isEqualTo(epochMilli);

LocalDate actual = this.converter.convert(intermediate, LocalDate.class, createConvertOptions(targetZone, zoneId));

assertThat(actual).isEqualTo(expected);
}

@ParameterizedTest
@MethodSource("epochMillis_withLocalDateTimeInformation")
void testZonedDateTimeToLocalDateTime(long epochMilli, ZoneId zoneId, LocalDateTime expected)
Expand Down Expand Up @@ -915,6 +1064,15 @@ void testDateToZonedDateTime(long epochMilli, ZoneId zoneId, LocalDateTime expec
assertThat(zonedDateTime.toLocalDateTime()).isEqualTo(expected);
}

@ParameterizedTest
@MethodSource("epochMillis_withLocalDateTimeInformation")
void testInstantToZonedDateTime(long epochMilli, ZoneId zoneId, LocalDateTime expected)
{
Instant date = Instant.ofEpochMilli(epochMilli);
ZonedDateTime zonedDateTime = this.converter.convert(date, ZonedDateTime.class, createConvertOptions(null, zoneId));
assertThat(zonedDateTime.toInstant()).isEqualTo(date);
}

@ParameterizedTest
@MethodSource("epochMillis_withLocalDateTimeInformation")
void testDateToInstant(long epochMilli, ZoneId zoneId, LocalDateTime expected)
Expand Down

0 comments on commit f9976ef

Please sign in to comment.