| 1 | package com.rox; | |
| 2 | ||
| 3 | import com.rox.cartridge.Cartridge; | |
| 4 | ||
| 5 | import javax.sound.sampled.LineUnavailableException; | |
| 6 | import java.io.IOException; | |
| 7 | import java.nio.file.Path; | |
| 8 | import java.util.Optional; | |
| 9 | ||
| 10 | /** The game currently loaded, if any, and swapping it for another ROM's. */ | |
| 11 | final class GameSwitcher { | |
| 12 | @FunctionalInterface | |
| 13 | interface RomReader { | |
| 14 | /** @throws IllegalArgumentException for a file that isn't a ROM this emulator can run */ | |
| 15 | Cartridge read(Path rom) throws IOException; | |
| 16 | } | |
| 17 | ||
| 18 | @FunctionalInterface | |
| 19 | interface SessionStarter { | |
| 20 | GameSession start(Path rom, Cartridge cartridge) throws LineUnavailableException; | |
| 21 | } | |
| 22 | ||
| 23 | /** Told just before a game closes and just after one starts, on the thread doing it. */ | |
| 24 | interface Listener { | |
| 25 | default void closing(final GameSession session){ | |
| 26 | } | |
| 27 | ||
| 28 | default void started(final GameSession session){ | |
| 29 | } | |
| 30 | } | |
| 31 | ||
| 32 | private final RomReader reader; | |
| 33 | private final SessionStarter starter; | |
| 34 | private final Listener listener; | |
| 35 | private GameSession current; | |
| 36 | /** set by {@link #shutDown} - no game opens after it */ | |
| 37 | private boolean shutDown; | |
| 38 | ||
| 39 | GameSwitcher(final RomReader reader, final SessionStarter starter, final Listener listener){ | |
| 40 | this.reader = reader; | |
| 41 | this.starter = starter; | |
| 42 | this.listener = listener; | |
| 43 | } | |
| 44 | ||
| 45 | synchronized Optional<GameSession> current(){ | |
| 46 |
1
1. current : replaced return value with Optional.empty for com/rox/GameSwitcher::current → KILLED |
return Optional.ofNullable(current); |
| 47 | } | |
| 48 | ||
| 49 | /** | |
| 50 | * Reads {@code rom} first, so one that can't be loaded leaves the current game running; then closes | |
| 51 | * the current game and starts {@code rom}'s. Does nothing once {@link #shutDown}. | |
| 52 | * | |
| 53 | * @throws IOException or {@link IllegalArgumentException} if {@code rom} can't be read - nothing changes | |
| 54 | * @throws LineUnavailableException if the new game can't start - no game is left loaded | |
| 55 | */ | |
| 56 | synchronized void open(final Path rom) throws IOException, LineUnavailableException { | |
| 57 |
1
1. open : negated conditional → KILLED |
if (shutDown){ |
| 58 | return; | |
| 59 | } | |
| 60 | final Cartridge cartridge = reader.read(rom); | |
| 61 |
1
1. open : removed call to com/rox/GameSwitcher::close → KILLED |
close(); |
| 62 | current = starter.start(rom, cartridge); | |
| 63 |
1
1. open : removed call to com/rox/GameSwitcher$Listener::started → KILLED |
listener.started(current); |
| 64 | } | |
| 65 | ||
| 66 | /** Closes the current game, if any, for good: later {@link #open}s do nothing. */ | |
| 67 | synchronized void shutDown(){ | |
| 68 | shutDown = true; | |
| 69 |
1
1. shutDown : removed call to com/rox/GameSwitcher::close → KILLED |
close(); |
| 70 | } | |
| 71 | ||
| 72 | /** Closes the current game, if any. */ | |
| 73 | synchronized void close(){ | |
| 74 |
1
1. close : negated conditional → KILLED |
if (current == null){ |
| 75 | return; | |
| 76 | } | |
| 77 | final GameSession closing = current; | |
| 78 | current = null; | |
| 79 |
1
1. close : removed call to com/rox/GameSwitcher$Listener::closing → KILLED |
listener.closing(closing); |
| 80 |
1
1. close : removed call to com/rox/GameSession::close → KILLED |
closing.close(); |
| 81 | } | |
| 82 | } | |
Mutations | ||
| 46 |
1.1 |
|
| 57 |
1.1 |
|
| 61 |
1.1 |
|
| 63 |
1.1 |
|
| 69 |
1.1 |
|
| 74 |
1.1 |
|
| 79 |
1.1 |
|
| 80 |
1.1 |