Skip to content

Commit

Permalink
Add exception for replace() with oldValue==null
Browse files Browse the repository at this point in the history
  • Loading branch information
lbooker42 committed Jan 24, 2025
1 parent 5f8ffd7 commit daa0b1c
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 7 deletions.
10 changes: 8 additions & 2 deletions src/main/java/io/deephaven/hash/KeyedDoubleObjectHash.java
Original file line number Diff line number Diff line change
Expand Up @@ -251,19 +251,25 @@ public synchronized V replace(double key, V value) {
}

public synchronized boolean replace(Double key, V oldValue, V newValue) {
if (oldValue == null) {
throw new NullPointerException("oldValue is null, but this map cannot hold null values");
}
if (!doubleKeyDef.equalDoubleKey(key, newValue)) {
throw new IllegalArgumentException(
"key and value are inconsistent:" + key + " and " + doubleKeyDef.getDoubleKey(newValue));
}
return internalPut(newValue, KeyedDoubleObjectHash.REPLACE, oldValue) != null;
return internalPut(newValue, KeyedDoubleObjectHash.REPLACE, oldValue).equals(oldValue);
}

public synchronized boolean replace(double key, V oldValue, V newValue) {
if (oldValue == null) {
throw new NullPointerException("oldValue is null, but this map cannot hold null values");
}
if (!doubleKeyDef.equalDoubleKey(key, newValue)) {
throw new IllegalArgumentException(
"key and value are inconsistent:" + key + " and " + doubleKeyDef.getDoubleKey(newValue));
}
return internalPut(newValue, KeyedDoubleObjectHash.REPLACE, oldValue) != null;
return internalPut(newValue, KeyedDoubleObjectHash.REPLACE, oldValue).equals(oldValue);
}

private static final int NORMAL = 0;
Expand Down
10 changes: 8 additions & 2 deletions src/main/java/io/deephaven/hash/KeyedIntObjectHash.java
Original file line number Diff line number Diff line change
Expand Up @@ -249,19 +249,25 @@ public synchronized V replace(int key, V value) {
}

public synchronized boolean replace(Integer key, V oldValue, V newValue) {
if (oldValue == null) {
throw new NullPointerException("oldValue is null, but this map cannot hold null values");
}
if (!intKeyDef.equalIntKey(key, newValue)) {
throw new IllegalArgumentException(
"key and value are inconsistent:" + key + " and " + intKeyDef.getIntKey(newValue));
}
return Objects.equals(internalPut(newValue, KeyedIntObjectHash.REPLACE, oldValue), oldValue);
return internalPut(newValue, KeyedIntObjectHash.REPLACE, oldValue).equals(oldValue);
}

public synchronized boolean replace(int key, V oldValue, V newValue) {
if (oldValue == null) {
throw new NullPointerException("oldValue is null, but this map cannot hold null values");
}
if (!intKeyDef.equalIntKey(key, newValue)) {
throw new IllegalArgumentException(
"key and value are inconsistent:" + key + " and " + intKeyDef.getIntKey(newValue));
}
return Objects.equals(internalPut(newValue, KeyedIntObjectHash.REPLACE, oldValue), oldValue);
return internalPut(newValue, KeyedIntObjectHash.REPLACE, oldValue).equals(oldValue);
}

private static final int NORMAL = 0;
Expand Down
10 changes: 8 additions & 2 deletions src/main/java/io/deephaven/hash/KeyedLongObjectHash.java
Original file line number Diff line number Diff line change
Expand Up @@ -250,19 +250,25 @@ public synchronized V replace(long key, V value) {
}

public synchronized boolean replace(Long key, V oldValue, V newValue) {
if (oldValue == null) {
throw new NullPointerException("oldValue is null, but this map cannot hold null values");
}
if (!longKeyDef.equalLongKey(key, newValue)) {
throw new IllegalArgumentException(
"key and value are inconsistent:" + key + " and " + longKeyDef.getLongKey(newValue));
}
return Objects.equals(internalPut(newValue, KeyedLongObjectHash.REPLACE, oldValue), oldValue);
return internalPut(newValue, KeyedLongObjectHash.REPLACE, oldValue).equals(oldValue);
}

public synchronized boolean replace(long key, V oldValue, V newValue) {
if (oldValue == null) {
throw new NullPointerException("oldValue is null, but this map cannot hold null values");
}
if (!longKeyDef.equalLongKey(key, newValue)) {
throw new IllegalArgumentException(
"key and value are inconsistent:" + key + " and " + longKeyDef.getLongKey(newValue));
}
return Objects.equals(internalPut(newValue, KeyedLongObjectHash.REPLACE, oldValue), oldValue);
return internalPut(newValue, KeyedLongObjectHash.REPLACE, oldValue).equals(oldValue);
}

private static final int NORMAL = 0;
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/io/deephaven/hash/KeyedObjectHash.java
Original file line number Diff line number Diff line change
Expand Up @@ -701,11 +701,14 @@ public synchronized V replace(K key, V value) {
}

public synchronized boolean replace(K key, V oldValue, V newValue) {
if (oldValue == null) {
throw new NullPointerException("oldValue is null, but this map cannot hold null values");
}
if (!keyDef.equalKey(key, newValue)) {
throw new IllegalArgumentException(
"key and value are inconsistent:" + key + " and " + keyDef.getKey(newValue));
}
return Objects.equals(internalPut(newValue, REPLACE, oldValue), oldValue);
return internalPut(newValue, REPLACE, oldValue).equals(oldValue);
}

public synchronized boolean add(V value) {
Expand Down

0 comments on commit daa0b1c

Please sign in to comment.