1717package com .google .common .collect ;
1818
1919import static com .google .common .base .Preconditions .checkNotNull ;
20- import static com .google .common .collect .NullnessCasts .uncheckedCastNullableTToT ;
2120import static java .util .Collections .emptyList ;
2221
2322import com .google .common .annotations .GwtCompatible ;
24- import com .google .common .base .Supplier ;
2523import java .util .ArrayList ;
2624import java .util .List ;
2725import java .util .NoSuchElementException ;
@@ -46,7 +44,7 @@ public final class MoreCollectors {
4644 */
4745 private static final Collector <Object , ?, Optional <Object >> TO_OPTIONAL =
4846 Collector .of (
49- () -> new ToOptionalState <>( null ) ,
47+ ToOptionalState :: new ,
5048 ToOptionalState ::add ,
5149 ToOptionalState ::combine ,
5250 ToOptionalState ::getOptional ,
@@ -68,8 +66,8 @@ public final class MoreCollectors {
6866 private static final Object NULL_PLACEHOLDER = new Object ();
6967
7068 private static final Collector <@ Nullable Object , ?, @ Nullable Object > ONLY_ELEMENT =
71- Collector .<@ Nullable Object , ToOptionalState < Object > , @ Nullable Object >of (
72- () -> new ToOptionalState <>( null ) ,
69+ Collector .<@ Nullable Object , ToOptionalState , @ Nullable Object >of (
70+ ToOptionalState :: new ,
7371 (state , o ) -> state .add ((o == null ) ? NULL_PLACEHOLDER : o ),
7472 ToOptionalState ::combine ,
7573 state -> {
@@ -93,23 +91,18 @@ public final class MoreCollectors {
9391 * than one, not just two.
9492 */
9593 @ SuppressWarnings ("EmptyList" ) // ImmutableList doesn't support nullable element types
96- private static final class ToOptionalState < T > {
94+ private static final class ToOptionalState {
9795 static final int MAX_EXTRAS = 4 ;
9896
99- @ Nullable T element ;
100- List <T > extras ;
101- final @ Nullable Supplier <? extends RuntimeException > exceptionSupplier ;
97+ @ Nullable Object element ;
98+ List <Object > extras ;
10299
103- ToOptionalState (@ Nullable Supplier <? extends RuntimeException > exceptionSupplier ) {
104- this .element = null ;
105- this .extras = emptyList ();
106- this .exceptionSupplier = exceptionSupplier ;
100+ ToOptionalState () {
101+ element = null ;
102+ extras = emptyList ();
107103 }
108104
109- RuntimeException multiples (boolean overflow ) {
110- if (exceptionSupplier != null ) {
111- throw exceptionSupplier .get ();
112- }
105+ IllegalArgumentException multiples (boolean overflow ) {
113106 StringBuilder sb =
114107 new StringBuilder ().append ("expected one element but was: <" ).append (element );
115108 for (Object o : extras ) {
@@ -122,7 +115,7 @@ RuntimeException multiples(boolean overflow) {
122115 throw new IllegalArgumentException (sb .toString ());
123116 }
124117
125- void add (T o ) {
118+ void add (Object o ) {
126119 checkNotNull (o );
127120 if (element == null ) {
128121 this .element = o ;
@@ -137,7 +130,7 @@ void add(T o) {
137130 }
138131 }
139132
140- ToOptionalState < T > combine (ToOptionalState < T > other ) {
133+ ToOptionalState combine (ToOptionalState other ) {
141134 if (element == null ) {
142135 return other ;
143136 } else if (other .element == null ) {
@@ -158,23 +151,19 @@ ToOptionalState<T> combine(ToOptionalState<T> other) {
158151 }
159152
160153 @ IgnoreJRERequirement // see enclosing class (whose annotation Animal Sniffer ignores here...)
161- Optional <T > getOptional () {
154+ Optional <Object > getOptional () {
162155 if (extras .isEmpty ()) {
163156 return Optional .ofNullable (element );
164157 } else {
165158 throw multiples (false );
166159 }
167160 }
168161
169- T getElement () {
162+ Object getElement () {
170163 if (element == null ) {
171- if (exceptionSupplier != null ) {
172- throw exceptionSupplier .get ();
173- } else {
174- throw new NoSuchElementException ();
175- }
164+ throw new NoSuchElementException ();
176165 } else if (extras .isEmpty ()) {
177- return uncheckedCastNullableTToT ( element ) ;
166+ return element ;
178167 } else {
179168 throw multiples (false );
180169 }
0 commit comments