/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package com.camilstaps.route66; import OO14route66.Car; import java.util.HashMap; import java.util.Map; /** * * @author pieterkoopman */ public class Overviewer { private static Overviewer instance; Map locations; protected Overviewer() { locations = new HashMap<>(); } public static Overviewer getInstance() { if (instance == null) { instance = new Overviewer(); } return instance; } public synchronized void setLocation(Car car, int location) { if (locations.containsKey(car)) { locations.remove(car); } locations.put(car, location); } public synchronized boolean isSafeLocation(Car car, int requested_location) { for (Map.Entry location : locations.entrySet()) { Car that_car = location.getKey(); if (that_car != car && that_car.getDirection() == car.getDirection() && that_car.getLocation() > requested_location && that_car.getLocation() < requested_location + Car.CARLENGTH + Car.MINCARSPACE) { return false; } } return true; } }