73 lines
2.8 KiB
Java
73 lines
2.8 KiB
Java
package androidx.work;
|
|
|
|
import androidx.annotation.NonNull;
|
|
import androidx.work.Data;
|
|
import java.lang.reflect.Array;
|
|
import java.util.HashMap;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
/* loaded from: classes.dex */
|
|
public final class ArrayCreatingInputMerger extends InputMerger {
|
|
private Object concatenateArrayAndNonArray(Object obj, Object obj2) {
|
|
int length = Array.getLength(obj);
|
|
Object newInstance = Array.newInstance(obj2.getClass(), length + 1);
|
|
System.arraycopy(obj, 0, newInstance, 0, length);
|
|
Array.set(newInstance, length, obj2);
|
|
return newInstance;
|
|
}
|
|
|
|
private Object concatenateArrays(Object obj, Object obj2) {
|
|
int length = Array.getLength(obj);
|
|
int length2 = Array.getLength(obj2);
|
|
Object newInstance = Array.newInstance(obj.getClass().getComponentType(), length + length2);
|
|
System.arraycopy(obj, 0, newInstance, 0, length);
|
|
System.arraycopy(obj2, 0, newInstance, length, length2);
|
|
return newInstance;
|
|
}
|
|
|
|
private Object concatenateNonArrays(Object obj, Object obj2) {
|
|
Object newInstance = Array.newInstance(obj.getClass(), 2);
|
|
Array.set(newInstance, 0, obj);
|
|
Array.set(newInstance, 1, obj2);
|
|
return newInstance;
|
|
}
|
|
|
|
private Object createArrayFor(Object obj) {
|
|
Object newInstance = Array.newInstance(obj.getClass(), 1);
|
|
Array.set(newInstance, 0, obj);
|
|
return newInstance;
|
|
}
|
|
|
|
@Override // androidx.work.InputMerger
|
|
@NonNull
|
|
public Data merge(@NonNull List<Data> list) {
|
|
Data.Builder builder = new Data.Builder();
|
|
HashMap hashMap = new HashMap();
|
|
for (Data data : list) {
|
|
for (Map.Entry<String, Object> entry : data.getKeyValueMap().entrySet()) {
|
|
String key = entry.getKey();
|
|
Object value = entry.getValue();
|
|
Class<?> cls = value.getClass();
|
|
Object obj = hashMap.get(key);
|
|
if (obj != null) {
|
|
Class<?> cls2 = obj.getClass();
|
|
if (cls2.equals(cls)) {
|
|
value = cls2.isArray() ? concatenateArrays(obj, value) : concatenateNonArrays(obj, value);
|
|
} else if (cls2.isArray() && cls2.getComponentType().equals(cls)) {
|
|
value = concatenateArrayAndNonArray(obj, value);
|
|
} else if (!cls.isArray() || !cls.getComponentType().equals(cls2)) {
|
|
throw new IllegalArgumentException();
|
|
} else {
|
|
value = concatenateArrayAndNonArray(value, obj);
|
|
}
|
|
} else if (!cls.isArray()) {
|
|
value = createArrayFor(value);
|
|
}
|
|
hashMap.put(key, value);
|
|
}
|
|
}
|
|
builder.putAll(hashMap);
|
|
return builder.build();
|
|
}
|
|
}
|