90public class DistributionFactory {
91 private DistributionFactory() {
102 int idx = str.indexOf(
'(', i);
105 distName = str.substring(i).trim();
107 distName = str.substring(i, idx).trim();
111 if (distName.equals(
"String"))
112 throw new IllegalArgumentException(
"Invalid distribution name: " + distName);
114 distClass = Class.forName(
"umontreal.ssj.probdist." + distName);
115 }
catch (ClassNotFoundException e) {
119 distClass = Class.forName(distName);
121 if (
Distribution.class.isAssignableFrom(distClass) ==
false)
122 throw new IllegalArgumentException(
"The given class is not a Probdist distribution class.");
123 }
catch (ClassNotFoundException ex) {
124 throw new IllegalArgumentException(
"Invalid distribution name: " + distName);
128 String paramStr =
"";
132 int parTo = str.lastIndexOf(
')');
134 paramStr = str.substring(parFrom + 1, parTo).trim();
135 if (paramStr.indexOf(
'(') != -1 || paramStr.indexOf(
')') != -1)
137 throw new IllegalArgumentException(
"Invalid parameter string: " + paramStr);
140 if (paramStr.equals(
"")) {
144 }
catch (IllegalAccessException e) {
145 throw new IllegalArgumentException(
"Default parameters not available");
146 }
catch (InstantiationException e) {
147 throw new IllegalArgumentException(
"Default parameters not available");
155 StringTokenizer paramTok =
new StringTokenizer(paramStr,
",");
156 int nparams = paramTok.countTokens();
157 Constructor[] cons = distClass.getConstructors();
158 Constructor<Distribution> distCons =
null;
159 Class[] paramTypes =
null;
161 for (i = 0; i < cons.length; i++) {
162 if (Modifier.isPublic(cons[i].getModifiers())
163 && ((paramTypes = cons[i].getParameterTypes()).length == nparams)) {
168 if (distCons ==
null)
169 throw new IllegalArgumentException(
"Invalid parameter number");
172 Object[] instParams =
new Object[nparams];
173 for (i = 0; i < nparams; i++) {
174 String par = paramTok.nextToken().trim();
177 if (paramTypes[i] ==
int.
class)
178 instParams[i] =
new Integer(par);
179 else if (paramTypes[i] ==
long.
class)
180 instParams[i] =
new Long(par);
181 else if (paramTypes[i] ==
float.
class) {
182 if (par.equalsIgnoreCase(
"infinity") || par.equalsIgnoreCase(
"+infinity"))
183 instParams[i] =
new Float(Float.POSITIVE_INFINITY);
184 else if (par.equalsIgnoreCase(
"-infinity"))
185 instParams[i] =
new Float(Float.NEGATIVE_INFINITY);
187 instParams[i] =
new Float(par);
188 }
else if (paramTypes[i] ==
double.
class) {
189 if (par.equalsIgnoreCase(
"infinity") || par.equalsIgnoreCase(
"+infinity"))
190 instParams[i] =
new Double(Double.POSITIVE_INFINITY);
191 else if (par.equalsIgnoreCase(
"-infinity"))
192 instParams[i] =
new Double(Double.NEGATIVE_INFINITY);
194 instParams[i] =
new Double(par);
196 throw new IllegalArgumentException(
197 "Parameter " + (i + 1) +
" type " + paramTypes[i].getName() +
"not supported");
198 }
catch (NumberFormatException e) {
199 throw new IllegalArgumentException(
"Parameter " + (i + 1) +
" of type " + paramTypes[i].getName()
200 +
" could not be converted from String");
207 }
catch (IllegalAccessException e) {
209 }
catch (InstantiationException e) {
211 }
catch (InvocationTargetException e) {
240 @SuppressWarnings(
"unchecked")
245 distClass = Class.forName(
"umontreal.ssj.probdist." + distName);
246 }
catch (ClassNotFoundException e) {
248 distClass = Class.forName(distName);
249 }
catch (ClassNotFoundException ex) {
250 throw new IllegalArgumentException(
"Invalid distribution name: " + distName);
267 @SuppressWarnings(
"unchecked")
272 distClass = Class.forName(
"umontreal.ssj.probdist." + distName);
273 }
catch (ClassNotFoundException e) {
275 distClass = Class.forName(distName);
276 }
catch (ClassNotFoundException ex) {
277 throw new IllegalArgumentException(
"Invalid distribution name: " + distName);
281 return getDistributionMLE((Class<? extends DiscreteDistributionInt>) distClass, x, n);
294 @SuppressWarnings(
"unchecked")
297 throw new IllegalArgumentException(
"The given class is not a Probdist distribution class.");
301 m = distClass.getMethod(
"getInstanceFromMLE",
double[].
class,
int.
class);
302 }
catch (NoSuchMethodException e) {
303 throw new IllegalArgumentException(
304 "The given class does not provide the static method getInstanceFromMLE (double[],int)");
306 if (!Modifier.isStatic(m.getModifiers()) || !distClass.isAssignableFrom(m.getReturnType()))
307 throw new IllegalArgumentException(
308 "The given class does not provide the static method getInstanceFromMLE (double[],int)");
311 return (T) m.invoke(
null, x, n);
312 }
catch (IllegalAccessException e) {
314 }
catch (IllegalArgumentException e) {
316 }
catch (InvocationTargetException e) {
331 @SuppressWarnings(
"unchecked")
334 throw new IllegalArgumentException(
"The given class is not a discrete distribution class over integers.");
338 m = distClass.getMethod(
"getInstanceFromMLE",
int[].
class,
int.
class);
339 }
catch (NoSuchMethodException e) {
340 throw new IllegalArgumentException(
341 "The given class does not provide the static method getInstanceFromMLE (int[],int)");
343 if (!Modifier.isStatic(m.getModifiers()) || !distClass.isAssignableFrom(m.getReturnType()))
344 throw new IllegalArgumentException(
345 "The given class does not provide the static method getInstanceFromMLE (int[],int)");
348 return (T) m.invoke(
null, x, n);
349 }
catch (IllegalAccessException e) {
351 }
catch (IllegalArgumentException e) {
353 }
catch (InvocationTargetException e) {