Skip to content

Commit d006421

Browse files
authored
Merge pull request #226 from openjavaformat/abashev/Make-Main-easier-to-read
Make Main easier to read
2 parents 1b71438 + 5f28292 commit d006421

1 file changed

Lines changed: 22 additions & 13 deletions

File tree

  • open-java-format/src/main/java/com/palantir/javaformat/java

‎open-java-format/src/main/java/com/palantir/javaformat/java/Main.java‎

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package com.palantir.javaformat.java;
1616

1717
import static java.nio.charset.StandardCharsets.UTF_8;
18+
import static java.util.Optional.ofNullable;
1819

1920
import com.google.common.io.ByteStreams;
2021
import com.palantir.javaformat.java.JavaFormatterOptions.Style;
@@ -30,7 +31,6 @@
3031
import java.util.LinkedHashMap;
3132
import java.util.Map;
3233
import java.util.Objects;
33-
import java.util.Optional;
3434
import java.util.concurrent.ExecutionException;
3535
import java.util.concurrent.ExecutorService;
3636
import java.util.concurrent.Executors;
@@ -96,12 +96,14 @@ public static void main(String[] args) {
9696
*
9797
* @param args the command-line arguments
9898
*/
99-
public int format(String... args) throws UsageException {
99+
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
}
@@ -144,7 +146,7 @@ private int formatFiles(
144146
Path path = Paths.get(fileName);
145147
String input;
146148
try {
147-
input = new String(Files.readAllBytes(path), UTF_8);
149+
input = Files.readString(path);
148150
inputs.put(path, input);
149151
results.put(path, executorService.submit(new FormatFileCallable(parameters, input, options)));
150152
} catch (IOException e) {
@@ -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) {
@@ -245,7 +251,7 @@ private static int exitCode(boolean allOk, boolean anyChanged) {
245251
}
246252

247253
/** Parses and validates command-line flags. */
248-
public static CommandLineOptions processArgs(String... args) throws UsageException {
254+
static CommandLineOptions processArgs(String... args) throws UsageException {
249255
CommandLineOptions parameters;
250256
try {
251257
parameters = CommandLineOptionsParser.parse(Arrays.asList(args));
@@ -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

Comments
 (0)