package io.grpc.internal; import com.google.common.math.LongMath; import java.text.ParseException; import java.util.List; import java.util.Map; import java.util.concurrent.TimeUnit; /* loaded from: classes6.dex */ public class JsonUtil { static final boolean $assertionsDisabled = false; private static final long DURATION_SECONDS_MAX = 315576000000L; private static final long DURATION_SECONDS_MIN = -315576000000L; private static final long NANOS_PER_SECOND = TimeUnit.SECONDS.toNanos(1); private static long saturatedAdd(long j, long j2) { long j3 = j + j2; return (((j2 ^ j) > 0L ? 1 : ((j2 ^ j) == 0L ? 0 : -1)) < 0) | ((j ^ j3) >= 0) ? j3 : ((j3 >>> 63) ^ 1) + Long.MAX_VALUE; } public static List getList(Map map, String str) { if (!map.containsKey(str)) { return null; } Object obj = map.get(str); if (!(obj instanceof List)) { throw new ClassCastException(String.format("value '%s' for key '%s' in '%s' is not List", obj, str, map)); } return (List) obj; } public static List> getListOfObjects(Map map, String str) { List list = getList(map, str); if (list == null) { return null; } return checkObjectList(list); } public static List getListOfStrings(Map map, String str) { List list = getList(map, str); if (list == null) { return null; } return checkStringList(list); } public static Map getObject(Map map, String str) { if (!map.containsKey(str)) { return null; } Object obj = map.get(str); if (!(obj instanceof Map)) { throw new ClassCastException(String.format("value '%s' for key '%s' in '%s' is not object", obj, str, map)); } return (Map) obj; } public static Double getNumberAsDouble(Map map, String str) { if (!map.containsKey(str)) { return null; } Object obj = map.get(str); if (obj instanceof Double) { return (Double) obj; } if (obj instanceof String) { try { return Double.valueOf(Double.parseDouble((String) obj)); } catch (NumberFormatException unused) { throw new IllegalArgumentException(String.format("value '%s' for key '%s' is not a double", obj, str)); } } throw new IllegalArgumentException(String.format("value '%s' for key '%s' in '%s' is not a number", obj, str, map)); } public static Integer getNumberAsInteger(Map map, String str) { if (!map.containsKey(str)) { return null; } Object obj = map.get(str); if (obj instanceof Double) { Double d = (Double) obj; int intValue = d.intValue(); if (intValue != d.doubleValue()) { throw new ClassCastException("Number expected to be integer: ".concat(String.valueOf(d))); } return Integer.valueOf(intValue); } if (obj instanceof String) { try { return Integer.valueOf(Integer.parseInt((String) obj)); } catch (NumberFormatException unused) { throw new IllegalArgumentException(String.format("value '%s' for key '%s' is not an integer", obj, str)); } } throw new IllegalArgumentException(String.format("value '%s' for key '%s' is not an integer", obj, str)); } public static Long getNumberAsLong(Map map, String str) { if (!map.containsKey(str)) { return null; } Object obj = map.get(str); if (obj instanceof Double) { Double d = (Double) obj; long longValue = d.longValue(); if (longValue != d.doubleValue()) { throw new ClassCastException("Number expected to be long: ".concat(String.valueOf(d))); } return Long.valueOf(longValue); } if (obj instanceof String) { try { return Long.valueOf(Long.parseLong((String) obj)); } catch (NumberFormatException unused) { throw new IllegalArgumentException(String.format("value '%s' for key '%s' is not a long integer", obj, str)); } } throw new IllegalArgumentException(String.format("value '%s' for key '%s' is not a long integer", obj, str)); } public static String getString(Map map, String str) { if (!map.containsKey(str)) { return null; } Object obj = map.get(str); if (!(obj instanceof String)) { throw new ClassCastException(String.format("value '%s' for key '%s' in '%s' is not String", obj, str, map)); } return (String) obj; } public static Long getStringAsDuration(Map map, String str) { String string = getString(map, str); if (string == null) { return null; } try { return Long.valueOf(parseDuration(string)); } catch (ParseException e) { throw new RuntimeException(e); } } public static Boolean getBoolean(Map map, String str) { if (!map.containsKey(str)) { return null; } Object obj = map.get(str); if (!(obj instanceof Boolean)) { throw new ClassCastException(String.format("value '%s' for key '%s' in '%s' is not Boolean", obj, str, map)); } return (Boolean) obj; } /* JADX WARN: Multi-variable type inference failed */ public static List> checkObjectList(List list) { for (int i = 0; i < list.size(); i++) { if (!(list.get(i) instanceof Map)) { throw new ClassCastException(String.format("value %s for idx %d in %s is not object", list.get(i), Integer.valueOf(i), list)); } } return list; } /* JADX WARN: Multi-variable type inference failed */ public static List checkStringList(List list) { for (int i = 0; i < list.size(); i++) { if (!(list.get(i) instanceof String)) { throw new ClassCastException(String.format("value '%s' for idx %d in '%s' is not string", list.get(i), Integer.valueOf(i), list)); } } return list; } private static long parseDuration(String str) throws ParseException { boolean z; String str2; if (str.isEmpty() || str.charAt(str.length() - 1) != 's') { throw new ParseException("Invalid duration string: ".concat(String.valueOf(str)), 0); } if (str.charAt(0) == '-') { str = str.substring(1); z = true; } else { z = false; } String substring = str.substring(0, str.length() - 1); int indexOf = substring.indexOf(46); if (indexOf != -1) { str2 = substring.substring(indexOf + 1); substring = substring.substring(0, indexOf); } else { str2 = ""; } long parseLong = Long.parseLong(substring); int parseNanos = str2.isEmpty() ? 0 : parseNanos(str2); if (parseLong < 0) { throw new ParseException("Invalid duration string: ".concat(String.valueOf(str)), 0); } if (z) { parseLong = -parseLong; parseNanos = -parseNanos; } try { return normalizedDuration(parseLong, parseNanos); } catch (IllegalArgumentException unused) { throw new ParseException("Duration value is out of range.", 0); } } private static int parseNanos(String str) throws ParseException { int i = 0; for (int i2 = 0; i2 < 9; i2++) { i *= 10; if (i2 < str.length()) { if (str.charAt(i2) < '0' || str.charAt(i2) > '9') { throw new ParseException("Invalid nanoseconds.", 0); } i += str.charAt(i2) - '0'; } } return i; } private static long normalizedDuration(long j, int i) { long j2 = i; long j3 = NANOS_PER_SECOND; if (j2 <= (-j3) || j2 >= j3) { j = LongMath.checkedAdd(j, j2 / j3); i = (int) (j2 % j3); } if (j > 0 && i < 0) { i = (int) (i + j3); j--; } if (j < 0 && i > 0) { i = (int) (i - j3); j++; } if (!durationIsValid(j, i)) { throw new IllegalArgumentException(String.format("Duration is not valid. See proto definition for valid values. Seconds (%s) must be in range [-315,576,000,000, +315,576,000,000]. Nanos (%s) must be in range [-999,999,999, +999,999,999]. Nanos must have the same sign as seconds", Long.valueOf(j), Integer.valueOf(i))); } return saturatedAdd(TimeUnit.SECONDS.toNanos(j), i); } private static boolean durationIsValid(long j, int i) { if (j < DURATION_SECONDS_MIN || j > DURATION_SECONDS_MAX) { return false; } long j2 = i; if (j2 < -999999999 || j2 >= NANOS_PER_SECOND) { return false; } if (j < 0 || i < 0) { return j <= 0 && i <= 0; } return true; } }