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

org.aksw.commons.collections.MutableCollectionViews Maven / Gradle / Ivy

There is a newer version: 0.9.9
Show newest version
package org.aksw.commons.collections;

import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.function.Predicate;

import org.aksw.commons.collections.sets.SetFromCollection;

import com.google.common.base.Converter;

public class MutableCollectionViews {

    /**
     * Return a live-view of the given collection with conflicting elements filtered out.
     * Conflicting elements are those for which the converter raises an exception.
     *
     * @param 
     * @param backend
     * @param converter
     * @return
     */
    public static  Collection filteringCollection(Collection backend, Converter converter) {
        Predicate predicate = PredicateFromConverter.create(converter);
        Collection result = new FilteringCollection<>(backend, predicate);
        return result;
    }

    public static  Set filteringSet(Set backend, Converter converter) {
        Predicate predicate = PredicateFromConverter.create(converter);
        Set result = new FilteringSet<>(backend, predicate);
        return result;
    }


    /**
     * There is no guava Lists.filter()
     * Reason: https://stackoverflow.com/questions/8458663/guava-why-is-there-no-lists-filter-function
     *
     * @param 
     * @param backend
     * @param converter
     * @return
     */
    public static  List filteringList(List backend, Converter converter) {
        Predicate predicate = PredicateFromConverter.create(converter);
        List result = new FilteringList<>(backend, predicate);
        return result;
    }


    public static  Collection convertingCollection(Collection backend, Converter converter) {
        Collection result = new ConvertingCollection>(backend, converter);
        return result;
    }

    public static  Set convertingSet(Set backend, Converter converter, boolean isInjective) {
        Set result = isInjective
                ? new ConvertingSet>(backend, converter)
                : wrapAsSet(convertingCollection(backend, converter));

        return result;
    }

    public static  List convertingList(List backend, Converter converter) {
        List result = new ConvertingList>(backend, converter);
        return result;
    }


    public static  Set wrapAsSet(Collection collection) {
        Set result = collection instanceof Set
            ? (Set)collection
            : new SetFromCollection<>(collection);

        return result;
    }
}