google / guava

Google core libraries for Java

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

open access to constuct HashBasedTable object for supporting deserializer

Dynasty-develop opened this issue · comments

1. What are you trying to do?

first build some HashBasedTable objects, then serializer them for store, eg redis.. But when i need to use them, i have a fail for deserializer them. Because the HashBasedTable cann`t expose contructure.

2. What's the best code you can write to accomplish that without the new feature?

public HashBasedTable(Map<R, Map<C, V>> backingMap);

3. What would that same code look like if we added your feature?

public HashBasedTable(Map<R, Map<C, V>> backingMap);

(Optional) What would the method signatures for your feature look like?

No response

Concrete Use Cases

I need to use this scenario in which I serializer/ deserializer the HashBasedTable object.

Packages

com.google.common.collect

Checklist

While it would be convenient to have a public constructor for HashBasedTable to support easier serialization and deserialization, it's important to note that the Guava library designers have intentionally chosen not to expose a public constructor for HashBasedTable. This design decision is likely due to the complexity and internal structure of the table, and they may have considered it safer to provide factory methods for creating instances.

If you still wish to have a public constructor for HashBasedTable, you could consider creating a wrapper class that extends HashBasedTable and exposes the constructor you need. This way, you can have a deserialization-friendly class while still keeping the internal implementation of HashBasedTable encapsulated.

Could you say a bit more about the problem you are seeing? It should normally be possible to serialize and deserialize a HashBasedTable, as verified by this test. Are we talking about something other than Java Serialization?

A code sample that shows the problem would help us understand it.

If you still wish to have a public constructor for HashBasedTable, you could consider creating a wrapper class that extends > HashBasedTable and exposes the constructor you need.

Unfortunately that isn't possible as such. HashBasedTable doesn't have a visible constructor, so it can't be subclassed. But it would be possible to make a wrapper class that contains a HashBasedTable and use that for the serialization and deserialization. The ForwardingTable class would be useful for this.

In general, these days Guava follows the guidance of Effective Java item 74 (2d edition; item 85 in the 3d edition): "Prefer alternatives to Java serialization". (Guava's "important warnings", item 4, is a consequence of this.)

Basically, Java serialization is fragile (because it depends on details of internal implementation which may change) and we don't recommend using it. This is why (for instance) the common.graph types we released several years ago don't implement Serializable.

Closing for lack of response. @Dynasty-develop feel free to reopen if there is more to say.