Skip to content

Commit

Permalink
U.safe returns new collections, instead of shared and not modifiable.
Browse files Browse the repository at this point in the history
  • Loading branch information
nmihajlovski committed Nov 1, 2015
1 parent ea54613 commit 17eb097
Showing 1 changed file with 6 additions and 11 deletions.
17 changes: 6 additions & 11 deletions rapidoid-u/src/main/java/org/rapidoid/u/U.java
Expand Up @@ -66,8 +66,6 @@
*/
public class U {

private static final Object[] EMPTY_ARRAY = {};

public static String str(Object obj) {
if (obj == null) {
return "null";
Expand Down Expand Up @@ -548,22 +546,19 @@ public static double safe(Double num) {
}

public static Object[] safe(Object[] arr) {
return or(arr, EMPTY_ARRAY);
return arr != null ? arr : new Object[0];
}

@SuppressWarnings("unchecked")
public static <T> List<T> safe(List<T> list) {
return or(list, Collections.EMPTY_LIST);
return list != null ? list : U.<T> list();
}

@SuppressWarnings("unchecked")
public static <T> Set<T> safe(Set<T> list) {
return or(list, Collections.EMPTY_SET);
public static <T> Set<T> safe(Set<T> set) {
return set != null ? set : U.<T> set();
}

@SuppressWarnings("unchecked")
public static <K, V> Map<K, V> safe(Map<K, V> list) {
return or(list, Collections.EMPTY_MAP);
public static <K, V> Map<K, V> safe(Map<K, V> map) {
return map != null ? map : U.<K, V> map();
}

public static long time() {
Expand Down

0 comments on commit 17eb097

Please sign in to comment.