Skip to content

Commit 117f552

Browse files
eamonnmcmanusgoogle-java-format Team
authored andcommitted
Convert classes to records where possible.
PiperOrigin-RevId: 890973123
1 parent bf4b13b commit 117f552

File tree

12 files changed

+75
-224
lines changed

12 files changed

+75
-224
lines changed

core/src/main/java/com/google/googlejavaformat/Doc.java

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -74,19 +74,7 @@ public enum FillMode {
7474
public static final int MAX_LINE_WIDTH = 1000;
7575

7676
/** State for writing. */
77-
public static final class State {
78-
final int lastIndent;
79-
final int indent;
80-
final int column;
81-
final boolean mustBreak;
82-
83-
State(int lastIndent, int indent, int column, boolean mustBreak) {
84-
this.lastIndent = lastIndent;
85-
this.indent = indent;
86-
this.column = column;
87-
this.mustBreak = mustBreak;
88-
}
89-
77+
public record State(int lastIndent, int indent, int column, boolean mustBreak) {
9078
public State(int indent0, int column0) {
9179
this(indent0, indent0, column0, false);
9280
}
@@ -98,16 +86,6 @@ State withColumn(int column) {
9886
State withMustBreak(boolean mustBreak) {
9987
return new State(lastIndent, indent, column, mustBreak);
10088
}
101-
102-
@Override
103-
public String toString() {
104-
return MoreObjects.toStringHelper(this)
105-
.add("lastIndent", lastIndent)
106-
.add("indent", indent)
107-
.add("column", column)
108-
.add("mustBreak", mustBreak)
109-
.toString();
110-
}
11189
}
11290

11391
private static final Range<Integer> EMPTY_RANGE = Range.closedOpen(-1, -1);

core/src/main/java/com/google/googlejavaformat/FormatterDiagnostic.java

Lines changed: 18 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -17,60 +17,36 @@
1717
import static com.google.common.base.Preconditions.checkArgument;
1818
import static com.google.common.base.Preconditions.checkNotNull;
1919

20-
/** An error that prevented formatting from succeeding. */
21-
public class FormatterDiagnostic {
22-
private final int lineNumber;
23-
private final String message;
24-
private final int column;
25-
26-
public static FormatterDiagnostic create(String message) {
27-
return new FormatterDiagnostic(-1, -1, message);
28-
}
29-
30-
public static FormatterDiagnostic create(int lineNumber, int column, String message) {
31-
checkArgument(lineNumber >= 0);
32-
checkArgument(column >= 0);
20+
/**
21+
* An error that prevented formatting from succeeding.
22+
*
23+
* @param line the line number on which the error occurred, or {@code -1} if the error does not have
24+
* a line number.
25+
* @param column the 1-indexed column number on which the error occurred, or {@code -1} if the error
26+
* does not have a column.
27+
* @param message a description of the problem that prevented formatting from succeeding.
28+
*/
29+
public record FormatterDiagnostic(int line, int column, String message) {
30+
public FormatterDiagnostic {
31+
checkArgument(line >= -1);
32+
checkArgument(column >= -1);
3333
checkNotNull(message);
34-
return new FormatterDiagnostic(lineNumber, column, message);
35-
}
36-
37-
private FormatterDiagnostic(int lineNumber, int column, String message) {
38-
this.lineNumber = lineNumber;
39-
this.column = column;
40-
this.message = message;
41-
}
42-
43-
/**
44-
* Returns the line number on which the error occurred, or {@code -1} if the error does not have a
45-
* line number.
46-
*/
47-
public int line() {
48-
return lineNumber;
49-
}
50-
51-
/**
52-
* Returns the 1-indexed column number on which the error occurred, or {@code -1} if the error
53-
* does not have a column.
54-
*/
55-
public int column() {
56-
return column;
5734
}
5835

59-
/** Returns a description of the problem that prevented formatting from succeeding. */
60-
public String message() {
61-
return message;
36+
public FormatterDiagnostic(String message) {
37+
this(-1, -1, message);
6238
}
6339

6440
@Override
6541
public String toString() {
6642
StringBuilder sb = new StringBuilder();
67-
if (lineNumber >= 0) {
68-
sb.append(lineNumber).append(':');
43+
if (line >= 0) {
44+
sb.append(line).append(':');
6945
}
7046
if (column >= 0) {
7147
sb.append(column).append(':');
7248
}
73-
if (lineNumber >= 0 || column >= 0) {
49+
if (line >= 0 || column >= 0) {
7450
sb.append(' ');
7551
}
7652
sb.append("error: ").append(message);

core/src/main/java/com/google/googlejavaformat/Input.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ public String toString() {
141141
* numbers.
142142
*/
143143
public FormatterDiagnostic createDiagnostic(int inputPosition, String message) {
144-
return FormatterDiagnostic.create(
144+
return new FormatterDiagnostic(
145145
getLineNumber(inputPosition), getColumnNumber(inputPosition), message);
146146
}
147147
}

core/src/main/java/com/google/googlejavaformat/java/FormatterException.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public final class FormatterException extends Exception {
3232
private final ImmutableList<FormatterDiagnostic> diagnostics;
3333

3434
public FormatterException(String message) {
35-
this(FormatterDiagnostic.create(message));
35+
this(new FormatterDiagnostic(message));
3636
}
3737

3838
public FormatterException(FormatterDiagnostic diagnostic) {
@@ -57,7 +57,7 @@ public static FormatterException fromJavacDiagnostics(
5757
}
5858

5959
private static FormatterDiagnostic toFormatterDiagnostic(Diagnostic<?> input) {
60-
return FormatterDiagnostic.create(
60+
return new FormatterDiagnostic(
6161
(int) input.getLineNumber(), (int) input.getColumnNumber(), input.getMessage(ENGLISH));
6262
}
6363

core/src/main/java/com/google/googlejavaformat/java/ImportOrderer.java

Lines changed: 15 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -195,31 +195,21 @@ enum ImportType {
195195
NORMAL
196196
}
197197

198-
/** An import statement. */
199-
class Import {
200-
private final String imported;
201-
private final String trailing;
202-
private final ImportType importType;
203-
204-
Import(String imported, String trailing, ImportType importType) {
205-
this.imported = imported;
206-
this.trailing = trailing;
207-
this.importType = importType;
208-
}
209-
210-
/** The name being imported, for example {@code java.util.List}. */
211-
String imported() {
212-
return imported;
213-
}
214-
215-
/** Returns the {@link ImportType}. */
216-
ImportType importType() {
217-
return importType;
218-
}
219-
198+
/**
199+
* An import statement.
200+
*
201+
* @param imported the name being imported, for example {@code java.util.List}.
202+
* @param trailing the {@code //} comment lines after the final {@code ;}, up to and including the
203+
* line terminator of the last one. Note: In case two imports were separated by a space (which
204+
* is disallowed by the style guide), the trailing whitespace of the first import does not
205+
* include a line terminator.
206+
* @param importType the {@link ImportType} of the import.
207+
* @param lineSeparator the line separator to use when formatting the import.
208+
*/
209+
record Import(String imported, String trailing, ImportType importType, String lineSeparator) {
220210
/** The top-level package of the import. */
221211
String topLevel() {
222-
return DOT_SPLITTER.split(imported()).iterator().next();
212+
return DOT_SPLITTER.split(imported).iterator().next();
223213
}
224214

225215
/** True if this is an Android import per AOSP style. */
@@ -236,16 +226,6 @@ boolean isJava() {
236226
};
237227
}
238228

239-
/**
240-
* The {@code //} comment lines after the final {@code ;}, up to and including the line
241-
* terminator of the last one. Note: In case two imports were separated by a space (which is
242-
* disallowed by the style guide), the trailing whitespace of the first import does not include
243-
* a line terminator.
244-
*/
245-
String trailing() {
246-
return trailing;
247-
}
248-
249229
/** True if this is a third-party import per AOSP style. */
250230
public boolean isThirdParty() {
251231
return !(isAndroid() || isJava());
@@ -280,15 +260,7 @@ private String tokString(int start, int end) {
280260
return sb.toString();
281261
}
282262

283-
private static class ImportsAndIndex {
284-
final ImmutableSortedSet<Import> imports;
285-
final int index;
286-
287-
ImportsAndIndex(ImmutableSortedSet<Import> imports, int index) {
288-
this.imports = imports;
289-
this.index = index;
290-
}
291-
}
263+
private record ImportsAndIndex(ImmutableSortedSet<Import> imports, int index) {}
292264

293265
/**
294266
* Scans a sequence of import lines. The parsing uses this approximate grammar:
@@ -366,7 +338,7 @@ private ImportsAndIndex scanImports(int i) throws FormatterException {
366338
// Extra semicolons are not allowed by the JLS but are accepted by javac.
367339
i++;
368340
}
369-
imports.add(new Import(importedName, trailing.toString(), importType));
341+
imports.add(new Import(importedName, trailing.toString(), importType, lineSeparator));
370342
// Remember the position just after the import we just saw, before skipping blank lines.
371343
// If the next thing after the blank lines is not another import then we don't want to
372344
// include those blank lines in the text to be replaced.

core/src/main/java/com/google/googlejavaformat/java/JavaOutput.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -337,13 +337,13 @@ private Range<Integer> expandToBreakableRegions(Range<Integer> iRange) {
337337

338338
public static String applyReplacements(String input, List<Replacement> replacements) {
339339
replacements = new ArrayList<>(replacements);
340-
replacements.sort(comparing((Replacement r) -> r.getReplaceRange().lowerEndpoint()).reversed());
340+
replacements.sort(comparing((Replacement r) -> r.replaceRange().lowerEndpoint()).reversed());
341341
StringBuilder writer = new StringBuilder(input);
342342
for (Replacement replacement : replacements) {
343343
writer.replace(
344-
replacement.getReplaceRange().lowerEndpoint(),
345-
replacement.getReplaceRange().upperEndpoint(),
346-
replacement.getReplacementString());
344+
replacement.replaceRange().lowerEndpoint(),
345+
replacement.replaceRange().upperEndpoint(),
346+
replacement.replacementString());
347347
}
348348
return writer.toString();
349349
}

core/src/main/java/com/google/googlejavaformat/java/JavacTokens.java

Lines changed: 14 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -37,40 +37,15 @@ final class JavacTokens {
3737
// TODO(b/33103797): fix javac and remove the work-around
3838
private static final CharSequence EOF_COMMENT = "\n//EOF";
3939

40-
/** An unprocessed input token, including whitespace and comments. */
41-
static class RawTok {
42-
private final String stringVal;
43-
private final TokenKind kind;
44-
private final int pos;
45-
private final int endPos;
46-
47-
RawTok(String stringVal, TokenKind kind, int pos, int endPos) {
48-
this.stringVal = stringVal;
49-
this.kind = kind;
50-
this.pos = pos;
51-
this.endPos = endPos;
52-
}
53-
54-
/** The token kind, or {@code null} for whitespace and comments. */
55-
public TokenKind kind() {
56-
return kind;
57-
}
58-
59-
/** The start position. */
60-
public int pos() {
61-
return pos;
62-
}
63-
64-
/** The end position. */
65-
public int endPos() {
66-
return endPos;
67-
}
68-
69-
/** The escaped string value of a literal, or {@code null} for other tokens. */
70-
public String stringVal() {
71-
return stringVal;
72-
}
73-
}
40+
/**
41+
* An unprocessed input token, including whitespace and comments.
42+
*
43+
* @param stringVal the escaped string value of a literal, or {@code null} for other tokens.
44+
* @param kind the token kind, or {@code null} for whitespace and comments.
45+
* @param pos the start position.
46+
* @param endPos the end position.
47+
*/
48+
record RawTok(String stringVal, TokenKind kind, int pos, int endPos) {}
7449

7550
/** Lex the input and return a list of {@link RawTok}s. */
7651
public static ImmutableList<RawTok> getTokens(
@@ -94,8 +69,8 @@ public static ImmutableList<RawTok> getTokens(
9469
tokens.add(new RawTok(null, null, last, c.getSourcePos(0)));
9570
}
9671
tokens.add(
97-
new RawTok(null, null, c.getSourcePos(0), c.getSourcePos(0) + c.getText().length()));
98-
last = c.getSourcePos(0) + c.getText().length();
72+
new RawTok(null, null, c.getSourcePos(0), c.getSourcePos(0) + c.text().length()));
73+
last = c.getSourcePos(0) + c.text().length();
9974
}
10075
}
10176
if (stopTokens.contains(t.kind)) {
@@ -173,25 +148,14 @@ private char[] getRawCharactersReflectively(int beginIndex, int endIndex) {
173148
}
174149

175150
/** A {@link Comment} that saves its text and start position. */
176-
static class CommentWithTextAndPosition {
177-
178-
private final int pos;
179-
private final int endPos;
180-
private final String text;
181-
182-
public CommentWithTextAndPosition(int pos, int endPos, String text) {
183-
this.pos = pos;
184-
this.endPos = endPos;
185-
this.text = text;
186-
}
187-
151+
record CommentWithTextAndPosition(int pos, int endPos, String text) {
188152
/**
189153
* Returns the source position of the character at index {@code index} in the comment text.
190154
*
191155
* <p>The handling of javadoc comments in javac has more logic to skip over leading whitespace
192156
* and '*' characters when indexing into doc comments, but we don't need any of that.
193157
*/
194-
public int getSourcePos(int index) {
158+
int getSourcePos(int index) {
195159
checkArgument(
196160
0 <= index && index < (endPos - pos),
197161
"Expected %s in the range [0, %s)",
@@ -200,13 +164,9 @@ public int getSourcePos(int index) {
200164
return pos + index;
201165
}
202166

203-
public String getText() {
204-
return text;
205-
}
206-
207167
@Override
208168
public String toString() {
209-
return String.format("Comment: '%s'", getText());
169+
return String.format("Comment: '%s'", text());
210170
}
211171
}
212172

core/src/main/java/com/google/googlejavaformat/java/ModifierOrderer.java

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,8 @@ static JavaInput reorderModifiers(String text) throws FormatterException {
5050
* (e.g. for {@code public}), but may be multiple tokens for modifiers containing {@code -} (e.g.
5151
* {@code non-sealed}).
5252
*/
53-
static class ModifierTokens implements Comparable<ModifierTokens> {
54-
private final ImmutableList<Token> tokens;
55-
private final Modifier modifier;
53+
record ModifierTokens(ImmutableList<Token> tokens, Modifier modifier)
54+
implements Comparable<ModifierTokens> {
5655

5756
static ModifierTokens create(ImmutableList<Token> tokens) {
5857
return new ModifierTokens(tokens, asModifier(tokens));
@@ -62,23 +61,10 @@ static ModifierTokens empty() {
6261
return new ModifierTokens(ImmutableList.of(), null);
6362
}
6463

65-
ModifierTokens(ImmutableList<Token> tokens, Modifier modifier) {
66-
this.tokens = tokens;
67-
this.modifier = modifier;
68-
}
69-
7064
boolean isEmpty() {
7165
return tokens.isEmpty() || modifier == null;
7266
}
7367

74-
Modifier modifier() {
75-
return modifier;
76-
}
77-
78-
ImmutableList<Token> tokens() {
79-
return tokens;
80-
}
81-
8268
private Token first() {
8369
return tokens.get(0);
8470
}

0 commit comments

Comments
 (0)