Skip to content

Commit b0a1b92

Browse files
author
Vincent Potucek
committed
Can be replaced with String.repeat()
1 parent 18f0eb8 commit b0a1b92

File tree

8 files changed

+103
-110
lines changed

8 files changed

+103
-110
lines changed

lib-extra/src/groovy/java/com/diffplug/spotless/extra/glue/groovy/GrEclipseFormatterStepImpl.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*/
1616
package com.diffplug.spotless.extra.glue.groovy;
1717

18+
import static java.lang.System.lineSeparator;
19+
1820
import java.io.ByteArrayInputStream;
1921
import java.io.ByteArrayOutputStream;
2022
import java.io.IOException;
@@ -143,7 +145,7 @@ public String toString() {
143145
}
144146
for (Throwable error : errors) {
145147
error.printStackTrace();
146-
string.append(System.lineSeparator());
148+
string.append(lineSeparator());
147149
string.append(error.getMessage());
148150
}
149151

lib-extra/src/jdt/java/com/diffplug/spotless/extra/glue/jdt/DefaultJavaElementComparator.java

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -347,28 +347,14 @@ public int compare(BodyDeclaration bodyDeclaration1, BodyDeclaration bodyDeclara
347347
}
348348

349349
private static int sortPreservedCategory(int category) {
350-
switch (category) {
351-
case STATIC_FIELDS_INDEX:
352-
case STATIC_INIT_INDEX:
353-
return STATIC_FIELDS_INDEX;
354-
case FIELDS_INDEX:
355-
case INIT_INDEX:
356-
return FIELDS_INDEX;
357-
default:
358-
return category;
359-
}
350+
return switch (category) {
351+
case STATIC_FIELDS_INDEX, STATIC_INIT_INDEX -> STATIC_FIELDS_INDEX;
352+
case FIELDS_INDEX, INIT_INDEX -> FIELDS_INDEX;
353+
default -> category;
354+
};
360355
}
361356

362-
private boolean isSortPreserved(BodyDeclaration bodyDeclaration) {
363-
switch (bodyDeclaration.getNodeType()) {
364-
case ASTNode.FIELD_DECLARATION:
365-
case ASTNode.ENUM_CONSTANT_DECLARATION:
366-
case ASTNode.INITIALIZER:
367-
return true;
368-
default:
369-
return false;
370-
}
371-
}
357+
private boolean isSortPreserved(BodyDeclaration bodyDeclaration) {return switch(bodyDeclaration.getNodeType()){case ASTNode.FIELD_DECLARATION,ASTNode.ENUM_CONSTANT_DECLARATION,ASTNode.INITIALIZER->true;default->false;};}
372358

373359
private int preserveRelativeOrder(BodyDeclaration bodyDeclaration1, BodyDeclaration bodyDeclaration2) {
374360
int value1 = (Integer) bodyDeclaration1.getProperty(CompilationUnitSorter.RELATIVE_ORDER);

lib-extra/src/main/java/com/diffplug/spotless/extra/GitAttributesLineEndings.java

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -300,15 +300,14 @@ public String getEndingFor(File file) {
300300
}
301301

302302
private static String convertEolToLineEnding(String eol, File file) {
303-
switch (eol.toLowerCase(Locale.ROOT)) {
304-
case "lf":
305-
return LineEnding.UNIX.str();
306-
case "crlf":
307-
return LineEnding.WINDOWS.str();
308-
default:
309-
LOGGER.warn(".gitattributes file has unspecified eol value: {} for {}, defaulting to platform native", eol, file);
310-
return LineEnding.PLATFORM_NATIVE.str();
311-
}
303+
return switch (eol.toLowerCase(Locale.ROOT)) {
304+
case "lf" -> LineEnding.UNIX.str();
305+
case "crlf" -> LineEnding.WINDOWS.str();
306+
default -> {
307+
LOGGER.warn(".gitattributes file has unspecified eol value: {} for {}, defaulting to platform native", eol, file);
308+
yield LineEnding.PLATFORM_NATIVE.str();
309+
}
310+
};
312311
}
313312

314313
private LineEnding findDefaultLineEnding(Config config) {
@@ -335,14 +334,11 @@ private LineEnding findDefaultLineEnding(Config config) {
335334

336335
/** Creates a LineEnding from an EOL. */
337336
private static LineEnding fromEol(EOL eol) {
338-
// @formatter:off
339-
switch (eol) {
340-
case CRLF: return LineEnding.WINDOWS;
341-
case LF: return LineEnding.UNIX;
342-
case NATIVE: return LineEnding.PLATFORM_NATIVE;
343-
default: throw new IllegalArgumentException("Unknown eol " + eol);
344-
}
345-
// @formatter:on
337+
return switch (eol) {
338+
case CRLF -> LineEnding.WINDOWS;
339+
case LF -> LineEnding.UNIX;
340+
case NATIVE -> LineEnding.PLATFORM_NATIVE;
341+
};
346342
}
347343
}
348344

lib/src/main/java/com/diffplug/spotless/Jvm.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*/
1616
package com.diffplug.spotless;
1717

18+
import static java.lang.System.lineSeparator;
19+
1820
import java.io.File;
1921
import java.util.Arrays;
2022
import java.util.Comparator;
@@ -253,7 +255,7 @@ private String buildUpgradeFormatterMessage(V fmtVersion) {
253255
public String toString() {
254256
return "%s alternatives:%n".formatted(fmtName)
255257
+ jvm2fmtMaxVersion.entrySet().stream().map(
256-
e -> "- Version %s requires JVM %d+".formatted(e.getValue(), e.getKey())).collect(Collectors.joining(System.lineSeparator()));
258+
e -> "- Version %s requires JVM %d+".formatted(e.getValue(), e.getKey())).collect(Collectors.joining(lineSeparator()));
257259
}
258260

259261
@SuppressFBWarnings("SE_COMPARATOR_SHOULD_BE_SERIALIZABLE")

lib/src/main/java/com/diffplug/spotless/LineEnding.java

Lines changed: 70 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*/
1616
package com.diffplug.spotless;
1717

18+
import static java.lang.System.lineSeparator;
19+
1820
import java.io.File;
1921
import java.io.FileReader;
2022
import java.io.IOException;
@@ -30,19 +32,20 @@
3032
* Represents the line endings which should be written by the tool.
3133
*/
3234
public enum LineEnding {
33-
// @formatter:off
3435
/** Uses the same line endings as Git, using {@code .gitattributes} and the {@code core.eol} property. */
3536
GIT_ATTRIBUTES {
3637
/** .gitattributes is path-specific, so you must use {@link LineEnding#createPolicy(File, Supplier)}. */
37-
@Override @Deprecated
38+
@Override
39+
@Deprecated
3840
public Policy createPolicy() {
3941
return super.createPolicy();
4042
}
4143
},
4244
/** Uses the same line endings as Git, and assumes that every single file being formatted will have the same line ending. */
4345
GIT_ATTRIBUTES_FAST_ALLSAME {
4446
/** .gitattributes is path-specific, so you must use {@link LineEnding#createPolicy(File, Supplier)}. */
45-
@Override @Deprecated
47+
@Override
48+
@Deprecated
4649
public Policy createPolicy() {
4750
return super.createPolicy();
4851
}
@@ -51,13 +54,12 @@ public Policy createPolicy() {
5154
PLATFORM_NATIVE,
5255
/** {@code \r\n} */
5356
WINDOWS,
54-
/** {@code \n} */
55-
UNIX,
56-
/** {@code \r} */
57-
MAC_CLASSIC,
58-
/** preserve the line ending of the first line (no matter which format) */
59-
PRESERVE;
60-
// @formatter:on
57+
/** {@code \n} */
58+
UNIX,
59+
/** {@code \r} */
60+
MAC_CLASSIC,
61+
/** preserve the line ending of the first line (no matter which format) */
62+
PRESERVE;
6163

6264
/** Returns a {@link Policy} appropriate for files which are contained within the given rootFolder. */
6365
public Policy createPolicy(File projectDir, Supplier<Iterable<File>> toFormat) {
@@ -80,21 +82,21 @@ public Policy createPolicy(File projectDir, Supplier<Iterable<File>> toFormat) {
8082
}
8183
}
8284

83-
// @formatter:off
8485
/** Should use {@link #createPolicy(File, Supplier)} instead, but this will work iff its a path-independent LineEnding policy. */
8586
public Policy createPolicy() {
86-
switch (this) {
87-
case PLATFORM_NATIVE: return _PLATFORM_NATIVE_POLICY;
88-
case WINDOWS: return WINDOWS_POLICY;
89-
case UNIX: return UNIX_POLICY;
90-
case MAC_CLASSIC: return MAC_CLASSIC_POLICY;
91-
case PRESERVE: return PRESERVE_POLICY;
92-
default: throw new UnsupportedOperationException(this + " is a path-specific line ending.");
93-
}
87+
return switch (this) {
88+
case PLATFORM_NATIVE -> _PLATFORM_NATIVE_POLICY;
89+
case WINDOWS -> WINDOWS_POLICY;
90+
case UNIX -> UNIX_POLICY;
91+
case MAC_CLASSIC -> MAC_CLASSIC_POLICY;
92+
case PRESERVE -> PRESERVE_POLICY;
93+
default -> throw new UnsupportedOperationException(this + " is a path-specific line ending.");
94+
};
9495
}
9596

9697
static class ConstantLineEndingPolicy extends NoLambda.EqualityBasedOnSerialization implements Policy {
97-
@Serial private static final long serialVersionUID = 1L;
98+
@Serial
99+
private static final long serialVersionUID = 1L;
98100

99101
final String lineEnding;
100102

@@ -109,50 +111,51 @@ public String getEndingFor(File file) {
109111
}
110112

111113
static class PreserveLineEndingPolicy extends NoLambda.EqualityBasedOnSerialization implements Policy {
112-
@Serial private static final long serialVersionUID = 2L;
113-
114-
@Override
115-
public String getEndingFor(File file) {
116-
// assume US-ASCII encoding (only line ending characters need to be decoded anyways)
117-
try (Reader reader = new FileReader(file, StandardCharsets.US_ASCII)) {
118-
return getEndingFor(reader);
119-
} catch (IOException e) {
120-
throw new IllegalArgumentException("Could not determine line ending of file: " + file, e);
121-
}
122-
}
123-
124-
static String getEndingFor(Reader reader) throws IOException {
125-
char previousCharacter = 0;
126-
char currentCharacter = 0;
127-
int readResult;
128-
while ((readResult = reader.read()) != -1) {
129-
currentCharacter = (char) readResult;
130-
if (currentCharacter == '\n') {
131-
if (previousCharacter == '\r') {
132-
return WINDOWS.str();
133-
} else {
134-
return UNIX.str();
135-
}
136-
} else {
137-
if (previousCharacter == '\r') {
138-
return MAC_CLASSIC.str();
139-
}
140-
}
141-
previousCharacter = currentCharacter;
142-
}
143-
if (previousCharacter == '\r') {
144-
return MAC_CLASSIC.str();
145-
}
146-
// assume UNIX line endings if no line ending was found
147-
return UNIX.str();
148-
}
114+
@Serial
115+
private static final long serialVersionUID = 2L;
116+
117+
@Override
118+
public String getEndingFor(File file) {
119+
// assume US-ASCII encoding (only line ending characters need to be decoded anyways)
120+
try (Reader reader = new FileReader(file, StandardCharsets.US_ASCII)) {
121+
return getEndingFor(reader);
122+
} catch (IOException e) {
123+
throw new IllegalArgumentException("Could not determine line ending of file: " + file, e);
124+
}
125+
}
126+
127+
static String getEndingFor(Reader reader) throws IOException {
128+
char previousCharacter = 0;
129+
char currentCharacter;
130+
int readResult;
131+
while ((readResult = reader.read()) != -1) {
132+
currentCharacter = (char) readResult;
133+
if (currentCharacter == '\n') {
134+
if (previousCharacter == '\r') {
135+
return WINDOWS.str();
136+
} else {
137+
return UNIX.str();
138+
}
139+
} else {
140+
if (previousCharacter == '\r') {
141+
return MAC_CLASSIC.str();
142+
}
143+
}
144+
previousCharacter = currentCharacter;
145+
}
146+
if (previousCharacter == '\r') {
147+
return MAC_CLASSIC.str();
148+
}
149+
// assume UNIX line endings if no line ending was found
150+
return UNIX.str();
151+
}
149152
}
150153

151154
private static final Policy WINDOWS_POLICY = new ConstantLineEndingPolicy(WINDOWS.str());
152155
private static final Policy UNIX_POLICY = new ConstantLineEndingPolicy(UNIX.str());
153-
private static final Policy MAC_CLASSIC_POLICY = new ConstantLineEndingPolicy(MAC_CLASSIC.str());
154-
private static final Policy PRESERVE_POLICY = new PreserveLineEndingPolicy();
155-
private static final String _PLATFORM_NATIVE = System.getProperty("line.separator");
156+
private static final Policy MAC_CLASSIC_POLICY = new ConstantLineEndingPolicy(MAC_CLASSIC.str());
157+
private static final Policy PRESERVE_POLICY = new PreserveLineEndingPolicy();
158+
private static final String _PLATFORM_NATIVE = lineSeparator();
156159
private static final Policy _PLATFORM_NATIVE_POLICY = new ConstantLineEndingPolicy(_PLATFORM_NATIVE);
157160
private static final boolean NATIVE_IS_WIN = _PLATFORM_NATIVE.equals(WINDOWS.str());
158161

@@ -169,15 +172,14 @@ public static boolean nativeIsWin() {
169172

170173
/** Returns the standard line ending for this policy. */
171174
public String str() {
172-
switch (this) {
173-
case PLATFORM_NATIVE: return _PLATFORM_NATIVE;
174-
case WINDOWS: return "\r\n";
175-
case UNIX: return "\n";
176-
case MAC_CLASSIC: return "\r";
177-
default: throw new UnsupportedOperationException(this + " is a path-specific line ending.");
178-
}
175+
return switch (this) {
176+
case PLATFORM_NATIVE -> _PLATFORM_NATIVE;
177+
case WINDOWS -> "\r\n";
178+
case UNIX -> "\n";
179+
case MAC_CLASSIC -> "\r";
180+
default -> throw new UnsupportedOperationException(this + " is a path-specific line ending.");
181+
};
179182
}
180-
// @formatter:on
181183

182184
/** A policy for line endings which can vary based on the specific file being requested. */
183185
public interface Policy extends Serializable, NoLambda {

lib/src/main/java/com/diffplug/spotless/groovy/RemoveSemicolonsStep.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*/
1616
package com.diffplug.spotless.groovy;
1717

18+
import static java.lang.System.lineSeparator;
19+
1820
import java.io.BufferedReader;
1921
import java.io.Serial;
2022
import java.io.Serializable;
@@ -54,7 +56,7 @@ FormatterFunc toFormatter() {
5456
String line;
5557
while ((line = reader.readLine()) != null) {
5658
result.append(removeSemicolon(line));
57-
result.append(System.lineSeparator());
59+
result.append(lineSeparator());
5860
}
5961
return result.toString();
6062
}

lib/src/main/java/com/diffplug/spotless/pom/SortPomCfg.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*/
1616
package com.diffplug.spotless.pom;
1717

18+
import static java.lang.System.lineSeparator;
19+
1820
import java.io.Serial;
1921
import java.io.Serializable;
2022

@@ -27,7 +29,7 @@ public class SortPomCfg implements Serializable {
2729

2830
public String encoding = "UTF-8";
2931

30-
public String lineSeparator = System.getProperty("line.separator");
32+
public String lineSeparator = lineSeparator();
3133

3234
public boolean expandEmptyElements;
3335

lib/src/test/java/com/diffplug/spotless/pom/SortPomCfgTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package com.diffplug.spotless.pom;
1717

18+
import static java.lang.System.lineSeparator;
1819
import static org.assertj.core.api.Assertions.assertThat;
1920

2021
import org.junit.jupiter.api.Test;
@@ -28,7 +29,7 @@ void testDefaultValues() {
2829
// Test default values using AssertJ
2930
assertThat(cfg.version).isEqualTo("4.0.0");
3031
assertThat(cfg.encoding).isEqualTo("UTF-8");
31-
assertThat(cfg.lineSeparator).isEqualTo(System.getProperty("line.separator"));
32+
assertThat(cfg.lineSeparator).isEqualTo(lineSeparator());
3233
assertThat(cfg.expandEmptyElements).isFalse();
3334
assertThat(cfg.spaceBeforeCloseEmptyElement).isFalse();
3435
assertThat(cfg.keepBlankLines).isTrue();

0 commit comments

Comments
 (0)