You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2024/12/31 14:22:22 UTC
(commons-compress) branch master updated: Add org.apache.commons.compress.compressors.gzip.ExtraField hashCode() and equals()
This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-compress.git
The following commit(s) were added to refs/heads/master by this push:
new da0afe1f7 Add org.apache.commons.compress.compressors.gzip.ExtraField hashCode() and equals()
da0afe1f7 is described below
commit da0afe1f7aec79f8cc81197010579bcf8491f533
Author: Gary D. Gregory <ga...@gmail.com>
AuthorDate: Tue Dec 31 09:22:14 2024 -0500
Add org.apache.commons.compress.compressors.gzip.ExtraField
hashCode() and equals()
---
.../compress/compressors/gzip/ExtraField.java | 20 ++++++++
.../compress/compressors/gzip/ExtraFieldTest.java | 57 ++++++++++++++++++++++
.../compress/compressors/gzip/SubFieldTest.java | 1 +
3 files changed, 78 insertions(+)
diff --git a/src/main/java/org/apache/commons/compress/compressors/gzip/ExtraField.java b/src/main/java/org/apache/commons/compress/compressors/gzip/ExtraField.java
index 9b4e694af..ae53a5999 100644
--- a/src/main/java/org/apache/commons/compress/compressors/gzip/ExtraField.java
+++ b/src/main/java/org/apache/commons/compress/compressors/gzip/ExtraField.java
@@ -215,6 +215,21 @@ public final class ExtraField implements Iterable<SubField> {
totalSize = 0;
}
+ @Override
+ public boolean equals(final Object obj) {
+ if (this == obj) {
+ return true;
+ }
+ if (obj == null) {
+ return false;
+ }
+ if (getClass() != obj.getClass()) {
+ return false;
+ }
+ final ExtraField other = (ExtraField) obj;
+ return Objects.equals(subFields, other.subFields) && totalSize == other.totalSize;
+ }
+
/**
* Finds the first subfield that matched the id if found, null otherwise.
*
@@ -246,6 +261,11 @@ public final class ExtraField implements Iterable<SubField> {
return subFields.get(index);
}
+ @Override
+ public int hashCode() {
+ return Objects.hash(subFields, totalSize);
+ }
+
/**
* Tests is this extra field has no subfields.
*
diff --git a/src/test/java/org/apache/commons/compress/compressors/gzip/ExtraFieldTest.java b/src/test/java/org/apache/commons/compress/compressors/gzip/ExtraFieldTest.java
new file mode 100644
index 000000000..bcfd5e510
--- /dev/null
+++ b/src/test/java/org/apache/commons/compress/compressors/gzip/ExtraFieldTest.java
@@ -0,0 +1,57 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.commons.compress.compressors.gzip;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotEquals;
+
+import java.io.IOException;
+import java.nio.charset.StandardCharsets;
+
+import org.junit.jupiter.api.Test;
+
+/**
+ * Tests {@link ExtraField}.
+ */
+public class ExtraFieldTest {
+
+ @Test
+ public void testEquals() throws IOException {
+ assertEquals(new ExtraField(), new ExtraField());
+ assertEquals(new ExtraField().addSubField("BB", "CCCC".getBytes(StandardCharsets.ISO_8859_1)),
+ new ExtraField().addSubField("BB", "CCCC".getBytes(StandardCharsets.ISO_8859_1)));
+ // not equals
+ assertNotEquals(new ExtraField().addSubField("XX", "CCCC".getBytes(StandardCharsets.ISO_8859_1)),
+ new ExtraField().addSubField("BB", "CCCC".getBytes(StandardCharsets.ISO_8859_1)));
+ assertNotEquals(new ExtraField().addSubField("XX", "AAAA".getBytes(StandardCharsets.ISO_8859_1)),
+ new ExtraField().addSubField("XX", "CCCC".getBytes(StandardCharsets.ISO_8859_1)));
+ }
+
+ @Test
+ public void testHashCode() throws IOException {
+ assertEquals(new ExtraField().hashCode(), new ExtraField().hashCode());
+ assertEquals(new ExtraField().addSubField("BB", "CCCC".getBytes(StandardCharsets.ISO_8859_1)).hashCode(),
+ new ExtraField().addSubField("BB", "CCCC".getBytes(StandardCharsets.ISO_8859_1)).hashCode());
+ // not equals
+ assertNotEquals(new ExtraField().addSubField("XX", "CCCC".getBytes(StandardCharsets.ISO_8859_1)).hashCode(),
+ new ExtraField().addSubField("BB", "CCCC".getBytes(StandardCharsets.ISO_8859_1)).hashCode());
+ assertNotEquals(new ExtraField().addSubField("XX", "AAAA".getBytes(StandardCharsets.ISO_8859_1)).hashCode(),
+ new ExtraField().addSubField("XX", "CCCC".getBytes(StandardCharsets.ISO_8859_1)).hashCode());
+ }
+}
diff --git a/src/test/java/org/apache/commons/compress/compressors/gzip/SubFieldTest.java b/src/test/java/org/apache/commons/compress/compressors/gzip/SubFieldTest.java
index e5cb21582..d0659a400 100644
--- a/src/test/java/org/apache/commons/compress/compressors/gzip/SubFieldTest.java
+++ b/src/test/java/org/apache/commons/compress/compressors/gzip/SubFieldTest.java
@@ -16,6 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
+
package org.apache.commons.compress.compressors.gzip;
import static org.junit.jupiter.api.Assertions.assertEquals;