Skip to content

Commit 942065c

Browse files
authored
Convert more classes to record classes (#13328)
1 parent 955986d commit 942065c

File tree

217 files changed

+1166
-2585
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

217 files changed

+1166
-2585
lines changed

lucene/CHANGES.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@ API Changes
118118
argument with a `FacetsCollectorManager` and update the return type to include both `TopDocs` results as well as
119119
facets results. (Luca Cavanna)
120120

121+
* GITHUB#13328: Convert many basic Lucene classes to record classes, including CollectionStatistics, TermStatistics and LeafMetadata. (Shubham Chaudhary)
122+
121123
New Features
122124
---------------------
123125

lucene/MIGRATE.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ access the members using method calls instead of field accesses. Affected classe
193193

194194
- `IOContext`, `MergeInfo`, and `FlushInfo` (GITHUB#13205)
195195
- `BooleanClause` (GITHUB#13261)
196+
- Many basic Lucene classes, including `CollectionStatistics`, `TermStatistics` and `LeafMetadata` (GITHUB#13328)
196197

197198
### Boolean flags on IOContext replaced with a new ReadAdvice enum.
198199

lucene/analysis/common/src/java/org/apache/lucene/analysis/hunspell/CheckCompoundPattern.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,15 @@ public String toString() {
5252

5353
boolean prohibitsCompounding(CharsRef word, int breakPos, Root<?> rootBefore, Root<?> rootAfter) {
5454
if (isNonAffixedPattern(endChars)) {
55-
if (!charsMatch(word, breakPos - rootBefore.word.length(), rootBefore.word)) {
55+
if (!charsMatch(word, breakPos - rootBefore.word().length(), rootBefore.word())) {
5656
return false;
5757
}
5858
} else if (!charsMatch(word, breakPos - endChars.length(), endChars)) {
5959
return false;
6060
}
6161

6262
if (isNonAffixedPattern(beginChars)) {
63-
if (!charsMatch(word, breakPos, rootAfter.word)) {
63+
if (!charsMatch(word, breakPos, rootAfter.word())) {
6464
return false;
6565
}
6666
} else if (!charsMatch(word, breakPos, beginChars)) {
@@ -84,7 +84,7 @@ private static boolean isNonAffixedPattern(String pattern) {
8484

8585
private boolean hasAllFlags(Root<?> root, char[] flags) {
8686
for (char flag : flags) {
87-
if (!dictionary.hasFlag(root.entryId, flag)) {
87+
if (!dictionary.hasFlag(root.entryId(), flag)) {
8888
return false;
8989
}
9090
}

lucene/analysis/common/src/java/org/apache/lucene/analysis/hunspell/GeneratingSuggester.java

Lines changed: 10 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import java.util.Comparator;
2525
import java.util.LinkedHashSet;
2626
import java.util.List;
27-
import java.util.Objects;
2827
import java.util.PriorityQueue;
2928
import java.util.Set;
3029
import java.util.TreeSet;
@@ -111,7 +110,7 @@ private List<Weighted<Root<String>>> findSimilarDictionaryEntries(
111110

112111
private static boolean isWorseThan(int score, CharsRef candidate, Weighted<Root<String>> root) {
113112
return score < root.score
114-
|| score == root.score && CharSequence.compare(candidate, root.word.word) > 0;
113+
|| score == root.score && CharSequence.compare(candidate, root.word.word()) > 0;
115114
}
116115

117116
private void processSuggestibleWords(
@@ -162,11 +161,11 @@ private List<String> expandRoot(Root<String> root, String misspelled) {
162161
List<char[]> crossProducts = new ArrayList<>();
163162
Set<String> result = new LinkedHashSet<>();
164163

165-
if (!dictionary.hasFlag(root.entryId, dictionary.needaffix)) {
166-
result.add(root.word);
164+
if (!dictionary.hasFlag(root.entryId(), dictionary.needaffix)) {
165+
result.add(root.word());
167166
}
168167

169-
char[] wordChars = root.word.toCharArray();
168+
char[] wordChars = root.word().toCharArray();
170169

171170
// suffixes
172171
processAffixes(
@@ -180,7 +179,7 @@ private List<String> expandRoot(Root<String> root, String misspelled) {
180179
}
181180

182181
String suffix = misspelled.substring(misspelled.length() - suffixLength);
183-
String withSuffix = root.word.substring(0, root.word.length() - stripLength) + suffix;
182+
String withSuffix = root.word().substring(0, root.word().length() - stripLength) + suffix;
184183
result.add(withSuffix);
185184
if (dictionary.isCrossProduct(suffixId)) {
186185
crossProducts.add(withSuffix.toCharArray());
@@ -192,7 +191,7 @@ private List<String> expandRoot(Root<String> root, String misspelled) {
192191
true,
193192
misspelled,
194193
(prefixLength, prefixId) -> {
195-
if (!dictionary.hasFlag(root.entryId, dictionary.affixData(prefixId, AFFIX_FLAG))
194+
if (!dictionary.hasFlag(root.entryId(), dictionary.affixData(prefixId, AFFIX_FLAG))
196195
|| !dictionary.isCrossProduct(prefixId)) {
197196
return;
198197
}
@@ -217,7 +216,7 @@ private List<String> expandRoot(Root<String> root, String misspelled) {
217216
if (hasCompatibleFlags(root, prefixId)
218217
&& checkAffixCondition(prefixId, wordChars, stripLength, stemLength)) {
219218
String prefix = misspelled.substring(0, prefixLength);
220-
result.add(prefix + root.word.substring(stripLength));
219+
result.add(prefix + root.word().substring(stripLength));
221220
}
222221
});
223222

@@ -263,7 +262,7 @@ private interface AffixProcessor {
263262
}
264263

265264
private boolean hasCompatibleFlags(Root<?> root, int affixId) {
266-
if (!dictionary.hasFlag(root.entryId, dictionary.affixData(affixId, AFFIX_FLAG))) {
265+
if (!dictionary.hasFlag(root.entryId(), dictionary.affixData(affixId, AFFIX_FLAG))) {
267266
return false;
268267
}
269268

@@ -447,28 +446,8 @@ private static int commonCharacterPositionScore(String s1, String s2) {
447446
return commonScore;
448447
}
449448

450-
private static class Weighted<T extends Comparable<T>> implements Comparable<Weighted<T>> {
451-
final T word;
452-
final int score;
453-
454-
Weighted(T word, int score) {
455-
this.word = word;
456-
this.score = score;
457-
}
458-
459-
@Override
460-
public boolean equals(Object o) {
461-
if (this == o) return true;
462-
if (!(o instanceof Weighted)) return false;
463-
@SuppressWarnings("unchecked")
464-
Weighted<T> that = (Weighted<T>) o;
465-
return score == that.score && word.equals(that.word);
466-
}
467-
468-
@Override
469-
public int hashCode() {
470-
return Objects.hash(word, score);
471-
}
449+
private record Weighted<T extends Comparable<T>>(T word, int score)
450+
implements Comparable<Weighted<T>> {
472451

473452
@Override
474453
public String toString() {

lucene/analysis/common/src/java/org/apache/lucene/analysis/hunspell/Hunspell.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ boolean checkWord(String word) {
132132
Boolean checkSimpleWord(char[] wordChars, int length, WordCase originalCase) {
133133
Root<CharsRef> entry = findStem(wordChars, 0, length, originalCase, SIMPLE_WORD);
134134
if (entry != null) {
135-
return !dictionary.hasFlag(entry.entryId, dictionary.forbiddenword);
135+
return !dictionary.hasFlag(entry.entryId(), dictionary.forbiddenword);
136136
}
137137

138138
return null;
@@ -229,7 +229,7 @@ private boolean checkCompounds(CharsRef word, WordCase originalCase, CompoundPar
229229
stem = findStem(word.chars, word.offset, breakPos + 1, originalCase, context);
230230
}
231231
if (stem != null
232-
&& !dictionary.hasFlag(stem.entryId, dictionary.forbiddenword)
232+
&& !dictionary.hasFlag(stem.entryId(), dictionary.forbiddenword)
233233
&& (prev == null || prev.mayCompound(stem, breakPos, originalCase))) {
234234
CompoundPart part = new CompoundPart(prev, word, breakPos, stem, null);
235235
if (checkCompoundsAfter(originalCase, part)) {
@@ -274,7 +274,7 @@ private boolean checkCompoundsAfter(WordCase originalCase, CompoundPart prev) {
274274
Root<CharsRef> lastRoot =
275275
findStem(word.chars, breakOffset, remainingLength, originalCase, COMPOUND_END);
276276
if (lastRoot != null
277-
&& !dictionary.hasFlag(lastRoot.entryId, dictionary.forbiddenword)
277+
&& !dictionary.hasFlag(lastRoot.entryId(), dictionary.forbiddenword)
278278
&& !(dictionary.checkCompoundDup && prev.root.equals(lastRoot))
279279
&& !hasForceUCaseProblem(lastRoot, originalCase, word.chars)
280280
&& prev.mayCompound(lastRoot, remainingLength, originalCase)) {
@@ -288,7 +288,7 @@ private boolean checkCompoundsAfter(WordCase originalCase, CompoundPart prev) {
288288
private boolean hasForceUCaseProblem(Root<?> root, WordCase originalCase, char[] wordChars) {
289289
if (originalCase == WordCase.TITLE || originalCase == WordCase.UPPER) return false;
290290
if (originalCase == null && Character.isUpperCase(wordChars[0])) return false;
291-
return dictionary.hasFlag(root.entryId, dictionary.forceUCase);
291+
return dictionary.hasFlag(root.entryId(), dictionary.forceUCase);
292292
}
293293

294294
/**

lucene/analysis/common/src/java/org/apache/lucene/analysis/hunspell/Root.java

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,36 +16,13 @@
1616
*/
1717
package org.apache.lucene.analysis.hunspell;
1818

19-
import java.util.Objects;
20-
21-
class Root<T extends CharSequence> implements Comparable<Root<T>> {
22-
final T word;
23-
final int entryId;
24-
25-
Root(T word, int entryId) {
26-
this.word = word;
27-
this.entryId = entryId;
28-
}
19+
record Root<T extends CharSequence>(T word, int entryId) implements Comparable<Root<T>> {
2920

3021
@Override
3122
public String toString() {
3223
return word.toString();
3324
}
3425

35-
@Override
36-
public boolean equals(Object o) {
37-
if (this == o) return true;
38-
if (!(o instanceof Root)) return false;
39-
@SuppressWarnings("unchecked")
40-
Root<T> root = (Root<T>) o;
41-
return entryId == root.entryId && word.equals(root.word);
42-
}
43-
44-
@Override
45-
public int hashCode() {
46-
return Objects.hash(word, entryId);
47-
}
48-
4926
@Override
5027
public int compareTo(Root<T> o) {
5128
return CharSequence.compare(word, o.word);

lucene/analysis/common/src/java/org/apache/lucene/analysis/pattern/PatternTypingFilter.java

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,12 @@ public PatternTypingFilter(TokenStream input, PatternTypingRule... replacementAn
5959
public final boolean incrementToken() throws IOException {
6060
if (input.incrementToken()) {
6161
for (PatternTypingRule rule : replacementAndFlagByPattern) {
62-
Matcher matcher = rule.getPattern().matcher(termAtt);
62+
Matcher matcher = rule.pattern().matcher(termAtt);
6363
if (matcher.find()) {
6464
// allow 2nd reset() and find() that occurs inside replaceFirst to avoid excess string
6565
// creation
66-
typeAtt.setType(matcher.replaceFirst(rule.getTypeTemplate()));
67-
flagAtt.setFlags(rule.getFlags());
66+
typeAtt.setType(matcher.replaceFirst(rule.typeTemplate()));
67+
flagAtt.setFlags(rule.flags());
6868
return true;
6969
}
7070
}
@@ -74,27 +74,5 @@ public final boolean incrementToken() throws IOException {
7474
}
7575

7676
/** Value holding class for pattern typing rules. */
77-
public static class PatternTypingRule {
78-
private final Pattern pattern;
79-
private final int flags;
80-
private final String typeTemplate;
81-
82-
public PatternTypingRule(Pattern pattern, int flags, String typeTemplate) {
83-
this.pattern = pattern;
84-
this.flags = flags;
85-
this.typeTemplate = typeTemplate;
86-
}
87-
88-
public Pattern getPattern() {
89-
return pattern;
90-
}
91-
92-
public int getFlags() {
93-
return flags;
94-
}
95-
96-
public String getTypeTemplate() {
97-
return typeTemplate;
98-
}
99-
}
77+
public record PatternTypingRule(Pattern pattern, int flags, String typeTemplate) {}
10078
}

lucene/analysis/common/src/java/org/apache/lucene/analysis/synonym/SynonymGraphFilter.java

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -142,22 +142,10 @@ public void reset() {
142142
}
143143
}
144144

145-
static class BufferedOutputToken {
146-
final String term;
147-
148-
// Non-null if this was an incoming token:
149-
final State state;
150-
151-
final int startNode;
152-
final int endNode;
153-
154-
public BufferedOutputToken(State state, String term, int startNode, int endNode) {
155-
this.state = state;
156-
this.term = term;
157-
this.startNode = startNode;
158-
this.endNode = endNode;
159-
}
160-
}
145+
/**
146+
* @param state Non-null if this was an incoming token:
147+
*/
148+
record BufferedOutputToken(State state, String term, int startNode, int endNode) {}
161149

162150
/**
163151
* Apply previously built synonyms to incoming tokens.

lucene/analysis/common/src/java/org/apache/lucene/analysis/synonym/word2vec/TermAndBoost.java

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,15 @@
1818

1919
import org.apache.lucene.util.BytesRef;
2020

21-
/** Wraps a term and boost */
22-
public class TermAndBoost {
23-
/** the term */
24-
public final BytesRef term;
25-
26-
/** the boost */
27-
public final float boost;
28-
21+
/**
22+
* Wraps a term and boost
23+
*
24+
* @param term the term
25+
* @param boost the boost
26+
*/
27+
public record TermAndBoost(BytesRef term, float boost) {
2928
/** Creates a new TermAndBoost */
30-
public TermAndBoost(BytesRef term, float boost) {
31-
this.term = BytesRef.deepCopyOf(term);
32-
this.boost = boost;
29+
public TermAndBoost {
30+
term = BytesRef.deepCopyOf(term);
3331
}
3432
}

lucene/analysis/common/src/java/org/apache/lucene/analysis/synonym/word2vec/Word2VecSynonymFilter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public boolean incrementToken() throws IOException {
8080
clearAttributes();
8181
restoreState(this.lastState);
8282
termAtt.setEmpty();
83-
termAtt.append(synonym.term.utf8ToString());
83+
termAtt.append(synonym.term().utf8ToString());
8484
typeAtt.setType(SynonymGraphFilter.TYPE_SYNONYM);
8585
posLenAtt.setPositionLength(1);
8686
posIncrementAtt.setPositionIncrement(0);

0 commit comments

Comments
 (0)