milingo
2/17/2014 - 11:47 PM

Java

Java

import java.text.MessageFormat;

String pattern = "I have {0} apples";
String message = MessageFormat.format(pattern, 3);
public <T extends Shape> void addIfPretty(List<T> shapes, T shape) {
    if (shape.isPretty()) {
       shapes.add(shape);
    }
}
public class CollectionUtils {

    /**
     * Check the intersection between list1 and list2 is not empty.
     *
     * @param list1
     * @param list2
     * @param f     the function to
     * @return whether the intersection is empty.
     */
    public static Boolean emptyIntersection(Collection<? extends Object> list1, Collection<? extends Object> list2, Predicate<? super Object> f) {
        return !list1.parallelStream().anyMatch(f);
    }

    /**
     * Check the intersection between list1 and list2 is not empty.
     *
     * @param list1
     * @param list2
     * @return whether the intersection is empty.
     */
    public static Boolean emptyIntersection(Collection<? extends Object> list1, Collection<? extends Object> list2) {
        return emptyIntersection(list1, list2, list2::contains);
    }

    /**
     * Check if the first collection contains at least one element which is not in the second collection.
     *
     * @param list1
     * @param list2
     * @return
     */
    public static Boolean hasDifferentElements(Collection<? extends Object> list1, Collection<? extends Object> list2, Predicate<? super Object> f) {
        return list1.parallelStream().anyMatch(f);
    }

    /**
     * Check if the first collection contains at least one element which is not in the second collection.
     *
     * @param list1
     * @param list2
     * @return
     */
    public static Boolean hasDifferentElements(Collection<? extends Object> list1, Collection<? extends Object> list2) {
        return hasDifferentElements(list1, list2, item -> !list2.contains(item));
    }

    public static <T> boolean isEmpty(Collection<T> list) {
        return list == null || list.isEmpty();
    }

    public static void addIfNotNull(List list, Object item) {
        if (item != null) {
            list.add(item);
        }
    }

    public static <T> Optional<T> findFirstNotNull(List<T> list) {
        return list.stream().filter(x -> x != null).findFirst();
    }

    public static Optional<String> findFirstNotNullOrEmpty(List<String> list) {
        return list.stream().filter(x -> !StringUtils.isNullOrEmpty(x)).findFirst();
    }

}

// Init
new String[]{"a", "b"}

List<String> list =  new ArrayList<String>(
   Arrays.asList("String A", "String B", "String C")
);  

Map<String, Owner> owners = new HashMap<String, Owner>() {{
    put("no-money", createOwnerWithoutMoney());
}};


// convert
new HashSet<>(Arrays.asList(new String[]{"a", "b"}));


// utils
public static <T> boolean any(Collection<T> collection, ConditionalFunction<T> f) {
    if (collection != null) {
        for (T e : collection) {
             if (f.fulfills(e)) {
                 return true;
            }
         }
     }
    return false;  
}

public static <T> boolean isEmpty(Collection<T> list) {
    return list == null || list.isEmpty();
}


// utils tests
public void testAny(List<Integer> numbers, boolean expected) throws Exception {
    ConditionalFunction<Integer> isEven = new ConditionalFunction<Integer>() {
        public boolean fulfills(Integer number) {
            return number % 2 == 0;
        }
    };
    assertTrue(CollectionUtils.any(numbers, isEven) == expected);
}