1 | package com.hammurapi.common; |
2 | |
3 | import java.lang.reflect.Array; |
4 | import java.util.ArrayList; |
5 | import java.util.Iterator; |
6 | import java.util.List; |
7 | import java.util.ServiceLoader; |
8 | |
9 | /** |
10 | * Helper class. |
11 | * @author Pavel Vlasov |
12 | * |
13 | */ |
14 | public class Util { |
15 | |
16 | /** |
17 | * Loads single instance of service. |
18 | * @param <T> |
19 | * @param type |
20 | * @return |
21 | */ |
22 | static Context loadContext() { |
23 | List<Context> parts = new ArrayList<Context>(); |
24 | Iterator<Context> it = ServiceLoader.load(Context.class, Util.class.getClassLoader()).iterator(); |
25 | while (it.hasNext()) { |
26 | parts.add(it.next()); |
27 | } |
28 | parts.add(new DefaultContext(Util.class.getClassLoader(), null)); |
29 | return new CompositeContext(parts.toArray(new Context[parts.size()])); |
30 | } |
31 | |
32 | /** |
33 | * Wraps a single object into array to avoid problems with generic arrays. |
34 | * @param <V> |
35 | * @param obj |
36 | * @return |
37 | */ |
38 | @SuppressWarnings("unchecked") |
39 | public static <V> V[] wrap(V obj) { |
40 | if (obj == null) { |
41 | return (V[]) new Object[] {null}; |
42 | } |
43 | V[] ret = (V[]) Array.newInstance(obj.getClass(), 1); |
44 | ret[0] = obj; |
45 | return ret; |
46 | } |
47 | |
48 | } |