[Megameknet-cvs] MegaMekNET/src/common/visitor Visitorable.java,NONE,1.1 AbstractVisitor.java,NONE,1
Status: Inactive
Brought to you by:
mcwizard
From: Immanuel S. <im...@us...> - 2005-02-22 22:41:15
|
Update of /cvsroot/megameknet/MegaMekNET/src/common/visitor In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv11673/src/common/visitor Added Files: Visitorable.java AbstractVisitor.java ToXmlRpcValueVisitor.java Log Message: new visitor pattern, first used for toXmlRpcValue. Other uses will follow shortly. --- NEW FILE: Visitorable.java --- package common.visitor; /** * Classes implementing this interface are usable together with the different * Visitor classes in common.visitor. * * @author imi */ public interface Visitorable { /** * User may call this function to initalize the visitor process. The * implementing class should first call to its corresponding * visitor.visit* function and then call visit(AbstractVisitor) for * every child class it contains. */ void visit(AbstractVisitor visitor); } --- NEW FILE: ToXmlRpcValueVisitor.java --- package common.visitor; import java.util.Hashtable; import java.util.Iterator; import java.util.Vector; import common.Continent; import common.House; import common.Influences; import common.Planet; import common.PlanetEnvironment; import common.Terrain; import common.Unit; import common.UnitFactories; import common.UnitFactory; /** * Convert the object tree to an xmlrpc-compatible value format. * * @author imi */ public class ToXmlRpcValueVisitor extends AbstractVisitor { /** * The resulting object to feed the xmlrpc-convertion */ private Object result; /** * The current subobject to modify within the result-tree. */ private Object currentObject; /** * Return the result */ public Object getResult() { return result; } /** * Planets are top-level */ public void visitPlanet(Planet data) { if (result != null) throw new IllegalStateException("Planets must be the top-level types for this converter."); result = new Hashtable(); currentObject = result; Hashtable h = (Hashtable)result; h.put("id", new Integer(data.getId())); h.put("name", data.getName()); h.put("x", new Double(data.getPosition().getX())); h.put("y", new Double(data.getPosition().getY())); h.put("baysProvided", new Integer(data.getBaysProvided())); h.put("compProduction", new Integer(data.getCompProduction())); h.put("isConquerable", new Boolean(data.isConquerable())); h.put("description", data.getDescription()); } /** * UnitFactories are tag "unitFactories" in Planets. */ public void visitUnitFactories(UnitFactories data) { currentObject = new Vector(); ((Hashtable)result).put("unitFactories", currentObject); } /** * UnitFactory's are elements in UnitFactories. */ public void visitUnitFactory(UnitFactory data) { Hashtable h = new Hashtable(); ((Vector)currentObject).add(h); h.put("name", data.getName()); h.put("size", data.getSize()); h.put("founder", data.getFounder()); h.put("productionTable", data.getProductionTable()); h.put("ticksUntilRefresh", new Integer(data.getTicksUntilRefresh())); h.put("refreshSpeed", new Integer(data.getRefreshSpeed())); h.put("type", new Integer(data.getType())); } /** * Terrains are tag "terrain" in planets */ public void visitTerrain(Terrain data) { currentObject = new Vector(); ((Hashtable)result).put("terrain", currentObject); } /** * Continents are elements of Terrains */ public void visitContinent(Continent data) { Hashtable h = new Hashtable(); ((Vector)currentObject).add(h); h.put("size", new Integer(data.getSize())); h.put("environmentId", new Integer(data.getEnvironment().getId())); } /** * PlanetEnvironments are either in Continents or top-level */ public void visitPlanetEnvironment(PlanetEnvironment data) { // do only something if we are converting planet environments // directly, since pe's in planets are stored by id only. if (result == null) { result = new Hashtable(); Hashtable h = (Hashtable)result; h.put("id", new Integer(data.getId())); h.put("name", data.getName()); h.put("craterProb", new Integer(data.getCraterProb())); h.put("craterMinNum", new Integer(data.getCraterMinNum())); h.put("craterMaxNum", new Integer(data.getCraterMaxNum())); h.put("craterMinRadius", new Integer(data.getCraterMinRadius())); h.put("craterMaxRadius", new Integer(data.getCraterMaxRadius())); h.put("hillyness", new Integer(data.getHillyness())); h.put("hillElevationRange", new Integer(data.getHillElevationRange())); h.put("hillInvertProb", new Integer(data.getHillInvertProb())); h.put("waterMinSpots", new Integer(data.getWaterMinSpots())); h.put("waterMaxSpots", new Integer(data.getWaterMaxSpots())); h.put("waterMinHexes", new Integer(data.getWaterMinHexes())); h.put("waterMaxHexes", new Integer(data.getWaterMaxHexes())); h.put("waterDeepProb", new Integer(data.getWaterDeepProb())); h.put("forestMinSpots", new Integer(data.getForestMinSpots())); h.put("forestMaxSpots", new Integer(data.getForestMaxSpots())); h.put("forestMinHexes", new Integer(data.getForestMinHexes())); h.put("forestMaxHexes", new Integer(data.getForestMaxHexes())); h.put("forestHeavyProb", new Integer(data.getForestHeavyProb())); h.put("roughMinSpots", new Integer(data.getRoughMinSpots())); h.put("roughMaxSpots", new Integer(data.getRoughMaxSpots())); h.put("roughMinHexes", new Integer(data.getRoughMinHexes())); h.put("roughMaxHexes", new Integer(data.getRoughMaxHexes())); h.put("roadProb", new Integer(data.getRoadProb())); h.put("riverProb", new Integer(data.getRiverProb())); h.put("algorithm", new Integer(data.getAlgorithm())); } } /** * Influences are tag "influence" in planets. */ public void visitInfluences(Influences data) { Hashtable h = new Hashtable(); for (Iterator it = data.getHouseIds().iterator(); it.hasNext();) { Integer i = (Integer)it.next(); h.put(i.toString(), new Integer(data.getInfluence(i.intValue()))); } ((Hashtable)result).put("influence", h); } /** * Houses are top-level */ public void visitHouse(House data) { if (result != null) throw new IllegalStateException("Houses must be the top-level types for this converter."); result = new Hashtable(); Hashtable h = (Hashtable)result; h.put("id", new Integer(data.getId())); h.put("name", data.getName()); h.put("logo", data.getLogo()); h.put("baseGunner", new Integer(data.getBaseGunner())); h.put("basePilot", new Integer(data.getBasePilot())); h.put("initialHouseRanking", new Integer(data.getInitialHouseRanking())); h.put("houseColor", data.getHouseColor()); h.put("abbreviation", data.getAbbreviation()); h.put("description", data.getDescription()); h.put("conquerable", new Boolean(data.isConquerable())); h.put("inHouseAttacks", new Boolean(data.isInHouseAttacks())); h.put("mayUseBM", new Boolean(data.isMayUseBM())); h.put("motd", data.getMotd()); for (int type = 0; type < Unit.TOTALTYPES; ++type) { for (int weight = 0; weight < Unit.WEIGHT_STR.length; ++weight) { h.put("componentMod"+Unit.TYPE_STR[type]+Unit.WEIGHT_STR[weight], new Integer(data.getHouseUnitComponentMod(type, weight))); h.put("priceMod"+Unit.TYPE_STR[type]+Unit.WEIGHT_STR[weight], new Integer(data.getHouseUnitPriceMod(type, weight))); } } } } --- NEW FILE: AbstractVisitor.java --- package common.visitor; import common.Army; import common.CampaignData; import common.Continent; import common.House; import common.Influences; import common.MegaMekPilotOption; import common.Pilot; import common.PilotSkill; import common.PilotSkills; import common.Planet; import common.PlanetEnvironment; import common.Player; import common.TaskInfo; import common.Terrain; import common.Unit; import common.UnitFactories; import common.UnitFactory; /** * Interface for the visitor-pattern as described in "design patterns". * Data classes in common call member functions of this interface to do * different operations for there class hierarchy. * * @author imi */ public abstract class AbstractVisitor { public void visitArmy(Army data) {throw new IllegalStateException("conversation not allowed for this type.");} public void visitCampaignData(CampaignData data) {throw new IllegalStateException("conversation not allowed for this type.");} public void visitContinent(Continent data) {throw new IllegalStateException("conversation not allowed for this type.");} public void visitHouse(House data) {throw new IllegalStateException("conversation not allowed for this type.");} public void visitInfluences(Influences data) {throw new IllegalStateException("conversation not allowed for this type.");} public void visitMegaMekPilotOption(MegaMekPilotOption data) {throw new IllegalStateException("conversation not allowed for this type.");} public void visitPlanet(Planet data) {throw new IllegalStateException("conversation not allowed for this type.");} public void visitPlanetEnvironment(PlanetEnvironment data) {throw new IllegalStateException("conversation not allowed for this type.");} public void visitPlayer(Player data) {throw new IllegalStateException("conversation not allowed for this type.");} public void visitTaskInfo(TaskInfo data) {throw new IllegalStateException("conversation not allowed for this type.");} public void visitTerrain(Terrain data) {throw new IllegalStateException("conversation not allowed for this type.");} public void visitUnit(Unit data) {throw new IllegalStateException("conversation not allowed for this type.");} public void visitUnitFactories(UnitFactories data) {throw new IllegalStateException("conversation not allowed for this type.");} public void visitUnitFactory(UnitFactory data) {throw new IllegalStateException("conversation not allowed for this type.");} public void visitPilot(Pilot data) {throw new IllegalStateException("conversation not allowed for this type.");} public void visitPilotSkills(PilotSkills data) {throw new IllegalStateException("conversation not allowed for this type.");} public void visitPilotSkill(PilotSkill data) {throw new IllegalStateException("conversation not allowed for this type.");} } |