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 1 1. serializeHelper : negated conditional → KILLED
        if (values.length == 0) {
36
            throw new UtilsException("No values provided to serializeHelper");
37
        }
38
        StringBuilder sb = new StringBuilder();
39 3 1. serializeHelper : Replaced integer subtraction with addition → KILLED
2. serializeHelper : negated conditional → KILLED
3. serializeHelper : changed conditional boundary → KILLED
        for (int i = 0; i < values.length-1; i++) {
40 1 1. serializeHelper : negated conditional → KILLED
            String value = values[i] == null ? null : values[i].toString();
41
            sb.append(StringUtils.encode(value)).append("|");
42
        }
43
        // append the last value with no pipe symbol afterwards
44 3 1. serializeHelper : Replaced integer subtraction with addition → KILLED
2. serializeHelper : negated conditional → KILLED
3. serializeHelper : Replaced integer subtraction with addition → KILLED
        String lastValue = values[values.length - 1] == null ? null : values[values.length - 1].toString();
45
        sb.append(StringUtils.encode(lastValue));
46 1 1. serializeHelper : replaced return value with "" for com/renomad/minum/utils/SerializationUtils::serializeHelper → KILLED
        return sb.toString();
47
    }
48
49
    /**
50
     * Splits up a string based on a pipe character.  See {@link #tokenizer(String, char, int)}
51
     * <p>
52
     *     This method is intended to be used as part of the database.  See
53
     *     the package "com.renomad.minum.database"
54
     * </p>
55
     * <p>
56
     *     For an example, see how this is used in {@link Inmate#deserialize(String)}
57
     * </p>
58
     * @param serializedText the string we are splitting into tokens
59
     */
60
    public static List<String> deserializeHelper(String serializedText) {
61
        /*
62
         * As a general precaution, loops throughout the system have
63
         * safety limits in place.  In this case, it would be unexpected
64
         * to have databases of type {@link com.renomad.minum.database.DbData} that
65
         * have this many fields.
66
         */
67
        int maximumDatabasePartitionsAllowed = 200;
68 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();
69
    }
70
71
    /**
72
     * Splits up a string into tokens.
73
     *
74
     * @param serializedText the string we are splitting up
75
     * @param delimiter the character acting as a boundary between sections
76
     * @param maxTokens the maximum tokens allowable.  Probably smart to include a number
77
     *                  here, since otherwise you could get into some infinite loops.
78
     * @return a list of strings.  If the delimiter is not found, we will just return the whole string
79
     */
80
    public static List<String> tokenizer(String serializedText, char delimiter, int maxTokens) {
81
        final var resultList = new ArrayList<String>();
82
        var currentPlace = 0;
83 1 1. tokenizer : Changed increment from 1 to -1 → KILLED
        for(int i = 0; ; i++) {
84 2 1. tokenizer : changed conditional boundary → KILLED
2. tokenizer : negated conditional → KILLED
            if (i >= maxTokens) {
85
                throw new ForbiddenUseException("Asked to split content into too many partitions in the tokenizer.  Current max: " + maxTokens);
86
            }
87
            final var nextDelimiterIndex = serializedText.indexOf(delimiter, currentPlace);
88 1 1. tokenizer : negated conditional → KILLED
            if (nextDelimiterIndex == -1) {
89
                // if we don't see any delimiters ahead, grab the rest of the text from our current place
90
                resultList.add(serializedText.substring(currentPlace));
91
                break;
92
            }
93
            resultList.add(serializedText.substring(currentPlace, nextDelimiterIndex));
94 1 1. tokenizer : Replaced integer addition with subtraction → KILLED
            currentPlace = nextDelimiterIndex + 1;
95
        }
96
97 1 1. tokenizer : replaced return value with Collections.emptyList for com/renomad/minum/utils/SerializationUtils::tokenizer → KILLED
        return resultList;
98
    }
99
100
}

Mutations

35

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

39

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

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

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

40

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

44

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
negated conditional → KILLED

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

46

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

68

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

83

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

84

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

88

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

94

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

97

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