Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/main/java/org/apache/commons/lang3/CharRange.java
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,9 @@ private void readObject(final ObjectInputStream in) throws IOException, ClassNot
*/
@Override
public String toString() {
if (iToString == null) {
// Read the lazily initialized field only once: a second racy read may see null.
String result = iToString;
if (result == null) {
final StringBuilder buf = new StringBuilder(4);
if (isNegated()) {
buf.append('^');
Expand All @@ -434,8 +436,9 @@ public String toString() {
buf.append('-');
buf.append(end);
}
iToString = buf.toString();
result = buf.toString();
iToString = result;
}
return iToString;
return result;
}
}
9 changes: 6 additions & 3 deletions src/main/java/org/apache/commons/lang3/Range.java
Original file line number Diff line number Diff line change
Expand Up @@ -645,10 +645,13 @@ private void readObject(final ObjectInputStream in) throws IOException, ClassNot
*/
@Override
public String toString() {
if (toString == null) {
toString = "[" + minimum + ".." + maximum + "]";
// Read the lazily initialized field only once: a second racy read may see null.
String result = toString;
if (result == null) {
result = "[" + minimum + ".." + maximum + "]";
toString = result;
}
return toString;
return result;
}

/**
Expand Down
28 changes: 17 additions & 11 deletions src/main/java/org/apache/commons/lang3/math/Fraction.java
Original file line number Diff line number Diff line change
Expand Up @@ -922,29 +922,32 @@ public Fraction subtract(final Fraction fraction) {
* @return A {@link String} form of the fraction
*/
public String toProperString() {
if (toProperString == null) {
// Read the lazily initialized field only once: a second racy read may see null.
String result = toProperString;
if (result == null) {
if (numerator == 0) {
toProperString = "0";
result = "0";
} else if (numerator == denominator) {
toProperString = "1";
result = "1";
} else if (numerator == -1 * denominator) {
toProperString = "-1";
result = "-1";
} else if ((numerator > 0 ? -numerator : numerator) < -denominator) {
// note that we do the magnitude comparison test above with
// NEGATIVE (not positive) numbers, since negative numbers
// have a larger range. otherwise numerator == Integer.MIN_VALUE
// is handled incorrectly.
final int properNumerator = getProperNumerator();
if (properNumerator == 0) {
toProperString = Integer.toString(getProperWhole());
result = Integer.toString(getProperWhole());
} else {
toProperString = getProperWhole() + " " + properNumerator + "/" + getDenominator();
result = getProperWhole() + " " + properNumerator + "/" + getDenominator();
}
} else {
toProperString = getNumerator() + "/" + getDenominator();
result = getNumerator() + "/" + getDenominator();
}
toProperString = result;
}
return toProperString;
return result;
}

/**
Expand All @@ -957,9 +960,12 @@ public String toProperString() {
*/
@Override
public String toString() {
if (toString == null) {
toString = getNumerator() + "/" + getDenominator();
// Read the lazily initialized field only once: a second racy read may see null.
String result = toString;
if (result == null) {
result = getNumerator() + "/" + getDenominator();
toString = result;
}
return toString;
return result;
}
}