aboutsummaryrefslogtreecommitdiff
path: root/core/src/main/java/com/google/googlejavaformat/Doc.java
diff options
context:
space:
mode:
Diffstat (limited to 'core/src/main/java/com/google/googlejavaformat/Doc.java')
-rw-r--r--core/src/main/java/com/google/googlejavaformat/Doc.java34
1 files changed, 11 insertions, 23 deletions
diff --git a/core/src/main/java/com/google/googlejavaformat/Doc.java b/core/src/main/java/com/google/googlejavaformat/Doc.java
index 35acca3..cab6885 100644
--- a/core/src/main/java/com/google/googlejavaformat/Doc.java
+++ b/core/src/main/java/com/google/googlejavaformat/Doc.java
@@ -15,9 +15,12 @@
package com.google.googlejavaformat;
import static com.google.common.collect.Iterables.getLast;
+import static com.google.googlejavaformat.CommentsHelper.reformatParameterComment;
import static java.lang.Math.max;
import com.google.common.base.MoreObjects;
+import com.google.common.base.Supplier;
+import com.google.common.base.Suppliers;
import com.google.common.collect.DiscreteDomain;
import com.google.common.collect.Iterators;
import com.google.common.collect.Range;
@@ -101,16 +104,13 @@ public abstract class Doc {
private static final DiscreteDomain<Integer> INTEGERS = DiscreteDomain.integers();
// Memoized width; Float.POSITIVE_INFINITY if contains forced breaks.
- private boolean widthComputed = false;
- private float width = 0.0F;
+ private final Supplier<Float> width = Suppliers.memoize(this::computeWidth);
// Memoized flat; not defined (and never computed) if contains forced breaks.
- private boolean flatComputed = false;
- private String flat = "";
+ private final Supplier<String> flat = Suppliers.memoize(this::computeFlat);
// Memoized Range.
- private boolean rangeComputed = false;
- private Range<Integer> range = EMPTY_RANGE;
+ private final Supplier<Range<Integer>> range = Suppliers.memoize(this::computeRange);
/**
* Return the width of a {@code Doc}, or {@code Float.POSITIVE_INFINITY} if it must be broken.
@@ -118,11 +118,7 @@ public abstract class Doc {
* @return the width
*/
final float getWidth() {
- if (!widthComputed) {
- width = computeWidth();
- widthComputed = true;
- }
- return width;
+ return width.get();
}
/**
@@ -132,11 +128,7 @@ public abstract class Doc {
* @return the flat-string value
*/
final String getFlat() {
- if (!flatComputed) {
- flat = computeFlat();
- flatComputed = true;
- }
- return flat;
+ return flat.get();
}
/**
@@ -145,11 +137,7 @@ public abstract class Doc {
* @return the {@code Doc}'s {@link Range}
*/
final Range<Integer> range() {
- if (!rangeComputed) {
- range = computeRange();
- rangeComputed = true;
- }
- return range;
+ return range.get();
}
/**
@@ -727,7 +715,7 @@ public abstract class Doc {
// Account for line comments with missing spaces, see computeFlat.
return tok.length() + 1;
} else {
- return tok.length();
+ return reformatParameterComment(tok).map(String::length).orElse(tok.length());
}
}
return idx != -1 ? Float.POSITIVE_INFINITY : (float) tok.length();
@@ -741,7 +729,7 @@ public abstract class Doc {
if (tok.isSlashSlashComment() && !tok.getOriginalText().startsWith("// ")) {
return "// " + tok.getOriginalText().substring("//".length());
}
- return tok.getOriginalText();
+ return reformatParameterComment(tok).orElse(tok.getOriginalText());
}
@Override