package g;

import com.seewo.vcommons.utils.ShellCommandUtils;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import javax.annotation.Nullable;

/* JADX INFO: loaded from: classes.dex */
public final class r {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final String[] f6437a;

    public static final class a {

        /* JADX INFO: renamed from: a, reason: collision with root package name */
        public final List<String> f6438a = new ArrayList(20);

        public a a(String str, String str2) {
            r.a(str);
            r.b(str2, str);
            this.f6438a.add(str);
            this.f6438a.add(str2.trim());
            return this;
        }

        public a b(String str, String str2) {
            this.f6438a.add(str);
            this.f6438a.add(str2.trim());
            return this;
        }

        public a c(String str) {
            int i2 = 0;
            while (i2 < this.f6438a.size()) {
                if (str.equalsIgnoreCase(this.f6438a.get(i2))) {
                    this.f6438a.remove(i2);
                    this.f6438a.remove(i2);
                    i2 -= 2;
                }
                i2 += 2;
            }
            return this;
        }
    }

    public r(a aVar) {
        List<String> list = aVar.f6438a;
        this.f6437a = (String[]) list.toArray(new String[list.size()]);
    }

    public r(String[] strArr) {
        this.f6437a = strArr;
    }

    public static void a(String str) {
        Objects.requireNonNull(str, "name == null");
        if (str.isEmpty()) {
            throw new IllegalArgumentException("name is empty");
        }
        int length = str.length();
        for (int i2 = 0; i2 < length; i2++) {
            char cCharAt = str.charAt(i2);
            if (cCharAt <= ' ' || cCharAt >= 127) {
                throw new IllegalArgumentException(g.i0.c.l("Unexpected char %#04x at %d in header name: %s", Integer.valueOf(cCharAt), Integer.valueOf(i2), str));
            }
        }
    }

    public static void b(String str, String str2) {
        if (str == null) {
            throw new NullPointerException(a.b.c.a.a.q("value for name ", str2, " == null"));
        }
        int length = str.length();
        for (int i2 = 0; i2 < length; i2++) {
            char cCharAt = str.charAt(i2);
            if ((cCharAt <= 31 && cCharAt != '\t') || cCharAt >= 127) {
                throw new IllegalArgumentException(g.i0.c.l("Unexpected char %#04x at %d in %s value: %s", Integer.valueOf(cCharAt), Integer.valueOf(i2), str2, str));
            }
        }
    }

    public static r f(String... strArr) {
        if (strArr.length % 2 != 0) {
            throw new IllegalArgumentException("Expected alternating header names and values");
        }
        String[] strArr2 = (String[]) strArr.clone();
        for (int i2 = 0; i2 < strArr2.length; i2++) {
            if (strArr2[i2] == null) {
                throw new IllegalArgumentException("Headers cannot be null");
            }
            strArr2[i2] = strArr2[i2].trim();
        }
        for (int i3 = 0; i3 < strArr2.length; i3 += 2) {
            String str = strArr2[i3];
            String str2 = strArr2[i3 + 1];
            a(str);
            b(str2, str);
        }
        return new r(strArr2);
    }

    @Nullable
    public String c(String str) {
        String[] strArr = this.f6437a;
        int length = strArr.length;
        do {
            length -= 2;
            if (length < 0) {
                return null;
            }
        } while (!str.equalsIgnoreCase(strArr[length]));
        return strArr[length + 1];
    }

    public String d(int i2) {
        return this.f6437a[i2 * 2];
    }

    public a e() {
        a aVar = new a();
        Collections.addAll(aVar.f6438a, this.f6437a);
        return aVar;
    }

    public boolean equals(@Nullable Object obj) {
        return (obj instanceof r) && Arrays.equals(((r) obj).f6437a, this.f6437a);
    }

    public int g() {
        return this.f6437a.length / 2;
    }

    public String h(int i2) {
        return this.f6437a[(i2 * 2) + 1];
    }

    public int hashCode() {
        return Arrays.hashCode(this.f6437a);
    }

    public String toString() {
        StringBuilder sb = new StringBuilder();
        int iG = g();
        for (int i2 = 0; i2 < iG; i2++) {
            sb.append(d(i2));
            sb.append(": ");
            sb.append(h(i2));
            sb.append(ShellCommandUtils.COMMAND_LINE_END);
        }
        return sb.toString();
    }
}
