| 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. All advertising materials mentioning features or use of this software must | |
| 16 | * display the following acknowledgement: | |
| 17 | * This product includes software developed by the Egothor project. | |
| 18 | * | |
| 19 | * 4. Neither the name of the copyright holder nor the names of its contributors | |
| 20 | * may be used to endorse or promote products derived from this software | |
| 21 | * without specific prior written permission. | |
| 22 | * | |
| 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
| 24 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
| 25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
| 26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | |
| 27 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
| 28 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
| 29 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
| 30 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
| 31 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
| 32 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
| 33 | * POSSIBILITY OF SUCH DAMAGE. | |
| 34 | ******************************************************************************/ | |
| 35 | package org.egothor.stemmer.trie; | |
| 36 | ||
| 37 | import java.util.LinkedHashMap; | |
| 38 | import java.util.Map; | |
| 39 | ||
| 40 | /** | |
| 41 | * Canonical reduced node used during subtree merging. | |
| 42 | * | |
| 43 | * <p> | |
| 44 | * The maps exposed by the accessors are the internal backing state of the | |
| 45 | * canonical reduced node. They are returned directly for efficiency and are | |
| 46 | * intended only for closely related trie-reduction infrastructure. | |
| 47 | * | |
| 48 | * @param <V> value type | |
| 49 | */ | |
| 50 | public final class ReducedNode<V> { | |
| 51 | ||
| 52 | /** | |
| 53 | * Reduction signature. | |
| 54 | */ | |
| 55 | private final ReductionSignature<V> signature; | |
| 56 | ||
| 57 | /** | |
| 58 | * Aggregated local value counts. | |
| 59 | */ | |
| 60 | private final Map<V, Integer> localCounts; | |
| 61 | ||
| 62 | /** | |
| 63 | * Canonical children by edge. | |
| 64 | */ | |
| 65 | private final Map<Character, ReducedNode<V>> children; | |
| 66 | ||
| 67 | /** | |
| 68 | * Creates a new reduced node. | |
| 69 | * | |
| 70 | * @param signature reduction signature | |
| 71 | * @param localCounts local counts | |
| 72 | * @param children children | |
| 73 | */ | |
| 74 | public ReducedNode(final ReductionSignature<V> signature, final Map<V, Integer> localCounts, | |
| 75 | final Map<Character, ReducedNode<V>> children) { | |
| 76 | this.signature = signature; | |
| 77 | this.localCounts = new LinkedHashMap<>(localCounts); | |
| 78 | this.children = new LinkedHashMap<>(children); | |
| 79 | } | |
| 80 | ||
| 81 | /** | |
| 82 | * Returns the reduction signature of this canonical node. | |
| 83 | * | |
| 84 | * @return reduction signature | |
| 85 | */ | |
| 86 | public ReductionSignature<V> signature() { | |
| 87 |
1
1. signature : replaced return value with null for org/egothor/stemmer/trie/ReducedNode::signature → KILLED |
return this.signature; |
| 88 | } | |
| 89 | ||
| 90 | /** | |
| 91 | * Returns the internal aggregated local value-count map. | |
| 92 | * | |
| 93 | * <p> | |
| 94 | * The returned map is the internal backing state of this canonical reduced node | |
| 95 | * and is exposed only for efficient cooperation with trie-reduction | |
| 96 | * infrastructure. | |
| 97 | * | |
| 98 | * @return internal aggregated local value-count map | |
| 99 | */ | |
| 100 | public Map<V, Integer> localCounts() { | |
| 101 |
1
1. localCounts : replaced return value with Collections.emptyMap for org/egothor/stemmer/trie/ReducedNode::localCounts → KILLED |
return this.localCounts; |
| 102 | } | |
| 103 | ||
| 104 | /** | |
| 105 | * Returns the internal canonical child map indexed by transition character. | |
| 106 | * | |
| 107 | * <p> | |
| 108 | * The returned map is the internal backing state of this canonical reduced node | |
| 109 | * and is exposed only for efficient cooperation with trie-reduction | |
| 110 | * infrastructure. | |
| 111 | * | |
| 112 | * @return internal canonical child map | |
| 113 | */ | |
| 114 | public Map<Character, ReducedNode<V>> children() { | |
| 115 |
1
1. children : replaced return value with Collections.emptyMap for org/egothor/stemmer/trie/ReducedNode::children → KILLED |
return this.children; |
| 116 | } | |
| 117 | ||
| 118 | /** | |
| 119 | * Merges additional local counts into this node. | |
| 120 | * | |
| 121 | * @param additionalCounts additional local counts | |
| 122 | */ | |
| 123 | public void mergeLocalCounts(final Map<V, Integer> additionalCounts) { | |
| 124 | for (Map.Entry<V, Integer> entry : additionalCounts.entrySet()) { | |
| 125 | final Integer previous = this.localCounts.get(entry.getKey()); | |
| 126 |
1
1. mergeLocalCounts : negated conditional → KILLED |
if (previous == null) { |
| 127 | this.localCounts.put(entry.getKey(), entry.getValue()); | |
| 128 | } else { | |
| 129 |
1
1. mergeLocalCounts : Replaced integer addition with subtraction → KILLED |
this.localCounts.put(entry.getKey(), previous + entry.getValue()); |
| 130 | } | |
| 131 | } | |
| 132 | } | |
| 133 | ||
| 134 | /** | |
| 135 | * Merges child references into this node. | |
| 136 | * | |
| 137 | * <p> | |
| 138 | * For nodes with the same reduction signature, child edge sets and child | |
| 139 | * signatures must be compatible. This method therefore only needs to verify | |
| 140 | * consistency and store the canonical child instance. | |
| 141 | * | |
| 142 | * @param additionalChildren additional children | |
| 143 | */ | |
| 144 | public void mergeChildren(final Map<Character, ReducedNode<V>> additionalChildren) { | |
| 145 | for (Map.Entry<Character, ReducedNode<V>> entry : additionalChildren.entrySet()) { | |
| 146 | final ReducedNode<V> existing = this.children.get(entry.getKey()); | |
| 147 |
1
1. mergeChildren : negated conditional → KILLED |
if (existing == null) { |
| 148 | this.children.put(entry.getKey(), entry.getValue()); | |
| 149 |
1
1. mergeChildren : negated conditional → KILLED |
} else if (existing != entry.getValue()) { // NOPMD - we have canonical instances |
| 150 | throw new IllegalStateException("Incompatible canonical child encountered during reduction."); | |
| 151 | } | |
| 152 | } | |
| 153 | } | |
| 154 | } | |
Mutations | ||
| 87 |
1.1 |
|
| 101 |
1.1 |
|
| 115 |
1.1 |
|
| 126 |
1.1 |
|
| 129 |
1.1 |
|
| 147 |
1.1 |
|
| 149 |
1.1 |