|
| 1 | +/* |
| 2 | + * Copyright 2017 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 | + * http://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 lombok.AccessLevel; |
| 19 | +import lombok.EqualsAndHashCode; |
| 20 | +import lombok.NonNull; |
| 21 | +import lombok.RequiredArgsConstructor; |
| 22 | +import lombok.experimental.Wither; |
| 23 | + |
| 24 | +import java.util.Collections; |
| 25 | +import java.util.Optional; |
| 26 | +import java.util.Set; |
| 27 | +import java.util.function.Function; |
| 28 | +import java.util.stream.Collectors; |
| 29 | + |
| 30 | +import org.springframework.core.convert.TypeDescriptor; |
| 31 | +import org.springframework.core.convert.converter.Converter; |
| 32 | +import org.springframework.core.convert.converter.GenericConverter; |
| 33 | +import org.springframework.core.convert.converter.GenericConverter.ConvertiblePair; |
| 34 | +import org.springframework.data.convert.ConverterBuilder.ConverterAware; |
| 35 | +import org.springframework.data.convert.ConverterBuilder.ReadingConverterBuilder; |
| 36 | +import org.springframework.data.convert.ConverterBuilder.WritingConverterBuilder; |
| 37 | +import org.springframework.data.util.Optionals; |
| 38 | + |
| 39 | +/** |
| 40 | + * Builder to easily set up (bi-directional) {@link Converter} instances for Spring Data type mapping using Lambdas. Use |
| 41 | + * factory methods on {@link ConverterBuilder} to create instances of this class. |
| 42 | + * |
| 43 | + * @author Oliver Gierke |
| 44 | + * @since 2.0 |
| 45 | + * @see ConverterBuilder#writing(Class, Class, Function) |
| 46 | + * @see ConverterBuilder#reading(Class, Class, Function) |
| 47 | + * @soundtrack John Mayer - Still Feel Like Your Man (The Search for Everything) |
| 48 | + */ |
| 49 | +@Wither(AccessLevel.PACKAGE) |
| 50 | +@RequiredArgsConstructor(access = AccessLevel.PACKAGE) |
| 51 | +class DefaultConverterBuilder<S, T> |
| 52 | + implements ConverterAware, ReadingConverterBuilder<T, S>, WritingConverterBuilder<S, T> { |
| 53 | + |
| 54 | + private final @NonNull ConvertiblePair convertiblePair; |
| 55 | + private final @NonNull Optional<Function<? super S, ? extends T>> writing; |
| 56 | + private final @NonNull Optional<Function<? super T, ? extends S>> reading; |
| 57 | + |
| 58 | + /* |
| 59 | + * (non-Javadoc) |
| 60 | + * @see org.springframework.data.convert.WritingConverterBuilder#andReading(java.util.function.Function) |
| 61 | + */ |
| 62 | + @Override |
| 63 | + public ConverterAware andReading(Function<? super T, ? extends S> function) { |
| 64 | + return withReading(Optional.of(function)); |
| 65 | + } |
| 66 | + |
| 67 | + /* |
| 68 | + * (non-Javadoc) |
| 69 | + * @see org.springframework.data.convert.ReadingConverterBuilder#andWriting(java.util.function.Function) |
| 70 | + */ |
| 71 | + @Override |
| 72 | + public ConverterAware andWriting(Function<? super S, ? extends T> function) { |
| 73 | + return withWriting(Optional.of(function)); |
| 74 | + } |
| 75 | + |
| 76 | + /* |
| 77 | + * (non-Javadoc) |
| 78 | + * @see org.springframework.data.convert.ReadingConverterBuilder#getRequiredReadingConverter() |
| 79 | + */ |
| 80 | + @Override |
| 81 | + public GenericConverter getReadingConverter() { |
| 82 | + return getOptionalReadingConverter() |
| 83 | + .orElseThrow(() -> new IllegalStateException("No reading converter specified!")); |
| 84 | + } |
| 85 | + |
| 86 | + /* |
| 87 | + * (non-Javadoc) |
| 88 | + * @see org.springframework.data.convert.WritingConverterBuilder#getRequiredWritingConverter() |
| 89 | + */ |
| 90 | + @Override |
| 91 | + public GenericConverter getWritingConverter() { |
| 92 | + return getOptionalWritingConverter() |
| 93 | + .orElseThrow(() -> new IllegalStateException("No writing converter specified!")); |
| 94 | + } |
| 95 | + |
| 96 | + /* |
| 97 | + * (non-Javadoc) |
| 98 | + * @see org.springframework.data.convert.ConverterBuilder#getConverters() |
| 99 | + */ |
| 100 | + @Override |
| 101 | + public Set<GenericConverter> getConverters() { |
| 102 | + return Optionals.toStream(getOptionalReadingConverter(), getOptionalWritingConverter()).collect(Collectors.toSet()); |
| 103 | + } |
| 104 | + |
| 105 | + private Optional<GenericConverter> getOptionalReadingConverter() { |
| 106 | + return reading.map(it -> new ConfigurableGenericConverter.Reading<>(convertiblePair, it)); |
| 107 | + } |
| 108 | + |
| 109 | + private Optional<GenericConverter> getOptionalWritingConverter() { |
| 110 | + return writing.map(it -> new ConfigurableGenericConverter.Writing<>(invertedPair(), it)); |
| 111 | + } |
| 112 | + |
| 113 | + private ConvertiblePair invertedPair() { |
| 114 | + return new ConvertiblePair(convertiblePair.getTargetType(), convertiblePair.getSourceType()); |
| 115 | + } |
| 116 | + |
| 117 | + @RequiredArgsConstructor |
| 118 | + @EqualsAndHashCode |
| 119 | + private static class ConfigurableGenericConverter<S, T> implements GenericConverter { |
| 120 | + |
| 121 | + private final ConvertiblePair convertiblePair; |
| 122 | + private final Function<? super S, ? extends T> function; |
| 123 | + |
| 124 | + /* |
| 125 | + * (non-Javadoc) |
| 126 | + * @see org.springframework.core.convert.converter.GenericConverter#convert(java.lang.Object, org.springframework.core.convert.TypeDescriptor, org.springframework.core.convert.TypeDescriptor) |
| 127 | + */ |
| 128 | + @Override |
| 129 | + @SuppressWarnings("unchecked") |
| 130 | + public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) { |
| 131 | + return function.apply((S) source); |
| 132 | + } |
| 133 | + |
| 134 | + /* |
| 135 | + * (non-Javadoc) |
| 136 | + * @see org.springframework.core.convert.converter.GenericConverter#getConvertibleTypes() |
| 137 | + */ |
| 138 | + @Override |
| 139 | + public Set<ConvertiblePair> getConvertibleTypes() { |
| 140 | + return Collections.singleton(convertiblePair); |
| 141 | + } |
| 142 | + |
| 143 | + @WritingConverter |
| 144 | + private static class Writing<S, T> extends ConfigurableGenericConverter<S, T> { |
| 145 | + |
| 146 | + public Writing(ConvertiblePair convertiblePair, Function<? super S, ? extends T> function) { |
| 147 | + super(convertiblePair, function); |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + @ReadingConverter |
| 152 | + private static class Reading<S, T> extends ConfigurableGenericConverter<S, T> { |
| 153 | + |
| 154 | + public Reading(ConvertiblePair convertiblePair, Function<? super S, ? extends T> function) { |
| 155 | + super(convertiblePair, function); |
| 156 | + } |
| 157 | + } |
| 158 | + } |
| 159 | +} |
0 commit comments