From 8d5dec52853bdb56ca8adeb5c81e92a466261202 Mon Sep 17 00:00:00 2001 From: George Gastaldi Date: Mon, 8 Jul 2024 12:20:03 -0300 Subject: [PATCH] Introduce putAll(Properties) - This is useful when you need to copy all the values from a JUL Properties to this structure --- .../org/codejive/properties/Properties.java | 42 +++++++++++++++++-- .../codejive/properties/TestProperties.java | 20 +++++++-- 2 files changed, 56 insertions(+), 6 deletions(-) diff --git a/src/main/java/org/codejive/properties/Properties.java b/src/main/java/org/codejive/properties/Properties.java index 2544a33..28721fe 100644 --- a/src/main/java/org/codejive/properties/Properties.java +++ b/src/main/java/org/codejive/properties/Properties.java @@ -2,12 +2,36 @@ import static org.codejive.properties.PropertiesParser.unescape; -import java.io.*; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.OutputStream; +import java.io.OutputStreamWriter; +import java.io.PrintStream; +import java.io.PrintWriter; +import java.io.Reader; +import java.io.Writer; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.StandardOpenOption; -import java.util.*; +import java.util.AbstractMap; +import java.util.AbstractSet; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.Enumeration; +import java.util.Iterator; +import java.util.LinkedHashMap; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.NoSuchElementException; +import java.util.Objects; +import java.util.Set; +import java.util.Spliterator; +import java.util.Spliterators; import java.util.function.Function; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -68,7 +92,7 @@ public String getProperty(String key) { * @param key the key to look up. * @param defaultValue the value to return if no mapping was found for the key. * @return the value in this property list with the specified key value or the value of - * defaultValue. + * defaultValue. */ public String getProperty(String key, String defaultValue) { if (containsKey(key)) { @@ -773,6 +797,18 @@ public List next() { Spliterators.spliterator(iter, tokens.size(), Spliterator.SORTED), false); } + /** + * Copies all entries from the java.util.Properties object to this object + * + * @param properties a java.util.Properties object + * @throws NullPointerException if the properties parameter is null + */ + public void putAll(java.util.Properties properties) { + for (Entry entry : properties.entrySet()) { + put(entry.getKey().toString(), entry.getValue().toString()); + } + } + /** * Returns a java.util.Properties with the same contents as this object. The * information is a copy, changes to one Properties object will not affect the other. diff --git a/src/test/java/org/codejive/properties/TestProperties.java b/src/test/java/org/codejive/properties/TestProperties.java index 4c51c72..ab2ee5e 100644 --- a/src/test/java/org/codejive/properties/TestProperties.java +++ b/src/test/java/org/codejive/properties/TestProperties.java @@ -1,8 +1,13 @@ package org.codejive.properties; -import static org.assertj.core.api.Assertions.*; - -import java.io.*; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import java.io.IOException; +import java.io.PrintWriter; +import java.io.Reader; +import java.io.StringReader; +import java.io.StringWriter; import java.net.URISyntaxException; import java.nio.file.Files; import java.nio.file.Path; @@ -691,6 +696,15 @@ void testMultiDelim() throws IOException, URISyntaxException { assertThat(sw.toString()).isEqualTo(readAll(getResource("/test-multidelim.properties"))); } + @Test + void testPutAll() { + Properties p = new Properties(); + java.util.Properties ju = new java.util.Properties(); + ju.setProperty("foo", "bar"); + p.putAll(ju); + assertThat(p.getProperty("foo")).isEqualTo("bar"); + } + private Path getResource(String name) throws URISyntaxException { return Paths.get(getClass().getResource(name).toURI()); }