| 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.ArrayList; | |
| 34 | import java.util.Collections; | |
| 35 | import java.util.List; | |
| 36 | import java.util.Map; | |
| 37 | import java.util.Objects; | |
| 38 | ||
| 39 | import org.egothor.stemmer.ReductionSettings; | |
| 40 | ||
| 41 | /** | |
| 42 | * Immutable reduction signature of a full subtree. | |
| 43 | * | |
| 44 | * @param <V> value type | |
| 45 | */ | |
| 46 | public final class ReductionSignature<V> { | |
| 47 | ||
| 48 | /** | |
| 49 | * Local semantic descriptor. | |
| 50 | */ | |
| 51 | private final Object localDescriptor; | |
| 52 | ||
| 53 | /** | |
| 54 | * Child edge descriptors in sorted edge order. | |
| 55 | */ | |
| 56 | private final List<ChildDescriptor<V>> childDescriptors; | |
| 57 | ||
| 58 | /** | |
| 59 | * Whether the represented node accepts any remaining lookup input. | |
| 60 | */ | |
| 61 | private final boolean acceptsRemainingInput; | |
| 62 | ||
| 63 | /** | |
| 64 | * Creates a signature. | |
| 65 | * | |
| 66 | * @param localDescriptor local descriptor | |
| 67 | * @param childDescriptors child descriptors | |
| 68 | */ | |
| 69 | private ReductionSignature(final Object localDescriptor, final List<ChildDescriptor<V>> childDescriptors, | |
| 70 | final boolean acceptsRemainingInput) { | |
| 71 | this.localDescriptor = localDescriptor; | |
| 72 | this.childDescriptors = childDescriptors; | |
| 73 | this.acceptsRemainingInput = acceptsRemainingInput; | |
| 74 | } | |
| 75 | ||
| 76 | /** | |
| 77 | * Creates a subtree signature according to the selected reduction mode. | |
| 78 | * | |
| 79 | * @param localSummary local value summary | |
| 80 | * @param children reduced children | |
| 81 | * @param settings reduction settings | |
| 82 | * @param acceptsRemainingInput whether this node accepts any remaining lookup | |
| 83 | * input | |
| 84 | * @param <V> value type | |
| 85 | * @return subtree signature | |
| 86 | */ | |
| 87 | public static <V> ReductionSignature<V> create(final LocalValueSummary<V> localSummary, | |
| 88 | final Map<Character, ReducedNode<V>> children, final ReductionSettings settings, | |
| 89 | final boolean acceptsRemainingInput) { | |
| 90 | final Object localDescriptor = switch (settings.reductionMode()) { | |
| 91 | case MERGE_SUBTREES_WITH_EQUIVALENT_RANKED_GET_ALL_RESULTS -> | |
| 92 | RankedLocalDescriptor.of(localSummary.orderedValues()); | |
| 93 | case MERGE_SUBTREES_WITH_EQUIVALENT_UNORDERED_GET_ALL_RESULTS -> | |
| 94 | UnorderedLocalDescriptor.of(localSummary.orderedValues()); | |
| 95 | case MERGE_SUBTREES_WITH_EQUIVALENT_DOMINANT_GET_RESULTS -> { | |
| 96 |
1
1. create : negated conditional → KILLED |
if (localSummary.hasQualifiedDominantWinner(settings)) { |
| 97 | yield new DominantLocalDescriptor<>(localSummary.dominantValue); | |
| 98 | } else { | |
| 99 | yield RankedLocalDescriptor.of(localSummary.orderedValues()); | |
| 100 | } | |
| 101 | } | |
| 102 | }; | |
| 103 | ||
| 104 | final List<Map.Entry<Character, ReducedNode<V>>> entries = new ArrayList<>(children.entrySet()); | |
| 105 |
1
1. create : removed call to java/util/List::sort → KILLED |
entries.sort(Map.Entry.comparingByKey()); |
| 106 | ||
| 107 | final List<ChildDescriptor<V>> childDescriptors = new ArrayList<>(entries.size()); | |
| 108 | ||
| 109 | for (Map.Entry<Character, ReducedNode<V>> entry : entries) { | |
| 110 | childDescriptors.add(new ChildDescriptor<>(entry.getKey(), entry.getValue().signature())); | |
| 111 | } | |
| 112 | ||
| 113 |
1
1. create : replaced return value with null for org/egothor/stemmer/trie/ReductionSignature::create → KILLED |
return new ReductionSignature<>(localDescriptor, Collections.unmodifiableList(childDescriptors), |
| 114 | acceptsRemainingInput); | |
| 115 | } | |
| 116 | ||
| 117 | /** | |
| 118 | * Creates a non-accepting subtree signature according to the selected reduction | |
| 119 | * mode. | |
| 120 | * | |
| 121 | * @param localSummary local value summary | |
| 122 | * @param children reduced children | |
| 123 | * @param settings reduction settings | |
| 124 | * @param <V> value type | |
| 125 | * @return subtree signature | |
| 126 | */ | |
| 127 | public static <V> ReductionSignature<V> create(final LocalValueSummary<V> localSummary, | |
| 128 | final Map<Character, ReducedNode<V>> children, final ReductionSettings settings) { | |
| 129 |
1
1. create : replaced return value with null for org/egothor/stemmer/trie/ReductionSignature::create → KILLED |
return create(localSummary, children, settings, false); |
| 130 | } | |
| 131 | ||
| 132 | @Override | |
| 133 | public int hashCode() { | |
| 134 |
1
1. hashCode : replaced int return with 0 for org/egothor/stemmer/trie/ReductionSignature::hashCode → TIMED_OUT |
return Objects.hash(this.localDescriptor, this.childDescriptors, this.acceptsRemainingInput); |
| 135 | } | |
| 136 | ||
| 137 | @Override | |
| 138 | public boolean equals(final Object other) { | |
| 139 |
1
1. equals : negated conditional → KILLED |
if (this == other) { |
| 140 |
1
1. equals : replaced boolean return with false for org/egothor/stemmer/trie/ReductionSignature::equals → NO_COVERAGE |
return true; |
| 141 | } | |
| 142 |
1
1. equals : negated conditional → KILLED |
if (!(other instanceof ReductionSignature<?>)) { |
| 143 |
1
1. equals : replaced boolean return with true for org/egothor/stemmer/trie/ReductionSignature::equals → NO_COVERAGE |
return false; |
| 144 | } | |
| 145 | final ReductionSignature<?> that = (ReductionSignature<?>) other; | |
| 146 |
2
1. equals : negated conditional → KILLED 2. equals : replaced boolean return with true for org/egothor/stemmer/trie/ReductionSignature::equals → KILLED |
return Objects.equals(this.localDescriptor, that.localDescriptor) |
| 147 |
2
1. equals : negated conditional → KILLED 2. equals : negated conditional → KILLED |
&& Objects.equals(this.childDescriptors, that.childDescriptors) |
| 148 | && this.acceptsRemainingInput == that.acceptsRemainingInput; | |
| 149 | } | |
| 150 | } | |
Mutations | ||
| 96 |
1.1 |
|
| 105 |
1.1 |
|
| 113 |
1.1 |
|
| 129 |
1.1 |
|
| 134 |
1.1 |
|
| 139 |
1.1 |
|
| 140 |
1.1 |
|
| 142 |
1.1 |
|
| 143 |
1.1 |
|
| 146 |
1.1 2.2 |
|
| 147 |
1.1 2.2 |