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