commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bode...@apache.org
Subject commons-compress git commit: COMPRESS-435 Replaces instanceof checks with a type marker in LZ77 support code.
Date Wed, 27 Dec 2017 14:13:39 GMT
Repository: commons-compress
Updated Branches:
  refs/heads/master 2a2f1dc48 -> 9db502865


COMPRESS-435 Replaces instanceof checks with a type marker in LZ77 support code.

Submitted-By: BELUGA BEHR


Project: http://git-wip-us.apache.org/repos/asf/commons-compress/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-compress/commit/9db50286
Tree: http://git-wip-us.apache.org/repos/asf/commons-compress/tree/9db50286
Diff: http://git-wip-us.apache.org/repos/asf/commons-compress/diff/9db50286

Branch: refs/heads/master
Commit: 9db5028656f9be41ae5809f88ddd4c8ac0b48364
Parents: 2a2f1dc
Author: Stefan Bodewig <bodewig@apache.org>
Authored: Wed Dec 27 15:07:37 2017 +0100
Committer: Stefan Bodewig <bodewig@apache.org>
Committed: Wed Dec 27 15:07:37 2017 +0100

----------------------------------------------------------------------
 src/changes/changes.xml                         |  4 +++
 .../lz4/BlockLZ4CompressorOutputStream.java     | 11 +++---
 .../compressors/lz77support/LZ77Compressor.java | 35 ++++++++++++++------
 .../snappy/SnappyCompressorOutputStream.java    | 10 ++++--
 .../lz77support/LZ77CompressorTest.java         |  2 +-
 5 files changed, 43 insertions(+), 19 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-compress/blob/9db50286/src/changes/changes.xml
----------------------------------------------------------------------
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 9bd0a52..aad1964 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -56,6 +56,10 @@ The <action> type attribute can be add,update,fix,remove.
               due-to="Bruno P. Kinoshita">
         Synchronized iteration over a synchronizedList in ParallelScatterZipCreator.
       </action>
+      <action issue="COMPRESS-435" type="update" date="2017-12-27"
+              due-to="BELUGA BEHR">
+        Replaces instanceof checks with a type marker in LZ77 support code.
+      </action>
     </release>
     <release version="1.15" date="2017-10-17"
              description="Release 1.15

http://git-wip-us.apache.org/repos/asf/commons-compress/blob/9db50286/src/main/java/org/apache/commons/compress/compressors/lz4/BlockLZ4CompressorOutputStream.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/compress/compressors/lz4/BlockLZ4CompressorOutputStream.java
b/src/main/java/org/apache/commons/compress/compressors/lz4/BlockLZ4CompressorOutputStream.java
index 9326023..2cce3a1 100644
--- a/src/main/java/org/apache/commons/compress/compressors/lz4/BlockLZ4CompressorOutputStream.java
+++ b/src/main/java/org/apache/commons/compress/compressors/lz4/BlockLZ4CompressorOutputStream.java
@@ -116,13 +116,16 @@ public class BlockLZ4CompressorOutputStream extends CompressorOutputStream
{
             new LZ77Compressor.Callback() {
                 @Override
                 public void accept(LZ77Compressor.Block block) throws IOException {
-                    //System.err.println(block);
-                    if (block instanceof LZ77Compressor.LiteralBlock) {
+                    switch (block.getType()) {
+                    case LITERAL:
                         addLiteralBlock((LZ77Compressor.LiteralBlock) block);
-                    } else if (block instanceof LZ77Compressor.BackReference) {
+                        break;
+                    case BACK_REFERENCE:
                         addBackReference((LZ77Compressor.BackReference) block);
-                    } else if (block instanceof LZ77Compressor.EOD) {
+                        break;
+                    case EOD:
                         writeFinalLiteralBlock();
+                        break;
                     }
                 }
             });

http://git-wip-us.apache.org/repos/asf/commons-compress/blob/9db50286/src/main/java/org/apache/commons/compress/compressors/lz77support/LZ77Compressor.java
----------------------------------------------------------------------
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 5379259..082336a 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
@@ -84,10 +84,15 @@ import java.util.Arrays;
  */
 public class LZ77Compressor {
 
-    /**
-     * Base class representing things the compressor may emit.
-     */
-    public static abstract class Block { }
+    /** Base class representing blocks the compressor may emit. */
+    public static abstract class Block {
+        /** Enumeration of the block types the compressor may emit. */
+        public static enum BlockType {
+            LITERAL, BACK_REFERENCE, EOD
+        }
+        abstract public BlockType getType();
+    }
+
     /**
      * Represents a literal block of data.
      *
@@ -128,12 +133,16 @@ public class LZ77Compressor {
         public int getLength() {
             return length;
         }
-
+        @Override
+        public BlockType getType() {
+            return BlockType.LITERAL;
+        }
         @Override
         public String toString() {
             return "LiteralBlock starting at " + offset + " with length " + length;
         }
     }
+
     /**
      * Represents a back-reference.
      */
@@ -157,18 +166,22 @@ public class LZ77Compressor {
         public int getLength() {
             return length;
         }
-
+        @Override
+        public BlockType getType() {
+            return BlockType.BACK_REFERENCE;
+        }
         @Override
         public String toString() {
             return "BackReference with offset " + offset + " and length " + length;
         }
     }
-    /**
-     * A simple "we are done" marker.
-     */
-    public static final class EOD extends Block { }
 
-    private static final EOD THE_EOD = new EOD();
+    /** A simple "we are done" marker. */
+    private static final Block THE_EOD = new Block() {
+        @Override
+        public BlockType getType() {
+            return BlockType.EOD;
+        } };
 
     /**
      * Callback invoked while the compressor processes data.

http://git-wip-us.apache.org/repos/asf/commons-compress/blob/9db50286/src/main/java/org/apache/commons/compress/compressors/snappy/SnappyCompressorOutputStream.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/compress/compressors/snappy/SnappyCompressorOutputStream.java
b/src/main/java/org/apache/commons/compress/compressors/snappy/SnappyCompressorOutputStream.java
index 5705bec..93a9d80 100644
--- a/src/main/java/org/apache/commons/compress/compressors/snappy/SnappyCompressorOutputStream.java
+++ b/src/main/java/org/apache/commons/compress/compressors/snappy/SnappyCompressorOutputStream.java
@@ -102,11 +102,15 @@ public class SnappyCompressorOutputStream extends CompressorOutputStream
{
         compressor = new LZ77Compressor(params, new LZ77Compressor.Callback() {
                 @Override
                 public void accept(LZ77Compressor.Block block) throws IOException {
-                    //System.err.println(block);
-                    if (block instanceof LZ77Compressor.LiteralBlock) {
+                    switch (block.getType()) {
+                    case LITERAL:
                         writeLiteralBlock((LZ77Compressor.LiteralBlock) block);
-                    } else if (block instanceof LZ77Compressor.BackReference) {
+                        break;
+                    case BACK_REFERENCE:
                         writeBackReference((LZ77Compressor.BackReference) block);
+                        break;
+                    case EOD:
+                        break;
                     }
                 }
             });

http://git-wip-us.apache.org/repos/asf/commons-compress/blob/9db50286/src/test/java/org/apache/commons/compress/compressors/lz77support/LZ77CompressorTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/compress/compressors/lz77support/LZ77CompressorTest.java
b/src/test/java/org/apache/commons/compress/compressors/lz77support/LZ77CompressorTest.java
index 3163295..db4e3ab 100644
--- a/src/test/java/org/apache/commons/compress/compressors/lz77support/LZ77CompressorTest.java
+++ b/src/test/java/org/apache/commons/compress/compressors/lz77support/LZ77CompressorTest.java
@@ -307,7 +307,7 @@ public class LZ77CompressorTest {
 
     private static final void assertSize(int expectedSize, List<LZ77Compressor.Block>
blocks) {
         assertEquals(expectedSize, blocks.size());
-        assertEquals(LZ77Compressor.EOD.class, blocks.get(expectedSize - 1).getClass());
+        assertEquals(LZ77Compressor.Block.BlockType.EOD, blocks.get(expectedSize - 1).getType());
     }
 
     private static final void assertLiteralBlock(String expectedContent, LZ77Compressor.Block
block)


Mime
View raw message