15
15
*/
16
16
package com .diffplug .spotless ;
17
17
18
+ import static com .diffplug .spotless .LibPreconditions .requireElementsNonNull ;
19
+
18
20
import java .io .File ;
19
21
import java .io .IOException ;
20
22
import java .io .ObjectInputStream ;
@@ -47,7 +49,7 @@ private Formatter(LineEnding.Policy lineEndingsPolicy, Charset encoding, Path ro
47
49
this .lineEndingsPolicy = Objects .requireNonNull (lineEndingsPolicy , "lineEndingsPolicy" );
48
50
this .encoding = Objects .requireNonNull (encoding , "encoding" );
49
51
this .rootDir = Objects .requireNonNull (rootDirectory , "rootDir" );
50
- this .steps = new ArrayList <>(Objects . requireNonNull ( steps , "steps" ));
52
+ this .steps = requireElementsNonNull ( new ArrayList <>(steps ));
51
53
this .exceptionPolicy = Objects .requireNonNull (exceptionPolicy , "exceptionPolicy" );
52
54
}
53
55
@@ -143,6 +145,8 @@ public Formatter build() {
143
145
144
146
/** Returns true iff the given file's formatting is up-to-date. */
145
147
public boolean isClean (File file ) throws IOException {
148
+ Objects .requireNonNull (file );
149
+
146
150
String raw = new String (Files .readAllBytes (file .toPath ()), encoding );
147
151
String unix = LineEnding .toUnix (raw );
148
152
@@ -178,6 +182,8 @@ public void applyTo(File file) throws IOException {
178
182
* formatted result with unix newlines if it was not.
179
183
*/
180
184
public @ Nullable String applyToAndReturnResultIfDirty (File file ) throws IOException {
185
+ Objects .requireNonNull (file );
186
+
181
187
byte [] rawBytes = Files .readAllBytes (file .toPath ());
182
188
String raw = new String (rawBytes , encoding );
183
189
String rawUnix = LineEnding .toUnix (raw );
@@ -199,6 +205,9 @@ public void applyTo(File file) throws IOException {
199
205
200
206
/** Applies the appropriate line endings to the given unix content. */
201
207
public String computeLineEndings (String unix , File file ) {
208
+ Objects .requireNonNull (unix , "unix" );
209
+ Objects .requireNonNull (file , "file" );
210
+
202
211
String ending = lineEndingsPolicy .getEndingFor (file );
203
212
if (!ending .equals (LineEnding .UNIX .str ())) {
204
213
return unix .replace (LineEnding .UNIX .str (), ending );
@@ -213,6 +222,9 @@ public String computeLineEndings(String unix, File file) {
213
222
* is guaranteed to also have unix line endings.
214
223
*/
215
224
public String compute (String unix , File file ) {
225
+ Objects .requireNonNull (unix , "unix" );
226
+ Objects .requireNonNull (file , "file" );
227
+
216
228
for (FormatterStep step : steps ) {
217
229
try {
218
230
String formatted = step .format (unix , file );
0 commit comments