From cdfda89734b089906a86f15b17b9be33864a78df Mon Sep 17 00:00:00 2001 From: Ken Partlow Date: Mon, 29 Jan 2024 20:39:32 -0500 Subject: [PATCH] added private constructors for static classes --- README.md | 2 +- .../convert/AtomicBooleanConversions.java | 4 +- .../util/convert/BooleanConversions.java | 4 +- .../util/convert/ByteArrayConversions.java | 4 +- .../util/convert/ByteBufferConversions.java | 4 +- .../util/convert/CalendarConversions.java | 4 +- .../util/convert/CharArrayConversions.java | 5 ++- .../util/convert/CharBufferConversions.java | 5 ++- .../convert/CharacterArrayConversions.java | 35 +++++++++++++++ .../util/convert/CharacterConversions.java | 5 +-- .../util/convert/ClassConversions.java | 5 ++- .../util/convert/CommonValues.java | 4 +- .../cedarsoftware/util/convert/Converter.java | 5 ++- .../util/convert/ConverterOptions.java | 5 +-- .../util/convert/DateConversions.java | 4 +- .../util/convert/DurationConversions.java | 5 ++- .../util/convert/InstantConversions.java | 4 +- .../util/convert/LocalDateConversions.java | 4 +- .../convert/LocalDateTimeConversions.java | 5 ++- .../util/convert/LocalTimeConversions.java | 6 ++- .../util/convert/MapConversions.java | 4 +- .../util/convert/MonthDayConversions.java | 5 ++- .../util/convert/NumberConversions.java | 4 +- .../util/convert/StringConversions.java | 8 ++-- .../util/convert/VoidConversions.java | 2 - .../convert/ZonedDateTimeConversions.java | 4 +- .../convert/CharArrayConversionsTests.java | 45 +++++++++++++++++++ .../CharacterArrayConversionsTests.java | 43 ++++++++++++++++++ .../util/convert/ConverterTest.java | 23 ++++++++-- 29 files changed, 215 insertions(+), 42 deletions(-) create mode 100644 src/main/java/com/cedarsoftware/util/convert/CharacterArrayConversions.java create mode 100644 src/test/java/com/cedarsoftware/util/convert/CharArrayConversionsTests.java create mode 100644 src/test/java/com/cedarsoftware/util/convert/CharacterArrayConversionsTests.java diff --git a/README.md b/README.md index 8d555591..1b6c2a94 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ The classes in the`.jar`file are version 52 (`JDK 1.8`). --- To include in your project: -##### Gradle +##### GradleF ``` implementation 'com.cedarsoftware:java-util:2.4.0' ``` diff --git a/src/main/java/com/cedarsoftware/util/convert/AtomicBooleanConversions.java b/src/main/java/com/cedarsoftware/util/convert/AtomicBooleanConversions.java index 6fda5819..16558852 100644 --- a/src/main/java/com/cedarsoftware/util/convert/AtomicBooleanConversions.java +++ b/src/main/java/com/cedarsoftware/util/convert/AtomicBooleanConversions.java @@ -23,7 +23,9 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -public class AtomicBooleanConversions { +public final class AtomicBooleanConversions { + + private AtomicBooleanConversions() {} static Byte toByte(Object from, Converter converter, ConverterOptions options) { AtomicBoolean b = (AtomicBoolean) from; diff --git a/src/main/java/com/cedarsoftware/util/convert/BooleanConversions.java b/src/main/java/com/cedarsoftware/util/convert/BooleanConversions.java index 62a348af..2d34f3b0 100644 --- a/src/main/java/com/cedarsoftware/util/convert/BooleanConversions.java +++ b/src/main/java/com/cedarsoftware/util/convert/BooleanConversions.java @@ -25,9 +25,7 @@ * limitations under the License. */ public final class BooleanConversions { - - private BooleanConversions() { - } + private BooleanConversions() {} static Byte toByte(Object from, Converter converter, ConverterOptions options) { Boolean b = (Boolean) from; diff --git a/src/main/java/com/cedarsoftware/util/convert/ByteArrayConversions.java b/src/main/java/com/cedarsoftware/util/convert/ByteArrayConversions.java index a8021ffb..aa3c8ef0 100644 --- a/src/main/java/com/cedarsoftware/util/convert/ByteArrayConversions.java +++ b/src/main/java/com/cedarsoftware/util/convert/ByteArrayConversions.java @@ -6,7 +6,9 @@ import java.nio.CharBuffer; import java.util.concurrent.atomic.AtomicInteger; -public class ByteArrayConversions { +public final class ByteArrayConversions { + + private ByteArrayConversions() {} static String toString(Object from, ConverterOptions options) { byte[] bytes = (byte[])from; diff --git a/src/main/java/com/cedarsoftware/util/convert/ByteBufferConversions.java b/src/main/java/com/cedarsoftware/util/convert/ByteBufferConversions.java index 47546f04..fac926f0 100644 --- a/src/main/java/com/cedarsoftware/util/convert/ByteBufferConversions.java +++ b/src/main/java/com/cedarsoftware/util/convert/ByteBufferConversions.java @@ -7,7 +7,9 @@ import static com.cedarsoftware.util.ArrayUtilities.EMPTY_BYTE_ARRAY; -public class ByteBufferConversions { +public final class ByteBufferConversions { + + private ByteBufferConversions() {} static ByteBuffer asReadOnlyBuffer(Object from) { // Create a readonly buffer so we aren't changing diff --git a/src/main/java/com/cedarsoftware/util/convert/CalendarConversions.java b/src/main/java/com/cedarsoftware/util/convert/CalendarConversions.java index 3e93c218..34e0e2f5 100644 --- a/src/main/java/com/cedarsoftware/util/convert/CalendarConversions.java +++ b/src/main/java/com/cedarsoftware/util/convert/CalendarConversions.java @@ -30,7 +30,9 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -public class CalendarConversions { +public final class CalendarConversions { + + private CalendarConversions() {} static Date toDate(Object from) { return ((Calendar)from).getTime(); diff --git a/src/main/java/com/cedarsoftware/util/convert/CharArrayConversions.java b/src/main/java/com/cedarsoftware/util/convert/CharArrayConversions.java index a00485ca..5ae4e702 100644 --- a/src/main/java/com/cedarsoftware/util/convert/CharArrayConversions.java +++ b/src/main/java/com/cedarsoftware/util/convert/CharArrayConversions.java @@ -4,7 +4,9 @@ import java.nio.CharBuffer; import java.util.Arrays; -public class CharArrayConversions { +public final class CharArrayConversions { + + private CharArrayConversions() {} static String toString(Object from) { char[] chars = (char[])from; @@ -25,7 +27,6 @@ static String toString(Object from, Converter converter, ConverterOptions option return toString(from); } - static CharBuffer toCharBuffer(Object from, Converter converter, ConverterOptions options) { return toCharBuffer(from); } diff --git a/src/main/java/com/cedarsoftware/util/convert/CharBufferConversions.java b/src/main/java/com/cedarsoftware/util/convert/CharBufferConversions.java index 9c8c4299..6ec95821 100644 --- a/src/main/java/com/cedarsoftware/util/convert/CharBufferConversions.java +++ b/src/main/java/com/cedarsoftware/util/convert/CharBufferConversions.java @@ -6,7 +6,10 @@ import static com.cedarsoftware.util.ArrayUtilities.EMPTY_BYTE_ARRAY; import static com.cedarsoftware.util.ArrayUtilities.EMPTY_CHAR_ARRAY; -public class CharBufferConversions { +public final class CharBufferConversions { + + private CharBufferConversions() {} + static CharBuffer asReadOnlyBuffer(Object from) { // Create a readonly buffer so we aren't changing // the original buffers mark and position when diff --git a/src/main/java/com/cedarsoftware/util/convert/CharacterArrayConversions.java b/src/main/java/com/cedarsoftware/util/convert/CharacterArrayConversions.java new file mode 100644 index 00000000..500fcdac --- /dev/null +++ b/src/main/java/com/cedarsoftware/util/convert/CharacterArrayConversions.java @@ -0,0 +1,35 @@ +package com.cedarsoftware.util.convert; + +public class CharacterArrayConversions { + + static StringBuilder toStringBuilder(Object from) { + Character[] chars = (Character[]) from; + StringBuilder builder = new StringBuilder(chars.length); + for (Character ch : chars) { + builder.append(ch); + } + return builder; + } + + static StringBuffer toStringBuffer(Object from) { + Character[] chars = (Character[]) from; + StringBuffer buffer = new StringBuffer(chars.length); + for (Character ch : chars) { + buffer.append(ch); + } + return buffer; + } + + static String toString(Object from, Converter converter, ConverterOptions options) { + return toStringBuilder(from).toString(); + } + + static StringBuilder toStringBuilder(Object from, Converter converter, ConverterOptions options) { + return toStringBuilder(from); + } + + static StringBuffer toStringBuffer(Object from, Converter converter, ConverterOptions options) { + return toStringBuffer(from); + } + +} diff --git a/src/main/java/com/cedarsoftware/util/convert/CharacterConversions.java b/src/main/java/com/cedarsoftware/util/convert/CharacterConversions.java index b7a3f9e7..da34b686 100644 --- a/src/main/java/com/cedarsoftware/util/convert/CharacterConversions.java +++ b/src/main/java/com/cedarsoftware/util/convert/CharacterConversions.java @@ -23,10 +23,9 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -public class CharacterConversions { +public final class CharacterConversions { - private CharacterConversions() { - } + private CharacterConversions() {} static boolean toBoolean(Object from) { char c = (char) from; diff --git a/src/main/java/com/cedarsoftware/util/convert/ClassConversions.java b/src/main/java/com/cedarsoftware/util/convert/ClassConversions.java index 6311ec40..b02bc7e8 100644 --- a/src/main/java/com/cedarsoftware/util/convert/ClassConversions.java +++ b/src/main/java/com/cedarsoftware/util/convert/ClassConversions.java @@ -1,6 +1,9 @@ package com.cedarsoftware.util.convert; -public class ClassConversions { +public final class ClassConversions { + + private ClassConversions() {} + static String toString(Object from, Converter converter, ConverterOptions options) { Class cls = (Class) from; return cls.getName(); diff --git a/src/main/java/com/cedarsoftware/util/convert/CommonValues.java b/src/main/java/com/cedarsoftware/util/convert/CommonValues.java index 3b2370f2..4c41f9f8 100644 --- a/src/main/java/com/cedarsoftware/util/convert/CommonValues.java +++ b/src/main/java/com/cedarsoftware/util/convert/CommonValues.java @@ -17,7 +17,9 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -public class CommonValues { +public final class CommonValues { + + private CommonValues() {} public static final Byte BYTE_ZERO = (byte) 0; public static final Byte BYTE_ONE = (byte) 1; public static final Short SHORT_ZERO = (short) 0; diff --git a/src/main/java/com/cedarsoftware/util/convert/Converter.java b/src/main/java/com/cedarsoftware/util/convert/Converter.java index d310f898..4563c24a 100644 --- a/src/main/java/com/cedarsoftware/util/convert/Converter.java +++ b/src/main/java/com/cedarsoftware/util/convert/Converter.java @@ -581,9 +581,10 @@ private static void buildFactoryConversions() { DEFAULT_FACTORY.put(pair(AtomicLong.class, String.class), StringConversions::toString); DEFAULT_FACTORY.put(pair(byte[].class, String.class), ByteArrayConversions::toString); DEFAULT_FACTORY.put(pair(char[].class, String.class), CharArrayConversions::toString); + DEFAULT_FACTORY.put(pair(Character[].class, String.class), CharacterArrayConversions::toString); DEFAULT_FACTORY.put(pair(ByteBuffer.class, String.class), ByteBufferConversions::toString); DEFAULT_FACTORY.put(pair(CharBuffer.class, String.class), CharBufferConversions::toString); - DEFAULT_FACTORY.put(pair(Class.class, String.class), StringConversions::classToString); + DEFAULT_FACTORY.put(pair(Class.class, String.class), ClassConversions::toString); DEFAULT_FACTORY.put(pair(Date.class, String.class), DateConversions::dateToString); DEFAULT_FACTORY.put(pair(java.sql.Date.class, String.class), DateConversions::sqlDateToString); DEFAULT_FACTORY.put(pair(Timestamp.class, String.class), DateConversions::timestampToString); @@ -649,6 +650,7 @@ private static void buildFactoryConversions() { DEFAULT_FACTORY.put(pair(StringBuffer.class, StringBuffer.class), StringConversions::toStringBuffer); DEFAULT_FACTORY.put(pair(ByteBuffer.class, StringBuffer.class), ByteBufferConversions::toStringBuffer); DEFAULT_FACTORY.put(pair(CharBuffer.class, StringBuffer.class), CharBufferConversions::toStringBuffer); + DEFAULT_FACTORY.put(pair(Character[].class, StringBuffer.class), CharacterArrayConversions::toStringBuffer); DEFAULT_FACTORY.put(pair(char[].class, StringBuffer.class), CharArrayConversions::toStringBuffer); DEFAULT_FACTORY.put(pair(byte[].class, StringBuffer.class), ByteArrayConversions::toStringBuffer); @@ -659,6 +661,7 @@ private static void buildFactoryConversions() { DEFAULT_FACTORY.put(pair(StringBuffer.class, StringBuilder.class), StringConversions::toStringBuilder); DEFAULT_FACTORY.put(pair(ByteBuffer.class, StringBuilder.class), ByteBufferConversions::toStringBuilder); DEFAULT_FACTORY.put(pair(CharBuffer.class, StringBuilder.class), CharBufferConversions::toStringBuilder); + DEFAULT_FACTORY.put(pair(Character[].class, StringBuilder.class), CharacterArrayConversions::toStringBuilder); DEFAULT_FACTORY.put(pair(char[].class, StringBuilder.class), CharArrayConversions::toStringBuilder); DEFAULT_FACTORY.put(pair(byte[].class, StringBuilder.class), ByteArrayConversions::toStringBuilder); diff --git a/src/main/java/com/cedarsoftware/util/convert/ConverterOptions.java b/src/main/java/com/cedarsoftware/util/convert/ConverterOptions.java index 60f74f84..348d8ee4 100644 --- a/src/main/java/com/cedarsoftware/util/convert/ConverterOptions.java +++ b/src/main/java/com/cedarsoftware/util/convert/ConverterOptions.java @@ -27,12 +27,9 @@ public interface ConverterOptions { - ConcurrentHashMap customOptions = new ConcurrentHashMap(); - /** * @return zoneId to use for source conversion when on is not provided on the source (Date, Instant, etc.) */ - //TODO: should we just throw an exception here if they don't override? default ZoneId getSourceZoneIdForLocalDates() { return ZoneId.systemDefault(); } /** @@ -59,7 +56,7 @@ public interface ConverterOptions { /** * @return custom option */ - default T getCustomOption(String name) { return (T)customOptions.get(name); } + default T getCustomOption(String name) { return null; } /** * @return TimeZone expected on the target when finished (only for types that support ZoneId or TimeZone) diff --git a/src/main/java/com/cedarsoftware/util/convert/DateConversions.java b/src/main/java/com/cedarsoftware/util/convert/DateConversions.java index 44f5bd05..168f2dd1 100644 --- a/src/main/java/com/cedarsoftware/util/convert/DateConversions.java +++ b/src/main/java/com/cedarsoftware/util/convert/DateConversions.java @@ -31,7 +31,9 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -public class DateConversions { +public final class DateConversions { + + private DateConversions() {} static long toLong(Object from) { return ((Date) from).getTime(); diff --git a/src/main/java/com/cedarsoftware/util/convert/DurationConversions.java b/src/main/java/com/cedarsoftware/util/convert/DurationConversions.java index e70bb995..8692dcd6 100644 --- a/src/main/java/com/cedarsoftware/util/convert/DurationConversions.java +++ b/src/main/java/com/cedarsoftware/util/convert/DurationConversions.java @@ -22,7 +22,10 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -public class DurationConversions { +public final class DurationConversions { + + private DurationConversions() {} + static Map toMap(Object from, Converter converter, ConverterOptions options) { long sec = ((Duration) from).getSeconds(); long nanos = ((Duration) from).getNano(); diff --git a/src/main/java/com/cedarsoftware/util/convert/InstantConversions.java b/src/main/java/com/cedarsoftware/util/convert/InstantConversions.java index 3a016418..96468044 100644 --- a/src/main/java/com/cedarsoftware/util/convert/InstantConversions.java +++ b/src/main/java/com/cedarsoftware/util/convert/InstantConversions.java @@ -32,7 +32,9 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -public class InstantConversions { +public final class InstantConversions { + + private InstantConversions() {} static long toLong(Object from) { return ((Instant)from).toEpochMilli(); diff --git a/src/main/java/com/cedarsoftware/util/convert/LocalDateConversions.java b/src/main/java/com/cedarsoftware/util/convert/LocalDateConversions.java index 6bf2c0f7..e20d562c 100644 --- a/src/main/java/com/cedarsoftware/util/convert/LocalDateConversions.java +++ b/src/main/java/com/cedarsoftware/util/convert/LocalDateConversions.java @@ -30,7 +30,9 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -public class LocalDateConversions { +public final class LocalDateConversions { + + private LocalDateConversions() {} private static ZonedDateTime toZonedDateTime(Object from, ConverterOptions options) { return ((LocalDate)from).atStartOfDay(options.getSourceZoneIdForLocalDates()).withZoneSameInstant(options.getZoneId()); diff --git a/src/main/java/com/cedarsoftware/util/convert/LocalDateTimeConversions.java b/src/main/java/com/cedarsoftware/util/convert/LocalDateTimeConversions.java index bd1773be..44aed4f3 100644 --- a/src/main/java/com/cedarsoftware/util/convert/LocalDateTimeConversions.java +++ b/src/main/java/com/cedarsoftware/util/convert/LocalDateTimeConversions.java @@ -30,7 +30,10 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -public class LocalDateTimeConversions { +public final class LocalDateTimeConversions { + + private LocalDateTimeConversions() {} + private static ZonedDateTime toZonedDateTime(Object from, ConverterOptions options) { return ((LocalDateTime)from).atZone(options.getSourceZoneIdForLocalDates()).withZoneSameInstant(options.getZoneId()); } diff --git a/src/main/java/com/cedarsoftware/util/convert/LocalTimeConversions.java b/src/main/java/com/cedarsoftware/util/convert/LocalTimeConversions.java index 2e749941..43b44da7 100644 --- a/src/main/java/com/cedarsoftware/util/convert/LocalTimeConversions.java +++ b/src/main/java/com/cedarsoftware/util/convert/LocalTimeConversions.java @@ -22,7 +22,9 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -public class LocalTimeConversions { +public final class LocalTimeConversions { + + private LocalTimeConversions() {} static Map toMap(Object from, Converter converter, ConverterOptions options) { LocalTime localTime = (LocalTime) from; @@ -39,4 +41,4 @@ static Map toMap(Object from, Converter converter, ConverterOptions options) { } return target; } -} \ No newline at end of file +} diff --git a/src/main/java/com/cedarsoftware/util/convert/MapConversions.java b/src/main/java/com/cedarsoftware/util/convert/MapConversions.java index d49b95e9..05c27495 100644 --- a/src/main/java/com/cedarsoftware/util/convert/MapConversions.java +++ b/src/main/java/com/cedarsoftware/util/convert/MapConversions.java @@ -42,7 +42,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -public class MapConversions { +public final class MapConversions { private static final String V = "_v"; private static final String VALUE = "value"; @@ -59,6 +59,8 @@ public class MapConversions { private static final String NANOS = "nanos"; private static final String MOST_SIG_BITS = "mostSigBits"; private static final String LEAST_SIG_BITS = "leastSigBits"; + + private MapConversions() {} public static final String KEY_VALUE_ERROR_MESSAGE = "To convert from Map to %s the map must include one of the following: %s[_v], or [value] with associated values."; diff --git a/src/main/java/com/cedarsoftware/util/convert/MonthDayConversions.java b/src/main/java/com/cedarsoftware/util/convert/MonthDayConversions.java index e668dec6..8ca29b2f 100644 --- a/src/main/java/com/cedarsoftware/util/convert/MonthDayConversions.java +++ b/src/main/java/com/cedarsoftware/util/convert/MonthDayConversions.java @@ -22,7 +22,10 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -public class MonthDayConversions { +public final class MonthDayConversions { + + private MonthDayConversions() {} + static Map toMap(Object from, Converter converter, ConverterOptions options) { MonthDay monthDay = (MonthDay) from; Map target = new CompactLinkedMap<>(); diff --git a/src/main/java/com/cedarsoftware/util/convert/NumberConversions.java b/src/main/java/com/cedarsoftware/util/convert/NumberConversions.java index dffb033f..5f74ed62 100644 --- a/src/main/java/com/cedarsoftware/util/convert/NumberConversions.java +++ b/src/main/java/com/cedarsoftware/util/convert/NumberConversions.java @@ -35,7 +35,9 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -public class NumberConversions { +public final class NumberConversions { + + private NumberConversions() {} static byte toByte(Object from, Converter converter, ConverterOptions options) { return ((Number)from).byteValue(); diff --git a/src/main/java/com/cedarsoftware/util/convert/StringConversions.java b/src/main/java/com/cedarsoftware/util/convert/StringConversions.java index 90730db9..df6cf267 100644 --- a/src/main/java/com/cedarsoftware/util/convert/StringConversions.java +++ b/src/main/java/com/cedarsoftware/util/convert/StringConversions.java @@ -45,7 +45,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -public class StringConversions { +public final class StringConversions { private static final BigDecimal bigDecimalMinByte = BigDecimal.valueOf(Byte.MIN_VALUE); private static final BigDecimal bigDecimalMaxByte = BigDecimal.valueOf(Byte.MAX_VALUE); private static final BigDecimal bigDecimalMinShort = BigDecimal.valueOf(Short.MIN_VALUE); @@ -55,6 +55,8 @@ public class StringConversions { private static final BigDecimal bigDecimalMaxLong = BigDecimal.valueOf(Long.MAX_VALUE); private static final BigDecimal bigDecimalMinLong = BigDecimal.valueOf(Long.MIN_VALUE); + private StringConversions() {} + static String asString(Object from) { return from == null ? null : from.toString(); } @@ -264,10 +266,6 @@ static Class toClass(Object from, Converter converter, ConverterOptions optio throw new IllegalArgumentException("Cannot convert String '" + str + "' to class. Class not found."); } - static String classToString(Object from, Converter converter, ConverterOptions options) { - return ((Class) from).getName(); - } - static MonthDay toMonthDay(Object from, Converter converter, ConverterOptions options) { String monthDay = (String) from; return MonthDay.parse(monthDay); diff --git a/src/main/java/com/cedarsoftware/util/convert/VoidConversions.java b/src/main/java/com/cedarsoftware/util/convert/VoidConversions.java index 08218952..0717e8f4 100644 --- a/src/main/java/com/cedarsoftware/util/convert/VoidConversions.java +++ b/src/main/java/com/cedarsoftware/util/convert/VoidConversions.java @@ -18,10 +18,8 @@ * limitations under the License. */ public final class VoidConversions { - private VoidConversions() { } - static Object toNull(Object from, Converter converter, ConverterOptions options) { return null; } diff --git a/src/main/java/com/cedarsoftware/util/convert/ZonedDateTimeConversions.java b/src/main/java/com/cedarsoftware/util/convert/ZonedDateTimeConversions.java index 1d418475..ff77edd1 100644 --- a/src/main/java/com/cedarsoftware/util/convert/ZonedDateTimeConversions.java +++ b/src/main/java/com/cedarsoftware/util/convert/ZonedDateTimeConversions.java @@ -30,7 +30,9 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -public class ZonedDateTimeConversions { +public final class ZonedDateTimeConversions { + + private ZonedDateTimeConversions() {} static ZonedDateTime toDifferentZone(Object from, ConverterOptions options) { return ((ZonedDateTime)from).withZoneSameInstant(options.getZoneId()); diff --git a/src/test/java/com/cedarsoftware/util/convert/CharArrayConversionsTests.java b/src/test/java/com/cedarsoftware/util/convert/CharArrayConversionsTests.java new file mode 100644 index 00000000..dcefa904 --- /dev/null +++ b/src/test/java/com/cedarsoftware/util/convert/CharArrayConversionsTests.java @@ -0,0 +1,45 @@ +package com.cedarsoftware.util.convert; + +import com.cedarsoftware.util.io.ReadOptionsBuilder; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +import java.util.Map; +import java.util.stream.Stream; + +import static org.assertj.core.api.Assertions.assertThat; + +class CharArrayConversionsTests { + + private Converter converter; + + @BeforeEach + public void beforeEach() { + this.converter = new Converter(new DefaultConverterOptions()); + } + + private static Stream charSequenceClasses() { + return Stream.of( + Arguments.of(String.class), + Arguments.of(StringBuilder.class), + Arguments.of(StringBuffer.class) + ); + } + + @ParameterizedTest + @MethodSource("charSequenceClasses") + void testConvert_toCharSequence_withDifferentCharTypes(Class c) { + CharSequence s = this.converter.convert(new char[] { 'a', '\t', '\u0005'}, c); + assertThat(s.toString()).isEqualTo("a\t\u0005"); + } + + @ParameterizedTest + @MethodSource("charSequenceClasses") + void testConvert_toCharSequence_withEmptyArray_returnsEmptyString(Class c) { + CharSequence s = this.converter.convert(new char[]{}, String.class); + assertThat(s.toString()).isEqualTo(""); + } +} diff --git a/src/test/java/com/cedarsoftware/util/convert/CharacterArrayConversionsTests.java b/src/test/java/com/cedarsoftware/util/convert/CharacterArrayConversionsTests.java new file mode 100644 index 00000000..659b01bc --- /dev/null +++ b/src/test/java/com/cedarsoftware/util/convert/CharacterArrayConversionsTests.java @@ -0,0 +1,43 @@ +package com.cedarsoftware.util.convert; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +import javax.swing.text.Segment; +import java.util.stream.Stream; + +import static org.assertj.core.api.Assertions.assertThat; + +class CharacterArrayConversionsTests { + private Converter converter; + + @BeforeEach + public void beforeEach() { + this.converter = new Converter(new DefaultConverterOptions()); + } + + private static Stream charSequenceClasses() { + return Stream.of( + Arguments.of(String.class), + Arguments.of(StringBuilder.class), + Arguments.of(StringBuffer.class) + ); + } + + @ParameterizedTest + @MethodSource("charSequenceClasses") + void testConvert_toCharSequence_withDifferentCharTypes(Class c) { + CharSequence s = this.converter.convert(new Character[] { 'a', '\t', '\u0006'}, c); + assertThat(s.toString()).isEqualTo("a\t\u0006"); + } + + @ParameterizedTest + @MethodSource("charSequenceClasses") + void testConvert_toCharSequence_withEmptyArray_returnsEmptyString(Class c) { + CharSequence s = this.converter.convert(new Character[]{}, c); + assertThat(s.toString()).isEqualTo(""); + } +} diff --git a/src/test/java/com/cedarsoftware/util/convert/ConverterTest.java b/src/test/java/com/cedarsoftware/util/convert/ConverterTest.java index 21fc493b..dc36509d 100644 --- a/src/test/java/com/cedarsoftware/util/convert/ConverterTest.java +++ b/src/test/java/com/cedarsoftware/util/convert/ConverterTest.java @@ -764,8 +764,6 @@ void testEpochMilliWithZoneId(String epochMilli, ZoneId zoneId) { .hasSecond(59); } - - @ParameterizedTest @MethodSource("dateStringNoZoneOffset") void testStringDateWithNoTimeZoneInformation(String date, ZoneId zoneId) { @@ -852,6 +850,25 @@ private static Stream epochMillis_withLocalDateTimeInformation() { ); } + @Test + void testEpochMillis() { + Instant instant = Instant.ofEpochMilli(1687622249729L); + + ZonedDateTime tokyo = instant.atZone(TOKYO); + assertThat(tokyo.toString()).contains("2023-06-25T00:57:29.729"); + assertThat(tokyo.toInstant().toEpochMilli()).isEqualTo(1687622249729L); + + ZonedDateTime ny = instant.atZone(NEW_YORK); + assertThat(ny.toString()).contains("2023-06-24T11:57:29.729"); + assertThat(ny.toInstant().toEpochMilli()).isEqualTo(1687622249729L); + + ZonedDateTime converted = tokyo.withZoneSameInstant(NEW_YORK); + assertThat(ny).isEqualTo(converted); + assertThat(converted.toInstant().toEpochMilli()).isEqualTo(1687622249729L); + } + + + @ParameterizedTest @MethodSource("epochMillis_withLocalDateTimeInformation") void testCalendarToLocalDateTime(long epochMilli, ZoneId zoneId, LocalDateTime expected) { @@ -2068,7 +2085,7 @@ void testAtomicInteger_withIllegalArguments(Object value, String partialMessage) private static Stream epochMilli_exampleOneParams() { return Stream.of( Arguments.of(1705601070270L), - Arguments.of( new Long(1705601070270L)), + Arguments.of( Long.valueOf(1705601070270L)), Arguments.of( new AtomicLong(1705601070270L)), Arguments.of( 1705601070270.798659898d), Arguments.of( BigInteger.valueOf(1705601070270L)),