]>
git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.util/src/org/argeo/util/LangUtils.java
1 package org
.argeo
.util
;
3 import java
.io
.IOException
;
4 import java
.io
.InputStream
;
5 import java
.io
.OutputStream
;
7 import java
.nio
.file
.Files
;
8 import java
.nio
.file
.Path
;
9 import java
.nio
.file
.StandardOpenOption
;
10 import java
.time
.ZonedDateTime
;
11 import java
.time
.temporal
.ChronoUnit
;
12 import java
.time
.temporal
.Temporal
;
13 import java
.util
.ArrayList
;
14 import java
.util
.Collections
;
15 import java
.util
.Dictionary
;
16 import java
.util
.Enumeration
;
17 import java
.util
.HashMap
;
18 import java
.util
.Hashtable
;
19 import java
.util
.List
;
21 import java
.util
.Properties
;
23 import javax
.naming
.InvalidNameException
;
24 import javax
.naming
.ldap
.LdapName
;
26 /** Utilities around Java basic features. */
27 public class LangUtils
{
32 * Returns an array with the names of the provided classes. Useful when
33 * registering services with multiple interfaces in OSGi.
35 public static String
[] names(Class
<?
>... clzz
) {
36 String
[] res
= new String
[clzz
.length
];
37 for (int i
= 0; i
< clzz
.length
; i
++)
38 res
[i
] = clzz
[i
].getName();
46 // * Creates a new {@link Map} with one key-value pair. Key should not be null,
47 // * but if the value is null, it returns an empty {@link Map}.
49 // * @deprecated Use {@link Collections#singletonMap(Object, Object)} instead.
52 // public static Map<String, Object> map(String key, Object value) {
53 // assert key != null;
54 // HashMap<String, Object> props = new HashMap<>();
56 // props.put(key, value);
65 * Creates a new {@link Dictionary} with one key-value pair. Key should not be
66 * null, but if the value is null, it returns an empty {@link Dictionary}.
68 public static Dictionary
<String
, Object
> dict(String key
, Object value
) {
70 Hashtable
<String
, Object
> props
= new Hashtable
<>();
72 props
.put(key
, value
);
76 /** @deprecated Use {@link #dict(String, Object)} instead. */
78 public static Dictionary
<String
, Object
> dico(String key
, Object value
) {
79 return dict(key
, value
);
82 /** Converts a {@link Dictionary} to a {@link Map} of strings. */
83 public static Map
<String
, String
> dictToStringMap(Dictionary
<String
, ?
> properties
) {
84 if (properties
== null) {
87 Map
<String
, String
> res
= new HashMap
<>(properties
.size());
88 Enumeration
<String
> keys
= properties
.keys();
89 while (keys
.hasMoreElements()) {
90 String key
= keys
.nextElement();
91 res
.put(key
, properties
.get(key
).toString());
97 * Get a string property from this map, expecting to find it, or
98 * <code>null</code> if not found.
100 public static String
get(Map
<String
, ?
> map
, String key
) {
101 Object res
= map
.get(key
);
104 return res
.toString();
108 * Get a string property from this map, expecting to find it.
110 * @throws IllegalArgumentException if the key was not found
112 public static String
getNotNull(Map
<String
, ?
> map
, String key
) {
113 Object res
= map
.get(key
);
115 throw new IllegalArgumentException("Map " + map
+ " should contain key " + key
);
116 return res
.toString();
120 * Wraps the keys of the provided {@link Dictionary} as an {@link Iterable}.
122 public static Iterable
<String
> keys(Dictionary
<String
, ?
> props
) {
123 assert props
!= null;
124 return new DictionaryKeys(props
);
127 static String
toJson(Dictionary
<String
, ?
> props
) {
128 return toJson(props
, false);
131 static String
toJson(Dictionary
<String
, ?
> props
, boolean pretty
) {
132 StringBuilder sb
= new StringBuilder();
136 Enumeration
<String
> keys
= props
.keys();
137 while (keys
.hasMoreElements()) {
138 String key
= keys
.nextElement();
141 sb
.append('\"').append(key
).append('\"');
146 sb
.append('\"').append(props
.get(key
)).append('\"');
147 if (keys
.hasMoreElements())
153 return sb
.toString();
156 static void storeAsProperties(Dictionary
<String
, Object
> props
, Path path
) throws IOException
{
158 throw new IllegalArgumentException("Props cannot be null");
159 Properties toStore
= new Properties();
160 for (Enumeration
<String
> keys
= props
.keys(); keys
.hasMoreElements();) {
161 String key
= keys
.nextElement();
162 toStore
.setProperty(key
, props
.get(key
).toString());
164 try (OutputStream out
= Files
.newOutputStream(path
)) {
165 toStore
.store(out
, null);
169 static void appendAsLdif(String dnBase
, String dnKey
, Dictionary
<String
, Object
> props
, Path path
)
172 throw new IllegalArgumentException("Props cannot be null");
173 Object dnValue
= props
.get(dnKey
);
174 String dnStr
= dnKey
+ '=' + dnValue
+ ',' + dnBase
;
177 dn
= new LdapName(dnStr
);
178 } catch (InvalidNameException e
) {
179 throw new IllegalArgumentException("Cannot interpret DN " + dnStr
, e
);
182 throw new IllegalArgumentException("DN key " + dnKey
+ " must have a value");
183 try (Writer writer
= Files
.newBufferedWriter(path
, StandardOpenOption
.APPEND
, StandardOpenOption
.CREATE
)) {
184 writer
.append("\ndn: ");
185 writer
.append(dn
.toString());
187 for (Enumeration
<String
> keys
= props
.keys(); keys
.hasMoreElements();) {
188 String key
= keys
.nextElement();
189 Object value
= props
.get(key
);
192 // FIXME deal with binary and multiple values
193 writer
.append(value
.toString());
199 static Dictionary
<String
, Object
> loadFromProperties(Path path
) throws IOException
{
200 Properties toLoad
= new Properties();
201 try (InputStream in
= Files
.newInputStream(path
)) {
204 Dictionary
<String
, Object
> res
= new Hashtable
<String
, Object
>();
205 for (Object key
: toLoad
.keySet())
206 res
.put(key
.toString(), toLoad
.get(key
));
214 * Convert a comma-separated separated {@link String} or a {@link String} array
215 * to a {@link List} of {@link String}, trimming them. Useful to quickly
216 * interpret OSGi services properties.
218 * @return a {@link List} containing the trimmed {@link String}s, or an empty
219 * {@link List} if the argument was <code>null</code>.
221 public static List
<String
> toStringList(Object value
) {
222 List
<String
> values
= new ArrayList
<>();
226 if (value
instanceof String
) {
227 arr
= ((String
) value
).split(",");
228 } else if (value
instanceof String
[]) {
229 arr
= (String
[]) value
;
231 throw new IllegalArgumentException("Unsupported value type " + value
.getClass());
233 for (String str
: arr
) {
234 values
.add(str
.trim());
243 * Chain the messages of all causes (one per line, <b>starts with a line
244 * return</b>) without all the stack
246 public static String
chainCausesMessages(Throwable t
) {
247 StringBuffer buf
= new StringBuffer();
248 chainCauseMessage(buf
, t
);
249 return buf
.toString();
252 /** Recursive chaining of messages */
253 private static void chainCauseMessage(StringBuffer buf
, Throwable t
) {
254 buf
.append('\n').append(' ').append(t
.getClass().getCanonicalName()).append(": ").append(t
.getMessage());
255 if (t
.getCause() != null)
256 chainCauseMessage(buf
, t
.getCause());
262 /** Formats time elapsed since start. */
263 public static String
since(ZonedDateTime start
) {
264 ZonedDateTime now
= ZonedDateTime
.now();
265 return duration(start
, now
);
268 /** Formats a duration. */
269 public static String
duration(Temporal start
, Temporal end
) {
270 long count
= ChronoUnit
.DAYS
.between(start
, end
);
272 return count
> 1 ? count
+ " days" : count
+ " day";
273 count
= ChronoUnit
.HOURS
.between(start
, end
);
275 return count
> 1 ? count
+ " hours" : count
+ " hours";
276 count
= ChronoUnit
.MINUTES
.between(start
, end
);
278 return count
> 1 ? count
+ " minutes" : count
+ " minute";
279 count
= ChronoUnit
.SECONDS
.between(start
, end
);
280 return count
> 1 ? count
+ " seconds" : count
+ " second";
283 /** Singleton constructor. */
284 private LangUtils() {