package com.google.gson;

import c.b.a.i;
import c.b.a.j;
import c.b.a.k;
import c.b.a.r;
import c.b.a.s;
import c.b.a.u.g;
import c.b.a.v.a;
import c.b.a.w.b;
import c.b.a.w.c;
import com.google.gson.internal.Excluder;
import com.google.gson.internal.bind.ArrayTypeAdapter;
import com.google.gson.internal.bind.CollectionTypeAdapterFactory;
import com.google.gson.internal.bind.DateTypeAdapter;
import com.google.gson.internal.bind.JsonAdapterAnnotationTypeAdapterFactory;
import com.google.gson.internal.bind.MapTypeAdapterFactory;
import com.google.gson.internal.bind.ObjectTypeAdapter;
import com.google.gson.internal.bind.ReflectiveTypeAdapterFactory;
import com.google.gson.internal.bind.SqlDateTypeAdapter;
import com.google.gson.internal.bind.TimeTypeAdapter;
import com.google.gson.internal.bind.TypeAdapters;
import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.lang.reflect.Type;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicLongArray;

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

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public static final a<?> f1548a = new a<>(Object.class);

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final ThreadLocal<Map<a<?>, FutureTypeAdapter<?>>> f1549b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public final Map<a<?>, r<?>> f1550c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public final g f1551d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public final JsonAdapterAnnotationTypeAdapterFactory f1552e;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public final List<s> f1553f;
    public final boolean g;

    /* JADX INFO: renamed from: com.google.gson.Gson$3, reason: invalid class name */
    public class AnonymousClass3 extends r<Number> {
        @Override // c.b.a.r
        public Number a(c.b.a.w.a aVar) throws IOException {
            if (aVar.v() != b.NULL) {
                return Long.valueOf(aVar.o());
            }
            aVar.r();
            return null;
        }

        @Override // c.b.a.r
        public void b(c cVar, Number number) throws IOException {
            Number number2 = number;
            if (number2 == null) {
                cVar.i();
            } else {
                cVar.q(number2.toString());
            }
        }
    }

    public static class FutureTypeAdapter<T> extends r<T> {

        /* JADX INFO: renamed from: a, reason: collision with root package name */
        public r<T> f1556a;

        @Override // c.b.a.r
        public T a(c.b.a.w.a aVar) {
            r<T> rVar = this.f1556a;
            if (rVar != null) {
                return rVar.a(aVar);
            }
            throw new IllegalStateException();
        }

        @Override // c.b.a.r
        public void b(c cVar, T t) {
            r<T> rVar = this.f1556a;
            if (rVar == null) {
                throw new IllegalStateException();
            }
            rVar.b(cVar, t);
        }
    }

    public Gson() {
        Excluder excluder = Excluder.f1558a;
        c.b.a.c cVar = c.b.a.c.f1319a;
        Map mapEmptyMap = Collections.emptyMap();
        Collections.emptyList();
        Collections.emptyList();
        List listEmptyList = Collections.emptyList();
        this.f1549b = new ThreadLocal<>();
        this.f1550c = new ConcurrentHashMap();
        this.f1551d = new g(mapEmptyMap);
        this.g = true;
        ArrayList arrayList = new ArrayList();
        arrayList.add(TypeAdapters.Y);
        arrayList.add(ObjectTypeAdapter.f1585a);
        arrayList.add(excluder);
        arrayList.addAll(listEmptyList);
        arrayList.add(TypeAdapters.D);
        arrayList.add(TypeAdapters.m);
        arrayList.add(TypeAdapters.g);
        arrayList.add(TypeAdapters.i);
        arrayList.add(TypeAdapters.k);
        final r<Number> rVar = TypeAdapters.t;
        arrayList.add(new TypeAdapters.AnonymousClass33(Long.TYPE, Long.class, rVar));
        arrayList.add(new TypeAdapters.AnonymousClass33(Double.TYPE, Double.class, new r<Number>(this) { // from class: com.google.gson.Gson.1
            @Override // c.b.a.r
            public Number a(c.b.a.w.a aVar) throws IOException {
                if (aVar.v() != b.NULL) {
                    return Double.valueOf(aVar.m());
                }
                aVar.r();
                return null;
            }

            @Override // c.b.a.r
            public void b(c cVar2, Number number) throws IOException {
                Number number2 = number;
                if (number2 == null) {
                    cVar2.i();
                } else {
                    Gson.a(number2.doubleValue());
                    cVar2.p(number2);
                }
            }
        }));
        arrayList.add(new TypeAdapters.AnonymousClass33(Float.TYPE, Float.class, new r<Number>(this) { // from class: com.google.gson.Gson.2
            @Override // c.b.a.r
            public Number a(c.b.a.w.a aVar) throws IOException {
                if (aVar.v() != b.NULL) {
                    return Float.valueOf((float) aVar.m());
                }
                aVar.r();
                return null;
            }

            @Override // c.b.a.r
            public void b(c cVar2, Number number) throws IOException {
                Number number2 = number;
                if (number2 == null) {
                    cVar2.i();
                } else {
                    Gson.a(number2.floatValue());
                    cVar2.p(number2);
                }
            }
        }));
        arrayList.add(TypeAdapters.x);
        arrayList.add(TypeAdapters.o);
        arrayList.add(TypeAdapters.q);
        arrayList.add(new TypeAdapters.AnonymousClass32(AtomicLong.class, new TypeAdapter$1(new r<AtomicLong>() { // from class: com.google.gson.Gson.4
            @Override // c.b.a.r
            public AtomicLong a(c.b.a.w.a aVar) {
                return new AtomicLong(((Number) rVar.a(aVar)).longValue());
            }

            @Override // c.b.a.r
            public void b(c cVar2, AtomicLong atomicLong) {
                rVar.b(cVar2, Long.valueOf(atomicLong.get()));
            }
        })));
        arrayList.add(new TypeAdapters.AnonymousClass32(AtomicLongArray.class, new TypeAdapter$1(new r<AtomicLongArray>() { // from class: com.google.gson.Gson.5
            @Override // c.b.a.r
            public AtomicLongArray a(c.b.a.w.a aVar) throws IOException {
                ArrayList arrayList2 = new ArrayList();
                aVar.a();
                while (aVar.i()) {
                    arrayList2.add(Long.valueOf(((Number) rVar.a(aVar)).longValue()));
                }
                aVar.e();
                int size = arrayList2.size();
                AtomicLongArray atomicLongArray = new AtomicLongArray(size);
                for (int i = 0; i < size; i++) {
                    atomicLongArray.set(i, ((Long) arrayList2.get(i)).longValue());
                }
                return atomicLongArray;
            }

            @Override // c.b.a.r
            public void b(c cVar2, AtomicLongArray atomicLongArray) throws IOException {
                AtomicLongArray atomicLongArray2 = atomicLongArray;
                cVar2.b();
                int length = atomicLongArray2.length();
                for (int i = 0; i < length; i++) {
                    rVar.b(cVar2, Long.valueOf(atomicLongArray2.get(i)));
                }
                cVar2.e();
            }
        })));
        arrayList.add(TypeAdapters.s);
        arrayList.add(TypeAdapters.z);
        arrayList.add(TypeAdapters.F);
        arrayList.add(TypeAdapters.H);
        arrayList.add(new TypeAdapters.AnonymousClass32(BigDecimal.class, TypeAdapters.B));
        arrayList.add(new TypeAdapters.AnonymousClass32(BigInteger.class, TypeAdapters.C));
        arrayList.add(TypeAdapters.J);
        arrayList.add(TypeAdapters.L);
        arrayList.add(TypeAdapters.P);
        arrayList.add(TypeAdapters.R);
        arrayList.add(TypeAdapters.W);
        arrayList.add(TypeAdapters.N);
        arrayList.add(TypeAdapters.f1613d);
        arrayList.add(DateTypeAdapter.f1576a);
        arrayList.add(TypeAdapters.U);
        arrayList.add(TimeTypeAdapter.f1599a);
        arrayList.add(SqlDateTypeAdapter.f1597a);
        arrayList.add(TypeAdapters.S);
        arrayList.add(ArrayTypeAdapter.f1570a);
        arrayList.add(TypeAdapters.f1611b);
        arrayList.add(new CollectionTypeAdapterFactory(this.f1551d));
        arrayList.add(new MapTypeAdapterFactory(this.f1551d, false));
        JsonAdapterAnnotationTypeAdapterFactory jsonAdapterAnnotationTypeAdapterFactory = new JsonAdapterAnnotationTypeAdapterFactory(this.f1551d);
        this.f1552e = jsonAdapterAnnotationTypeAdapterFactory;
        arrayList.add(jsonAdapterAnnotationTypeAdapterFactory);
        arrayList.add(TypeAdapters.Z);
        arrayList.add(new ReflectiveTypeAdapterFactory(this.f1551d, cVar, excluder, jsonAdapterAnnotationTypeAdapterFactory));
        this.f1553f = Collections.unmodifiableList(arrayList);
    }

    public static void a(double d2) {
        if (Double.isNaN(d2) || Double.isInfinite(d2)) {
            throw new IllegalArgumentException(d2 + " is not a valid double value as per JSON specification. To override this behavior, use GsonBuilder.serializeSpecialFloatingPointValues() method.");
        }
    }

    public <T> r<T> b(a<T> aVar) {
        r<T> rVar = (r) this.f1550c.get(aVar);
        if (rVar != null) {
            return rVar;
        }
        Map<a<?>, FutureTypeAdapter<?>> map = this.f1549b.get();
        boolean z = false;
        if (map == null) {
            map = new HashMap<>();
            this.f1549b.set(map);
            z = true;
        }
        FutureTypeAdapter<?> futureTypeAdapter = map.get(aVar);
        if (futureTypeAdapter != null) {
            return futureTypeAdapter;
        }
        try {
            FutureTypeAdapter<?> futureTypeAdapter2 = new FutureTypeAdapter<>();
            map.put(aVar, futureTypeAdapter2);
            Iterator<s> it = this.f1553f.iterator();
            while (it.hasNext()) {
                r<T> rVarA = it.next().a(this, aVar);
                if (rVarA != null) {
                    if (futureTypeAdapter2.f1556a != null) {
                        throw new AssertionError();
                    }
                    futureTypeAdapter2.f1556a = rVarA;
                    this.f1550c.put(aVar, rVarA);
                    return rVarA;
                }
            }
            throw new IllegalArgumentException("GSON (2.8.7) cannot handle " + aVar);
        } finally {
            map.remove(aVar);
            if (z) {
                this.f1549b.remove();
            }
        }
    }

    public <T> r<T> c(s sVar, a<T> aVar) {
        if (!this.f1553f.contains(sVar)) {
            sVar = this.f1552e;
        }
        boolean z = false;
        for (s sVar2 : this.f1553f) {
            if (z) {
                r<T> rVarA = sVar2.a(this, aVar);
                if (rVarA != null) {
                    return rVarA;
                }
            } else if (sVar2 == sVar) {
                z = true;
            }
        }
        throw new IllegalArgumentException("GSON cannot serialize " + aVar);
    }

    public c d(Writer writer) {
        c cVar = new c(writer);
        cVar.k = false;
        return cVar;
    }

    public String e(i iVar) {
        StringWriter stringWriter = new StringWriter();
        try {
            g(iVar, d(stringWriter));
            return stringWriter.toString();
        } catch (IOException e2) {
            throw new j(e2);
        }
    }

    public String f(Object obj) {
        if (obj == null) {
            return e(k.f1326a);
        }
        Type type = obj.getClass();
        StringWriter stringWriter = new StringWriter();
        try {
            h(obj, type, d(stringWriter));
            return stringWriter.toString();
        } catch (IOException e2) {
            throw new j(e2);
        }
    }

    public void g(i iVar, c cVar) {
        boolean z = cVar.h;
        cVar.h = true;
        boolean z2 = cVar.i;
        cVar.i = this.g;
        boolean z3 = cVar.k;
        cVar.k = false;
        try {
            try {
                TypeAdapters.X.b(cVar, iVar);
            } catch (IOException e2) {
                throw new j(e2);
            } catch (AssertionError e3) {
                AssertionError assertionError = new AssertionError("AssertionError (GSON 2.8.7): " + e3.getMessage());
                assertionError.initCause(e3);
                throw assertionError;
            }
        } finally {
            cVar.h = z;
            cVar.i = z2;
            cVar.k = z3;
        }
    }

    public void h(Object obj, Type type, c cVar) {
        r rVarB = b(new a(type));
        boolean z = cVar.h;
        cVar.h = true;
        boolean z2 = cVar.i;
        cVar.i = this.g;
        boolean z3 = cVar.k;
        cVar.k = false;
        try {
            try {
                try {
                    rVarB.b(cVar, obj);
                } catch (IOException e2) {
                    throw new j(e2);
                }
            } catch (AssertionError e3) {
                AssertionError assertionError = new AssertionError("AssertionError (GSON 2.8.7): " + e3.getMessage());
                assertionError.initCause(e3);
                throw assertionError;
            }
        } finally {
            cVar.h = z;
            cVar.i = z2;
            cVar.k = z3;
        }
    }

    public String toString() {
        return "{serializeNulls:false,factories:" + this.f1553f + ",instanceCreators:" + this.f1551d + "}";
    }
}
