-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6629 from dotty-staging/java-enums
Java enums
- Loading branch information
Showing
12 changed files
with
174 additions
and
11 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
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
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 |
---|---|---|
|
@@ -17,3 +17,4 @@ mixin-forwarder-overload | |
t8905 | ||
t10889 | ||
i5257.scala | ||
enum-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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
API Test A | ||
compareTo greater:-1 | ||
compareTo lesser: 1 | ||
compareTo self: 0 | ||
equals other: false | ||
equals self: true | ||
getDeclaringClass:class A | ||
name: MONDAY | ||
ordinal: 0 | ||
toString: MONDAY | ||
|
||
API Test B | ||
compareTo greater:-1 | ||
compareTo lesser: 1 | ||
compareTo self: 0 | ||
equals other: false | ||
equals self: true | ||
getDeclaringClass:class B | ||
name: EARTH | ||
ordinal: 0 | ||
toString: EARTH | ||
|
||
Module Test | ||
Values class: class [LA; | ||
MONDAY : 0 | ||
TUESDAY : 1 | ||
SATURDAY : 2 | ||
By-name value: MONDAY | ||
Correctly failed to retrieve illegal name, message: key not found: stuff | ||
|
||
Collections Test | ||
Retrieving Monday: workday | ||
Contains Tuesday: false | ||
All days: | ||
MONDAY | ||
TUESDAY | ||
SATURDAY | ||
|
||
Switch Test | ||
Saw tuesday | ||
Jup | ||
|
||
Misc Tests | ||
Gravity on Earth: 9.8 |
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,10 @@ | ||
enum A extends java.lang.Enum[A] { | ||
case MONDAY, TUESDAY, SATURDAY | ||
} | ||
|
||
enum B(val gravity: Double) extends java.lang.Enum[B] { | ||
case EARTH extends B(9.8) | ||
case JUPITER extends B(100) | ||
case MOON extends B(4.3) | ||
case Foo extends B(10) | ||
} |
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,88 @@ | ||
public class Test { | ||
public static void log(String x) { System.out.println(x); } | ||
public static void check(Boolean p, String message) { | ||
if (!p) throw new RuntimeException("Assertion failed: " + message); | ||
} | ||
|
||
public static <E extends java.lang.Enum<E>> void apiTest(E t1, E t2) { | ||
log("compareTo greater:" + t1.compareTo(t2) ); | ||
log("compareTo lesser: " + t2.compareTo(t1) ); | ||
log("compareTo self: " + t1.compareTo(t1) ); | ||
log("equals other: " + t1.equals(t2) ); | ||
log("equals self: " + t1.equals(t1) ); | ||
log("getDeclaringClass:" + t1.getDeclaringClass() ); | ||
log("name: " + t1.name() ); | ||
log("ordinal: " + t1.ordinal() ); | ||
log("toString: " + t1.toString() ); | ||
} | ||
|
||
public static void moduleTest() { | ||
A[] values = A.values(); | ||
log("Values class: " + values.getClass()); | ||
for (A v: values) log(v.name() + " : " + v.ordinal()); | ||
log("By-name value: " + A.valueOf("MONDAY")); | ||
try { | ||
A.valueOf("stuff"); | ||
} | ||
catch (IllegalArgumentException e) { | ||
log("Correctly failed to retrieve illegal name, message: " + e.getMessage()); | ||
} | ||
} | ||
|
||
public static void collectionsTest() { | ||
java.util.EnumMap<A, String> days = new java.util.EnumMap<A, String>(A.class); | ||
days.put(A.MONDAY, "workday"); | ||
days.put(A.SATURDAY, "weekend"); | ||
|
||
log("Retrieving Monday: " + days.get(A.MONDAY)); | ||
log("Contains Tuesday: " + days.containsKey(A.TUESDAY)); | ||
|
||
java.util.EnumSet<A> allDays = java.util.EnumSet.allOf(A.class); | ||
log("All days:"); | ||
for (A d : allDays) log(d.toString()); | ||
} | ||
|
||
public static void switchTest() { | ||
A a = A.TUESDAY; | ||
switch(a) { | ||
case MONDAY: | ||
log("Saw monday"); | ||
break; | ||
case TUESDAY: | ||
log("Saw tuesday"); | ||
break; | ||
} | ||
|
||
B b = B.JUPITER; | ||
switch (b) { | ||
case JUPITER: log("Jup"); break; | ||
case EARTH: log("Earth"); break; | ||
} | ||
} | ||
|
||
public static void miscTests() { | ||
log("Gravity on Earth: " + B.EARTH.gravity()); | ||
check(A.class.isEnum(), "A class must be enum"); | ||
check(B.class.isEnum(), "B class must be enum"); | ||
} | ||
|
||
public static void main(String[] args) { | ||
log("API Test A"); | ||
apiTest(A.MONDAY, A.TUESDAY); | ||
|
||
log("\nAPI Test B"); | ||
apiTest(B.EARTH, B.JUPITER); | ||
|
||
log("\nModule Test"); | ||
moduleTest(); | ||
|
||
log("\nCollections Test"); | ||
collectionsTest(); | ||
|
||
log("\nSwitch Test"); | ||
switchTest(); | ||
|
||
log("\nMisc Tests"); | ||
miscTests(); | ||
} | ||
} |