From d8417f2ae3acac4fc77711fc9cb3ceb18a8ca660 Mon Sep 17 00:00:00 2001 From: John DeRegnaucourt Date: Sat, 6 Apr 2024 00:21:07 -0400 Subject: [PATCH] - Updated to allow the project to be compiled by versions of JDK > 1.8 yet still generate class file format 52 .class files so that they can be executed on JDK 1.8+ and up. - Incorporated AxataDarji GraphComparator changes that reduce cyclomatic code complexity (refactored to smaller methods) --- pom.xml | 10 +- .../com/cedarsoftware/util/Converter.java | 2 +- .../cedarsoftware/util/GraphComparator.java | 114 ++++++++---------- .../cedarsoftware/util/convert/Converter.java | 4 +- 4 files changed, 59 insertions(+), 71 deletions(-) diff --git a/pom.xml b/pom.xml index fbaaa3e2..65108e23 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ com.cedarsoftware java-util jar - 2.4.8 + 2.4.9 Java Utilities https://github.com/jdereg/java-util @@ -27,7 +27,7 @@ 1.8 1.8 - + 8 5.10.2 @@ -46,7 +46,7 @@ 3.3.0 1.26.4 5.1.9 - 1.0.0.Final + 1.2.1.Final 1.6.13 @@ -173,7 +173,7 @@ ${maven.compiler.source} ${maven.compiler.target} - + ${maven.compiler.release} @@ -230,7 +230,7 @@ org.moditect moditect-maven-plugin - ${version.moditect} + ${version.moditect-maven-plugin} add-module-infos diff --git a/src/main/java/com/cedarsoftware/util/Converter.java b/src/main/java/com/cedarsoftware/util/Converter.java index ac718ec1..cc06ac6b 100644 --- a/src/main/java/com/cedarsoftware/util/Converter.java +++ b/src/main/java/com/cedarsoftware/util/Converter.java @@ -95,7 +95,7 @@ public static Map, Set>> allSupportedConversions() { } /** - * @return Map> which contains all supported conversions. The key of the Map is a source class + * @return {@code Map>} which contains all supported conversions. The key of the Map is a source class * name, and the Set contains all the target class names that the source can be converted to. */ public static Map> getSupportedConversions() { diff --git a/src/main/java/com/cedarsoftware/util/GraphComparator.java b/src/main/java/com/cedarsoftware/util/GraphComparator.java index 38bf0ed4..13df4165 100644 --- a/src/main/java/com/cedarsoftware/util/GraphComparator.java +++ b/src/main/java/com/cedarsoftware/util/GraphComparator.java @@ -563,96 +563,84 @@ private static boolean isIdObject(Object o, ID idFetcher) } } + /** * Deeply compare two Arrays []. Both arrays must be of the same type, same length, and all * elements within the arrays must be deeply equal in order to return true. The appropriate * 'resize' or 'setElement' commands will be generated. + * + * Cyclomatic code complexity reduction by: AxataDarji */ - private static void compareArrays(Delta delta, Collection deltas, LinkedList stack, ID idFetcher) - { + private static void compareArrays(Delta delta, Collection deltas, LinkedList stack, ID idFetcher) { int srcLen = Array.getLength(delta.srcValue); int targetLen = Array.getLength(delta.targetValue); - if (srcLen != targetLen) - { - delta.setCmd(ARRAY_RESIZE); - delta.setOptionalKey(targetLen); - deltas.add(delta); + if (srcLen != targetLen) { + handleArrayResize(delta, deltas, targetLen); } final String sysId = "(" + System.identityHashCode(delta.srcValue) + ')'; final Class compType = delta.targetValue.getClass().getComponentType(); - if (isLogicalPrimitive(compType)) - { - for (int i=0; i < targetLen; i++) - { - final Object targetValue = Array.get(delta.targetValue, i); - String srcPtr = sysId + '[' + i + ']'; + if (isLogicalPrimitive(compType)) { + processPrimitiveArray(delta, deltas, sysId, srcLen, targetLen); + } else { + processNonPrimitiveArray(delta, deltas, stack, idFetcher, sysId, srcLen, targetLen); + } + } - if (i < srcLen) - { // Do positional check - final Object srcValue = Array.get(delta.srcValue, i); + private static void handleArrayResize(Delta delta, Collection deltas, int targetLen) { + delta.setCmd(ARRAY_RESIZE); + delta.setOptionalKey(targetLen); + deltas.add(delta); + } - if (srcValue == null && targetValue != null || - srcValue != null && targetValue == null || - !srcValue.equals(targetValue)) - { - copyArrayElement(delta, deltas, srcPtr, srcValue, targetValue, i); - } - } - else - { // Target array is larger, issue set-element-commands for each additional element - copyArrayElement(delta, deltas, srcPtr, null, targetValue, i); + private static void processPrimitiveArray(Delta delta, Collection deltas, String sysId, int srcLen, int targetLen) { + for (int i = 0; i < targetLen; i++) { + final Object targetValue = Array.get(delta.targetValue, i); + String srcPtr = sysId + '[' + i + ']'; + + if (i < srcLen) { + final Object srcValue = Array.get(delta.srcValue, i); + if (srcValue == null && targetValue != null || + srcValue != null && targetValue == null || + !srcValue.equals(targetValue)) { + copyArrayElement(delta, deltas, srcPtr, srcValue, targetValue, i); } + } else { + copyArrayElement(delta, deltas, srcPtr, null, targetValue, i); } } - else - { // Only map IDs in array when the array type is non-primitive - for (int i = targetLen - 1; i >= 0; i--) - { - final Object targetValue = Array.get(delta.targetValue, i); - String srcPtr = sysId + '[' + i + ']'; + } - if (i < srcLen) - { // Do positional check - final Object srcValue = Array.get(delta.srcValue, i); + private static void processNonPrimitiveArray(Delta delta, Collection deltas, LinkedList stack, ID idFetcher, String sysId, int srcLen, int targetLen) { + for (int i = targetLen - 1; i >= 0; i--) { + final Object targetValue = Array.get(delta.targetValue, i); + String srcPtr = sysId + '[' + i + ']'; - if (targetValue == null || srcValue == null) - { - if (srcValue != targetValue) - { // element was nulled out, create a command to copy it (no need to recurse [add to stack] because null has no depth) - copyArrayElement(delta, deltas, srcPtr, srcValue, targetValue, i); - } - } - else if (isIdObject(srcValue, idFetcher) && isIdObject(targetValue, idFetcher)) - { - Object srcId = idFetcher.getId(srcValue); - Object targetId = idFetcher.getId(targetValue); - - if (targetId.equals(srcId)) - { // No need to copy, same object in same array position, but it's fields could have changed, so add the object to - // the stack for further graph delta comparison. - stack.push(new Delta(delta.id, delta.fieldName, srcPtr, srcValue, targetValue, i)); - } - else - { // IDs do not match? issue a set-element-command - copyArrayElement(delta, deltas, srcPtr, srcValue, targetValue, i); - } + if (i < srcLen) { + final Object srcValue = Array.get(delta.srcValue, i); + if (targetValue == null || srcValue == null) { + if (srcValue != targetValue) { + copyArrayElement(delta, deltas, srcPtr, srcValue, targetValue, i); } - else if (!DeepEquals.deepEquals(srcValue, targetValue)) - { + } else if (isIdObject(srcValue, idFetcher) && isIdObject(targetValue, idFetcher)) { + Object srcId = idFetcher.getId(srcValue); + Object targetId = idFetcher.getId(targetValue); + if (targetId.equals(srcId)) { + stack.push(new Delta(delta.id, delta.fieldName, srcPtr, srcValue, targetValue, i)); + } else { copyArrayElement(delta, deltas, srcPtr, srcValue, targetValue, i); } + } else if (!DeepEquals.deepEquals(srcValue, targetValue)) { + copyArrayElement(delta, deltas, srcPtr, srcValue, targetValue, i); } - else - { // Target is larger than source - elements have been added, issue a set-element-command for each new position one at the end - copyArrayElement(delta, deltas, srcPtr, null, targetValue, i); - } + } else { + copyArrayElement(delta, deltas, srcPtr, null, targetValue, i); } } } - + private static void copyArrayElement(Delta delta, Collection deltas, String srcPtr, Object srcValue, Object targetValue, int index) { Delta copyDelta = new Delta(delta.id, delta.fieldName, srcPtr, srcValue, targetValue, index); diff --git a/src/main/java/com/cedarsoftware/util/convert/Converter.java b/src/main/java/com/cedarsoftware/util/convert/Converter.java index 1979914b..41a1f782 100644 --- a/src/main/java/com/cedarsoftware/util/convert/Converter.java +++ b/src/main/java/com/cedarsoftware/util/convert/Converter.java @@ -1088,7 +1088,7 @@ public boolean isConversionSupportedFor(Class source, Class target) { } /** - * @return Map> which contains all supported conversions. The key of the Map is a source class, + * @return {@code Map>} which contains all supported conversions. The key of the Map is a source class, * and the Set contains all the target types (classes) that the source can be converted to. */ public Map, Set>> allSupportedConversions() { @@ -1104,7 +1104,7 @@ public Map, Set>> allSupportedConversions() { } /** - * @return Map> which contains all supported conversions. The key of the Map is a source class + * @return {@code Map>} which contains all supported conversions. The key of the Map is a source class * name, and the Set contains all the target class names that the source can be converted to. */ public Map> getSupportedConversions() {