0

I am trying to create a class which models a program stack. Is it possible to make this class serializable? I want to be able to use this as Akka messages. Thanks!

public class ProgramStack<T>{

    public final Queue<UnaryOperator<T>> programStack;
    private T context;

    ProgramStack(Queue<UnaryOperator<T>> programStack, T context) {
        this.programStack = programStack;
        this.context = context;
    }

    public void next() {
        UnaryOperator function = programStack.poll();
        function.apply(context);
    }

    public boolean hasNext() {
        return !programStack.isEmpty();
    }

}
Mohammadreza Khatami
  • 1,444
  • 2
  • 13
  • 27
dwong
  • 103
  • 5
  • 14

2 Answers2

1

For one you don't serialize methods you serialize objects that have behaviors. Anyway in order to be able to serialize this you must implement Serializable. All of it's member must do this. If you look at the doc https://docs.oracle.com/javase/8/docs/api/java/util/Queue.html Queue does not implement Serializable so make sure what ever object implements Queue is serializable. I don't know what T is must but that must also implement Serializable in order to serialize your object of type ProgramStack.

Javant
  • 1,009
  • 10
  • 17
  • Thanks @Javant ! I think the problem is UnaryOperator is not serializable. – dwong Jul 29 '16 at 18:58
  • 2
    A `UnaryOperator` is an interface. So is a `Queue`. All that matters is that the concrete object implements `Serializable` at some level. (Ex. `LinkedList` is `Serializable` and implements `Queue`) The duplicate answer shows how to serialize a lambda/function reference. – 4castle Jul 29 '16 at 19:03
  • @dwong All you need to do is declare `` in your class declaration, and then make sure that all objects which are added to the `Queue` structure are declared by casting to `(UnaryOperator & Serializable)` – 4castle Jul 29 '16 at 19:05
  • @4castle Thanks a lot. It works! – dwong Jul 29 '16 at 19:18
  • Interesting.. I never knew the object type was determined at run time @4castle – Javant Jul 29 '16 at 19:18
  • @Javant An object can implement more than one interface. – 4castle Jul 29 '16 at 19:27
  • @4castle i meant during serialization, I would have thought the compiler would have tried to throw an error when you tried to serialize an interface or class that didn't explicitly inherit or implement Serializable . – Javant Jul 29 '16 at 19:30
  • 3
    @4castle Not quite correct. You can't just make something serializable by casting it. What the cast you show does is provide a _target type_, which, if the cast object is a lambda or method ref, will result in an object that implements both `UnaryOperator` and `Serializable`. Alternately, you could create a new interface `SerializableUnaryOp`, which extends both `UnaryOperator` and `Serializable`, and use that as a target type. – Brian Goetz Jul 29 '16 at 21:41
  • @BrianGoetz that's a great idea. thank you! – dwong Aug 01 '16 at 00:59
-5
import java.io.Serializable;

public class ProgramStack<T> implements Serializable {

// etc etc

}
Pritam Banerjee
  • 17,953
  • 10
  • 93
  • 108
Dson
  • 1
  • 1