Skip to content
Closed
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 @@ -37,4 +37,16 @@ public interface Factory {
* using "-" (e.g. {@code elasticsearch-7}).
*/
String identifier();

/**
* This is the priority of the factory, allows you to override the default factory
* implementation. The default officially implemented factory priority is 9. If the number is
* less than 9, the smaller number will be selected for execution. If the smallest priority also
* conflicts, an exception will be thrown.
*
* @return priority
*/
default int priority() {
return 9;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.ServiceLoader;
Expand Down Expand Up @@ -74,6 +75,12 @@ public static <T extends Factory> T discoverFactory(
.collect(Collectors.joining("\n"))));
}
if (matchingFactories.size() > 1) {
if (matchingFactories.stream().map(Factory::priority).distinct().count() > 1) {
return (T)
matchingFactories.stream()
.min(Comparator.comparingInt(Factory::priority))
.get();
}
throw new FactoryException(
String.format(
"Multiple factories for identifier '%s' that implement '%s' found in the classpath.\n\n"
Expand Down
Loading