Rabbit-R1/android (non root)/java/sources/androidx/media3/datasource/cache/DefaultContentMetadata.java
2024-05-21 17:08:36 -04:00

122 lines
4.2 KiB
Java

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<String, byte[]> metadata;
public DefaultContentMetadata() {
this(Collections.emptyMap());
}
public DefaultContentMetadata(Map<String, byte[]> map) {
this.metadata = Collections.unmodifiableMap(map);
}
public DefaultContentMetadata copyWithMutationsApplied(ContentMetadataMutations contentMetadataMutations) {
Map<String, byte[]> applyMutations = applyMutations(this.metadata, contentMetadataMutations);
return isMetadataEqual(this.metadata, applyMutations) ? this : new DefaultContentMetadata(applyMutations);
}
public Set<Map.Entry<String, byte[]>> 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<String, byte[]> entry : this.metadata.entrySet()) {
i += Arrays.hashCode(entry.getValue()) ^ entry.getKey().hashCode();
}
this.hashCode = i;
}
return this.hashCode;
}
private static boolean isMetadataEqual(Map<String, byte[]> map, Map<String, byte[]> map2) {
if (map.size() != map2.size()) {
return false;
}
for (Map.Entry<String, byte[]> entry : map.entrySet()) {
if (!Arrays.equals(entry.getValue(), map2.get(entry.getKey()))) {
return false;
}
}
return true;
}
private static Map<String, byte[]> applyMutations(Map<String, byte[]> map, ContentMetadataMutations contentMetadataMutations) {
HashMap hashMap = new HashMap(map);
removeValues(hashMap, contentMetadataMutations.getRemovedValues());
addValues(hashMap, contentMetadataMutations.getEditedValues());
return hashMap;
}
private static void removeValues(HashMap<String, byte[]> hashMap, List<String> list) {
for (int i = 0; i < list.size(); i++) {
hashMap.remove(list.get(i));
}
}
private static void addValues(HashMap<String, byte[]> hashMap, Map<String, Object> map) {
for (Map.Entry<String, Object> 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();
}
}