james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rouaz...@apache.org
Subject [02/17] james-project git commit: JAMES-2436 Factorize long extra fields
Date Wed, 27 Jun 2018 14:44:24 GMT
JAMES-2436 Factorize long extra fields


Project: http://git-wip-us.apache.org/repos/asf/james-project/repo
Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/1d3e4a92
Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/1d3e4a92
Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/1d3e4a92

Branch: refs/heads/master
Commit: 1d3e4a92b9b7c0a620d0dd7449e3708ed72c032b
Parents: 10c8033
Author: Raphael Ouazana <raphael.ouazana@linagora.com>
Authored: Wed Jun 20 16:10:15 2018 +0200
Committer: Raphael Ouazana <raphael.ouazana@linagora.com>
Committed: Wed Jun 27 16:36:12 2018 +0200

----------------------------------------------------------------------
 .../james/mailbox/backup/LongExtraField.java    | 105 +++++++++++++++++++
 .../james/mailbox/backup/SizeExtraField.java    |  78 ++------------
 .../james/mailbox/backup/UidExtraField.java     |  78 ++------------
 .../mailbox/backup/SizeExtraFieldTest.java      |  16 +--
 .../james/mailbox/backup/UidExtraFieldTest.java |  16 +--
 5 files changed, 133 insertions(+), 160 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/1d3e4a92/mailbox/backup/src/main/java/org/apache/james/mailbox/backup/LongExtraField.java
----------------------------------------------------------------------
diff --git a/mailbox/backup/src/main/java/org/apache/james/mailbox/backup/LongExtraField.java
b/mailbox/backup/src/main/java/org/apache/james/mailbox/backup/LongExtraField.java
new file mode 100644
index 0000000..6cc5fc5
--- /dev/null
+++ b/mailbox/backup/src/main/java/org/apache/james/mailbox/backup/LongExtraField.java
@@ -0,0 +1,105 @@
+/****************************************************************
+ * 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   *
+ *                                                              *
+ *   http://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.james.mailbox.backup;
+
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+import java.util.Objects;
+import java.util.Optional;
+import java.util.zip.ZipException;
+
+import org.apache.commons.compress.archivers.zip.ZipExtraField;
+import org.apache.commons.compress.archivers.zip.ZipShort;
+
+public abstract class LongExtraField implements ZipExtraField {
+
+    private Optional<Long> value;
+
+    public LongExtraField() {
+        this(Optional.empty());
+    }
+
+    public LongExtraField(long value) {
+        this(Optional.of(value));
+    }
+
+    public LongExtraField(Optional<Long> value) {
+        this.value = value;
+    }
+
+    @Override
+    public ZipShort getLocalFileDataLength() {
+        return new ZipShort(Long.BYTES);
+    }
+
+    @Override
+    public ZipShort getCentralDirectoryLength() {
+        return getLocalFileDataLength();
+    }
+
+    @Override
+    public byte[] getLocalFileDataData() {
+        long value = this.value.orElseThrow(() -> new RuntimeException("Value must by
initialized"));
+        return ByteBuffer.allocate(Long.BYTES)
+            .order(ByteOrder.LITTLE_ENDIAN)
+            .putLong(value)
+            .array();
+    }
+
+    @Override
+    public byte[] getCentralDirectoryData() {
+        return getLocalFileDataData();
+    }
+
+    @Override
+    public void parseFromLocalFileData(byte[] buffer, int offset, int length) throws ZipException
{
+        if (length != Long.BYTES) {
+            throw new ZipException("Unexpected data length for ExtraField. Expected " + Long.BYTES
+ " but got " + length + ".");
+        }
+        value = Optional.of(ByteBuffer
+                .wrap(buffer, offset, Long.BYTES)
+                .order(ByteOrder.LITTLE_ENDIAN)
+                .getLong());
+    }
+
+    @Override
+    public void parseFromCentralDirectoryData(byte[] buffer, int offset, int length) throws
ZipException {
+        parseFromLocalFileData(buffer, offset, length);
+    }
+
+    public Optional<Long> getValue() {
+        return value;
+    }
+
+    @Override
+    public final boolean equals(Object o) {
+        if (o instanceof LongExtraField) {
+            LongExtraField that = (LongExtraField) o;
+
+            return Objects.equals(this.value, that.value);
+        }
+        return false;
+    }
+
+    @Override
+    public final int hashCode() {
+        return Objects.hash(value);
+    }
+}

http://git-wip-us.apache.org/repos/asf/james-project/blob/1d3e4a92/mailbox/backup/src/main/java/org/apache/james/mailbox/backup/SizeExtraField.java
----------------------------------------------------------------------
diff --git a/mailbox/backup/src/main/java/org/apache/james/mailbox/backup/SizeExtraField.java
b/mailbox/backup/src/main/java/org/apache/james/mailbox/backup/SizeExtraField.java
index 4da7f9c..a90af18 100644
--- a/mailbox/backup/src/main/java/org/apache/james/mailbox/backup/SizeExtraField.java
+++ b/mailbox/backup/src/main/java/org/apache/james/mailbox/backup/SizeExtraField.java
@@ -18,93 +18,27 @@
  ****************************************************************/
 package org.apache.james.mailbox.backup;
 
-import java.nio.ByteBuffer;
-import java.nio.ByteOrder;
-import java.util.Objects;
 import java.util.Optional;
-import java.util.zip.ZipException;
 
-import org.apache.commons.compress.archivers.zip.ZipExtraField;
 import org.apache.commons.compress.archivers.zip.ZipShort;
 
-public class SizeExtraField implements ZipExtraField {
+public class SizeExtraField extends LongExtraField {
     public static final ZipShort ID = new ZipShort(0x6A61); // "aj" in little-endian
 
-    private Optional<Long> size;
-
     public SizeExtraField() {
-        this(Optional.empty());
+        super();
     }
 
-    public SizeExtraField(long size) {
-        this(Optional.of(size));
+    public SizeExtraField(long value) {
+        super(value);
     }
 
-    public SizeExtraField(Optional<Long> size) {
-        this.size = size;
+    public SizeExtraField(Optional<Long> value) {
+        super(value);
     }
 
     @Override
     public ZipShort getHeaderId() {
         return ID;
     }
-
-    @Override
-    public ZipShort getLocalFileDataLength() {
-        return new ZipShort(Long.BYTES);
-    }
-
-    @Override
-    public ZipShort getCentralDirectoryLength() {
-        return getLocalFileDataLength();
-    }
-
-    @Override
-    public byte[] getLocalFileDataData() {
-        long value = size.orElseThrow(() -> new RuntimeException("Value must by initialized"));
-        return ByteBuffer.allocate(Long.BYTES)
-            .order(ByteOrder.LITTLE_ENDIAN)
-            .putLong(value)
-            .array();
-    }
-
-    @Override
-    public byte[] getCentralDirectoryData() {
-        return getLocalFileDataData();
-    }
-
-    @Override
-    public void parseFromLocalFileData(byte[] buffer, int offset, int length) throws ZipException
{
-        if (length != Long.BYTES) {
-            throw new ZipException("Unexpected data length for SizeExtraField. Expected "
+ Long.BYTES + " but got " + length + ".");
-        }
-        size = Optional.of(ByteBuffer
-                .wrap(buffer, offset, Long.BYTES)
-                .order(ByteOrder.LITTLE_ENDIAN)
-                .getLong());
-    }
-
-    @Override
-    public void parseFromCentralDirectoryData(byte[] buffer, int offset, int length) throws
ZipException {
-        parseFromLocalFileData(buffer, offset, length);
-    }
-
-    public Optional<Long> getSize() {
-        return size;
-    }
-
-    @Override
-    public final boolean equals(Object o) {
-        if (o instanceof SizeExtraField) {
-            SizeExtraField that = (SizeExtraField) o;
-
-            return Objects.equals(this.size, that.size);
-        }
-        return false;
-    }
-
-    @Override
-    public final int hashCode() {
-        return Objects.hash(size);
-    }
 }

http://git-wip-us.apache.org/repos/asf/james-project/blob/1d3e4a92/mailbox/backup/src/main/java/org/apache/james/mailbox/backup/UidExtraField.java
----------------------------------------------------------------------
diff --git a/mailbox/backup/src/main/java/org/apache/james/mailbox/backup/UidExtraField.java
b/mailbox/backup/src/main/java/org/apache/james/mailbox/backup/UidExtraField.java
index cd1607e..6972ceb 100644
--- a/mailbox/backup/src/main/java/org/apache/james/mailbox/backup/UidExtraField.java
+++ b/mailbox/backup/src/main/java/org/apache/james/mailbox/backup/UidExtraField.java
@@ -19,94 +19,28 @@
 
 package org.apache.james.mailbox.backup;
 
-import java.nio.ByteBuffer;
-import java.nio.ByteOrder;
-import java.util.Objects;
 import java.util.Optional;
-import java.util.zip.ZipException;
 
-import org.apache.commons.compress.archivers.zip.ZipExtraField;
 import org.apache.commons.compress.archivers.zip.ZipShort;
 
-public class UidExtraField implements ZipExtraField {
+public class UidExtraField extends LongExtraField {
 
     public static final ZipShort ID = new ZipShort(0x6B61); // "ak" in little-endian
 
-    private Optional<Long> uid;
-
     public UidExtraField() {
-        this(Optional.empty());
+        super();
     }
 
-    public UidExtraField(long uid) {
-        this(Optional.of(uid));
+    public UidExtraField(long value) {
+        super(value);
     }
 
-    public UidExtraField(Optional<Long> uid) {
-        this.uid = uid;
+    public UidExtraField(Optional<Long> value) {
+        super(value);
     }
 
     @Override
     public ZipShort getHeaderId() {
         return ID;
     }
-
-    @Override
-    public ZipShort getLocalFileDataLength() {
-        return new ZipShort(Long.BYTES);
-    }
-
-    @Override
-    public ZipShort getCentralDirectoryLength() {
-        return getLocalFileDataLength();
-    }
-
-    @Override
-    public byte[] getLocalFileDataData() {
-        long value = uid.orElseThrow(() -> new RuntimeException("Value must by initialized"));
-        return ByteBuffer.allocate(Long.BYTES)
-            .order(ByteOrder.LITTLE_ENDIAN)
-            .putLong(value)
-            .array();
-    }
-
-    @Override
-    public byte[] getCentralDirectoryData() {
-        return getLocalFileDataData();
-    }
-
-    @Override
-    public void parseFromLocalFileData(byte[] buffer, int offset, int length) throws ZipException
{
-        if (length != Long.BYTES) {
-            throw new ZipException("Unexpected data length for UidExtraField. Expected "
+ Long.BYTES + " but got " + length + ".");
-        }
-        uid = Optional.of(ByteBuffer
-                .wrap(buffer, offset, Long.BYTES)
-                .order(ByteOrder.LITTLE_ENDIAN)
-                .getLong());
-    }
-
-    @Override
-    public void parseFromCentralDirectoryData(byte[] buffer, int offset, int length) throws
ZipException {
-        parseFromLocalFileData(buffer, offset, length);
-    }
-
-    public Optional<Long> getUid() {
-        return uid;
-    }
-
-    @Override
-    public final boolean equals(Object o) {
-        if (o instanceof UidExtraField) {
-            UidExtraField that = (UidExtraField) o;
-
-            return Objects.equals(this.uid, that.uid);
-        }
-        return false;
-    }
-
-    @Override
-    public final int hashCode() {
-        return Objects.hash(uid);
-    }
 }

http://git-wip-us.apache.org/repos/asf/james-project/blob/1d3e4a92/mailbox/backup/src/test/java/org/apache/james/mailbox/backup/SizeExtraFieldTest.java
----------------------------------------------------------------------
diff --git a/mailbox/backup/src/test/java/org/apache/james/mailbox/backup/SizeExtraFieldTest.java
b/mailbox/backup/src/test/java/org/apache/james/mailbox/backup/SizeExtraFieldTest.java
index db2b274..d38384a 100644
--- a/mailbox/backup/src/test/java/org/apache/james/mailbox/backup/SizeExtraFieldTest.java
+++ b/mailbox/backup/src/test/java/org/apache/james/mailbox/backup/SizeExtraFieldTest.java
@@ -129,14 +129,14 @@ public class SizeExtraFieldTest {
     @Test
     void parseFromLocalFileDataShouldParseWhenZero() throws Exception {
         testee.parseFromLocalFileData(ZERO_AS_BYTE_ARRAY, 0, 8);
-        assertThat(testee.getSize())
+        assertThat(testee.getValue())
             .contains(0L);
     }
 
     @Test
     void parseFromLocalFileDataShouldParseWhen123456789ABCDEF0InLittleEndian() throws Exception
{
         testee.parseFromLocalFileData(_123456789ABCDEF0_AS_LE_BYTE_ARRAY, 0, 8);
-        assertThat(testee.getSize())
+        assertThat(testee.getValue())
             .contains(0x123456789ABCDEF0L);
     }
 
@@ -144,7 +144,7 @@ public class SizeExtraFieldTest {
     void parseFromLocalFileDataShouldParseWhenFEDCBA9876543210InLittleEndian() throws Exception
{
         byte[] input = FEDCBA9876543210_AS_LE_BYTE_ARRAY;
         testee.parseFromLocalFileData(input, 0, 8);
-        assertThat(testee.getSize())
+        assertThat(testee.getValue())
             .contains(0xFEDCBA9876543210L);
     }
 
@@ -152,7 +152,7 @@ public class SizeExtraFieldTest {
     void parseFromLocalFileDataShouldHandleOffset() throws Exception {
         byte[] input = Arrays.concatenate(UNUSED, _123456789ABCDEF0_AS_LE_BYTE_ARRAY);
         testee.parseFromLocalFileData(input, 2, 8);
-        assertThat(testee.getSize())
+        assertThat(testee.getValue())
             .contains(0x123456789ABCDEF0L);
     }
 
@@ -173,14 +173,14 @@ public class SizeExtraFieldTest {
     @Test
     void parseFromCentralDirectoryDataShouldParseWhenZero() throws Exception {
         testee.parseFromCentralDirectoryData(ZERO_AS_BYTE_ARRAY, 0, 8);
-        assertThat(testee.getSize())
+        assertThat(testee.getValue())
             .contains(0L);
     }
 
     @Test
     void parseFromCentralDirectoryDataShouldParseWhen123456789ABCDEF0InLittleEndian() throws
Exception {
         testee.parseFromCentralDirectoryData(_123456789ABCDEF0_AS_LE_BYTE_ARRAY, 0, 8);
-        assertThat(testee.getSize())
+        assertThat(testee.getValue())
             .contains(0x123456789ABCDEF0L);
     }
 
@@ -188,7 +188,7 @@ public class SizeExtraFieldTest {
     void parseFromCentralDirectoryDataShouldParseWhenFEDCBA9876543210InLittleEndian() throws
Exception {
         byte[] input = FEDCBA9876543210_AS_LE_BYTE_ARRAY;
         testee.parseFromCentralDirectoryData(input, 0, 8);
-        assertThat(testee.getSize())
+        assertThat(testee.getValue())
             .contains(0xFEDCBA9876543210L);
     }
 
@@ -196,7 +196,7 @@ public class SizeExtraFieldTest {
     void parseFromCentralDirectoryDataShouldHandleOffset() throws Exception {
         byte[] input = Arrays.concatenate(UNUSED, _123456789ABCDEF0_AS_LE_BYTE_ARRAY);
         testee.parseFromCentralDirectoryData(input, 2, 8);
-        assertThat(testee.getSize())
+        assertThat(testee.getValue())
             .contains(0x123456789ABCDEF0L);
     }
 }

http://git-wip-us.apache.org/repos/asf/james-project/blob/1d3e4a92/mailbox/backup/src/test/java/org/apache/james/mailbox/backup/UidExtraFieldTest.java
----------------------------------------------------------------------
diff --git a/mailbox/backup/src/test/java/org/apache/james/mailbox/backup/UidExtraFieldTest.java
b/mailbox/backup/src/test/java/org/apache/james/mailbox/backup/UidExtraFieldTest.java
index b920ec5..ed3376d 100644
--- a/mailbox/backup/src/test/java/org/apache/james/mailbox/backup/UidExtraFieldTest.java
+++ b/mailbox/backup/src/test/java/org/apache/james/mailbox/backup/UidExtraFieldTest.java
@@ -158,7 +158,7 @@ public class UidExtraFieldTest {
             UidExtraField testee = new UidExtraField();
 
             testee.parseFromLocalFileData(ZERO_AS_BYTE_ARRAY, 0, 8);
-            assertThat(testee.getUid())
+            assertThat(testee.getValue())
                 .contains(0L);
         }
 
@@ -167,7 +167,7 @@ public class UidExtraFieldTest {
             UidExtraField testee = new UidExtraField();
 
             testee.parseFromLocalFileData(_123456789ABCDEF0_AS_LE_BYTE_ARRAY, 0, 8);
-            assertThat(testee.getUid())
+            assertThat(testee.getValue())
                 .contains(0x123456789ABCDEF0L);
         }
 
@@ -177,7 +177,7 @@ public class UidExtraFieldTest {
 
             byte[] input = FEDCBA9876543210_AS_LE_BYTE_ARRAY;
             testee.parseFromLocalFileData(input, 0, 8);
-            assertThat(testee.getUid())
+            assertThat(testee.getValue())
                 .contains(0xFEDCBA9876543210L);
         }
 
@@ -187,7 +187,7 @@ public class UidExtraFieldTest {
 
             byte[] input = Arrays.concatenate(UNUSED, _123456789ABCDEF0_AS_LE_BYTE_ARRAY);
             testee.parseFromLocalFileData(input, 2, 8);
-            assertThat(testee.getUid())
+            assertThat(testee.getValue())
                 .contains(0x123456789ABCDEF0L);
         }
     }
@@ -218,7 +218,7 @@ public class UidExtraFieldTest {
             UidExtraField testee = new UidExtraField();
 
             testee.parseFromCentralDirectoryData(ZERO_AS_BYTE_ARRAY, 0, 8);
-            assertThat(testee.getUid())
+            assertThat(testee.getValue())
                 .contains(0L);
         }
 
@@ -227,7 +227,7 @@ public class UidExtraFieldTest {
             UidExtraField testee = new UidExtraField();
 
             testee.parseFromCentralDirectoryData(_123456789ABCDEF0_AS_LE_BYTE_ARRAY, 0, 8);
-            assertThat(testee.getUid())
+            assertThat(testee.getValue())
                 .contains(0x123456789ABCDEF0L);
         }
 
@@ -237,7 +237,7 @@ public class UidExtraFieldTest {
             byte[] input = FEDCBA9876543210_AS_LE_BYTE_ARRAY;
 
             testee.parseFromCentralDirectoryData(input, 0, 8);
-            assertThat(testee.getUid())
+            assertThat(testee.getValue())
                 .contains(0xFEDCBA9876543210L);
         }
 
@@ -247,7 +247,7 @@ public class UidExtraFieldTest {
             byte[] input = Arrays.concatenate(UNUSED, _123456789ABCDEF0_AS_LE_BYTE_ARRAY);
 
             testee.parseFromCentralDirectoryData(input, 2, 8);
-            assertThat(testee.getUid())
+            assertThat(testee.getValue())
                 .contains(0x123456789ABCDEF0L);
         }
     }


---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org


Mime
View raw message