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

Mutations

90

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

99

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

107

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

112

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

117

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

118

1.1
Location : equals
Killed by : none
replaced boolean return with false 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

121

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

124

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

125

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