Skip to content

Commit a8b8d2d

Browse files
authored
Feature/bael 4662 sealed classes (eugenp#10224)
* BAEL-4662: Initial commit of new module * BAEL-4662: Example with Records * BAEL-4662: Example with Interface * BAEL-4662: Add test classes * BAEL-4662: Update examples and tests * BAEL-4662: Rename test classes
1 parent c89dee8 commit a8b8d2d

13 files changed

Lines changed: 389 additions & 0 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
--enable-preview
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
## Core Java 15
2+
3+
This module contains articles about Java 15.
4+
5+
### Relevant articles
6+
7+
- TODO: add article links here
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project
3+
xmlns="http://maven.apache.org/POM/4.0.0"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
6+
<modelVersion>4.0.0</modelVersion>
7+
<artifactId>core-java-15</artifactId>
8+
<name>core-java-15</name>
9+
<packaging>jar</packaging>
10+
<url>http://maven.apache.org</url>
11+
12+
<parent>
13+
<groupId>com.baeldung</groupId>
14+
<artifactId>parent-modules</artifactId>
15+
<version>1.0.0-SNAPSHOT</version>
16+
<relativePath>../../pom.xml</relativePath>
17+
</parent>
18+
19+
<dependencies>
20+
<dependency>
21+
<groupId>org.apache.commons</groupId>
22+
<artifactId>commons-lang3</artifactId>
23+
<version>${apache-commons-lang3.version}</version>
24+
</dependency>
25+
<dependency>
26+
<groupId>org.assertj</groupId>
27+
<artifactId>assertj-core</artifactId>
28+
<version>${assertj.version}</version>
29+
<scope>test</scope>
30+
</dependency>
31+
<dependency>
32+
<groupId>org.junit.jupiter</groupId>
33+
<artifactId>junit-jupiter-engine</artifactId>
34+
<version>${junit-jupiter.version}</version>
35+
<scope>test</scope>
36+
</dependency>
37+
<dependency>
38+
<groupId>org.junit.jupiter</groupId>
39+
<artifactId>junit-jupiter-api</artifactId>
40+
<version>${junit-jupiter.version}</version>
41+
<scope>test</scope>
42+
</dependency>
43+
</dependencies>
44+
45+
<build>
46+
<plugins>
47+
<plugin>
48+
<groupId>org.apache.maven.plugins</groupId>
49+
<artifactId>maven-compiler-plugin</artifactId>
50+
<version>${maven-compiler-plugin.version}</version>
51+
<configuration>
52+
<release>${maven.compiler.release}</release>
53+
<compilerArgs>--enable-preview</compilerArgs>
54+
<source>15</source>
55+
<target>15</target>
56+
</configuration>
57+
</plugin>
58+
<plugin>
59+
<groupId>org.apache.maven.plugins</groupId>
60+
<artifactId>maven-surefire-plugin</artifactId>
61+
<version>${surefire.plugin.version}</version>
62+
<configuration>
63+
<argLine>--enable-preview</argLine>
64+
</configuration>
65+
</plugin>
66+
</plugins>
67+
</build>
68+
69+
<properties>
70+
<maven.compiler.release>15</maven.compiler.release>
71+
<apache-commons-lang3.version>3.11</apache-commons-lang3.version>
72+
<assertj.version>3.17.2</assertj.version>
73+
<maven-compiler-plugin.version>3.8.1</maven-compiler-plugin.version>
74+
<surefire.plugin.version>3.0.0-M3</surefire.plugin.version>
75+
</properties>
76+
77+
</project>
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.baeldung.sealed.alternative;
2+
3+
public class Vehicles {
4+
5+
abstract static class Vehicle {
6+
7+
private final String registrationNumber;
8+
9+
public Vehicle(String registrationNumber) {
10+
this.registrationNumber = registrationNumber;
11+
}
12+
13+
public String getRegistrationNumber() {
14+
return registrationNumber;
15+
}
16+
17+
}
18+
19+
public static final class Car extends Vehicle {
20+
21+
private final int numberOfSeats;
22+
23+
public Car(int numberOfSeats, String registrationNumber) {
24+
super(registrationNumber);
25+
this.numberOfSeats = numberOfSeats;
26+
}
27+
28+
public int getNumberOfSeats() {
29+
return numberOfSeats;
30+
}
31+
32+
}
33+
34+
public static final class Truck extends Vehicle {
35+
36+
private final int loadCapacity;
37+
38+
public Truck(int loadCapacity, String registrationNumber) {
39+
super(registrationNumber);
40+
this.loadCapacity = loadCapacity;
41+
}
42+
43+
public int getLoadCapacity() {
44+
return loadCapacity;
45+
}
46+
47+
}
48+
49+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.baeldung.sealed.classes;
2+
3+
public non-sealed class Car extends Vehicle implements Service {
4+
5+
private final int numberOfSeats;
6+
7+
public Car(int numberOfSeats, String registrationNumber) {
8+
super(registrationNumber);
9+
this.numberOfSeats = numberOfSeats;
10+
}
11+
12+
public int getNumberOfSeats() {
13+
return numberOfSeats;
14+
}
15+
16+
@Override
17+
public int getMaxServiceIntervalInMonths() {
18+
return 12;
19+
}
20+
21+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.baeldung.sealed.classes;
2+
3+
public sealed interface Service permits Car, Truck {
4+
5+
int getMaxServiceIntervalInMonths();
6+
7+
default int getMaxDistanceBetweenServicesInKilometers() {
8+
return 100000;
9+
}
10+
11+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.baeldung.sealed.classes;
2+
3+
public final class Truck extends Vehicle implements Service {
4+
5+
private final int loadCapacity;
6+
7+
public Truck(int loadCapacity, String registrationNumber) {
8+
super(registrationNumber);
9+
this.loadCapacity = loadCapacity;
10+
}
11+
12+
public int getLoadCapacity() {
13+
return loadCapacity;
14+
}
15+
16+
@Override
17+
public int getMaxServiceIntervalInMonths() {
18+
return 18;
19+
}
20+
21+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.baeldung.sealed.classes;
2+
3+
public abstract sealed class Vehicle permits Car, Truck {
4+
5+
protected final String registrationNumber;
6+
7+
public Vehicle(String registrationNumber) {
8+
this.registrationNumber = registrationNumber;
9+
}
10+
11+
public String getRegistrationNumber() {
12+
return registrationNumber;
13+
}
14+
15+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.baeldung.sealed.records;
2+
3+
public record Car(int numberOfSeats, String registrationNumber) implements Vehicle {
4+
5+
@Override
6+
public String getRegistrationNumber() {
7+
return registrationNumber;
8+
}
9+
10+
public int getNumberOfSeats() {
11+
return numberOfSeats;
12+
}
13+
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.baeldung.sealed.records;
2+
3+
public record Truck(int loadCapacity, String registrationNumber) implements Vehicle {
4+
5+
@Override
6+
public String getRegistrationNumber() {
7+
return registrationNumber;
8+
}
9+
10+
public int getLoadCapacity() {
11+
return loadCapacity;
12+
}
13+
14+
}

0 commit comments

Comments
 (0)