commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bode...@apache.org
Subject svn commit: r1561268 - /commons/proper/compress/branches/compress-2.0/src/main/java/org/apache/commons/compress2/archivers/Archivers.java
Date Sat, 25 Jan 2014 06:46:11 GMT
Author: bodewig
Date: Sat Jan 25 06:46:10 2014
New Revision: 1561268

URL: http://svn.apache.org/r1561268
Log:
internally sort archives found in a way that during auto-detection the formats requiring less
bytes come first

Modified:
    commons/proper/compress/branches/compress-2.0/src/main/java/org/apache/commons/compress2/archivers/Archivers.java

Modified: commons/proper/compress/branches/compress-2.0/src/main/java/org/apache/commons/compress2/archivers/Archivers.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/branches/compress-2.0/src/main/java/org/apache/commons/compress2/archivers/Archivers.java?rev=1561268&r1=1561267&r2=1561268&view=diff
==============================================================================
--- commons/proper/compress/branches/compress-2.0/src/main/java/org/apache/commons/compress2/archivers/Archivers.java
(original)
+++ commons/proper/compress/branches/compress-2.0/src/main/java/org/apache/commons/compress2/archivers/Archivers.java
Sat Jan 25 06:46:10 2014
@@ -18,13 +18,18 @@
  */
 package org.apache.commons.compress2.archivers;
 
+import java.util.ArrayList;
 import java.util.Collections;
-import java.util.HashMap;
+import java.util.Comparator;
 import java.util.Iterator;
+import java.util.LinkedHashMap;
+import java.util.List;
 import java.util.Map;
 import java.util.NoSuchElementException;
 import java.util.ServiceConfigurationError;
 import java.util.ServiceLoader;
+import java.util.Set;
+import java.util.TreeSet;
 
 /**
  * Loads ArchiveFormats defined as "services" from {@code
@@ -113,10 +118,11 @@ public class Archivers implements Iterab
     }
 
     private void fillMap() throws ServiceConfigurationError {
-        // TODO make that a TreeMap sorted for auto-detection order
+        Set<ArchiveFormat> ts = new TreeSet<ArchiveFormat>(SORT_FOR_AUTO_DETECTION);
+        ts.addAll(asList(formatLoader));
         Map<String, ArchiveFormat<? extends ArchiveEntry>> a =
-            new HashMap<String, ArchiveFormat<? extends ArchiveEntry>>();
-        for (ArchiveFormat<? extends ArchiveEntry> f : formatLoader) {
+            new LinkedHashMap<String, ArchiveFormat<? extends ArchiveEntry>>();
+        for (ArchiveFormat<? extends ArchiveEntry> f : ts) {
             a.put(f.getName(), f);
         }
         archivers = Collections.unmodifiableMap(a);
@@ -168,6 +174,29 @@ public class Archivers implements Iterab
         };
     }
 
+    private static <T> List<T> asList(Iterable<T> i) {
+        List<T> l = new ArrayList<T>();
+        for (T t : i) {
+            l.add(t);
+        }
+        return l;
+    }
+
+    private Comparator<ArchiveFormat> SORT_FOR_AUTO_DETECTION = new Comparator<ArchiveFormat>()
{
+        public int compare(ArchiveFormat a1, ArchiveFormat a2) {
+            if (a1.supportsAutoDetection() && a2.supportsAutoDetection()) {
+                return a1.getNumberOfBytesRequiredForAutodetection() - a2.getNumberOfBytesRequiredForAutodetection();
+            }
+            if (!a1.supportsAutoDetection() && !a2.supportsAutoDetection()) {
+                return 0;
+            }
+            if (a1.supportsAutoDetection()) {
+                return -1;
+            }
+            return 1;
+        }
+    };
+
     private static class FilteringIterator<T> implements Iterator<T> {
         private final Iterator<T> i;
         private final Predicate<? super T> filter;



Mime
View raw message