Skip to content

Commit 7b0d3a3

Browse files
eamonnmcmanusgoogle-java-format Team
authored andcommitted
Make Token be a record.
The main motivation here is to get an `equals` method so `Token` instances can be compared in tests. PiperOrigin-RevId: 890538523
1 parent 57572c3 commit 7b0d3a3

File tree

4 files changed

+32
-53
lines changed

4 files changed

+32
-53
lines changed

core/src/main/java/com/google/googlejavaformat/java/javadoc/JavadocFormatter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public static String formatJavadoc(String input, int blockIndent) {
5656
private static String render(List<Token> input, int blockIndent) {
5757
JavadocWriter output = new JavadocWriter(blockIndent);
5858
for (Token token : input) {
59-
switch (token.getType()) {
59+
switch (token.type()) {
6060
case BEGIN_JAVADOC -> output.writeBeginJavadoc();
6161
case END_JAVADOC -> {
6262
output.writeEndJavadoc();
@@ -105,7 +105,7 @@ private static Token standardizePToken(Token token) {
105105
}
106106

107107
private static Token standardize(Token token, Token standardToken) {
108-
return SIMPLE_TAG_PATTERN.matcher(token.getValue()).matches() ? standardToken : token;
108+
return SIMPLE_TAG_PATTERN.matcher(token.value()).matches() ? standardToken : token;
109109
}
110110

111111
private static final Token STANDARD_BR_TOKEN = new Token(BR_TAG, "<br>");

core/src/main/java/com/google/googlejavaformat/java/javadoc/JavadocLexer.java

Lines changed: 25 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -295,8 +295,8 @@ private static ImmutableList<Token> joinAdjacentLiteralsAndAdjacentWhitespace(Li
295295
StringBuilder accumulated = new StringBuilder();
296296

297297
for (PeekingIterator<Token> tokens = peekingIterator(input.iterator()); tokens.hasNext(); ) {
298-
if (tokens.peek().getType() == LITERAL) {
299-
accumulated.append(tokens.peek().getValue());
298+
if (tokens.peek().type() == LITERAL) {
299+
accumulated.append(tokens.peek().value());
300300
tokens.next();
301301
continue;
302302
}
@@ -315,14 +315,14 @@ private static ImmutableList<Token> joinAdjacentLiteralsAndAdjacentWhitespace(Li
315315
}
316316

317317
StringBuilder seenWhitespace = new StringBuilder();
318-
while (tokens.peek().getType() == WHITESPACE) {
319-
seenWhitespace.append(tokens.next().getValue());
318+
while (tokens.peek().type() == WHITESPACE) {
319+
seenWhitespace.append(tokens.next().value());
320320
}
321321

322-
if (tokens.peek().getType() == LITERAL && tokens.peek().getValue().startsWith("@")) {
322+
if (tokens.peek().type() == LITERAL && tokens.peek().value().startsWith("@")) {
323323
// OK, we're in the case described above.
324324
accumulated.append(" ");
325-
accumulated.append(tokens.peek().getValue());
325+
accumulated.append(tokens.peek().value());
326326
tokens.next();
327327
continue;
328328
}
@@ -355,14 +355,13 @@ private static ImmutableList<Token> inferParagraphTags(List<Token> input) {
355355
ImmutableList.Builder<Token> output = ImmutableList.builder();
356356

357357
for (PeekingIterator<Token> tokens = peekingIterator(input.iterator()); tokens.hasNext(); ) {
358-
if (tokens.peek().getType() == LITERAL) {
358+
if (tokens.peek().type() == LITERAL) {
359359
output.add(tokens.next());
360360

361-
if (tokens.peek().getType() == WHITESPACE
362-
&& hasMultipleNewlines(tokens.peek().getValue())) {
361+
if (tokens.peek().type() == WHITESPACE && hasMultipleNewlines(tokens.peek().value())) {
363362
output.add(tokens.next());
364363

365-
if (tokens.peek().getType() == LITERAL) {
364+
if (tokens.peek().type() == LITERAL) {
366365
output.add(new Token(PARAGRAPH_OPEN_TAG, "<p>"));
367366
}
368367
}
@@ -393,11 +392,11 @@ private static ImmutableList<Token> optionalizeSpacesAfterLinks(List<Token> inpu
393392
ImmutableList.Builder<Token> output = ImmutableList.builder();
394393

395394
for (PeekingIterator<Token> tokens = peekingIterator(input.iterator()); tokens.hasNext(); ) {
396-
if (tokens.peek().getType() == LITERAL && tokens.peek().getValue().matches("href=[^>]*>")) {
395+
if (tokens.peek().type() == LITERAL && tokens.peek().value().matches("href=[^>]*>")) {
397396
output.add(tokens.next());
398397

399-
if (tokens.peek().getType() == WHITESPACE) {
400-
output.add(new Token(OPTIONAL_LINE_BREAK, tokens.next().getValue()));
398+
if (tokens.peek().type() == WHITESPACE) {
399+
output.add(new Token(OPTIONAL_LINE_BREAK, tokens.next().value()));
401400
}
402401
} else {
403402
output.add(tokens.next());
@@ -422,18 +421,17 @@ private static ImmutableList<Token> deindentPreCodeBlocks(List<Token> input) {
422421
// TODO: b/323389829 - De-indent {@snippet ...} blocks, too.
423422
ImmutableList.Builder<Token> output = ImmutableList.builder();
424423
for (PeekingIterator<Token> tokens = peekingIterator(input.iterator()); tokens.hasNext(); ) {
425-
if (tokens.peek().getType() != PRE_OPEN_TAG) {
424+
if (tokens.peek().type() != PRE_OPEN_TAG) {
426425
output.add(tokens.next());
427426
continue;
428427
}
429428

430429
output.add(tokens.next());
431430
List<Token> initialNewlines = new ArrayList<>();
432-
while (tokens.hasNext() && tokens.peek().getType() == FORCED_NEWLINE) {
431+
while (tokens.hasNext() && tokens.peek().type() == FORCED_NEWLINE) {
433432
initialNewlines.add(tokens.next());
434433
}
435-
if (tokens.peek().getType() != LITERAL
436-
|| !tokens.peek().getValue().matches("[ \t]*[{]@code")) {
434+
if (tokens.peek().type() != LITERAL || !tokens.peek().value().matches("[ \t]*[{]@code")) {
437435
output.addAll(initialNewlines);
438436
output.add(tokens.next());
439437
continue;
@@ -447,15 +445,15 @@ private static ImmutableList<Token> deindentPreCodeBlocks(List<Token> input) {
447445
private static void deindentPreCodeBlock(
448446
ImmutableList.Builder<Token> output, PeekingIterator<Token> tokens) {
449447
Deque<Token> saved = new ArrayDeque<>();
450-
output.add(new Token(LITERAL, tokens.next().getValue().trim()));
451-
while (tokens.hasNext() && tokens.peek().getType() != PRE_CLOSE_TAG) {
448+
output.add(new Token(LITERAL, tokens.next().value().trim()));
449+
while (tokens.hasNext() && tokens.peek().type() != PRE_CLOSE_TAG) {
452450
Token token = tokens.next();
453451
saved.addLast(token);
454452
}
455-
while (!saved.isEmpty() && saved.peekFirst().getType() == FORCED_NEWLINE) {
453+
while (!saved.isEmpty() && saved.peekFirst().type() == FORCED_NEWLINE) {
456454
saved.removeFirst();
457455
}
458-
while (!saved.isEmpty() && saved.peekLast().getType() == FORCED_NEWLINE) {
456+
while (!saved.isEmpty() && saved.peekLast().type() == FORCED_NEWLINE) {
459457
saved.removeLast();
460458
}
461459
if (saved.isEmpty()) {
@@ -465,20 +463,19 @@ private static void deindentPreCodeBlock(
465463
// move the trailing `}` to its own line
466464
Token last = saved.peekLast();
467465
boolean trailingBrace = false;
468-
if (last.getType() == LITERAL && last.getValue().endsWith("}")) {
466+
if (last.type() == LITERAL && last.value().endsWith("}")) {
469467
saved.removeLast();
470468
if (last.length() > 1) {
471-
saved.addLast(
472-
new Token(LITERAL, last.getValue().substring(0, last.getValue().length() - 1)));
469+
saved.addLast(new Token(LITERAL, last.value().substring(0, last.value().length() - 1)));
473470
saved.addLast(new Token(FORCED_NEWLINE, null));
474471
}
475472
trailingBrace = true;
476473
}
477474

478475
int trim = -1;
479476
for (Token token : saved) {
480-
if (token.getType() == LITERAL) {
481-
int idx = CharMatcher.isNot(' ').indexIn(token.getValue());
477+
if (token.type() == LITERAL) {
478+
int idx = CharMatcher.isNot(' ').indexIn(token.value());
482479
if (idx != -1 && (trim == -1 || idx < trim)) {
483480
trim = idx;
484481
}
@@ -487,13 +484,11 @@ private static void deindentPreCodeBlock(
487484

488485
output.add(new Token(FORCED_NEWLINE, "\n"));
489486
for (Token token : saved) {
490-
if (token.getType() == LITERAL) {
487+
if (token.type() == LITERAL) {
491488
output.add(
492489
new Token(
493490
LITERAL,
494-
trim > 0 && token.length() > trim
495-
? token.getValue().substring(trim)
496-
: token.getValue()));
491+
trim > 0 && token.length() > trim ? token.value().substring(trim) : token.value()));
497492
} else {
498493
output.add(token);
499494
}

core/src/main/java/com/google/googlejavaformat/java/javadoc/JavadocWriter.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -359,17 +359,17 @@ private void writeToken(Token token) {
359359
}
360360

361361
if (requestedMoeBeginStripComment != null) {
362-
output.append(requestedMoeBeginStripComment.getValue());
362+
output.append(requestedMoeBeginStripComment.value());
363363
requestedMoeBeginStripComment = null;
364364
indentForMoeEndStripComment = innerIndent();
365365
requestNewline();
366366
writeToken(token);
367367
return;
368368
}
369369

370-
output.append(token.getValue());
370+
output.append(token.value());
371371

372-
if (!START_OF_LINE_TOKENS.contains(token.getType())) {
372+
if (!START_OF_LINE_TOKENS.contains(token.type())) {
373373
atStartOfLine = false;
374374
}
375375

core/src/main/java/com/google/googlejavaformat/java/javadoc/Token.java

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
* naturally expect. The decision is usually pragmatic rather than theoretical. Most of the details
2020
* are in {@link JavadocLexer}.
2121
*/
22-
final class Token {
22+
record Token(Token.Type type, String value) {
2323
/**
2424
* Javadoc token type.
2525
*
@@ -108,28 +108,12 @@ enum Type {
108108
;
109109
}
110110

111-
private final Type type;
112-
private final String value;
113-
114-
Token(Type type, String value) {
115-
this.type = type;
116-
this.value = value;
117-
}
118-
119-
Type getType() {
120-
return type;
121-
}
122-
123-
String getValue() {
124-
return value;
125-
}
126-
127111
int length() {
128112
return value.length();
129113
}
130114

131115
@Override
132116
public String toString() {
133-
return "\n" + getType() + ": " + getValue();
117+
return "\n" + type() + ": " + value();
134118
}
135119
}

0 commit comments

Comments
 (0)