| 1 | package com.renomad.minum.utils; | |
| 2 | ||
| 3 | import com.renomad.minum.security.ForbiddenUseException; | |
| 4 | import com.renomad.minum.security.Inmate; | |
| 5 | ||
| 6 | import java.util.ArrayList; | |
| 7 | import java.util.List; | |
| 8 | ||
| 9 | public final class SerializationUtils { | |
| 10 | ||
| 11 | private SerializationUtils() { | |
| 12 | // not meant to be constructed. | |
| 13 | } | |
| 14 | ||
| 15 | /** | |
| 16 | * This is a helper that will encode the values you give it | |
| 17 | * in preparation for storage in a database file. | |
| 18 | * <p> | |
| 19 | * The values will be encoded in URL-encoding (see {@link StringUtils#encode(String)}) | |
| 20 | * and concatenated together with pipe-symbol "|" delimiters. | |
| 21 | * </p> | |
| 22 | * <p> | |
| 23 | * <em>Please note</em>: You need to keep track of value order, | |
| 24 | * and making sure all the values are accounted for. | |
| 25 | * </p> | |
| 26 | * <p> | |
| 27 | * For example, see how this is used in {@link Inmate#serialize()} | |
| 28 | * </p> | |
| 29 | */ | |
| 30 | public static String serializeHelper(Object... values) { | |
| 31 | StringBuilder sb = new StringBuilder(); | |
| 32 |
3
1. serializeHelper : negated conditional → KILLED 2. serializeHelper : changed conditional boundary → KILLED 3. serializeHelper : Replaced integer subtraction with addition → KILLED |
for (int i = 0; i < values.length-1; i++) { |
| 33 |
1
1. serializeHelper : negated conditional → KILLED |
String value = values[i] == null ? null : values[i].toString(); |
| 34 | sb.append(StringUtils.encode(value)).append("|"); | |
| 35 | } | |
| 36 | // append the last value with no pipe symbol afterwards | |
| 37 |
3
1. serializeHelper : Replaced integer subtraction with addition → KILLED 2. serializeHelper : Replaced integer subtraction with addition → KILLED 3. serializeHelper : negated conditional → KILLED |
String lastValue = values[values.length - 1] == null ? null : values[values.length - 1].toString(); |
| 38 | sb.append(StringUtils.encode(lastValue)); | |
| 39 |
1
1. serializeHelper : replaced return value with "" for com/renomad/minum/utils/SerializationUtils::serializeHelper → KILLED |
return sb.toString(); |
| 40 | } | |
| 41 | ||
| 42 | /** | |
| 43 | * Splits up a string based on a pipe character. See {@link #tokenizer(String, char, int)} | |
| 44 | * <p> | |
| 45 | * This method is intended to be used as part of the database. See | |
| 46 | * the package "com.renomad.minum.database" | |
| 47 | * </p> | |
| 48 | * <p> | |
| 49 | * For an example, see how this is used in {@link Inmate#deserialize(String)} | |
| 50 | * </p> | |
| 51 | * @param serializedText the string we are splitting into tokens | |
| 52 | */ | |
| 53 | public static List<String> deserializeHelper(String serializedText) { | |
| 54 | /* | |
| 55 | * As a general precaution, loops throughout the system have | |
| 56 | * safety limits in place. In this case, it would be unexpected | |
| 57 | * to have databases of type {@link com.renomad.minum.database.DbData} that | |
| 58 | * have this many fields. | |
| 59 | */ | |
| 60 | int maximumDatabasePartitionsAllowed = 200; | |
| 61 |
1
1. deserializeHelper : replaced return value with Collections.emptyList for com/renomad/minum/utils/SerializationUtils::deserializeHelper → KILLED |
return tokenizer(serializedText, '|', maximumDatabasePartitionsAllowed).stream().map(StringUtils::decode).toList(); |
| 62 | } | |
| 63 | ||
| 64 | /** | |
| 65 | * Splits up a string into tokens. | |
| 66 | * | |
| 67 | * @param serializedText the string we are splitting up | |
| 68 | * @param delimiter the character acting as a boundary between sections | |
| 69 | * @param maxTokens the maximum tokens allowable. Probably smart to include a number | |
| 70 | * here, since otherwise you could get into some infinite loops. | |
| 71 | * @return a list of strings. If the delimiter is not found, we will just return the whole string | |
| 72 | */ | |
| 73 | public static List<String> tokenizer(String serializedText, char delimiter, int maxTokens) { | |
| 74 | final var resultList = new ArrayList<String>(); | |
| 75 | var currentPlace = 0; | |
| 76 |
1
1. tokenizer : Changed increment from 1 to -1 → KILLED |
for(int i = 0; ; i++) { |
| 77 |
2
1. tokenizer : changed conditional boundary → KILLED 2. tokenizer : negated conditional → KILLED |
if (i >= maxTokens) { |
| 78 | throw new ForbiddenUseException("Asked to split content into too many partitions in the tokenizer. Current max: " + maxTokens); | |
| 79 | } | |
| 80 | final var nextDelimiterIndex = serializedText.indexOf(delimiter, currentPlace); | |
| 81 |
1
1. tokenizer : negated conditional → KILLED |
if (nextDelimiterIndex == -1) { |
| 82 | // if we don't see any delimiters ahead, grab the rest of the text from our current place | |
| 83 | resultList.add(serializedText.substring(currentPlace)); | |
| 84 | break; | |
| 85 | } | |
| 86 | resultList.add(serializedText.substring(currentPlace, nextDelimiterIndex)); | |
| 87 |
1
1. tokenizer : Replaced integer addition with subtraction → KILLED |
currentPlace = nextDelimiterIndex + 1; |
| 88 | } | |
| 89 | ||
| 90 |
1
1. tokenizer : replaced return value with Collections.emptyList for com/renomad/minum/utils/SerializationUtils::tokenizer → KILLED |
return resultList; |
| 91 | } | |
| 92 | ||
| 93 | } | |
Mutations | ||
| 32 |
1.1 2.2 3.3 |
|
| 33 |
1.1 |
|
| 37 |
1.1 2.2 3.3 |
|
| 39 |
1.1 |
|
| 61 |
1.1 |
|
| 76 |
1.1 |
|
| 77 |
1.1 2.2 |
|
| 81 |
1.1 |
|
| 87 |
1.1 |
|
| 90 |
1.1 |