1515package com .palantir .javaformat .java ;
1616
1717import static java .nio .charset .StandardCharsets .UTF_8 ;
18+ import static java .util .Optional .ofNullable ;
1819
1920import com .google .common .io .ByteStreams ;
2021import com .palantir .javaformat .java .JavaFormatterOptions .Style ;
3031import java .util .LinkedHashMap ;
3132import java .util .Map ;
3233import java .util .Objects ;
33- import java .util .Optional ;
3434import java .util .concurrent .ExecutionException ;
3535import java .util .concurrent .ExecutorService ;
3636import java .util .concurrent .Executors ;
@@ -98,10 +98,12 @@ public static void main(String[] args) {
9898 */
9999 public int format (String ... args ) throws UsageException {
100100 CommandLineOptions parameters = processArgs (args );
101+
101102 if (parameters .version ()) {
102103 errWriter .println (versionString ());
103104 return 0 ;
104105 }
106+
105107 if (parameters .help ()) {
106108 throw new UsageException ();
107109 }
@@ -153,31 +155,35 @@ private int formatFiles(
153155 }
154156 }
155157
156- for (Map . Entry < Path , Future < String >> result : results .entrySet ()) {
158+ for (var result : results .entrySet ()) {
157159 Path path = result .getKey ();
158160 String formatted ;
161+
159162 try {
160163 formatted = result .getValue ().get ();
161164 } catch (InterruptedException e ) {
162165 errWriter .println (e .getMessage ());
163166 allOk = false ;
164167 continue ;
165168 } catch (ExecutionException e ) {
166- if (e .getCause () instanceof FormatterException ) {
167- for (FormatterDiagnostic diagnostic : ((FormatterException ) e .getCause ()).diagnostics ()) {
169+ var cause = e .getCause ();
170+
171+ if (cause instanceof FormatterException formatterException ) {
172+ for (FormatterDiagnostic diagnostic : formatterException .diagnostics ()) {
168173 errWriter .println (path + ":" + diagnostic );
169174 }
170175 } else {
171176 errWriter .println (path + ": error: "
172- + Optional .ofNullable (e .getCause ())
173- .map (Throwable ::getMessage )
174- .orElse ("null" ));
175- Optional .ofNullable (e .getCause ()).ifPresent (cause -> cause .printStackTrace (errWriter ));
177+ + ofNullable (cause ).map (Throwable ::getMessage ).orElse ("null" ));
178+
179+ ofNullable (cause ).ifPresent (c -> c .printStackTrace (errWriter ));
176180 }
177181 allOk = false ;
178182 continue ;
179183 }
184+
180185 boolean changed = !formatted .equals (inputs .get (path ));
186+
181187 if (changed && parameters .setExitIfChanged ()) {
182188 anyChanged = true ;
183189 }
@@ -186,11 +192,11 @@ private int formatFiles(
186192 continue ; // preserve original file
187193 }
188194 try {
189- Files .write (path , formatted . getBytes ( UTF_8 ) );
195+ Files .writeString (path , formatted );
190196 } catch (IOException e ) {
191197 errWriter .println (path + ": could not write file: " + e .getMessage ());
198+
192199 allOk = false ;
193- continue ;
194200 }
195201 } else if (parameters .dryRun ()) {
196202 if (changed ) {
@@ -255,7 +261,9 @@ public static CommandLineOptions processArgs(String... args) throws UsageExcepti
255261 t .printStackTrace ();
256262 throw new UsageException (Objects .requireNonNullElse (t .getMessage (), "null" ));
257263 }
264+
258265 int filesToFormat = parameters .files ().size ();
266+
259267 if (parameters .stdin ()) {
260268 filesToFormat ++;
261269 }
@@ -281,6 +289,7 @@ public static CommandLineOptions processArgs(String... args) throws UsageExcepti
281289 if (parameters .dryRun () && parameters .inPlace ()) {
282290 throw new UsageException ("cannot use --dry-run and --replace at the same time" );
283291 }
292+
284293 return parameters ;
285294 }
286295}
0 commit comments