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:32:55 UTC
(commons-compress) branch master updated: Add org.apache.commons.compress.compressors.gzip.GzipParameters 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 108b6ea6a Add org.apache.commons.compress.compressors.gzip.GzipParameters hashCode() and equals()
108b6ea6a is described below
commit 108b6ea6ab63298322a45632306d0ed77de00abf
Author: Gary D. Gregory <ga...@gmail.com>
AuthorDate: Tue Dec 31 09:32:44 2024 -0500
Add org.apache.commons.compress.compressors.gzip.GzipParameters
hashCode() and equals()
---
.../compress/compressors/gzip/GzipParameters.java | 22 ++++++++++++
.../compressors/gzip/GzipParametersTest.java | 42 +++++++++++++++++++---
2 files changed, 59 insertions(+), 5 deletions(-)
diff --git a/src/main/java/org/apache/commons/compress/compressors/gzip/GzipParameters.java b/src/main/java/org/apache/commons/compress/compressors/gzip/GzipParameters.java
index e607a3eb9..2f7316043 100644
--- a/src/main/java/org/apache/commons/compress/compressors/gzip/GzipParameters.java
+++ b/src/main/java/org/apache/commons/compress/compressors/gzip/GzipParameters.java
@@ -22,6 +22,7 @@ package org.apache.commons.compress.compressors.gzip;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.time.Instant;
+import java.util.Objects;
import java.util.zip.Deflater;
import org.apache.commons.io.Charsets;
@@ -300,6 +301,21 @@ public class GzipParameters {
private int deflateStrategy = Deflater.DEFAULT_STRATEGY;
private boolean headerCRC;
+ @Override
+ public boolean equals(final Object obj) {
+ if (this == obj) {
+ return true;
+ }
+ if (!(obj instanceof GzipParameters)) {
+ return false;
+ }
+ final GzipParameters other = (GzipParameters) obj;
+ return bufferSize == other.bufferSize && Objects.equals(comment, other.comment) && compressionLevel == other.compressionLevel
+ && deflateStrategy == other.deflateStrategy && Objects.equals(extraField, other.extraField) && Objects.equals(fileName, other.fileName)
+ && Objects.equals(fileNameCharset, other.fileNameCharset) && headerCRC == other.headerCRC
+ && Objects.equals(modificationInstant, other.modificationInstant) && operatingSystem == other.operatingSystem;
+ }
+
/**
* Gets size of the buffer used to retrieve compressed data.
*
@@ -442,6 +458,12 @@ public class GzipParameters {
return operatingSystem;
}
+ @Override
+ public int hashCode() {
+ return Objects.hash(bufferSize, comment, compressionLevel, deflateStrategy, extraField, fileName, fileNameCharset, headerCRC, modificationInstant,
+ operatingSystem);
+ }
+
private String requireNonNulByte(final String text) {
if (StringUtils.isNotEmpty(text) && ArrayUtils.contains(text.getBytes(fileNameCharset), (byte) 0)) {
throw new IllegalArgumentException("String encoded in Charset '" + fileNameCharset + "' contains the nul byte 0 which is not supported in gzip.");
diff --git a/src/test/java/org/apache/commons/compress/compressors/gzip/GzipParametersTest.java b/src/test/java/org/apache/commons/compress/compressors/gzip/GzipParametersTest.java
index e09fde49a..e8bc10617 100644
--- a/src/test/java/org/apache/commons/compress/compressors/gzip/GzipParametersTest.java
+++ b/src/test/java/org/apache/commons/compress/compressors/gzip/GzipParametersTest.java
@@ -16,15 +16,17 @@
* 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 static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
+import java.io.IOException;
import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
import java.util.zip.Deflater;
import org.junit.jupiter.api.Test;
@@ -44,15 +46,45 @@ public class GzipParametersTest {
assertEquals(Deflater.HUFFMAN_ONLY, gzipParameters.getDeflateStrategy());
}
+ @Test
+ public void testEquals() throws IOException {
+ final GzipParameters gzipParameters1 = new GzipParameters();
+ final GzipParameters gzipParameters2 = new GzipParameters();
+ assertEquals(gzipParameters1, gzipParameters2);
+ gzipParameters1.setExtraField(new ExtraField().addSubField("AA", "XXXX".getBytes(StandardCharsets.ISO_8859_1)));
+ gzipParameters2.setExtraField(new ExtraField().addSubField("AA", "XXXX".getBytes(StandardCharsets.ISO_8859_1)));
+ assertEquals(gzipParameters1, gzipParameters1);
+ assertEquals(gzipParameters1, gzipParameters2);
+ assertEquals(gzipParameters2, gzipParameters1);
+ // not equals
+ gzipParameters2.setExtraField(new ExtraField().addSubField("BB", "XXXX".getBytes(StandardCharsets.ISO_8859_1)));
+ assertNotEquals(gzipParameters1, gzipParameters2);
+ }
+
+ @Test
+ public void testHashCode() throws IOException {
+ final GzipParameters gzipParameters1 = new GzipParameters();
+ final GzipParameters gzipParameters2 = new GzipParameters();
+ assertEquals(gzipParameters1.hashCode(), gzipParameters2.hashCode());
+ gzipParameters1.setExtraField(new ExtraField().addSubField("AA", "XXXX".getBytes(StandardCharsets.ISO_8859_1)));
+ gzipParameters2.setExtraField(new ExtraField().addSubField("AA", "XXXX".getBytes(StandardCharsets.ISO_8859_1)));
+ assertEquals(gzipParameters1.hashCode(), gzipParameters1.hashCode());
+ assertEquals(gzipParameters1.hashCode(), gzipParameters2.hashCode());
+ assertEquals(gzipParameters2.hashCode(), gzipParameters1.hashCode());
+ // not equals
+ gzipParameters2.setExtraField(new ExtraField().addSubField("BB", "XXXX".getBytes(StandardCharsets.ISO_8859_1)));
+ assertNotEquals(gzipParameters1.hashCode(), gzipParameters2.hashCode());
+ }
+
@ParameterizedTest
- //@formatter:off
+ // @formatter:off
@CsvSource({
" , hello\0world, false",
"ISO-8859-1, hello\0world, false",
"UTF-8 , hello\0world, false",
"UTF-16BE , helloworld, false"
})
- //@formatter:on
+ // @formatter:on
public void testIllegalCommentOrFileName(final Charset charset, final String text) {
final GzipParameters gzipParameters = new GzipParameters();
// null resets to default value
@@ -66,7 +98,7 @@ public class GzipParametersTest {
}
@ParameterizedTest
- //@formatter:off
+ // @formatter:off
@CsvSource({
" , helloworld",
" , helloƩworld",
@@ -75,7 +107,7 @@ public class GzipParametersTest {
"UTF-8 , helloworld",
"UTF-8 , helloƩworld"
})
- //@formatter:on
+ // @formatter:on
public void testLegalCommentOrFileName(final Charset charset, final String text) {
final GzipParameters gzipParameters = new GzipParameters();
// null resets to default value