package com.discord.utilities.debug; import android.os.Build; import com.discord.utilities.logging.Logger; import com.discord.utilities.logging.LoggingProvider; import d0.z.d.m; import java.util.ArrayList; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.Map; import kotlin.jvm.internal.DefaultConstructorMarker; /* compiled from: DebugPrintable.kt */ public final class DebugPrintableCollection { public static final Companion Companion = new Companion(null); private static String libdiscordVersion = null; private static final int maxDebugPrintableStringLength = 524288; private final Map collection = new LinkedHashMap(); private long idCounter = 1; private final Object sync = new Object(); /* compiled from: DebugPrintable.kt */ public static final class Companion { private Companion() { } public /* synthetic */ Companion(DefaultConstructorMarker defaultConstructorMarker) { this(); } public final void initialize(String str) { m.checkNotNullParameter(str, "libdiscordVersion"); DebugPrintableCollection.access$setLibdiscordVersion$cp(str); } } public static final /* synthetic */ String access$getLibdiscordVersion$cp() { return libdiscordVersion; } public static final /* synthetic */ void access$setLibdiscordVersion$cp(String str) { libdiscordVersion = str; } public static /* synthetic */ long add$default(DebugPrintableCollection debugPrintableCollection, DebugPrintable debugPrintable, String str, int i, Object obj) { if ((i & 2) != 0) { str = null; } return debugPrintableCollection.add(debugPrintable, str); } private final void addSystemEntry(DebugPrintBuilder debugPrintBuilder) { debugPrintBuilder.appendLine("System:"); debugPrintBuilder.appendKeyValue("libdiscordVersion", libdiscordVersion); debugPrintBuilder.appendKeyValue("SDK_INT", Integer.valueOf(Build.VERSION.SDK_INT)); debugPrintBuilder.append("\n"); } private final void removeDeadReferencesLocked() { ArrayList arrayList = new ArrayList(); for (Map.Entry entry : this.collection.entrySet()) { long longValue = entry.getKey().longValue(); if (entry.getValue().getReference().get() == null) { arrayList.add(Long.valueOf(longValue)); } } for (Number number : arrayList) { this.collection.remove(Long.valueOf(number.longValue())); } } public final long add(DebugPrintable debugPrintable, String str) { long j; m.checkNotNullParameter(debugPrintable, "ref"); synchronized (this.sync) { j = this.idCounter; this.idCounter = j + 1; long j2 = 0; while (this.collection.containsKey(Long.valueOf(j))) { j = this.idCounter; this.idCounter = j + 1; j2++; if (j2 < 0) { break; } } this.collection.put(Long.valueOf(j), new DebugPrintableRef(str, debugPrintable)); removeDeadReferencesLocked(); } return j; } public final void debugPrint(StringBuilder sb) { m.checkNotNullParameter(sb, "sb"); DebugPrintBuilder debugPrintBuilder = new DebugPrintBuilder(sb); synchronized (this.sync) { addSystemEntry(debugPrintBuilder); Iterator> it = this.collection.entrySet().iterator(); while (true) { if (!it.hasNext()) { break; } Map.Entry next = it.next(); long longValue = next.getKey().longValue(); DebugPrintableRef value = next.getValue(); DebugPrintable debugPrintable = value.getReference().get(); if (debugPrintable != null) { m.checkNotNullExpressionValue(debugPrintable, "printableRef.reference.get() ?: continue"); sb.append(longValue); if (value.getTag() != null) { sb.append(' '); sb.append(value.getTag()); } sb.append(":\n"); try { debugPrintable.debugPrint(debugPrintBuilder); } catch (Exception e) { sb.append("Exception: " + e); } sb.append('\n'); if (sb.length() > 524288) { sb.delete(524288, sb.length() - 1); sb.append(" {truncated}"); break; } } } } } public final void remove(long j) { synchronized (this.sync) { if (this.collection.remove(Long.valueOf(j)) == null) { Logger logger = LoggingProvider.INSTANCE.get(); Logger.w$default(logger, "DebugPrintable", "Unable to locate tag '" + j + '\'', null, 4, null); } } } }