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