Skip to content

Commit

Permalink
- Updated to allow the project to be compiled by versions of JDK > 1.…
Browse files Browse the repository at this point in the history
…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)
  • Loading branch information
jdereg committed Apr 6, 2024
1 parent 7495a06 commit d8417f2
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 71 deletions.
10 changes: 5 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<groupId>com.cedarsoftware</groupId>
<artifactId>java-util</artifactId>
<packaging>jar</packaging>
<version>2.4.8</version>
<version>2.4.9</version>
<description>Java Utilities</description>
<url>https://github.com/jdereg/java-util</url>

Expand All @@ -27,7 +27,7 @@
<!-- Java source, target, and release version -->
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<!-- <maven.compiler.release>11</maven.compiler.release>-->
<maven.compiler.release>8</maven.compiler.release>

<!-- testing only -->
<version.junit-jupiter-api>5.10.2</version.junit-jupiter-api>
Expand All @@ -46,7 +46,7 @@
<version.maven-source-plugin>3.3.0</version.maven-source-plugin>
<version.maven-scr-plugin>1.26.4</version.maven-scr-plugin>
<version.maven-bundle-plugin>5.1.9</version.maven-bundle-plugin>
<version.moditect>1.0.0.Final</version.moditect>
<version.moditect-maven-plugin>1.2.1.Final</version.moditect-maven-plugin>

<!-- release plugin -->
<version.nexus-staging-maven-plugin>1.6.13</version.nexus-staging-maven-plugin>
Expand Down Expand Up @@ -173,7 +173,7 @@
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
<!-- <release>${maven.compiler.release}</release>-->
<release>${maven.compiler.release}</release>
</configuration>
</plugin>

Expand Down Expand Up @@ -230,7 +230,7 @@
<plugin>
<groupId>org.moditect</groupId>
<artifactId>moditect-maven-plugin</artifactId>
<version>${version.moditect}</version>
<version>${version.moditect-maven-plugin}</version>
<executions>
<execution>
<id>add-module-infos</id>
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/cedarsoftware/util/Converter.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public static Map<Class<?>, Set<Class<?>>> allSupportedConversions() {
}

/**
* @return Map<String, Set < String>> which contains all supported conversions. The key of the Map is a source class
* @return {@code Map<String, Set<String>>} 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<String, Set<String>> getSupportedConversions() {
Expand Down
114 changes: 51 additions & 63 deletions src/main/java/com/cedarsoftware/util/GraphComparator.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Delta> deltas, LinkedList<Delta> stack, ID idFetcher)
{
private static void compareArrays(Delta delta, Collection<Delta> deltas, LinkedList<Delta> 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<Delta> 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<Delta> 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<Delta> deltas, LinkedList<Delta> 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<Delta> deltas, String srcPtr, Object srcValue, Object targetValue, int index)
{
Delta copyDelta = new Delta(delta.id, delta.fieldName, srcPtr, srcValue, targetValue, index);
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/cedarsoftware/util/convert/Converter.java
Original file line number Diff line number Diff line change
Expand Up @@ -1088,7 +1088,7 @@ public boolean isConversionSupportedFor(Class<?> source, Class<?> target) {
}

/**
* @return Map<Class, Set < Class>> which contains all supported conversions. The key of the Map is a source class,
* @return {@code Map<Class, Set<Class>>} 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<Class<?>, Set<Class<?>>> allSupportedConversions() {
Expand All @@ -1104,7 +1104,7 @@ public Map<Class<?>, Set<Class<?>>> allSupportedConversions() {
}

/**
* @return Map<String, Set < String>> which contains all supported conversions. The key of the Map is a source class
* @return {@code Map<String, Set<String>>} 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<String, Set<String>> getSupportedConversions() {
Expand Down

0 comments on commit d8417f2

Please sign in to comment.