// 
// Decompiled by Procyon v0.6.0
// 

package io.sentry.util;

import java.util.ListIterator;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.ArrayList;
import java.util.List;
import java.util.HashMap;
import java.util.concurrent.ConcurrentHashMap;
import org.jetbrains.annotations.Nullable;
import java.util.Map;
import java.util.Iterator;
import java.util.Collection;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.ApiStatus;

@ApiStatus.Internal
public final class CollectionUtils
{
    private CollectionUtils() {
    }
    
    public static int size(@NotNull final Iterable<?> data) {
        if (data instanceof Collection) {
            return ((Collection)data).size();
        }
        int counter = 0;
        for (final Object ignored : data) {
            ++counter;
        }
        return counter;
    }
    
    @Nullable
    public static <K, V> Map<K, V> newConcurrentHashMap(@Nullable final Map<K, V> map) {
        if (map != null) {
            final Map<K, V> concurrentMap = new ConcurrentHashMap<K, V>();
            for (final Map.Entry<K, V> entry : map.entrySet()) {
                if (entry.getKey() != null && entry.getValue() != null) {
                    concurrentMap.put(entry.getKey(), entry.getValue());
                }
            }
            return concurrentMap;
        }
        return null;
    }
    
    @Nullable
    public static <K, V> Map<K, V> newHashMap(@Nullable final Map<K, V> map) {
        if (map != null) {
            return new HashMap<K, V>((Map<? extends K, ? extends V>)map);
        }
        return null;
    }
    
    @Nullable
    public static <T> List<T> newArrayList(@Nullable final List<T> list) {
        if (list != null) {
            return new ArrayList<T>((Collection<? extends T>)list);
        }
        return null;
    }
    
    @NotNull
    public static <K, V> Map<K, V> filterMapEntries(@NotNull final Map<K, V> map, @NotNull final Predicate<Map.Entry<K, V>> predicate) {
        final Map<K, V> filteredMap = new HashMap<K, V>();
        for (final Map.Entry<K, V> entry : map.entrySet()) {
            if (predicate.test(entry)) {
                filteredMap.put(entry.getKey(), entry.getValue());
            }
        }
        return filteredMap;
    }
    
    @NotNull
    public static <T, R> List<R> map(@NotNull final List<T> list, @NotNull final Mapper<T, R> f) {
        final List<R> mappedList = new ArrayList<R>(list.size());
        for (final T t : list) {
            mappedList.add(f.map(t));
        }
        return mappedList;
    }
    
    @NotNull
    public static <T> List<T> filterListEntries(@NotNull final List<T> list, @NotNull final Predicate<T> predicate) {
        final List<T> filteredList = new ArrayList<T>(list.size());
        for (final T entry : list) {
            if (predicate.test(entry)) {
                filteredList.add(entry);
            }
        }
        return filteredList;
    }
    
    public static <T> boolean contains(@NotNull final T[] array, @NotNull final T element) {
        for (final T t : array) {
            if (element.equals(t)) {
                return true;
            }
        }
        return false;
    }
    
    @NotNull
    public static <T> ListIterator<T> reverseListIterator(@NotNull final CopyOnWriteArrayList<T> list) {
        final CopyOnWriteArrayList<T> copy = new CopyOnWriteArrayList<T>((Collection<? extends T>)list);
        return copy.listIterator(copy.size());
    }
    
    public interface Predicate<T>
    {
        boolean test(final T p0);
    }
    
    public interface Mapper<T, R>
    {
        R map(final T p0);
    }
}
