package androidx.media3.datasource.cache; import com.google.common.base.Charsets; import java.nio.ByteBuffer; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Set; /* loaded from: classes2.dex */ public final class DefaultContentMetadata implements ContentMetadata { public static final DefaultContentMetadata EMPTY = new DefaultContentMetadata(Collections.emptyMap()); private int hashCode; private final Map metadata; public DefaultContentMetadata() { this(Collections.emptyMap()); } public DefaultContentMetadata(Map map) { this.metadata = Collections.unmodifiableMap(map); } public DefaultContentMetadata copyWithMutationsApplied(ContentMetadataMutations contentMetadataMutations) { Map applyMutations = applyMutations(this.metadata, contentMetadataMutations); return isMetadataEqual(this.metadata, applyMutations) ? this : new DefaultContentMetadata(applyMutations); } public Set> entrySet() { return this.metadata.entrySet(); } @Override // androidx.media3.datasource.cache.ContentMetadata public final byte[] get(String str, byte[] bArr) { byte[] bArr2 = this.metadata.get(str); return bArr2 != null ? Arrays.copyOf(bArr2, bArr2.length) : bArr; } @Override // androidx.media3.datasource.cache.ContentMetadata public final String get(String str, String str2) { byte[] bArr = this.metadata.get(str); return bArr != null ? new String(bArr, Charsets.UTF_8) : str2; } @Override // androidx.media3.datasource.cache.ContentMetadata public final long get(String str, long j) { byte[] bArr = this.metadata.get(str); return bArr != null ? ByteBuffer.wrap(bArr).getLong() : j; } @Override // androidx.media3.datasource.cache.ContentMetadata public final boolean contains(String str) { return this.metadata.containsKey(str); } public boolean equals(Object obj) { if (this == obj) { return true; } if (obj == null || getClass() != obj.getClass()) { return false; } return isMetadataEqual(this.metadata, ((DefaultContentMetadata) obj).metadata); } public int hashCode() { if (this.hashCode == 0) { int i = 0; for (Map.Entry entry : this.metadata.entrySet()) { i += Arrays.hashCode(entry.getValue()) ^ entry.getKey().hashCode(); } this.hashCode = i; } return this.hashCode; } private static boolean isMetadataEqual(Map map, Map map2) { if (map.size() != map2.size()) { return false; } for (Map.Entry entry : map.entrySet()) { if (!Arrays.equals(entry.getValue(), map2.get(entry.getKey()))) { return false; } } return true; } private static Map applyMutations(Map map, ContentMetadataMutations contentMetadataMutations) { HashMap hashMap = new HashMap(map); removeValues(hashMap, contentMetadataMutations.getRemovedValues()); addValues(hashMap, contentMetadataMutations.getEditedValues()); return hashMap; } private static void removeValues(HashMap hashMap, List list) { for (int i = 0; i < list.size(); i++) { hashMap.remove(list.get(i)); } } private static void addValues(HashMap hashMap, Map map) { for (Map.Entry entry : map.entrySet()) { hashMap.put(entry.getKey(), getBytes(entry.getValue())); } } private static byte[] getBytes(Object obj) { if (obj instanceof Long) { return ByteBuffer.allocate(8).putLong(((Long) obj).longValue()).array(); } if (obj instanceof String) { return ((String) obj).getBytes(Charsets.UTF_8); } if (obj instanceof byte[]) { return (byte[]) obj; } throw new IllegalArgumentException(); } }