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