001/*
002 * Copyright (c) 2020-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.gdcrux;
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.crux.PointN;
025import com.github.tommyettinger.crux.PointPair;
026
027/**
028 * Kryo {@link Serializer} for crux {@link PointPair}s, which are used by gdcrux.
029 * Needs the type of the points to be registered, such as with
030 * {@link PointF2Serializer} for {@link com.github.tommyettinger.gdcrux.PointF2}.
031 */
032@SuppressWarnings({"rawtypes", "unchecked"})
033public class PointPairSerializer extends com.esotericsoftware.kryo.Serializer<PointPair> {
034    public PointPairSerializer() {
035        setImmutable(false);
036        setAcceptsNull(false);
037    }
038
039    @Override
040    public void write (Kryo kryo, Output output, PointPair object) {
041        Class valueClass = kryo.getGenerics().nextGenericClass();
042
043        if (valueClass != null && kryo.isFinal(valueClass)) {
044            Serializer serializer = kryo.getSerializer(valueClass);
045            kryo.writeObject(output, object.a, serializer);
046            kryo.writeObject(output, object.b, serializer);
047        } else {
048            kryo.writeClassAndObject(output, object.a);
049            kryo.writeClassAndObject(output, object.b);
050        }
051        kryo.getGenerics().popGenericType();
052    }
053
054    @Override
055    public PointPair read (Kryo kryo, Input input, Class<? extends PointPair> type) {
056        Class valueClass = kryo.getGenerics().nextGenericClass();
057
058        PointPair pair;
059
060        if (valueClass != null && kryo.isFinal(valueClass)) {
061            Serializer serializer = kryo.getSerializer(valueClass);
062            pair = new PointPair((PointN) kryo.readObject(input, valueClass, serializer), (PointN) kryo.readObject(input, valueClass, serializer));
063        } else
064            pair = new PointPair((PointN)kryo.readClassAndObject(input), (PointN)kryo.readClassAndObject(input));
065
066        kryo.getGenerics().popGenericType();
067        return pair;
068    }
069
070    @Override
071    public PointPair copy(Kryo kryo, PointPair original) {
072        return new PointPair(original.a, original.b);
073    }
074}