Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert more classes to record classes #13328

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import java.util.Comparator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Objects;
import java.util.PriorityQueue;
import java.util.Set;
import java.util.TreeSet;
Expand Down Expand Up @@ -447,14 +446,8 @@ private static int commonCharacterPositionScore(String s1, String s2) {
return commonScore;
}

private static class Weighted<T extends Comparable<T>> implements Comparable<Weighted<T>> {
final T word;
final int score;

Weighted(T word, int score) {
this.word = word;
this.score = score;
}
private record Weighted<T extends Comparable<T>>(T word, int score)
implements Comparable<Weighted<T>> {

@Override
public boolean equals(Object o) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

needs to be removed, too

Expand All @@ -465,11 +458,6 @@ public boolean equals(Object o) {
return score == that.score && word.equals(that.word);
}

@Override
public int hashCode() {
return Objects.hash(word, score);
}

@Override
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's keep that one, as the toString output is different from default

public String toString() {
return word + "(" + score + ")";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@ public PatternTypingFilter(TokenStream input, PatternTypingRule... replacementAn
public final boolean incrementToken() throws IOException {
if (input.incrementToken()) {
for (PatternTypingRule rule : replacementAndFlagByPattern) {
Matcher matcher = rule.getPattern().matcher(termAtt);
Matcher matcher = rule.pattern().matcher(termAtt);
if (matcher.find()) {
// allow 2nd reset() and find() that occurs inside replaceFirst to avoid excess string
// creation
typeAtt.setType(matcher.replaceFirst(rule.getTypeTemplate()));
flagAtt.setFlags(rule.getFlags());
typeAtt.setType(matcher.replaceFirst(rule.typeTemplate()));
flagAtt.setFlags(rule.flags());
return true;
}
}
Expand All @@ -74,27 +74,5 @@ public final boolean incrementToken() throws IOException {
}

/** Value holding class for pattern typing rules. */
public static class PatternTypingRule {
private final Pattern pattern;
private final int flags;
private final String typeTemplate;

public PatternTypingRule(Pattern pattern, int flags, String typeTemplate) {
this.pattern = pattern;
this.flags = flags;
this.typeTemplate = typeTemplate;
}

public Pattern getPattern() {
return pattern;
}

public int getFlags() {
return flags;
}

public String getTypeTemplate() {
return typeTemplate;
}
}
public record PatternTypingRule(Pattern pattern, int flags, String typeTemplate) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -142,22 +142,10 @@ public void reset() {
}
}

static class BufferedOutputToken {
final String term;

// Non-null if this was an incoming token:
final State state;

final int startNode;
final int endNode;

public BufferedOutputToken(State state, String term, int startNode, int endNode) {
this.state = state;
this.term = term;
this.startNode = startNode;
this.endNode = endNode;
}
}
/**
* @param state Non-null if this was an incoming token:
*/
record BufferedOutputToken(State state, String term, int startNode, int endNode) {}

/**
* Apply previously built synonyms to incoming tokens.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,13 @@

import org.apache.lucene.util.BytesRef;

/** Wraps a term and boost */
public class TermAndBoost {
/** the term */
public final BytesRef term;

/** the boost */
public final float boost;

/**
* Wraps a term and boost
*
* @param term the term
* @param boost the boost
*/
public record TermAndBoost(BytesRef term, float boost) {
/** Creates a new TermAndBoost */
public TermAndBoost(BytesRef term, float boost) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this can be converted to the generic ctor without parameters:

public TermAndBoost {
  term = BytesRef.deepCopyOf(term);
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!.....I changed others such occurrences that I could find as well

this.term = BytesRef.deepCopyOf(term);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,23 +58,23 @@ private Word2VecModel(
public void addTermAndVector(TermAndVector modelEntry) {
modelEntry.normalizeVector();
this.termsAndVectors[loadedCount++] = modelEntry;
this.word2Vec.add(modelEntry.getTerm());
this.word2Vec.add(modelEntry.term());
}

@Override
public float[] vectorValue(int targetOrd) {
return termsAndVectors[targetOrd].getVector();
return termsAndVectors[targetOrd].vector();
}

public float[] vectorValue(BytesRef term) {
int termOrd = this.word2Vec.find(term);
if (termOrd < 0) return null;
TermAndVector entry = this.termsAndVectors[termOrd];
return (entry == null) ? null : entry.getVector();
return (entry == null) ? null : entry.vector();
}

public BytesRef termValue(int targetOrd) {
return termsAndVectors[targetOrd].getTerm();
return termsAndVectors[targetOrd].term();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public boolean incrementToken() throws IOException {
clearAttributes();
restoreState(this.lastState);
termAtt.setEmpty();
termAtt.append(synonym.term.utf8ToString());
termAtt.append(synonym.term().utf8ToString());
typeAtt.setType(SynonymGraphFilter.TYPE_SYNONYM);
posLenAtt.setPositionLength(1);
posIncrementAtt.setPositionIncrement(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public void getSynonyms_shouldReturnSynonymsBasedOnMinAcceptedSimilarity() throw

assertEquals(4, actualSynonymsResults.size());
for (int i = 0; i < expectedSynonyms.length; i++) {
assertEquals(new BytesRef(expectedSynonyms[i]), actualSynonymsResults.get(i).term);
assertEquals(new BytesRef(expectedSynonyms[i]), actualSynonymsResults.get(i).term());
}
}

Expand All @@ -83,8 +83,8 @@ public void getSynonyms_shouldReturnSynonymsBoost() throws Exception {

BytesRef expectedFirstSynonymTerm = new BytesRef("b");
double expectedFirstSynonymBoost = 1.0;
assertEquals(expectedFirstSynonymTerm, actualSynonymsResults.get(0).term);
assertEquals(expectedFirstSynonymBoost, actualSynonymsResults.get(0).boost, 0.001f);
assertEquals(expectedFirstSynonymTerm, actualSynonymsResults.get(0).term());
assertEquals(expectedFirstSynonymBoost, actualSynonymsResults.get(0).boost(), 0.001f);
}

@Test
Expand Down Expand Up @@ -121,7 +121,7 @@ public void testModel_shouldReturnNormalizedVectors() {
public void normalizedVector_shouldReturnModule1() {
TermAndVector synonymTerm = new TermAndVector(new BytesRef("a"), new float[] {10, 10});
synonymTerm.normalizeVector();
float[] vector = synonymTerm.getVector();
float[] vector = synonymTerm.vector();
float len = 0;
for (int i = 0; i < vector.length; i++) {
len += vector[i] * vector[i];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,19 +139,7 @@ private void mayIncrementToken() throws IOException {
}
}

private static class CompletionToken {
final String term;
final boolean isFirst;
final int startOffset;
final int endOffset;

CompletionToken(String term, boolean isFirst, int startOffset, int endOffset) {
this.term = term;
this.isFirst = isFirst;
this.startOffset = startOffset;
this.endOffset = endOffset;
}
}
private record CompletionToken(String term, boolean isFirst, int startOffset, int endOffset) {}

private static class CompletionTokenGenerator implements Iterator<CompletionToken> {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,13 +180,5 @@ private MatchedKeystroke longestKeystrokeMatch(CharsRef input, int inputOffset)
return null;
}

private static class MatchedKeystroke {
final int keystrokeLen;
final int keystrokeIndex;

MatchedKeystroke(int keystrokeLen, int keystrokeIndex) {
this.keystrokeLen = keystrokeLen;
this.keystrokeIndex = keystrokeIndex;
}
}
private record MatchedKeystroke(int keystrokeLen, int keystrokeIndex) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -268,19 +268,19 @@ protected void backtrace(Position endPosData, int fromIDX) {
final KoMorphData.Morpheme morpheme = morphemes[i];
final Token compoundToken;
if (token.getPOSType() == POS.Type.COMPOUND) {
assert endOffset - morpheme.surfaceForm.length() >= 0;
assert endOffset - morpheme.surfaceForm().length() >= 0;
compoundToken =
new DecompoundToken(
morpheme.posTag,
morpheme.surfaceForm,
endOffset - morpheme.surfaceForm.length(),
morpheme.posTag(),
morpheme.surfaceForm(),
endOffset - morpheme.surfaceForm().length(),
endOffset,
backType);
} else {
compoundToken =
new DecompoundToken(
morpheme.posTag,
morpheme.surfaceForm,
morpheme.posTag(),
morpheme.surfaceForm(),
token.getStartOffset(),
token.getEndOffset(),
backType);
Expand All @@ -289,7 +289,7 @@ protected void backtrace(Position endPosData, int fromIDX) {
compoundToken.setPositionIncrement(0);
}
++posLen;
endOffset -= morpheme.surfaceForm.length();
endOffset -= morpheme.surfaceForm().length();
pending.add(compoundToken);
if (VERBOSE) {
System.out.println(" add token=" + pending.get(pending.size() - 1));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,7 @@
/** Represents Korean morphological information. */
public interface KoMorphData extends MorphData {
/** A morpheme extracted from a compound token. */
class Morpheme {
public final POS.Tag posTag;
public final String surfaceForm;

public Morpheme(POS.Tag posTag, String surfaceForm) {
this.posTag = posTag;
this.surfaceForm = surfaceForm;
}
}
record Morpheme(POS.Tag posTag, String surfaceForm) {}

/**
* Get the {@link org.apache.lucene.analysis.ko.POS.Type} of specified word (morpheme, compound,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,13 +150,13 @@ protected int putEntry(String[] entry) {
int compoundOffset = 0;
for (KoMorphData.Morpheme morpheme : morphemes) {
if (hasSinglePOS == false) {
buffer.put((byte) morpheme.posTag.ordinal());
buffer.put((byte) morpheme.posTag().ordinal());
}
if (posType != POS.Type.INFLECT) {
buffer.put((byte) morpheme.surfaceForm.length());
compoundOffset += morpheme.surfaceForm.length();
buffer.put((byte) morpheme.surfaceForm().length());
compoundOffset += morpheme.surfaceForm().length();
} else {
writeString(morpheme.surfaceForm);
writeString(morpheme.surfaceForm());
}
assert compoundOffset <= entry[0].length() : Arrays.toString(entry);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,11 @@ private String displayMorphemes(KoMorphData.Morpheme[] morphemes) {
builder.append("+");
}
builder
.append(morpheme.surfaceForm)
.append(morpheme.surfaceForm())
.append('/')
.append(morpheme.posTag.name())
.append(morpheme.posTag().name())
.append('(')
.append(morpheme.posTag.description())
.append(morpheme.posTag().description())
.append(')');
}
return builder.toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,14 +170,14 @@ public void testEnumerateAll() throws Exception {
if (decompound != null) {
int offset = 0;
for (KoMorphData.Morpheme morph : decompound) {
assertTrue(UnicodeUtil.validUTF16String(morph.surfaceForm));
assertFalse(morph.surfaceForm.isEmpty());
assertEquals(morph.surfaceForm.trim(), morph.surfaceForm);
assertTrue(UnicodeUtil.validUTF16String(morph.surfaceForm()));
assertFalse(morph.surfaceForm().isEmpty());
assertEquals(morph.surfaceForm().trim(), morph.surfaceForm());
if (type != POS.Type.INFLECT) {
assertEquals(
morph.surfaceForm,
surfaceForm.substring(offset, offset + morph.surfaceForm.length()));
offset += morph.surfaceForm.length();
morph.surfaceForm(),
surfaceForm.substring(offset, offset + morph.surfaceForm().length()));
offset += morph.surfaceForm().length();
}
}
assertTrue(offset <= surfaceForm.length());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ public void testLookup() throws IOException {
dictionary.getMorphAttributes().getMorphemes(wordIds.get(1), sArray, 0, s.length());
assertNotNull(decompound);
assertEquals(2, decompound.length);
assertEquals(decompound[0].posTag, POS.Tag.NNG);
assertEquals(decompound[0].surfaceForm, "세종");
assertEquals(decompound[1].posTag, POS.Tag.NNG);
assertEquals(decompound[1].surfaceForm, "시");
assertEquals(decompound[0].posTag(), POS.Tag.NNG);
assertEquals(decompound[0].surfaceForm(), "세종");
assertEquals(decompound[1].posTag(), POS.Tag.NNG);
assertEquals(decompound[1].surfaceForm(), "시");

s = "c++";
sArray = s.toCharArray();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,19 +103,20 @@ private static int encodedSize(
for (int bpv = 1; bpv <= 32; ++bpv) {
final FormatAndBits formatAndBits =
PackedInts.fastestFormatAndBits(BLOCK_SIZE, bpv, acceptableOverheadRatio);
assert formatAndBits.format.isSupported(formatAndBits.bitsPerValue);
assert formatAndBits.bitsPerValue <= 32;
assert formatAndBits.format().isSupported(formatAndBits.bitsPerValue());
assert formatAndBits.bitsPerValue() <= 32;
encodedSizes[bpv] =
encodedSize(formatAndBits.format, PackedInts.VERSION_CURRENT, formatAndBits.bitsPerValue);
encodedSize(
formatAndBits.format(), PackedInts.VERSION_CURRENT, formatAndBits.bitsPerValue());
encoders[bpv] =
PackedInts.getEncoder(
formatAndBits.format, PackedInts.VERSION_CURRENT, formatAndBits.bitsPerValue);
formatAndBits.format(), PackedInts.VERSION_CURRENT, formatAndBits.bitsPerValue());
decoders[bpv] =
PackedInts.getDecoder(
formatAndBits.format, PackedInts.VERSION_CURRENT, formatAndBits.bitsPerValue);
formatAndBits.format(), PackedInts.VERSION_CURRENT, formatAndBits.bitsPerValue());
iterations[bpv] = computeIterations(decoders[bpv]);

out.writeVInt(formatAndBits.format.getId() << 5 | (formatAndBits.bitsPerValue - 1));
out.writeVInt(formatAndBits.format().getId() << 5 | (formatAndBits.bitsPerValue() - 1));
}
}

Expand Down