You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tika.apache.org by kk...@apache.org on 2020/11/05 22:47:48 UTC

[tika] branch main updated: Add CompareUtils and simplify some sort

This is an automated email from the ASF dual-hosted git repository.

kkrugler pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/tika.git


The following commit(s) were added to refs/heads/main by this push:
     new 71486d7  Add CompareUtils and simplify some sort
     new 793eb4a  Merge pull request #375 from PeterAlfredLee/addCompareUtils
71486d7 is described below

commit 71486d765318062f8cb04cd3335bd9049a2ff5fc
Author: PeterAlfredLee <pe...@gmail.com>
AuthorDate: Sat Oct 31 12:13:55 2020 +0800

    Add CompareUtils and simplify some sort
---
 .../tika/language/detect/LanguageDetector.java     | 20 +---------
 .../tika/language/translate/DefaultTranslator.java | 19 +--------
 .../java/org/apache/tika/utils/CompareUtils.java   | 46 ++++++++++++++++++++++
 .../org/apache/tika/utils/ServiceLoaderUtils.java  | 18 +--------
 4 files changed, 51 insertions(+), 52 deletions(-)

diff --git a/tika-core/src/main/java/org/apache/tika/language/detect/LanguageDetector.java b/tika-core/src/main/java/org/apache/tika/language/detect/LanguageDetector.java
index 7be4e4f..7b9d1ca 100644
--- a/tika-core/src/main/java/org/apache/tika/language/detect/LanguageDetector.java
+++ b/tika-core/src/main/java/org/apache/tika/language/detect/LanguageDetector.java
@@ -17,13 +17,12 @@
 package org.apache.tika.language.detect;
 
 import java.io.IOException;
-import java.util.Collections;
-import java.util.Comparator;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
 import org.apache.tika.config.ServiceLoader;
+import org.apache.tika.utils.CompareUtils;
 
 // We should use the IANA registry for primary language names...see
 // http://www.iana.org/assignments/language-subtag-registry/language-subtag-registry
@@ -73,22 +72,7 @@ public abstract class LanguageDetector {
 	
 	public static List<LanguageDetector> getLanguageDetectors(ServiceLoader loader) {
         List<LanguageDetector> detectors = loader.loadStaticServiceProviders(LanguageDetector.class);
-        Collections.sort(detectors, new Comparator<LanguageDetector>() {
-            public int compare(LanguageDetector d1, LanguageDetector d2) {
-                String n1 = d1.getClass().getName();
-                String n2 = d2.getClass().getName();
-                boolean tika1 = n1.startsWith("org.apache.tika.");
-                boolean tika2 = n2.startsWith("org.apache.tika.");
-                if (tika1 == tika2) {
-                    return n1.compareTo(n2);
-                } else if (tika1) {
-                    return -1;
-                } else {
-                    return 1;
-                }
-            }
-        });
-        
+        detectors.sort(CompareUtils::compareClassName);
         return detectors;
 	}
 	
diff --git a/tika-core/src/main/java/org/apache/tika/language/translate/DefaultTranslator.java b/tika-core/src/main/java/org/apache/tika/language/translate/DefaultTranslator.java
index f3e0600..93fcda0 100644
--- a/tika-core/src/main/java/org/apache/tika/language/translate/DefaultTranslator.java
+++ b/tika-core/src/main/java/org/apache/tika/language/translate/DefaultTranslator.java
@@ -18,12 +18,11 @@
 package org.apache.tika.language.translate;
 
 import java.io.IOException;
-import java.util.Collections;
-import java.util.Comparator;
 import java.util.List;
 
 import org.apache.tika.config.ServiceLoader;
 import org.apache.tika.exception.TikaException;
+import org.apache.tika.utils.CompareUtils;
 
 /**
  * A translator which picks the first available {@link Translator} 
@@ -51,21 +50,7 @@ public class DefaultTranslator implements Translator {
      */
     private static List<Translator> getDefaultTranslators(ServiceLoader loader) {
         List<Translator> translators = loader.loadStaticServiceProviders(Translator.class);
-        Collections.sort(translators, new Comparator<Translator>() {
-            public int compare(Translator t1, Translator t2) {
-                String n1 = t1.getClass().getName();
-                String n2 = t2.getClass().getName();
-                boolean tika1 = n1.startsWith("org.apache.tika.");
-                boolean tika2 = n2.startsWith("org.apache.tika.");
-                if (tika1 == tika2) {
-                    return n1.compareTo(n2);
-                } else if (tika1) {
-                    return -1;
-                } else {
-                    return 1;
-                }
-            }
-        });
+        translators.sort(CompareUtils::compareClassName);
         return translators;
     }
     /**
diff --git a/tika-core/src/main/java/org/apache/tika/utils/CompareUtils.java b/tika-core/src/main/java/org/apache/tika/utils/CompareUtils.java
new file mode 100644
index 0000000..e7e1d9d
--- /dev/null
+++ b/tika-core/src/main/java/org/apache/tika/utils/CompareUtils.java
@@ -0,0 +1,46 @@
+/**
+ * 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.tika.utils;
+
+public class CompareUtils {
+
+    /**
+     * Compare two classes by class names.
+     * If both classes are Tika's or both are not Tika's class, compare by name String.
+     * Otherwise one of these two class is Tika's class. Then the Tika's class comes before non-Tika's class.
+     * @param o1 the object 1 to be compared
+     * @param o2 the object 2 to be compared
+     * @return a negative integer, zero, or a positive integer
+     */
+    public static int compareClassName(Object o1, Object o2) {
+        // Get class names.
+        String n1 = o1.getClass().getName();
+        String n2 = o2.getClass().getName();
+
+        // Judge if they are Tika's class by name.
+        boolean tika1 = n1.startsWith("org.apache.tika.");
+        boolean tika2 = n2.startsWith("org.apache.tika.");
+
+        // If both classes are Tika's class or both are not Tika's class, compare by name String.
+        if (tika1 == tika2) {
+            return n1.compareTo(n2);
+        }
+
+        // Otherwise one of these two class is Tika's class. Then the Tika's class comes before non-Tika's class.
+        return tika1 ? -1 : 1;
+    }
+}
diff --git a/tika-core/src/main/java/org/apache/tika/utils/ServiceLoaderUtils.java b/tika-core/src/main/java/org/apache/tika/utils/ServiceLoaderUtils.java
index 0887e0d..d34667d 100644
--- a/tika-core/src/main/java/org/apache/tika/utils/ServiceLoaderUtils.java
+++ b/tika-core/src/main/java/org/apache/tika/utils/ServiceLoaderUtils.java
@@ -16,8 +16,6 @@
  */
 package org.apache.tika.utils;
 
-import java.util.Collections;
-import java.util.Comparator;
 import java.util.List;
 
 import org.apache.tika.config.ServiceLoader;
@@ -31,21 +29,7 @@ public class ServiceLoaderUtils {
      *  before Tika ones, and otherwise in reverse alphabetical order
      */
     public static <T> void sortLoadedClasses(List<T> loaded) {
-        Collections.sort(loaded, new Comparator<T>() {
-            public int compare(T c1, T c2) {
-                String n1 = c1.getClass().getName();
-                String n2 = c2.getClass().getName();
-                boolean t1 = n1.startsWith("org.apache.tika.");
-                boolean t2 = n2.startsWith("org.apache.tika.");
-                if (t1 == t2) {
-                    return n1.compareTo(n2);
-                } else if (t1) {
-                    return -1;
-                } else {
-                    return 1;
-                }
-            }
-        });
+        loaded.sort(CompareUtils::compareClassName);
     }
 
     /**