• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

oracle / opengrok / #3716

30 Nov 2023 08:55AM UTC coverage: 66.158% (+0.05%) from 66.106%
#3716

push

web-flow
Refactoring to reduce sonar code smell fixes (#4485)

---------

Signed-off-by: Gino Augustine <ginoaugustine@gmail.com>
Co-authored-by: Vladimir Kotal <vlada@kotalovi.cz>

389 of 478 new or added lines in 51 files covered. (81.38%)

11 existing lines in 9 files now uncovered.

38764 of 58593 relevant lines covered (66.16%)

0.66 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

80.0
/opengrok-indexer/src/main/java/org/opengrok/indexer/util/SourceSplitter.java
1
/*
2
 * CDDL HEADER START
3
 *
4
 * The contents of this file are subject to the terms of the
5
 * Common Development and Distribution License (the "License").
6
 * You may not use this file except in compliance with the License.
7
 *
8
 * See LICENSE.txt included in this distribution for the specific
9
 * language governing permissions and limitations under the License.
10
 *
11
 * When distributing Covered Code, include this CDDL HEADER in each
12
 * file and include the License file at LICENSE.txt.
13
 * If applicable, add the following below this CDDL HEADER, with the
14
 * fields enclosed by brackets "[]" replaced with your own identifying
15
 * information: Portions Copyright [yyyy] [name of copyright owner]
16
 *
17
 * CDDL HEADER END
18
 */
19

20
/*
21
 * Copyright (c) 2018, 2020, Chris Fraire <cfraire@me.com>.
22
 */
23
package org.opengrok.indexer.util;
24

25
import java.io.IOException;
26
import java.io.Reader;
27
import java.io.StringReader;
28
import java.util.ArrayList;
29
import java.util.List;
30
import org.opengrok.indexer.analysis.StreamSource;
31

32
/**
33
 * Represents a splitter of source text into lines, where end-of-line tokens --
34
 * in accordance with {@link StringUtils#STANDARD_EOL} -- are maintained instead
35
 * of being stripped.
36
 */
37
public class SourceSplitter {
1✔
38

39
    private static final String RESET_FAILED_MSG = "reset() did not succeed";
40

41
    private int length;
42
    private String[] lines;
43
    private int[] lineOffsets;
44

45
    /**
46
     * Gets the number of characters in the original source document.
47
     */
48
    public int originalLength() {
49
        return length;
1✔
50
    }
51

52
    /**
53
     * Gets the number of split lines.
54
     */
55
    public int count() {
56
        if (lines == null) {
1✔
NEW
57
            throw new IllegalStateException(RESET_FAILED_MSG);
×
58
        }
59
        return lines.length;
1✔
60
    }
61

62
    /**
63
     * Gets the line at the specified index in the lines list.
64
     * @param index greater than or equal to zero and less than
65
     * {@link #count()}
66
     * @return defined instance
67
     * @throws IllegalArgumentException if {@code index} is out of bounds
68
     */
69
    public String getLine(int index) {
70
        if (lines == null) {
1✔
NEW
71
            throw new IllegalStateException(RESET_FAILED_MSG);
×
72
        }
73
        if (index < 0 || index >= lines.length) {
1✔
74
            throw new IllegalArgumentException("index is out of bounds");
×
75
        }
76
        return lines[index];
1✔
77
    }
78

79
    /**
80
     * Gets the starting document character offset of the line at the
81
     * specified index in the lines list.
82
     * @param index greater than or equal to zero and less than or equal to
83
     * {@link #count()}
84
     * @return line starting offset
85
     * @throws IllegalArgumentException if {@code index} is out of bounds
86
     */
87
    public int getOffset(int index) {
88
        if (lineOffsets == null) {
1✔
NEW
89
            throw new IllegalStateException(RESET_FAILED_MSG);
×
90
        }
91
        if (index < 0 || index >= lineOffsets.length) {
1✔
92
            throw new IllegalArgumentException("index is out of bounds");
×
93
        }
94
        return lineOffsets[index];
1✔
95
    }
96

97
    /**
98
     * Find the line index for the specified document offset.
99
     * @param offset greater than or equal to zero and less than
100
     * {@link #originalLength()}.
101
     * @return -1 if {@code offset} is beyond the document bounds; otherwise,
102
     * a valid index
103
     */
104
    public int findLineIndex(int offset) {
105
        if (lineOffsets == null) {
1✔
NEW
106
            throw new IllegalStateException(RESET_FAILED_MSG);
×
107
        }
108
        return SplitterUtil.findLineIndex(length, lineOffsets, offset);
1✔
109
    }
110

111
    /**
112
     * Reset the splitter to use the specified content.
113
     * @param original a defined instance
114
     */
115
    public void reset(String original) {
116
        if (original == null) {
1✔
117
            throw new IllegalArgumentException("`original' is null");
×
118
        }
119

120
        try {
121
            reset(new StringReader(original));
1✔
122
        } catch (IOException ex) {
×
123
            /*
124
             * Should not get here, as String and StringReader operations cannot
125
             * throw IOException.
126
             */
NEW
127
            throw new WrapperIOException(ex);
×
128
        }
1✔
129
    }
1✔
130

131
    /**
132
     * Calls
133
     * {@link #reset(org.opengrok.indexer.analysis.StreamSource, org.opengrok.indexer.util.ReaderWrapper)}
134
     * with {@code src} and {@code null}.
135
     * @param src a defined instance
136
     * @throws java.io.IOException if an I/O error occurs
137
     */
138
    public void reset(StreamSource src) throws IOException {
139
        reset(src, null);
1✔
140
    }
1✔
141

142
    /**
143
     * Reset the splitter to use the specified inputs.
144
     * @param src a defined instance
145
     * @param wrapper an optional instance
146
     * @throws java.io.IOException if an I/O error occurs
147
     */
148
    public void reset(StreamSource src, ReaderWrapper wrapper)
149
            throws IOException {
150
        if (src == null) {
1✔
151
            throw new IllegalArgumentException("`src' is null");
×
152
        }
153

154
        SplitterUtil.reset(this::reset, src, wrapper);
1✔
155
    }
1✔
156

157
    private void reset(Reader reader) throws IOException {
158
        length = 0;
1✔
159
        lines = null;
1✔
160
        lineOffsets = null;
1✔
161

162
        List<String> slist = new ArrayList<>();
1✔
163
        SourceSplitterScanner scanner = new SourceSplitterScanner(reader);
1✔
164
        scanner.setTarget(slist);
1✔
165
        scanner.consume();
1✔
166
        long fullLength = scanner.getLength();
1✔
167
        /*
168
         * Lucene cannot go past Integer.MAX_VALUE so revise the length to fit
169
         * within the Integer constraint.
170
         */
171
        length = fullLength > Integer.MAX_VALUE ? Integer.MAX_VALUE : (int) fullLength;
1✔
172

173
        lines = slist.toArray(new String[0]);
1✔
174
        setLineOffsets();
1✔
175
    }
1✔
176

177
    private void setLineOffsets() {
178
        /*
179
         * Add one more entry for lineOffsets so that findLineIndex() can
180
         * easily work on the last line.
181
         */
182
        lineOffsets = new int[lines.length + 1];
1✔
183
        int offset = 0;
1✔
184
        for (int i = 0; i < lineOffsets.length; ++i) {
1✔
185
            lineOffsets[i] = offset;
1✔
186
            if (i < lines.length) {
1✔
187
                offset += lines[i].length();
1✔
188
            }
189
        }
190
    }
1✔
191
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc