25
25
import java .util .regex .Matcher ;
26
26
import java .util .regex .Pattern ;
27
27
28
+ import javax .annotation .Nullable ;
29
+
28
30
import com .diffplug .spotless .Formatter ;
29
31
import com .diffplug .spotless .FormatterFunc ;
30
32
import com .diffplug .spotless .FormatterStep ;
31
33
import com .diffplug .spotless .LineEnding ;
32
- import com .diffplug .spotless .SerializedFunction ;
33
34
34
35
import edu .umd .cs .findbugs .annotations .SuppressFBWarnings ;
35
36
@@ -81,10 +82,7 @@ private void assertRegexSet() {
81
82
/** Returns a step which will apply the given steps but preserve the content selected by the regex / openClose pair. */
82
83
public FormatterStep preserveWithin (List <FormatterStep > steps ) {
83
84
assertRegexSet ();
84
- return FormatterStep .createLazy (name ,
85
- () -> new PreserveWithin (regex , steps ),
86
- SerializedFunction .identity (),
87
- state -> FormatterFunc .Closeable .of (state .buildFormatter (), state ));
85
+ return new PreserveWithin (name , regex , steps );
88
86
}
89
87
90
88
/**
@@ -93,17 +91,14 @@ public FormatterStep preserveWithin(List<FormatterStep> steps) {
93
91
*/
94
92
public FormatterStep applyWithin (List <FormatterStep > steps ) {
95
93
assertRegexSet ();
96
- return FormatterStep .createLazy (name ,
97
- () -> new ApplyWithin (regex , steps ),
98
- SerializedFunction .identity (),
99
- state -> FormatterFunc .Closeable .of (state .buildFormatter (), state ));
94
+ return new ApplyWithin (name , regex , steps );
100
95
}
101
96
102
- static class ApplyWithin extends Apply implements FormatterFunc . Closeable . ResourceFuncNeedsFile < Formatter > {
97
+ static class ApplyWithin extends BaseStep {
103
98
private static final long serialVersionUID = 17061466531957339L ;
104
99
105
- ApplyWithin (Pattern regex , List <FormatterStep > steps ) {
106
- super (regex , steps );
100
+ ApplyWithin (String name , Pattern regex , List <FormatterStep > steps ) {
101
+ super (name , regex , steps );
107
102
}
108
103
109
104
@ Override
@@ -119,11 +114,11 @@ public String apply(Formatter formatter, String unix, File file) throws Exceptio
119
114
}
120
115
}
121
116
122
- static class PreserveWithin extends Apply implements FormatterFunc . Closeable . ResourceFuncNeedsFile < Formatter > {
117
+ static class PreserveWithin extends BaseStep {
123
118
private static final long serialVersionUID = -8676786492305178343L ;
124
119
125
- PreserveWithin (Pattern regex , List <FormatterStep > steps ) {
126
- super (regex , steps );
120
+ PreserveWithin (String name , Pattern regex , List <FormatterStep > steps ) {
121
+ super (name , regex , steps );
127
122
}
128
123
129
124
private void storeGroups (String unix ) {
@@ -144,15 +139,17 @@ public String apply(Formatter formatter, String unix, File file) throws Exceptio
144
139
}
145
140
146
141
@ SuppressFBWarnings ("SE_TRANSIENT_FIELD_NOT_RESTORED" )
147
- static class Apply implements Serializable {
142
+ public static abstract class BaseStep implements Serializable , FormatterStep , FormatterFunc .Closeable .ResourceFuncNeedsFile <Formatter > {
143
+ final String name ;
148
144
private static final long serialVersionUID = -2301848328356559915L ;
149
145
final Pattern regex ;
150
146
final List <FormatterStep > steps ;
151
147
152
148
transient ArrayList <String > groups = new ArrayList <>();
153
149
transient StringBuilder builderInternal ;
154
150
155
- public Apply (Pattern regex , List <FormatterStep > steps ) {
151
+ public BaseStep (String name , Pattern regex , List <FormatterStep > steps ) {
152
+ this .name = name ;
156
153
this .regex = regex ;
157
154
this .steps = steps ;
158
155
}
@@ -218,5 +215,43 @@ protected String assembleGroups(String unix) {
218
215
throw new Error ("An intermediate step removed a match of " + pattern );
219
216
}
220
217
}
218
+
219
+ @ Override
220
+ public String getName () {
221
+ return name ;
222
+ }
223
+
224
+ private transient Formatter formatter ;
225
+
226
+ @ Nullable
227
+ @ Override
228
+ public String format (String rawUnix , File file ) throws Exception {
229
+ if (formatter == null ) {
230
+ formatter = buildFormatter ();
231
+ }
232
+ return this .apply (formatter , rawUnix , file );
233
+ }
234
+
235
+ @ Override
236
+ public boolean equals (Object o ) {
237
+ if (this == o )
238
+ return true ;
239
+ if (o == null || getClass () != o .getClass ())
240
+ return false ;
241
+ BaseStep step = (BaseStep ) o ;
242
+ return name .equals (step .name ) && regex .pattern ().equals (step .regex .pattern ()) && regex .flags () == step .regex .flags () && steps .equals (step .steps );
243
+ }
244
+
245
+ @ Override
246
+ public int hashCode () {
247
+ return Objects .hash (name , regex .pattern (), regex .flags (), steps );
248
+ }
249
+
250
+ public void cleanup () {
251
+ if (formatter != null ) {
252
+ formatter .close ();
253
+ formatter = null ;
254
+ }
255
+ }
221
256
}
222
257
}
0 commit comments