package network.Messages; import network.Messages.Enums.XMLMessageType; import shared.dataInput.RaceDataSource; import java.util.HashMap; import java.util.Map; import java.util.Observable; /** * This class contains a set of the latest messages received (e.g., the latest RaceStatus, the latest BoatLocation for each boat, etc...). * Currently, LatestMessage only notifies observers of change when a new XMLMessage is received. */ public class LatestMessages extends Observable { /** * The latest RaceStatus message. */ private RaceStatus raceStatus; /** * A map of the last BoatStatus message received, for each boat. */ private final Map boatStatusMap = new HashMap<>(); /** * A map of the last BoatLocation message received, for each boat. */ private final Map boatLocationMap = new HashMap<>(); /** * A map of the last MarkRounding message received, for each boat. */ private final Map markRoundingMap = new HashMap<>(); /** * The last AverageWind message received. */ private AverageWind averageWind; /** * The last CourseWinds message received. */ private CourseWinds courseWinds; /** * The latest race data XML message. */ private XMLMessage raceXMLMessage; /** * The latest boat data XML message. */ private XMLMessage boatXMLMessage; /** * The latest regatta data XML message. */ private XMLMessage regattaXMLMessage; /** * Ctor. */ public LatestMessages() { } /** * Gets the latest RaceStatus message received. * @return The latest RaceStatus message received. */ public RaceStatus getRaceStatus() { return raceStatus; } /** * Sets the latest RaceStatus message received. * @param raceStatus The new RaceStatus message to store. */ public void setRaceStatus(RaceStatus raceStatus) { this.raceStatus = raceStatus; } /** * Returns the latest BoatStatus message received for a given boat. * @param sourceID Source ID of the boat. * @return The latest BoatStatus message for the specified boat. */ public BoatStatus getBoatStatus(int sourceID) { return boatStatusMap.get(sourceID); } /** * Inserts a BoatStatus message for a given boat. * @param boatStatus The BoatStatus message to set. */ public void setBoatStatus(BoatStatus boatStatus) { boatStatusMap.put(boatStatus.getSourceID(), boatStatus); } /** * Returns the latest BoatLocation message received for a given boat. * @param sourceID Source ID of the boat. * @return The latest BoatLocation message for the specified boat. */ public BoatLocation getBoatLocation(int sourceID) { return boatLocationMap.get(sourceID); } /** * Inserts a BoatLocation message for a given boat. * @param boatLocation The BoatLocation message to set. */ public void setBoatLocation(BoatLocation boatLocation) { //TODO should compare the sequence number of the new boatLocation with the existing boatLocation for this boat (if it exists), and use the newer one. boatLocationMap.put(boatLocation.getSourceID(), boatLocation); } /** * Returns the latest MarkRounding message received for a given boat. * @param sourceID Source ID of the boat. * @return The latest MarkRounding message for the specified boat. */ public MarkRounding getMarkRounding(int sourceID) { return markRoundingMap.get(sourceID); } /** * Inserts a MarkRounding message for a given boat. * @param markRounding The MarkRounding message to set. */ public void setMarkRounding(MarkRounding markRounding) { //TODO should compare the sequence number of the new markRounding with the existing boatLocation for this boat (if it exists), and use the newer one. markRoundingMap.put(markRounding.getSourceID(), markRounding); } /** * Gets the latest AverageWind message received. * @return The latest AverageWind message received. */ public AverageWind getAverageWind() { return averageWind; } /** * Sets the latest AverageWind message received. * @param averageWind The new AverageWind message to store. */ public void setAverageWind(AverageWind averageWind) { this.averageWind = averageWind; } /** * Gets the latest CourseWinds message received. * @return The latest CourseWinds message received. */ public CourseWinds getCourseWinds() { return courseWinds; } /** * Sets the latest CourseWinds message received. * @param courseWinds The new CourseWinds message to store. */ public void setCourseWinds(CourseWinds courseWinds) { this.courseWinds = courseWinds; } /** * Returns the map of boat sourceIDs to BoatLocation messages. * @return Map between boat sourceID and BoatLocation. */ public Map getBoatLocationMap() { return boatLocationMap; } /** * Returns the map of boat sourceIDs to BoatStatus messages. * @return Map between boat sourceID and BoatStatus. */ public Map getBoatStatusMap() { return boatStatusMap; } /** * Returns the map of boat sourceIDs to MarkRounding messages. * @return Map between boat sourceID and MarkRounding. */ public Map getMarkRoundingMap() { return markRoundingMap; } /** * Returns the latest race xml message. * @return The latest race xml message. */ public XMLMessage getRaceXMLMessage() { return raceXMLMessage; } /** * Sets the latest race xml message to a specified race XML message. * @param raceXMLMessage The new race XML message to use. */ public void setRaceXMLMessage(XMLMessage raceXMLMessage) { this.raceXMLMessage = raceXMLMessage; this.setChanged(); this.notifyObservers(); } /** * Returns the latest boat xml message. * @return The latest boat xml message. */ public XMLMessage getBoatXMLMessage() { return boatXMLMessage; } /** * Sets the latest boat xml message to a specified boat XML message. * @param boatXMLMessage The new boat XML message to use. */ public void setBoatXMLMessage(XMLMessage boatXMLMessage) { this.boatXMLMessage = boatXMLMessage; this.setChanged(); this.notifyObservers(); } /** * Returns the latest regatta xml message. * @return The latest regatta xml message. */ public XMLMessage getRegattaXMLMessage() { return regattaXMLMessage; } /** * Sets the latest regatta xml message to a specified regatta XML message. * @param regattaXMLMessage The new regatta XML message to use. */ public void setRegattaXMLMessage(XMLMessage regattaXMLMessage) { this.regattaXMLMessage = regattaXMLMessage; this.setChanged(); this.notifyObservers(); } /** * Checks the type of xml message, and places it in this LatestMessages object. * @param xmlMessage The new xml message to use. */ public void setXMLMessage(XMLMessage xmlMessage) { if (xmlMessage.getXmlMsgSubType() == XMLMessageType.RACE) { this.setRaceXMLMessage(xmlMessage); } else if (xmlMessage.getXmlMsgSubType() == XMLMessageType.REGATTA) { this.setRegattaXMLMessage(xmlMessage); } else if (xmlMessage.getXmlMsgSubType() == XMLMessageType.BOAT) { this.setBoatXMLMessage(xmlMessage); } } /** * Returns whether or not there is an xml message for each message type. * @return True if race, boat, and regatta have an xml message, false otherwise. */ public boolean hasAllXMLMessages() { if (this.regattaXMLMessage == null || this.boatXMLMessage == null || this.raceXMLMessage == null) { return false; } else { return true; } } }