|
41 | 41 | import java.util.NoSuchElementException; |
42 | 42 | import java.util.Objects; |
43 | 43 | import java.util.Optional; |
| 44 | +import java.util.OptionalDouble; |
| 45 | +import java.util.OptionalInt; |
| 46 | +import java.util.OptionalLong; |
44 | 47 | import java.util.Set; |
45 | 48 | import javax.lang.model.SourceVersion; |
46 | 49 | import org.junit.Test; |
@@ -779,4 +782,135 @@ public void builderTypeVariableWithNullableBound() { |
779 | 782 | NullPointerException.class, |
780 | 783 | () -> FrobCaller.<@Nullable String, String>caller().arg(null).notNull(null).call()); |
781 | 784 | } |
| 785 | + |
| 786 | + static class NullablesExposedAsOptionals { |
| 787 | + private final @Nullable String aString1; |
| 788 | + private final @Nullable String aString2; |
| 789 | + private final @Nullable Double aDouble; |
| 790 | + private final @Nullable Integer anInt; |
| 791 | + private final @Nullable Long aLong; |
| 792 | + |
| 793 | + NullablesExposedAsOptionals( |
| 794 | + @Nullable String aString1, |
| 795 | + @Nullable String aString2, |
| 796 | + @Nullable Double aDouble, |
| 797 | + @Nullable Integer anInt, |
| 798 | + @Nullable Long aLong) { |
| 799 | + this.aString1 = aString1; |
| 800 | + this.aString2 = aString2; |
| 801 | + this.aDouble = aDouble; |
| 802 | + this.anInt = anInt; |
| 803 | + this.aLong = aLong; |
| 804 | + } |
| 805 | + |
| 806 | + Optional<String> aString1() { |
| 807 | + return Optional.ofNullable(aString1); |
| 808 | + } |
| 809 | + |
| 810 | + com.google.common.base.Optional<String> aString2() { |
| 811 | + return com.google.common.base.Optional.fromNullable(aString2); |
| 812 | + } |
| 813 | + |
| 814 | + OptionalDouble aDouble() { |
| 815 | + return aDouble == null ? OptionalDouble.empty() : OptionalDouble.of(aDouble); |
| 816 | + } |
| 817 | + |
| 818 | + OptionalInt anInt() { |
| 819 | + return anInt == null ? OptionalInt.empty() : OptionalInt.of(anInt); |
| 820 | + } |
| 821 | + |
| 822 | + OptionalLong aLong() { |
| 823 | + return aLong == null ? OptionalLong.empty() : OptionalLong.of(aLong); |
| 824 | + } |
| 825 | + |
| 826 | + Builder toBuilder() { |
| 827 | + return new AutoBuilder_AutoBuilderTest_NullablesExposedAsOptionals_Builder(this); |
| 828 | + } |
| 829 | + |
| 830 | + static Builder builder() { |
| 831 | + return new AutoBuilder_AutoBuilderTest_NullablesExposedAsOptionals_Builder(); |
| 832 | + } |
| 833 | + |
| 834 | + @AutoBuilder |
| 835 | + abstract static class Builder { |
| 836 | + abstract Builder setAString1(Optional<String> aString1); |
| 837 | + |
| 838 | + abstract Builder setAString2(com.google.common.base.Optional<String> aString2); |
| 839 | + |
| 840 | + abstract Builder setADouble(OptionalDouble aDouble); |
| 841 | + |
| 842 | + abstract Builder setAnInt(OptionalInt anInt); |
| 843 | + |
| 844 | + abstract Builder setALong(OptionalLong aLong); |
| 845 | + |
| 846 | + abstract NullablesExposedAsOptionals build(); |
| 847 | + } |
| 848 | + } |
| 849 | + |
| 850 | + @Test |
| 851 | + public void builderExposingNullableAsOptionalString() { |
| 852 | + NullablesExposedAsOptionals built = |
| 853 | + NullablesExposedAsOptionals.builder().setAString1(Optional.of("foo")).build(); |
| 854 | + assertThat(built.aString1()).hasValue("foo"); |
| 855 | + assertThat(built.toBuilder().build().aString1()).hasValue("foo"); |
| 856 | + |
| 857 | + NullablesExposedAsOptionals builtEmpty = |
| 858 | + built.toBuilder().setAString1(Optional.empty()).build(); |
| 859 | + assertThat(builtEmpty.aString1()).isEmpty(); |
| 860 | + assertThat(builtEmpty.toBuilder().build().aString1()).isEmpty(); |
| 861 | + } |
| 862 | + |
| 863 | + @Test |
| 864 | + public void builderExposingNullableAsBaseOptionalString() { |
| 865 | + NullablesExposedAsOptionals built = |
| 866 | + NullablesExposedAsOptionals.builder() |
| 867 | + .setAString2(com.google.common.base.Optional.of("foo")) |
| 868 | + .build(); |
| 869 | + assertThat(built.aString2()).hasValue("foo"); |
| 870 | + assertThat(built.toBuilder().build().aString2()).hasValue("foo"); |
| 871 | + |
| 872 | + NullablesExposedAsOptionals builtAbsent = |
| 873 | + built.toBuilder().setAString2(com.google.common.base.Optional.absent()).build(); |
| 874 | + assertThat(builtAbsent.aString2()).isAbsent(); |
| 875 | + assertThat(builtAbsent.toBuilder().build().aString2()).isAbsent(); |
| 876 | + } |
| 877 | + |
| 878 | + @Test |
| 879 | + public void builderExposingNullableAsOptionalDouble() { |
| 880 | + NullablesExposedAsOptionals built = |
| 881 | + NullablesExposedAsOptionals.builder().setADouble(OptionalDouble.of(3.14)).build(); |
| 882 | + assertThat(built.aDouble()).hasValue(3.14); |
| 883 | + assertThat(built.toBuilder().build().aDouble()).hasValue(3.14); |
| 884 | + |
| 885 | + NullablesExposedAsOptionals builtEmpty = |
| 886 | + built.toBuilder().setADouble(OptionalDouble.empty()).build(); |
| 887 | + assertThat(builtEmpty.aDouble()).isEmpty(); |
| 888 | + assertThat(builtEmpty.toBuilder().build().aDouble()).isEmpty(); |
| 889 | + } |
| 890 | + |
| 891 | + @Test |
| 892 | + public void builderExposingNullableAsOptionalInt() { |
| 893 | + NullablesExposedAsOptionals built = |
| 894 | + NullablesExposedAsOptionals.builder().setAnInt(OptionalInt.of(3)).build(); |
| 895 | + assertThat(built.anInt()).hasValue(3); |
| 896 | + assertThat(built.toBuilder().build().anInt()).hasValue(3); |
| 897 | + |
| 898 | + NullablesExposedAsOptionals builtEmpty = |
| 899 | + built.toBuilder().setAnInt(OptionalInt.empty()).build(); |
| 900 | + assertThat(builtEmpty.anInt()).isEmpty(); |
| 901 | + assertThat(builtEmpty.toBuilder().build().anInt()).isEmpty(); |
| 902 | + } |
| 903 | + |
| 904 | + @Test |
| 905 | + public void builderExposingNullableAsOptionalLong() { |
| 906 | + NullablesExposedAsOptionals built = |
| 907 | + NullablesExposedAsOptionals.builder().setALong(OptionalLong.of(3)).build(); |
| 908 | + assertThat(built.aLong()).hasValue(3); |
| 909 | + assertThat(built.toBuilder().build().aLong()).hasValue(3); |
| 910 | + |
| 911 | + NullablesExposedAsOptionals builtEmpty = |
| 912 | + built.toBuilder().setALong(OptionalLong.empty()).build(); |
| 913 | + assertThat(builtEmpty.aLong()).isEmpty(); |
| 914 | + assertThat(builtEmpty.toBuilder().build().aLong()).isEmpty(); |
| 915 | + } |
782 | 916 | } |
0 commit comments