1 /*
2 * Copyright 2012-2020 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.model;
17
18 import java.util.Collections;
19 import java.util.Map;
20
21 import org.springframework.data.mapping.PersistentEntity;
22 import org.springframework.util.Assert;
23
24 /**
25 * Simple value object allowing access to {@link EntityInstantiator} instances for a given type falling back to a
26 * default one.
27 *
28 * @author Oliver Drotbohm
29 * @author Thomas Darimont
30 * @author Christoph Strobl
31 * @author Mark Paluch
32 * @since 2.3
33 */
34 public class EntityInstantiators {
35
36 private final EntityInstantiator fallback;
37 private final Map<Class<?>, EntityInstantiator> customInstantiators;
38
39 /**
40 * Creates a new {@link EntityInstantiators} using the default fallback instantiator and no custom ones.
41 */
42 public EntityInstantiators() {
43 this(Collections.emptyMap());
44 }
45
46 /**
47 * Creates a new {@link EntityInstantiators} using the given {@link EntityInstantiator} as fallback.
48 *
49 * @param fallback must not be {@literal null}.
50 */
51 public EntityInstantiators(EntityInstantiator fallback) {
52 this(fallback, Collections.emptyMap());
53 }
54
55 /**
56 * Creates a new {@link EntityInstantiators} using the default fallback instantiator and the given custom ones.
57 *
58 * @param customInstantiators must not be {@literal null}.
59 */
60 public EntityInstantiators(Map<Class<?>, EntityInstantiator> customInstantiators) {
61 this(new KotlinClassGeneratingEntityInstantiator(), customInstantiators);
62 }
63
64 /**
65 * Creates a new {@link EntityInstantiator} using the given fallback {@link EntityInstantiator} and the given custom
66 * ones.
67 *
68 * @param defaultInstantiator must not be {@literal null}.
69 * @param customInstantiators must not be {@literal null}.
70 */
71 public EntityInstantiators(EntityInstantiator defaultInstantiator,
72 Map<Class<?>, EntityInstantiator> customInstantiators) {
73
74 Assert.notNull(defaultInstantiator, "DefaultInstantiator must not be null!");
75 Assert.notNull(customInstantiators, "CustomInstantiators must not be null!");
76
77 this.fallback = defaultInstantiator;
78 this.customInstantiators = customInstantiators;
79 }
80
81 /**
82 * Returns the {@link EntityInstantiator} to be used to create the given {@link PersistentEntity}.
83 *
84 * @param entity must not be {@literal null}.
85 * @return will never be {@literal null}.
86 */
87 public EntityInstantiator getInstantiatorFor(PersistentEntity<?, ?> entity) {
88
89 Assert.notNull(entity, "Entity must not be null!");
90 Class<?> type = entity.getType();
91
92 if (!customInstantiators.containsKey(type)) {
93 return fallback;
94 }
95
96 EntityInstantiator instantiator = customInstantiators.get(entity.getType());
97 return instantiator == null ? fallback : instantiator;
98 }
99 }
100