MATSIM
LegImpl.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * Leg.java
4  * *
5  * *********************************************************************** *
6  * *
7  * copyright : (C) 2007, 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;
22 
27 import org.matsim.core.utils.misc.Time;
30 
31 /* deliberately package */ final class LegImpl implements Leg {
32 
33  private static final double UNDEFINED_TIME = Double.NEGATIVE_INFINITY;
34 
35  private Route route = null;
36 
37  private double depTime = UNDEFINED_TIME;
38  private double travTime = UNDEFINED_TIME;
39  private String mode;
40  private String routingMode;
41 
42  private Attributes attributes = null;
43 
44  /* deliberately package */ LegImpl(final String transportMode) {
45  this.mode = transportMode;
46  }
47 
48 
49  private static OptionalTime asOptionalTime(double seconds) {
50  return seconds == UNDEFINED_TIME ? OptionalTime.undefined() : OptionalTime.defined(seconds);
51  }
52 
53  @Override
54  public String getMode() {
55  return this.mode;
56  }
57 
58  @Override
59  public final void setMode(String transportMode) {
60  this.mode = transportMode == null ? null : transportMode.intern();
61  TripStructureUtils.setRoutingMode( this, null );
62 // TripStructureUtils.setRoutingMode( this, null ); // setting routingMode to null leads to exceptions in AttributesXmlWriterDelegate.writeAttributes() : Class<?> clazz = objAttribute.getValue().getClass();
63  // (yyyy or maybe "transportMode" instead of "null"?? kai, oct'19)
64  }
65 
66  @Override
67  public final String getRoutingMode() {
68  return this.routingMode;
69  }
70 
71  @Override
72  public final void setRoutingMode(String routingMode) {
73  this.routingMode = routingMode == null ? null : routingMode.intern();
74  }
75 
76  @Override
77  public OptionalTime getDepartureTime() {
78  return asOptionalTime(this.depTime);
79  }
80 
81  @Override
82  public void setDepartureTime(final double depTime) {
83  OptionalTime.assertDefined(depTime);
84  this.depTime = depTime;
85  }
86 
87  @Override
88  public void setDepartureTimeUndefined() {
89  this.depTime = UNDEFINED_TIME;
90  }
91 
92  @Override
93  public OptionalTime getTravelTime() {
94  return asOptionalTime(this.travTime);
95  }
96 
97  @Override
98  public void setTravelTime(final double travTime) {
99  OptionalTime.assertDefined(travTime);
100  this.travTime = travTime;
101  }
102 
103  @Override
104  public void setTravelTimeUndefined() {
105  this.travTime = UNDEFINED_TIME;
106  }
107 
108  @Override
109  public Route getRoute() {
110  return this.route;
111  }
112 
113  @Override
114  public void setRoute(Route route) {
115  this.route = route;
116  }
117 
118  @Override
119  public String toString() {
120  return "leg [mode="
121  + this.getMode()
122  + "]"
123  + "[depTime="
124  + Time.writeTime(this.getDepartureTime())
125  + "]"
126  + "[travTime="
127  + Time.writeTime(this.getTravelTime())
128  + "]"
129  + "[arrTime="
130  + (getDepartureTime().isDefined() && getTravelTime().isDefined() ?
131  Time.writeTime(getDepartureTime().seconds() + getTravelTime().seconds()) :
132  Time.writeTime(OptionalTime.undefined()))
133  + "]"
134  + "[route="
135  + this.route
136  + "]";
137  }
138 
139 
140  @Override
141  public Attributes getAttributes() {
142  if (this.attributes != null) {
143  return this.attributes;
144  }
145  return new LazyAllocationAttributes(attributes -> this.attributes = attributes, () -> this.attributes);
146  }
147 
148  // private boolean locked;
149 //
150 // public void setLocked() {
151 // this.locked = true ;
152 // }
153 // private void testForLocked() {
154 // if ( this.locked ) {
155 // throw new RuntimeException("too late to change this") ;
156 // }
157 // }
158 
159 }