This is an automated email from the ASF dual-hosted git repository. tv pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-jcs.git
commit f8890646688188c9b03b70ad070228e6079d71b5 Author: Thomas Vandahl <t...@apache.org> AuthorDate: Mon Feb 15 22:23:20 2021 +0100 Fix toString() and hashCode() --- .../jcs3/engine/control/group/GroupAttrName.java | 29 +++------------------- .../commons/jcs3/engine/control/group/GroupId.java | 21 ++++++---------- 2 files changed, 11 insertions(+), 39 deletions(-) diff --git a/commons-jcs-core/src/main/java/org/apache/commons/jcs3/engine/control/group/GroupAttrName.java b/commons-jcs-core/src/main/java/org/apache/commons/jcs3/engine/control/group/GroupAttrName.java index fc194fe..dd150a2 100644 --- a/commons-jcs-core/src/main/java/org/apache/commons/jcs3/engine/control/group/GroupAttrName.java +++ b/commons-jcs-core/src/main/java/org/apache/commons/jcs3/engine/control/group/GroupAttrName.java @@ -20,6 +20,7 @@ package org.apache.commons.jcs3.engine.control.group; */ import java.io.Serializable; +import java.util.Objects; /** * Description of the Class @@ -36,9 +37,6 @@ public class GroupAttrName<T> /** the name of the attribute */ public final T attrName; - /** Cached toString value */ - private String toString; - /** * Constructor for the GroupAttrName object * @param groupId @@ -71,16 +69,7 @@ public class GroupAttrName<T> if (groupId.equals( to.groupId )) { - if (attrName == null && to.attrName == null) - { - return true; - } - else if (attrName == null || to.attrName == null) - { - return false; - } - - return attrName.equals( to.attrName ); + return Objects.equals(attrName, to.attrName); } return false; @@ -92,12 +81,7 @@ public class GroupAttrName<T> @Override public int hashCode() { - if (attrName == null) - { - return groupId.hashCode(); - } - - return groupId.hashCode() ^ attrName.hashCode(); + return Objects.hash(groupId, attrName); } /** @@ -106,12 +90,7 @@ public class GroupAttrName<T> @Override public String toString() { - if ( toString == null ) - { - toString = "[GAN: groupId=" + groupId + ", attrName=" + attrName + "]"; - } - - return toString; + return String.format("GAN:%s:%s", groupId, Objects.toString(attrName, "")); } } diff --git a/commons-jcs-core/src/main/java/org/apache/commons/jcs3/engine/control/group/GroupId.java b/commons-jcs-core/src/main/java/org/apache/commons/jcs3/engine/control/group/GroupId.java index 86f90b6..fa4bee4 100644 --- a/commons-jcs-core/src/main/java/org/apache/commons/jcs3/engine/control/group/GroupId.java +++ b/commons-jcs-core/src/main/java/org/apache/commons/jcs3/engine/control/group/GroupId.java @@ -20,6 +20,7 @@ package org.apache.commons.jcs3.engine.control.group; */ import java.io.Serializable; +import java.util.Objects; /** * Used to avoid name conflict when group cache items are mixed with non-group cache items in the @@ -37,9 +38,6 @@ public class GroupId /** the name of the region. */ public final String cacheName; - /** Cached toString value. */ - private String toString; - /** * Constructor for the GroupId object * <p> @@ -77,27 +75,22 @@ public class GroupId } /** - * @return cacheName.hashCode() + groupName.hashCode(); + * @return Objects.hash(cacheName, groupName); */ @Override public int hashCode() { - return cacheName.hashCode() + groupName.hashCode(); + return Objects.hash(cacheName, groupName); } /** - * Caches the value. - * <p> - * @return debugging string. + * Convert to string + * + * @return the string representation of this ID. */ @Override public String toString() { - if ( toString == null ) - { - toString = "[groupId=" + cacheName + ", " + groupName + ']'; - } - - return toString; + return String.format("[groupId=%s, %s]", cacheName, groupName); } }