All Downloads are FREE. Search and download functionalities are using the official Maven repository.

nl.vpro.domain.media.CollectionUtils Maven / Gradle / Ivy

Go to download

The basic domain classes for 'media', the core of POMS. Also, the 'update' XML bindings for it. It also contains some closely related domain classes like the enum to contain NICAM kijkwijzer settings.

There is a newer version: 8.3.1
Show newest version
package nl.vpro.domain.media;

import java.util.*;
import java.util.function.Predicate;

import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;

/**
 * Some static methods used in this package that are related to dealing with collections
 * @author Michiel Meeuwissen
 * @since 5.12
 */
public class CollectionUtils {

    private CollectionUtils() {
    }

    /**
     * Given a collection of values, and a list of object to update, updates the list, optionally creating one first.
     *
     */
    static  List updateList(@Nullable List toUpdate, @Nullable Collection values) {
        if (toUpdate != null && toUpdate == values) {
            return toUpdate;
        }
        if (toUpdate == null) {
            toUpdate = new ArrayList<>();
        } else {
            if (toUpdate.equals(values)) {
                // the object is already exactly correct, do nothing
                return toUpdate;
            }
            toUpdate.clear();
        }
        if (values != null) {
            toUpdate.addAll(values);
        }
        return toUpdate;
    }

    @SuppressWarnings("unchecked")
    static > SortedSet updateSortedSet(SortedSet toUpdate, Collection values) {
        if (toUpdate == values) {
            return toUpdate;
        }
        if (toUpdate == null) {
            toUpdate = new TreeSet<>();
            if (values != null) {
                toUpdate.addAll(values);
            }

        } else {
            if (values != null) {
                toUpdate.retainAll(values);
                for (T v : values) {
                    for (T toUpdateValue : toUpdate) {
                        if (toUpdateValue instanceof Updatable && toUpdateValue.equals(v)) {
                            ((Updatable) toUpdateValue).update(v);
                        }
                    }
                }
                toUpdate.addAll(values);
            }
        }
        return toUpdate;
    }

    @Nullable
    protected static  E getFromList(@Nullable List<@NonNull E> list) {
        if (list == null || list.isEmpty()) {
            return null;
        } else {
            return list.get(0);
        }
    }

    public static  int indexOf(List list, E o, int offset) {
        ListIterator it = list.listIterator();
        while(offset-- > 0) {
            it.next();
        }
        if (o == null) {
            while (it.hasNext())
                if (it.next()==null)
                    return it.previousIndex();
        } else {
            while (it.hasNext())
                if (o.equals(it.next()))
                    return it.previousIndex();
        }
        return -1;
    }

    public static 

> SortedSet

createIfNull(SortedSet

set) { if(set == null) { set = new TreeSet<>(); } return set; } @Nullable public static

> SortedSet

createIfNullUnlessNull(@Nullable SortedSet

set, @Nullable Object check) { if (check == null) { return null; } else { if (set == null) { set = new TreeSet<>(); } return set; } } /** * {@code null}-safe {@link Collection#contains(Object)}. {@code null} is never in the list. *

* Mainly because link java 9's Immutable lists resulting from things as {@link List#of()} throw exception if called with {@code null} * (Things like {@link Collections#unmodifiableList(List)} didn't behave like that). *

* Note that the sets in e.g. {@link nl.vpro.domain.media.support.Workflow} are not wrapped with {@link #nullSafeSet(Set)} and don't need this anymore. * * @since 7.2 * @see #nullSafeSet(Set) */ public static

boolean inCollection(@NonNull Collection<@NonNull P> col, @Nullable P element) { return element != null && col.contains(element); } /** * Wraps the given set in a new set, with the same elements. *

* The only difference will be that its {@link Set#contains(Object)} will simply return {@code false} if the argument is {@code null}. Unless the given set argument itself contains {@code null}, then also this set will contain it. * @since 7.10 */ public static

Set<@NonNull P> nullSafeSet(@NonNull final Set<@NonNull P> set) { boolean containsNull = false; try { containsNull = set.contains(null); } catch (NullPointerException npe) { // ignore } return nullSafeSet(set, containsNull); } public static

Set<@NonNull P> nullSafeSet(@NonNull final Set<@NonNull P> set, boolean containsNull) { return new AbstractSet

() { @Override public @NonNull Iterator

iterator() { return set.iterator(); } @Override public int size() { return set.size(); } @Override public boolean add(@NonNull P o) { return set.add(o); } @Override public boolean contains(@Nullable Object o) { return (o == null && containsNull) || (o != null && set.contains(o)); } }; } /** * Like {@link Collection#removeIf(Predicate)} but returns the number of removed items. * TODO it seems odd that we would be the first to want this? Guava? * @since 7.10 */ public static int removeIf(Collection collection, Predicate filter) { Objects.requireNonNull(filter); int result = 0; final Iterator each = collection.iterator(); while (each.hasNext()) { if (filter.test(each.next())) { each.remove(); result++; } } return result; } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy