Fix toString() and hashCode()
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 @@
*/
import java.io.Serializable;
+import java.util.Objects;
/**
* Description of the Class
@@ -36,9 +37,6 @@
/** 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 @@
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 @@
@Override
public int hashCode()
{
- if (attrName == null)
- {
- return groupId.hashCode();
- }
-
- return groupId.hashCode() ^ attrName.hashCode();
+ return Objects.hash(groupId, attrName);
}
/**
@@ -106,12 +90,7 @@
@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 @@
*/
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 @@
/** 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 @@
}
/**
- * @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);
}
}