From 0b09e98f31929ff99dbdcb5c391d1fa90e45ddd0 Mon Sep 17 00:00:00 2001 From: Ken Partlow Date: Mon, 24 Feb 2025 21:06:02 -0500 Subject: [PATCH] cleaning up warnings --- .../cedarsoftware/util/ClassUtilities.java | 6 +++- .../com/cedarsoftware/util/Converter.java | 3 ++ .../cedarsoftware/util/ReflectionUtils.java | 10 ++---- .../util/SafeSimpleDateFormat.java | 2 +- .../cedarsoftware/util/StringUtilities.java | 32 +++++++++++-------- .../util/cache/LockingLRUCacheStrategy.java | 6 ++-- 6 files changed, 33 insertions(+), 26 deletions(-) diff --git a/src/main/java/com/cedarsoftware/util/ClassUtilities.java b/src/main/java/com/cedarsoftware/util/ClassUtilities.java index 94e44be1..23b28e80 100644 --- a/src/main/java/com/cedarsoftware/util/ClassUtilities.java +++ b/src/main/java/com/cedarsoftware/util/ClassUtilities.java @@ -147,6 +147,10 @@ * limitations under the License. */ public class ClassUtilities { + + private ClassUtilities() { + } + private static final Set> prims = new HashSet<>(); private static final Map, Class> primitiveToWrapper = new HashMap<>(20, .8f); private static final Map> nameToClass = new ConcurrentHashMap<>(); @@ -1546,4 +1550,4 @@ private static int getDepth(Class clazz) { public static boolean haveCommonAncestor(Class a, Class b) { return !findLowestCommonSupertypes(a, b).isEmpty(); } -} \ No newline at end of file +} diff --git a/src/main/java/com/cedarsoftware/util/Converter.java b/src/main/java/com/cedarsoftware/util/Converter.java index a9b78ce1..891a7e06 100644 --- a/src/main/java/com/cedarsoftware/util/Converter.java +++ b/src/main/java/com/cedarsoftware/util/Converter.java @@ -774,6 +774,7 @@ public static AtomicBoolean convertToAtomicBoolean(Object fromInstance) * No longer needed - use convert(localDate, long.class) * @param localDate A Java LocalDate * @return a long representing the localDate as epoch milliseconds (since 1970 Jan 1 at midnight) + * @deprecated replaced by convert(localDate, long.class) */ @Deprecated public static long localDateToMillis(LocalDate localDate) @@ -785,6 +786,7 @@ public static long localDateToMillis(LocalDate localDate) * No longer needed - use convert(localDateTime, long.class) * @param localDateTime A Java LocalDateTime * @return a long representing the localDateTime as epoch milliseconds (since 1970 Jan 1 at midnight) + * @deprecated replaced by convert(localDateTime, long.class) */ @Deprecated public static long localDateTimeToMillis(LocalDateTime localDateTime) @@ -796,6 +798,7 @@ public static long localDateTimeToMillis(LocalDateTime localDateTime) * No longer needed - use convert(ZonedDateTime, long.class) * @param zonedDateTime A Java ZonedDateTime * @return a long representing the ZonedDateTime as epoch milliseconds (since 1970 Jan 1 at midnight) + * @deprecated replaced by convert(ZonedDateTime, long.class) */ @Deprecated public static long zonedDateTimeToMillis(ZonedDateTime zonedDateTime) diff --git a/src/main/java/com/cedarsoftware/util/ReflectionUtils.java b/src/main/java/com/cedarsoftware/util/ReflectionUtils.java index b4e8255b..07cb009a 100644 --- a/src/main/java/com/cedarsoftware/util/ReflectionUtils.java +++ b/src/main/java/com/cedarsoftware/util/ReflectionUtils.java @@ -360,12 +360,8 @@ public int hashCode() { return false; } - if (declaringClass.isAssignableFrom(Enum.class) && - ("hash".equals(fieldName) || "ordinal".equals(fieldName))) { - return false; - } - - return true; + return !declaringClass.isAssignableFrom(Enum.class) || + (!"hash".equals(fieldName) && !"ordinal".equals(fieldName)); }; /** @@ -1488,4 +1484,4 @@ private static String getClassLoaderName(Class c) { // Example: "org.example.MyLoader@1a2b3c4" return loader.getClass().getName() + '@' + Integer.toHexString(System.identityHashCode(loader)); } -} \ No newline at end of file +} diff --git a/src/main/java/com/cedarsoftware/util/SafeSimpleDateFormat.java b/src/main/java/com/cedarsoftware/util/SafeSimpleDateFormat.java index e7285cb9..039fd3da 100644 --- a/src/main/java/com/cedarsoftware/util/SafeSimpleDateFormat.java +++ b/src/main/java/com/cedarsoftware/util/SafeSimpleDateFormat.java @@ -118,7 +118,7 @@ public void set2DigitYearStart(Date date) } public String toString() { - return _format.toString(); + return _format; } @Override diff --git a/src/main/java/com/cedarsoftware/util/StringUtilities.java b/src/main/java/com/cedarsoftware/util/StringUtilities.java index 6eb23d67..6c4ee57d 100644 --- a/src/main/java/com/cedarsoftware/util/StringUtilities.java +++ b/src/main/java/com/cedarsoftware/util/StringUtilities.java @@ -1,6 +1,8 @@ package com.cedarsoftware.util; import java.io.UnsupportedEncodingException; +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.util.Arrays; import java.util.Collections; import java.util.LinkedHashSet; @@ -115,7 +117,7 @@ * limitations under the License. */ public final class StringUtilities { - private static char[] _hex = { + private static final char[] _hex = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; @@ -636,7 +638,7 @@ public static String getRandomString(Random random, int minLen, int maxLen) { public static String getRandomChar(Random random, boolean upper) { int r = random.nextInt(26); - return upper ? EMPTY + (char) ((int) 'A' + r) : EMPTY + (char) ((int) 'a' + r); + return upper ? EMPTY + (char) ('A' + r) : EMPTY + (char) ('a' + r); } /** @@ -658,6 +660,8 @@ public static byte[] getBytes(String s, String encoding) { } + // TODO: The following two methods are exactly the same other than the case of the method. + // TODO: deprecate one and remove next major version. /** * Convert a byte[] into a UTF-8 String. Preferable used when the encoding * is one of the guaranteed Java types and you don't want to have to catch @@ -666,7 +670,16 @@ public static byte[] getBytes(String s, String encoding) { * @param bytes bytes to encode into a string */ public static String createUtf8String(byte[] bytes) { - return createString(bytes, "UTF-8"); + return bytes == null ? null : new String(bytes, StandardCharsets.UTF_8); + } + + /** + * Convert a byte[] into a UTF-8 encoded String. + * + * @param bytes bytes to encode into a string + */ + public static String createUTF8String(byte[] bytes) { + return bytes == null ? null : new String(bytes, StandardCharsets.UTF_8); } /** @@ -675,7 +688,7 @@ public static String createUtf8String(byte[] bytes) { * @param s string to encode into bytes */ public static byte[] getUTF8Bytes(String s) { - return getBytes(s, "UTF-8"); + return s == null ? null : s.getBytes(StandardCharsets.UTF_8); } /** @@ -696,15 +709,6 @@ public static String createString(byte[] bytes, String encoding) { } } - /** - * Convert a byte[] into a UTF-8 encoded String. - * - * @param bytes bytes to encode into a string - */ - public static String createUTF8String(byte[] bytes) { - return createString(bytes, "UTF-8"); - } - /** * Get the hashCode of a String, insensitive to case, without any new Strings * being created on the heap. @@ -844,4 +848,4 @@ public static Set commaSeparatedStringToSet(String commaSeparatedString) .filter(s -> !s.isEmpty()) .collect(Collectors.toSet()); } -} \ No newline at end of file +} diff --git a/src/main/java/com/cedarsoftware/util/cache/LockingLRUCacheStrategy.java b/src/main/java/com/cedarsoftware/util/cache/LockingLRUCacheStrategy.java index eb0aa74c..a94aa7ec 100644 --- a/src/main/java/com/cedarsoftware/util/cache/LockingLRUCacheStrategy.java +++ b/src/main/java/com/cedarsoftware/util/cache/LockingLRUCacheStrategy.java @@ -178,8 +178,8 @@ public V put(K key, V value) { cache.put(key, newNode); addToHead(newNode); if (cache.size() > capacity) { - Node tail = removeTail(); - cache.remove(tail.key); + Node tailToRemove = removeTail(); + cache.remove(tailToRemove.key); } return null; } @@ -442,4 +442,4 @@ public int hashCode() { lock.unlock(); } } -} \ No newline at end of file +}