1515 */
1616package 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-
2518import org .springframework .lang .Nullable ;
2619import org .springframework .util .Assert ;
27- import org .springframework .util .ClassUtils ;
28- import org .springframework .util .StringUtils ;
29- import org .springframework .util .function .SingletonSupplier ;
3020
3121import 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