8000 Objects, immutability and switch expressions · pennli/Java-Coding-Problems@8f100d7 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8f100d7

Browse files
committed
Objects, immutability and switch expressions
1 parent cb9fdb7 commit 8f100d7

File tree

16 files changed

+348
-0
lines changed
  • 16 files changed

    +348
    -0
    lines changed
    Lines changed: 46 additions & 0 deletions
    Original file line numberDiff line numberDiff line change
    @@ -0,0 +1,46 @@
    1+
    <?xml version="1.0" encoding="UTF-8"?>
    2+
    <actions>
    3+
    <action>
    4+
    <actionName>run</actionName>
    5+
    <packagings>
    6+
    <packaging>jar</packaging>
    7+
    </packagings>
    8+
    <goals>
    9+
    <goal>process-classes</goal>
    10+
    <goal>org.codehaus.mojo:exec-maven-plugin:1.5.0:exec</goal>
    11+
    </goals>
    12+
    <properties>
    13+
    <exec.args>--enable-preview -classpath %classpath ${packageClassName}</exec.args>
    14+
    <exec.executable>java</exec.executable>
    15+
    </properties>
    16+
    </action>
    17+
    <action>
    18+
    <actionName>debug</actionName>
    19+
    <packagings>
    20+
    <packaging>jar</packaging>
    21+
    </packagings>
    22+
    <goals>
    23+
    <goal>process-classes</goal>
    24+
    <goal>org.codehaus.mojo:exec-maven-plugin:1.5.0:exec</goal>
    25+
    </goals>
    26+
    <properties>
    27+
    <exec.args>--enable-preview -agentlib:jdwp=transport=dt_socket,server=n,address=${jpda.address} -classpath %classpath ${packageClassName}</exec.args>
    28+
    <exec.executable>java</exec.executable>
    29+
    <jpda.listen>true</jpda.listen>
    30+
    </properties>
    31+
    </action>
    32+
    <action>
    33+
    <actionName>profile</actionName>
    34+
    <packagings>
    35+
    <packaging>jar</packaging>
    36+
    </packagings>
    37+
    <goals>
    38+
    <goal>process-classes</goal>
    39+
    <goal>org.codehaus.mojo:exec-maven-plugin:1.5.0:exec</goal>
    40+
    </goals>
    41+
    <properties>
    42+
    <exec.args>--enable-preview -classpath %classpath ${packageClassName}</exec.args>
    43+
    <exec.executable>j B422 ava</exec.executable>
    44+
    </properties>
    45+
    </action>
    46+
    </actions>
    Lines changed: 28 additions & 0 deletions
    Original file line numberDiff line numberDiff line change
    @@ -0,0 +1,28 @@
    1+
    <?xml version="1.0" encoding="UTF-8"?>
    2+
    <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">
    3+
    <modelVersion>4.0.0</modelVersion>
    4+
    <groupId>modern.challenge</groupId>
    5+
    <artifactId>P55_JDK13_SwitchExpression</artifactId>
    6+
    <version>1.0</version>
    7+
    <packaging>jar</packaging>
    8+
    <build>
    9+
    <plugins>
    10+
    <plugin>
    11+
    <groupId>org.apache.maven.plugins</groupId>
    12+
    <artifactId>maven-compiler-plugin</artifactId>
    13+
    <version>3.3</version>
    14+
    <configuration>
    15+
    <compilerArgs>
    16+
    <arg>--enable-preview</arg>
    17+
    </compilerArgs>
    18+
    </configuration>
    19+
    </plugin>
    20+
    </plugins>
    21+
    </build>
    22+
    <properties>
    23+
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    24+
    F438 <maven.compiler.source>13</maven.compiler.source>
    25+
    <maven.compiler.target>13</maven.compiler.target>
    26+
    </properties>
    27+
    <name>P55_JDK13_SwitchExpression</name>
    28+
    </project>
    Lines changed: 4 additions & 0 deletions
    Original file line numberDiff line numberDiff line change
    @@ -0,0 +1,4 @@
    1+
    package modern.challenge;
    2+
    3+
    public class FootballPlayer extends Player {
    4+
    }
    Lines changed: 91 additions & 0 deletions
    Original file line numberDiff line numberDiff line change
    @@ -0,0 +1,91 @@
    1+
    package modern.challenge;
    2+
    3+
    public class Main {
    4+
    5+
    public enum PlayerTypes {
    6+
    TENNIS,
    7+
    FOOTBALL,
    8+
    SNOOKER,
    9+
    UNKNOWN
    10+
    }
    11+
    12+
    public static void main(String[] args) {
    13+
    // switch statement
    14+
    Player playerSwitchStatementUgly = createPlayerSwitchStatementUgly(PlayerTypes.TENNIS);
    15+
    Player playerSwitchStatementNice = createPlayerSwitchStatementNice(PlayerTypes.TENNIS);
    16+
    17+
    // switch expression
    18+
    Player playerSwitchExpression = createPlayerSwitchExpression(PlayerTypes.TENNIS);
    19+
    Player playerSwitchExpressionYield = createPlayerSwitchExpressionBreak(PlayerTypes.TENNIS);
    20+
    }
    21+
    22+
    private static Player createPlayerSwitchStatementUgly(PlayerTypes playerType) {
    23+
    24+
    Player player = null;
    25+
    26+
    switch (playerType) {
    27+
    case TENNIS:
    28+
    player = new TennisPlayer();
    29+
    break;
    30+
    case FOOTBALL:
    31+
    player = new FootballPlayer();
    32+
    break;
    33+
    case SNOOKER:
    34+
    player = new SnookerPlayer();
    35+
    break;
    36+
    case UNKNOWN:
    37+
    throw new UnknownPlayerException("Player type is unknown");
    38+
    default:
    39+
    throw new IllegalArgumentException("Invalid player type: " + playerType);
    40+
    }
    41+
    42+
    return player;
    43+
    }
    44+
    45+
    private static Player createPlayerSwitchStatementNice(PlayerTypes playerType) {
    46+
    switch (playerType) {
    47+
    case TENNIS:
    48+
    return new TennisPlayer();
    49+
    case FOOTBALL:
    50+
    return new FootballPlayer();
    51+
    case SNOOKER:
    52+
    return new SnookerPlayer();
    53+
    case UNKNOWN:
    54+
    throw new UnknownPlayerException("Player type is unknown");
    55+
    default:
    56+
    throw new IllegalArgumentException("Invalid player type: " + playerType);
    57+
    }
    58+
    }
    59+
    60+
    private static Player createPlayerSwitchExpression(PlayerTypes playerType) {
    61+
    return switch (playerType) {
    62+
    case TENNIS->
    63+
    new TennisPlayer();
    64+
    case FOOTBALL->
    65+
    new FootballPlayer();
    66+
    case SNOOKER->
    67+
    new SnookerPlayer();
    68+
    case UNKNOWN->
    69+
    throw new UnknownPlayerException("Player type is unknown");
    70+
    // default is not mandatory
    71+
    default->
    72+
    throw new IllegalArgumentException("Invalid player type: " + playerType);
    73+
    };
    74+
    }
    75+
    76+
    private static Player createPlayerSwitchExpressionBreak(PlayerTypes playerType) {
    77+
    return switch (playerType) {
    78+
    case TENNIS:
    79+
    yield new TennisPlayer();
    80+
    case FOOTBALL:
    81+
    yield new FootballPlayer();
    82+
    case SNOOKER:
    83+
    yield new SnookerPlayer();
    84+
    case UNKNOWN:
    85+
    throw new UnknownPlayerException("Player type is unknown");
    86+
    // default is not mandatory
    87+
    default:
    88+
    throw new IllegalArgumentException("Invalid player type: " + playerType);
    89+
    };
    90+
    }
    91+
    }
    Lines changed: 4 additions & 0 deletions
    Original file line numberDiff line numberDiff line change
    @@ -0,0 +1,4 @@
    1+
    package modern.challenge;
    2+
    3+
    public class Player {
    4+
    }
    Lines changed: 4 additions & 0 deletions
    Original file line numberDiff line numberDiff line change
    @@ -0,0 +1,4 @@
    1+
    package modern.challenge;
    2+
    3+
    public class SnookerPlayer extends Player {
    4+
    }
    Lines changed: 4 additions & 0 deletions
    Original file line numberDiff line numberDiff line change
    @@ -0,0 +1,4 @@
    1+
    package modern.challenge;
    2+
    3+
    public class TennisPlayer extends Player{
    4+
    }
    Lines changed: 22 additions & 0 deletions
    Original file line numberDiff line numberDiff line change
    @@ -0,0 +1,22 @@
    1+
    package modern.challenge;
    2+
    3+
    public class UnknownPlayerException extends RuntimeException {
    4+
    5+
    private static final long serialVersionUID = 1L;
    6+
    7+
    public UnknownPlayerException() {
    8+
    super();
    9+
    }
    10+
    11+
    public UnknownPlayerException(String message) {
    12+
    super(message);
    13+
    }
    14+
    15+
    public UnknownPlayerException(Throwable cause) {
    16+
    super(cause);
    17+
    }
    18+
    19+
    public UnknownPlayerException(String message, Throwable cause) {
    20+
    super(message, cause);
    21+
    }
    22+
    }
    Lines changed: 46 additions & 0 deletions
    Original file line numberDiff line numberDiff line change
    @@ -0,0 +1,46 @@
    1+
    <?xml version="1.0" encoding="UTF-8"?>
    2+
    <actions>
    3+
    <action>
    4+
    <actionName>run</actionName>
    5+
    <packagings>
    6+
    <packaging>jar</packaging>
    7+
    </packagings>
    8+
    <goals>
    9+
    <goal>process-classes</goal>
    10+
    <goal>org.codehaus.mojo:exec-maven-plugin:1.5.0:exec</goal>
    11+
    </goals>
    12+
    <properties>
    13+
    <exec.args>--enable-preview -classpath %classpath ${packageClassName}</exec.args>
    14+
    <exec.executable>java</exec.executable>
    15+
    </properties>
    16+
    </action>
    17+
    <action>
    18+
    <actionName>debug</actionName>
    19+
    <packagings>
    20+
    <packaging>jar</packaging>
    21+
    </packagings>
    22+
    <goals>
    23+
    <goal>process-classes</goal>
    24+
    <goal>org.codehaus.mojo:exec-maven-plugin:1.5.0:exec</goal>
    25+
    </goals>
    26+
    <properties>
    27+
    <exec.args>--enable-preview -agentlib:jdwp=transport=dt_socket,server=n,address=${jpda.address} -classpath %classpath ${packageClassName}</exec.args>
    28+
    <exec.executable>java</exec.executable>
    29+
    <jpda.listen>true</jpda.listen>
    30+
    </properties>
    31+
    </action>
    32+
    <action>
    33+
    <actionName>profile</actionName>
    34+
    <packagings>
    35+
    <packaging>jar</packaging>
    36+
    </packagings>
    37+
    <goals>
    38+
    <goal>process-classes</goal>
    39+
    <goal>org.codehaus.mojo:exec-maven-plugin:1.5.0:exec</goal>
    40+
    </goals>
    41+
    <properties>
    42+
    <exec.args>--enable-preview -classpath %classpath ${packageClassName}</exec.args>
    43+
    <exec.executable>java</exec.executable>
    44+
    </properties>
    45+
    </action>
    46+
    </actions>
    Lines changed: 28 additions & 0 deletions
    Original file line numberDiff line numberDiff line change
    @@ -0,0 +1,28 @@
    1+
    <?xml version="1.0" encoding="UTF-8"?>
    2+
    <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">
    3+
    <modelVersion>4.0.0</modelVersion>
    4+
    <groupId>modern.challenge</groupId>
    5+
    <artifactId>P57_JDK13_StatementBlocks</artifactId>
    6+
    <version>1.0</version>
    7+
    <packaging>jar</packaging>
    8+
    <build>
    9+
    <plugins>
    10+
    <plugin>
    11+
    <groupId>org.apache.maven.plugins</groupId>
    12+
    <artifactId>maven-compiler-plugin</artifactId>
    13+
    <version>3.3</version>
    14+
    <configuration>
    15+
    <compilerArgs>
    16+
    <arg>--enable-preview</arg>
    17+
    </compilerArgs>
    18+
    </configuration>
    19+
    </plugin>
    20+
    </plugins>
    21+
    </build>
    22+
    <properties>
    23+
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    24+
    <maven.compiler.source>13</maven.compiler.source>
    25+
    <maven.compiler.target>13</maven.compiler.target>
    26+
    </properties>
    27+
    <name>P57_JDK13_StatementBlocks</name>
    28+
    </project>

    0 commit comments

    Comments
     (0)
    0