package d.j0;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;

/* JADX INFO: Access modifiers changed from: package-private */
/* JADX INFO: compiled from: _Collections.kt */
/* JADX INFO: loaded from: classes.dex */
@d.n
public class x extends w {
    public static boolean[] A(Collection<Boolean> collection) {
        d.n0.c.s.e(collection, "<this>");
        boolean[] zArr = new boolean[collection.size()];
        Iterator<Boolean> it = collection.iterator();
        int i = 0;
        while (it.hasNext()) {
            zArr[i] = it.next().booleanValue();
            i++;
        }
        return zArr;
    }

    public static final <T, C extends Collection<? super T>> C B(Iterable<? extends T> iterable, C c2) {
        d.n0.c.s.e(iterable, "<this>");
        d.n0.c.s.e(c2, "destination");
        Iterator<? extends T> it = iterable.iterator();
        while (it.hasNext()) {
            c2.add(it.next());
        }
        return c2;
    }

    public static <T> HashSet<T> C(Iterable<? extends T> iterable) {
        d.n0.c.s.e(iterable, "<this>");
        return (HashSet) B(iterable, new HashSet(k0.b(q.j(iterable, 12))));
    }

    public static <T> List<T> D(Iterable<? extends T> iterable) {
        d.n0.c.s.e(iterable, "<this>");
        if (!(iterable instanceof Collection)) {
            return p.h(E(iterable));
        }
        Collection collection = (Collection) iterable;
        int size = collection.size();
        if (size == 0) {
            return p.d();
        }
        if (size != 1) {
            return F(collection);
        }
        return o.b(iterable instanceof List ? ((List) iterable).get(0) : iterable.iterator().next());
    }

    public static final <T> List<T> E(Iterable<? extends T> iterable) {
        d.n0.c.s.e(iterable, "<this>");
        return iterable instanceof Collection ? F((Collection) iterable) : (List) B(iterable, new ArrayList());
    }

    public static final <T> List<T> F(Collection<? extends T> collection) {
        d.n0.c.s.e(collection, "<this>");
        return new ArrayList(collection);
    }

    public static final <T> T n(Iterable<? extends T> iterable) {
        d.n0.c.s.e(iterable, "<this>");
        if (iterable instanceof List) {
            return (T) o((List) iterable);
        }
        Iterator<? extends T> it = iterable.iterator();
        if (it.hasNext()) {
            return it.next();
        }
        throw new NoSuchElementException("Collection is empty.");
    }

    public static final <T> T o(List<? extends T> list) {
        d.n0.c.s.e(list, "<this>");
        if (list.isEmpty()) {
            throw new NoSuchElementException("List is empty.");
        }
        return list.get(0);
    }

    public static final <T, A extends Appendable> A p(Iterable<? extends T> iterable, A a2, CharSequence charSequence, CharSequence charSequence2, CharSequence charSequence3, int i, CharSequence charSequence4, d.n0.b.l<? super T, ? extends CharSequence> lVar) throws IOException {
        d.n0.c.s.e(iterable, "<this>");
        d.n0.c.s.e(a2, "buffer");
        d.n0.c.s.e(charSequence, "separator");
        d.n0.c.s.e(charSequence2, "prefix");
        d.n0.c.s.e(charSequence3, "postfix");
        d.n0.c.s.e(charSequence4, "truncated");
        a2.append(charSequence2);
        int i2 = 0;
        for (T t : iterable) {
            i2++;
            if (i2 > 1) {
                a2.append(charSequence);
            }
            if (i >= 0 && i2 > i) {
                break;
            }
            d.s0.i.a(a2, t, lVar);
        }
        if (i >= 0 && i2 > i) {
            a2.append(charSequence4);
        }
        a2.append(charSequence3);
        return a2;
    }

    public static final <T> String r(Iterable<? extends T> iterable, CharSequence charSequence, CharSequence charSequence2, CharSequence charSequence3, int i, CharSequence charSequence4, d.n0.b.l<? super T, ? extends CharSequence> lVar) {
        d.n0.c.s.e(iterable, "<this>");
        d.n0.c.s.e(charSequence, "separator");
        d.n0.c.s.e(charSequence2, "prefix");
        d.n0.c.s.e(charSequence3, "postfix");
        d.n0.c.s.e(charSequence4, "truncated");
        String string = ((StringBuilder) p(iterable, new StringBuilder(), charSequence, charSequence2, charSequence3, i, charSequence4, lVar)).toString();
        d.n0.c.s.d(string, "joinTo(StringBuilder(), …ed, transform).toString()");
        return string;
    }

    public static /* synthetic */ String s(Iterable iterable, CharSequence charSequence, CharSequence charSequence2, CharSequence charSequence3, int i, CharSequence charSequence4, d.n0.b.l lVar, int i2, Object obj) {
        if ((i2 & 1) != 0) {
            charSequence = ", ";
        }
        CharSequence charSequence5 = (i2 & 2) != 0 ? "" : charSequence2;
        CharSequence charSequence6 = (i2 & 4) == 0 ? charSequence3 : "";
        if ((i2 & 8) != 0) {
            i = -1;
        }
        int i3 = i;
        if ((i2 & 16) != 0) {
            charSequence4 = "...";
        }
        CharSequence charSequence7 = charSequence4;
        if ((i2 & 32) != 0) {
            lVar = null;
        }
        return r(iterable, charSequence, charSequence5, charSequence6, i3, charSequence7, lVar);
    }

    public static <T> T t(List<? extends T> list) {
        d.n0.c.s.e(list, "<this>");
        if (list.isEmpty()) {
            throw new NoSuchElementException("List is empty.");
        }
        return list.get(p.e(list));
    }

    public static <T> T u(List<? extends T> list) {
        d.n0.c.s.e(list, "<this>");
        if (list.isEmpty()) {
            return null;
        }
        return list.get(list.size() - 1);
    }

    public static <T extends Comparable<? super T>> T v(Iterable<? extends T> iterable) {
        d.n0.c.s.e(iterable, "<this>");
        Iterator<? extends T> it = iterable.iterator();
        if (!it.hasNext()) {
            return null;
        }
        T next = it.next();
        while (it.hasNext()) {
            T next2 = it.next();
            if (next.compareTo(next2) > 0) {
                next = next2;
            }
        }
        return next;
    }

    public static <T> T w(Iterable<? extends T> iterable) {
        d.n0.c.s.e(iterable, "<this>");
        if (iterable instanceof List) {
            return (T) x((List) iterable);
        }
        Iterator<? extends T> it = iterable.iterator();
        if (!it.hasNext()) {
            throw new NoSuchElementException("Collection is empty.");
        }
        T next = it.next();
        if (it.hasNext()) {
            throw new IllegalArgumentException("Collection has more than one element.");
        }
        return next;
    }

    public static final <T> T x(List<? extends T> list) {
        d.n0.c.s.e(list, "<this>");
        int size = list.size();
        if (size == 0) {
            throw new NoSuchElementException("List is empty.");
        }
        if (size == 1) {
            return list.get(0);
        }
        throw new IllegalArgumentException("List has more than one element.");
    }

    public static <T> T y(List<? extends T> list) {
        d.n0.c.s.e(list, "<this>");
        if (list.size() == 1) {
            return list.get(0);
        }
        return null;
    }

    public static <T> List<T> z(Iterable<? extends T> iterable, int i) {
        d.n0.c.s.e(iterable, "<this>");
        int i2 = 0;
        if (!(i >= 0)) {
            throw new IllegalArgumentException(("Requested element count " + i + " is less than zero.").toString());
        }
        if (i == 0) {
            return p.d();
        }
        if (iterable instanceof Collection) {
            if (i >= ((Collection) iterable).size()) {
                return D(iterable);
            }
            if (i == 1) {
                return o.b(n(iterable));
            }
        }
        ArrayList arrayList = new ArrayList(i);
        Iterator<? extends T> it = iterable.iterator();
        while (it.hasNext()) {
            arrayList.add(it.next());
            i2++;
            if (i2 == i) {
                break;
            }
        }
        return p.h(arrayList);
    }
}
