/* * Copyright (c) 2015 Camil Staps */ package com.camilstaps.route66; import OO14route66.Direction; import OO14route66.Model; import java.util.logging.Level; import java.util.logging.Logger; /** * * @author camilstaps */ public class Crossing { private final Model model; private Direction allowed; private boolean waiting = false; private int requests = 0; public Crossing(Model model, Direction allowed) { this.model = model; this.allowed = allowed; } public Direction getAllowed() { return allowed; } public synchronized void doSwitch() { waiting = true; while (!model.isNoCarsOnCrossing()); System.err.println("Switching"); if (allowed == Direction.East || allowed == Direction.West) { allowed = Direction.North; } else { allowed = Direction.East; } requests = 0; System.err.println("Switched to " + allowed); waiting = false; //notifyAll(); } public boolean isAllowed(Direction direction) { return !waiting && (direction == allowed || Direction.opposite(direction) == allowed); } public boolean doRequest() { //if (++requests >= 2) { if (waiting) { return false; } doSwitch(); return true; //} //return false; } }