[GitHub] [commons-text] garydgregory commented on a change in pull request #281: [TEXT-211] TextStringBuilder.equals whatever the capacity is

2022-03-31 Thread GitBox


garydgregory commented on a change in pull request #281:
URL: https://github.com/apache/commons-text/pull/281#discussion_r839568158



##
File path: src/main/java/org/apache/commons/text/TextStringBuilder.java
##
@@ -1850,7 +1850,25 @@ public boolean equals(final Object obj) {
  * @return true if the builders contain the same characters in the same 
order
  */
 public boolean equals(final TextStringBuilder other) {
-return other != null && Arrays.equals(buffer, other.buffer);
+if (this == other) {
+return true;
+}
+if (other == null) {
+return false;
+}
+if (this.size != other.size) {
+return false;
+}
+// Be aware not to use Arrays.equals(buffer, other.buffer) for 
equals() method
+// as length of the buffers may be different (TEXT-211)

Review comment:
   They cannot be different by the time you get here though, can they?




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscr...@commons.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org




[GitHub] [commons-text] garydgregory commented on a change in pull request #281: TEXT-211 - TextStringBuilder.equals whatever the capacity is

2022-03-31 Thread GitBox


garydgregory commented on a change in pull request #281:
URL: https://github.com/apache/commons-text/pull/281#discussion_r836796333



##
File path: src/main/java/org/apache/commons/text/TextStringBuilder.java
##
@@ -1850,7 +1850,25 @@ public boolean equals(final Object obj) {
  * @return true if the builders contain the same characters in the same 
order
  */
 public boolean equals(final TextStringBuilder other) {
-return other != null && Arrays.equals(buffer, other.buffer);
+if(this == other) {

Review comment:
   Add a space before the open parenthesis.

##
File path: src/main/java/org/apache/commons/text/TextStringBuilder.java
##
@@ -1850,7 +1850,25 @@ public boolean equals(final Object obj) {
  * @return true if the builders contain the same characters in the same 
order
  */
 public boolean equals(final TextStringBuilder other) {
-return other != null && Arrays.equals(buffer, other.buffer);
+if(this == other) {
+return true;
+}
+if(other == null) {

Review comment:
   Add a space before the open parenthesis.

##
File path: src/test/java/org/apache/commons/text/TextStringBuilderTest.java
##
@@ -2276,4 +2289,4 @@ public void testWrap_CharArray_Int_Exceptions() {
 assertThrows(IllegalArgumentException.class, () -> 
TextStringBuilder.wrap(ArrayUtils.EMPTY_CHAR_ARRAY, 1));
 }
 
-}
+}

Review comment:
   @sebx59 
   MIssing blank line as the last line (see red icon warning from GitHub above).




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscr...@commons.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org




[GitHub] [commons-text] garydgregory commented on a change in pull request #281: TEXT-211 - TextStringBuilder.equals whatever the capacity is

2021-11-03 Thread GitBox


garydgregory commented on a change in pull request #281:
URL: https://github.com/apache/commons-text/pull/281#discussion_r742274854



##
File path: src/main/java/org/apache/commons/text/TextStringBuilder.java
##
@@ -1850,7 +1850,14 @@ public boolean equals(final Object obj) {
  * @return true if the builders contain the same characters in the same 
order
  */
 public boolean equals(final TextStringBuilder other) {
-return other != null && Arrays.equals(buffer, other.buffer);
+   
+   if(other == null) {
+   return false;
+   }
+   if (this.size != other.size) {
+return false;
+}
+return Arrays.equals(ArrayUtils.subarray(buffer, 0, size), 
ArrayUtils.subarray(other.buffer, 0, size));

Review comment:
   I think we can reuse our own toString() here.

##
File path: src/main/java/org/apache/commons/text/TextStringBuilder.java
##
@@ -1947,7 +1954,7 @@ public String getNullText() {
  */
 @Override
 public int hashCode() {
-return Arrays.hashCode(buffer);
+return Arrays.hashCode(ArrayUtils.subarray(buffer, 0, size));

Review comment:
   I think we can reuse our own toString() here.

##
File path: src/test/java/org/apache/commons/text/TextStringBuilderTest.java
##
@@ -1057,15 +1057,24 @@ public void testHashCode() {
 final TextStringBuilder sb = new TextStringBuilder();
 final int hc1a = sb.hashCode();
 final int hc1b = sb.hashCode();
-final int emptyHc = Arrays.hashCode(sb.getBuffer());
-assertEquals(emptyHc, hc1a);

Review comment:
   I'm not a fan of removing passing asserts because it could hide a 
regression.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscr...@commons.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org




[GitHub] [commons-text] garydgregory commented on a change in pull request #281: TEXT-211 - TextStringBuilder.equals whatever the capacity is

2021-11-03 Thread GitBox


garydgregory commented on a change in pull request #281:
URL: https://github.com/apache/commons-text/pull/281#discussion_r742274854



##
File path: src/main/java/org/apache/commons/text/TextStringBuilder.java
##
@@ -1850,7 +1850,14 @@ public boolean equals(final Object obj) {
  * @return true if the builders contain the same characters in the same 
order
  */
 public boolean equals(final TextStringBuilder other) {
-return other != null && Arrays.equals(buffer, other.buffer);
+   
+   if(other == null) {
+   return false;
+   }
+   if (this.size != other.size) {
+return false;
+}
+return Arrays.equals(ArrayUtils.subarray(buffer, 0, size), 
ArrayUtils.subarray(other.buffer, 0, size));

Review comment:
   I think we can reuse our own toString() here.

##
File path: src/main/java/org/apache/commons/text/TextStringBuilder.java
##
@@ -1947,7 +1954,7 @@ public String getNullText() {
  */
 @Override
 public int hashCode() {
-return Arrays.hashCode(buffer);
+return Arrays.hashCode(ArrayUtils.subarray(buffer, 0, size));

Review comment:
   I think we can reuse our own toString() here.

##
File path: src/test/java/org/apache/commons/text/TextStringBuilderTest.java
##
@@ -1057,15 +1057,24 @@ public void testHashCode() {
 final TextStringBuilder sb = new TextStringBuilder();
 final int hc1a = sb.hashCode();
 final int hc1b = sb.hashCode();
-final int emptyHc = Arrays.hashCode(sb.getBuffer());
-assertEquals(emptyHc, hc1a);

Review comment:
   I'm not a fan of removing passing asserts because it could hide a 
regression.

##
File path: src/main/java/org/apache/commons/text/TextStringBuilder.java
##
@@ -1850,7 +1850,14 @@ public boolean equals(final Object obj) {
  * @return true if the builders contain the same characters in the same 
order
  */
 public boolean equals(final TextStringBuilder other) {
-return other != null && Arrays.equals(buffer, other.buffer);
+   
+   if(other == null) {
+   return false;
+   }
+   if (this.size != other.size) {
+return false;
+}
+return Arrays.equals(ArrayUtils.subarray(buffer, 0, size), 
ArrayUtils.subarray(other.buffer, 0, size));

Review comment:
   I think we can reuse our own toString() here.

##
File path: src/main/java/org/apache/commons/text/TextStringBuilder.java
##
@@ -1947,7 +1954,7 @@ public String getNullText() {
  */
 @Override
 public int hashCode() {
-return Arrays.hashCode(buffer);
+return Arrays.hashCode(ArrayUtils.subarray(buffer, 0, size));

Review comment:
   I think we can reuse our own toString() here.

##
File path: src/test/java/org/apache/commons/text/TextStringBuilderTest.java
##
@@ -1057,15 +1057,24 @@ public void testHashCode() {
 final TextStringBuilder sb = new TextStringBuilder();
 final int hc1a = sb.hashCode();
 final int hc1b = sb.hashCode();
-final int emptyHc = Arrays.hashCode(sb.getBuffer());
-assertEquals(emptyHc, hc1a);

Review comment:
   I'm not a fan of removing passing asserts because it could hide a 
regression.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscr...@commons.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org




[GitHub] [commons-text] garydgregory commented on a change in pull request #281: TEXT-211 - TextStringBuilder.equals whatever the capacity is

2021-11-03 Thread GitBox


garydgregory commented on a change in pull request #281:
URL: https://github.com/apache/commons-text/pull/281#discussion_r742274854



##
File path: src/main/java/org/apache/commons/text/TextStringBuilder.java
##
@@ -1850,7 +1850,14 @@ public boolean equals(final Object obj) {
  * @return true if the builders contain the same characters in the same 
order
  */
 public boolean equals(final TextStringBuilder other) {
-return other != null && Arrays.equals(buffer, other.buffer);
+   
+   if(other == null) {
+   return false;
+   }
+   if (this.size != other.size) {
+return false;
+}
+return Arrays.equals(ArrayUtils.subarray(buffer, 0, size), 
ArrayUtils.subarray(other.buffer, 0, size));

Review comment:
   I think we can reuse our own toString() here.

##
File path: src/main/java/org/apache/commons/text/TextStringBuilder.java
##
@@ -1947,7 +1954,7 @@ public String getNullText() {
  */
 @Override
 public int hashCode() {
-return Arrays.hashCode(buffer);
+return Arrays.hashCode(ArrayUtils.subarray(buffer, 0, size));

Review comment:
   I think we can reuse our own toString() here.

##
File path: src/test/java/org/apache/commons/text/TextStringBuilderTest.java
##
@@ -1057,15 +1057,24 @@ public void testHashCode() {
 final TextStringBuilder sb = new TextStringBuilder();
 final int hc1a = sb.hashCode();
 final int hc1b = sb.hashCode();
-final int emptyHc = Arrays.hashCode(sb.getBuffer());
-assertEquals(emptyHc, hc1a);

Review comment:
   I'm not a fan of removing passing asserts because it could hide a 
regression.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscr...@commons.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org