MATSIM
LinkNetworkRouteImpl.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * LinkRoute.java
4  * *
5  * *********************************************************************** *
6  * *
7  * copyright : (C) 2008 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.population.routes;
22 
23 import java.util.ArrayList;
24 import java.util.Collections;
25 import java.util.List;
26 
27 import org.matsim.api.core.v01.Id;
30 import org.matsim.vehicles.Vehicle;
31 
37 final class LinkNetworkRouteImpl extends AbstractRoute implements NetworkRoute {
38 
39  /*package*/ final static String ROUTE_TYPE = "links";
40 
41  private ArrayList<Id<Link>> route = new ArrayList<>();
42  private List<Id<Link>> safeRoute = Collections.unmodifiableList(this.route);
43  private double travelCost = Double.NaN;
44  private Id<Vehicle> vehicleId = null;
45 
46  LinkNetworkRouteImpl(final Id<Link> startLinkId, final Id<Link> endLinkId) {
47  super(startLinkId, endLinkId);
48  }
49 
50  LinkNetworkRouteImpl(final Id<Link> startLinkId, final List<Id<Link>> linkIds, final Id<Link> endLinkId) {
51  super(startLinkId, endLinkId);
52  setLinkIds(startLinkId, linkIds, endLinkId);
53  }
54 
55  LinkNetworkRouteImpl(final Id<Link> startLinkId, final Id<Link>[] linkIds, final Id<Link> endLinkId) {
56  super(startLinkId, endLinkId);
57  Collections.addAll(this.route, linkIds);
58  this.route.trimToSize();
59  }
60 
61  @Override
62  public LinkNetworkRouteImpl clone() {
63  LinkNetworkRouteImpl cloned = (LinkNetworkRouteImpl) super.clone();
64  ArrayList<Id<Link>> tmp = cloned.route;
65  cloned.route = new ArrayList<>(tmp); // deep copy of route
66  cloned.safeRoute = Collections.unmodifiableList(cloned.route);
67  return cloned;
68  }
69 
70  @Override
71  public List<Id<Link>> getLinkIds() {
72  return this.safeRoute;
73  }
74 
75  @Override
76  public NetworkRoute getSubRoute(Id<Link> fromLinkId, Id<Link> toLinkId) {
82  int fromIndex = -1;
86  int toIndex = -1;
87 
88  if (fromLinkId.equals(this.getStartLinkId())) {
89  fromIndex = 0;
90  } else {
91  for (int i = 0, n = this.route.size(); (i < n) && (fromIndex < 0); i++) {
92  if (fromLinkId.equals(this.route.get(i))) {
93  fromIndex = i+1;
94  }
95  }
96  if (fromIndex < 0 && fromLinkId.equals(this.getEndLinkId())) {
97  fromIndex = this.route.size();
98  }
99  if (fromIndex < 0) {
100  throw new IllegalArgumentException("Cannot create subroute because fromLinkId is not part of the route.");
101  }
102  }
103 
104  if (fromLinkId.equals(toLinkId)) {
105  toIndex = fromIndex - 1;
106  } else {
107  for (int i = fromIndex, n = this.route.size(); (i < n) && (toIndex < 0); i++) {
108  if (fromLinkId.equals(this.route.get(i))) {
109  fromIndex = i+1; // in case of a loop, cut it short
110  }
111  if (toLinkId.equals(this.route.get(i))) {
112  toIndex = i;
113  }
114  }
115  if (toIndex < 0 && toLinkId.equals(this.getEndLinkId())) {
116  toIndex = this.route.size();
117  }
118  if (toIndex < 0) {
119  throw new IllegalArgumentException("Cannot create subroute because toLinkId is not part of the route.");
120  }
121  }
122  NetworkRoute ret = RouteUtils.createLinkNetworkRouteImpl(fromLinkId, toLinkId);
123  if (toIndex > fromIndex) {
124  ret.setLinkIds(fromLinkId, this.route.subList(fromIndex, toIndex), toLinkId);
125  } else {
126  ret.setLinkIds(fromLinkId, null, toLinkId);
127  }
128  return ret;
129  }
130 
131  @Override
132  public double getTravelCost() {
133  return this.travelCost;
134  }
135 
136  @Override
137  public void setTravelCost(final double travelCost) {
138  this.travelCost = travelCost;
139  }
140 
141  @Override
142  public void setLinkIds(final Id<Link> startLinkId, final List<Id<Link>> srcRoute, final Id<Link> endLinkId) {
143  this.route.clear();
144  setStartLinkId(startLinkId);
145  setEndLinkId(endLinkId);
146  if (srcRoute != null) {
147  this.route.addAll(srcRoute);
148  }
149  this.route.trimToSize();
150  }
151 
152  @Override
153  public Id<Vehicle> getVehicleId() {
154  return this.vehicleId;
155  }
156 
157  @Override
158  public void setVehicleId(final Id<Vehicle> vehicleId) {
159  this.vehicleId = vehicleId;
160  }
161 
162  @Override
163  public String getRouteDescription() {
164  StringBuilder desc = new StringBuilder(100);
165  desc.append(this.getStartLinkId().toString());
166  for (Id<Link> linkId : this.getLinkIds()) {
167  desc.append(" ");
168  desc.append(linkId.toString());
169  }
170  // If the start links equals the end link additionally check if its is a round trip.
171  if (!this.getEndLinkId().equals(this.getStartLinkId()) || this.getLinkIds().size() > 0) {
172  desc.append(" ");
173  desc.append(this.getEndLinkId().toString());
174  }
175  return desc.toString();
176  }
177 
178  @Override
179  public void setRouteDescription(String routeDescription) {
180  List<Id<Link>> linkIds = NetworkUtils.getLinkIds(routeDescription);
181  Id<Link> startLinkId = getStartLinkId();
182  Id<Link> endLinkId = getEndLinkId();
183  if (linkIds.size() > 0) {
184  startLinkId = linkIds.remove(0);
185  setStartLinkId(startLinkId);
186  }
187  if (linkIds.size() > 0) {
188  endLinkId = linkIds.remove(linkIds.size() - 1);
189  setEndLinkId(endLinkId);
190  }
191  this.setLinkIds(startLinkId, linkIds, endLinkId);
192  }
193 
194  @Override
195  public String getRouteType() {
196  return ROUTE_TYPE;
197  }
198 
199  @Override
200  public String toString() {
201  String str = super.toString();
202  str += " linkIds=" + this.getLinkIds() ;
203  str += " travelCost=" + this.getTravelCost() ;
204  return str ;
205  }
206 }
final void setEndLinkId(final Id< Link > linkId)
final void setStartLinkId(final Id< Link > linkId)