MATSIM
RouteFactories.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * *
4  * *********************************************************************** *
5  * *
6  * copyright : (C) 2011 by the members listed in the COPYING, *
7  * LICENSE and WARRANTY file. *
8  * email : info at matsim dot org *
9  * *
10  * *********************************************************************** *
11  * *
12  * This program is free software; you can redistribute it and/or modify *
13  * it under the terms of the GNU General Public License as published by *
14  * the Free Software Foundation; either version 2 of the License, or *
15  * (at your option) any later version. *
16  * See also COPYING, LICENSE and WARRANTY file *
17  * *
18  * *********************************************************************** */
19 
20 package org.matsim.core.population.routes;
21 
22 import java.util.HashMap;
23 import java.util.Map;
24 
25 import org.matsim.api.core.v01.Id;
32 
36 public final class RouteFactories {
37  private final Map<Class<? extends Route>, RouteFactory> routeFactories = new HashMap<>();
39  private final Map<String, Class<? extends Route>> type2class = new HashMap<>();
40 
41  public RouteFactories() {
45  }
46 
55  public <R extends Route> R createRoute(final Class<R> routeClass, final Id<Link> startLinkId, final Id<Link> endLinkId) {
56  return (R)this.routeFactories.getOrDefault(routeClass, defaultFactory).createRoute(startLinkId, endLinkId);
57  }
58 
67  public void setRouteFactory(final Class<? extends Route> routeClass, final RouteFactory factory) {
68  if (routeClass == null) {
69  this.defaultFactory = factory;
70  } else {
71  if (factory == null) {
72  this.routeFactories.remove(routeClass);
73  } else {
74  this.routeFactories.put(routeClass, factory);
75  this.type2class.put(factory.getCreatedRouteType(), routeClass);
76  }
77  }
78  }
79 
80  public Class<? extends Route> getRouteClassForType(String routeType) {
81  //Route.class will result in a generic route
82  return this.type2class.getOrDefault(routeType, Route.class);
83  }
84 }
void setRouteFactory(final Class<? extends Route > routeClass, final RouteFactory factory)
final Map< Class<? extends Route >, RouteFactory > routeFactories
final Map< String, Class<? extends Route > > type2class
Class<? extends Route > getRouteClassForType(String routeType)