-
Notifications
You must be signed in to change notification settings - Fork 669
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Custom collection support is now centralized in ….util.CustomCollections. It exposes API to detect whether a type is a map or collection, identifies the map base type etc. Support for different collection implementations is externalized via the CustomCollectionRegistrar SPI that allows to define implementations via spring.factories. The current support for Vavr collections has been moved into an implementation of that, VavrCollections. Unit tests for custom collection handling and conversion previously living in QueryExecutionConverterUnitTests have been moved into CustomCollectionsUnitTests. Fixes #2619.
- Loading branch information
Showing
10 changed files
with
838 additions
and
430 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
src/main/java/org/springframework/data/util/CustomCollectionRegistrar.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/* | ||
* Copyright 2022 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.springframework.data.util; | ||
|
||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Set; | ||
import java.util.function.Function; | ||
|
||
import org.springframework.core.convert.converter.ConverterRegistry; | ||
|
||
/** | ||
* An SPI to register custom collection types. Implementations need to be registered via | ||
* {@code META-INF/spring.factories}. | ||
* | ||
* @author Oliver Drotbohm | ||
* @since 2.7 | ||
*/ | ||
public interface CustomCollectionRegistrar { | ||
|
||
/** | ||
* Returns whether the registrar is available, meaning whether it can be used at runtime. Primary use is for | ||
* implementations that need to perform a classpath check to prevent the actual methods loading classes that might not | ||
* be available. | ||
* | ||
* @return whether the registrar is available | ||
*/ | ||
default boolean isAvailable() { | ||
return true; | ||
} | ||
|
||
/** | ||
* Returns all types that are supposed to be considered maps. Primary requirement is key and value generics expressed | ||
* in the first and second generics parameter of the type. Also, the types should be transformable into their | ||
* Java-native equivalent using {@link #toJavaNativeCollection()}. | ||
* | ||
* @return will never be {@literal null}. | ||
* @see #toJavaNativeCollection() | ||
*/ | ||
Collection<Class<?>> getMapTypes(); | ||
|
||
/** | ||
* Returns all types that are supposed to be considered collections. Primary requirement is that their component types | ||
* are expressed as first generics parameter. Also, the types should be transformable into their Java-native | ||
* equivalent using {@link #toJavaNativeCollection()}. | ||
* | ||
* @return will never be {@literal null}. | ||
* @see #toJavaNativeCollection() | ||
*/ | ||
Collection<Class<?>> getCollectionTypes(); | ||
|
||
/** | ||
* Return all types that are considered valid return types for methods using pagination. These are usually collections | ||
* with a stable order, like {@link List} but no {@link Set}s, as pagination usually involves sorting. | ||
* | ||
* @return will never be {@literal null}. | ||
*/ | ||
default Collection<Class<?>> getAllowedPaginationReturnTypes() { | ||
return Collections.emptyList(); | ||
} | ||
|
||
/** | ||
* Register all converters to convert instances of the types returned by {@link #getCollectionTypes()} and | ||
* {@link #getMapTypes()} from an to their Java-native counterparts. | ||
* | ||
* @param registry will never be {@literal null}. | ||
*/ | ||
void registerConvertersIn(ConverterRegistry registry); | ||
|
||
/** | ||
* Returns a {@link Function} to convert instances of their Java-native counterpart. | ||
* | ||
* @return must not be {@literal null}. | ||
*/ | ||
Function<Object, Object> toJavaNativeCollection(); | ||
} |
Oops, something went wrong.