Make an adapter for the board to serialize the one and only inner map only

This commit is contained in:
Olof Larsson 2013-04-11 09:38:08 +02:00
parent 1ebb6a3f69
commit 2c12fb7739
3 changed files with 67 additions and 2 deletions

View File

@ -1,6 +1,7 @@
package com.massivecraft.factions;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
@ -50,8 +51,24 @@ public class Board extends Entity<Board, String> implements BoardInterface
// FIELDS
// -------------------------------------------- //
// TODO: Make TerritoryAccess immutable!
private ConcurrentSkipListMap<PS, TerritoryAccess> map = new ConcurrentSkipListMap<PS, TerritoryAccess>();
// TODO: Make TerritoryAccess immutable.
private ConcurrentSkipListMap<PS, TerritoryAccess> map;
public Map<PS, TerritoryAccess> getMap() { return Collections.unmodifiableMap(this.map); }
// -------------------------------------------- //
// CONSTRUCT
// -------------------------------------------- //
public Board()
{
this.map = new ConcurrentSkipListMap<PS, TerritoryAccess>();
}
public Board(Map<PS, TerritoryAccess> map)
{
this.map = new ConcurrentSkipListMap<PS, TerritoryAccess>(map);
}
// -------------------------------------------- //
// OVERRIDE: BOARD

View File

@ -4,6 +4,7 @@ import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Arrays;
import com.massivecraft.factions.adapters.BoardAdapter;
import com.massivecraft.factions.adapters.FFlagAdapter;
import com.massivecraft.factions.adapters.FPermAdapter;
import com.massivecraft.factions.adapters.LazyLocationAdapter;
@ -135,6 +136,7 @@ public class Factions extends MPlugin
.excludeFieldsWithModifiers(Modifier.TRANSIENT, Modifier.VOLATILE)
.registerTypeAdapter(LazyLocation.class, new LazyLocationAdapter())
.registerTypeAdapter(TerritoryAccess.class, TerritoryAccessAdapter.get())
.registerTypeAdapter(Board.class, BoardAdapter.get())
.registerTypeAdapter(Rel.class, new RelAdapter())
.registerTypeAdapter(FPerm.class, new FPermAdapter())
.registerTypeAdapter(FFlag.class, new FFlagAdapter());

View File

@ -0,0 +1,46 @@
package com.massivecraft.factions.adapters;
import java.lang.reflect.Type;
import java.util.Map;
import com.massivecraft.mcore.ps.PS;
import com.massivecraft.mcore.xlib.gson.JsonDeserializationContext;
import com.massivecraft.mcore.xlib.gson.JsonDeserializer;
import com.massivecraft.mcore.xlib.gson.JsonElement;
import com.massivecraft.mcore.xlib.gson.JsonParseException;
import com.massivecraft.mcore.xlib.gson.JsonSerializationContext;
import com.massivecraft.mcore.xlib.gson.JsonSerializer;
import com.massivecraft.mcore.xlib.gson.reflect.TypeToken;
import com.massivecraft.factions.Board;
import com.massivecraft.factions.TerritoryAccess;
public class BoardAdapter implements JsonDeserializer<Board>, JsonSerializer<Board>
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static BoardAdapter i = new BoardAdapter();
public static BoardAdapter get() { return i; }
//----------------------------------------------//
// OVERRIDE
//----------------------------------------------//
private static final Type mapType = new TypeToken<Map<PS, TerritoryAccess>>(){}.getType();
@SuppressWarnings("unchecked")
@Override
public Board deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException
{
return new Board((Map<PS, TerritoryAccess>) context.deserialize(json, mapType));
}
@Override
public JsonElement serialize(Board src, Type typeOfSrc, JsonSerializationContext context)
{
return context.serialize(src.getMap(), mapType);
}
}