GH-323 - Move off of Google's Suppliers.memoize(…).

This commit is contained in:
Oliver Drotbohm
2023-10-15 19:22:33 +02:00
parent cb6d71ba60
commit 4a857dcddf
6 changed files with 46 additions and 45 deletions

View File

@@ -30,6 +30,7 @@ import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@@ -38,6 +39,7 @@ import org.springframework.modulith.core.Types.JMoleculesTypes;
import org.springframework.modulith.core.Types.SpringTypes;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import org.springframework.util.function.SingletonSupplier;
import com.tngtech.archunit.base.DescribedPredicate;
import com.tngtech.archunit.core.domain.Dependency;
@@ -48,8 +50,6 @@ import com.tngtech.archunit.core.domain.JavaField;
import com.tngtech.archunit.core.domain.JavaMember;
import com.tngtech.archunit.core.domain.JavaMethod;
import com.tngtech.archunit.core.domain.SourceCodeLocation;
import com.tngtech.archunit.thirdparty.com.google.common.base.Supplier;
import com.tngtech.archunit.thirdparty.com.google.common.base.Suppliers;
/**
* An application module.
@@ -89,11 +89,11 @@ public class ApplicationModule {
this.namedInterfaces = NamedInterfaces.discoverNamedInterfaces(basePackage);
this.useFullyQualifiedModuleNames = useFullyQualifiedModuleNames;
this.springBeans = Suppliers.memoize(() -> filterSpringBeans(basePackage));
this.aggregateRoots = Suppliers.memoize(() -> findAggregateRoots(basePackage));
this.valueTypes = Suppliers
.memoize(() -> findArchitecturallyEvidentType(ArchitecturallyEvidentType::isValueObject));
this.publishedEvents = Suppliers.memoize(() -> findPublishedEvents());
this.springBeans = SingletonSupplier.of(() -> filterSpringBeans(basePackage));
this.aggregateRoots = SingletonSupplier.of(() -> findAggregateRoots(basePackage));
this.valueTypes = SingletonSupplier
.of(() -> findArchitecturallyEvidentType(ArchitecturallyEvidentType::isValueObject));
this.publishedEvents = SingletonSupplier.of(() -> findPublishedEvents());
}
/**
@@ -417,7 +417,7 @@ public class ApplicationModule {
/**
* Returns whether the {@link ApplicationModule} contains the package with the given name, which means the given
* package is either the module's base package or a sub package of it.
*
*
* @param packageName must not be {@literal null} or empty.
* @return whether the {@link ApplicationModule} contains the package with the given name.
* @since 1.0.2

View File

@@ -27,6 +27,7 @@ import java.util.Optional;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.stream.Stream;
import org.springframework.data.repository.core.RepositoryMetadata;
@@ -34,12 +35,10 @@ import org.springframework.data.repository.core.support.AbstractRepositoryMetada
import org.springframework.modulith.core.Types.JMoleculesTypes;
import org.springframework.modulith.core.Types.SpringDataTypes;
import org.springframework.modulith.core.Types.SpringTypes;
import org.springframework.util.function.SingletonSupplier;
import com.tngtech.archunit.core.domain.JavaClass;
import com.tngtech.archunit.core.domain.JavaMethod;
import com.tngtech.archunit.core.domain.JavaType;
import com.tngtech.archunit.thirdparty.com.google.common.base.Supplier;
import com.tngtech.archunit.thirdparty.com.google.common.base.Suppliers;
/**
* A type that is architecturally relevant, i.e. it fulfills a significant role within the architecture.
@@ -488,36 +487,37 @@ public abstract class ArchitecturallyEvidentType {
public static DelegatingType of(JavaClass type, List<ArchitecturallyEvidentType> types) {
var isAggregateRoot = Suppliers
.memoize(() -> types.stream().anyMatch(ArchitecturallyEvidentType::isAggregateRoot));
var isAggregateRoot = SingletonSupplier
.of(() -> types.stream().anyMatch(ArchitecturallyEvidentType::isAggregateRoot));
var isRepository = Suppliers
.memoize(() -> types.stream().anyMatch(ArchitecturallyEvidentType::isRepository));
var isRepository = SingletonSupplier
.of(() -> types.stream().anyMatch(ArchitecturallyEvidentType::isRepository));
var isEntity = Suppliers
.memoize(() -> types.stream().anyMatch(ArchitecturallyEvidentType::isEntity));
var isEntity = SingletonSupplier
.of(() -> types.stream().anyMatch(ArchitecturallyEvidentType::isEntity));
var isService = Suppliers
.memoize(() -> types.stream().anyMatch(ArchitecturallyEvidentType::isService));
var isService = SingletonSupplier
.of(() -> types.stream().anyMatch(ArchitecturallyEvidentType::isService));
var isController = Suppliers
.memoize(() -> types.stream().anyMatch(ArchitecturallyEvidentType::isController));
var isController = SingletonSupplier
.of(() -> types.stream().anyMatch(ArchitecturallyEvidentType::isController));
var isEventListener = Suppliers
.memoize(() -> types.stream().anyMatch(ArchitecturallyEvidentType::isEventListener));
var isEventListener = SingletonSupplier
.of(() -> types.stream().anyMatch(ArchitecturallyEvidentType::isEventListener));
var isConfigurationProperties = Suppliers
.memoize(() -> types.stream().anyMatch(ArchitecturallyEvidentType::isConfigurationProperties));
var isConfigurationProperties = SingletonSupplier
.of(() -> types.stream().anyMatch(ArchitecturallyEvidentType::isConfigurationProperties));
var isInjectable = Suppliers.memoize(() -> types.stream().anyMatch(ArchitecturallyEvidentType::isInjectable));
var isInjectable = SingletonSupplier.of(() -> types.stream().anyMatch(ArchitecturallyEvidentType::isInjectable));
var isValueObject = Suppliers.memoize(() -> types.stream().anyMatch(ArchitecturallyEvidentType::isValueObject));
var isValueObject = SingletonSupplier
.of(() -> types.stream().anyMatch(ArchitecturallyEvidentType::isValueObject));
Supplier<Collection<JavaClass>> referenceTypes = Suppliers.memoize(() -> types.stream() //
Supplier<Collection<JavaClass>> referenceTypes = SingletonSupplier.of(() -> types.stream() //
.flatMap(ArchitecturallyEvidentType::getReferenceTypes) //
.toList());
Supplier<Collection<ReferenceMethod>> referenceMethods = Suppliers.memoize(() -> types.stream() //
Supplier<Collection<ReferenceMethod>> referenceMethods = SingletonSupplier.of(() -> types.stream() //
.flatMap(ArchitecturallyEvidentType::getReferenceMethods) //
.toList());

View File

@@ -17,6 +17,7 @@ package org.springframework.modulith.core;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
@@ -25,10 +26,9 @@ import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.util.StringUtils;
import org.springframework.util.function.SingletonSupplier;
import com.tngtech.archunit.core.domain.JavaClass;
import com.tngtech.archunit.thirdparty.com.google.common.base.Supplier;
import com.tngtech.archunit.thirdparty.com.google.common.base.Suppliers;
/**
* Wrapper around {@link JavaClass} that allows creating additional formatted names.
@@ -106,7 +106,7 @@ public class FormatableType {
Assert.hasText(type, "Type must not be null or empty!");
this.type = type;
this.abbreviatedName = Suppliers.memoize(() -> {
this.abbreviatedName = SingletonSupplier.of(() -> {
String abbreviatedPackage = Stream //
.of(ClassUtils.getPackageName(type).split("\\.")) //

View File

@@ -23,11 +23,13 @@ import java.util.Iterator;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.util.Assert;
import org.springframework.util.function.SingletonSupplier;
import com.tngtech.archunit.base.DescribedIterable;
import com.tngtech.archunit.base.DescribedPredicate;
@@ -35,8 +37,6 @@ import com.tngtech.archunit.core.domain.JavaClass;
import com.tngtech.archunit.core.domain.JavaModifier;
import com.tngtech.archunit.core.domain.properties.CanBeAnnotated;
import com.tngtech.archunit.core.domain.properties.HasModifiers;
import com.tngtech.archunit.thirdparty.com.google.common.base.Supplier;
import com.tngtech.archunit.thirdparty.com.google.common.base.Suppliers;
/**
* An abstraction of a Java package.
@@ -67,7 +67,7 @@ public class JavaPackage implements DescribedIterable<JavaClass> {
this.classes = classes;
this.packageClasses = classes.that(resideInAPackage(includeSubPackages ? name.concat("..") : name));
this.name = name;
this.directSubPackages = Suppliers.memoize(() -> packageClasses.stream() //
this.directSubPackages = SingletonSupplier.of(() -> packageClasses.stream() //
.map(it -> it.getPackageName()) //
.filter(it -> !it.equals(name)) //
.map(it -> extractDirectSubPackage(it)) //

View File

@@ -17,16 +17,17 @@ package org.springframework.modulith.core;
import static com.tngtech.archunit.core.domain.JavaClass.Predicates.*;
import java.util.function.Supplier;
import org.jmolecules.ddd.annotation.AggregateRoot;
import org.springframework.data.repository.Repository;
import org.springframework.util.Assert;
import org.springframework.util.function.SingletonSupplier;
import com.tngtech.archunit.base.DescribedPredicate;
import com.tngtech.archunit.core.domain.JavaClass;
import com.tngtech.archunit.core.domain.JavaClasses;
import com.tngtech.archunit.core.importer.ClassFileImporter;
import com.tngtech.archunit.thirdparty.com.google.common.base.Supplier;
import com.tngtech.archunit.thirdparty.com.google.common.base.Suppliers;
/**
* Utilities for testing.
@@ -35,13 +36,14 @@ import com.tngtech.archunit.thirdparty.com.google.common.base.Suppliers;
*/
class TestUtils {
private static Supplier<JavaClasses> imported = Suppliers.memoize(() -> new ClassFileImporter() //
private static Supplier<JavaClasses> imported = SingletonSupplier.of(() -> new ClassFileImporter() //
.importPackagesOf(ApplicationModules.class, Repository.class, AggregateRoot.class));
private static DescribedPredicate<JavaClass> IS_MODULE_TYPE = JavaClass.Predicates
.resideInAPackage(ApplicationModules.class.getPackage().getName());
private static Supplier<Classes> classes = Suppliers.memoize(() -> Classes.of(imported.get()).that(IS_MODULE_TYPE));
private static Supplier<Classes> classes = SingletonSupplier
.of(() -> Classes.of(imported.get()).that(IS_MODULE_TYPE));
/**
* Returns all {@link Classes} of this module.

View File

@@ -22,6 +22,7 @@ import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Supplier;
import java.util.stream.Stream;
import org.slf4j.Logger;
@@ -33,9 +34,7 @@ import org.springframework.modulith.core.ApplicationModule;
import org.springframework.modulith.core.ApplicationModules;
import org.springframework.modulith.core.JavaPackage;
import org.springframework.modulith.test.ApplicationModuleTest.BootstrapMode;
import com.tngtech.archunit.thirdparty.com.google.common.base.Supplier;
import com.tngtech.archunit.thirdparty.com.google.common.base.Suppliers;
import org.springframework.util.function.SingletonSupplier;
/**
* @author Oliver Drotbohm
@@ -66,7 +65,7 @@ public class ModuleTestExecution implements Iterable<ApplicationModule> {
this.extraIncludes = getExtraModules(annotation, modules).toList();
this.basePackages = Suppliers.memoize(() -> {
this.basePackages = SingletonSupplier.of(() -> {
var moduleBasePackages = module.getBootstrapBasePackages(modules, bootstrapMode.getDepth());
var sharedBasePackages = modules.getSharedModules().stream().map(it -> it.getBasePackage());
@@ -77,7 +76,7 @@ public class ModuleTestExecution implements Iterable<ApplicationModule> {
return Stream.concat(intermediate, sharedBasePackages).distinct().toList();
});
this.dependencies = Suppliers.memoize(() -> {
this.dependencies = SingletonSupplier.of(() -> {
var bootstrapDependencies = module.getBootstrapDependencies(modules,
bootstrapMode.getDepth());
@@ -89,7 +88,7 @@ public class ModuleTestExecution implements Iterable<ApplicationModule> {
}
}
public static java.util.function.Supplier<ModuleTestExecution> of(Class<?> type) {
public static Supplier<ModuleTestExecution> of(Class<?> type) {
return () -> {