Skip to content

Commit

Permalink
GH-813 - Introduce FormattableType.
Browse files Browse the repository at this point in the history
As replacement for misspelled Formatable type.
  • Loading branch information
odrotbohm committed Sep 20, 2024
1 parent 9d818b4 commit 438f17b
Show file tree
Hide file tree
Showing 11 changed files with 250 additions and 120 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ public ArchitecturallyEvidentType getArchitecturallyEvidentType(Class<?> type) {
return getType(type.getName())
.map(it -> ArchitecturallyEvidentType.of(it, getSpringBeansInternal()))
.orElseThrow(() -> new IllegalArgumentException("Couldn't find type %s in module %s!".formatted(
FormatableType.of(type).getAbbreviatedFullName(this), getName())));
FormattableType.of(type).getAbbreviatedFullName(this), getName())));
}

/**
Expand Down Expand Up @@ -1267,8 +1267,8 @@ Violations isValidDependencyWithin(ApplicationModules modules) {

var violationText = INVALID_SUB_MODULE_REFERENCE
.formatted(originModule.getName(), targetModule.getName(),
FormatableType.of(source).getAbbreviatedFullName(originModule),
FormatableType.of(target).getAbbreviatedFullName(targetModule));
FormattableType.of(source).getAbbreviatedFullName(originModule),
FormattableType.of(target).getAbbreviatedFullName(targetModule));

return violations.and(new Violation(violationText));
}
Expand All @@ -1289,7 +1289,7 @@ ApplicationModule getExistingModuleOf(JavaClass javaClass, ApplicationModules mo
*/
@Override
public String toString() {
return type.format(FormatableType.of(source), FormatableType.of(target));
return type.format(FormattableType.of(source), FormattableType.of(target));
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public JavaClass getType() {
* @return will never be {@literal null}.
*/
String getAbbreviatedFullName() {
return FormatableType.of(getType()).getAbbreviatedFullName();
return FormattableType.of(getType()).getAbbreviatedFullName();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,15 @@ public enum DependencyType {
public String format(FormatableType source, FormatableType target) {
return String.format("Component %s using %s", source.getAbbreviatedFullName(), target.getAbbreviatedFullName());
}

/*
* (non-Javadoc)
* @see org.springframework.modulith.core.DependencyType#format(org.springframework.modulith.core.FormattableType, org.springframework.modulith.core.FormattableType)
*/
@Override
public String format(FormattableType source, FormattableType target) {
return String.format("Component %s using %s", source.getAbbreviatedFullName(), target.getAbbreviatedFullName());
}
},

/**
Expand All @@ -68,6 +77,16 @@ public String format(FormatableType source, FormatableType target) {
return String.format("Entity %s depending on %s", source.getAbbreviatedFullName(),
target.getAbbreviatedFullName());
}

/*
* (non-Javadoc)
* @see org.springframework.modulith.core.DependencyType#format(org.springframework.modulith.core.FormattableType, org.springframework.modulith.core.FormattableType)
*/
@Override
public String format(FormattableType source, FormattableType target) {
return String.format("Entity %s depending on %s", source.getAbbreviatedFullName(),
target.getAbbreviatedFullName());
}
},

/**
Expand All @@ -85,6 +104,16 @@ public String format(FormatableType source, FormatableType target) {
return String.format("%s listening to events of type %s", source.getAbbreviatedFullName(),
target.getAbbreviatedFullName());
}

/*
* (non-Javadoc)
* @see org.springframework.modulith.core.DependencyType#format(org.springframework.modulith.core.FormattableType, org.springframework.modulith.core.FormattableType)
*/
@Override
public String format(FormattableType source, FormattableType target) {
return String.format("%s listening to events of type %s", source.getAbbreviatedFullName(),
target.getAbbreviatedFullName());
}
},

DEFAULT {
Expand All @@ -106,6 +135,15 @@ public DependencyType defaultOr(Supplier<DependencyType> supplier) {
public String format(FormatableType source, FormatableType target) {
return String.format("%s depending on %s", source.getAbbreviatedFullName(), target.getAbbreviatedFullName());
}

/*
* (non-Javadoc)
* @see org.springframework.modulith.core.DependencyType#format(org.springframework.modulith.core.FormattableType, org.springframework.modulith.core.FormattableType)
*/
@Override
public String format(FormattableType source, FormattableType target) {
return String.format("%s depending on %s", source.getAbbreviatedFullName(), target.getAbbreviatedFullName());
}
};

/**
Expand Down Expand Up @@ -137,8 +175,14 @@ static DependencyType forDependency(Dependency dependency) {
return forParameter(dependency.getTargetClass());
}

/**
* @deprecated since 1.3, prefer {@link #format(FormattableType, FormattableType)}.
*/
@Deprecated
public abstract String format(FormatableType source, FormatableType target);

public abstract String format(FormattableType source, FormattableType target);

/**
* Returns all {@link DependencyType}s except the given ones.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,47 +15,19 @@
*/
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;

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;

/**
* Wrapper around {@link JavaClass} that allows creating additional formatted names.
*
* @author Oliver Drotbohm
* @deprecated since 1.3, use {@link FormattableType} instead.
*/
public class FormatableType {

private static final Map<String, FormatableType> CACHE = new ConcurrentHashMap<>();

private final String type;
private final Supplier<String> abbreviatedName;

/**
* Creates a new {@link FormatableType} for the given source {@link String} and lazily computed abbreviated name.
*
* @param type must not be {@literal null} or empty.
* @param abbreviatedName must not be {@literal null}.
*/
private FormatableType(String type, Supplier<String> abbreviatedName) {

Assert.hasText(type, "Type string must not be null or empty!");
Assert.notNull(abbreviatedName, "Computed abbreviated name must not be null!");

this.type = type;
this.abbreviatedName = abbreviatedName;
}
@Deprecated
public abstract class FormatableType {

/**
* Creates a new {@link FormatableType} for the given {@link JavaClass}.
Expand All @@ -67,7 +39,7 @@ public static FormatableType of(JavaClass type) {

Assert.notNull(type, "JavaClass must not be null!");

return CACHE.computeIfAbsent(type.getName(), FormatableType::new);
return FormattableType.of(type);
}

/**
Expand All @@ -77,7 +49,7 @@ public static FormatableType of(JavaClass type) {
* @return will never be {@literal null}.
*/
public static FormatableType of(Class<?> type) {
return CACHE.computeIfAbsent(type.getName(), FormatableType::new);
return FormattableType.of(type);
}

/**
Expand All @@ -87,35 +59,7 @@ public static FormatableType of(Class<?> type) {
* @return will never be {@literal null}.
*/
public static String format(Iterable<JavaClass> types) {

Assert.notNull(types, "Types must not be null!");

return StreamSupport.stream(types.spliterator(), false)
.map(FormatableType::of)
.map(FormatableType::getAbbreviatedFullName)
.collect(Collectors.joining(", "));
}

/**
* Creates a new {@link FormatableType} for the given fully-qualified type name.
*
* @param type must not be {@literal null} or empty.
*/
private FormatableType(String type) {

Assert.hasText(type, "Type must not be null or empty!");

this.type = type;
this.abbreviatedName = SingletonSupplier.of(() -> {

String abbreviatedPackage = Stream //
.of(ClassUtils.getPackageName(type).split("\\.")) //
.map(it -> it.substring(0, 1)) //
.collect(Collectors.joining("."));

return abbreviatedPackage.concat(".") //
.concat(ClassUtils.getShortName(getFullName()));
});
return FormattableType.format(types);
}

/**
Expand All @@ -124,9 +68,7 @@ private FormatableType(String type) {
*
* @return will never be {@literal null}.
*/
public String getAbbreviatedFullName() {
return abbreviatedName.get();
}
public abstract String getAbbreviatedFullName();

/**
* Returns the abbreviated full name of the type abbreviating only the part of the given {@link ApplicationModule}'s
Expand All @@ -135,48 +77,12 @@ public String getAbbreviatedFullName() {
* @param module can be {@literal null}.
* @return will never be {@literal null}.
*/
public String getAbbreviatedFullName(@Nullable ApplicationModule module) {

if (module == null) {
return getAbbreviatedFullName();
}

String basePackageName = module.getBasePackage().getName();

if (!StringUtils.hasText(basePackageName)) {
return getAbbreviatedFullName();
}

String typePackageName = ClassUtils.getPackageName(type);

if (basePackageName.equals(typePackageName)) {
return getAbbreviatedFullName();
}

if (!typePackageName.startsWith(basePackageName)) {
return getFullName();
}

return abbreviate(basePackageName) //
.concat(typePackageName.substring(basePackageName.length())) //
.concat(".") //
.concat(ClassUtils.getShortName(getFullName()));
}
public abstract String getAbbreviatedFullName(@Nullable ApplicationModule module);

/**
* Returns the type's full name.
*
* @return will never be {@literal null}.
*/
public String getFullName() {
return type.replace("$", ".");
}

private static String abbreviate(String source) {

return Stream //
.of(source.split("\\.")) //
.map(it -> it.substring(0, 1)) //
.collect(Collectors.joining("."));
}
public abstract String getFullName();
}
Loading

0 comments on commit 438f17b

Please sign in to comment.