| 1 | package com.github.dakusui.symfonion.utils; | |
| 2 | ||
| 3 | import com.github.dakusui.symfonion.exceptions.SymfonionException; | |
| 4 | import com.github.dakusui.valid8j_pcond.forms.Printables; | |
| 5 | ||
| 6 | import java.io.*; | |
| 7 | import java.util.Optional; | |
| 8 | import java.util.concurrent.atomic.AtomicReference; | |
| 9 | import java.util.function.BiFunction; | |
| 10 | import java.util.function.Predicate; | |
| 11 | import java.util.regex.Matcher; | |
| 12 | import java.util.stream.Collector; | |
| 13 | ||
| 14 | import static com.github.dakusui.symfonion.exceptions.ExceptionThrower.*; | |
| 15 | import static com.github.dakusui.valid8j.Requires.require; | |
| 16 | import static com.github.dakusui.valid8j_pcond.forms.Predicates.isNotNull; | |
| 17 | import static java.nio.charset.StandardCharsets.UTF_8; | |
| 18 | ||
| 19 | ||
| 20 | public enum Utils { | |
| 21 | ; | |
| 22 | public static final java.util.regex.Pattern lengthPattern = java.util.regex.Pattern.compile("([1-9][0-9]*)(\\.*)"); | |
| 23 | ||
| 24 | /** | |
| 25 | * Count occurrences of a given character {@code ch} in a string {@code s}. | |
| 26 | * | |
| 27 | * @param ch A character to count its occurrences. | |
| 28 | * @param s A string in which the number of {@code ch} should be counted. | |
| 29 | * @return The number of occurrences of {@code ch} in string {@code s}. | |
| 30 | */ | |
| 31 | public static int count(char ch, String s) { | |
| 32 | int ret = 0; | |
| 33 |
3
1. count : negated conditional → TIMED_OUT 2. count : Replaced integer addition with subtraction → TIMED_OUT 3. count : changed conditional boundary → KILLED |
for (int i = s.indexOf(ch); i >= 0; i = s.indexOf(ch, i + 1)) { |
| 34 |
1
1. count : Changed increment from 1 to -1 → KILLED |
ret++; |
| 35 | } | |
| 36 |
1
1. count : replaced int return with 0 for com/github/dakusui/symfonion/utils/Utils::count → KILLED |
return ret; |
| 37 | } | |
| 38 | ||
| 39 | public static String loadResource(String resourceName) throws SymfonionException { | |
| 40 | StringBuffer b = new StringBuffer(4096); | |
| 41 | try { | |
| 42 | InputStream is = new BufferedInputStream(require(ClassLoader.getSystemResourceAsStream(resourceName), resourceIsNotNull(resourceName))); | |
| 43 |
1
1. loadResource : removed call to com/github/dakusui/symfonion/utils/Utils::loadFromInputStream → KILLED |
loadFromInputStream(b, is); |
| 44 | } catch (IOException e) { | |
| 45 | throw loadResourceException(resourceName, e); | |
| 46 | } | |
| 47 |
1
1. loadResource : replaced return value with "" for com/github/dakusui/symfonion/utils/Utils::loadResource → KILLED |
return b.toString(); |
| 48 | } | |
| 49 | ||
| 50 | public static String loadFile(String fileName) throws SymfonionException { | |
| 51 | StringBuffer b = new StringBuffer(4096); | |
| 52 | File f = new File(fileName); | |
| 53 | try (InputStream is = new BufferedInputStream(new FileInputStream(f))){ | |
| 54 |
1
1. loadFile : removed call to com/github/dakusui/symfonion/utils/Utils::loadFromInputStream → KILLED |
loadFromInputStream(b, is); |
| 55 | } catch (FileNotFoundException e) { | |
| 56 | throw fileNotFoundException(f, e); | |
| 57 | } catch (IOException e) { | |
| 58 | throw loadFileException(e); | |
| 59 | } | |
| 60 |
1
1. loadFile : replaced return value with "" for com/github/dakusui/symfonion/utils/Utils::loadFile → KILLED |
return b.toString(); |
| 61 | } | |
| 62 | ||
| 63 | private static void loadFromInputStream(StringBuffer b, InputStream is) throws IOException { | |
| 64 | Reader r = new InputStreamReader(is, UTF_8); | |
| 65 | int c; | |
| 66 |
1
1. loadFromInputStream : negated conditional → TIMED_OUT |
while ((c = r.read()) != -1) { |
| 67 | b.append((char) c); | |
| 68 | } | |
| 69 | } | |
| 70 | ||
| 71 | public static Fraction parseNoteLength(String length) { | |
| 72 | Matcher m = lengthPattern.matcher(length); | |
| 73 | Fraction ret = null; | |
| 74 |
1
1. parseNoteLength : negated conditional → KILLED |
if (m.matches()) { |
| 75 | int l = Integer.parseInt(m.group(1)); | |
| 76 | ret = new Fraction(1, l); | |
| 77 | int dots = Utils.count('.', m.group(2)); | |
| 78 |
2
1. parseNoteLength : changed conditional boundary → KILLED 2. parseNoteLength : negated conditional → KILLED |
for (int i = 0; i < dots; i++) { |
| 79 |
1
1. parseNoteLength : Replaced integer multiplication with division → KILLED |
l *= 2; |
| 80 | ret = Fraction.add(ret, new Fraction(1, l)); | |
| 81 | } | |
| 82 |
1
1. parseNoteLength : negated conditional → KILLED |
} else if (!"0".equals(length)) { |
| 83 | } else { | |
| 84 | ret = new Fraction(0, 1); | |
| 85 | } | |
| 86 |
1
1. parseNoteLength : replaced return value with null for com/github/dakusui/symfonion/utils/Utils::parseNoteLength → KILLED |
return ret; |
| 87 | } | |
| 88 | ||
| 89 | public static byte[] getIntBytes(int input) { | |
| 90 | byte[] ret = new byte[3]; | |
| 91 | ||
| 92 |
2
1. getIntBytes : Replaced bitwise AND with OR → KILLED 2. getIntBytes : Replaced Shift Right with Shift Left → KILLED |
ret[0] = (byte) (input >> 16 & 0xff); |
| 93 |
2
1. getIntBytes : Replaced bitwise AND with OR → KILLED 2. getIntBytes : Replaced Shift Right with Shift Left → KILLED |
ret[1] = (byte) (input >> 8 & 0xff); |
| 94 |
1
1. getIntBytes : Replaced bitwise AND with OR → KILLED |
ret[2] = (byte) (input & 0xff); |
| 95 | ||
| 96 |
1
1. getIntBytes : replaced return value with null for com/github/dakusui/symfonion/utils/Utils::getIntBytes → KILLED |
return ret; |
| 97 | } | |
| 98 | ||
| 99 | /** | |
| 100 | * This method was copied from <a href="https://stackoverflow.com/questions/22694884/filter-java-stream-to-1-and-only-1-element/22695424#22695424">stackoverflow.com</a> and renamed. | |
| 101 | * | |
| 102 | * @param <E> Type of the element to be collected. | |
| 103 | * @return A collector | |
| 104 | */ | |
| 105 | public static <E> Collector<E, ?, Optional<E>> onlyElement() { | |
| 106 |
1
1. onlyElement : replaced return value with null for com/github/dakusui/symfonion/utils/Utils::onlyElement → NO_COVERAGE |
return onlyElement((e1, e2) -> { |
| 107 | throw new IllegalArgumentException("Multiple values are found in the stream: <" + e1 + "> and <" + e2 + ">"); | |
| 108 | }); | |
| 109 | } | |
| 110 | ||
| 111 | ||
| 112 | public static <E> Collector<E, AtomicReference<E>, Optional<E>> onlyElement(BiFunction<E, E, ? extends RuntimeException> multipleElements) { | |
| 113 |
1
1. onlyElement : replaced return value with null for com/github/dakusui/symfonion/utils/Utils::onlyElement → KILLED |
return Collector.of( |
| 114 | AtomicReference::new, | |
| 115 | (ref, e) -> { | |
| 116 |
1
1. lambda$onlyElement$1 : negated conditional → KILLED |
if (!ref.compareAndSet(null, e)) { |
| 117 | throw multipleElements.apply(ref.get(), e); | |
| 118 | } | |
| 119 | }, | |
| 120 | (ref1, ref2) -> { | |
| 121 |
1
1. lambda$onlyElement$2 : negated conditional → NO_COVERAGE |
if (ref1.get() == null) { |
| 122 |
1
1. lambda$onlyElement$2 : replaced return value with null for com/github/dakusui/symfonion/utils/Utils::lambda$onlyElement$2 → NO_COVERAGE |
return ref2; |
| 123 |
1
1. lambda$onlyElement$2 : negated conditional → NO_COVERAGE |
} else if (ref2.get() != null) { |
| 124 | throw multipleElements.apply(ref1.get(), ref2.get()); | |
| 125 | } else { | |
| 126 |
1
1. lambda$onlyElement$2 : replaced return value with null for com/github/dakusui/symfonion/utils/Utils::lambda$onlyElement$2 → NO_COVERAGE |
return ref1; |
| 127 | } | |
| 128 | }, | |
| 129 |
1
1. lambda$onlyElement$3 : replaced return value with Optional.empty for com/github/dakusui/symfonion/utils/Utils::lambda$onlyElement$3 → KILLED |
ref -> Optional.ofNullable(ref.get()), |
| 130 | Collector.Characteristics.UNORDERED); | |
| 131 | } | |
| 132 | ||
| 133 | private static Predicate<InputStream> resourceIsNotNull(String resourceName) { | |
| 134 |
2
1. lambda$resourceIsNotNull$4 : replaced return value with "" for com/github/dakusui/symfonion/utils/Utils::lambda$resourceIsNotNull$4 → SURVIVED 2. resourceIsNotNull : replaced return value with null for com/github/dakusui/symfonion/utils/Utils::resourceIsNotNull → KILLED |
return Printables.predicate(() -> "isNotNull[resourceLoadedFrom[" + resourceName + "]]", isNotNull()); |
| 135 | } | |
| 136 | ||
| 137 | } | |
Mutations | ||
| 33 |
1.1 2.2 3.3 |
|
| 34 |
1.1 |
|
| 36 |
1.1 |
|
| 43 |
1.1 |
|
| 47 |
1.1 |
|
| 54 |
1.1 |
|
| 60 |
1.1 |
|
| 66 |
1.1 |
|
| 74 |
1.1 |
|
| 78 |
1.1 2.2 |
|
| 79 |
1.1 |
|
| 82 |
1.1 |
|
| 86 |
1.1 |
|
| 92 |
1.1 2.2 |
|
| 93 |
1.1 2.2 |
|
| 94 |
1.1 |
|
| 96 |
1.1 |
|
| 106 |
1.1 |
|
| 113 |
1.1 |
|
| 116 |
1.1 |
|
| 121 |
1.1 |
|
| 122 |
1.1 |
|
| 123 |
1.1 |
|
| 126 |
1.1 |
|
| 129 |
1.1 |
|
| 134 |
1.1 2.2 |