Skip to content

Commit 438f17b

Browse files
committed
GH-813 - Introduce FormattableType.
As replacement for misspelled Formatable type.
1 parent 9d818b4 commit 438f17b

File tree

11 files changed

+250
-120
lines changed

11 files changed

+250
-120
lines changed

spring-modulith-core/src/main/java/org/springframework/modulith/core/ApplicationModule.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ public ArchitecturallyEvidentType getArchitecturallyEvidentType(Class<?> type) {
306306
return getType(type.getName())
307307
.map(it -> ArchitecturallyEvidentType.of(it, getSpringBeansInternal()))
308308
.orElseThrow(() -> new IllegalArgumentException("Couldn't find type %s in module %s!".formatted(
309-
FormatableType.of(type).getAbbreviatedFullName(this), getName())));
309+
FormattableType.of(type).getAbbreviatedFullName(this), getName())));
310310
}
311311

312312
/**
@@ -1267,8 +1267,8 @@ Violations isValidDependencyWithin(ApplicationModules modules) {
12671267

12681268
var violationText = INVALID_SUB_MODULE_REFERENCE
12691269
.formatted(originModule.getName(), targetModule.getName(),
1270-
FormatableType.of(source).getAbbreviatedFullName(originModule),
1271-
FormatableType.of(target).getAbbreviatedFullName(targetModule));
1270+
FormattableType.of(source).getAbbreviatedFullName(originModule),
1271+
FormattableType.of(target).getAbbreviatedFullName(targetModule));
12721272

12731273
return violations.and(new Violation(violationText));
12741274
}
@@ -1289,7 +1289,7 @@ ApplicationModule getExistingModuleOf(JavaClass javaClass, ApplicationModules mo
12891289
*/
12901290
@Override
12911291
public String toString() {
1292-
return type.format(FormatableType.of(source), FormatableType.of(target));
1292+
return type.format(FormattableType.of(source), FormattableType.of(target));
12931293
}
12941294

12951295
/*

spring-modulith-core/src/main/java/org/springframework/modulith/core/ArchitecturallyEvidentType.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public JavaClass getType() {
9999
* @return will never be {@literal null}.
100100
*/
101101
String getAbbreviatedFullName() {
102-
return FormatableType.of(getType()).getAbbreviatedFullName();
102+
return FormattableType.of(getType()).getAbbreviatedFullName();
103103
}
104104

105105
/**

spring-modulith-core/src/main/java/org/springframework/modulith/core/DependencyType.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,15 @@ public enum DependencyType {
5252
public String format(FormatableType source, FormatableType target) {
5353
return String.format("Component %s using %s", source.getAbbreviatedFullName(), target.getAbbreviatedFullName());
5454
}
55+
56+
/*
57+
* (non-Javadoc)
58+
* @see org.springframework.modulith.core.DependencyType#format(org.springframework.modulith.core.FormattableType, org.springframework.modulith.core.FormattableType)
59+
*/
60+
@Override
61+
public String format(FormattableType source, FormattableType target) {
62+
return String.format("Component %s using %s", source.getAbbreviatedFullName(), target.getAbbreviatedFullName());
63+
}
5564
},
5665

5766
/**
@@ -68,6 +77,16 @@ public String format(FormatableType source, FormatableType target) {
6877
return String.format("Entity %s depending on %s", source.getAbbreviatedFullName(),
6978
target.getAbbreviatedFullName());
7079
}
80+
81+
/*
82+
* (non-Javadoc)
83+
* @see org.springframework.modulith.core.DependencyType#format(org.springframework.modulith.core.FormattableType, org.springframework.modulith.core.FormattableType)
84+
*/
85+
@Override
86+
public String format(FormattableType source, FormattableType target) {
87+
return String.format("Entity %s depending on %s", source.getAbbreviatedFullName(),
88+
target.getAbbreviatedFullName());
89+
}
7190
},
7291

7392
/**
@@ -85,6 +104,16 @@ public String format(FormatableType source, FormatableType target) {
85104
return String.format("%s listening to events of type %s", source.getAbbreviatedFullName(),
86105
target.getAbbreviatedFullName());
87106
}
107+
108+
/*
109+
* (non-Javadoc)
110+
* @see org.springframework.modulith.core.DependencyType#format(org.springframework.modulith.core.FormattableType, org.springframework.modulith.core.FormattableType)
111+
*/
112+
@Override
113+
public String format(FormattableType source, FormattableType target) {
114+
return String.format("%s listening to events of type %s", source.getAbbreviatedFullName(),
115+
target.getAbbreviatedFullName());
116+
}
88117
},
89118

90119
DEFAULT {
@@ -106,6 +135,15 @@ public DependencyType defaultOr(Supplier<DependencyType> supplier) {
106135
public String format(FormatableType source, FormatableType target) {
107136
return String.format("%s depending on %s", source.getAbbreviatedFullName(), target.getAbbreviatedFullName());
108137
}
138+
139+
/*
140+
* (non-Javadoc)
141+
* @see org.springframework.modulith.core.DependencyType#format(org.springframework.modulith.core.FormattableType, org.springframework.modulith.core.FormattableType)
142+
*/
143+
@Override
144+
public String format(FormattableType source, FormattableType target) {
145+
return String.format("%s depending on %s", source.getAbbreviatedFullName(), target.getAbbreviatedFullName());
146+
}
109147
};
110148

111149
/**
@@ -137,8 +175,14 @@ static DependencyType forDependency(Dependency dependency) {
137175
return forParameter(dependency.getTargetClass());
138176
}
139177

178+
/**
179+
* @deprecated since 1.3, prefer {@link #format(FormattableType, FormattableType)}.
180+
*/
181+
@Deprecated
140182
public abstract String format(FormatableType source, FormatableType target);
141183

184+
public abstract String format(FormattableType source, FormattableType target);
185+
142186
/**
143187
* Returns all {@link DependencyType}s except the given ones.
144188
*

spring-modulith-core/src/main/java/org/springframework/modulith/core/FormatableType.java

Lines changed: 9 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -15,47 +15,19 @@
1515
*/
1616
package org.springframework.modulith.core;
1717

18-
import java.util.Map;
19-
import java.util.concurrent.ConcurrentHashMap;
20-
import java.util.function.Supplier;
21-
import java.util.stream.Collectors;
22-
import java.util.stream.Stream;
23-
import java.util.stream.StreamSupport;
24-
2518
import org.springframework.lang.Nullable;
2619
import org.springframework.util.Assert;
27-
import org.springframework.util.ClassUtils;
28-
import org.springframework.util.StringUtils;
29-
import org.springframework.util.function.SingletonSupplier;
3020

3121
import com.tngtech.archunit.core.domain.JavaClass;
3222

3323
/**
3424
* Wrapper around {@link JavaClass} that allows creating additional formatted names.
3525
*
3626
* @author Oliver Drotbohm
27+
* @deprecated since 1.3, use {@link FormattableType} instead.
3728
*/
38-
public class FormatableType {
39-
40-
private static final Map<String, FormatableType> CACHE = new ConcurrentHashMap<>();
41-
42-
private final String type;
43-
private final Supplier<String> abbreviatedName;
44-
45-
/**
46-
* Creates a new {@link FormatableType} for the given source {@link String} and lazily computed abbreviated name.
47-
*
48-
* @param type must not be {@literal null} or empty.
49-
* @param abbreviatedName must not be {@literal null}.
50-
*/
51-
private FormatableType(String type, Supplier<String> abbreviatedName) {
52-
53-
Assert.hasText(type, "Type string must not be null or empty!");
54-
Assert.notNull(abbreviatedName, "Computed abbreviated name must not be null!");
55-
56-
this.type = type;
57-
this.abbreviatedName = abbreviatedName;
58-
}
29+
@Deprecated
30+
public abstract class FormatableType {
5931

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

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

70-
return CACHE.computeIfAbsent(type.getName(), FormatableType::new);
42+
return FormattableType.of(type);
7143
}
7244

7345
/**
@@ -77,7 +49,7 @@ public static FormatableType of(JavaClass type) {
7749
* @return will never be {@literal null}.
7850
*/
7951
public static FormatableType of(Class<?> type) {
80-
return CACHE.computeIfAbsent(type.getName(), FormatableType::new);
52+
return FormattableType.of(type);
8153
}
8254

8355
/**
@@ -87,35 +59,7 @@ public static FormatableType of(Class<?> type) {
8759
* @return will never be {@literal null}.
8860
*/
8961
public static String format(Iterable<JavaClass> types) {
90-
91-
Assert.notNull(types, "Types must not be null!");
92-
93-
return StreamSupport.stream(types.spliterator(), false)
94-
.map(FormatableType::of)
95-
.map(FormatableType::getAbbreviatedFullName)
96-
.collect(Collectors.joining(", "));
97-
}
98-
99-
/**
100-
* Creates a new {@link FormatableType} for the given fully-qualified type name.
101-
*
102-
* @param type must not be {@literal null} or empty.
103-
*/
104-
private FormatableType(String type) {
105-
106-
Assert.hasText(type, "Type must not be null or empty!");
107-
108-
this.type = type;
109-
this.abbreviatedName = SingletonSupplier.of(() -> {
110-
111-
String abbreviatedPackage = Stream //
112-
.of(ClassUtils.getPackageName(type).split("\\.")) //
113-
.map(it -> it.substring(0, 1)) //
114-
.collect(Collectors.joining("."));
115-
116-
return abbreviatedPackage.concat(".") //
117-
.concat(ClassUtils.getShortName(getFullName()));
118-
});
62+
return FormattableType.format(types);
11963
}
12064

12165
/**
@@ -124,9 +68,7 @@ private FormatableType(String type) {
12468
*
12569
* @return will never be {@literal null}.
12670
*/
127-
public String getAbbreviatedFullName() {
128-
return abbreviatedName.get();
129-
}
71+
public abstract String getAbbreviatedFullName();
13072

13173
/**
13274
* Returns the abbreviated full name of the type abbreviating only the part of the given {@link ApplicationModule}'s
@@ -135,48 +77,12 @@ public String getAbbreviatedFullName() {
13577
* @param module can be {@literal null}.
13678
* @return will never be {@literal null}.
13779
*/
138-
public String getAbbreviatedFullName(@Nullable ApplicationModule module) {
139-
140-
if (module == null) {
141-
return getAbbreviatedFullName();
142-
}
143-
144-
String basePackageName = module.getBasePackage().getName();
145-
146-
if (!StringUtils.hasText(basePackageName)) {
147-
return getAbbreviatedFullName();
148-
}
149-
150-
String typePackageName = ClassUtils.getPackageName(type);
151-
152-
if (basePackageName.equals(typePackageName)) {
153-
return getAbbreviatedFullName();
154-
}
155-
156-
if (!typePackageName.startsWith(basePackageName)) {
157-
return getFullName();
158-
}
159-
160-
return abbreviate(basePackageName) //
161-
.concat(typePackageName.substring(basePackageName.length())) //
162-
.concat(".") //
163-
.concat(ClassUtils.getShortName(getFullName()));
164-
}
80+
public abstract String getAbbreviatedFullName(@Nullable ApplicationModule module);
16581

16682
/**
16783
* Returns the type's full name.
16884
*
16985
* @return will never be {@literal null}.
17086
*/
171-
public String getFullName() {
172-
return type.replace("$", ".");
173-
}
174-
175-
private static String abbreviate(String source) {
176-
177-
return Stream //
178-
.of(source.split("\\.")) //
179-
.map(it -> it.substring(0, 1)) //
180-
.collect(Collectors.joining("."));
181-
}
87+
public abstract String getFullName();
18288
}

0 commit comments

Comments
 (0)