package geniusweb.runserver; import java.util.Collection; import java.util.Collections; import java.util.HashMap; import java.util.Map; import java.util.logging.Level; import tudelft.utilities.listener.DefaultListenable; import tudelft.utilities.logging.ReportToLogger; import tudelft.utilities.logging.Reporter; import tudelft.utilities.repository.Repository; /** * Central repo for all currently running sessions. */ public class RunningNegotiationsRepo extends DefaultListenable implements Repository { private final Map negotiations; private final static RunningNegotiationsRepo instance = new RunningNegotiationsRepo( new HashMap<>(), new ReportToLogger("runserver")); private final Reporter log; /** * Only for testing and internal use * * @param newnegos the currently running negotiations */ protected RunningNegotiationsRepo(Map newnegos, Reporter reporter) { this.negotiations = newnegos; this.log = reporter; } public static RunningNegotiationsRepo instance() { return instance; } @Override public RunningNego get(String id) { return negotiations.get(id); } @Override public Collection list() { return Collections.unmodifiableCollection(negotiations.values()); } @Override public void put(RunningNego entity) { if (negotiations.containsKey(entity.getID())) { throw new IllegalArgumentException( "Negotiation " + entity.getID() + " is already running"); } negotiations.put(entity.getID(), entity); notifyListeners(entity.getID()); log.log(Level.FINEST, "started " + entity.getID()); } @Override public void remove(String id) { negotiations.remove(id); notifyListeners(id); log.log(Level.FINEST, "stopped negotiation " + id); } @Override public void replace(RunningNego entity) { if (!negotiations.containsKey(entity.getID())) { throw new IllegalArgumentException( "Negotiation " + entity.getID() + " is not running"); } negotiations.put(entity.getID(), entity); notifyListeners(entity.getID()); log.log(Level.FINEST, "replaced " + entity.getID()); } }