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 f6ebabf6e Refactor LZ77Compressor block classes to reduce duplication
f6ebabf6e is described below
commit f6ebabf6e75cd4ddc5f10453ec50d5fd19f14595
Author: Gary D. Gregory <[email protected]>
AuthorDate: Sun Jan 5 07:51:07 2025 -0500
Refactor LZ77Compressor block classes to reduce duplication
---
src/changes/changes.xml | 1 +
.../compressors/lz77support/LZ77Compressor.java | 124 ++++++++++++---------
.../lz77support/LZ77CompressorBlockTest.java | 60 ++++++++++
3 files changed, 134 insertions(+), 51 deletions(-)
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 9e8b25d03..ee978eec6 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -65,6 +65,7 @@ The <action> type attribute can be add,update,fix,remove.
<action type="fix" dev="ggregory" due-to="Gary Gregory">Fix Javadoc and
names in the org.apache.commons.compress.archivers.sevenz package to specify
kibibyte scale in memory limits.</action>
<action type="fix" dev="ggregory" due-to="Gary Gregory">Fix Javadoc and
names in the org.apache.commons.compress.compressors.lzw package to specify
kibibyte scale in memory limits.</action>
<action type="fix" dev="ggregory" due-to="Gary Gregory">Fix Javadoc and
names in the org.apache.commons.compress.compressors.z package to specify
kibibyte scale in memory limits.</action>
+ <action type="fix" dev="ggregory" due-to="Gary Gregory">Refactor
LZ77Compressor block classes to reduce duplication.</action>
<!-- ADD -->
<action type="add" dev="ggregory" due-to="Gary Gregory">Add
GzipParameters.getModificationInstant().</action>
<action type="add" dev="ggregory" due-to="Gary Gregory">Add
GzipParameters.setModificationInstant(Instant).</action>
diff --git
a/src/main/java/org/apache/commons/compress/compressors/lz77support/LZ77Compressor.java
b/src/main/java/org/apache/commons/compress/compressors/lz77support/LZ77Compressor.java
index d17a9fed1..3e7e8eacd 100644
---
a/src/main/java/org/apache/commons/compress/compressors/lz77support/LZ77Compressor.java
+++
b/src/main/java/org/apache/commons/compress/compressors/lz77support/LZ77Compressor.java
@@ -81,7 +81,7 @@ public class LZ77Compressor {
/**
* Represents a back-reference.
*/
- public static final class BackReference extends Block {
+ public abstract static class AbstractReference extends Block {
private final int offset;
private final int length;
@@ -89,16 +89,18 @@ public class LZ77Compressor {
/**
* Constructs a new instance.
*
- * @param offset the offset of the back-reference.
- * @param length the offset of the back-reference.
+ * @param blockType The block type.
+ * @param offset the offset of the reference.
+ * @param length the offset of the reference.
*/
- public BackReference(final int offset, final int length) {
+ public AbstractReference(final BlockType blockType, final int offset,
final int length) {
+ super(blockType);
this.offset = offset;
this.length = length;
}
/**
- * Gets the offset of the back-reference.
+ * Gets the offset of the reference.
*
* @return the length
*/
@@ -107,7 +109,7 @@ public class LZ77Compressor {
}
/**
- * Gets the offset of the back-reference.
+ * Gets the offset of the reference.
*
* @return the offset
*/
@@ -116,14 +118,26 @@ public class LZ77Compressor {
}
@Override
- public BlockType getType() {
- return BlockType.BACK_REFERENCE;
+ public String toString() {
+ return super.toString() + " with offset " + offset + " and length
" + length;
}
+ }
- @Override
- public String toString() {
- return "BackReference with offset " + offset + " and length " +
length;
+ /**
+ * Represents a back-reference.
+ */
+ public static final class BackReference extends AbstractReference {
+
+ /**
+ * Constructs a new instance.
+ *
+ * @param offset the offset of the back-reference.
+ * @param length the offset of the back-reference.
+ */
+ public BackReference(final int offset, final int length) {
+ super(BlockType.BACK_REFERENCE, offset, length);
}
+
}
/**
@@ -136,7 +150,9 @@ public class LZ77Compressor {
*/
public abstract static class Block {
- /** Enumeration of the block types the compressor may emit. */
+ /**
+ * Enumerates the block types the compressor emits.
+ */
public enum BlockType {
/**
@@ -155,12 +171,40 @@ public class LZ77Compressor {
EOD
}
+ private final BlockType type;
+
+ /**
+ * Constructs a new typeless instance.
+ *
+ * @deprecated Use {@link #Block()}.
+ */
+ @Deprecated
+ public Block() {
+ this.type = null;
+ }
+
+ /**
+ * Constructs a new instance.
+ *
+ * @param type
+ */
+ protected Block(final BlockType type) {
+ this.type = Objects.requireNonNull(type);
+ }
+
/**
* Gets the the block type.
*
* @return the the block type.
*/
- public abstract BlockType getType();
+ public BlockType getType() {
+ return type;
+ }
+
+ @Override
+ public String toString() {
+ return getClass().getSimpleName() + " " + getType();
+ }
}
/**
@@ -172,6 +216,7 @@ public class LZ77Compressor {
* </p>
*/
public interface Callback {
+
/**
* Consumes a block.
*
@@ -183,10 +228,19 @@ public class LZ77Compressor {
/** A simple "we are done" marker. */
public static final class EOD extends Block {
- @Override
- public BlockType getType() {
- return BlockType.EOD;
+
+ /**
+ * Singleton instance.
+ */
+ private static final EOD INSTANCE = new EOD();
+
+ /**
+ * Constructs a new instance.
+ */
+ public EOD() {
+ super(BlockType.EOD);
}
+
}
/**
@@ -197,11 +251,9 @@ public class LZ77Compressor {
* immediately as it will get overwritten sooner or later.
* </p>
*/
- public static final class LiteralBlock extends Block {
+ public static final class LiteralBlock extends AbstractReference {
private final byte[] data;
- private final int offset;
- private final int length;
/**
* Constructs a new instance.
@@ -211,9 +263,8 @@ public class LZ77Compressor {
* @param length the length of literal block.
*/
public LiteralBlock(final byte[] data, final int offset, final int
length) {
+ super(BlockType.LITERAL, offset, length);
this.data = data;
- this.offset = offset;
- this.length = length;
}
/**
@@ -229,37 +280,8 @@ public class LZ77Compressor {
return data;
}
- /**
- * Gets the length of literal block.
- *
- * @return the length
- */
- public int getLength() {
- return length;
- }
-
- /**
- * Gets the length of literal block.
- *
- * @return the offset
- */
- public int getOffset() {
- return offset;
- }
-
- @Override
- public BlockType getType() {
- return BlockType.LITERAL;
- }
-
- @Override
- public String toString() {
- return "LiteralBlock starting at " + offset + " with length " +
length;
- }
}
- private static final Block THE_EOD = new EOD();
-
static final int NUMBER_OF_BYTES_IN_HASH = 3;
private static final int NO_MATCH = -1;
@@ -434,7 +456,7 @@ public class LZ77Compressor {
currentPosition += lookahead;
flushLiteralBlock();
}
- callback.accept(THE_EOD);
+ callback.accept(EOD.INSTANCE);
}
private void flushBackReference(final int matchLength) throws IOException {
diff --git
a/src/test/java/org/apache/commons/compress/compressors/lz77support/LZ77CompressorBlockTest.java
b/src/test/java/org/apache/commons/compress/compressors/lz77support/LZ77CompressorBlockTest.java
new file mode 100644
index 000000000..c550a7bb7
--- /dev/null
+++
b/src/test/java/org/apache/commons/compress/compressors/lz77support/LZ77CompressorBlockTest.java
@@ -0,0 +1,60 @@
+/*
+ * 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.lz77support;
+
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import
org.apache.commons.compress.compressors.lz77support.LZ77Compressor.BackReference;
+import
org.apache.commons.compress.compressors.lz77support.LZ77Compressor.Block;
+import
org.apache.commons.compress.compressors.lz77support.LZ77Compressor.Block.BlockType;
+import org.apache.commons.compress.compressors.lz77support.LZ77Compressor.EOD;
+import
org.apache.commons.compress.compressors.lz77support.LZ77Compressor.LiteralBlock;
+import org.junit.jupiter.api.Test;
+
+/**
+ * Tests {@link LZ77Compressor.Block}.
+ */
+public class LZ77CompressorBlockTest {
+
+ @SuppressWarnings("deprecation")
+ static final class DeprecatedBlock extends Block {
+ }
+
+ @Test
+ public void testBackReferenceBlockToString() {
+ assertTrue(new BackReference(1,
2).toString().contains(BlockType.BACK_REFERENCE.name()));
+ }
+
+ @Test
+ public void testDeprecatedBlock() {
+ assertNotNull(new
DeprecatedBlock().toString().contains(DeprecatedBlock.class.getSimpleName()));
+ }
+
+ @Test
+ public void testEodBlockToString() {
+ assertTrue(new EOD().toString().contains(BlockType.EOD.name()));
+ }
+
+ @Test
+ public void testLiteralBlockToString() {
+ assertTrue(new LiteralBlock(new byte[10], 1,
2).toString().contains(BlockType.LITERAL.name()));
+ }
+
+}