| 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.Arrays; | |
| 34 | import java.util.Objects; | |
| 35 | ||
| 36 | /** | |
| 37 | * Immutable compiled trie node optimized for read access. | |
| 38 | * | |
| 39 | * <p> | |
| 40 | * The returned arrays are the internal backing storage of the compiled node. | |
| 41 | * They are exposed for efficient access by closely related trie infrastructure | |
| 42 | * and therefore must never be modified by callers. The node itself is still | |
| 43 | * immutable from the public API perspective because construction wires these | |
| 44 | * arrays once and all lookup operations thereafter treat them as read-only. | |
| 45 | * | |
| 46 | * @param <V> value type | |
| 47 | * @param edgeLabels internal edge label array | |
| 48 | * @param children internal child array | |
| 49 | * @param orderedValues internal ordered values array | |
| 50 | * @param orderedCounts internal ordered counts array | |
| 51 | */ | |
| 52 | @SuppressWarnings("PMD.DataClass") | |
| 53 | public record CompiledNode<V>(char[] edgeLabels, CompiledNode<V>[] children, V[] orderedValues, int... orderedCounts) { | |
| 54 | ||
| 55 | /** | |
| 56 | * Creates one validated compiled node. | |
| 57 | * | |
| 58 | * @throws NullPointerException if any array argument is {@code null} | |
| 59 | * @throws IllegalArgumentException if the edge-related arrays or value-related | |
| 60 | * arrays do not have matching lengths | |
| 61 | */ | |
| 62 | public CompiledNode { | |
| 63 | Objects.requireNonNull(edgeLabels, "edgeLabels"); | |
| 64 | Objects.requireNonNull(children, "children"); | |
| 65 | Objects.requireNonNull(orderedValues, "orderedValues"); | |
| 66 | Objects.requireNonNull(orderedCounts, "orderedCounts"); | |
| 67 | ||
| 68 | if (edgeLabels.length != children.length) { | |
| 69 | throw new IllegalArgumentException("edgeLabels and children must have the same length."); | |
| 70 | } | |
| 71 | if (orderedValues.length != orderedCounts.length) { | |
| 72 | throw new IllegalArgumentException("orderedValues and orderedCounts must have the same length."); | |
| 73 | } | |
| 74 | } | |
| 75 | ||
| 76 | /** | |
| 77 | * Returns the internal edge-label array. | |
| 78 | * | |
| 79 | * <p> | |
| 80 | * The returned array is not copied for performance reasons and must be treated | |
| 81 | * as read-only. | |
| 82 | * | |
| 83 | * @return internal edge-label array | |
| 84 | */ | |
| 85 | @Override | |
| 86 | @SuppressWarnings("PMD.MethodReturnsInternalArray") | |
| 87 | public char[] edgeLabels() { | |
| 88 | return this.edgeLabels; | |
| 89 | } | |
| 90 | ||
| 91 | /** | |
| 92 | * Returns the internal child-node array. | |
| 93 | * | |
| 94 | * <p> | |
| 95 | * The returned array is not copied for performance reasons and must be treated | |
| 96 | * as read-only by external callers. | |
| 97 | * | |
| 98 | * @return internal child-node array | |
| 99 | */ | |
| 100 | @Override | |
| 101 | @SuppressWarnings("PMD.MethodReturnsInternalArray") | |
| 102 | public CompiledNode<V>[] children() { | |
| 103 | return this.children; | |
| 104 | } | |
| 105 | ||
| 106 | /** | |
| 107 | * Returns the internal ordered-values array. | |
| 108 | * | |
| 109 | * <p> | |
| 110 | * The returned array is not copied for performance reasons and must be treated | |
| 111 | * as read-only. | |
| 112 | * | |
| 113 | * @return internal ordered-values array | |
| 114 | */ | |
| 115 | @Override | |
| 116 | @SuppressWarnings("PMD.MethodReturnsInternalArray") | |
| 117 | public V[] orderedValues() { | |
| 118 | return this.orderedValues; | |
| 119 | } | |
| 120 | ||
| 121 | /** | |
| 122 | * Returns the internal ordered-counts array. | |
| 123 | * | |
| 124 | * <p> | |
| 125 | * The returned array is not copied for performance reasons and must be treated | |
| 126 | * as read-only. | |
| 127 | * | |
| 128 | * @return internal ordered-counts array | |
| 129 | */ | |
| 130 | @Override | |
| 131 | @SuppressWarnings("PMD.MethodReturnsInternalArray") | |
| 132 | public int[] orderedCounts() { | |
| 133 | return this.orderedCounts; | |
| 134 | } | |
| 135 | ||
| 136 | /** | |
| 137 | * Finds a child for the supplied edge character. | |
| 138 | * | |
| 139 | * @param edge edge character | |
| 140 | * @return child node, or {@code null} if absent | |
| 141 | */ | |
| 142 | public CompiledNode<V> findChild(final char edge) { | |
| 143 | final int index = Arrays.binarySearch(this.edgeLabels, edge); | |
| 144 |
2
1. findChild : negated conditional → KILLED 2. findChild : changed conditional boundary → KILLED |
if (index < 0) { |
| 145 | return null; | |
| 146 | } | |
| 147 |
1
1. findChild : replaced return value with null for org/egothor/stemmer/trie/CompiledNode::findChild → KILLED |
return this.children[index]; |
| 148 | } | |
| 149 | } | |
Mutations | ||
| 144 |
1.1 2.2 |
|
| 147 |
1.1 |