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