21 package org.matsim.core.population.routes;
23 import java.util.ArrayList;
24 import java.util.Collections;
25 import java.util.List;
37 final class LinkNetworkRouteImpl
extends AbstractRoute implements NetworkRoute {
39 final static String ROUTE_TYPE =
"links";
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;
46 LinkNetworkRouteImpl(
final Id<Link> startLinkId,
final Id<Link> endLinkId) {
47 super(startLinkId, endLinkId);
50 LinkNetworkRouteImpl(
final Id<Link> startLinkId,
final List<Id<Link>> linkIds,
final Id<Link> endLinkId) {
51 super(startLinkId, endLinkId);
52 setLinkIds(startLinkId, linkIds, endLinkId);
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();
62 public LinkNetworkRouteImpl clone() {
63 LinkNetworkRouteImpl cloned = (LinkNetworkRouteImpl) super.clone();
64 ArrayList<Id<Link>> tmp = cloned.route;
65 cloned.route =
new ArrayList<>(tmp);
66 cloned.safeRoute = Collections.unmodifiableList(cloned.route);
71 public List<Id<Link>> getLinkIds() {
72 return this.safeRoute;
76 public NetworkRoute getSubRoute(Id<Link> fromLinkId, Id<Link> toLinkId) {
88 if (fromLinkId.equals(
this.getStartLinkId())) {
91 for (
int i = 0, n = this.route.size(); (i < n) && (fromIndex < 0); i++) {
92 if (fromLinkId.equals(
this.route.get(i))) {
96 if (fromIndex < 0 && fromLinkId.equals(
this.getEndLinkId())) {
97 fromIndex = this.route.size();
100 throw new IllegalArgumentException(
"Cannot create subroute because fromLinkId is not part of the route.");
104 if (fromLinkId.equals(toLinkId)) {
105 toIndex = fromIndex - 1;
107 for (
int i = fromIndex, n = this.route.size(); (i < n) && (toIndex < 0); i++) {
108 if (fromLinkId.equals(
this.route.get(i))) {
111 if (toLinkId.equals(
this.route.get(i))) {
115 if (toIndex < 0 && toLinkId.equals(
this.getEndLinkId())) {
116 toIndex = this.route.size();
119 throw new IllegalArgumentException(
"Cannot create subroute because toLinkId is not part of the route.");
122 NetworkRoute ret = RouteUtils.createLinkNetworkRouteImpl(fromLinkId, toLinkId);
123 if (toIndex > fromIndex) {
124 ret.setLinkIds(fromLinkId, this.route.subList(fromIndex, toIndex), toLinkId);
126 ret.setLinkIds(fromLinkId, null, toLinkId);
132 public double getTravelCost() {
133 return this.travelCost;
137 public void setTravelCost(
final double travelCost) {
138 this.travelCost = travelCost;
142 public void setLinkIds(
final Id<Link> startLinkId,
final List<Id<Link>> srcRoute,
final Id<Link> endLinkId) {
146 if (srcRoute != null) {
147 this.route.addAll(srcRoute);
149 this.route.trimToSize();
153 public Id<Vehicle> getVehicleId() {
154 return this.vehicleId;
158 public void setVehicleId(
final Id<Vehicle> vehicleId) {
159 this.vehicleId = vehicleId;
163 public String getRouteDescription() {
164 StringBuilder desc =
new StringBuilder(100);
166 for (Id<Link> linkId : this.getLinkIds()) {
168 desc.append(linkId.toString());
175 return desc.toString();
179 public void setRouteDescription(String routeDescription) {
180 List<Id<Link>> linkIds = NetworkUtils.getLinkIds(routeDescription);
183 if (linkIds.size() > 0) {
184 startLinkId = linkIds.remove(0);
187 if (linkIds.size() > 0) {
188 endLinkId = linkIds.remove(linkIds.size() - 1);
191 this.setLinkIds(startLinkId, linkIds, endLinkId);
195 public String getRouteType() {
200 public String toString() {
201 String str = super.toString();
202 str +=
" linkIds=" + this.getLinkIds() ;
203 str +=
" travelCost=" + this.getTravelCost() ;
final void setEndLinkId(final Id< Link > linkId)
final Id< Link > getEndLinkId()
final void setStartLinkId(final Id< Link > linkId)
final Id< Link > getStartLinkId()