From 4fbb0c5eedf5658c5f3fac574b8bb0d3b3664621 Mon Sep 17 00:00:00 2001 From: Protobuf Team Bot Date: Wed, 25 Sep 2024 10:29:52 -0700 Subject: [PATCH] Return constant Value objects for true, false, and "" These instances are all identical. PiperOrigin-RevId: 678755099 --- .../src/main/java/com/google/protobuf/util/Values.java | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/java/util/src/main/java/com/google/protobuf/util/Values.java b/java/util/src/main/java/com/google/protobuf/util/Values.java index f5babbe2b44e8..b5a115f30dfc4 100644 --- a/java/util/src/main/java/com/google/protobuf/util/Values.java +++ b/java/util/src/main/java/com/google/protobuf/util/Values.java @@ -17,6 +17,9 @@ 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; @@ -24,7 +27,7 @@ public static Value ofNull() { /** 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. */ @@ -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. */