20 package org.matsim.vehicles;
26 import java.util.Collections;
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;
39 private final Counter counter =
new Counter(
"[VehiclesImpl] added vehicle # " );
45 this.vehicleTypes =
new IdMap<>(VehicleType.class);
46 this.builder =
new VehiclesFactoryImpl() ;
47 this.vehicles =
new IdMap<>(Vehicle.class);
52 public VehiclesFactory getFactory() {
57 public final Map<Id<Vehicle>, Vehicle> getVehicles() {
58 return Collections.unmodifiableMap(this.vehicles);
63 public Map<Id<VehicleType>, VehicleType> getVehicleTypes() {
64 return Collections.unmodifiableMap(this.vehicleTypes);
75 public void addVehicle(
final Vehicle v) {
77 if(this.getVehicles().containsKey(v.getId())){
78 throw new IllegalArgumentException(
"Vehicle with id = " + v.getId() +
" already exists.");
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.");
90 this.vehicles.put(v.getId(), v);
91 this.counter.incCounter();
100 public void removeVehicle(
final Id<Vehicle> vehicleId) {
101 this.vehicles.remove(vehicleId);
112 public void addVehicleType(VehicleType type){
114 if(this.getVehicleTypes().containsKey(type.getId())){
115 throw new IllegalArgumentException(
"Vehicle type with id = " + type.getId() +
" already exists.");
119 this.vehicleTypes.put(type.getId(), type);
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.");
129 this.vehicleTypes.remove(vehicleTypeId);