Skip to content

Commit

Permalink
Return constant Value objects for true, false, and ""
Browse files Browse the repository at this point in the history
These instances are all identical.

PiperOrigin-RevId: 678755099
  • Loading branch information
protobuf-github-bot authored and copybara-github committed Sep 25, 2024
1 parent 3b62052 commit 4fbb0c5
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions java/util/src/main/java/com/google/protobuf/util/Values.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,17 @@ public final class Values {

private static final Value NULL_VALUE =
Value.newBuilder().setNullValue(NullValue.NULL_VALUE).build();
private static final Value TRUE_VALUE = Value.newBuilder().setBoolValue(true).build();
private static final Value FALSE_VALUE = Value.newBuilder().setBoolValue(false).build();
private static final Value EMPTY_STR_VALUE = Value.newBuilder().setStringValue("").build();

public static Value ofNull() {
return NULL_VALUE;
}

/** Returns a Value object with number set to value. */
public static Value of(boolean value) {
return Value.newBuilder().setBoolValue(value).build();
return value ? TRUE_VALUE : FALSE_VALUE;
}

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

/** Returns a Value object with string set to value. */
public static Value of(String value) {
return Value.newBuilder().setStringValue(value).build();
return value.isEmpty() ? EMPTY_STR_VALUE : Value.newBuilder().setStringValue(value).build();
}

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

0 comments on commit 4fbb0c5

Please sign in to comment.