15
15
*/
16
16
package org .springframework .modulith .core ;
17
17
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
-
25
18
import org .springframework .lang .Nullable ;
26
19
import org .springframework .util .Assert ;
27
- import org .springframework .util .ClassUtils ;
28
- import org .springframework .util .StringUtils ;
29
- import org .springframework .util .function .SingletonSupplier ;
30
20
31
21
import com .tngtech .archunit .core .domain .JavaClass ;
32
22
33
23
/**
34
24
* Wrapper around {@link JavaClass} that allows creating additional formatted names.
35
25
*
36
26
* @author Oliver Drotbohm
27
+ * @deprecated since 1.3, use {@link FormattableType} instead.
37
28
*/
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 {
59
31
60
32
/**
61
33
* Creates a new {@link FormatableType} for the given {@link JavaClass}.
@@ -67,7 +39,7 @@ public static FormatableType of(JavaClass type) {
67
39
68
40
Assert .notNull (type , "JavaClass must not be null!" );
69
41
70
- return CACHE . computeIfAbsent (type . getName (), FormatableType :: new );
42
+ return FormattableType . of (type );
71
43
}
72
44
73
45
/**
@@ -77,7 +49,7 @@ public static FormatableType of(JavaClass type) {
77
49
* @return will never be {@literal null}.
78
50
*/
79
51
public static FormatableType of (Class <?> type ) {
80
- return CACHE . computeIfAbsent (type . getName (), FormatableType :: new );
52
+ return FormattableType . of (type );
81
53
}
82
54
83
55
/**
@@ -87,35 +59,7 @@ public static FormatableType of(Class<?> type) {
87
59
* @return will never be {@literal null}.
88
60
*/
89
61
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 );
119
63
}
120
64
121
65
/**
@@ -124,9 +68,7 @@ private FormatableType(String type) {
124
68
*
125
69
* @return will never be {@literal null}.
126
70
*/
127
- public String getAbbreviatedFullName () {
128
- return abbreviatedName .get ();
129
- }
71
+ public abstract String getAbbreviatedFullName ();
130
72
131
73
/**
132
74
* 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() {
135
77
* @param module can be {@literal null}.
136
78
* @return will never be {@literal null}.
137
79
*/
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 );
165
81
166
82
/**
167
83
* Returns the type's full name.
168
84
*
169
85
* @return will never be {@literal null}.
170
86
*/
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 ();
182
88
}
0 commit comments