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