|
18 | 18 | import java.lang.annotation.Annotation;
|
19 | 19 | import java.lang.reflect.Constructor;
|
20 | 20 | import java.util.Arrays;
|
21 |
| -import java.util.HashMap; |
22 | 21 | import java.util.List;
|
23 | 22 | import java.util.Map;
|
24 |
| -import java.util.concurrent.locks.Lock; |
25 |
| -import java.util.concurrent.locks.ReentrantReadWriteLock; |
| 23 | +import java.util.concurrent.ConcurrentHashMap; |
26 | 24 |
|
27 | 25 | import org.springframework.beans.factory.annotation.Value;
|
28 | 26 | import org.springframework.data.annotation.PersistenceConstructor;
|
|
42 | 40 | * @author Thomas Darimont
|
43 | 41 | * @author Christoph Strobl
|
44 | 42 | * @author Mark Paluch
|
| 43 | + * @author Myeonghyeon Lee |
45 | 44 | */
|
46 | 45 | public class PreferredConstructor<T, P extends PersistentProperty<P>> {
|
47 | 46 |
|
48 | 47 | private final Constructor<T> constructor;
|
49 | 48 | private final List<Parameter<Object, P>> parameters;
|
50 |
| - private final Map<PersistentProperty<?>, Boolean> isPropertyParameterCache = new HashMap<>(); |
51 |
| - |
52 |
| - private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
53 |
| - private final Lock read = lock.readLock(); |
54 |
| - private final Lock write = lock.writeLock(); |
| 49 | + private final Map<PersistentProperty<?>, Boolean> isPropertyParameterCache = new ConcurrentHashMap<>(); |
55 | 50 |
|
56 | 51 | /**
|
57 | 52 | * Creates a new {@link PreferredConstructor} from the given {@link Constructor} and {@link Parameter}s.
|
@@ -128,36 +123,22 @@ public boolean isConstructorParameter(PersistentProperty<?> property) {
|
128 | 123 |
|
129 | 124 | Assert.notNull(property, "Property must not be null!");
|
130 | 125 |
|
131 |
| - try { |
132 |
| - |
133 |
| - read.lock(); |
134 |
| - Boolean cached = isPropertyParameterCache.get(property); |
135 |
| - |
136 |
| - if (cached != null) { |
137 |
| - return cached; |
138 |
| - } |
| 126 | + Boolean cached = isPropertyParameterCache.get(property); |
139 | 127 |
|
140 |
| - } finally { |
141 |
| - read.unlock(); |
| 128 | + if (cached != null) { |
| 129 | + return cached; |
142 | 130 | }
|
143 | 131 |
|
144 |
| - try { |
145 |
| - |
146 |
| - write.lock(); |
147 |
| - |
148 |
| - for (Parameter<?, P> parameter : parameters) { |
149 |
| - if (parameter.maps(property)) { |
150 |
| - isPropertyParameterCache.put(property, true); |
151 |
| - return true; |
152 |
| - } |
| 132 | + boolean result = false; |
| 133 | + for (Parameter<?, P> parameter : parameters) { |
| 134 | + if (parameter.maps(property)) { |
| 135 | + isPropertyParameterCache.put(property, true); |
| 136 | + result = true; |
| 137 | + break; |
153 | 138 | }
|
154 |
| - |
155 |
| - isPropertyParameterCache.put(property, false); |
156 |
| - return false; |
157 |
| - |
158 |
| - } finally { |
159 |
| - write.unlock(); |
160 | 139 | }
|
| 140 | + |
| 141 | + return result; |
161 | 142 | }
|
162 | 143 |
|
163 | 144 | /**
|
|
0 commit comments