StemmerPatchTrieBinaryIO.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;
36
37
import java.io.BufferedInputStream;
38
import java.io.BufferedOutputStream;
39
import java.io.DataInputStream;
40
import java.io.DataOutputStream;
41
import java.io.IOException;
42
import java.io.InputStream;
43
import java.io.OutputStream;
44
import java.nio.file.Files;
45
import java.nio.file.Path;
46
import java.util.Objects;
47
import java.util.logging.Level;
48
import java.util.logging.Logger;
49
import java.util.zip.GZIPInputStream;
50
import java.util.zip.GZIPOutputStream;
51
52
/**
53
 * Binary persistence helper for patch-command stemmer tries.
54
 *
55
 * <p>
56
 * This class persists {@link FrequencyTrie} instances whose values are compact
57
 * patch commands represented as {@link String}. The serialized trie payload is
58
 * the native binary format of {@link FrequencyTrie}, wrapped in GZip
59
 * compression.
60
 *
61
 * <p>
62
 * The helper centralizes the codec and compression details so that higher-level
63
 * loader APIs can remain focused on source selection rather than stream
64
 * mechanics.
65
 */
66
public final class StemmerPatchTrieBinaryIO {
67
68
    /**
69
     * Logger of this class.
70
     */
71
    private static final Logger LOGGER = Logger.getLogger(StemmerPatchTrieBinaryIO.class.getName());
72
73
    /**
74
     * Value codec for persisted patch-command strings.
75
     */
76
    private static final FrequencyTrie.ValueStreamCodec<String> STRING_CODEC = new StringValueStreamCodec();
77
78
    /**
79
     * Utility class.
80
     */
81
    private StemmerPatchTrieBinaryIO() {
82
        throw new AssertionError("No instances.");
83
    }
84
85
    /**
86
     * Reads a GZip-compressed binary patch-command trie from a filesystem path.
87
     *
88
     * @param path source file
89
     * @return deserialized trie
90
     * @throws NullPointerException if {@code path} is {@code null}
91
     * @throws IOException          if reading or decompression fails
92
     */
93
    public static FrequencyTrie<String> read(final Path path) throws IOException {
94
        Objects.requireNonNull(path, "path");
95
96
        try (InputStream fileInputStream = Files.newInputStream(path)) {
97 1 1. read : replaced return value with null for org/egothor/stemmer/StemmerPatchTrieBinaryIO::read → KILLED
            return read(fileInputStream);
98
        }
99
    }
100
101
    /**
102
     * Reads a GZip-compressed binary patch-command trie from a filesystem path
103
     * string.
104
     *
105
     * @param fileName source file name or path string
106
     * @return deserialized trie
107
     * @throws NullPointerException if {@code fileName} is {@code null}
108
     * @throws IOException          if reading or decompression fails
109
     */
110
    public static FrequencyTrie<String> read(final String fileName) throws IOException {
111
        Objects.requireNonNull(fileName, "fileName");
112 1 1. read : replaced return value with null for org/egothor/stemmer/StemmerPatchTrieBinaryIO::read → KILLED
        return read(Path.of(fileName));
113
    }
114
115
    /**
116
     * Reads a GZip-compressed binary patch-command trie from an input stream.
117
     *
118
     * <p>
119
     * The supplied stream is consumed but not interpreted as plain trie bytes; it
120
     * is first decompressed using {@link GZIPInputStream}.
121
     *
122
     * @param inputStream source stream
123
     * @return deserialized trie
124
     * @throws NullPointerException if {@code inputStream} is {@code null}
125
     * @throws IOException          if reading or decompression fails
126
     */
127
    public static FrequencyTrie<String> read(final InputStream inputStream) throws IOException {
128
        Objects.requireNonNull(inputStream, "inputStream");
129
130
        try (GZIPInputStream gzipInputStream = new GZIPInputStream(new BufferedInputStream(inputStream));
131
                DataInputStream dataInputStream = new DataInputStream(gzipInputStream)) {
132 1 1. lambda$read$0 : replaced return value with null for org/egothor/stemmer/StemmerPatchTrieBinaryIO::lambda$read$0 → KILLED
            final FrequencyTrie<String> trie = FrequencyTrie.readFrom(dataInputStream, String[]::new, STRING_CODEC);
133
134
            LOGGER.log(Level.FINE, "Read compressed binary stemmer trie.");
135 1 1. read : replaced return value with null for org/egothor/stemmer/StemmerPatchTrieBinaryIO::read → KILLED
            return trie;
136
        }
137
    }
138
139
    /**
140
     * Writes a GZip-compressed binary patch-command trie to a filesystem path.
141
     *
142
     * @param trie trie to persist
143
     * @param path target file
144
     * @throws NullPointerException if any argument is {@code null}
145
     * @throws IOException          if writing fails
146
     */
147
    public static void write(final FrequencyTrie<String> trie, final Path path) throws IOException {
148
        Objects.requireNonNull(trie, "trie");
149
        Objects.requireNonNull(path, "path");
150
151
        final Path parent = path.toAbsolutePath().getParent();
152 1 1. write : negated conditional → KILLED
        if (parent != null) {
153
            Files.createDirectories(parent);
154
        }
155
156
        try (OutputStream fileOutputStream = Files.newOutputStream(path)) {
157 1 1. write : removed call to org/egothor/stemmer/StemmerPatchTrieBinaryIO::write → KILLED
            write(trie, fileOutputStream);
158
        }
159
    }
160
161
    /**
162
     * Writes a GZip-compressed binary patch-command trie to a filesystem path
163
     * string.
164
     *
165
     * @param trie     trie to persist
166
     * @param fileName target file name or path string
167
     * @throws NullPointerException if any argument is {@code null}
168
     * @throws IOException          if writing fails
169
     */
170
    public static void write(final FrequencyTrie<String> trie, final String fileName) throws IOException {
171
        Objects.requireNonNull(fileName, "fileName");
172 1 1. write : removed call to org/egothor/stemmer/StemmerPatchTrieBinaryIO::write → KILLED
        write(trie, Path.of(fileName));
173
    }
174
175
    /**
176
     * Writes a GZip-compressed binary patch-command trie to an output stream.
177
     *
178
     * @param trie         trie to persist
179
     * @param outputStream target stream
180
     * @throws NullPointerException if any argument is {@code null}
181
     * @throws IOException          if writing fails
182
     */
183
    public static void write(final FrequencyTrie<String> trie, final OutputStream outputStream) throws IOException {
184
        Objects.requireNonNull(trie, "trie");
185
        Objects.requireNonNull(outputStream, "outputStream");
186
187
        try (GZIPOutputStream gzipOutputStream = new GZIPOutputStream(new BufferedOutputStream(outputStream));
188
                DataOutputStream dataOutputStream = new DataOutputStream(gzipOutputStream)) {
189 1 1. write : removed call to org/egothor/stemmer/FrequencyTrie::writeTo → KILLED
            trie.writeTo(dataOutputStream, STRING_CODEC);
190
        }
191
192
        LOGGER.log(Level.FINE, "Wrote compressed binary stemmer trie.");
193
    }
194
195
    /**
196
     * Binary stream codec for persisted patch-command strings.
197
     */
198
    private static final class StringValueStreamCodec implements FrequencyTrie.ValueStreamCodec<String> {
199
200
        /**
201
         * Creates a codec instance.
202
         */
203
        private StringValueStreamCodec() {
204
        }
205
206
        @Override
207
        public void write(final DataOutputStream dataOutput, final String value) throws IOException {
208 1 1. write : removed call to java/io/DataOutputStream::writeUTF → KILLED
            dataOutput.writeUTF(value);
209
        }
210
211
        @Override
212
        public String read(final DataInputStream dataInput) throws IOException {
213 1 1. read : replaced return value with "" for org/egothor/stemmer/StemmerPatchTrieBinaryIO$StringValueStreamCodec::read → KILLED
            return dataInput.readUTF();
214
        }
215
    }
216
}

Mutations

97

1.1
Location : read
Killed by : org.egothor.stemmer.StemmerPatchTrieBinaryIOTest.[engine:junit-jupiter]/[class:org.egothor.stemmer.StemmerPatchTrieBinaryIOTest]/[nested-class:ReadTests]/[method:shouldReadGzipPayloadFromFileNameString()]
replaced return value with null for org/egothor/stemmer/StemmerPatchTrieBinaryIO::read → KILLED

112

1.1
Location : read
Killed by : org.egothor.stemmer.StemmerPatchTrieBinaryIOTest.[engine:junit-jupiter]/[class:org.egothor.stemmer.StemmerPatchTrieBinaryIOTest]/[nested-class:ReadTests]/[method:shouldReadGzipPayloadFromFileNameString()]
replaced return value with null for org/egothor/stemmer/StemmerPatchTrieBinaryIO::read → KILLED

132

1.1
Location : lambda$read$0
Killed by : org.egothor.stemmer.StemmerPatchTrieLoaderTest.[engine:junit-jupiter]/[class:org.egothor.stemmer.StemmerPatchTrieLoaderTest]/[nested-class:FilesystemAndParserTests]/[method:shouldRoundTripBinaryTrieAcrossAllBinaryOverloads()]
replaced return value with null for org/egothor/stemmer/StemmerPatchTrieBinaryIO::lambda$read$0 → KILLED

135

1.1
Location : read
Killed by : org.egothor.stemmer.StemmerPatchTrieBinaryIOTest.[engine:junit-jupiter]/[class:org.egothor.stemmer.StemmerPatchTrieBinaryIOTest]/[nested-class:ReadTests]/[method:shouldReadGzipPayloadFromFileNameString()]
replaced return value with null for org/egothor/stemmer/StemmerPatchTrieBinaryIO::read → KILLED

152

1.1
Location : write
Killed by : org.egothor.stemmer.StemmerPatchTrieBinaryIOTest.[engine:junit-jupiter]/[class:org.egothor.stemmer.StemmerPatchTrieBinaryIOTest]/[nested-class:WriteTests]/[method:shouldCreateParentDirectoriesAndWriteGzipFile()]
negated conditional → KILLED

157

1.1
Location : write
Killed by : org.egothor.stemmer.StemmerPatchTrieBinaryIOTest.[engine:junit-jupiter]/[class:org.egothor.stemmer.StemmerPatchTrieBinaryIOTest]/[nested-class:WriteTests]/[method:shouldWriteToFilesystemWhenFileNameStringIsUsed()]
removed call to org/egothor/stemmer/StemmerPatchTrieBinaryIO::write → KILLED

172

1.1
Location : write
Killed by : org.egothor.stemmer.StemmerPatchTrieBinaryIOTest.[engine:junit-jupiter]/[class:org.egothor.stemmer.StemmerPatchTrieBinaryIOTest]/[nested-class:WriteTests]/[method:shouldRejectNullArgumentsAcrossAllWriteOverloads()]
removed call to org/egothor/stemmer/StemmerPatchTrieBinaryIO::write → KILLED

189

1.1
Location : write
Killed by : org.egothor.stemmer.StemmerPatchTrieBinaryIOTest.[engine:junit-jupiter]/[class:org.egothor.stemmer.StemmerPatchTrieBinaryIOTest]/[nested-class:WriteTests]/[method:shouldPropagateWriteFailureFromTrieSerialization()]
removed call to org/egothor/stemmer/FrequencyTrie::writeTo → KILLED

208

1.1
Location : write
Killed by : org.egothor.stemmer.StemmerPatchTrieLoaderTest.[engine:junit-jupiter]/[class:org.egothor.stemmer.StemmerPatchTrieLoaderTest]/[nested-class:FilesystemAndParserTests]/[method:shouldRoundTripBinaryTrieAcrossAllBinaryOverloads()]
removed call to java/io/DataOutputStream::writeUTF → KILLED

213

1.1
Location : read
Killed by : org.egothor.stemmer.StemmerPatchTrieLoaderTest.[engine:junit-jupiter]/[class:org.egothor.stemmer.StemmerPatchTrieLoaderTest]/[nested-class:FilesystemAndParserTests]/[method:shouldRoundTripBinaryTrieAcrossAllBinaryOverloads()]
replaced return value with "" for org/egothor/stemmer/StemmerPatchTrieBinaryIO$StringValueStreamCodec::read → KILLED

Active mutators

Tests examined


Report generated by PIT 1.22.1