package it.cavallium.dbengine.client.query; import com.squareup.moshi.JsonAdapter; import it.cavallium.dbengine.client.IntOpenHashSetJsonAdapter; 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; 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; import it.unimi.dsi.fastutil.objects.Object2ObjectMap; import it.unimi.dsi.fastutil.objects.Object2ObjectMaps; import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap; import it.unimi.dsi.fastutil.shorts.ShortList; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Set; 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; import org.warp.commonutils.moshi.MoshiPolymorphic; import org.warp.commonutils.moshi.ShortListJsonAdapter; public class QueryMoshi extends MoshiPolymorphic { private final Set> abstractClasses; private final Set> concreteClasses; private final Map, JsonAdapter> extraAdapters; @SuppressWarnings({"unchecked", "RedundantCast", "rawtypes"}) public QueryMoshi() { super(true, GetterStyle.RECORDS_GETTERS); HashSet> abstractClasses = new HashSet<>(); HashSet> concreteClasses = new HashSet<>(); // Add all super types with their implementations for (var superTypeClass : CurrentVersion.getSuperTypeClasses()) { for (Class superTypeSubtypesClass : CurrentVersion.getSuperTypeSubtypesClasses( superTypeClass)) { concreteClasses.add((Class) (Class) superTypeSubtypesClass); } abstractClasses.add((Class) (Class) superTypeClass); } // Add IBasicType with all basic types abstractClasses.add((Class) (Class) IBasicType.class); for (BasicType basicType : BasicType.values()) { concreteClasses.add((Class) (Class) CurrentVersion.VERSION.getClass(basicType)); } this.abstractClasses = abstractClasses; this.concreteClasses = concreteClasses; Object2ObjectMap, JsonAdapter> extraAdapters = new Object2ObjectOpenHashMap<>(); 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()); this.extraAdapters = Object2ObjectMaps.unmodifiable(extraAdapters); } @Override public Map, JsonAdapter> getExtraAdapters() { return extraAdapters; } @Override protected Set> getAbstractClasses() { return abstractClasses; } @Override protected Set> getConcreteClasses() { return concreteClasses; } @Override protected boolean shouldIgnoreField(String fieldName) { return fieldName.contains("$"); } }