Skip to content

Commit

Permalink
Introduce putAll(Properties)
Browse files Browse the repository at this point in the history
- This is useful when you need to copy all the values from a JUL Properties to this structure
  • Loading branch information
gastaldi committed Jul 8, 2024
1 parent 256287d commit 8d5dec5
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 6 deletions.
42 changes: 39 additions & 3 deletions src/main/java/org/codejive/properties/Properties.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 <code>
* defaultValue</code>.
* defaultValue</code>.
*/
public String getProperty(String key, String defaultValue) {
if (containsKey(key)) {
Expand Down Expand Up @@ -773,6 +797,18 @@ public List<PropertiesParser.Token> next() {
Spliterators.spliterator(iter, tokens.size(), Spliterator.SORTED), false);
}

/**
* Copies all entries from the <code>java.util.Properties</code> object to this object
*
* @param properties a <code>java.util.Properties</code> object
* @throws NullPointerException if the properties parameter is null
*/
public void putAll(java.util.Properties properties) {
for (Entry<Object, Object> entry : properties.entrySet()) {
put(entry.getKey().toString(), entry.getValue().toString());
}
}

/**
* Returns a <code>java.util.Properties</code> with the same contents as this object. The
* information is a copy, changes to one Properties object will not affect the other.
Expand Down
20 changes: 17 additions & 3 deletions src/test/java/org/codejive/properties/TestProperties.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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());
}
Expand Down

0 comments on commit 8d5dec5

Please sign in to comment.