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