3

I must be missing something obvious but I can't manage to serialize a TreeBasedTable. It is marked as @GwtCompatible(serializable = true), so my understanding is that I need to use the guava-gwt library to (de-)serialize it.

But I can't find do that. A contrived code example would be extremely appreciated.

For information, my pom contains guava and guava-gwt, both version 14.0.

EDIT

So thanks to the answer, I now understand that TreeBasedTable is serializable. So I have removed all the gwt references and got it to work. However, this code still fails (which is the code that made me think that TreeBasedTable was not serializable) - so I guess the problem is with the custom Comparators...

public static void main(String[] args) throws Exception {
    //with the following table it works
    //Table<Integer, String, Object> table = TreeBasedTable.create();

    //but with this one, it fails
    Table<Integer, String, Object> table = TreeBasedTable.create(new Comparator<Integer>() {
        @Override
        public int compare(Integer o1, Integer o2) {
            return o1.compareTo(o2);
        }
    }, String.CASE_INSENSITIVE_ORDER);

    table.put(1, "s", 123);

    try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(Paths.get("c:/temp/test").
            toFile()));) {
        oos.writeObject(table);
    }

    Table<Integer, String, Object> saved = null;

    try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(Paths.get("c:/temp/test").
            toFile()));) {
        saved = (Table<Integer, String, Object>) ois.readObject();
    }

    System.out.println(table.equals(saved));
    Files.delete(Paths.get("C:/temp/test"));
}
assylias
  • 321,522
  • 82
  • 660
  • 783
  • I ended up doing the serialization manually - still interested to know if there is an easier/more efficient way. – assylias Mar 12 '13 at 02:00
  • Can you clarify whether you're talking about Java serialization or GWT serialization? – Kevin Bourrillion Mar 12 '13 at 05:02
  • Either way, listing both guava and guava-gwt in the same pom can't be a good idea! – Kevin Bourrillion Mar 12 '13 at 05:02
  • @KevinBourrillion I'm not sure to be honest - I understand that java serialization is not directly possible and I need to "save" the table to disk. So I ended up writing a quick Java serialization method (serializing each cell individually) but would rather use something built in if possible. – assylias Mar 12 '13 at 09:35

1 Answers1

3

If you are talking about plain Java serialization, yes: TreeBasedTable is Serializable.

TreeBasedTable extends StandardRowSortedTable which extends StandardTable which implements Serializable.

BTW: a simple check would have helped you:

Serializable foo = TreeBasedTable.create();

Since the compiler doesn't complain about this line, you know that TreeBasedTable implements Serializable.


Update:

String.CASE_INSENSITIVE_ORDER implements Serializable, so all you need to do is to refactor your anonymous Integer comparator into an inner class that implements Serializable. Or: better yet, use Ordering.natural() instead, which is Serializable already.

Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
  • Not in front of the PC now - but when I tried I think I got an error due to `TreeBasedTable` not being serializable (on `oos.writeObject(treeTable);`... Will try again. – assylias Mar 12 '13 at 14:58
  • @assylias I guess the problem is that you have two versions of the same class on the classpath. You'll want to serialize the non-GWT version. Get rid of either the GWT or the non-GWT version! – Sean Patrick Floyd Mar 12 '13 at 15:00
  • OK TreeBasedTable is indeed serializable. But it seems that adding comparators breaks the "serializability". Updated the question. – assylias Mar 12 '13 at 15:12