MATSIM
FeatureGeneratorBuilderImpl.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * FeatureGeneratorBuilder.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.utils.gis.matsim2esri.network;
22 
23 import java.lang.reflect.Constructor;
24 import java.lang.reflect.InvocationTargetException;
25 
26 import org.geotools.api.referencing.crs.CoordinateReferenceSystem;
29 
41 
42 
43  private final Network network;
44  private CoordinateReferenceSystem crs;
45 
46  private Constructor<? extends FeatureGenerator> featureGeneratorPrototypeContructor;
47  private static final Class[] FEATURE_GENERATOR_PROTOTYPECONSTRUCTOR = { WidthCalculator.class, CoordinateReferenceSystem.class};
48 
49  private Constructor<? extends WidthCalculator> widthCalculatorPrototypeContructor;
50 
51  private double widthCoefficient = 1;
52  private static final Class[] WIDTH_CALCULATOR_PROTOTYPECONSTRUCTOR = { Network.class, Double.class};
53 
54 
55  public FeatureGeneratorBuilderImpl(final Network network, final String coordinateSystem) {
56  this.network = network;
57  this.crs = MGC.getCRS(coordinateSystem);
58  try {
59  this.featureGeneratorPrototypeContructor = PolygonFeatureGenerator.class.getConstructor(FEATURE_GENERATOR_PROTOTYPECONSTRUCTOR);
60  } catch (SecurityException e) {
61  e.printStackTrace();
62  } catch (NoSuchMethodException e) {
63  e.printStackTrace();
64  }
65 
66  try {
67  this.widthCalculatorPrototypeContructor = LanesBasedWidthCalculator.class.getConstructor(WIDTH_CALCULATOR_PROTOTYPECONSTRUCTOR);
68  } catch (SecurityException e) {
69  e.printStackTrace();
70  } catch (NoSuchMethodException e) {
71  e.printStackTrace();
72  }
73  }
74 
75  @Override
77 
79  FeatureGenerator ret;
80  Exception ex;
81  try {
82  ret = this.featureGeneratorPrototypeContructor.newInstance(widthCalc, this.crs);
83  return ret;
84  } catch (IllegalArgumentException e) {
85  ex = e;
86  } catch (InstantiationException e) {
87  ex = e;
88  } catch (IllegalAccessException e) {
89  ex = e;
90  } catch (InvocationTargetException e) {
91  ex = e;
92  }
93  throw new RuntimeException(
94  "Could not instantiate feature generator from prototype! " + this.featureGeneratorPrototypeContructor.getDeclaringClass().getCanonicalName(),
95  ex);
96  }
97 
99  WidthCalculator ret;
100  Exception ex;
101  try {
102  ret = this.widthCalculatorPrototypeContructor.newInstance(this.network, this.widthCoefficient);
103  return ret;
104  } catch (IllegalArgumentException e) {
105  ex = e;
106  } catch (InstantiationException e) {
107  ex = e;
108  } catch (IllegalAccessException e) {
109  ex = e;
110  } catch (InvocationTargetException e) {
111  ex = e;
112  }
113  throw new RuntimeException(
114  "Could not instantiate width calculator from prototype!",
115  ex);
116 
117  }
118 
119 
120  public void setWidthCoefficient(final double coef) {
121  this.widthCoefficient = coef;
122  }
123 
124  public void setCoordinateReferenceSystem(final CoordinateReferenceSystem crs) {
125  this.crs = crs;
126  }
127 
128  public void setWidthCalculatorPrototype(final Class<? extends WidthCalculator> prototype) {
129 
130  try {
131  Constructor<? extends WidthCalculator> c = prototype.getConstructor(WIDTH_CALCULATOR_PROTOTYPECONSTRUCTOR);
132  if (null != c) {
133  this.widthCalculatorPrototypeContructor = c;
134  }
135  else {
136  throw new IllegalArgumentException("Wrong prototype constructor!");
137  }
138  } catch (SecurityException e) {
139  e.printStackTrace();
140  } catch (NoSuchMethodException e) {
141  e.printStackTrace();
142  }
143  }
144 
145  public void setFeatureGeneratorPrototype(final Class<? extends FeatureGenerator> prototype) {
146 
147  try {
148  Constructor<? extends FeatureGenerator> c = prototype.getConstructor(FEATURE_GENERATOR_PROTOTYPECONSTRUCTOR);
149  if (null != c) {
150  this.featureGeneratorPrototypeContructor = c;
151  }
152  else {
153  throw new IllegalArgumentException("Wrong prototype constructor!");
154  }
155  } catch (SecurityException e) {
156  e.printStackTrace();
157  } catch (NoSuchMethodException e) {
158  e.printStackTrace();
159  }
160  }
161 
162 }
void setFeatureGeneratorPrototype(final Class<? extends FeatureGenerator > prototype)
FeatureGeneratorBuilderImpl(final Network network, final String coordinateSystem)
void setWidthCalculatorPrototype(final Class<? extends WidthCalculator > prototype)
static CoordinateReferenceSystem getCRS(final String wktOrAuthorityCodeOrShorthandName)
Definition: MGC.java:169