Updated methods to use path insetad of file from java.nio

This commit is contained in:
Isaac Parenteau
2019-07-21 10:52:22 -05:00
parent 4000f46e71
commit 0c3739c8db
8 changed files with 141 additions and 72 deletions

View File

@@ -4,11 +4,12 @@ import static net.locusworks.common.Charsets.UTF_8;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Map;
import java.util.Properties;
import java.util.stream.Collectors;
@@ -41,17 +42,31 @@ public class PropertiesManager {
}
/**
* Load configuration from a file
* Load configuration from a file. This method has been deprecated and may be removed in
* future released. Please use java.nio.Path
* @param file File to load
* @return properties
* @throws IOException Exception thrown the file can't be read
*/
@Deprecated
public static Properties loadConfiguration(File file) throws IOException {
if (!file.exists()) {
return loadConfiguration(file.toPath());
}
/**
* Load configuration from a file.
* @param path the path to the file
* @return properties
* @throws IOException Exception thrown the file can't be read
*/
public static Properties loadConfiguration(Path path) throws IOException {
if (Files.notExists(path)) {
return new Properties();
}
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file), UTF_8));
return loadConfiguration(br);
try(BufferedReader br = Files.newBufferedReader(path)) {
return loadConfiguration(br);
}
}
/**
@@ -74,19 +89,19 @@ public class PropertiesManager {
*/
public static Map<String, String> addConfiguration(Properties to, Properties from) {
Map<String, String> results = from.entrySet()
.stream()
.filter(entry -> !to.containsKey(entry.getKey()))
.map(entry -> {
String key = entry.getKey().toString();
String value = entry.getValue().toString();
to.put(key, value);
return new Pair<String, String>(key, value);
})
.collect(Collectors.toMap(key -> key.getValue1(), value -> value.getValue2()));
.stream()
.filter(entry -> !to.containsKey(entry.getKey()))
.map(entry -> {
String key = entry.getKey().toString();
String value = entry.getValue().toString();
to.put(key, value);
return new Pair<String, String>(key, value);
})
.collect(Collectors.toMap(key -> key.getValue1(), value -> value.getValue2()));
return results;
}
/**
* Removes configuration values that are not present in the comparedTo
* @param from Properties file to remove values from
@@ -95,29 +110,42 @@ public class PropertiesManager {
*/
public static Map<String, String> removeConfiguration(Properties from, Properties comparedTo) {
Map<String, String> results = from.keySet()
.stream()
.filter(key -> !comparedTo.containsKey(key)) //only get the items that are not in the comparedTo properties
.map(key -> new Pair<String, String>(String.valueOf(key), String.valueOf(from.get(key))))
.collect(Collectors.toList()) //Create a list of paired items (key value) of the items that were filtered
.stream()
.map(pair -> { //remove those pairs from the from properties
from.remove(pair.getValue1());
return pair;
})
.collect(Collectors.toMap(key -> key.getValue1(), value -> value.getValue2())); //create a map of what was removed
.stream()
.filter(key -> !comparedTo.containsKey(key)) //only get the items that are not in the comparedTo properties
.map(key -> new Pair<String, String>(String.valueOf(key), String.valueOf(from.get(key))))
.collect(Collectors.toList()) //Create a list of paired items (key value) of the items that were filtered
.stream()
.map(pair -> { //remove those pairs from the from properties
from.remove(pair.getValue1());
return pair;
})
.collect(Collectors.toMap(key -> key.getValue1(), value -> value.getValue2())); //create a map of what was removed
return results;
}
/**
* Save the properties file to disk
* <p>Save the properties file to disk.</p>
* <p><b>This method has been depecreated and could be removed in future release.<br/>Please use java.nio.Path</b></p>
* @param props Properties file to save
* @param fileToSave File to save to
* @param comment Any comments to add
*/
@Deprecated
public static void saveConfiguration(Properties props, File fileToSave, String comment) {
try(FileOutputStream fos = new FileOutputStream(fileToSave)) {
props.store(fos, comment == null ? "" : comment);
saveConfiguration(props, fileToSave.toPath(), comment);
}
/**
* <p>Save the properties file to disk.</p>
* @param props Properties file to save
* @param fileToSave File to save to
* @param comment Any comments to add
*/
public static void saveConfiguration(Properties props, Path fileToSave, String comment) {
try(OutputStream fos = Files.newOutputStream(fileToSave)) {
props.store(fos, comment == null ? "" : comment);
} catch (IOException ex) {
throw new RuntimeException(ex.getMessage(), ex);
}