Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -73,22 +72,7 @@ public static List<LanguageDetector> getLanguageDetectors() {

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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down Expand Up @@ -51,21 +50,7 @@ public DefaultTranslator() {
*/
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;
}
/**
Expand Down
46 changes: 46 additions & 0 deletions tika-core/src/main/java/org/apache/tika/utils/CompareUtils.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}

/**
Expand Down