1919import static java .lang .Math .max ;
2020
2121import com .google .common .base .MoreObjects ;
22+ import com .google .common .base .Suppliers ;
2223import com .google .common .collect .DiscreteDomain ;
2324import com .google .common .collect .Iterators ;
2425import com .google .common .collect .Range ;
2526import com .google .googlejavaformat .Output .BreakTag ;
2627import java .util .ArrayList ;
2728import java .util .List ;
2829import java .util .Optional ;
30+ import java .util .function .Supplier ;
2931
3032/**
3133 * {@link com.google.googlejavaformat.java.JavaInputAstVisitor JavaInputAstVisitor} outputs a
@@ -102,28 +104,21 @@ public String toString() {
102104 private static final DiscreteDomain <Integer > INTEGERS = DiscreteDomain .integers ();
103105
104106 // Memoized width; Float.POSITIVE_INFINITY if contains forced breaks.
105- private boolean widthComputed = false ;
106- private float width = 0.0F ;
107+ private final Supplier <Float > width = Suppliers .memoize (this ::computeWidth );
107108
108109 // Memoized flat; not defined (and never computed) if contains forced breaks.
109- private boolean flatComputed = false ;
110- private String flat = "" ;
110+ private final Supplier <String > flat = Suppliers .memoize (this ::computeFlat );
111111
112112 // Memoized Range.
113- private boolean rangeComputed = false ;
114- private Range <Integer > range = EMPTY_RANGE ;
113+ private final Supplier <Range <Integer >> range = Suppliers .memoize (this ::computeRange );
115114
116115 /**
117116 * Return the width of a {@code Doc}, or {@code Float.POSITIVE_INFINITY} if it must be broken.
118117 *
119118 * @return the width
120119 */
121120 final float getWidth () {
122- if (!widthComputed ) {
123- width = computeWidth ();
124- widthComputed = true ;
125- }
126- return width ;
121+ return width .get ();
127122 }
128123
129124 /**
@@ -133,11 +128,7 @@ final float getWidth() {
133128 * @return the flat-string value
134129 */
135130 final String getFlat () {
136- if (!flatComputed ) {
137- flat = computeFlat ();
138- flatComputed = true ;
139- }
140- return flat ;
131+ return flat .get ();
141132 }
142133
143134 /**
@@ -146,11 +137,7 @@ final String getFlat() {
146137 * @return the {@code Doc}'s {@link Range}
147138 */
148139 final Range <Integer > range () {
149- if (!rangeComputed ) {
150- range = computeRange ();
151- rangeComputed = true ;
152- }
153- return range ;
140+ return range .get ();
154141 }
155142
156143 /**
0 commit comments