MATSIM
DefaultSignalizeableItem.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * QSignalizedItem
4  * *
5  * *********************************************************************** *
6  * *
7  * copyright : (C) 2010 by the members listed in the COPYING, *
8  * LICENSE and WARRANTY file. *
9  * email : info at matsim dot org *
10  * *
11  * *********************************************************************** *
12  * *
13  * This program is free software; you can redistribute it and/or modify *
14  * it under the terms of the GNU General Public License as published by *
15  * the Free Software Foundation; either version 2 of the License, or *
16  * (at your option) any later version. *
17  * See also COPYING, LICENSE and WARRANTY file *
18  * *
19  * *********************************************************************** */
20 package org.matsim.core.mobsim.qsim.qnetsimengine;
21 
22 import java.util.HashMap;
23 import java.util.Map;
24 import java.util.Set;
25 
26 import org.matsim.api.core.v01.Id;
30 
31 
36 public final class DefaultSignalizeableItem implements SignalizeableItem {
37 
38  private Map<Id<Link>, SignalGroupState> toLinkIdSignalStates = null;
40  private boolean linkGreen = true;
41  private Set<Id<Link>> outLinks;
42 
43  public DefaultSignalizeableItem(Set<Id<Link>> outLinks){
44  this.outLinks = outLinks;
45  }
46 
47  @Override
49  this.allToLinksState = state;
50  this.linkGreen = checkGreen(state);
51  }
52 
53  private void initToLinkIdSignalStates(){
54  this.allToLinksState = null;
55  this.toLinkIdSignalStates = new HashMap<>();
56  for (Id<Link> outLinkId : this.outLinks){
57  this.toLinkIdSignalStates.put(outLinkId, SignalGroupState.GREEN);
58  }
59  }
60 
61  @Override
63  if (this.toLinkIdSignalStates == null){
65  }
66  this.toLinkIdSignalStates.put(toLinkId, state);
67  if (checkGreen(state)){
68  this.linkGreen = true;
69  }
70  else {
71  boolean foundGreen = false;
72  for (SignalGroupState sgs : this.toLinkIdSignalStates.values()){
73  if (checkGreen(sgs)){
74  foundGreen = true;
75  }
76  }
77  this.linkGreen = foundGreen;
78  }
79  }
80 
81  private static boolean checkGreen(SignalGroupState state) {
82  return (state.equals(SignalGroupState.GREEN) || state.equals(SignalGroupState.YELLOW) || state.equals(SignalGroupState.OFF));
83  }
84 
88  public boolean hasGreenForAllToLinks() {
89  return linkGreen;
90  }
91 
92  public boolean hasGreenForToLink(Id<Link> toLinkId){
93  if (this.allToLinksState != null) {
94  return checkGreen(this.allToLinksState);
95  }
96  return checkGreen(this.toLinkIdSignalStates.get(toLinkId));
97  }
98 
99  @Override
100  public void setSignalized(boolean isSignalized) {
101  //nothing to do
102  }
103 
104 }
void setSignalStateForTurningMove(SignalGroupState state, Id< Link > toLinkId)