MATSIM
ActivityOptionImpl.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * Activity.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 
23 import java.util.Collection;
24 import java.util.Iterator;
25 import java.util.SortedSet;
26 import java.util.TreeSet;
27 
28 public class ActivityOptionImpl implements ActivityOption {
29 
30  private final String type;
31  private Double capacity = (double)Integer.MAX_VALUE;
32  private SortedSet<OpeningTime> openingTimes = new TreeSet<>();
33 
34  public ActivityOptionImpl(final String type) {
35  this.type = type;
36  }
37 
38  public void clearOpeningTimes() {
39  this.openingTimes.clear();
40  }
41 
42  @Override
43  public void addOpeningTime(OpeningTime opentime) {
44  if (openingTimes.isEmpty()) {
45  openingTimes.add(opentime);
46  return;
47  }
48  TreeSet<OpeningTime> new_o_set = new TreeSet<>();
49  Iterator<OpeningTime> o_it = openingTimes.iterator();
50  while (o_it.hasNext()) {
51  OpeningTime o = o_it.next();
52  int merge_type = o.compareTo(opentime); // see Opentime for the meaning
53  if ((merge_type == -6) || (merge_type == 6)) {
54  // complete disjoint
55  new_o_set.add(o);
56  new_o_set.add(opentime);
57  }
58  else if ((merge_type >= -1) && (merge_type <= 2)) {
59  // opentime is subset of o
60  new_o_set.add(o);
61  }
62  else if ((merge_type == -3) || (merge_type == -2) || (merge_type == 3)) {
63  // o is subset of opentime
64  new_o_set.add(opentime);
65  }
66  else { // union of the two opentimes
67  if ((merge_type == -5) || (merge_type == -4)) {
68  // start_time of opentime and endtime of o
69  opentime.setEndTime(o.getEndTime());
70  new_o_set.add(opentime);
71  }
72  else if ((merge_type == 4) || (merge_type == 5)) {
73  // start_time of o and endtime of opentime
74  opentime.setStartTime(o.getStartTime());
75  new_o_set.add(opentime);
76  }
77  else {
78  throw new RuntimeException("[Something is wrong]");
79  }
80  }
81  }
82  this.openingTimes.clear();
83  this.openingTimes.addAll(new_o_set);
84  }
85 
86  public void setOpeningTimes(Collection<OpeningTime> times) {
87  this.clearOpeningTimes();
88  for (OpeningTime t : times) {
89  this.addOpeningTime(t);
90  }
91  }
92 
93  @Override
94  public final void setCapacity(double capacity) {
95  if (capacity < 0) {
96  throw new NumberFormatException("A capacity of an activity must be >= 0.");
97  }
98  this.capacity = capacity;
99  }
100 
101  @Override
102  public final String getType() {
103  return this.type;
104  }
105 
106  @Override
107  public final double getCapacity() {
108  return this.capacity;
109  }
110 
111  @Override
112  public final SortedSet<OpeningTime> getOpeningTimes() {
113  return this.openingTimes;
114  }
115 
116  @Override
117  public final String toString() {
118  return "[type=" + this.type + "]" +
119  "[capacity=" + this.capacity + "]" +
120  "[nof_opentimes=" + this.getOpeningTimes().size() + "]";
121  }
122 
123 }
void setEndTime(double endtime)
final SortedSet< OpeningTime > getOpeningTimes()
void setOpeningTimes(Collection< OpeningTime > times)
void setStartTime(double starttime)