cache hashcode for UUIDBased and TbSubscription

This commit is contained in:
Sergey Matvienko 2024-09-17 14:11:07 +02:00
parent 1f518cddbe
commit 6a726a8c56
2 changed files with 16 additions and 5 deletions

View File

@ -27,6 +27,9 @@ import java.util.function.BiConsumer;
@AllArgsConstructor
public abstract class TbSubscription<T> {
/** Cache the hash code */
private transient int hash; // Default to 0. The hash code calculated for this object likely never be zero
private final String serviceId;
private final String sessionId;
private final int subscriptionId;
@ -49,7 +52,10 @@ public abstract class TbSubscription<T> {
@Override
public int hashCode() {
return Objects.hash(sessionId, subscriptionId, tenantId, entityId, type);
if (hash == 0) {
hash = Objects.hash(sessionId, subscriptionId, tenantId, entityId, type);
}
return hash;
}
}

View File

@ -25,6 +25,9 @@ public abstract class UUIDBased implements HasUUID, Serializable {
private static final long serialVersionUID = 1L;
/** Cache the hash code */
private transient int hash; // Default to 0. The hash code calculated for this object likely never be zero
private final UUID id;
public UUIDBased() {
@ -43,10 +46,12 @@ public abstract class UUIDBased implements HasUUID, Serializable {
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
return result;
if (hash == 0) {
final int prime = 31;
int result = 1;
hash = prime * result + ((id == null) ? 0 : id.hashCode());
}
return hash;
}
@Override