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