Skip to content

Commit 3571680

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

File tree

3 files changed

+38
-55
lines changed

3 files changed

+38
-55
lines changed

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

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -347,27 +347,18 @@ 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

362357
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-
}
358+
return switch (bodyDeclaration.getNodeType()) {
359+
case ASTNode.FIELD_DECLARATION, ASTNode.ENUM_CONSTANT_DECLARATION, ASTNode.INITIALIZER -> true;
360+
default -> false;
361+
};
371362
}
372363

373364
private int preserveRelativeOrder(BodyDeclaration bodyDeclaration1, BodyDeclaration bodyDeclaration2) {

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/LineEnding.java

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
* Represents the line endings which should be written by the tool.
3131
*/
3232
public enum LineEnding {
33-
// @formatter:off
3433
/** Uses the same line endings as Git, using {@code .gitattributes} and the {@code core.eol} property. */
3534
GIT_ATTRIBUTES {
3635
/** .gitattributes is path-specific, so you must use {@link LineEnding#createPolicy(File, Supplier)}. */
@@ -57,7 +56,6 @@ public Policy createPolicy() {
5756
MAC_CLASSIC,
5857
/** preserve the line ending of the first line (no matter which format) */
5958
PRESERVE;
60-
// @formatter:on
6159

6260
/** Returns a {@link Policy} appropriate for files which are contained within the given rootFolder. */
6361
public Policy createPolicy(File projectDir, Supplier<Iterable<File>> toFormat) {
@@ -80,17 +78,16 @@ public Policy createPolicy(File projectDir, Supplier<Iterable<File>> toFormat) {
8078
}
8179
}
8280

83-
// @formatter:off
8481
/** Should use {@link #createPolicy(File, Supplier)} instead, but this will work iff its a path-independent LineEnding policy. */
8582
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-
}
83+
return switch (this) {
84+
case PLATFORM_NATIVE -> _PLATFORM_NATIVE_POLICY;
85+
case WINDOWS -> WINDOWS_POLICY;
86+
case UNIX -> UNIX_POLICY;
87+
case MAC_CLASSIC -> MAC_CLASSIC_POLICY;
88+
case PRESERVE -> PRESERVE_POLICY;
89+
default -> throw new UnsupportedOperationException(this + " is a path-specific line ending.");
90+
};
9491
}
9592

9693
static class ConstantLineEndingPolicy extends NoLambda.EqualityBasedOnSerialization implements Policy {
@@ -123,7 +120,7 @@ public String getEndingFor(File file) {
123120

124121
static String getEndingFor(Reader reader) throws IOException {
125122
char previousCharacter = 0;
126-
char currentCharacter = 0;
123+
char currentCharacter;
127124
int readResult;
128125
while ((readResult = reader.read()) != -1) {
129126
currentCharacter = (char) readResult;
@@ -169,15 +166,14 @@ public static boolean nativeIsWin() {
169166

170167
/** Returns the standard line ending for this policy. */
171168
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-
}
169+
return switch (this) {
170+
case PLATFORM_NATIVE -> _PLATFORM_NATIVE;
171+
case WINDOWS -> "\r\n";
172+
case UNIX -> "\n";
173+
case MAC_CLASSIC -> "\r";
174+
default -> throw new UnsupportedOperationException(this + " is a path-specific line ending.");
175+
};
179176
}
180-
// @formatter:on
181177

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

0 commit comments

Comments
 (0)