| 1 | package com.rox; | |
| 2 | ||
| 3 | import java.nio.file.Path; | |
| 4 | import java.util.Optional; | |
| 5 | import java.util.OptionalInt; | |
| 6 | ||
| 7 | /** | |
| 8 | * The game window's command line - every flag optional: | |
| 9 | * {@code [--rom <file.nes>] [--controllers <file.properties>] [--seconds <n>]}. | |
| 10 | * | |
| 11 | * @param seconds how long to run before closing, or empty to run until the window is closed | |
| 12 | */ | |
| 13 | record LaunchOptions(Optional<Path> rom, Optional<Path> controllers, OptionalInt seconds) { | |
| 14 | static final String USAGE = "Usage: RomVideoSmokeDemo [--rom <file.nes>] [--controllers <file.properties>]" | |
| 15 | + " [--seconds <n>, or omit to run until the window is closed]"; | |
| 16 | ||
| 17 | /** @throws IllegalArgumentException naming what's wrong: an unknown or repeated flag, a missing value, bad seconds */ | |
| 18 | static LaunchOptions parse(final String... args){ | |
| 19 | Path rom = null; | |
| 20 | Path controllers = null; | |
| 21 | Integer seconds = null; | |
| 22 |
2
1. parse : changed conditional boundary → KILLED 2. parse : negated conditional → KILLED |
for (int i = 0; i < args.length; i += 2){ |
| 23 | final String flag = args[i]; | |
| 24 | switch (flag){ | |
| 25 | case "--rom" -> rom = once(flag, rom, Path.of(value(args, i))); | |
| 26 | case "--controllers" -> controllers = once(flag, controllers, Path.of(value(args, i))); | |
| 27 | case "--seconds" -> seconds = once(flag, seconds, seconds(value(args, i))); | |
| 28 | default -> throw new IllegalArgumentException("Unknown option: " + flag); | |
| 29 | } | |
| 30 | } | |
| 31 |
1
1. parse : replaced return value with null for com/rox/LaunchOptions::parse → KILLED |
return new LaunchOptions(Optional.ofNullable(rom), Optional.ofNullable(controllers), |
| 32 |
1
1. parse : negated conditional → KILLED |
seconds == null ? OptionalInt.empty() : OptionalInt.of(seconds)); |
| 33 | } | |
| 34 | ||
| 35 | /** The value after the flag at {@code args[i]} - not another flag, so a missing value can't swallow one. */ | |
| 36 | private static String value(final String[] args, final int i){ | |
| 37 |
5
1. value : negated conditional → KILLED 2. value : Replaced integer addition with subtraction → KILLED 3. value : changed conditional boundary → KILLED 4. value : negated conditional → KILLED 5. value : Replaced integer addition with subtraction → KILLED |
if (i + 1 >= args.length || args[i + 1].startsWith("--")){ |
| 38 | throw new IllegalArgumentException(args[i] + " needs a value"); | |
| 39 | } | |
| 40 |
2
1. value : replaced return value with "" for com/rox/LaunchOptions::value → KILLED 2. value : Replaced integer addition with subtraction → KILLED |
return args[i + 1]; |
| 41 | } | |
| 42 | ||
| 43 | private static <T> T once(final String flag, final T previous, final T value){ | |
| 44 |
1
1. once : negated conditional → KILLED |
if (previous != null){ |
| 45 | throw new IllegalArgumentException(flag + " given more than once"); | |
| 46 | } | |
| 47 |
1
1. once : replaced return value with null for com/rox/LaunchOptions::once → KILLED |
return value; |
| 48 | } | |
| 49 | ||
| 50 | private static int seconds(final String value){ | |
| 51 | final int seconds; | |
| 52 | try { | |
| 53 | seconds = Integer.parseInt(value); | |
| 54 | } catch (NumberFormatException e){ | |
| 55 | throw new IllegalArgumentException("--seconds must be a whole number, not " + value); | |
| 56 | } | |
| 57 | if (seconds < 0){ | |
| 58 | throw new IllegalArgumentException("--seconds must be non-negative"); | |
| 59 | } | |
| 60 | return seconds; | |
| 61 | } | |
| 62 | } | |
Mutations | ||
| 22 |
1.1 2.2 |
|
| 31 |
1.1 |
|
| 32 |
1.1 |
|
| 37 |
1.1 2.2 3.3 4.4 5.5 |
|
| 40 |
1.1 2.2 |
|
| 44 |
1.1 |
|
| 47 |
1.1 |