All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jhotdraw8.icollection.serialization.SetSerializationProxy Maven / Gradle / Ivy

/*
 * @(#)SetSerializationProxy.java
 * Copyright © 2023 The authors and contributors of JHotDraw. MIT License.
 */

package org.jhotdraw8.icollection.serialization;


import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serial;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * A serialization proxy that serializes a set independently of its internal
 * structure.
 * 

* Usage: *

 * class MySet<E> implements Set<E>, Serializable {
 *   private final static long serialVersionUID = 0L;
 *
 *   private Object writeReplace() throws ObjectStreamException {
 *      return new SerializationProxy<>(this);
 *   }
 *
 *   static class SerializationProxy<E>
 *                  extends SetSerializationProxy<E> {
 *      private final static long serialVersionUID = 0L;
 *      SerializationProxy(Set<E> target) {
 *          super(target);
 *      }
 *     {@literal @Override}
 *      protected Object readResolve() {
 *          return new MySet<>(deserializedElements);
 *      }
 *   }
 * }
 * 
*

* References: *

*
Java Object Serialization Specification: 2 - Object Output Classes, * 2.5 The writeReplace Method
*
oracle.com
* *
Java Object Serialization Specification: 3 - Object Input Classes, * 3.7 The readResolve Method
*
oracle.com
*
* * @param the element type */ public abstract class SetSerializationProxy implements Serializable { @Serial private static final long serialVersionUID = 0L; private final transient Set serialized; protected transient List deserializedElements; protected SetSerializationProxy(Set serialized) { this.serialized = serialized; } @Serial private void writeObject(ObjectOutputStream s) throws IOException { s.writeInt(serialized.size()); for (E e : serialized) { s.writeObject(e); } } @Serial private void readObject(ObjectInputStream s) throws IOException, ClassNotFoundException { int n = s.readInt(); deserializedElements = new ArrayList<>(n); for (int i = 0; i < n; i++) { @SuppressWarnings("unchecked") E e = (E) s.readObject(); deserializedElements.add(e); } } @Serial protected abstract Object readResolve(); }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy