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.ObjectLongOrderedMap;
025import com.github.tommyettinger.ds.OrderType;
026
027import java.util.Iterator;
028
029/**
030 * Kryo {@link Serializer} for jdkgdxds {@link ObjectLongOrderedMap}s.
031 */
032public class ObjectLongOrderedMapSerializer extends Serializer<ObjectLongOrderedMap<?>> {
033
034    private static final OrderType[] ORDER_TYPES = OrderType.values();
035
036    public ObjectLongOrderedMapSerializer() {
037        setAcceptsNull(true);
038    }
039
040    @Override
041    public void write(final Kryo kryo, final Output output, final ObjectLongOrderedMap<?> data) {
042        int length = data.size();
043        output.writeInt(length, true);
044        output.writeVarInt(data.getOrderType().ordinal(), true);
045        output.writeVarLong(data.getDefaultValue(), false);
046        for(Iterator<? extends ObjectLongOrderedMap.Entry<?>> it = new ObjectLongOrderedMap.OrderedMapEntries<>(data).iterator(); it.hasNext();) {
047            ObjectLongOrderedMap.Entry<?> ent = it.next();
048            kryo.writeClassAndObject(output, ent.key);
049            output.writeVarLong(ent.value, false);
050        }
051    }
052
053    @SuppressWarnings({"rawtypes", "unchecked", "UnnecessaryLocalVariable"})
054    @Override
055    public ObjectLongOrderedMap<?> read(final Kryo kryo, final Input input, final Class<? extends ObjectLongOrderedMap<?>> dataClass) {
056        int length = input.readInt(true);
057        ObjectLongOrderedMap<?> data = new ObjectLongOrderedMap<>(length, ORDER_TYPES[input.readVarInt(true)]);
058        data.setDefaultValue(input.readVarLong(false));
059        ObjectLongOrderedMap rawData = data;
060        for (int i = 0; i < length; i++)
061            rawData.put(kryo.readClassAndObject(input), input.readVarLong(false));
062        return data;
063    }
064
065    @SuppressWarnings({"rawtypes", "unchecked", "UnnecessaryLocalVariable"})
066    @Override
067    public ObjectLongOrderedMap<?> copy(Kryo kryo, ObjectLongOrderedMap<?> original) {
068        ObjectLongOrderedMap<?> map = new ObjectLongOrderedMap<>(original.size(), original.getLoadFactor());
069        kryo.reference(map);
070        map.setDefaultValue(original.getDefaultValue());
071        ObjectLongOrderedMap rawMap = map;
072        for(ObjectLongOrderedMap.Entry ent : original) {
073            rawMap.put(kryo.copy(ent.key), ent.value);
074        }
075        return map;
076    }
077}