-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9ba0171
commit 33a6073
Showing
4 changed files
with
78 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<groupId>com.ibrahimatay</groupId> | ||
<artifactId>Java-Features</artifactId> | ||
<version>1.0-SNAPSHOT</version> | ||
</parent> | ||
|
||
<artifactId>java-17</artifactId> | ||
|
||
<properties> | ||
<maven.compiler.source>17</maven.compiler.source> | ||
<maven.compiler.target>17</maven.compiler.target> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
</properties> | ||
|
||
</project> |
54 changes: 54 additions & 0 deletions
54
java-17/src/main/java/com/ibrahimatay/JEP409SealedClasses.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,54 @@ | ||
package com.ibrahimatay; | ||
|
||
/* | ||
JEP 409: Sealed Classes | ||
https://openjdk.org/jeps/409 | ||
*/ | ||
|
||
import java.util.UUID; | ||
|
||
public class JEP409SealedClasses { | ||
public static void main(String[] args) { | ||
System.out.println(getNumberOfSeats(new Truck(100, UUID.randomUUID().toString()))); | ||
System.out.println(getNumberOfSeats(new Car(20, UUID.randomUUID().toString()))); | ||
} | ||
|
||
static int getNumberOfSeats(Vehicle vehicle) { | ||
if (vehicle instanceof Car) { | ||
return ((Car) vehicle).getNumberOfSeats(); | ||
} else if (vehicle instanceof Truck) { | ||
return ((Truck) vehicle).getLoadCapacity(); | ||
} else { | ||
throw new RuntimeException("Unknown instance of Vehicle"); | ||
} | ||
} | ||
} | ||
|
||
sealed interface Vehicle permits Car, Truck { | ||
String getRegistrationNumber(); | ||
} | ||
|
||
record Car(int numberOfSeats, String registrationNumber) implements Vehicle { | ||
|
||
@Override | ||
public String getRegistrationNumber() { | ||
return registrationNumber; | ||
} | ||
|
||
public int getNumberOfSeats() { | ||
return numberOfSeats; | ||
} | ||
} | ||
|
||
record Truck(int loadCapacity, String registrationNumber) implements Vehicle { | ||
|
||
@Override | ||
public String getRegistrationNumber() { | ||
return registrationNumber; | ||
} | ||
|
||
public int getLoadCapacity() { | ||
return loadCapacity; | ||
} | ||
} |
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