2014-06-13 1 views
0

J'essaie de sérialiser/désérialiser un groupe de collections et j'ai du mal à lire le Set<E>. J'ai créé une nouvelle classe: CollectionTypesSerializer pour sérialiser/désérialiser toutes mes collections mais cela ne fonctionne pas. Voici un échantillon de mon code:Comment lire un ensemble avec PofSerializer

public class CollectionTypesPofSerializer implements PofSerializer { 

private static final int ID = 0; 
     // Set 
     private static final int SET1NUMBER = 1;       //Set<Number> 
     private static final int SET2MAPBASICTYPESCOLLECTION = 2;   //Set<Map<BasicTypesCollection>> 
     private static final int SET3INNERTYPESPRIMITIVEINNERTYPES = 3;  //Set<InnerTypes.PrimitiveInnerTypes> 

@Override 
public Object deserialize(PofReader reader) throws IOException { 

    CollectionTypes collection = new CollectionTypes(); 

    collection.id = reader.readInt(ID); 

    collection.set1 = (Set<Number>) reader.readCollection(SET1NUMBER, null); 
    collection.set2 = (Set<Map<BasicTypes, Collection<?>>>) reader.readCollection(SET2MAPBASICTYPESCOLLECTION, null); 
    collection.set3 = (Set<InnerTypes.PrimitiveInnerTypes>) reader.readCollection(SET3INNERTYPESPRIMITIVEINNERTYPES, null); 

    reader.readRemainder(); 
return collection; 
} 

J'ai beaucoup d'autres collections dans cette méthode et ils n'ont pas le même problème. Je sais quel est le problème, je ne peux pas utiliser reader.readCollection(...) avec Set et le casting ne suffit pas. Cependant, je ne connais pas d'autre moyen de le faire. Pouvez-vous m'aider s'il vous plaît?

Merci.

Répondre

0

J'ai résolu le problème:

collection.set1 = new HashSet<Number>(reader.readCollection(SET1NUMBER, null)); 
collection.set2 = new HashSet<Map<BasicTypes, Collection<?>>>(reader.readCollection(SET2MAPBASICTYPESCOLLECTION, null)); 
collection.set3 = new HashSet<InnerTypes.PrimitiveInnerTypes>(reader.readCollection(SET3INNERTYPESPRIMITIVEINNERTYPES, null)); 

Il était un problème de casting. Ne peut pas être fait de cette façon. Celui-ci fonctionne :)

Questions connexes