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.simplegraphs;
019
020import com.esotericsoftware.kryo.Kryo;
021import com.esotericsoftware.kryo.Serializer;
022import com.esotericsoftware.kryo.io.Input;
023import com.esotericsoftware.kryo.io.Output;
024import space.earlygrey.simplegraphs.Connection;
025import space.earlygrey.simplegraphs.DirectedGraph;
026
027import java.util.Collection;
028
029/**
030 * Kryo {@link Serializer} for simple-graphs {@link DirectedGraph}s.
031 * You should register the vertex type when you register this as the serializer for DirectedGraph.
032 */
033public class DirectedGraphSerializer extends Serializer<DirectedGraph<?>> {
034
035    public DirectedGraphSerializer() {
036        setAcceptsNull(false);
037    }
038
039    @Override
040    public void write(final Kryo kryo, final Output output, final DirectedGraph<?> data) {
041        Collection<?> vertices = data.getVertices();
042        Collection<? extends Connection<?>> edges = data.internals().getConnections();
043        int length = vertices.size();
044        output.writeInt(length, true);
045        for(Object v : vertices) {
046            kryo.writeClassAndObject(output, v);
047        }
048        length = edges.size();
049        output.writeInt(length, true);
050        for(Connection<?> e : edges) {
051            kryo.writeClassAndObject(output, e.getA());
052            kryo.writeClassAndObject(output, e.getB());
053            output.writeFloat(e.getWeight());
054        }
055    }
056
057    @SuppressWarnings({"rawtypes", "unchecked", "UnnecessaryLocalVariable"})
058    @Override
059    public DirectedGraph<?> read(final Kryo kryo, final Input input, final Class<? extends DirectedGraph<?>> dataClass) {
060        DirectedGraph<?> graph = new DirectedGraph<>();
061        DirectedGraph raw = graph;
062        int length = input.readInt(true);
063        for (int i = 0; i < length; i++) {
064            raw.addVertex(kryo.readClassAndObject(input));
065        }
066        length = input.readInt(true);
067        for (int i = 0; i < length; i++) {
068//            raw.addEdge(kryo.readClassAndObject(input), kryo.readClassAndObject(input), (WeightFunction) kryo.readClassAndObject(input));
069            raw.addEdge(kryo.readClassAndObject(input), kryo.readClassAndObject(input), input.readFloat());
070        }
071        return graph;
072    }
073
074    @SuppressWarnings({"rawtypes", "unchecked", "UnnecessaryLocalVariable"})
075    @Override
076    public DirectedGraph<?> copy(Kryo kryo, DirectedGraph<?> original) {
077        DirectedGraph<?> graph = new DirectedGraph<>();
078        DirectedGraph raw = graph;
079        Collection<?> vertices = graph.getVertices();
080        for(Object v : vertices){
081            raw.addVertex(kryo.copy(v));
082        }
083        Collection<? extends Connection<?>> edges = graph.internals().getConnections();
084        for(Connection<?> e : edges){
085            raw.addEdge(kryo.copy(e.getA()), kryo.copy(e.getB()), e.getWeight());
086        }
087        return graph;
088    }
089}