|
| 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 java.util.Optional; |
| 19 | +import java.util.Set; |
| 20 | +import java.util.function.Function; |
| 21 | + |
| 22 | +import org.springframework.core.convert.converter.GenericConverter; |
| 23 | +import org.springframework.core.convert.converter.GenericConverter.ConvertiblePair; |
| 24 | +import org.springframework.util.Assert; |
| 25 | + |
| 26 | +/** |
| 27 | + * API to easily set up {@link GenericConverter} instances using Java 8 lambdas, mostly in bidirectional fashion for |
| 28 | + * easy registration as custom type converters of the Spring Data mapping subsystem. The registration starts either with |
| 29 | + * the definition of a reading or writing converter that can then be completed. |
| 30 | + * |
| 31 | + * @author Oliver Gierke |
| 32 | + * @since 2.0 |
| 33 | + * @see #reading(Class, Class, Function) |
| 34 | + * @see #writing(Class, Class, Function) |
| 35 | + * @soundtrack John Mayer - Moving On and Getting Over (The Search for Everything) |
| 36 | + */ |
| 37 | +public interface ConverterBuilder { |
| 38 | + |
| 39 | + /** |
| 40 | + * Creates a new {@link ReadingConverterBuilder} to produce a converter to read values of the given source (the store |
| 41 | + * type) into the given target (the domain type). |
| 42 | + * |
| 43 | + * @param source must not be {@literal null}. |
| 44 | + * @param target must not be {@literal null}. |
| 45 | + * @param function must not be {@literal null}. |
| 46 | + * @return |
| 47 | + */ |
| 48 | + static <S, T> ReadingConverterBuilder<S, T> reading(Class<S> source, Class<T> target, |
| 49 | + Function<? super S, ? extends T> function) { |
| 50 | + |
| 51 | + Assert.notNull(source, "Source type must not be null!"); |
| 52 | + Assert.notNull(target, "Target type must not be null!"); |
| 53 | + Assert.notNull(function, "Conversion function must not be null!"); |
| 54 | + |
| 55 | + return new DefaultConverterBuilder<>(new ConvertiblePair(source, target), Optional.empty(), Optional.of(function)); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Creates a new {@link WritingConverterBuilder} to produce a converter to write values of the given source (the |
| 60 | + * domain type) into the given target (the store type). |
| 61 | + * |
| 62 | + * @param source must not be {@literal null}. |
| 63 | + * @param target must not be {@literal null}. |
| 64 | + * @param function must not be {@literal null}. |
| 65 | + * @return |
| 66 | + */ |
| 67 | + static <S, T> WritingConverterBuilder<S, T> writing(Class<S> source, Class<T> target, |
| 68 | + Function<? super S, ? extends T> function) { |
| 69 | + |
| 70 | + Assert.notNull(source, "Source type must not be null!"); |
| 71 | + Assert.notNull(target, "Target type must not be null!"); |
| 72 | + Assert.notNull(function, "Conversion function must not be null!"); |
| 73 | + |
| 74 | + return new DefaultConverterBuilder<>(new ConvertiblePair(target, source), Optional.of(function), Optional.empty()); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Returns all {@link GenericConverter} instances to be registered for the current {@link ConverterBuilder}. |
| 79 | + * |
| 80 | + * @return |
| 81 | + */ |
| 82 | + Set<GenericConverter> getConverters(); |
| 83 | + |
| 84 | + /** |
| 85 | + * Exposes a writing converter. |
| 86 | + * |
| 87 | + * @author Oliver Gierke |
| 88 | + * @since 2.0 |
| 89 | + */ |
| 90 | + interface WritingConverterAware { |
| 91 | + |
| 92 | + /** |
| 93 | + * Returns the writing converter already created. |
| 94 | + * |
| 95 | + * @return |
| 96 | + */ |
| 97 | + GenericConverter getWritingConverter(); |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Exposes a reading converter. |
| 102 | + * |
| 103 | + * @author Oliver Gierke |
| 104 | + * @since 2.0 |
| 105 | + */ |
| 106 | + interface ReadingConverterAware { |
| 107 | + |
| 108 | + /** |
| 109 | + * Returns the reading converter already created. |
| 110 | + * |
| 111 | + * @return |
| 112 | + */ |
| 113 | + GenericConverter getReadingConverter(); |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * Interface to represent an intermediate setup step of {@link ConverterAware} defining a reading converter first. |
| 118 | + * |
| 119 | + * @author Oliver Gierke |
| 120 | + * @since 2.0 |
| 121 | + */ |
| 122 | + interface ReadingConverterBuilder<T, S> extends ConverterBuilder, ReadingConverterAware { |
| 123 | + |
| 124 | + /** |
| 125 | + * Creates a new {@link ConverterAware} by registering the given {@link Function} to add a write converter. |
| 126 | + * |
| 127 | + * @param function must not be {@literal null}. |
| 128 | + * @return |
| 129 | + */ |
| 130 | + ConverterAware andWriting(Function<? super S, ? extends T> function); |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * Interface to represent an intermediate setup step of {@link ConverterAware} defining a writing converter first. |
| 135 | + * |
| 136 | + * @author Oliver Gierke |
| 137 | + * @since 2.0 |
| 138 | + */ |
| 139 | + interface WritingConverterBuilder<S, T> extends ConverterBuilder, WritingConverterAware { |
| 140 | + |
| 141 | + /** |
| 142 | + * Creates a new {@link ConverterAware} by registering the given {@link Function} to add a write converter. |
| 143 | + * |
| 144 | + * @param function must not be {@literal null}. |
| 145 | + * @return |
| 146 | + */ |
| 147 | + ConverterAware andReading(Function<? super T, ? extends S> function); |
| 148 | + } |
| 149 | + |
| 150 | + /** |
| 151 | + * A {@link ConverterBuilder} aware of both a reading and writing converter. |
| 152 | + * |
| 153 | + * @author Oliver Gierke |
| 154 | + * @since 2.0 |
| 155 | + */ |
| 156 | + interface ConverterAware extends ConverterBuilder, ReadingConverterAware, WritingConverterAware {} |
| 157 | +} |
0 commit comments