Skip to content

Commit ca09b1f

Browse files
committed
Introduce putIfAbsent() in AnnotationAttributes
Issue: SPR-13060
1 parent 18946c8 commit ca09b1f

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

spring-core/src/main/java/org/springframework/core/annotation/AnnotatedElementUtils.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -963,10 +963,7 @@ else if (!AnnotationUtils.VALUE.equals(attributeName) && attributes.containsKey(
963963
String aliasedAttributeNameInTarget = AnnotationUtils.getAliasedAttributeName(
964964
attributeMethodInTarget, null);
965965
if (aliasedAttributeNameInTarget != null) {
966-
Object aliasedValueInTarget = attributes.get(aliasedAttributeNameInTarget);
967-
if (aliasedValueInTarget == null) {
968-
attributes.put(aliasedAttributeNameInTarget, adaptedValue);
969-
}
966+
attributes.putIfAbsent(aliasedAttributeNameInTarget, adaptedValue);
970967
}
971968
}
972969
}

spring-core/src/main/java/org/springframework/core/annotation/AnnotationAttributes.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,25 @@ private <T> T doGet(String attributeName, Class<T> expectedType) {
158158
return (T) value;
159159
}
160160

161+
/**
162+
* Store the supplied {@code value} in this map under the specified
163+
* {@code key}, unless a value is already stored under the key.
164+
* @param key the key under which to store the value
165+
* @param value the value to store
166+
* @return the current value stored in this map, or {@code null} if no
167+
* value was previously stored in this map
168+
* @see #get
169+
* @see #put
170+
*/
171+
@Override
172+
public Object putIfAbsent(String key, Object value) {
173+
Object obj = get(key);
174+
if (obj == null) {
175+
obj = put(key, value);
176+
}
177+
return obj;
178+
}
179+
161180
@Override
162181
public String toString() {
163182
Iterator<Map.Entry<String, Object>> entries = entrySet().iterator();

0 commit comments

Comments
 (0)