ReductionSignature.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.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
     * Creates a signature.
60
     *
61
     * @param localDescriptor  local descriptor
62
     * @param childDescriptors child descriptors
63
     */
64
    private ReductionSignature(final Object localDescriptor, final List<ChildDescriptor<V>> childDescriptors) {
65
        this.localDescriptor = localDescriptor;
66
        this.childDescriptors = childDescriptors;
67
    }
68
69
    /**
70
     * Creates a subtree signature according to the selected reduction mode.
71
     *
72
     * @param localSummary local value summary
73
     * @param children     reduced children
74
     * @param settings     reduction settings
75
     * @param <V>          value type
76
     * @return subtree signature
77
     */
78
    public static <V> ReductionSignature<V> create(final LocalValueSummary<V> localSummary,
79
            final Map<Character, ReducedNode<V>> children, final ReductionSettings settings) {
80
        final Object localDescriptor = switch (settings.reductionMode()) {
81
            case MERGE_SUBTREES_WITH_EQUIVALENT_RANKED_GET_ALL_RESULTS ->
82
                RankedLocalDescriptor.of(localSummary.orderedValues());
83
            case MERGE_SUBTREES_WITH_EQUIVALENT_UNORDERED_GET_ALL_RESULTS ->
84
                UnorderedLocalDescriptor.of(localSummary.orderedValues());
85
            case MERGE_SUBTREES_WITH_EQUIVALENT_DOMINANT_GET_RESULTS -> {
86 1 1. create : negated conditional → KILLED
                if (localSummary.hasQualifiedDominantWinner(settings)) {
87
                    yield new DominantLocalDescriptor<>(localSummary.dominantValue);
88
                } else {
89
                    yield RankedLocalDescriptor.of(localSummary.orderedValues());
90
                }
91
            }
92
        };
93
94
        final List<Map.Entry<Character, ReducedNode<V>>> entries = new ArrayList<>(children.entrySet());
95 1 1. create : removed call to java/util/List::sort → KILLED
        entries.sort(Map.Entry.comparingByKey());
96
97
        final List<ChildDescriptor<V>> childDescriptors = new ArrayList<>(entries.size());
98
99
        for (Map.Entry<Character, ReducedNode<V>> entry : entries) {
100
            childDescriptors.add(new ChildDescriptor<>(entry.getKey(), entry.getValue().signature()));
101
        }
102
103 1 1. create : replaced return value with null for org/egothor/stemmer/trie/ReductionSignature::create → KILLED
        return new ReductionSignature<>(localDescriptor, Collections.unmodifiableList(childDescriptors));
104
    }
105
106
    @Override
107
    public int hashCode() {
108 1 1. hashCode : replaced int return with 0 for org/egothor/stemmer/trie/ReductionSignature::hashCode → TIMED_OUT
        return Objects.hash(this.localDescriptor, this.childDescriptors);
109
    }
110
111
    @Override
112
    public boolean equals(final Object other) {
113 1 1. equals : negated conditional → KILLED
        if (this == other) {
114 1 1. equals : replaced boolean return with false for org/egothor/stemmer/trie/ReductionSignature::equals → NO_COVERAGE
            return true;
115
        }
116 1 1. equals : negated conditional → KILLED
        if (!(other instanceof ReductionSignature<?>)) {
117 1 1. equals : replaced boolean return with true for org/egothor/stemmer/trie/ReductionSignature::equals → NO_COVERAGE
            return false;
118
        }
119
        final ReductionSignature<?> that = (ReductionSignature<?>) other;
120 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)
121 1 1. equals : negated conditional → KILLED
                && Objects.equals(this.childDescriptors, that.childDescriptors);
122
    }
123
}

Mutations

86

1.1
Location : create
Killed by : org.egothor.stemmer.trie.ReductionSignatureTest.[engine:junit-jupiter]/[class:org.egothor.stemmer.trie.ReductionSignatureTest]/[method:shouldFallBackToRankedDescriptorWhenDominantWinnerDoesNotQualify()]
negated conditional → KILLED

95

1.1
Location : create
Killed by : org.egothor.stemmer.trie.ReductionSignatureTest.[engine:junit-jupiter]/[class:org.egothor.stemmer.trie.ReductionSignatureTest]/[method:shouldSortChildDescriptorsByEdgeRegardlessOfMapInsertionOrder()]
removed call to java/util/List::sort → KILLED

103

1.1
Location : create
Killed by : org.egothor.stemmer.trie.ReductionSignatureTest.[engine:junit-jupiter]/[class:org.egothor.stemmer.trie.ReductionSignatureTest]/[method:shouldFallBackToRankedDescriptorWhenDominantWinnerDoesNotQualify()]
replaced return value with null for org/egothor/stemmer/trie/ReductionSignature::create → KILLED

108

1.1
Location : hashCode
Killed by : none
replaced int return with 0 for org/egothor/stemmer/trie/ReductionSignature::hashCode → TIMED_OUT

113

1.1
Location : equals
Killed by : org.egothor.stemmer.trie.ReductionSignatureTest.[engine:junit-jupiter]/[class:org.egothor.stemmer.trie.ReductionSignatureTest]/[method:shouldFallBackToRankedDescriptorWhenDominantWinnerDoesNotQualify()]
negated conditional → KILLED

114

1.1
Location : equals
Killed by : none
replaced boolean return with false for org/egothor/stemmer/trie/ReductionSignature::equals → NO_COVERAGE

116

1.1
Location : equals
Killed by : org.egothor.stemmer.trie.ReductionSignatureTest.[engine:junit-jupiter]/[class:org.egothor.stemmer.trie.ReductionSignatureTest]/[method:shouldIgnoreLocalOrderingInUnorderedMode()]
negated conditional → KILLED

117

1.1
Location : equals
Killed by : none
replaced boolean return with true for org/egothor/stemmer/trie/ReductionSignature::equals → NO_COVERAGE

120

1.1
Location : equals
Killed by : org.egothor.stemmer.trie.ReductionSignatureTest.[engine:junit-jupiter]/[class:org.egothor.stemmer.trie.ReductionSignatureTest]/[method:shouldIgnoreLocalOrderingInUnorderedMode()]
negated conditional → KILLED

2.2
Location : equals
Killed by : org.egothor.stemmer.trie.ReductionSignatureTest.[engine:junit-jupiter]/[class:org.egothor.stemmer.trie.ReductionSignatureTest]/[method:shouldFallBackToRankedDescriptorWhenDominantWinnerDoesNotQualify()]
replaced boolean return with true for org/egothor/stemmer/trie/ReductionSignature::equals → KILLED

121

1.1
Location : equals
Killed by : org.egothor.stemmer.trie.ReductionSignatureTest.[engine:junit-jupiter]/[class:org.egothor.stemmer.trie.ReductionSignatureTest]/[method:shouldIgnoreLocalOrderingInUnorderedMode()]
negated conditional → KILLED

Active mutators

Tests examined


Report generated by PIT 1.22.1