Skip to content

Commit 4fbb0c5

Browse files
Return constant Value objects for true, false, and ""
These instances are all identical. PiperOrigin-RevId: 678755099
1 parent 3b62052 commit 4fbb0c5

File tree

1 file changed

+5
-2
lines changed
  • java/util/src/main/java/com/google/protobuf/util

1 file changed

+5
-2
lines changed

java/util/src/main/java/com/google/protobuf/util/Values.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,17 @@ public final class Values {
1717

1818
private static final Value NULL_VALUE =
1919
Value.newBuilder().setNullValue(NullValue.NULL_VALUE).build();
20+
private static final Value TRUE_VALUE = Value.newBuilder().setBoolValue(true).build();
21+
private static final Value FALSE_VALUE = Value.newBuilder().setBoolValue(false).build();
22+
private static final Value EMPTY_STR_VALUE = Value.newBuilder().setStringValue("").build();
2023

2124
public static Value ofNull() {
2225
return NULL_VALUE;
2326
}
2427

2528
/** Returns a Value object with number set to value. */
2629
public static Value of(boolean value) {
27-
return Value.newBuilder().setBoolValue(value).build();
30+
return value ? TRUE_VALUE : FALSE_VALUE;
2831
}
2932

3033
/** Returns a Value object with number set to value. */
@@ -34,7 +37,7 @@ public static Value of(double value) {
3437

3538
/** Returns a Value object with string set to value. */
3639
public static Value of(String value) {
37-
return Value.newBuilder().setStringValue(value).build();
40+
return value.isEmpty() ? EMPTY_STR_VALUE : Value.newBuilder().setStringValue(value).build();
3841
}
3942

4043
/** Returns a Value object with struct set to value. */

0 commit comments

Comments
 (0)