2023-04-20 10:11:12 +02:00
|
|
|
package it.cavallium.datagen.nativedata;
|
2023-01-21 18:44:49 +01:00
|
|
|
|
2023-04-20 10:11:12 +02:00
|
|
|
import it.cavallium.datagen.DataUpgrader;
|
2023-01-21 18:44:49 +01:00
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
public class UpgradeUtil {
|
|
|
|
@SuppressWarnings("unchecked")
|
2023-03-03 22:43:49 +01:00
|
|
|
public static <A, B> List<B> upgradeArray(List<A> from, DataUpgrader<A, B> upgrader) {
|
2023-01-21 18:44:49 +01:00
|
|
|
Object[] array;
|
2023-04-20 20:22:07 +02:00
|
|
|
if (from.getClass() == ImmutableWrappedArrayList.class
|
|
|
|
&& ((ImmutableWrappedArrayList<?>) from).a.getClass() == Object[].class) {
|
|
|
|
array = ((ImmutableWrappedArrayList<?>) from).a;
|
2023-01-21 18:44:49 +01:00
|
|
|
} else {
|
|
|
|
array = from.toArray();
|
|
|
|
}
|
|
|
|
for (int i = 0; i < array.length; i++) {
|
|
|
|
array[i] = (B) upgrader.upgrade((A) array[i]);
|
|
|
|
}
|
|
|
|
return (ImmutableWrappedArrayList<B>) new ImmutableWrappedArrayList<>(array);
|
|
|
|
}
|
|
|
|
|
2023-03-03 22:43:49 +01:00
|
|
|
public static <A, B> B upgradeNullable(A nullableValue, DataUpgrader<A, B> upgrader) {
|
2023-01-21 18:44:49 +01:00
|
|
|
if (nullableValue == null) {
|
|
|
|
return null;
|
|
|
|
} else {
|
|
|
|
return upgrader.upgrade(nullableValue);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|