Skip to content

Commit 9f89c8c

Browse files
committed
DATACMNS-836 - Refactoring.
Move ReactiveWrappers and ReactiveWrapperConverters to util package. Provide reactive type details in ReactiveWrappers.
1 parent 6fb8260 commit 9f89c8c

File tree

7 files changed

+344
-240
lines changed

7 files changed

+344
-240
lines changed

src/main/java/org/springframework/data/repository/query/ReactiveWrappers.java

Lines changed: 0 additions & 119 deletions
This file was deleted.

src/main/java/org/springframework/data/repository/query/ResultProcessor.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import org.springframework.core.convert.support.DefaultConversionService;
3333
import org.springframework.data.domain.Slice;
3434
import org.springframework.data.projection.ProjectionFactory;
35+
import org.springframework.data.repository.util.ReactiveWrapperConverters;
3536
import org.springframework.data.util.ReflectionUtils;
3637
import org.springframework.util.Assert;
3738

src/main/java/org/springframework/data/repository/util/QueryExecutionConverters.java

Lines changed: 20 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -57,17 +57,13 @@
5757
* <li>{@code java.util.concurrent.Future}</li>
5858
* <li>{@code java.util.concurrent.CompletableFuture}</li>
5959
* <li>{@code org.springframework.util.concurrent.ListenableFuture<}</li>
60-
* <li>{@code rx.Single}</li>
61-
* <li>{@code rx.Observable}</li>
62-
* <li>{@code rx.Completable}</li>
63-
* <li>{@code reactor.core.publisher.Mono}</li>
64-
* <li>{@code reactor.core.publisher.Flux}</li>
65-
* <li>{@code org.reactivestreams.Publisher}</li>
60+
* <li>Reactive wrappers supported by {@link ReactiveWrappers}</li>
6661
* </ul>
6762
*
6863
* @author Oliver Gierke
6964
* @author Mark Paluch
7065
* @since 1.8
66+
* @see ReactiveWrappers
7167
*/
7268
public abstract class QueryExecutionConverters {
7369

@@ -85,13 +81,6 @@ public abstract class QueryExecutionConverters {
8581
private static final boolean SCALA_PRESENT = ClassUtils.isPresent("scala.Option",
8682
QueryExecutionConverters.class.getClassLoader());
8783

88-
private static final boolean PROJECT_REACTOR_PRESENT = ClassUtils.isPresent("reactor.core.publisher.Mono",
89-
QueryExecutionConverters.class.getClassLoader());
90-
private static final boolean RXJAVA1_PRESENT = ClassUtils.isPresent("rx.Completable",
91-
QueryExecutionConverters.class.getClassLoader());
92-
private static final boolean RXJAVA2_PRESENT = ClassUtils.isPresent("io.reactivex.Flowable",
93-
QueryExecutionConverters.class.getClassLoader());
94-
9584
private static final Set<Class<?>> WRAPPER_TYPES = new HashSet<Class<?>>();
9685
private static final Set<Class<?>> UNWRAPPER_TYPES = new HashSet<Class<?>>();
9786
private static final Set<Converter<Object, Object>> UNWRAPPERS = new HashSet<Converter<Object, Object>>();
@@ -127,25 +116,9 @@ public abstract class QueryExecutionConverters {
127116
UNWRAPPERS.add(ScalOptionUnwrapper.INSTANCE);
128117
}
129118

130-
if (PROJECT_REACTOR_PRESENT) {
131-
WRAPPER_TYPES.add(Publisher.class);
132-
WRAPPER_TYPES.add(Mono.class);
133-
WRAPPER_TYPES.add(Flux.class);
134-
}
135-
136-
if (RXJAVA1_PRESENT) {
137-
WRAPPER_TYPES.add(Single.class);
138-
WRAPPER_TYPES.add(Completable.class);
139-
WRAPPER_TYPES.add(Observable.class);
140-
}
141-
142-
if (RXJAVA2_PRESENT) {
143-
WRAPPER_TYPES.add(io.reactivex.Single.class);
144-
WRAPPER_TYPES.add(io.reactivex.Maybe.class);
145-
WRAPPER_TYPES.add(io.reactivex.Completable.class);
146-
WRAPPER_TYPES.add(io.reactivex.Observable.class);
147-
WRAPPER_TYPES.add(io.reactivex.Flowable.class);
148-
}
119+
WRAPPER_TYPES.addAll(ReactiveWrappers.getNoValueTypes());
120+
WRAPPER_TYPES.addAll(ReactiveWrappers.getSingleValueTypes());
121+
WRAPPER_TYPES.addAll(ReactiveWrappers.getMultiValueTypes());
149122
}
150123

151124
private QueryExecutionConverters() {}
@@ -214,9 +187,9 @@ public static void registerConvertersIn(ConfigurableConversionService conversion
214187
conversionService.addConverter(new NullableWrapperToFutureConverter(conversionService));
215188
}
216189

217-
if (PROJECT_REACTOR_PRESENT) {
190+
if (ReactiveWrappers.isAvailable()) {
218191

219-
if (RXJAVA1_PRESENT) {
192+
if (ReactiveWrappers.RXJAVA1_PRESENT) {
220193

221194
conversionService.addConverter(PublisherToRxJava1CompletableConverter.INSTANCE);
222195
conversionService.addConverter(RxJava1CompletableToPublisherConverter.INSTANCE);
@@ -233,7 +206,7 @@ public static void registerConvertersIn(ConfigurableConversionService conversion
233206
conversionService.addConverter(RxJava1ObservableToFluxConverter.INSTANCE);
234207
}
235208

236-
if (RXJAVA2_PRESENT) {
209+
if (ReactiveWrappers.RXJAVA2_PRESENT) {
237210

238211
conversionService.addConverter(PublisherToRxJava2CompletableConverter.INSTANCE);
239212
conversionService.addConverter(RxJava2CompletableToPublisherConverter.INSTANCE);
@@ -258,18 +231,20 @@ public static void registerConvertersIn(ConfigurableConversionService conversion
258231
conversionService.addConverter(RxJava2MaybeToFluxConverter.INSTANCE);
259232
}
260233

261-
conversionService.addConverter(PublisherToMonoConverter.INSTANCE);
262-
conversionService.addConverter(PublisherToFluxConverter.INSTANCE);
263-
}
234+
if (ReactiveWrappers.PROJECT_REACTOR_PRESENT) {
235+
conversionService.addConverter(PublisherToMonoConverter.INSTANCE);
236+
conversionService.addConverter(PublisherToFluxConverter.INSTANCE);
237+
}
264238

265-
if (RXJAVA1_PRESENT) {
266-
conversionService.addConverter(RxJava1SingleToObservableConverter.INSTANCE);
267-
conversionService.addConverter(RxJava1ObservableToSingleConverter.INSTANCE);
268-
}
239+
if (ReactiveWrappers.RXJAVA1_PRESENT) {
240+
conversionService.addConverter(RxJava1SingleToObservableConverter.INSTANCE);
241+
conversionService.addConverter(RxJava1ObservableToSingleConverter.INSTANCE);
242+
}
269243

270-
if (RXJAVA2_PRESENT) {
271-
conversionService.addConverter(RxJava2SingleToObservableConverter.INSTANCE);
272-
conversionService.addConverter(RxJava2ObservableToSingleConverter.INSTANCE);
244+
if (ReactiveWrappers.RXJAVA2_PRESENT) {
245+
conversionService.addConverter(RxJava2SingleToObservableConverter.INSTANCE);
246+
conversionService.addConverter(RxJava2ObservableToSingleConverter.INSTANCE);
247+
}
273248
}
274249
}
275250

src/main/java/org/springframework/data/repository/query/ReactiveWrapperConverters.java renamed to src/main/java/org/springframework/data/repository/util/ReactiveWrapperConverters.java

Lines changed: 21 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -13,56 +13,61 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package org.springframework.data.repository.query;
17-
18-
import static org.springframework.data.repository.query.ReactiveWrappers.*;
16+
package org.springframework.data.repository.util;
1917

2018
import java.util.ArrayList;
2119
import java.util.List;
22-
import java.util.Optional;
23-
import java.util.function.Predicate;
2420

2521
import org.reactivestreams.Publisher;
2622
import org.springframework.core.ReactiveAdapterRegistry;
2723
import org.springframework.core.convert.converter.Converter;
2824
import org.springframework.core.convert.support.GenericConversionService;
29-
import org.springframework.data.repository.util.QueryExecutionConverters;
3025
import org.springframework.util.Assert;
3126
import org.springframework.util.ClassUtils;
3227

3328
import io.reactivex.Flowable;
29+
import lombok.experimental.UtilityClass;
3430
import reactor.core.publisher.Flux;
3531
import reactor.core.publisher.Mono;
3632
import rx.Observable;
3733
import rx.Single;
3834

3935
/**
40-
* Conversion support for reactive wrapper types.
36+
* Conversion support for reactive wrapper types. This class is a logical extension to {@link QueryExecutionConverters}.
37+
* <p>
38+
* This class discovers reactive wrapper availability and their conversion support based on the class path. Reactive
39+
* wrapper types might be supported/on the class path but conversion may require additional dependencies.
4140
*
4241
* @author Mark Paluch
4342
* @since 2.0
43+
* @see ReactiveWrappers
44+
* @see ReactiveAdapterRegistry
4445
*/
45-
public abstract class ReactiveWrapperConverters {
46+
@UtilityClass
47+
public class ReactiveWrapperConverters {
4648

4749
private static final List<AbstractReactiveWrapper<?>> REACTIVE_WRAPPERS = new ArrayList<>();
4850
private static final GenericConversionService GENERIC_CONVERSION_SERVICE = new GenericConversionService();
4951
private static final ReactiveAdapterRegistry REACTIVE_ADAPTER_REGISTRY = new ReactiveAdapterRegistry();
5052

5153
static {
5254

53-
if (RXJAVA1_PRESENT) {
55+
if (ReactiveWrappers.RXJAVA1_PRESENT) {
56+
5457
REACTIVE_WRAPPERS.add(RxJava1SingleWrapper.INSTANCE);
5558
REACTIVE_WRAPPERS.add(RxJava1ObservableWrapper.INSTANCE);
5659
}
5760

58-
if (RXJAVA2_PRESENT) {
61+
if (ReactiveWrappers.RXJAVA2_PRESENT) {
62+
5963
REACTIVE_WRAPPERS.add(RxJava2SingleWrapper.INSTANCE);
6064
REACTIVE_WRAPPERS.add(RxJava2MaybeWrapper.INSTANCE);
6165
REACTIVE_WRAPPERS.add(RxJava2ObservableWrapper.INSTANCE);
6266
REACTIVE_WRAPPERS.add(RxJava2FlowableWrapper.INSTANCE);
6367
}
6468

65-
if (PROJECT_REACTOR_PRESENT) {
69+
if (ReactiveWrappers.PROJECT_REACTOR_PRESENT) {
70+
6671
REACTIVE_WRAPPERS.add(FluxWrapper.INSTANCE);
6772
REACTIVE_WRAPPERS.add(MonoWrapper.INSTANCE);
6873
REACTIVE_WRAPPERS.add(PublisherWrapper.INSTANCE);
@@ -71,11 +76,12 @@ public abstract class ReactiveWrapperConverters {
7176
QueryExecutionConverters.registerConvertersIn(GENERIC_CONVERSION_SERVICE);
7277
}
7378

74-
private ReactiveWrapperConverters() {
75-
}
76-
7779
/**
78-
* Returns whether the given type is a supported wrapper type.
80+
* Returns whether the given type is supported for wrapper type conversion.
81+
* <p>
82+
* NOTE: A reactive wrapper type might be supported in general by {@link ReactiveWrappers#supports(Class)} but not
83+
* necessarily for conversion using this method.
84+
* </p>
7985
*
8086
* @param type must not be {@literal null}.
8187
* @return {@literal true} if the {@code type} is a supported reactive wrapper type.
@@ -84,34 +90,6 @@ public static boolean supports(Class<?> type) {
8490
return REACTIVE_ADAPTER_REGISTRY.getAdapterFrom(type) != null;
8591
}
8692

87-
/**
88-
* Returns whether the type is a single-like wrapper.
89-
*
90-
* @param type must not be {@literal null}.
91-
* @return {@literal true} if the {@code type} is a single-like reactive wrapper type.
92-
* @see rx.Single
93-
* @see io.reactivex.Single
94-
* @see Mono
95-
*/
96-
public static boolean isSingleLike(Class<?> type) {
97-
return ReactiveWrappers.isSingleType(type);
98-
}
99-
100-
/**
101-
* Returns whether the type is a collection/multi-element-like wrapper.
102-
*
103-
* @param type must not be {@literal null}.
104-
* @return {@literal true} if the {@code type} is a collection/multi-element-like reactive wrapper type.
105-
* @see rx.Observable
106-
* @see io.reactivex.Observable
107-
* @see io.reactivex.Flowable
108-
* @see Flux
109-
* @see Publisher
110-
*/
111-
public static boolean isCollectionLike(Class<?> type) {
112-
return ReactiveWrappers.isMultiType(type);
113-
}
114-
11593
/**
11694
* Casts or converts the given wrapper type into a different wrapper type.
11795
*

0 commit comments

Comments
 (0)