MATSIM
VehiclesImpl.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * BasicVehiclesImpl
4  * *
5  * *********************************************************************** *
6  * *
7  * copyright : (C) 2009 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 package org.matsim.vehicles;
21 
22 import org.matsim.api.core.v01.Id;
23 import org.matsim.api.core.v01.IdMap;
25 
26 import java.util.Collections;
27 import java.util.Map;
28 
29 
34 final class VehiclesImpl implements Vehicles {
35  private final Map<Id<VehicleType>, VehicleType> vehicleTypes;
36  private final Map<Id<Vehicle>, Vehicle> vehicles;
37  private final VehiclesFactoryImpl builder;
38 
39  private final Counter counter = new Counter("[VehiclesImpl] added vehicle # " );
40 
44  VehiclesImpl(){
45  this.vehicleTypes = new IdMap<>(VehicleType.class); // FIXME potential iteration order change
46  this.builder = new VehiclesFactoryImpl() ;
47  this.vehicles = new IdMap<>(Vehicle.class); // FIXME potential iteration order change
48  }
49 
50 
51  @Override
52  public VehiclesFactory getFactory() {
53  return this.builder;
54  }
55 
56  @Override
57  public final Map<Id<Vehicle>, Vehicle> getVehicles() {
58  return Collections.unmodifiableMap(this.vehicles);
59  }
60 
61 
62  @Override
63  public Map<Id<VehicleType>, VehicleType> getVehicleTypes() {
64  return Collections.unmodifiableMap(this.vehicleTypes);
65  }
66 
74  @Override
75  public void addVehicle(final Vehicle v) {
76  /* Validation. */
77  if(this.getVehicles().containsKey(v.getId())){
78  throw new IllegalArgumentException("Vehicle with id = " + v.getId() + " already exists.");
79  }
80 
81  /* Check if the VehicleType associated with the vehicle already exist.
82  * Here only an error message is given. A RuntimeException is thrown
83  * when the MatsimVehicleWriter is called (JWJ, '14). */
84  if(!this.vehicleTypes.containsKey(v.getType().getId())){
85  throw new IllegalArgumentException("Cannot add Vehicle with type = " + v.getType().getId().toString() +
86  " if the VehicleType has not been added to the Vehicles container.");
87  }
88 
89  /* Add the vehicle. */
90  this.vehicles.put(v.getId(), v);
91  this.counter.incCounter();
92  }
93 
99  @Override
100  public void removeVehicle(final Id<Vehicle> vehicleId) {
101  this.vehicles.remove(vehicleId);
102  }
103 
111  @Override
112  public void addVehicleType(VehicleType type){
113  /* Validation. */
114  if(this.getVehicleTypes().containsKey(type.getId())){
115  throw new IllegalArgumentException("Vehicle type with id = " + type.getId() + " already exists.");
116  }
117 
118  /* Add the vehicle type. */
119  this.vehicleTypes.put(type.getId(), type);
120  }
121 
122  @Override
123  public void removeVehicleType(Id<VehicleType> vehicleTypeId) {
124  for (Vehicle veh : this.vehicles.values()) {
125  if (veh.getType().getId().equals(vehicleTypeId)) {
126  throw new IllegalArgumentException("Cannot remove vehicle type as it is used by at least one vehicle.");
127  }
128  }
129  this.vehicleTypes.remove(vehicleTypeId);
130  }
131 }