|
| 1 | +/* |
| 2 | + * Copyright 2021 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.springframework.data.convert; |
| 17 | + |
| 18 | +import java.util.ArrayList; |
| 19 | +import java.util.Collection; |
| 20 | +import java.util.Collections; |
| 21 | +import java.util.List; |
| 22 | +import java.util.function.Supplier; |
| 23 | + |
| 24 | +import org.jmolecules.spring.AssociationToPrimitivesConverter; |
| 25 | +import org.jmolecules.spring.IdentifierToPrimitivesConverter; |
| 26 | +import org.jmolecules.spring.PrimitivesToAssociationConverter; |
| 27 | +import org.jmolecules.spring.PrimitivesToIdentifierConverter; |
| 28 | +import org.springframework.core.convert.ConversionService; |
| 29 | +import org.springframework.core.convert.support.DefaultConversionService; |
| 30 | +import org.springframework.util.ClassUtils; |
| 31 | + |
| 32 | +/** |
| 33 | + * Registers jMolecules converter implementations with {@link CustomConversions} if the former is on the classpath. |
| 34 | + * |
| 35 | + * @author Oliver Drotbohm |
| 36 | + * @since 2.5 |
| 37 | + */ |
| 38 | +public class JMoleculesConverters { |
| 39 | + |
| 40 | + private static final boolean JMOLECULES_PRESENT = ClassUtils.isPresent( |
| 41 | + "org.jmolecules.spring.IdentifierToPrimitivesConverter", |
| 42 | + JMoleculesConverters.class.getClassLoader()); |
| 43 | + |
| 44 | + /** |
| 45 | + * Returns all jMolecules-specific converters to be registered. |
| 46 | + * |
| 47 | + * @return will never be {@literal null}. |
| 48 | + */ |
| 49 | + public static Collection<Object> getConvertersToRegister() { |
| 50 | + |
| 51 | + if (!JMOLECULES_PRESENT) { |
| 52 | + return Collections.emptyList(); |
| 53 | + } |
| 54 | + |
| 55 | + List<Object> converters = new ArrayList<>(); |
| 56 | + |
| 57 | + Supplier<ConversionService> conversionService = () -> DefaultConversionService.getSharedInstance(); |
| 58 | + |
| 59 | + IdentifierToPrimitivesConverter toPrimitives = new IdentifierToPrimitivesConverter(conversionService); |
| 60 | + PrimitivesToIdentifierConverter toIdentifier = new PrimitivesToIdentifierConverter(conversionService); |
| 61 | + |
| 62 | + converters.add(toPrimitives); |
| 63 | + converters.add(toIdentifier); |
| 64 | + converters.add(new AssociationToPrimitivesConverter<>(toPrimitives)); |
| 65 | + converters.add(new PrimitivesToAssociationConverter<>(toIdentifier)); |
| 66 | + |
| 67 | + return converters; |
| 68 | + } |
| 69 | +} |
0 commit comments