Author: bodewig
Date: Mon Feb 24 10:56:59 2014
New Revision: 1571229
URL: http://svn.apache.org/r1571229
Log:
a bit more typesafety by using a custom class rather than Map.Entry - more
incremental changes to follow
Added:
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/SevenZMethodConfiguration.java
(with props)
commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/sevenz/SevenZMethodConfigurationTest.java
(with props)
Modified:
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/LZMA2Decoder.java
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/SevenZMethod.java
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/SevenZOutputFile.java
Modified:
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/LZMA2Decoder.java
URL:
http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/LZMA2Decoder.java?rev=1571229&r1=1571228&r2=1571229&view=diff
==============================================================================
---
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/LZMA2Decoder.java
(original)
+++
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/LZMA2Decoder.java
Mon Feb 24 10:56:59 2014
@@ -56,8 +56,6 @@ class LZMA2Decoder extends Coders.CoderB
private LZMA2Options getOptions(Object opts) throws IOException {
if (opts instanceof LZMA2Options) {
return (LZMA2Options) opts;
- } else if (opts != null) {
- throw new IllegalArgumentException("LZMA2 method only supports
LZMA2Options objects as option");
}
LZMA2Options options = new LZMA2Options();
options.setDictSize(LZMA2Options.DICT_SIZE_DEFAULT);
Modified:
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/SevenZMethod.java
URL:
http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/SevenZMethod.java?rev=1571229&r1=1571228&r2=1571229&view=diff
==============================================================================
---
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/SevenZMethod.java
(original)
+++
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/SevenZMethod.java
Mon Feb 24 10:56:59 2014
@@ -28,7 +28,7 @@ public enum SevenZMethod {
/** LZMA - only supported when reading */
LZMA(new byte[] { (byte)0x03, (byte)0x01, (byte)0x01 }),
/** LZMA2 */
- LZMA2(new byte[] { (byte)0x21 }) {
+ LZMA2(new byte[] { (byte)0x21 }, LZMA2Options.class) {
@Override
byte[] getProperties(Object opts) {
int dictSize = getDictSize(opts);
@@ -38,11 +38,9 @@ public enum SevenZMethod {
(byte) ((19 - lead) * 2 + secondBit)
};
}
- int getDictSize(Object opts) {
+ private int getDictSize(Object opts) {
if (opts instanceof LZMA2Options) {
return ((LZMA2Options) opts).getDictSize();
- } else if (opts != null) {
- throw new IllegalArgumentException("LZMA2 method only supports
LZMA2Options objects as option");
}
return LZMA2Options.DICT_SIZE_DEFAULT;
}
@@ -58,9 +56,11 @@ public enum SevenZMethod {
AES256SHA256(new byte[] { (byte)0x06, (byte)0xf1, (byte)0x07, (byte)0x01
});
private final byte[] id;
+ private final Class<?>[] acceptableOptions;
- private SevenZMethod(byte[] id) {
+ private SevenZMethod(byte[] id, Class<?>... acceptableOptions) {
this.id = id;
+ this.acceptableOptions = acceptableOptions;
}
byte[] getId() {
@@ -76,4 +76,16 @@ public enum SevenZMethod {
return new byte[0];
}
+ /**
+ * @return whether this method can extract options from the given object.
+ */
+ boolean canAcceptOptions(Object opts) {
+ for (Class<?> c : acceptableOptions) {
+ if (c.isInstance(opts)) {
+ return true;
+ }
+ }
+ return false;
+ }
+
}
Added:
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/SevenZMethodConfiguration.java
URL:
http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/SevenZMethodConfiguration.java?rev=1571229&view=auto
==============================================================================
---
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/SevenZMethodConfiguration.java
(added)
+++
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/SevenZMethodConfiguration.java
Mon Feb 24 10:56:59 2014
@@ -0,0 +1,66 @@
+/*
+ * 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.commons.compress.archivers.sevenz;
+
+/**
+ * Combines a SevenZMethod with configuration options for the method.
+ * @Immutable
+ * @since 1.8
+ */
+public class SevenZMethodConfiguration {
+ private final SevenZMethod method;
+ private final Object options;
+
+ /**
+ * Doesn't configure any additional options.
+ * @param method the method to use
+ */
+ public SevenZMethodConfiguration(SevenZMethod method) {
+ this(method, null);
+ }
+
+ /**
+ * Specifies and method plus configuration options.
+ * @param method the method to use
+ * @param options the options to use
+ * @throws IllegalArgumentException if the method doesn't understand the
options specified.
+ */
+ public SevenZMethodConfiguration(SevenZMethod method, Object options) {
+ this.method = method;
+ this.options = options;
+ if (options != null && !method.canAcceptOptions(options)) {
+ throw new IllegalArgumentException("The " + method + " method
doesn't support options of type "
+ + options.getClass());
+ }
+ }
+
+ /**
+ * The specified method.
+ */
+ public SevenZMethod getMethod() {
+ return method;
+ }
+
+ /**
+ * The specified options.
+ */
+ public Object getOptions() {
+ return options;
+ }
+
+}
Propchange:
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/SevenZMethodConfiguration.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified:
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/SevenZOutputFile.java
URL:
http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/SevenZOutputFile.java?rev=1571229&r1=1571228&r2=1571229&view=diff
==============================================================================
---
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/SevenZOutputFile.java
(original)
+++
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/SevenZOutputFile.java
Mon Feb 24 10:56:59 2014
@@ -31,7 +31,6 @@ import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.LinkedList;
-import java.util.Map.Entry;
import java.util.zip.CRC32;
import org.apache.commons.compress.archivers.ArchiveEntry;
@@ -50,8 +49,8 @@ public class SevenZOutputFile implements
private long fileBytesWritten = 0;
private boolean finished = false;
private CountingOutputStream currentOutputStream;
- private Iterable<? extends Entry<SevenZMethod, ?>> contentMethods =
- Collections.singletonList(new FakeEntry(SevenZMethod.LZMA2));
+ private Iterable<? extends SevenZMethodConfiguration> contentMethods =
+ Collections.singletonList(new
SevenZMethodConfiguration(SevenZMethod.LZMA2));
/**
* Opens file to write a 7z archive to.
@@ -76,7 +75,7 @@ public class SevenZOutputFile implements
* to {@link #setContentMethods}.</p>
*/
public void setContentCompression(SevenZMethod method) {
- setContentMethods(Collections.singletonList(new FakeEntry(method)));
+ setContentMethods(Collections.singletonList(new
SevenZMethodConfiguration(method)));
}
/**
@@ -92,7 +91,7 @@ public class SevenZOutputFile implements
*
* @since 1.8
*/
- public void setContentMethods(Iterable<? extends Entry<SevenZMethod, ?>>
methods) {
+ public void setContentMethods(Iterable<? extends
SevenZMethodConfiguration> methods) {
this.contentMethods = methods;
}
@@ -262,8 +261,8 @@ public class SevenZOutputFile implements
private CountingOutputStream setupFileOutputStream() throws IOException {
OutputStream out = new OutputStreamWrapper();
- for (Entry<SevenZMethod, ?> m : reverse(contentMethods)) {
- out = Coders.addEncoder(out, m.getKey(), m.getValue());
+ for (SevenZMethodConfiguration m : reverse(contentMethods)) {
+ out = Coders.addEncoder(out, m.getMethod(), m.getOptions());
}
return new CountingOutputStream(out) {
@Override
@@ -288,7 +287,7 @@ public class SevenZOutputFile implements
}
private static <T> Iterable<T> reverse(Iterable<T> i) {
- LinkedList<T> l = new LinkedList();
+ LinkedList<T> l = new LinkedList<T>();
for (T t : i) {
l.addFirst(t);
}
@@ -370,7 +369,7 @@ public class SevenZOutputFile implements
private void writeFolder(final DataOutput header) throws IOException {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
int numCoders = 0;
- for (Entry<SevenZMethod, ?> m : contentMethods) {
+ for (SevenZMethodConfiguration m : contentMethods) {
numCoders++;
writeSingleCodec(m, bos);
}
@@ -379,9 +378,9 @@ public class SevenZOutputFile implements
header.write(bos.toByteArray());
}
- private void writeSingleCodec(Entry<SevenZMethod, ?> m, OutputStream bos)
throws IOException {
- byte[] id = m.getKey().getId();
- byte[] properties = m.getKey().getProperties(m.getValue());
+ private void writeSingleCodec(SevenZMethodConfiguration m, OutputStream
bos) throws IOException {
+ byte[] id = m.getMethod().getId();
+ byte[] properties = m.getMethod().getProperties(m.getOptions());
int codecFlags = id.length;
if (properties.length > 0) {
@@ -721,20 +720,4 @@ public class SevenZOutputFile implements
// the file will be closed by the containing class's close method
}
}
-
- private static class FakeEntry implements Entry<SevenZMethod, Object> {
- private final SevenZMethod m;
- FakeEntry(SevenZMethod m) {
- this.m = m;
- }
- public SevenZMethod getKey() {
- return m;
- }
- public Object getValue() {
- return null;
- }
- public Object setValue(Object o) {
- return null;
- }
- }
}
Added:
commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/sevenz/SevenZMethodConfigurationTest.java
URL:
http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/sevenz/SevenZMethodConfigurationTest.java?rev=1571229&view=auto
==============================================================================
---
commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/sevenz/SevenZMethodConfigurationTest.java
(added)
+++
commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/sevenz/SevenZMethodConfigurationTest.java
Mon Feb 24 10:56:59 2014
@@ -0,0 +1,44 @@
+/*
+ * 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.commons.compress.archivers.sevenz;
+
+import org.junit.Assert;
+import org.junit.Test;
+import org.tukaani.xz.LZMA2Options;
+
+public class SevenZMethodConfigurationTest {
+
+ @Test
+ public void shouldAllowNullOptions() {
+ Assert.assertNull(new SevenZMethodConfiguration(SevenZMethod.LZMA2,
null)
+ .getOptions());
+ }
+
+ @Test
+ public void shouldAllowLZMA2OptionsForLZMA2() {
+ Assert.assertNotNull(new SevenZMethodConfiguration(SevenZMethod.LZMA2,
+ new LZMA2Options())
+ .getOptions());
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void shouldNotAllowStringOptionsForLZMA2() {
+ new SevenZMethodConfiguration(SevenZMethod.LZMA2, "");
+ }
+
+}
Propchange:
commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/sevenz/SevenZMethodConfigurationTest.java
------------------------------------------------------------------------------
svn:eol-style = native