package poly; /** * Serves as a factory for manufacturing Polynomial objects. * Uses the flyweight pattern to construct the zero polynomial.
 * Copyright 2000, by Dung X. Nguyen, All rights reserved.
 
* @author Dung X. Nguyen * @since January 01, 2000 * @dependency poly.APolynomial instantiates */ public class PolyFactory { /** @SBGen Variable (,zero polynomial,,64) */ public final static APolynomial ZeroPoly = new ConstPoly (0.0); /** * Creates a constant Polynomial. Uses the flyweight pattern for the zero polynomial. * @return a constant Polynomial. */ public static APolynomial MakeConstPoly (double coef) { return 0.0 == coef? ZeroPoly: new ConstPoly (coef); } /** * Creates a non-constant Polynomial with a given leading coefficient, a given degree, * and a given lower order polynomial. Checks for legal input parameters. * Will ignore zero leading coefficient. * @param coef the leading coefficient. * @param degree the degree, >= 0. * @param lowPoly != null, the lower ordered polynomial with degree < degree. * @return a non-constant Polynomial. * @exception throws IllegalArgumentException if conditions on degree and lowerPoly are violated. */ public static APolynomial MakePoly (double coef, int degree, APolynomial lowerPoly) { if (degree <= 0) { throw new IllegalArgumentException ("Degree must be positive!"); } if (null == lowerPoly) { throw new IllegalArgumentException ("lowerPoly must be non-null!"); } if (0.0 == coef) { return lowerPoly; } if (degree <= lowerPoly.getDegree()) { throw new IllegalArgumentException ("lowPoly is not a lower order term!"); } return new NonConstPoly (coef, degree, lowerPoly); } }