001/*
002 * Copyright (c) 2022-2024 See AUTHORS file.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *   http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 */
017
018package com.github.tommyettinger.kryo.jdkgdxds;
019
020import com.esotericsoftware.kryo.Kryo;
021import com.esotericsoftware.kryo.Serializer;
022import com.esotericsoftware.kryo.io.Input;
023import com.esotericsoftware.kryo.io.Output;
024import com.github.tommyettinger.ds.ObjectIntMap;
025import com.github.tommyettinger.ds.ObjectIntOrderedMap;
026import com.github.tommyettinger.ds.OrderType;
027
028import java.util.Iterator;
029
030/**
031 * Kryo {@link Serializer} for jdkgdxds {@link ObjectIntOrderedMap}s.
032 */
033public class ObjectIntOrderedMapSerializer extends Serializer<ObjectIntOrderedMap<?>> {
034
035    private static final OrderType[] ORDER_TYPES = OrderType.values();
036
037    public ObjectIntOrderedMapSerializer() {
038        setAcceptsNull(true);
039    }
040
041    @Override
042    public void write(final Kryo kryo, final Output output, final ObjectIntOrderedMap<?> data) {
043        int length = data.size();
044        output.writeInt(length, true);
045        output.writeVarInt(data.getOrderType().ordinal(), true);
046        output.writeVarInt(data.getDefaultValue(), false);
047        for(Iterator<? extends ObjectIntMap.Entry<?>> it = new ObjectIntOrderedMap.OrderedMapEntries<>(data).iterator(); it.hasNext();) {
048            ObjectIntOrderedMap.Entry<?> ent = it.next();
049            kryo.writeClassAndObject(output, ent.key);
050            output.writeVarInt(ent.value, false);
051        }
052    }
053
054    @SuppressWarnings({"rawtypes", "unchecked", "UnnecessaryLocalVariable"})
055    @Override
056    public ObjectIntOrderedMap<?> read(final Kryo kryo, final Input input, final Class<? extends ObjectIntOrderedMap<?>> dataClass) {
057        int length = input.readInt(true);
058        ObjectIntOrderedMap<?> data = new ObjectIntOrderedMap<>(length, ORDER_TYPES[input.readVarInt(true)]);
059        data.setDefaultValue(input.readVarInt(false));
060        ObjectIntOrderedMap rawData = data;
061        for (int i = 0; i < length; i++)
062            rawData.put(kryo.readClassAndObject(input), input.readVarInt(false));
063        return data;
064    }
065
066    @SuppressWarnings({"rawtypes", "unchecked", "UnnecessaryLocalVariable"})
067    @Override
068    public ObjectIntOrderedMap<?> copy(Kryo kryo, ObjectIntOrderedMap<?> original) {
069        ObjectIntOrderedMap<?> map = new ObjectIntOrderedMap<>(original.size(), original.getLoadFactor());
070        kryo.reference(map);
071        map.setDefaultValue(original.getDefaultValue());
072        ObjectIntOrderedMap rawMap = map;
073        for(ObjectIntOrderedMap.Entry ent : original) {
074            rawMap.put(kryo.copy(ent.key), ent.value);
075        }
076        return map;
077    }
078}