FrequencyTrieBuilders.java

1
/*******************************************************************************
2
 * Copyright (C) 2026, Leo Galambos
3
 * All rights reserved.
4
 * 
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions are met:
7
 * 
8
 * 1. Redistributions of source code must retain the above copyright notice,
9
 *    this list of conditions and the following disclaimer.
10
 * 
11
 * 2. Redistributions in binary form must reproduce the above copyright notice,
12
 *    this list of conditions and the following disclaimer in the documentation
13
 *    and/or other materials provided with the distribution.
14
 * 
15
 * 3. Neither the name of the copyright holder nor the names of its contributors
16
 *    may be used to endorse or promote products derived from this software
17
 *    without specific prior written permission.
18
 * 
19
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29
 * POSSIBILITY OF SUCH DAMAGE.
30
 ******************************************************************************/
31
package org.egothor.stemmer;
32
33
import java.util.Objects;
34
import java.util.function.IntFunction;
35
import java.util.logging.Level;
36
import java.util.logging.Logger;
37
38
import org.egothor.stemmer.trie.CompiledNode;
39
40
/**
41
 * Factory utilities related to {@link FrequencyTrie.Builder}.
42
 *
43
 * <p>
44
 * This helper reconstructs writable builders from compiled read-only tries. The
45
 * reconstruction preserves the semantics and local counts of the compiled trie
46
 * as currently stored, which makes it suitable for subsequent modifications
47
 * followed by recompilation.
48
 *
49
 * <p>
50
 * Reconstruction operates on the compiled form. Therefore, if the compiled trie
51
 * was produced using a reduction mode that merged semantically equivalent
52
 * subtrees, the recreated builder reflects that reduced compiled state rather
53
 * than the exact original unreduced insertion history.
54
 */
55
public final class FrequencyTrieBuilders {
56
57
    /**
58
     * Logger of this class.
59
     */
60
    private static final Logger LOGGER = Logger.getLogger(FrequencyTrieBuilders.class.getName());
61
62
    /**
63
     * Utility class.
64
     */
65
    private FrequencyTrieBuilders() {
66
        throw new AssertionError("No instances.");
67
    }
68
69
    /**
70
     * Reconstructs a new writable builder from a compiled read-only trie.
71
     *
72
     * <p>
73
     * The returned builder contains the same key-local value counts as the supplied
74
     * compiled trie. Callers may continue modifying the returned builder and then
75
     * compile a new {@link FrequencyTrie} instance.
76
     *
77
     * @param source            source compiled trie
78
     * @param arrayFactory      array factory for the reconstructed builder
79
     * @param reductionSettings reduction settings to associate with the new builder
80
     * @param <V>               value type
81
     * @return reconstructed writable builder
82
     * @throws NullPointerException if any argument is {@code null}
83
     */
84
    public static <V> FrequencyTrie.Builder<V> copyOf(final FrequencyTrie<V> source,
85
            final IntFunction<V[]> arrayFactory, final ReductionSettings reductionSettings) {
86
        Objects.requireNonNull(source, "source");
87
        Objects.requireNonNull(arrayFactory, "arrayFactory");
88
        Objects.requireNonNull(reductionSettings, "reductionSettings");
89
90
        final FrequencyTrie.Builder<V> builder = new FrequencyTrie.Builder<>(arrayFactory, reductionSettings);
91
        final StringBuilder keyBuilder = new StringBuilder(64);
92
93 1 1. copyOf : removed call to org/egothor/stemmer/FrequencyTrieBuilders::copyNode → KILLED
        copyNode(source.root(), keyBuilder, builder);
94
95
        LOGGER.log(Level.FINE, "Reconstructed writable builder from compiled trie.");
96 1 1. copyOf : replaced return value with null for org/egothor/stemmer/FrequencyTrieBuilders::copyOf → KILLED
        return builder;
97
    }
98
99
    /**
100
     * Reconstructs a new writable builder from a compiled read-only trie using
101
     * default settings for the supplied reduction mode.
102
     *
103
     * @param source        source compiled trie
104
     * @param arrayFactory  array factory for the reconstructed builder
105
     * @param reductionMode reduction mode to associate with the new builder
106
     * @param <V>           value type
107
     * @return reconstructed writable builder
108
     * @throws NullPointerException if any argument is {@code null}
109
     */
110
    public static <V> FrequencyTrie.Builder<V> copyOf(final FrequencyTrie<V> source,
111
            final IntFunction<V[]> arrayFactory, final ReductionMode reductionMode) {
112
        Objects.requireNonNull(reductionMode, "reductionMode");
113 1 1. copyOf : replaced return value with null for org/egothor/stemmer/FrequencyTrieBuilders::copyOf → KILLED
        return copyOf(source, arrayFactory, ReductionSettings.withDefaults(reductionMode));
114
    }
115
116
    /**
117
     * Copies one compiled node and all reachable descendants into the target
118
     * builder.
119
     *
120
     * @param node       current compiled node
121
     * @param keyBuilder current key builder
122
     * @param builder    target mutable builder
123
     * @param <V>        value type
124
     */
125
    private static <V> void copyNode(final CompiledNode<V> node, final StringBuilder keyBuilder,
126
            final FrequencyTrie.Builder<V> builder) {
127 2 1. copyNode : changed conditional boundary → KILLED
2. copyNode : negated conditional → KILLED
        for (int valueIndex = 0; valueIndex < node.orderedValues().length; valueIndex++) {
128
            builder.put(keyBuilder.toString(), node.orderedValues()[valueIndex], node.orderedCounts()[valueIndex]);
129
        }
130
131 2 1. copyNode : changed conditional boundary → KILLED
2. copyNode : negated conditional → KILLED
        for (int childIndex = 0; childIndex < node.edgeLabels().length; childIndex++) {
132
            keyBuilder.append(node.edgeLabels()[childIndex]);
133 1 1. copyNode : removed call to org/egothor/stemmer/FrequencyTrieBuilders::copyNode → KILLED
            copyNode(node.children()[childIndex], keyBuilder, builder);
134 2 1. copyNode : Replaced integer subtraction with addition → KILLED
2. copyNode : removed call to java/lang/StringBuilder::setLength → KILLED
            keyBuilder.setLength(keyBuilder.length() - 1);
135
        }
136
    }
137
}

Mutations

93

1.1
Location : copyOf
Killed by : org.egothor.stemmer.FrequencyTrieBuildersTest.[engine:junit-jupiter]/[class:org.egothor.stemmer.FrequencyTrieBuildersTest]/[method:shouldPreserveLocalCountsAndOrdering()]
removed call to org/egothor/stemmer/FrequencyTrieBuilders::copyNode → KILLED

96

1.1
Location : copyOf
Killed by : org.egothor.stemmer.FrequencyTrieBuildersTest.[engine:junit-jupiter]/[class:org.egothor.stemmer.FrequencyTrieBuildersTest]/[method:shouldReconstructEmptyTrie()]
replaced return value with null for org/egothor/stemmer/FrequencyTrieBuilders::copyOf → KILLED

113

1.1
Location : copyOf
Killed by : org.egothor.stemmer.FrequencyTrieBuildersTest.[engine:junit-jupiter]/[class:org.egothor.stemmer.FrequencyTrieBuildersTest]/[method:shouldReconstructUsingReductionModeShortcut()]
replaced return value with null for org/egothor/stemmer/FrequencyTrieBuilders::copyOf → KILLED

127

1.1
Location : copyNode
Killed by : org.egothor.stemmer.FrequencyTrieBuildersTest.[engine:junit-jupiter]/[class:org.egothor.stemmer.FrequencyTrieBuildersTest]/[method:shouldReconstructEmptyTrie()]
changed conditional boundary → KILLED

2.2
Location : copyNode
Killed by : org.egothor.stemmer.FrequencyTrieBuildersTest.[engine:junit-jupiter]/[class:org.egothor.stemmer.FrequencyTrieBuildersTest]/[method:shouldReconstructEmptyTrie()]
negated conditional → KILLED

131

1.1
Location : copyNode
Killed by : org.egothor.stemmer.FrequencyTrieBuildersTest.[engine:junit-jupiter]/[class:org.egothor.stemmer.FrequencyTrieBuildersTest]/[method:shouldReconstructEmptyTrie()]
changed conditional boundary → KILLED

2.2
Location : copyNode
Killed by : org.egothor.stemmer.FrequencyTrieBuildersTest.[engine:junit-jupiter]/[class:org.egothor.stemmer.FrequencyTrieBuildersTest]/[method:shouldReconstructEmptyTrie()]
negated conditional → KILLED

133

1.1
Location : copyNode
Killed by : org.egothor.stemmer.FrequencyTrieBuildersTest.[engine:junit-jupiter]/[class:org.egothor.stemmer.FrequencyTrieBuildersTest]/[method:shouldPreserveLocalCountsAndOrdering()]
removed call to org/egothor/stemmer/FrequencyTrieBuilders::copyNode → KILLED

134

1.1
Location : copyNode
Killed by : org.egothor.stemmer.FrequencyTrieBuildersTest.[engine:junit-jupiter]/[class:org.egothor.stemmer.FrequencyTrieBuildersTest]/[method:shouldPreserveSiblingBranchesUnderSharedPrefix()]
Replaced integer subtraction with addition → KILLED

2.2
Location : copyNode
Killed by : org.egothor.stemmer.FrequencyTrieBuildersTest.[engine:junit-jupiter]/[class:org.egothor.stemmer.FrequencyTrieBuildersTest]/[method:shouldPreserveSiblingBranchesUnderSharedPrefix()]
removed call to java/lang/StringBuilder::setLength → KILLED

Active mutators

Tests examined


Report generated by PIT 1.22.1