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.io.Input; 022import com.esotericsoftware.kryo.io.Output; 023import com.esotericsoftware.kryo.serializers.CollectionSerializer; 024import com.github.tommyettinger.ds.HolderSet; 025import com.github.tommyettinger.function.ObjToObjFunction; 026 027import java.util.NoSuchElementException; 028 029public class HolderSetSerializer extends CollectionSerializer<HolderSet<?, ?>> { 030 public HolderSetSerializer() { 031 super(); 032 setElementsCanBeNull(false); 033 } 034 035 @Override 036 protected void writeHeader(Kryo kryo, Output output, HolderSet<?, ?> collection) { 037 ObjToObjFunction<?, ?> ext = collection.getExtractor(); 038 if(ext == null) 039 throw new NoSuchElementException("A HolderSet must have a non-null extractor to be serialized."); 040 if(kryo.getClassResolver().getRegistration(ext.getClass()) == null) 041 kryo.register(ext.getClass()); 042 kryo.writeClassAndObject(output, ext); 043 } 044 045 @SuppressWarnings({"unchecked", "rawtypes"}) 046 @Override 047 protected HolderSet<?, ?> create(Kryo kryo, Input input, Class<? extends HolderSet<?, ?>> type, int size) { 048 return new HolderSet<>((ObjToObjFunction)kryo.readClassAndObject(input), size); 049 } 050 051 @SuppressWarnings("DataFlowIssue") 052 @Override 053 protected HolderSet<?, ?> createCopy(Kryo kryo, HolderSet<?, ?> original) { 054 return new HolderSet<>(original.getExtractor(), original.size(), original.getLoadFactor()); 055 } 056}