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.gand;
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.gand.Connection;
025import com.github.tommyettinger.gand.Float3UndirectedGraph;
026import com.github.tommyettinger.gdcrux.PointF3;
027
028import java.util.Collection;
029
030/**
031 * Kryo {@link Serializer} for gand {@link Float3UndirectedGraph}s.
032 * You must have {@link PointF3} registered to use this as the serializer for Float3UndirectedGraph,
033 * such as with {@link com.github.tommyettinger.kryo.gdcrux.PointF3Serializer}.
034 */
035public class Float3UndirectedGraphSerializer extends Serializer<Float3UndirectedGraph> {
036
037    public Float3UndirectedGraphSerializer() {
038        setAcceptsNull(false);
039    }
040
041    @Override
042    public void write(final Kryo kryo, final Output output, final Float3UndirectedGraph data) {
043        Collection<PointF3> vertices = data.getVertices();
044        Collection<? extends Connection<PointF3>> edges = data.internals().getConnections();
045        int length = vertices.size();
046        output.writeInt(length, true);
047        for(PointF3 v : vertices) {
048            kryo.writeObject(output, v);
049        }
050        length = edges.size();
051        output.writeInt(length, true);
052        for(Connection<PointF3> e : edges) {
053            kryo.writeObject(output, e.getA());
054            kryo.writeObject(output, e.getB());
055            output.writeFloat(e.getWeight());
056        }
057    }
058
059    @Override
060    public Float3UndirectedGraph read(final Kryo kryo, final Input input, final Class<? extends Float3UndirectedGraph> dataClass) {
061        Float3UndirectedGraph graph = new Float3UndirectedGraph();
062        int length = input.readInt(true);
063        for (int i = 0; i < length; i++) {
064            graph.addVertex(kryo.readObject(input, PointF3.class));
065        }
066        length = input.readInt(true);
067        for (int i = 0; i < length; i++) {
068            graph.addEdge(kryo.readObject(input, PointF3.class), kryo.readObject(input, PointF3.class), input.readFloat());
069        }
070        return graph;
071    }
072
073    @Override
074    public Float3UndirectedGraph copy(Kryo kryo, Float3UndirectedGraph original) {
075        Float3UndirectedGraph graph = new Float3UndirectedGraph();
076        Collection<PointF3> vertices = graph.getVertices();
077        for(PointF3 v : vertices){
078            graph.addVertex(kryo.copy(v));
079        }
080        Collection<? extends Connection<PointF3>> edges = graph.internals().getConnections();
081        for(Connection<PointF3> e : edges){
082            graph.addEdge(kryo.copy(e.getA()), kryo.copy(e.getB()), e.getWeight());
083        }
084        return graph;
085    }
086}