]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.util/src/org/argeo/util/LangUtils.java
Store and load props as properties files.
[lgpl/argeo-commons.git] / org.argeo.util / src / org / argeo / util / LangUtils.java
1 package org.argeo.util;
2
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.io.OutputStream;
6 import java.nio.file.Files;
7 import java.nio.file.Path;
8 import java.util.Dictionary;
9 import java.util.Enumeration;
10 import java.util.Hashtable;
11 import java.util.Map;
12 import java.util.Properties;
13
14 public class LangUtils {
15 /*
16 * NON-API OSGi
17 */
18 /**
19 * Returns an array with the names of the provided classes. Useful when
20 * registering services with multiple interfaces in OSGi.
21 */
22 public static String[] names(Class<?>... clzz) {
23 String[] res = new String[clzz.length];
24 for (int i = 0; i < clzz.length; i++)
25 res[i] = clzz[i].getName();
26 return res;
27 }
28
29 /*
30 * DICTIONARY
31 */
32
33 /**
34 * Creates a new {@link Dictionary} with one key-value pair (neither key not
35 * value should be null)
36 */
37 public static Dictionary<String, Object> init(String key, Object value) {
38 assert key != null;
39 assert value != null;
40 Hashtable<String, Object> props = new Hashtable<>();
41 props.put(key, value);
42 return props;
43 }
44
45 /**
46 * Wraps the keys of the provided {@link Dictionary} as an {@link Iterable}.
47 */
48 public static Iterable<String> keys(Dictionary<String, ?> props) {
49 assert props != null;
50 return new DictionaryKeys(props);
51 }
52
53 public static String toJson(Dictionary<String, ?> props) {
54 return toJson(props, false);
55 }
56
57 public static String toJson(Dictionary<String, ?> props, boolean pretty) {
58 StringBuilder sb = new StringBuilder();
59 sb.append('{');
60 if (pretty)
61 sb.append('\n');
62 Enumeration<String> keys = props.keys();
63 while (keys.hasMoreElements()) {
64 String key = keys.nextElement();
65 if (pretty)
66 sb.append(' ');
67 sb.append('\"').append(key).append('\"');
68 if (pretty)
69 sb.append(" : ");
70 else
71 sb.append(':');
72 sb.append('\"').append(props.get(key)).append('\"');
73 if (keys.hasMoreElements())
74 sb.append(", ");
75 if (pretty)
76 sb.append('\n');
77 }
78 sb.append('}');
79 return sb.toString();
80 }
81
82 public static void storeAsProperties(Dictionary<String, Object> props, Path path) throws IOException {
83 if (props == null)
84 throw new IllegalArgumentException("Props cannot be null");
85 Properties toStore = new Properties();
86 for (Enumeration<String> keys = props.keys(); keys.hasMoreElements();) {
87 String key = keys.nextElement();
88 toStore.setProperty(key, props.get(key).toString());
89 }
90 try (OutputStream out = Files.newOutputStream(path)) {
91 toStore.store(out, null);
92 }
93 }
94
95 public static Dictionary<String, Object> loadFromProperties(Path path) throws IOException {
96 Properties toLoad = new Properties();
97 try (InputStream in = Files.newInputStream(path)) {
98 toLoad.load(in);
99 }
100 Dictionary<String, Object> res = new Hashtable<String, Object>();
101 for (Object key : toLoad.keySet())
102 res.put(key.toString(), toLoad.get(key));
103 return res;
104 }
105
106 /** Singleton constructor. */
107 private LangUtils() {
108
109 }
110
111 }