Class StateSaver

java.lang.Object
nl.tudelft.simulation.language.reflection.StateSaver

public final class StateSaver extends Object
StateSaver can serialize a full state of a single object, including the fields of its superclass, to an object. Later, this object can be used to reset the full state of the object to the old value. This is useful when doing rollback in a simulation; the state of objects can be rolled back to their old values, including random number generators, which will reset their seed to the old value. Be careful with objects with shared pointers to e.g., collections, as the restoreState() on a single object might lead to the duplication of the shared objects indicated by these pointers. When the rollback is needed for an entire simulation, use XmlUtil, GSON, or another library to serialize / deserialize an entire simulation model at once.

Copyright (c) 2018 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See for project information https://simulation.tudelft.nl. The DSOL project is distributed under a three-clause BSD-style license, which can be found at https://https://simulation.tudelft.nl/dsol/docs/latest/license.html.

Author:
Alexander Verbraeck
  • Method Details

    • saveState

      public static byte[] saveState(Object object) throws DsolException
      Save the state of a single object into another object. Fields of the superclass are included. The state save is a deep copy, using the writeObject() method of serialization.
      Parameters:
      object - Object; the object to save the state from
      Returns:
      the state packed into a memory object
      Throws:
      DsolException - on serialization error
    • restoreState

      public static void restoreState(Object target, byte[] state) throws DsolException
      Retrieve the earlier saved state of a single object and write it into an object (which could be a clone or the original object). Fields of the superclass are included. The state retrieval uses all information from a deep copy, using the readObject() method of deserialization to fill the target object.
      Parameters:
      target - Object; the target object to write the deserialized information into
      state - Object; the earlier saved state to write
      Throws:
      DsolException - on deserialization error