SerializationUtils.java

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

Mutations

36

1.1
Location : serializeHelper
Killed by : com.renomad.minum.utils.SerializationUtilsTests
negated conditional → KILLED

2.2
Location : serializeHelper
Killed by : com.renomad.minum.utils.SerializationUtilsTests
changed conditional boundary → KILLED

3.3
Location : serializeHelper
Killed by : com.renomad.minum.utils.SerializationUtilsTests
Replaced integer subtraction with addition → KILLED

37

1.1
Location : serializeHelper
Killed by : com.renomad.minum.utils.SerializationUtilsTests
negated conditional → KILLED

41

1.1
Location : serializeHelper
Killed by : com.renomad.minum.web.WebTests
Replaced integer subtraction with addition → KILLED

2.2
Location : serializeHelper
Killed by : com.renomad.minum.utils.SerializationUtilsTests
Replaced integer subtraction with addition → KILLED

3.3
Location : serializeHelper
Killed by : com.renomad.minum.utils.SerializationUtilsTests
negated conditional → KILLED

43

1.1
Location : serializeHelper
Killed by : com.renomad.minum.utils.SerializationUtilsTests
replaced return value with "" for com/renomad/minum/utils/SerializationUtils::serializeHelper → KILLED

65

1.1
Location : deserializeHelper
Killed by : com.renomad.minum.security.TheBrigTests
replaced return value with Collections.emptyList for com/renomad/minum/utils/SerializationUtils::deserializeHelper → KILLED

80

1.1
Location : tokenizer
Killed by : com.renomad.minum.utils.SerializationUtilsTests
Changed increment from 1 to -1 → KILLED

81

1.1
Location : tokenizer
Killed by : com.renomad.minum.utils.SerializationUtilsTests
changed conditional boundary → KILLED

2.2
Location : tokenizer
Killed by : com.renomad.minum.utils.SerializationUtilsTests
negated conditional → KILLED

85

1.1
Location : tokenizer
Killed by : com.renomad.minum.utils.SerializationUtilsTests
negated conditional → KILLED

91

1.1
Location : tokenizer
Killed by : com.renomad.minum.utils.SerializationUtilsTests
Replaced integer addition with subtraction → KILLED

94

1.1
Location : tokenizer
Killed by : com.renomad.minum.utils.SerializationUtilsTests
replaced return value with Collections.emptyList for com/renomad/minum/utils/SerializationUtils::tokenizer → KILLED

Active mutators

Tests examined


Report generated by PIT 1.17.0