| 1 | package com.roxoft.test; | |
| 2 | ||
| 3 | import java.util.ArrayList; | |
| 4 | import java.util.Arrays; | |
| 5 | import java.util.List; | |
| 6 | ||
| 7 | public final class RomanNumeral { | |
| 8 | /** Roman notation for ZERO. */ | |
| 9 | public static final String ZERO = "NULLA"; | |
| 10 | ||
| 11 | /** Characters which cannot be repeated in a Roman numeral. */ | |
| 12 | private static final String UNREPEATABLES = "VLD"; | |
| 13 | /** Characters which can be used as a subtractive in a Roman numeral. */ | |
| 14 | private static final String SUBTRACTIVE = "IXC"; | |
| 15 | ||
| 16 | /** The Roman numeral String in question. */ | |
| 17 | private final String numeral; | |
| 18 | /** A numerical representation of the Roman numeral in question. */ | |
| 19 | private final int value; | |
| 20 | ||
| 21 | @SuppressWarnings("magicnumber") | |
| 22 | private int characterValue(final String romanCharacter) { | |
| 23 |
1
1. characterValue : replaced int return with 0 for com/roxoft/RomanNumeral::characterValue → KILLED |
return switch (romanCharacter) { |
| 24 | case "I" -> 1; | |
| 25 | case "V" -> 5; | |
| 26 | case "X" -> 10; | |
| 27 | case "L" -> 50; | |
| 28 | case "C" -> 100; | |
| 29 | case "D" -> 500; | |
| 30 | case "M" -> 1000; | |
| 31 | default -> | |
| 32 | throw new InvalidRomanNumeralException("'" | |
| 33 | + romanCharacter | |
| 34 | + "' is an unknown Roman numeral character" | |
| 35 | ); | |
| 36 | }; | |
| 37 | } | |
| 38 | ||
| 39 | private RomanNumeral(final String numeralString) { | |
| 40 |
1
1. <init> : negated conditional → KILLED |
if (numeralString.isEmpty()) { |
| 41 | value = 0; | |
| 42 | this.numeral = ZERO; | |
| 43 | return; | |
| 44 | } else { | |
| 45 | this.numeral = numeralString; | |
| 46 | } | |
| 47 | ||
| 48 | //get all the characters that are the same | |
| 49 | final List<Integer> asIntegers = Arrays.stream(numeralString.split("")) | |
| 50 | .map(this::characterValue) | |
| 51 | .toList(); | |
| 52 | final List<Integer> calculatedValues = new ArrayList<>(List.of(asIntegers.get(0))); | |
| 53 | ||
| 54 | for (int characterIndex = 1, | |
| 55 | previousValue = calculatedValues.get(0), | |
| 56 | calculatedValueIndex = 0, | |
| 57 | characterRepetitionCount = 1; | |
| 58 |
2
1. <init> : changed conditional boundary → KILLED 2. <init> : negated conditional → KILLED |
characterIndex < asIntegers.size(); characterIndex++) { |
| 59 | int subject = asIntegers.get(characterIndex); | |
| 60 | ||
| 61 |
2
1. <init> : negated conditional → KILLED 2. <init> : changed conditional boundary → KILLED |
if (subject > previousValue) { |
| 62 | characterRepetitionCount = 1; | |
| 63 |
2
1. <init> : Replaced integer subtraction with addition → KILLED 2. <init> : negated conditional → KILLED |
if (SUBTRACTIVE.contains(numeralString.substring(characterIndex - 1, characterIndex))) { |
| 64 |
1
1. <init> : Replaced integer subtraction with addition → KILLED |
calculatedValues.set(calculatedValueIndex, subject - calculatedValues.get(calculatedValueIndex)); |
| 65 | } else { | |
| 66 | throw new InvalidRomanNumeralException("Use of '" + numeralString.charAt(characterIndex) + "' as subtractive character. Only I, X and C can be used as subtractive numerals."); | |
| 67 | } | |
| 68 | } else if (subject == previousValue) { | |
| 69 |
1
1. <init> : negated conditional → KILLED |
if (UNREPEATABLES.contains(numeralString.substring(characterIndex, characterIndex + 1))) { |
| 70 |
2
1. <init> : Replaced integer addition with subtraction → KILLED 2. <init> : negated conditional → KILLED |
throw new InvalidRomanNumeralException("Repeated instance of '" + numeralString.charAt(characterIndex) + "'. V, L or D cannot be repeated."); |
| 71 | } | |
| 72 | if (++characterRepetitionCount > 3) { | |
| 73 |
3
1. <init> : changed conditional boundary → KILLED 2. <init> : negated conditional → KILLED 3. <init> : Changed increment from 1 to -1 → KILLED |
throw new InvalidRomanNumeralException("Roman characters (in this case '" + numeralString.charAt(characterIndex) + "') cannot repeat more than 3 times"); |
| 74 | } | |
| 75 | calculatedValues.set(calculatedValueIndex, calculatedValues.get(calculatedValueIndex) + subject); | |
| 76 |
1
1. <init> : Replaced integer addition with subtraction → KILLED |
} else { |
| 77 | characterRepetitionCount = 1; | |
| 78 | calculatedValues.add(subject); | |
| 79 | calculatedValueIndex++; | |
| 80 |
1
1. <init> : Changed increment from 1 to -1 → KILLED |
} |
| 81 | previousValue = subject; | |
| 82 | } | |
| 83 | ||
| 84 | value = calculatedValues.stream().mapToInt(i -> i).sum(); | |
| 85 |
1
1. lambda$new$0 : replaced int return with 0 for com/roxoft/RomanNumeral::lambda$new$0 → KILLED |
|
| 86 | } | |
| 87 | ||
| 88 | /** | |
| 89 | * @param numeralString a Roman numeral String. | |
| 90 | * @return a {@link RomanNumeral} of the provided String. | |
| 91 | */ | |
| 92 | public static RomanNumeral of(final String numeralString) { | |
| 93 | return new RomanNumeral(numeralString); | |
| 94 |
1
1. of : replaced return value with null for com/roxoft/RomanNumeral::of → KILLED |
} |
| 95 | ||
| 96 | /** @return the numerical value of this Roman numeral. */ | |
| 97 | public int numericalValue() { | |
| 98 | return value; | |
| 99 |
1
1. numericalValue : replaced int return with 0 for com/roxoft/RomanNumeral::numericalValue → KILLED |
} |
| 100 | ||
| 101 | /** @return the String representation of this Roman numeral. */ | |
| 102 | public String romanNotation() { | |
| 103 | return numeral; | |
| 104 |
1
1. romanNotation : replaced return value with "" for com/roxoft/RomanNumeral::romanNotation → KILLED |
} |
| 105 | } | |
Mutations | ||
| 23 |
1.1 |
|
| 40 |
1.1 |
|
| 58 |
1.1 2.2 |
|
| 61 |
1.1 2.2 |
|
| 63 |
1.1 2.2 |
|
| 64 |
1.1 |
|
| 69 |
1.1 |
|
| 70 |
1.1 2.2 |
|
| 73 |
1.1 2.2 3.3 |
|
| 76 |
1.1 |
|
| 80 |
1.1 |
|
| 85 |
1.1 |
|
| 94 |
1.1 |
|
| 99 |
1.1 |
|
| 104 |
1.1 |