Skip to content

Commit

Permalink
cache providers to avoid printing the warnings all the time, but prin…
Browse files Browse the repository at this point in the history
…t it only once
  • Loading branch information
radeusgd committed Nov 29, 2024
1 parent 0c7a6d9 commit 162231c
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public static List<Value> get_types(boolean refresh) {
if (refresh) {
loader.reload();
}
return loader.getProviders().map(AbstractEnsoTypeSPI::getTypeObject).toList();
return loader.getProviders().stream().map(AbstractEnsoTypeSPI::getTypeObject).toList();
}

public static Value findFormatForDataLinkSubType(String subType) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public static List<Value> get_types(boolean refresh) {
if (refresh) {
loader.reload();
}
return loader.getProviders().map(FileSystemSPI::getTypeObject).toList();
return loader.getProviders().stream().map(FileSystemSPI::getTypeObject).toList();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ public static List<Value> get_types(boolean refresh) {
if (refresh) {
loader.reload();
}
return loader.getProviders().map(ReadManyReturnSPI::getTypeObject).toList();
return loader.getProviders().stream().map(ReadManyReturnSPI::getTypeObject).toList();
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package org.enso.base.spi;

import java.util.List;
import java.util.ServiceLoader;
import java.util.function.Predicate;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.enso.base.polyglot.EnsoMeta;
import org.graalvm.polyglot.PolyglotException;
import org.graalvm.polyglot.Value;
Expand Down Expand Up @@ -67,16 +67,27 @@ public Loader(Class<T> clazz) {
loader = ServiceLoader.load(clazz, clazz.getClassLoader());
}

private transient List<T> cachedProviders = null;

public final void reload() {
cachedProviders = null;
loader.reload();
}

public final Stream<T> getProviders() {
return loader.stream().map(ServiceLoader.Provider::get).filter(AbstractEnsoTypeSPI::isLoaded);
public final List<T> getProviders() {
if (cachedProviders == null) {
cachedProviders =
loader.stream()
.map(ServiceLoader.Provider::get)
.filter(AbstractEnsoTypeSPI::isLoaded)
.toList();
}

return cachedProviders;
}

public T findSingleProvider(Predicate<T> predicate, String predicateDescription) {
var found = getProviders().filter(predicate).toList();
var found = getProviders().stream().filter(predicate).toList();
if (found.isEmpty()) {
return null;
} else if (found.size() > 1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ public static String[][] get_default_constructors(boolean refresh) {
if (refresh) {
loader.reload();
}
return loader
.getProviders()
return loader.getProviders().stream()
.map(
provider ->
new String[] {
Expand All @@ -37,7 +36,7 @@ public static List<Value> get_types(boolean refresh) {
if (refresh) {
loader.reload();
}
return loader.getProviders().map(DatabaseConnectionDetailsSPI::getTypeObject).toList();
return loader.getProviders().stream().map(DatabaseConnectionDetailsSPI::getTypeObject).toList();
}

private static final class DatabaseConnectionDetailsLoader
Expand Down

0 comments on commit 162231c

Please sign in to comment.