Skip to content

Commit

Permalink
* Performance improvement: DeepEquals.deepHashCode() - now using `…
Browse files Browse the repository at this point in the history
…IdentityHashMap()` for cycle (visited) detection.

  * Modernization: `UniqueIdGenerator` - updated to use `Lock.lock()` and `Lock.unlock()` instead of `synchronized` keyword.
  * Using json-io 4.14.1 for cloning object in "test" scope, eliminates cycle depedencies when building both json-io and java-util.
  • Loading branch information
jdereg committed Mar 31, 2024
1 parent 61242dd commit ec270f9
Show file tree
Hide file tree
Showing 12 changed files with 160 additions and 130 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ The classes in the`.jar`file are version 52 (`JDK 1.8`).
To include in your project:
##### GradleF
```
implementation 'com.cedarsoftware:java-util:2.4.7'
implementation 'com.cedarsoftware:java-util:2.4.8'
```

##### Maven
```
<dependency>
<groupId>com.cedarsoftware</groupId>
<artifactId>java-util</artifactId>
<version>2.4.7</version>
<version>2.4.8</version>
</dependency>
```
---
Expand Down
6 changes: 4 additions & 2 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
### Revision History
* 2.4.8-SNAPSHOT
* Using json-io 4.14.2 for cloning object in "test" scope, eliminates cycle depedencies
* 2.4.8
* Performance improvement: `DeepEquals.deepHashCode()` - now using `IdentityHashMap()` for cycle (visited) detection.
* Modernization: `UniqueIdGenerator` - updated to use `Lock.lock()` and `Lock.unlock()` instead of `synchronized` keyword.
* Using json-io 4.14.1 for cloning object in "test" scope, eliminates cycle depedencies when building both json-io and java-util.
* 2.4.7
* All 687 conversions supported are now 100% cross-product tested. Converter test suite is complete.
* 2.4.6
Expand Down
4 changes: 2 additions & 2 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-SNAPSHOT</version>
<version>2.4.8</version>
<description>Java Utilities</description>
<url>https://github.com/jdereg/java-util</url>

Expand Down Expand Up @@ -33,7 +33,7 @@
<version.junit-jupiter-api>5.10.2</version.junit-jupiter-api>
<version.junit-jupiter-params>5.10.2</version.junit-jupiter-params>
<version.assertj-core>3.25.3</version.assertj-core>
<version.json-io>4.14.2</version.json-io>
<version.json-io>4.14.1</version.json-io>
<version.mockito-junit-jupiter>4.11.0</version.mockito-junit-jupiter>
<version.agrona>1.21.1</version.agrona>

Expand Down
12 changes: 5 additions & 7 deletions src/main/java/com/cedarsoftware/util/CaseInsensitiveMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -623,12 +623,12 @@ public boolean equals(Object other)
{
return true;
}
else if (other instanceof CaseInsensitiveString)
if (other instanceof CaseInsensitiveString)
{
return hash == ((CaseInsensitiveString)other).hash &&
original.equalsIgnoreCase(((CaseInsensitiveString)other).original);
}
else if (other instanceof String)
if (other instanceof String)
{
return original.equalsIgnoreCase((String)other);
}
Expand All @@ -642,15 +642,13 @@ public int compareTo(Object o)
CaseInsensitiveString other = (CaseInsensitiveString) o;
return original.compareToIgnoreCase(other.original);
}
else if (o instanceof String)
if (o instanceof String)
{
String other = (String)o;
return original.compareToIgnoreCase(other);
}
else
{ // Strings are less than non-Strings (come before)
return -1;
}
// Strings are less than non-Strings (come before)
return -1;
}
}
}
4 changes: 2 additions & 2 deletions src/main/java/com/cedarsoftware/util/CaseInsensitiveSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ public CaseInsensitiveSet(Collection<? extends E> collection)
{
if (collection instanceof ConcurrentSkipListSet)
{
map = new CaseInsensitiveMap<>(new ConcurrentSkipListMap());
map = new CaseInsensitiveMap<>(new ConcurrentSkipListMap<>());
}
else if (collection instanceof SortedSet)
{
map = new CaseInsensitiveMap<>(new TreeMap());
map = new CaseInsensitiveMap<>(new TreeMap<>());
}
else
{
Expand Down
9 changes: 5 additions & 4 deletions src/main/java/com/cedarsoftware/util/DeepEquals.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.util.Deque;
import java.util.HashMap;
import java.util.HashSet;
import java.util.IdentityHashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
Expand Down Expand Up @@ -736,22 +737,22 @@ public static boolean hasCustomEquals(Class<?> c)
* @return the 'deep' hashCode value for the passed in object.
*/
public static int deepHashCode(Object obj) {
Set<Object> visited = new HashSet<>();
Map<Object, Object> visited = new IdentityHashMap<>();
return deepHashCode(obj, visited);
}

private static int deepHashCode(Object obj, Set<Object> visited) {
private static int deepHashCode(Object obj, Map<Object, Object> visited) {
LinkedList<Object> stack = new LinkedList<>();
stack.addFirst(obj);
int hash = 0;

while (!stack.isEmpty()) {
obj = stack.removeFirst();
if (obj == null || visited.contains(obj)) {
if (obj == null || visited.containsKey(obj)) {
continue;
}

visited.add(obj);
visited.put(obj, null);

// Ensure array order matters to hash
if (obj.getClass().isArray()) {
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/cedarsoftware/util/MapUtilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* Usefule utilities for Maps
*
* @author Kenneth Partlow
* @author John DeRegnaucourt
* <br>
* Copyright (c) Cedar Software LLC
* <br><br>
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/cedarsoftware/util/StringUtilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public static boolean equalsIgnoreCase(CharSequence cs1, CharSequence cs2) {
}

/**
* @see StringUtilities@equalsIgnoreCase(CharSequence, CharSequence)
* @see StringUtilities#equalsIgnoreCase(CharSequence, CharSequence)
*/
public static boolean equalsIgnoreCase(String s1, String s2) {
return equalsIgnoreCase((CharSequence) s1, (CharSequence) s2);
Expand Down
Loading

0 comments on commit ec270f9

Please sign in to comment.