This is an automated email from the ASF dual-hosted git repository. desruisseaux pushed a commit to branch geoapi-4.0 in repository https://gitbox.apache.org/repos/asf/sis.git
commit 281a020ec5f7e47acac28961cdb6a80a4bb28e39 Author: Martin Desruisseaux <martin.desruisse...@geomatys.com> AuthorDate: Thu Jan 27 12:30:38 2022 +0100 Add a `MapValue.putIfAbsent` method for efficiency. It is used in various places. --- .../main/java/org/apache/sis/metadata/ValueMap.java | 20 +++++++++++++++++++- .../java/org/apache/sis/metadata/ValueMapTest.java | 15 ++++++++++++++- 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/core/sis-metadata/src/main/java/org/apache/sis/metadata/ValueMap.java b/core/sis-metadata/src/main/java/org/apache/sis/metadata/ValueMap.java index a68fdae..3538fe0 100644 --- a/core/sis-metadata/src/main/java/org/apache/sis/metadata/ValueMap.java +++ b/core/sis-metadata/src/main/java/org/apache/sis/metadata/ValueMap.java @@ -31,7 +31,7 @@ import static org.apache.sis.metadata.PropertyAccessor.RETURN_PREVIOUS; * are the value returned by the {@code getFoo()} method using reflection. * * @author Martin Desruisseaux (Geomatys) - * @version 0.3 + * @version 1.2 * * @see MetadataStandard#asValueMap(Object, Class, KeyNamePolicy, ValueExistencePolicy) * @@ -118,6 +118,24 @@ final class ValueMap extends PropertyMap<Object> { } /** + * Associates the specified value with the specified key in this map if no value is currently associated. + * + * @throws IllegalArgumentException if the given key is not the name of a property in the metadata. + * @throws ClassCastException if the given value is not of the expected type. + * @throws UnmodifiableMetadataException if the property for the given key is read-only. + */ + @Override + public Object putIfAbsent(final String key, final Object value) { + final int index = accessor.indexOf(key, true); + final Object old = accessor.get(index, metadata); + if (old == null || valuePolicy.isSkipped(old)) { + return accessor.set(index, metadata, value, RETURN_NULL); + } else { + return old; + } + } + + /** * Puts every entries from the given map. This method is overloaded for performance reasons * since we are not interested in the return value of the {@link #put(String, Object)} method. * diff --git a/core/sis-metadata/src/test/java/org/apache/sis/metadata/ValueMapTest.java b/core/sis-metadata/src/test/java/org/apache/sis/metadata/ValueMapTest.java index 64b0577..a5d58b5 100644 --- a/core/sis-metadata/src/test/java/org/apache/sis/metadata/ValueMapTest.java +++ b/core/sis-metadata/src/test/java/org/apache/sis/metadata/ValueMapTest.java @@ -46,7 +46,7 @@ import static org.apache.sis.test.TestUtilities.getSingleton; * Unless otherwise specified, all tests use the {@link MetadataStandard#ISO_19115} constant. * * @author Martin Desruisseaux (Geomatys) - * @version 1.0 + * @version 1.2 * * @see MetadataStandardTest#testValueMap() * @@ -305,4 +305,17 @@ public final strictfp class ValueMapTest extends TestCase { new SimpleEntry<>("ISBN", "9782505004509") }, map.entrySet().toArray()); } + + /** + * Tests {@link ValueMap#putIfAbsent(String, Object)}. + */ + @Test + public void testPutIfAbsent() { + final Map<String, Object> citation = createCitation(); + assertEquals("Undercurrent", String.valueOf(citation.putIfAbsent("title", "A new title"))); + assertEquals("Undercurrent", String.valueOf(citation.get("title"))); + assertEquals("Undercurrent", String.valueOf(citation.remove("title"))); + assertNull(citation.putIfAbsent("title", "A new title")); + assertEquals("A new title", String.valueOf(citation.get("title"))); + } }