|
| 1 | +/* |
| 2 | + * Copyright 2022 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.lang.reflect.Executable; |
| 19 | +import java.util.Arrays; |
| 20 | +import java.util.List; |
| 21 | +import java.util.Map; |
| 22 | +import java.util.concurrent.ConcurrentHashMap; |
| 23 | + |
| 24 | +import org.springframework.util.Assert; |
| 25 | + |
| 26 | +/** |
| 27 | + * Value object to encapsulate the entity creation mechanism through a {@link Executable} to be used when mapping |
| 28 | + * persistent data to objects. |
| 29 | + * |
| 30 | + * @author Mark Paluch |
| 31 | + * @since 3.0 |
| 32 | + */ |
| 33 | +class EntityCreatorMetadataSupport<T, P extends PersistentProperty<P>> implements EntityCreatorMetadata<P> { |
| 34 | + |
| 35 | + private final Executable executable; |
| 36 | + private final List<Parameter<Object, P>> parameters; |
| 37 | + private final Map<PersistentProperty<?>, Boolean> isPropertyParameterCache = new ConcurrentHashMap<>(); |
| 38 | + |
| 39 | + /** |
| 40 | + * Creates a new {@link EntityCreatorMetadataSupport} from the given {@link Executable} and {@link Parameter}s. |
| 41 | + * |
| 42 | + * @param executable must not be {@literal null}. |
| 43 | + * @param parameters must not be {@literal null}. |
| 44 | + */ |
| 45 | + @SafeVarargs |
| 46 | + public EntityCreatorMetadataSupport(Executable executable, Parameter<Object, P>... parameters) { |
| 47 | + |
| 48 | + Assert.notNull(executable, "Executable must not be null!"); |
| 49 | + Assert.notNull(parameters, "Parameters must not be null!"); |
| 50 | + |
| 51 | + this.executable = executable; |
| 52 | + this.parameters = Arrays.asList(parameters); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Returns the underlying {@link Executable} that can be invoked reflectively. |
| 57 | + * |
| 58 | + * @return |
| 59 | + */ |
| 60 | + Executable getExecutable() { |
| 61 | + return executable; |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Returns the {@link Parameter}s of the executable. |
| 66 | + * |
| 67 | + * @return |
| 68 | + */ |
| 69 | + public List<Parameter<Object, P>> getParameters() { |
| 70 | + return parameters; |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Returns whether the given {@link PersistentProperty} is referenced in a creator argument of the |
| 75 | + * {@link PersistentEntity} backing this {@link EntityCreatorMetadataSupport}. |
| 76 | + * <p> |
| 77 | + * Results of this call are cached and reused on the next invocation. Calling this method for a |
| 78 | + * {@link PersistentProperty} that was not yet added to its owning {@link PersistentEntity} will capture that state |
| 79 | + * and return the same result after adding {@link PersistentProperty} to its entity. |
| 80 | + * |
| 81 | + * @param property must not be {@literal null}. |
| 82 | + * @return {@literal true} if the {@link PersistentProperty} is used in the creator. |
| 83 | + */ |
| 84 | + @Override |
| 85 | + public boolean isCreatorParameter(PersistentProperty<?> property) { |
| 86 | + |
| 87 | + Assert.notNull(property, "Property must not be null!"); |
| 88 | + |
| 89 | + Boolean cached = isPropertyParameterCache.get(property); |
| 90 | + |
| 91 | + if (cached != null) { |
| 92 | + return cached; |
| 93 | + } |
| 94 | + |
| 95 | + boolean result = doGetIsCreatorParameter(property); |
| 96 | + |
| 97 | + isPropertyParameterCache.put(property, result); |
| 98 | + |
| 99 | + return result; |
| 100 | + } |
| 101 | + |
| 102 | + @Override |
| 103 | + public String toString() { |
| 104 | + return executable.toString(); |
| 105 | + } |
| 106 | + |
| 107 | + private boolean doGetIsCreatorParameter(PersistentProperty<?> property) { |
| 108 | + |
| 109 | + for (Parameter<?, P> parameter : parameters) { |
| 110 | + if (parameter.maps(property)) { |
| 111 | + return true; |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + return false; |
| 116 | + } |
| 117 | +} |
0 commit comments