|
| 1 | +/* |
| 2 | + * Copyright 2019 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.mapping; |
| 17 | + |
| 18 | +import java.util.Collection; |
| 19 | +import java.util.HashMap; |
| 20 | +import java.util.List; |
| 21 | +import java.util.Map; |
| 22 | +import java.util.Set; |
| 23 | +import java.util.function.Function; |
| 24 | + |
| 25 | +import org.springframework.lang.Nullable; |
| 26 | +import org.springframework.util.Assert; |
| 27 | + |
| 28 | +/** |
| 29 | + * A context object for lookups of values for {@link PersistentPropertyPaths} via a {@link PersistentPropertyAccessor}. |
| 30 | + * It allows to register functions to post-process the objects returned for a particular property, so that the |
| 31 | + * subsequent traversal would rather use the processed object. This is especially helpful if you need to traverse paths |
| 32 | + * that contain {@link Collection}s and {@link Map} that usually need indices and keys to reasonably traverse nested |
| 33 | + * properties. |
| 34 | + * |
| 35 | + * @author Oliver Drotbohm |
| 36 | + * @since 2.2 |
| 37 | + * @soundtrack 8-Bit Misfits - Crash Into Me (Dave Matthews Band cover) |
| 38 | + */ |
| 39 | +public class TraversalContext { |
| 40 | + |
| 41 | + private Map<PersistentProperty<?>, Function<Object, Object>> handlers = new HashMap<>(); |
| 42 | + |
| 43 | + /** |
| 44 | + * Registers a {@link Function} to post-process values for the given property. |
| 45 | + * |
| 46 | + * @param property must not be {@literal null}. |
| 47 | + * @param handler must not be {@literal null}. |
| 48 | + * @return |
| 49 | + */ |
| 50 | + public TraversalContext registerHandler(PersistentProperty<?> property, Function<Object, Object> handler) { |
| 51 | + |
| 52 | + Assert.notNull(property, "Property must not be null!"); |
| 53 | + Assert.notNull(handler, "Handler must not be null!"); |
| 54 | + |
| 55 | + handlers.put(property, handler); |
| 56 | + |
| 57 | + return this; |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Registers a {@link Function} to handle {@link Collection} values for the given property. |
| 62 | + * |
| 63 | + * @param property must not be {@literal null}. |
| 64 | + * @param handler must not be {@literal null}. |
| 65 | + * @return |
| 66 | + */ |
| 67 | + @SuppressWarnings("unchecked") |
| 68 | + public TraversalContext registerCollectionHandler(PersistentProperty<?> property, |
| 69 | + Function<? super Collection<?>, Object> handler) { |
| 70 | + return registerHandler(property, Collection.class, (Function<Object, Object>) handler); |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Registers a {@link Function} to handle {@link List} values for the given property. |
| 75 | + * |
| 76 | + * @param property must not be {@literal null}. |
| 77 | + * @param handler must not be {@literal null}. |
| 78 | + * @return |
| 79 | + */ |
| 80 | + @SuppressWarnings("unchecked") |
| 81 | + public TraversalContext registerListHandler(PersistentProperty<?> property, |
| 82 | + Function<? super List<?>, Object> handler) { |
| 83 | + return registerHandler(property, List.class, (Function<Object, Object>) handler); |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Registers a {@link Function} to handle {@link Set} values for the given property. |
| 88 | + * |
| 89 | + * @param property must not be {@literal null}. |
| 90 | + * @param handler must not be {@literal null}. |
| 91 | + * @return |
| 92 | + */ |
| 93 | + @SuppressWarnings("unchecked") |
| 94 | + public TraversalContext registerSetHandler(PersistentProperty<?> property, Function<? super Set<?>, Object> handler) { |
| 95 | + return registerHandler(property, Set.class, (Function<Object, Object>) handler); |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Registers a {@link Function} to handle {@link Map} values for the given property. |
| 100 | + * |
| 101 | + * @param property must not be {@literal null}. |
| 102 | + * @param handler must not be {@literal null}. |
| 103 | + * @return |
| 104 | + */ |
| 105 | + @SuppressWarnings("unchecked") |
| 106 | + public TraversalContext registerMapHandler(PersistentProperty<?> property, |
| 107 | + Function<? super Map<?, ?>, Object> handler) { |
| 108 | + return registerHandler(property, Map.class, (Function<Object, Object>) handler); |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Registers the given {@link Function} to post-process values obtained for the given {@link PersistentProperty} for |
| 113 | + * the given type. |
| 114 | + * |
| 115 | + * @param <T> the type of the value to handle. |
| 116 | + * @param property must not be {@literal null}. |
| 117 | + * @param type must not be {@literal null}. |
| 118 | + * @param handler must not be {@literal null}. |
| 119 | + * @return |
| 120 | + */ |
| 121 | + public <T> TraversalContext registerHandler(PersistentProperty<?> property, Class<T> type, |
| 122 | + Function<? super T, Object> handler) { |
| 123 | + |
| 124 | + Assert.isTrue(type.isAssignableFrom(property.getType()), () -> String |
| 125 | + .format("Cannot register a property handler for %s on a property of type %s!", type, property.getType())); |
| 126 | + |
| 127 | + Function<Object, T> caster = it -> type.cast(it); |
| 128 | + |
| 129 | + return registerHandler(property, caster.andThen(handler)); |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * Post-processes the value obtained for the given {@link PersistentProperty} using the registered handler. |
| 134 | + * |
| 135 | + * @param property must not be {@literal null}. |
| 136 | + * @param value can be {@literal null}. |
| 137 | + * @return the post-processed value or the value itself if no handlers registered. |
| 138 | + */ |
| 139 | + @Nullable |
| 140 | + Object postProcess(PersistentProperty<?> property, @Nullable Object value) { |
| 141 | + |
| 142 | + Function<Object, Object> handler = handlers.get(property); |
| 143 | + |
| 144 | + return handler == null ? value : handler.apply(value); |
| 145 | + } |
| 146 | +} |
0 commit comments