| 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.trie; | |
| 32 | ||
| 33 | import java.util.LinkedHashMap; | |
| 34 | import java.util.Map; | |
| 35 | ||
| 36 | /** | |
| 37 | * Canonical reduced node used during subtree merging. | |
| 38 | * | |
| 39 | * <p> | |
| 40 | * The maps exposed by the accessors are the internal backing state of the | |
| 41 | * canonical reduced node. They are returned directly for efficiency and are | |
| 42 | * intended only for closely related trie-reduction infrastructure. | |
| 43 | * | |
| 44 | * @param <V> value type | |
| 45 | */ | |
| 46 | public final class ReducedNode<V> { | |
| 47 | ||
| 48 | /** | |
| 49 | * Reduction signature. | |
| 50 | */ | |
| 51 | private final ReductionSignature<V> signature; | |
| 52 | ||
| 53 | /** | |
| 54 | * Aggregated local value counts. | |
| 55 | */ | |
| 56 | private final Map<V, Integer> localCounts; | |
| 57 | ||
| 58 | /** | |
| 59 | * Canonical children by edge. | |
| 60 | */ | |
| 61 | private final Map<Character, ReducedNode<V>> children; | |
| 62 | ||
| 63 | /** | |
| 64 | * Creates a new reduced node. | |
| 65 | * | |
| 66 | * @param signature reduction signature | |
| 67 | * @param localCounts local counts | |
| 68 | * @param children children | |
| 69 | */ | |
| 70 | public ReducedNode(final ReductionSignature<V> signature, final Map<V, Integer> localCounts, | |
| 71 | final Map<Character, ReducedNode<V>> children) { | |
| 72 | this.signature = signature; | |
| 73 | this.localCounts = new LinkedHashMap<>(localCounts); | |
| 74 | this.children = new LinkedHashMap<>(children); | |
| 75 | } | |
| 76 | ||
| 77 | /** | |
| 78 | * Returns the reduction signature of this canonical node. | |
| 79 | * | |
| 80 | * @return reduction signature | |
| 81 | */ | |
| 82 | public ReductionSignature<V> signature() { | |
| 83 |
1
1. signature : replaced return value with null for org/egothor/stemmer/trie/ReducedNode::signature → KILLED |
return this.signature; |
| 84 | } | |
| 85 | ||
| 86 | /** | |
| 87 | * Returns the internal aggregated local value-count map. | |
| 88 | * | |
| 89 | * <p> | |
| 90 | * The returned map is the internal backing state of this canonical reduced node | |
| 91 | * and is exposed only for efficient cooperation with trie-reduction | |
| 92 | * infrastructure. | |
| 93 | * | |
| 94 | * @return internal aggregated local value-count map | |
| 95 | */ | |
| 96 | public Map<V, Integer> localCounts() { | |
| 97 |
1
1. localCounts : replaced return value with Collections.emptyMap for org/egothor/stemmer/trie/ReducedNode::localCounts → KILLED |
return this.localCounts; |
| 98 | } | |
| 99 | ||
| 100 | /** | |
| 101 | * Returns the internal canonical child map indexed by transition character. | |
| 102 | * | |
| 103 | * <p> | |
| 104 | * The returned map is the internal backing state of this canonical reduced node | |
| 105 | * and is exposed only for efficient cooperation with trie-reduction | |
| 106 | * infrastructure. | |
| 107 | * | |
| 108 | * @return internal canonical child map | |
| 109 | */ | |
| 110 | public Map<Character, ReducedNode<V>> children() { | |
| 111 |
1
1. children : replaced return value with Collections.emptyMap for org/egothor/stemmer/trie/ReducedNode::children → KILLED |
return this.children; |
| 112 | } | |
| 113 | ||
| 114 | /** | |
| 115 | * Merges additional local counts into this node. | |
| 116 | * | |
| 117 | * @param additionalCounts additional local counts | |
| 118 | */ | |
| 119 | public void mergeLocalCounts(final Map<V, Integer> additionalCounts) { | |
| 120 | for (Map.Entry<V, Integer> entry : additionalCounts.entrySet()) { | |
| 121 | final Integer previous = this.localCounts.get(entry.getKey()); | |
| 122 |
1
1. mergeLocalCounts : negated conditional → KILLED |
if (previous == null) { |
| 123 | this.localCounts.put(entry.getKey(), entry.getValue()); | |
| 124 | } else { | |
| 125 |
1
1. mergeLocalCounts : Replaced integer addition with subtraction → KILLED |
this.localCounts.put(entry.getKey(), previous + entry.getValue()); |
| 126 | } | |
| 127 | } | |
| 128 | } | |
| 129 | ||
| 130 | /** | |
| 131 | * Merges child references into this node. | |
| 132 | * | |
| 133 | * <p> | |
| 134 | * For nodes with the same reduction signature, child edge sets and child | |
| 135 | * signatures must be compatible. This method therefore only needs to verify | |
| 136 | * consistency and store the canonical child instance. | |
| 137 | * | |
| 138 | * @param additionalChildren additional children | |
| 139 | */ | |
| 140 | public void mergeChildren(final Map<Character, ReducedNode<V>> additionalChildren) { | |
| 141 | for (Map.Entry<Character, ReducedNode<V>> entry : additionalChildren.entrySet()) { | |
| 142 | final ReducedNode<V> existing = this.children.get(entry.getKey()); | |
| 143 |
1
1. mergeChildren : negated conditional → KILLED |
if (existing == null) { |
| 144 | this.children.put(entry.getKey(), entry.getValue()); | |
| 145 |
1
1. mergeChildren : negated conditional → KILLED |
} else if (existing != entry.getValue()) { // NOPMD - we have canonical instances |
| 146 | throw new IllegalStateException("Incompatible canonical child encountered during reduction."); | |
| 147 | } | |
| 148 | } | |
| 149 | } | |
| 150 | } | |
Mutations | ||
| 83 |
1.1 |
|
| 97 |
1.1 |
|
| 111 |
1.1 |
|
| 122 |
1.1 |
|
| 125 |
1.1 |
|
| 143 |
1.1 |
|
| 145 |
1.1 |