From a6c110ebd05155fa5bdae4e2d195493d2d04dd4f Mon Sep 17 00:00:00 2001 From: Todd Lipcon Date: Tue, 1 Nov 2011 05:25:21 +0000 Subject: [PATCH] HADOOP-7785. Add equals, hashcode, toString to DataChecksum. Contributed by Todd Lipcon. git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/trunk@1195832 13f79535-47bb-0310-9956-ffa450edef68 --- .../hadoop-common/CHANGES.txt | 2 ++ .../org/apache/hadoop/util/DataChecksum.java | 32 ++++++++++++++++++- .../apache/hadoop/util/TestDataChecksum.java | 20 ++++++++++++ 3 files changed, 53 insertions(+), 1 deletion(-) diff --git a/hadoop-common-project/hadoop-common/CHANGES.txt b/hadoop-common-project/hadoop-common/CHANGES.txt index 47bbf94f1b..928da6c2c8 100644 --- a/hadoop-common-project/hadoop-common/CHANGES.txt +++ b/hadoop-common-project/hadoop-common/CHANGES.txt @@ -531,6 +531,8 @@ Release 0.23.0 - Unreleased HADOOP-7763. Add top-level navigation to APT docs. (tomwhite) + HADOOP-7785. Add equals, hashcode, toString to DataChecksum (todd) + BUG FIXES HADOOP-7740. Fixed security audit logger configuration. (Arpit Gupta via Eric Yang) diff --git a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/DataChecksum.java b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/DataChecksum.java index b227a65fbe..74e2be639c 100644 --- a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/DataChecksum.java +++ b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/DataChecksum.java @@ -44,6 +44,10 @@ public class DataChecksum implements Checksum { public static final int CHECKSUM_CRC32 = 1; public static final int CHECKSUM_CRC32C = 2; + private static String[] NAMES = new String[] { + "NULL", "CRC32", "CRC32C" + }; + private static final int CHECKSUM_NULL_SIZE = 0; private static final int CHECKSUM_CRC32_SIZE = 4; private static final int CHECKSUM_CRC32C_SIZE = 4; @@ -395,7 +399,33 @@ private void calculateChunkedSums( } } - + @Override + public boolean equals(Object other) { + if (!(other instanceof DataChecksum)) { + return false; + } + DataChecksum o = (DataChecksum)other; + return o.bytesPerChecksum == this.bytesPerChecksum && + o.type == this.type; + } + + @Override + public int hashCode() { + return (this.type + 31) * this.bytesPerChecksum; + } + + @Override + public String toString() { + String strType; + if (type < NAMES.length && type > 0) { + strType = NAMES[type]; + } else { + strType = String.valueOf(type); + } + return "DataChecksum(type=" + strType + + ", chunkSize=" + bytesPerChecksum + ")"; + } + /** * This just provides a dummy implimentation for Checksum class * This is used when there is no checksum available or required for diff --git a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/util/TestDataChecksum.java b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/util/TestDataChecksum.java index 9f0aba173c..e8d1670fad 100644 --- a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/util/TestDataChecksum.java +++ b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/util/TestDataChecksum.java @@ -114,6 +114,26 @@ private void doBulkTest(DataChecksum checksum, int dataLength, assertTrue(ce.getMessage().contains("fake file")); } } + + @Test + public void testEquality() { + assertEquals( + DataChecksum.newDataChecksum(DataChecksum.CHECKSUM_CRC32, 512), + DataChecksum.newDataChecksum(DataChecksum.CHECKSUM_CRC32, 512)); + assertFalse( + DataChecksum.newDataChecksum(DataChecksum.CHECKSUM_CRC32, 512).equals( + DataChecksum.newDataChecksum(DataChecksum.CHECKSUM_CRC32, 1024))); + assertFalse( + DataChecksum.newDataChecksum(DataChecksum.CHECKSUM_CRC32, 512).equals( + DataChecksum.newDataChecksum(DataChecksum.CHECKSUM_CRC32C, 512))); + } + + @Test + public void testToString() { + assertEquals("DataChecksum(type=CRC32, chunkSize=512)", + DataChecksum.newDataChecksum(DataChecksum.CHECKSUM_CRC32, 512) + .toString()); + } private static void corruptBufferOffset(ByteBuffer buf, int offset) { buf.put(offset, (byte)(buf.get(offset) + 1));