1 package org.opentrafficsim.road.gtu.lane.tactical.util.lmrs; 2 3 import java.util.HashSet; 4 import java.util.Set; 5 6 import org.opentrafficsim.road.gtu.lane.perception.headway.HeadwayGTU; 7 8 /** 9 * Keeps data for LMRS for a specific GTU. 10 * <p> 11 * Copyright (c) 2013-2016 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. 12 * <br> 13 * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>. 14 * <p> 15 * @version $Revision$, $LastChangedDate$, by $Author$, initial version 8 nov. 2016 <br> 16 * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a> 17 * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a> 18 * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a> 19 */ 20 public final class LmrsData 21 { 22 23 /** Form of synchronization. */ 24 private final Synchronization synchronization; 25 26 /** Most recent leaders. */ 27 private final Set<String> leaders = new HashSet<>(); 28 29 /** Current leaders. */ 30 private final Set<String> tempLeaders = new HashSet<>(); 31 32 /** Vehicle that is being synchronized to. */ 33 private String syncVehicle; 34 35 /** 36 * @param synchronization synchronization 37 */ 38 public LmrsData(final Synchronization synchronization) 39 { 40 this.synchronization = synchronization; 41 } 42 43 /** 44 * Checks if the given leader is a new leader. 45 * @param gtu gtu to check 46 * @return whether the gtu is a new leader 47 */ 48 boolean isNewLeader(final HeadwayGTU gtu) 49 { 50 this.tempLeaders.add(gtu.getId()); 51 return !this.leaders.contains(gtu.getId()); 52 } 53 54 /** 55 * Remembers the leaders of the current time step (those forwarded to isNewLeader()) for the next time step. 56 */ 57 void finalizeStep() 58 { 59 this.leaders.clear(); 60 this.leaders.addAll(this.tempLeaders); 61 this.tempLeaders.clear(); 62 } 63 64 /** 65 * Remembers the gtu that is synchronized to. 66 * @param gtu gtu that is synchronized to 67 */ 68 void setSyncVehicle(final HeadwayGTU gtu) 69 { 70 this.syncVehicle = gtu.getId(); 71 } 72 73 /** 74 * Returns whether the provided gtu is the gtu that is synchronized to. 75 * @param gtu gtu to inquiry 76 * @return whether the provided gtu is the gtu that is synchronized to 77 */ 78 boolean isSyncVehicle(final HeadwayGTU gtu) 79 { 80 return gtu.getId().equals(this.syncVehicle); 81 } 82 83 /** 84 * Returns the id of the vehicle that is synchronized to. 85 * @return id of the vehicle that is synchronized to 86 */ 87 String getSyncVehicleId() 88 { 89 return this.syncVehicle; 90 } 91 92 /** 93 * Returns the gtu from the set that is the current sync vehicle, or {@code null} of there is no sync vehicle or it is 94 * not in the set. 95 * @param adjLeaders leaders in adjacent lane 96 * @return gtu from the set that is the current sync vehicle 97 */ 98 HeadwayGTU getSyncVehicle(final Set<HeadwayGTU> adjLeaders) 99 { 100 if (this.syncVehicle == null) 101 { 102 return null; 103 } 104 for (HeadwayGTU leader : adjLeaders) 105 { 106 if (leader.getId().equals(this.syncVehicle)) 107 { 108 return leader; 109 } 110 } 111 return null; 112 } 113 114 /** 115 * Returns the synchronization. 116 * @return synchronization 117 */ 118 Synchronization getSynchronization() 119 { 120 return this.synchronization; 121 } 122 123 }