Compute Model(s) used by the icon provider only once
@@ -14,8 +14,12 @@ import java.util.Optional;
* @author Edoardo Luppi
*/
public abstract class BaseIconProvider extends IconProvider {
+ private Model[] models;
+
/**
* Returns the Model(s) supported by this icon provider.
+ * This method will be called exactly once, so there is no need
+ * to provide a static array, as the array returned by this method is cached.
*/
@NotNull
protected abstract Model[] getModels();
@@ -33,7 +37,7 @@ public abstract class BaseIconProvider extends IconProvider {
if (optFile.isPresent() && isSupported(file = optFile.get())) {
final String fileName = file.getName().toLowerCase();
- final Model[] models = getModels();
+ final Model[] models = lazyGetModels();
for (final Model model : models) {
if (model.check(fileName)) {
@@ -44,4 +48,12 @@ public abstract class BaseIconProvider extends IconProvider {
return null;
}
+
+ /**
+ * Lazily computes the Model(s) used by this icon provider.
+ * This should be already thread safe in the IDEA platform context.
+ */
+ private Model[] lazyGetModels() {
+ return models == null ? (models = getModels()) : models;
+ }
}
