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
                source.traversalDirection(), source.metadata().caseProcessingMode(),
92
                source.metadata().diacriticProcessingMode());
93
        final StringBuilder keyBuilder = new StringBuilder(64);
94
95 1 1. copyOf : removed call to org/egothor/stemmer/FrequencyTrieBuilders::copyNode → KILLED
        copyNode(source.root(), keyBuilder, builder, source.traversalDirection());
96
97
        LOGGER.log(Level.FINE, "Reconstructed writable builder from compiled trie.");
98 1 1. copyOf : replaced return value with null for org/egothor/stemmer/FrequencyTrieBuilders::copyOf → KILLED
        return builder;
99
    }
100
101
    /**
102
     * Reconstructs a new writable builder from a compiled read-only trie using
103
     * default settings for the supplied reduction mode.
104
     *
105
     * @param source        source compiled trie
106
     * @param arrayFactory  array factory for the reconstructed builder
107
     * @param reductionMode reduction mode to associate with the new builder
108
     * @param <V>           value type
109
     * @return reconstructed writable builder
110
     * @throws NullPointerException if any argument is {@code null}
111
     */
112
    public static <V> FrequencyTrie.Builder<V> copyOf(final FrequencyTrie<V> source,
113
            final IntFunction<V[]> arrayFactory, final ReductionMode reductionMode) {
114
        Objects.requireNonNull(reductionMode, "reductionMode");
115 1 1. copyOf : replaced return value with null for org/egothor/stemmer/FrequencyTrieBuilders::copyOf → KILLED
        return copyOf(source, arrayFactory, ReductionSettings.withDefaults(reductionMode));
116
    }
117
118
    /**
119
     * Copies one compiled node and all reachable descendants into the target
120
     * builder.
121
     *
122
     * @param node       current compiled node
123
     * @param keyBuilder current key builder
124
     * @param builder            target mutable builder
125
     * @param traversalDirection logical key traversal direction used by the source
126
     * @param <V>                 value type
127
     */
128
    private static <V> void copyNode(final CompiledNode<V> node, final StringBuilder keyBuilder,
129
            final FrequencyTrie.Builder<V> builder, final WordTraversalDirection traversalDirection) {
130
        final String logicalKey = traversalDirection.traversalPathToLogicalKey(keyBuilder);
131 2 1. copyNode : changed conditional boundary → KILLED
2. copyNode : negated conditional → KILLED
        for (int valueIndex = 0; valueIndex < node.orderedValues().length; valueIndex++) {
132
            builder.put(logicalKey, node.orderedValues()[valueIndex], node.orderedCounts()[valueIndex]);
133
        }
134
135 2 1. copyNode : changed conditional boundary → KILLED
2. copyNode : negated conditional → KILLED
        for (int childIndex = 0; childIndex < node.edgeLabels().length; childIndex++) {
136
            keyBuilder.append(node.edgeLabels()[childIndex]);
137 1 1. copyNode : removed call to org/egothor/stemmer/FrequencyTrieBuilders::copyNode → KILLED
            copyNode(node.children()[childIndex], keyBuilder, builder, traversalDirection);
138 2 1. copyNode : Replaced integer subtraction with addition → KILLED
2. copyNode : removed call to java/lang/StringBuilder::setLength → KILLED
            keyBuilder.setLength(keyBuilder.length() - 1);
139
        }
140
    }
141
}

Mutations

95

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

98

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

115

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

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

135

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

137

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

138

1.1
Location : copyNode
Killed by : org.egothor.stemmer.FrequencyTrieBuildersTest.[engine:junit-jupiter]/[class:org.egothor.stemmer.FrequencyTrieBuildersTest]/[method:shouldReconstructUsingReductionModeShortcut()]
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:shouldReconstructUsingReductionModeShortcut()]
removed call to java/lang/StringBuilder::setLength → KILLED

Active mutators

Tests examined


Report generated by PIT 1.22.1