Make google-java-format friendlier to TSAN · google/google-java-format@9f2cd68

@@ -19,13 +19,15 @@

1919

import static java.lang.Math.max;

20202121

import com.google.common.base.MoreObjects;

22+

import com.google.common.base.Suppliers;

2223

import com.google.common.collect.DiscreteDomain;

2324

import com.google.common.collect.Iterators;

2425

import com.google.common.collect.Range;

2526

import com.google.googlejavaformat.Output.BreakTag;

2627

import java.util.ArrayList;

2728

import java.util.List;

2829

import java.util.Optional;

30+

import java.util.function.Supplier;

29313032

/**

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();

103105104106

// 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);

107108108109

// 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);

111111112112

// 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);

115114116115

/**

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

}

128123129124

/**

@@ -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

}

142133143134

/**

@@ -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

}

155142156143

/**