MATSIM
OpeningTimeImpl.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * Opentime.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.facilities;
22 
24 import org.matsim.core.utils.misc.Time;
25 
26 import com.google.common.base.Preconditions;
27 
28 public class OpeningTimeImpl implements OpeningTime {
30  return new OpeningTimeImpl(start.orElse(Double.NEGATIVE_INFINITY), end.orElse(Double.POSITIVE_INFINITY));
31  }
32 
33  private double startTime;
34  private double endTime;
35 
36  public OpeningTimeImpl(final double startTime, final double endTime) {
37  this.startTime = startTime;
38  this.endTime = endTime;
39  this.validateTimes();
40  }
41 
43  //
44  // interface implementation
45  //
47 
48  @Override
49  public int compareTo(OpeningTime other) {
50  // two functionalities in one:
51  // 1. the earlier start_time comes before the other. If they're the same,
52  // the end times decides which comes first
53  // 2. the meaning of the return value. See the ASCII figures for that.
54  if (this.startTime > other.getEndTime()) { // this: |-----|
55  return -6; // other: |--|
56  }
57  else if (this.startTime == other.getEndTime()) { // this: |-----|
58  return -5; // other: |----|
59  }
60  else if (this.startTime > other.getStartTime()) {
61  if (this.endTime > other.getEndTime()) { // this: |-----|
62  return -4; // other: |--------|
63  }
64  else if (this.endTime == other.getEndTime()) { // this: |-----|
65  return -3; // other: |----------|
66  }
67  else { // this: |-----|
68  return -2; // other: |---------------|
69  }
70  }
71  else if (this.startTime == other.getStartTime()) {
72  if (this.endTime > other.getEndTime()) { // this: |-----|
73  return -1; // other: |---|
74  }
75  else if (this.endTime == other.getEndTime()) { // this: |-----|
76  return 0; // other: |-----|
77  }
78  else { // this: |-----|
79  return 3; // other: |----------|
80  }
81  }
82  else if (this.endTime > other.getEndTime()) { // this: |-----|
83  return 2; // other: |-|
84  }
85  else if (this.endTime == other.getEndTime()) { // this: |-----|
86  return 1; // other: |---|
87  }
88  else if (this.endTime > other.getStartTime()) { // this: |-----|
89  return 4; // other: |--------|
90  }
91  else if (this.endTime == other.getStartTime()) { // this: |-----|
92  return 5; // other: |----|
93  }
94  else { // this: |-----|
95  return 6; // other: |--|
96  }
97  }
98 
100  // query methods
102 
103  @Override
104  public final boolean equals(final Object o) {
105  if (o instanceof OpeningTimeImpl) {
106  OpeningTimeImpl other = (OpeningTimeImpl)o;
107  if ((other.startTime == this.startTime) && (other.endTime == this.endTime)) {
108  return true;
109  }
110  }
111  return false;
112  }
113 
114  @Override
115  public final int hashCode() {
116  /* equals() checks day, startTime and endTime, so we should include those into hashCode as well */
117  return (Double.hashCode(this.startTime)
118  + Double.hashCode(this.endTime));
119  }
120 
121  private final void validateTimes() {
122  Preconditions.checkState(startTime != Double.POSITIVE_INFINITY);
123  Preconditions.checkState(endTime != Double.NEGATIVE_INFINITY);
124  Preconditions.checkState(this.startTime < this.endTime,
125  "[startTime=%s] >= [endTime=%s] not allowed]", this.startTime, this.endTime);
126  }
127 
129  // set methods
131 
132  @Override
133  public final void setStartTime(final double start_time) {
134  this.startTime = start_time;
135  this.validateTimes();
136  }
137 
138  @Override
139  public final void setEndTime(final double end_time) {
140  this.endTime = end_time;
141  this.validateTimes();
142  }
143 
145  // get methods
147 
148  @Override
149  public final double getStartTime() {
150  return this.startTime;
151  }
152 
153  @Override
154  public final double getEndTime() {
155  return this.endTime;
156  }
157 
159  // print methods
161 
162  @Override
163  public final String toString() {
164  return "[startTime=" + Time.writeTime(this.startTime) + "]" +
165  "[endTime=" + Time.writeTime(this.endTime) + "]";
166  }
167 
168 }
final boolean equals(final Object o)
final void setStartTime(final double start_time)
OpeningTimeImpl(final double startTime, final double endTime)
final void setEndTime(final double end_time)
static final String writeTime(final double seconds, final String timeformat)
Definition: Time.java:80
static OpeningTime createFromOptionalTimes(OptionalTime start, OptionalTime end)