All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.finmath.modelling.productfactory.ProductFactoryCascade Maven / Gradle / Ivy

Go to download

finmath lib is a Mathematical Finance Library in Java. It provides algorithms and methodologies related to mathematical finance.

There is a newer version: 6.0.19
Show newest version
package net.finmath.modelling.productfactory;

import java.util.ArrayList;
import java.util.List;

import net.finmath.modelling.DescribedProduct;
import net.finmath.modelling.ProductDescriptor;
import net.finmath.modelling.ProductFactory;

/**
 * Implements a product factory based on a cascade of given factories. When invoking getProductFromDescriptor(ProductDescriptor productDescriptor)
 * the cascade will query all its individual factories until one builds the product. When querying the factories the cascade will start at index 0.
 *
 * @author Christian Fries
 * @author Roland Bachl
 *
 * @param  The base class of the product descriptors which can be handled by this ProductFactory.
 * @version 1.0
 */
public class ProductFactoryCascade implements ProductFactory {

	private final ArrayList> factories;

	/**
	 * Construct an empty factory cascade. This will build no products until amended.
	 */
	public ProductFactoryCascade() {
		super();
		factories = new ArrayList<>(0);
	}

	/**
	 * Construct a factory cascade from an ordered list of product factories. When querying the factories the cascade will start at index 0.
	 *
	 * @param factories A list of product factories, i.e. object implementing ProductFactory for the product descriptor T.
	 */
	public ProductFactoryCascade(final List> factories) {
		super();
		this.factories = new ArrayList<>();
		this.factories.addAll(factories);
	}

	/**
	 * Add a given factory to the list of factories at the BEGINNING.
	 *
	 * @param factory The factory to be added.
	 * @return Cascade with amended factory list.
	 */
	public ProductFactoryCascade addFactoryBefore(final ProductFactory factory) {
		final ArrayList> factories = new ArrayList<>(this.factories.size()+1);
		factories.addAll(this.factories);
		factories.add(0, factory);
		return new ProductFactoryCascade<>(factories);
	}

	/**
	 * Add a given factory to the list of factories at the END.
	 *
	 * @param factory The factory to be added.
	 * @return Cascade with amended factory list.
	 */
	public ProductFactoryCascade addFactoryAfter(final ProductFactory factory) {
		final ArrayList> factories = new ArrayList<>(this.factories.size()+1);
		factories.addAll(this.factories);
		factories.add(factory);
		return new ProductFactoryCascade<>(factories);
	}

	@Override
	public DescribedProduct getProductFromDescriptor(final ProductDescriptor productDescriptor) {
		DescribedProduct product = null;
		for(final ProductFactory factory : factories) {
			try {
				product = factory.getProductFromDescriptor(productDescriptor);
			} catch ( final IllegalArgumentException e) {continue;}
			if(product != null) {
				return product;
			}
		}

		// Product not found
		throw new IllegalArgumentException("Unsupported product type " + productDescriptor.name());
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy