2021-05-07 12:05:14 +02:00
|
|
|
package it.cavallium.dbengine.client.query;
|
|
|
|
|
2021-06-20 01:09:06 +02:00
|
|
|
import com.squareup.moshi.JsonAdapter;
|
|
|
|
import it.cavallium.dbengine.client.IntOpenHashSetJsonAdapter;
|
2021-05-07 12:05:14 +02:00
|
|
|
import it.cavallium.dbengine.client.query.current.CurrentVersion;
|
|
|
|
import it.cavallium.dbengine.client.query.current.data.IBasicType;
|
|
|
|
import it.cavallium.dbengine.client.query.current.data.IType;
|
2021-06-20 01:09:06 +02:00
|
|
|
import it.unimi.dsi.fastutil.booleans.BooleanList;
|
|
|
|
import it.unimi.dsi.fastutil.bytes.ByteList;
|
|
|
|
import it.unimi.dsi.fastutil.chars.CharList;
|
|
|
|
import it.unimi.dsi.fastutil.ints.IntList;
|
|
|
|
import it.unimi.dsi.fastutil.ints.IntOpenHashSet;
|
|
|
|
import it.unimi.dsi.fastutil.longs.LongList;
|
2022-02-11 15:29:30 +01:00
|
|
|
import it.unimi.dsi.fastutil.objects.Object2ObjectMap;
|
|
|
|
import it.unimi.dsi.fastutil.objects.Object2ObjectMaps;
|
|
|
|
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
|
2021-06-20 01:09:06 +02:00
|
|
|
import it.unimi.dsi.fastutil.shorts.ShortList;
|
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.HashMap;
|
2021-05-07 12:05:14 +02:00
|
|
|
import java.util.HashSet;
|
2021-06-20 01:09:06 +02:00
|
|
|
import java.util.Map;
|
2021-05-07 12:05:14 +02:00
|
|
|
import java.util.Set;
|
2021-06-20 01:09:06 +02:00
|
|
|
import org.warp.commonutils.moshi.BooleanListJsonAdapter;
|
|
|
|
import org.warp.commonutils.moshi.ByteListJsonAdapter;
|
|
|
|
import org.warp.commonutils.moshi.CharListJsonAdapter;
|
|
|
|
import org.warp.commonutils.moshi.IntListJsonAdapter;
|
|
|
|
import org.warp.commonutils.moshi.LongListJsonAdapter;
|
2021-05-07 12:05:14 +02:00
|
|
|
import org.warp.commonutils.moshi.MoshiPolymorphic;
|
2021-06-20 01:09:06 +02:00
|
|
|
import org.warp.commonutils.moshi.ShortListJsonAdapter;
|
2021-05-07 12:05:14 +02:00
|
|
|
|
|
|
|
public class QueryMoshi extends MoshiPolymorphic<IType> {
|
|
|
|
|
|
|
|
private final Set<Class<IType>> abstractClasses;
|
|
|
|
private final Set<Class<IType>> concreteClasses;
|
2021-06-20 01:09:06 +02:00
|
|
|
private final Map<Class<?>, JsonAdapter<?>> extraAdapters;
|
2021-05-07 12:05:14 +02:00
|
|
|
|
|
|
|
@SuppressWarnings({"unchecked", "RedundantCast", "rawtypes"})
|
|
|
|
public QueryMoshi() {
|
2021-07-13 23:07:39 +02:00
|
|
|
super(true, GetterStyle.RECORDS_GETTERS);
|
2021-05-07 12:05:14 +02:00
|
|
|
HashSet<Class<IType>> abstractClasses = new HashSet<>();
|
|
|
|
HashSet<Class<IType>> concreteClasses = new HashSet<>();
|
2021-06-20 01:09:06 +02:00
|
|
|
|
|
|
|
// Add all super types with their implementations
|
2021-05-07 12:05:14 +02:00
|
|
|
for (var superTypeClass : CurrentVersion.getSuperTypeClasses()) {
|
|
|
|
for (Class<? extends IBasicType> superTypeSubtypesClass : CurrentVersion.getSuperTypeSubtypesClasses(
|
|
|
|
superTypeClass)) {
|
|
|
|
concreteClasses.add((Class<IType>) (Class) superTypeSubtypesClass);
|
|
|
|
}
|
|
|
|
abstractClasses.add((Class<IType>) (Class) superTypeClass);
|
|
|
|
}
|
2021-06-20 01:09:06 +02:00
|
|
|
|
|
|
|
// Add IBasicType with all basic types
|
|
|
|
abstractClasses.add((Class<IType>) (Class) IBasicType.class);
|
|
|
|
for (BasicType basicType : BasicType.values()) {
|
|
|
|
concreteClasses.add((Class<IType>) (Class) CurrentVersion.VERSION.getClass(basicType));
|
|
|
|
}
|
|
|
|
|
2021-05-07 12:05:14 +02:00
|
|
|
this.abstractClasses = abstractClasses;
|
|
|
|
this.concreteClasses = concreteClasses;
|
2022-02-11 15:29:30 +01:00
|
|
|
Object2ObjectMap<Class<?>, JsonAdapter<?>> extraAdapters = new Object2ObjectOpenHashMap<>();
|
2021-06-20 01:09:06 +02:00
|
|
|
extraAdapters.put(BooleanList.class, new BooleanListJsonAdapter());
|
|
|
|
extraAdapters.put(ByteList.class, new ByteListJsonAdapter());
|
|
|
|
extraAdapters.put(ShortList.class, new ShortListJsonAdapter());
|
|
|
|
extraAdapters.put(CharList.class, new CharListJsonAdapter());
|
|
|
|
extraAdapters.put(IntList.class, new IntListJsonAdapter());
|
|
|
|
extraAdapters.put(LongList.class, new LongListJsonAdapter());
|
|
|
|
extraAdapters.put(IntOpenHashSet.class, new IntOpenHashSetJsonAdapter());
|
2022-02-11 15:29:30 +01:00
|
|
|
this.extraAdapters = Object2ObjectMaps.unmodifiable(extraAdapters);
|
2021-06-20 01:09:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Map<Class<?>, JsonAdapter<?>> getExtraAdapters() {
|
|
|
|
return extraAdapters;
|
2021-05-07 12:05:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected Set<Class<IType>> getAbstractClasses() {
|
|
|
|
return abstractClasses;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected Set<Class<IType>> getConcreteClasses() {
|
|
|
|
return concreteClasses;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected boolean shouldIgnoreField(String fieldName) {
|
2021-06-20 01:09:06 +02:00
|
|
|
return fieldName.contains("$");
|
2021-05-07 12:05:14 +02:00
|
|
|
}
|
|
|
|
}
|