MATSIM
NodeImpl.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * Node.java
4  * *
5  * *********************************************************************** *
6  * *
7  * copyright : (C) 2007 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 
21 package org.matsim.core.network;
22 
23 import java.util.Collections;
24 import java.util.Map;
25 
26 import org.apache.logging.log4j.LogManager;
27 import org.apache.logging.log4j.Logger;
28 import org.matsim.api.core.v01.Coord;
29 import org.matsim.api.core.v01.Id;
32 import org.matsim.core.gbl.Gbl;
37 
38 /*deliberately package*/ class NodeImpl implements Node, Lockable {
39 
41  // member variables
43 
44  private String type = null;
45  private String origid = null;
46 
47  private transient Map<Id<Link>, Link> inlinks = new IdentifiableArrayMap<>();
48  private transient Map<Id<Link>, Link> outlinks = new IdentifiableArrayMap<>();
49 
50  private Coord coord;
51  private final Id<Node> id;
52  private boolean locked = false ;
53 
54  private final static Logger log = LogManager.getLogger(Node.class);
55  private final Attributes attributes = new AttributesImpl();
56 
58  // constructor
60 
61  NodeImpl(final Id<Node> id, final Coord coord) {
62  this(id, coord, null);
63  }
64 
65  /* package */NodeImpl(final Id<Node> id, final Coord coord, final String type) {
66  this(id);
67  this.coord = coord;
68  NetworkUtils.setType(this,type);
69  }
70 
71  /* package */NodeImpl(Id<Node> id) {
72  this.id = id ;
73  this.coord = null ;
74  }
75 
76  /* package */ void setType( final String type ) {
77  this.type = type == null ? null : type.intern();
78  }
79 
80  private static int cnt2 = 0 ;
81  @Override
82  public final boolean addInLink(Link inlink) {
83  Id<Link> linkid = inlink.getId();
84  if (this.inlinks.containsKey(linkid)) {
85  throw new IllegalArgumentException(this + ": inlink_id=" + inlink.getId() + " already exists");
86  }
87 // if (this.outlinks.containsKey(linkid) && (cnt2 < 1)) {
88 // cnt2++ ;
89 // log.warn(this + ": inlink_id=" + inlink.getId() + " is now in- and out-link");
90 // log.warn(Gbl.ONLYONCE) ;
91 // }
92  // (this means it is a loop link; they have become an acceptable data structure within matsim. kai, sep'19)
93  this.inlinks.put(linkid, inlink);
94  return true; // yy should return true only if collection changed as result of call
95  }
96 
97  private static int cnt = 0 ;
98  @Override
99  public final boolean addOutLink(Link outlink) {
100  Id<Link> linkid = outlink.getId();
101  if (this.outlinks.containsKey(linkid)) {
102  throw new IllegalArgumentException(this + ": outlink_id=" + outlink.getId() + " already exists");
103  }
104  if (this.inlinks.containsKey(linkid) && (cnt < 1)) {
105  cnt++ ;
106  log.warn(this.toString() + ": outlink_id=" + outlink.getId() + " is now in- and out-link");
107  log.warn(Gbl.ONLYONCE) ;
108  }
109  this.outlinks.put(linkid, outlink);
110  return true ; // yy should return true only if collection changed as result of call
111  }
112  @Override
113  public void setCoord(final Coord coord){
114  testForLocked();
115  this.coord = coord;
116  }
117 
118  /*package*/ void setOrigId(final String origId){
119  this.origid = origId ;
120  }
121 
123  // remove methods
125 
126  @Override
127  public final Link removeInLink( final Id<Link> linkId ) {
128  return this.inlinks.remove(linkId) ;
129  }
130 
131  @Override
132  public Link removeOutLink(final Id<Link> outLinkId) {
133  return this.outlinks.remove(outLinkId);
134  }
135 
137  // get methods
139 
140  /*package*/ String getOrigId() {
141  return this.origid ;
142  }
143 
144  /*package*/ String getType() {
145  return this.type ;
146  }
147 
148  @Override
149  public Map<Id<Link>, ? extends Link> getInLinks() {
150  return Collections.unmodifiableMap(this.inlinks);
151  }
152 
153  @Override
154  public Map<Id<Link>, ? extends Link> getOutLinks() {
155  return Collections.unmodifiableMap(this.outlinks);
156  }
157 
158  @Override
159  public Coord getCoord() {
160  return this.coord;
161  }
162 
163  @Override
164  public Id<Node> getId() {
165  return this.id;
166  }
167 
168 
169 // private void readObject(ObjectInputStream ois) throws ClassNotFoundException, IOException {
170 // ois.defaultReadObject();
171 //
172 // inlinks = new LinkedHashMap<>(4, 0.95f);
173 // outlinks = new LinkedHashMap<>(4, 0.95f);
174 //
175 // }
176 
178  // print methods
180 
181  @Override
182  public String toString() {
183  return "[id=" + this.id + "]" +
184  "[coord=" + this.coord + "]" +
185  "[type=" + this.type + "]" +
186  "[nof_inlinks=" + this.inlinks.size() + "]" +
187  "[nof_outlinks=" + this.outlinks.size() + "]";
188  }
189 
190  @Override
191  public void setLocked() {
192  this.locked = true ;
193  }
194  private void testForLocked() {
195  if ( locked ) {
196  throw new RuntimeException( "Network is locked; too late to do this. See comments in code.") ;
197  }
198  }
199 
200  @Override
201  public Attributes getAttributes() {
202  return attributes;
203  }
204 }