Skip to content

Commit f5ec2ab

Browse files
klueverGoogle Java Core Libraries
authored and
Google Java Core Libraries
committed
add ToStringHelper.omitEmptyValues(). Fixes #7415
RELNOTES=Add `ToStringHelper.omitEmptyValues()`. PiperOrigin-RevId: 684168908
1 parent 99d2ced commit f5ec2ab

File tree

4 files changed

+361
-0
lines changed

4 files changed

+361
-0
lines changed

android/guava-tests/test/com/google/common/base/ToStringHelperTest.java

+157
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,12 @@
2020

2121
import com.google.common.annotations.GwtCompatible;
2222
import com.google.common.annotations.GwtIncompatible;
23+
import com.google.common.annotations.J2ktIncompatible;
2324
import com.google.common.collect.ImmutableMap;
25+
import java.nio.CharBuffer;
26+
import java.util.ArrayList;
2427
import java.util.Arrays;
28+
import java.util.HashMap;
2529
import java.util.Map;
2630
import junit.framework.TestCase;
2731

@@ -316,6 +320,13 @@ public void testToStringOmitNullValues_oneField() {
316320
assertEquals("TestClass{}", toTest);
317321
}
318322

323+
@GwtIncompatible // Class names are obfuscated in GWT
324+
public void testToStringOmitEmptyValues_oneField() {
325+
String toTest =
326+
MoreObjects.toStringHelper(new TestClass()).omitEmptyValues().add("field1", "").toString();
327+
assertEquals("TestClass{}", toTest);
328+
}
329+
319330
@GwtIncompatible // Class names are obfuscated in GWT
320331
public void testToStringOmitNullValues_manyFieldsFirstNull() {
321332
String toTest =
@@ -328,6 +339,18 @@ public void testToStringOmitNullValues_manyFieldsFirstNull() {
328339
assertEquals("TestClass{field2=Googley, field3=World}", toTest);
329340
}
330341

342+
@GwtIncompatible // Class names are obfuscated in GWT
343+
public void testToStringOmitEmptyValues_manyFieldsFirstEmpty() {
344+
String toTest =
345+
MoreObjects.toStringHelper(new TestClass())
346+
.omitEmptyValues()
347+
.add("field1", "")
348+
.add("field2", "Googley")
349+
.add("field3", "World")
350+
.toString();
351+
assertEquals("TestClass{field2=Googley, field3=World}", toTest);
352+
}
353+
331354
@GwtIncompatible // Class names are obfuscated in GWT
332355
public void testToStringOmitNullValues_manyFieldsOmitAfterNull() {
333356
String toTest =
@@ -340,6 +363,18 @@ public void testToStringOmitNullValues_manyFieldsOmitAfterNull() {
340363
assertEquals("TestClass{field2=Googley, field3=World}", toTest);
341364
}
342365

366+
@GwtIncompatible // Class names are obfuscated in GWT
367+
public void testToStringOmitEmptyValues_manyFieldsOmitAfterEmpty() {
368+
String toTest =
369+
MoreObjects.toStringHelper(new TestClass())
370+
.add("field1", "")
371+
.add("field2", "Googley")
372+
.add("field3", "World")
373+
.omitEmptyValues()
374+
.toString();
375+
assertEquals("TestClass{field2=Googley, field3=World}", toTest);
376+
}
377+
343378
@GwtIncompatible // Class names are obfuscated in GWT
344379
public void testToStringOmitNullValues_manyFieldsLastNull() {
345380
String toTest =
@@ -352,6 +387,25 @@ public void testToStringOmitNullValues_manyFieldsLastNull() {
352387
assertEquals("TestClass{field1=Hello, field2=Googley}", toTest);
353388
}
354389

390+
@GwtIncompatible // Class names are obfuscated in GWT
391+
public void testToStringOmitEmptyValues_manyFieldsLastEmpty() {
392+
String toTest =
393+
MoreObjects.toStringHelper(new TestClass())
394+
.omitEmptyValues()
395+
.add("field1", "Hello")
396+
.add("field2", "Googley")
397+
.add("field3", "")
398+
.toString();
399+
assertEquals("TestClass{field1=Hello, field2=Googley}", toTest);
400+
}
401+
402+
@GwtIncompatible // Class names are obfuscated in GWT
403+
public void testToStringOmitNullValues_oneValue() {
404+
String toTest =
405+
MoreObjects.toStringHelper(new TestClass()).omitEmptyValues().addValue("").toString();
406+
assertEquals("TestClass{}", toTest);
407+
}
408+
355409
@GwtIncompatible // Class names are obfuscated in GWT
356410
public void testToStringOmitEmptyValues_oneValue() {
357411
String toTest =
@@ -371,6 +425,18 @@ public void testToStringOmitNullValues_manyValuesFirstNull() {
371425
assertEquals("TestClass{Googley, World}", toTest);
372426
}
373427

428+
@GwtIncompatible // Class names are obfuscated in GWT
429+
public void testToStringOmitEmptyValues_manyValuesFirstEmpty() {
430+
String toTest =
431+
MoreObjects.toStringHelper(new TestClass())
432+
.omitEmptyValues()
433+
.addValue("")
434+
.addValue("Googley")
435+
.addValue("World")
436+
.toString();
437+
assertEquals("TestClass{Googley, World}", toTest);
438+
}
439+
374440
@GwtIncompatible // Class names are obfuscated in GWT
375441
public void testToStringOmitNullValues_manyValuesLastNull() {
376442
String toTest =
@@ -383,6 +449,18 @@ public void testToStringOmitNullValues_manyValuesLastNull() {
383449
assertEquals("TestClass{Hello, Googley}", toTest);
384450
}
385451

452+
@GwtIncompatible // Class names are obfuscated in GWT
453+
public void testToStringOmitEmptyValues_manyValuesLastEmpty() {
454+
String toTest =
455+
MoreObjects.toStringHelper(new TestClass())
456+
.omitEmptyValues()
457+
.addValue("Hello")
458+
.addValue("Googley")
459+
.addValue("")
460+
.toString();
461+
assertEquals("TestClass{Hello, Googley}", toTest);
462+
}
463+
386464
@GwtIncompatible // Class names are obfuscated in GWT
387465
public void testToStringOmitNullValues_differentOrder() {
388466
String expected = "TestClass{field1=Hello, field2=Googley, field3=World}";
@@ -404,6 +482,27 @@ public void testToStringOmitNullValues_differentOrder() {
404482
assertEquals(expected, toTest2);
405483
}
406484

485+
@GwtIncompatible // Class names are obfuscated in GWT
486+
public void testToStringOmitEmptyValues_differentOrder() {
487+
String expected = "TestClass{field1=Hello, field2=Googley, field3=World}";
488+
String toTest1 =
489+
MoreObjects.toStringHelper(new TestClass())
490+
.omitEmptyValues()
491+
.add("field1", "Hello")
492+
.add("field2", "Googley")
493+
.add("field3", "World")
494+
.toString();
495+
String toTest2 =
496+
MoreObjects.toStringHelper(new TestClass())
497+
.add("field1", "Hello")
498+
.add("field2", "Googley")
499+
.omitEmptyValues()
500+
.add("field3", "World")
501+
.toString();
502+
assertEquals(expected, toTest1);
503+
assertEquals(expected, toTest2);
504+
}
505+
407506
@GwtIncompatible // Class names are obfuscated in GWT
408507
public void testToStringOmitNullValues_canBeCalledManyTimes() {
409508
String toTest =
@@ -419,6 +518,64 @@ public void testToStringOmitNullValues_canBeCalledManyTimes() {
419518
assertEquals("TestClass{field1=Hello, field2=Googley, field3=World}", toTest);
420519
}
421520

521+
@GwtIncompatible // Class names are obfuscated in GWT
522+
public void testToStringOmitEmptyValues_canBeCalledManyTimes() {
523+
String toTest =
524+
MoreObjects.toStringHelper(new TestClass())
525+
.omitEmptyValues()
526+
.omitEmptyValues()
527+
.add("field1", "Hello")
528+
.omitEmptyValues()
529+
.add("field2", "Googley")
530+
.omitEmptyValues()
531+
.add("field3", "World")
532+
.toString();
533+
assertEquals("TestClass{field1=Hello, field2=Googley, field3=World}", toTest);
534+
}
535+
536+
@GwtIncompatible // Class names are obfuscated in GWT
537+
public void testToStringOmitEmptyValues_allEmptyTypes() {
538+
String toTest =
539+
MoreObjects.toStringHelper(new TestClass())
540+
.omitEmptyValues()
541+
// CharSequences
542+
.add("field1", "")
543+
.add("field2", new StringBuilder())
544+
// nio CharBuffer (implements CharSequence) is tested separately below
545+
// Collections and Maps
546+
.add("field11", Arrays.asList("Hello"))
547+
.add("field12", new ArrayList<>())
548+
.add("field13", new HashMap<>())
549+
// Optionals
550+
.add("field26", Optional.of("World"))
551+
.add("field27", Optional.absent())
552+
// Arrays
553+
.add("field31", new Object[] {"!!!"})
554+
.add("field32", new boolean[0])
555+
.add("field33", new byte[0])
556+
.add("field34", new char[0])
557+
.add("field35", new short[0])
558+
.add("field36", new int[0])
559+
.add("field37", new long[0])
560+
.add("field38", new float[0])
561+
.add("field39", new double[0])
562+
.add("field40", new Object[0])
563+
.toString();
564+
assertEquals("TestClass{field11=[Hello], field26=Optional.of(World), field31=[!!!]}", toTest);
565+
}
566+
567+
@J2ktIncompatible // J2kt CharBuffer does not implement CharSequence so not recognized as empty
568+
@GwtIncompatible // CharBuffer not available
569+
public void testToStringOmitEmptyValues_charBuffer() {
570+
String toTest =
571+
MoreObjects.toStringHelper(new TestClass())
572+
.omitEmptyValues()
573+
.add("field1", "Hello")
574+
.add("field2", CharBuffer.allocate(0))
575+
.toString();
576+
assertEquals("TestClass{field1=Hello}", toTest);
577+
}
578+
422579
public void testToStringHelperWithArrays() {
423580
String[] strings = {"hello", "world"};
424581
int[] ints = {2, 42};

android/guava/src/com/google/common/base/MoreObjects.java

+18
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,24 @@ public ToStringHelper omitNullValues() {
169169
return this;
170170
}
171171

172+
/**
173+
* Configures the {@link ToStringHelper} so {@link #toString()} will ignore properties with
174+
* empty values. The order of calling this method, relative to the {@code add()}/{@code
175+
* addValue()} methods, is not significant.
176+
*
177+
* <p><b>Note:</b> in general, code should assume that the string form returned by
178+
* `ToStringHelper` for a given object may change. In particular, the list of types which are
179+
* checked for emptiness is subject to change. We currently check {@code CharSequence}s, {@code
180+
* Collection}s, {@code Map}s, optionals (including Guava's), and arrays.
181+
*
182+
* @since NEXT
183+
*/
184+
@CanIgnoreReturnValue
185+
public ToStringHelper omitEmptyValues() {
186+
omitEmptyValues = true;
187+
return this;
188+
}
189+
172190
/**
173191
* Adds a name/value pair to the formatted output in {@code name=value} format. If {@code value}
174192
* is {@code null}, the string {@code "null"} is used, unless {@link #omitNullValues()} is

0 commit comments

Comments
 (0)