MATSIM
AbstractQNode.java
Go to the documentation of this file.
1 
2 /* *********************************************************************** *
3  * project: org.matsim.*
4  * AbstractQNode.java
5  * *
6  * *********************************************************************** *
7  * *
8  * copyright : (C) 2019 by the members listed in the COPYING, *
9  * LICENSE and WARRANTY file. *
10  * email : info at matsim dot org *
11  * *
12  * *********************************************************************** *
13  * *
14  * This program is free software; you can redistribute it and/or modify *
15  * it under the terms of the GNU General Public License as published by *
16  * the Free Software Foundation; either version 2 of the License, or *
17  * (at your option) any later version. *
18  * See also COPYING, LICENSE and WARRANTY file *
19  * *
20  * *********************************************************************** */
21 
22  package org.matsim.core.mobsim.qsim.qnetsimengine;
23 
24 import java.util.HashMap;
25 import java.util.Map;
26 import java.util.concurrent.atomic.AtomicBoolean;
27 
29 
36 abstract class AbstractQNode implements QNodeI {
37 
38  // necessary if Nodes are (de)activated
39  private NetElementActivationRegistry activator = null;
40 
41  /*
42  * This needs to be atomic since this allows us to ensure that an node which is
43  * already active is not activated again. This could happen if multiple thread call
44  * activateNode() concurrently.
45  * cdobler, sep'14
46  */
47  private final AtomicBoolean active = new AtomicBoolean(false);
48 
49  // for Customizable
50  private final Map<String, Object> customAttributes = new HashMap<>();
51 
52  final Node node;
53 
54 
55 
56  AbstractQNode(final Node n){
57  this.node = n;
58  }
59 
60 
61  @Override
62  public Node getNode() {
63  return this.node;
64  }
65 
70  /*package*/ void setNetElementActivationRegistry(NetElementActivationRegistry activator) {
71  // yyyy I cannot say if this needs to be in QNodeI or not. The mechanics of this are tricky to implement, so it would
72  // not be a stable/robust API. kai, jul'17
73 
74  this.activator = activator;
75  }
76 
84  /*package*/ final void activateNode() {
85  // yyyy I cannot say if this needs to be in QNodeI or not. The mechanics of this are tricky to implement, so it would
86  // not be a stable/robust API. kai, jul'17
87 
88  /*
89  * this.active.compareAndSet(boolean expected, boolean update)
90  * We expect the value to be false, i.e. the node is de-activated. If this is
91  * true, the value is changed to true and the activator is informed.
92  */
93  if (this.active.compareAndSet(false, true)) {
94  this.activator.registerNodeAsActive(this);
95  }
96  }
97 
98  final boolean isActive() {
99  // yyyy I cannot say if this needs to be in QNodeI or not. The mechanics of this are tricky to implement, so it would
100  // not be a stable/robust API. kai, jul'17
101 
102  return this.active.get();
103  }
104 
105  void setActive(boolean active) {
106  this.active.set(active);
107  }
108 
109 
110  @Override
111  public final Map<String, Object> getCustomAttributes() {
112  return customAttributes;
113  }
114 }