This discussion is archived
3 Replies Latest reply: Apr 25, 2012 9:31 AM by user738616 RSS

Enums in POF ?

810106 Newbie
Currently Being Moderated
I have a POF object called ResourceConnectionTokenPOF, inside that I have an enum 'Status' and a 'status' object of type 'enum->Status'.

However when I try to put a POF of type ResourceConnectionTokenPOF on the cache it throws me an error message, I believe it is something to do with my usage

of enum, can someone help ?

How do i incorporate enum's in my POF, is there an example somewhere.


Here is an abstract of my code:
====================

public class ResourceConnectionTokenPOF implements PortableObject {

public static enum Status {
AVAILABLE, RESERVED, DEFUNCT
}

private Status status;

public static final int STATUS = 0;

void ResourceConnectionTokenPOF() {
}

public void readExternal(PofReader reader) throws IOException {
this.status = (Status) reader.readObject(STATUS);
}

public void writeExternal(PofWriter writer) throws IOException {
writer.writeObject(STATUS, this.status);
}
}

Edited by: Ankit Asthana on Apr 25, 2012 12:46 AM
  • 1. Re: Enums in POF ?
    651263 Newbie
    Currently Being Moderated
    Enum's are not POF serializable, so you'll need to find some alternative serialization form.

    One approach is to serialize them as strings:

    public void readExternal(PofReader reader) throws IOException {
    this.status = Status.valueOf(reader.readString(STATUS));
    }

    public void writeExternal(PofWriter writer) throws IOException {
    writer.writeString(STATUS, this.status.name());
    }
  • 2. Re: Enums in POF ?
    ivan.cikic Newbie
    Currently Being Moderated
    Other way would be to leave read/writeObject methods but configure ResourceConnectionTokenPOF$Status as user type and use EnumPofSerializer (the end result will be the same).
        <user-type>
          <type-id>xyz</type-id>
          <class-name>package.ResourceConnectionTokenPOF$Status</class-name>
          <serializer>
            <class-name>com.tangosol.io.pof.EnumPofSerializer</class-name>
          </serializer>
        </user-type>
  • 3. Re: Enums in POF ?
    user738616 Pro
    Currently Being Moderated
    Hi Ankit,

    Alternatively, you can use this generic implementation for all your Enum objects:
    public class EnumSerializer implements PofSerializer {
    
    public void serialize(PofWriter writer, Object o) throws IOException {
    writer.writeString(0, ((Enum) o).name());
    writer.writeRemainder(null);
    }
    public Object deserialize(PofReader reader) throws IOException {
    PofContext pofContext = reader.getPofContext();
    Class enumType = pofContext.getClass(reader.getUserTypeId());
    Enum enumValue = Enum.valueOf(enumType, reader.readString(0));
    reader.readRemainder();
    return enumValue;
    }     
    }
    Hope this helps!

    Cheers,
    NJ

    Edited by: user738616 on Apr 25, 2012 9:31 AM

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points