forked from checkstyle/checkstyle
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue checkstyle#6615: Add support for Java 14 switch
- Loading branch information
1 parent
11bb48a
commit 8d39027
Showing
5 changed files
with
1,143 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
128 changes: 128 additions & 0 deletions
128
...ilable/com/puppycrawl/tools/checkstyle/grammar/InputRegressionJava14SwitchExpression.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
//non-compiled with javac: Compilable with Java14 | ||
package com.puppycrawl.tools.checkstyle.grammar; | ||
|
||
import java.util.EnumSet; | ||
import java.util.Set; | ||
|
||
public class InputRegressionJava14SwitchExpression { | ||
|
||
enum Day { | ||
MON, | ||
TUE, | ||
WED, | ||
THU, | ||
FRI, | ||
SAT, | ||
SUN, | ||
} | ||
|
||
String whatIsToday(Day day) { | ||
return switch (day) { | ||
case SAT, SUN -> "Weekend"; | ||
case MON, TUE, WED, THU, FRI -> "Working day"; | ||
default -> throw new IllegalArgumentException("Invalid day: " + day.name()); | ||
}; | ||
} | ||
|
||
Set<Day> days(String weekPart) { | ||
return switch (weekPart) { | ||
case "Weekend" -> EnumSet.of(Day.SAT, Day.SUN); | ||
case "Working day" -> EnumSet.of(Day.MON, Day.TUE, Day.WED, Day.THU, Day.FRI); | ||
default -> throw new IllegalArgumentException("Invalid weekPart: " + weekPart); | ||
}; | ||
} | ||
|
||
String isDayNameLong(Day day) { | ||
return switch (day) { | ||
case MON, FRI, SUN -> 6; | ||
case TUE -> 7; | ||
case THU, SAT -> 8; | ||
case WED -> 9; | ||
} > 7 ? "long" : "short"; | ||
} | ||
|
||
int assignement(Day day) { | ||
int numLetters = switch (day) { | ||
case MON, FRI, SUN -> 6; | ||
case TUE -> 7; | ||
case THU, SAT -> 8; | ||
case WED -> 9; | ||
}; | ||
return numLetters; | ||
} | ||
|
||
static void howMany(int k) { | ||
switch (k) { | ||
case 1 -> System.out.println("one"); | ||
case 2 -> System.out.println("two"); | ||
case 3 -> System.out.println("many"); | ||
default -> throw new IllegalArgumentException("Unknown"); | ||
} | ||
} | ||
|
||
int methodCalls(Day day) { | ||
return switch (day) { | ||
case MON, TUE -> Math.addExact(0, 1); | ||
case WED -> Math.addExact(1, 1); | ||
default -> { | ||
yield Math.addExact(2, 1); | ||
} | ||
}; | ||
} | ||
|
||
int yieldAcceptsExpressions(Day day) { | ||
return switch (day) { | ||
case MON, TUE -> 0; | ||
case WED -> 1; | ||
default -> day.toString().length() > 5 ? 1 : 0; | ||
}; | ||
} | ||
|
||
int complexCase(Day day) { | ||
return switch (day) { | ||
case MON, TUE -> { | ||
int l = day.toString().length(); | ||
yield Math.addExact(l, 0); | ||
} | ||
case WED -> { | ||
int l = day.toString().length(); | ||
yield Math.addExact(l, 1); | ||
} | ||
default -> { | ||
int l = day.toString().length(); | ||
yield Math.addExact(l, 2); | ||
} | ||
}; | ||
} | ||
|
||
int arithmetic(Day day) { | ||
return switch (day) { | ||
case MON, FRI, SUN -> 6; | ||
case TUE -> 7; | ||
case THU, SAT -> 8; | ||
case WED -> 9; | ||
} % 2; | ||
} | ||
|
||
int signArithmetic(Day day) { | ||
return - switch (day) { | ||
case MON, FRI, SUN -> 6; | ||
case TUE -> 7; | ||
case THU, SAT -> 8; | ||
case WED -> 9; | ||
}; | ||
} | ||
|
||
int usedOnBothSidesOfAriphmeticExpression(Day day) { | ||
return switch (day) { | ||
case MON, TUE -> 0; | ||
case WED -> 1; | ||
default -> 2; | ||
} * switch (day) { | ||
case WED, THU -> 3; | ||
case FRI -> 4; | ||
default -> 5; | ||
}; | ||
} | ||
|
||
} |
Oops, something went wrong.