| 1 | package com.github.dakusui.symfonion.song; | |
| 2 | ||
| 3 | import com.github.dakusui.symfonion.compat.exceptions.SymfonionException; | |
| 4 | import com.github.dakusui.symfonion.compat.json.CompatJsonException; | |
| 5 | import com.github.dakusui.symfonion.compat.json.CompatJsonUtils; | |
| 6 | import com.google.gson.JsonArray; | |
| 7 | import com.google.gson.JsonElement; | |
| 8 | import com.google.gson.JsonObject; | |
| 9 | ||
| 10 | import java.util.Collections; | |
| 11 | import java.util.LinkedList; | |
| 12 | import java.util.List; | |
| 13 | import java.util.Map; | |
| 14 | ||
| 15 | import static com.github.dakusui.symfonion.compat.exceptions.CompatExceptionThrower.ContextKey.PART_MEASURE_JSON; | |
| 16 | import static com.github.dakusui.symfonion.compat.exceptions.CompatExceptionThrower.exceptionContext; | |
| 17 | import static com.github.dakusui.symfonion.compat.exceptions.CompatExceptionThrower.noteMapNotFoundException; | |
| 18 | import static com.github.dakusui.symfonion.compat.exceptions.ExceptionContext.entry; | |
| 19 | import static com.github.dakusui.symfonion.compat.json.CompatJsonUtils.asJsonArray; | |
| 20 | import static com.github.dakusui.symfonion.compat.json.CompatJsonUtils.asJsonElement; | |
| 21 | ||
| 22 | ||
| 23 | /** | |
| 24 | * A reusable unit which consists of a sequence of part measures. | |
| 25 | * | |
| 26 | * @see PartMeasure | |
| 27 | */ | |
| 28 | public class Pattern { | |
| 29 | private final List<PartMeasure> body; | |
| 30 | ||
| 31 | Pattern(JsonObject jsonObject, NoteMap noteMap) { | |
| 32 | // Initialize 'body'. | |
| 33 | this.body = new LinkedList<>(); | |
| 34 | PartMeasureParameters params = new PartMeasureParameters(jsonObject, noteMap); | |
| 35 | JsonArray bodyJSON; | |
| 36 |
1
1. <init> : negated conditional → KILLED |
if (asJsonElement(jsonObject, Keyword.body).isJsonPrimitive()) { |
| 37 | bodyJSON = new JsonArray(); | |
| 38 |
1
1. <init> : removed call to com/google/gson/JsonArray::add → KILLED |
bodyJSON.add(asJsonElement(jsonObject, Keyword.body)); |
| 39 | } else { | |
| 40 | bodyJSON = asJsonArray(jsonObject, Keyword.body); | |
| 41 | } | |
| 42 | int len = bodyJSON.size(); | |
| 43 |
2
1. <init> : negated conditional → KILLED 2. <init> : changed conditional boundary → KILLED |
for (int i = 0; i < len; i++) { |
| 44 | JsonElement cur = bodyJSON.get(i); | |
| 45 | try (var ignored = exceptionContext(entry(PART_MEASURE_JSON, cur))) { | |
| 46 | body.add(new PartMeasure(cur, params)); | |
| 47 | } | |
| 48 | } | |
| 49 | } | |
| 50 | ||
| 51 | /** | |
| 52 | * Returns a list of part measures which this `Pattern` consists of. | |
| 53 | * | |
| 54 | * @return A list of part measures. | |
| 55 | */ | |
| 56 | public List<PartMeasure> partMeasures() { | |
| 57 |
1
1. partMeasures : replaced return value with Collections.emptyList for com/github/dakusui/symfonion/song/Pattern::partMeasures → KILLED |
return Collections.unmodifiableList(this.body); |
| 58 | } | |
| 59 | ||
| 60 | /** | |
| 61 | * // @formatter:off | |
| 62 | * Creates an object of this class from a given `jsonObject` and `noteMap`. | |
| 63 | * The `jsonObject` can be either: | |
| 64 | * | |
| 65 | * [source, JSON] | |
| 66 | * ---- | |
| 67 | * { | |
| 68 | * "body": "{part measure string}", | |
| 69 | * } | |
| 70 | * ---- | |
| 71 | * | |
| 72 | * Or: | |
| 73 | * [source, JSON] | |
| 74 | * ---- | |
| 75 | * { | |
| 76 | * "body": [ | |
| 77 | * "{stroke 1}", | |
| 78 | * "{stroke 2}" | |
| 79 | * ], | |
| 80 | * "length": "<bodyValue>", | |
| 81 | * "gate": "<gateValue>", | |
| 82 | * "$otherParameter": "<otherParameterValue>", | |
| 83 | * } | |
| 84 | * ---- | |
| 85 | * // @formatter:on | |
| 86 | * | |
| 87 | * `<otherParameterValue>` can be one of `Pattern.Parameters`. | |
| 88 | * This method creates `Pattern` object and a note map (`Map<String, NoteMap>`) used for it is chosen from the `noteMaps` | |
| 89 | * passed to this method. | |
| 90 | * If no matching entry is found in `noteMaps`, an exception will be thrown. | |
| 91 | * | |
| 92 | * @param jsonObject A JSON object which contains the definition of a `Pattern` object. | |
| 93 | * @param noteMaps note maps available for this pattern. | |
| 94 | * @see NoteMap | |
| 95 | * @see PartMeasureParameters | |
| 96 | * @see PartMeasure | |
| 97 | */ | |
| 98 | public static Pattern createPattern(JsonObject jsonObject, Map<String, NoteMap> noteMaps) throws SymfonionException, CompatJsonException { | |
| 99 | NoteMap noteMap = NoteMap.defaultNoteMap; | |
| 100 |
1
1. createPattern : negated conditional → KILLED |
if (CompatJsonUtils.hasPath(jsonObject, Keyword.notemap)) { |
| 101 | String noteMapName = CompatJsonUtils.asString(jsonObject, Keyword.notemap); | |
| 102 | noteMap = noteMaps.get(noteMapName); | |
| 103 |
1
1. createPattern : negated conditional → KILLED |
if (noteMap == null) { |
| 104 | throw noteMapNotFoundException(asJsonElement(jsonObject, Keyword.notemap), noteMapName); | |
| 105 | } | |
| 106 | } | |
| 107 |
1
1. createPattern : replaced return value with null for com/github/dakusui/symfonion/song/Pattern::createPattern → KILLED |
return new Pattern(jsonObject, noteMap); |
| 108 | } | |
| 109 | } | |
Mutations | ||
| 36 |
1.1 |
|
| 38 |
1.1 |
|
| 43 |
1.1 2.2 |
|
| 57 |
1.1 |
|
| 100 |
1.1 |
|
| 103 |
1.1 |
|
| 107 |
1.1 |