]>
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
.Collection
;
15 import java
.util
.Dictionary
;
16 import java
.util
.Enumeration
;
17 import java
.util
.HashMap
;
18 import java
.util
.Hashtable
;
19 import java
.util
.Iterator
;
20 import java
.util
.List
;
22 import java
.util
.Properties
;
24 import javax
.naming
.InvalidNameException
;
25 import javax
.naming
.ldap
.LdapName
;
27 /** Utilities around Java basic features. */
28 public class LangUtils
{
33 * Returns an array with the names of the provided classes. Useful when
34 * registering services with multiple interfaces in OSGi.
36 public static String
[] names(Class
<?
>... clzz
) {
37 String
[] res
= new String
[clzz
.length
];
38 for (int i
= 0; i
< clzz
.length
; i
++)
39 res
[i
] = clzz
[i
].getName();
47 // * Creates a new {@link Map} with one key-value pair. Key should not be null,
48 // * but if the value is null, it returns an empty {@link Map}.
50 // * @deprecated Use {@link Collections#singletonMap(Object, Object)} instead.
53 // public static Map<String, Object> map(String key, Object value) {
54 // assert key != null;
55 // HashMap<String, Object> props = new HashMap<>();
57 // props.put(key, value);
66 * Creates a new {@link Dictionary} with one key-value pair. Key should not be
67 * null, but if the value is null, it returns an empty {@link Dictionary}.
69 public static Dictionary
<String
, Object
> dict(String key
, Object value
) {
71 Hashtable
<String
, Object
> props
= new Hashtable
<>();
73 props
.put(key
, value
);
77 /** @deprecated Use {@link #dict(String, Object)} instead. */
79 public static Dictionary
<String
, Object
> dico(String key
, Object value
) {
80 return dict(key
, value
);
83 /** Converts a {@link Dictionary} to a {@link Map} of strings. */
84 public static Map
<String
, String
> dictToStringMap(Dictionary
<String
, ?
> properties
) {
85 if (properties
== null) {
88 Map
<String
, String
> res
= new HashMap
<>(properties
.size());
89 Enumeration
<String
> keys
= properties
.keys();
90 while (keys
.hasMoreElements()) {
91 String key
= keys
.nextElement();
92 res
.put(key
, properties
.get(key
).toString());
98 * Get a string property from this map, expecting to find it, or
99 * <code>null</code> if not found.
101 public static String
get(Map
<String
, ?
> map
, String key
) {
102 Object res
= map
.get(key
);
105 return res
.toString();
109 * Get a string property from this map, expecting to find it.
111 * @throws IllegalArgumentException if the key was not found
113 public static String
getNotNull(Map
<String
, ?
> map
, String key
) {
114 Object res
= map
.get(key
);
116 throw new IllegalArgumentException("Map " + map
+ " should contain key " + key
);
117 return res
.toString();
121 * Wraps the keys of the provided {@link Dictionary} as an {@link Iterable}.
123 public static Iterable
<String
> keys(Dictionary
<String
, ?
> props
) {
124 assert props
!= null;
125 return new DictionaryKeys(props
);
128 static String
toJson(Dictionary
<String
, ?
> props
) {
129 return toJson(props
, false);
132 static String
toJson(Dictionary
<String
, ?
> props
, boolean pretty
) {
133 StringBuilder sb
= new StringBuilder();
137 Enumeration
<String
> keys
= props
.keys();
138 while (keys
.hasMoreElements()) {
139 String key
= keys
.nextElement();
142 sb
.append('\"').append(key
).append('\"');
147 sb
.append('\"').append(props
.get(key
)).append('\"');
148 if (keys
.hasMoreElements())
154 return sb
.toString();
157 static void storeAsProperties(Dictionary
<String
, Object
> props
, Path path
) throws IOException
{
159 throw new IllegalArgumentException("Props cannot be null");
160 Properties toStore
= new Properties();
161 for (Enumeration
<String
> keys
= props
.keys(); keys
.hasMoreElements();) {
162 String key
= keys
.nextElement();
163 toStore
.setProperty(key
, props
.get(key
).toString());
165 try (OutputStream out
= Files
.newOutputStream(path
)) {
166 toStore
.store(out
, null);
170 static void appendAsLdif(String dnBase
, String dnKey
, Dictionary
<String
, Object
> props
, Path path
)
173 throw new IllegalArgumentException("Props cannot be null");
174 Object dnValue
= props
.get(dnKey
);
175 String dnStr
= dnKey
+ '=' + dnValue
+ ',' + dnBase
;
178 dn
= new LdapName(dnStr
);
179 } catch (InvalidNameException e
) {
180 throw new IllegalArgumentException("Cannot interpret DN " + dnStr
, e
);
183 throw new IllegalArgumentException("DN key " + dnKey
+ " must have a value");
184 try (Writer writer
= Files
.newBufferedWriter(path
, StandardOpenOption
.APPEND
, StandardOpenOption
.CREATE
)) {
185 writer
.append("\ndn: ");
186 writer
.append(dn
.toString());
188 for (Enumeration
<String
> keys
= props
.keys(); keys
.hasMoreElements();) {
189 String key
= keys
.nextElement();
190 Object value
= props
.get(key
);
193 // FIXME deal with binary and multiple values
194 writer
.append(value
.toString());
200 static Dictionary
<String
, Object
> loadFromProperties(Path path
) throws IOException
{
201 Properties toLoad
= new Properties();
202 try (InputStream in
= Files
.newInputStream(path
)) {
205 Dictionary
<String
, Object
> res
= new Hashtable
<String
, Object
>();
206 for (Object key
: toLoad
.keySet())
207 res
.put(key
.toString(), toLoad
.get(key
));
215 * Convert a comma-separated separated {@link String} or a {@link String} array
216 * to a {@link List} of {@link String}, trimming them. Useful to quickly
217 * interpret OSGi services properties.
219 * @return a {@link List} containing the trimmed {@link String}s, or an empty
220 * {@link List} if the argument was <code>null</code>.
222 public static List
<String
> toStringList(Object value
) {
223 List
<String
> values
= new ArrayList
<>();
227 if (value
instanceof String
) {
228 arr
= ((String
) value
).split(",");
229 } else if (value
instanceof String
[]) {
230 arr
= (String
[]) value
;
232 throw new IllegalArgumentException("Unsupported value type " + value
.getClass());
234 for (String str
: arr
) {
235 values
.add(str
.trim());
240 /** Size of an {@link Iterable}, optimised if it is a {@link Collection}. */
241 public static int size(Iterable
<?
> iterable
) {
242 if (iterable
instanceof Collection
)
243 return ((Collection
<?
>) iterable
).size();
246 for (Iterator
<?
> it
= iterable
.iterator(); it
.hasNext(); size
++)
251 public static <T
> T
getAt(Iterable
<T
> iterable
, int index
) {
252 if (iterable
instanceof List
) {
253 List
<T
> lst
= ((List
<T
>) iterable
);
254 if (index
>= lst
.size())
255 throw new IllegalArgumentException("Index " + index
+ " is not available (size is " + lst
.size() + ")");
256 return lst
.get(index
);
259 for (Iterator
<T
> it
= iterable
.iterator(); it
.hasNext(); i
++) {
265 throw new IllegalArgumentException("Index " + index
+ " is not available (size is " + i
+ ")");
272 * Chain the messages of all causes (one per line, <b>starts with a line
273 * return</b>) without all the stack
275 public static String
chainCausesMessages(Throwable t
) {
276 StringBuffer buf
= new StringBuffer();
277 chainCauseMessage(buf
, t
);
278 return buf
.toString();
281 /** Recursive chaining of messages */
282 private static void chainCauseMessage(StringBuffer buf
, Throwable t
) {
283 buf
.append('\n').append(' ').append(t
.getClass().getCanonicalName()).append(": ").append(t
.getMessage());
284 if (t
.getCause() != null)
285 chainCauseMessage(buf
, t
.getCause());
291 /** Formats time elapsed since start. */
292 public static String
since(ZonedDateTime start
) {
293 ZonedDateTime now
= ZonedDateTime
.now();
294 return duration(start
, now
);
297 /** Formats a duration. */
298 public static String
duration(Temporal start
, Temporal end
) {
299 long count
= ChronoUnit
.DAYS
.between(start
, end
);
301 return count
> 1 ? count
+ " days" : count
+ " day";
302 count
= ChronoUnit
.HOURS
.between(start
, end
);
304 return count
> 1 ? count
+ " hours" : count
+ " hours";
305 count
= ChronoUnit
.MINUTES
.between(start
, end
);
307 return count
> 1 ? count
+ " minutes" : count
+ " minute";
308 count
= ChronoUnit
.SECONDS
.between(start
, end
);
309 return count
> 1 ? count
+ " seconds" : count
+ " second";
312 /** Singleton constructor. */
313 private LangUtils() {