33
33
import com .diffplug .spotless .FormatterStep ;
34
34
import com .diffplug .spotless .JarState ;
35
35
import com .diffplug .spotless .Provisioner ;
36
- import com .diffplug .spotless .ThrowingEx ;
36
+ import com .diffplug .spotless .SerializedFunction ;
37
37
38
38
/**
39
39
* Generic Eclipse based formatter step {@link State} builder.
40
40
*/
41
41
public class EclipseBasedStepBuilder {
42
42
private final String formatterName ;
43
43
private final String formatterStepExt ;
44
- private final ThrowingEx . Function <State , FormatterFunc > stateToFormatter ;
44
+ private final SerializedFunction <State , FormatterFunc > stateToFormatter ;
45
45
private final Provisioner jarProvisioner ;
46
46
private String formatterVersion ;
47
47
@@ -63,12 +63,12 @@ public class EclipseBasedStepBuilder {
63
63
private Iterable <File > settingsFiles = new ArrayList <>();
64
64
65
65
/** Initialize valid default configuration, taking latest version */
66
- public EclipseBasedStepBuilder (String formatterName , Provisioner jarProvisioner , ThrowingEx . Function <State , FormatterFunc > stateToFormatter ) {
66
+ public EclipseBasedStepBuilder (String formatterName , Provisioner jarProvisioner , SerializedFunction <State , FormatterFunc > stateToFormatter ) {
67
67
this (formatterName , "" , jarProvisioner , stateToFormatter );
68
68
}
69
69
70
70
/** Initialize valid default configuration, taking latest version */
71
- public EclipseBasedStepBuilder (String formatterName , String formatterStepExt , Provisioner jarProvisioner , ThrowingEx . Function <State , FormatterFunc > stateToFormatter ) {
71
+ public EclipseBasedStepBuilder (String formatterName , String formatterStepExt , Provisioner jarProvisioner , SerializedFunction <State , FormatterFunc > stateToFormatter ) {
72
72
this .formatterName = Objects .requireNonNull (formatterName , "formatterName" );
73
73
this .formatterStepExt = Objects .requireNonNull (formatterStepExt , "formatterStepExt" );
74
74
this .jarProvisioner = Objects .requireNonNull (jarProvisioner , "jarProvisioner" );
@@ -78,7 +78,11 @@ public EclipseBasedStepBuilder(String formatterName, String formatterStepExt, Pr
78
78
79
79
/** Returns the FormatterStep (whose state will be calculated lazily). */
80
80
public FormatterStep build () {
81
- return FormatterStep .createLazy (formatterName + formatterStepExt , this ::get , stateToFormatter );
81
+ var roundtrippableState = new EclipseStep (formatterVersion , formatterStepExt , FileSignature .promise (settingsFiles ), JarState .promise (() -> {
82
+ return JarState .withoutTransitives (dependencies , jarProvisioner );
83
+ }));
84
+ return FormatterStep .create (formatterName + formatterStepExt , roundtrippableState ,
85
+ EclipseStep ::state , stateToFormatter );
82
86
}
83
87
84
88
/** Set dependencies for the corresponding Eclipse version */
@@ -122,21 +126,23 @@ public void setPreferences(Iterable<File> settingsFiles) {
122
126
this .settingsFiles = settingsFiles ;
123
127
}
124
128
125
- /** Creates the state of the configuration. */
126
- EclipseBasedStepBuilder .State get () throws IOException {
127
- /*
128
- * The current use case is tailored for Gradle.
129
- * Gradle calls this method only once per execution
130
- * and compares the State with the one of a previous run
131
- * for incremental building.
132
- * Hence a lazy construction is not required.
133
- */
134
- return new State (
135
- formatterVersion ,
136
- formatterStepExt ,
137
- jarProvisioner ,
138
- dependencies ,
139
- settingsFiles );
129
+ static class EclipseStep implements Serializable {
130
+ private static final long serialVersionUID = 1 ;
131
+ private final String semanticVersion ;
132
+ private final String formatterStepExt ;
133
+ private final FileSignature .Promised settingsPromise ;
134
+ private final JarState .Promised jarPromise ;
135
+
136
+ EclipseStep (String semanticVersion , String formatterStepExt , FileSignature .Promised settingsPromise , JarState .Promised jarPromise ) {
137
+ this .semanticVersion = semanticVersion ;
138
+ this .formatterStepExt = formatterStepExt ;
139
+ this .settingsPromise = settingsPromise ;
140
+ this .jarPromise = jarPromise ;
141
+ }
142
+
143
+ private State state () {
144
+ return new State (semanticVersion , formatterStepExt , jarPromise .get (), settingsPromise .get ());
145
+ }
140
146
}
141
147
142
148
/**
@@ -155,9 +161,9 @@ public static class State implements Serializable {
155
161
private final FileSignature settingsFiles ;
156
162
157
163
/** State constructor expects that all passed items are not modified afterwards */
158
- protected State (String formatterVersion , String formatterStepExt , Provisioner jarProvisioner , List < String > dependencies , Iterable < File > settingsFiles ) throws IOException {
159
- this .jarState = JarState . withoutTransitives ( dependencies , jarProvisioner ) ;
160
- this .settingsFiles = FileSignature . signAsList ( settingsFiles ) ;
164
+ protected State (String formatterVersion , String formatterStepExt , JarState jarState , FileSignature settingsFiles ) {
165
+ this .jarState = jarState ;
166
+ this .settingsFiles = settingsFiles ;
161
167
this .formatterStepExt = formatterStepExt ;
162
168
semanticVersion = convertEclipseVersion (formatterVersion );
163
169
}
0 commit comments