net.finmath.modelling.productfactory.ProductFactoryCascade Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of finmath-lib Show documentation
Show all versions of finmath-lib Show documentation
finmath lib is a Mathematical Finance Library in Java.
It provides algorithms and methodologies related to mathematical finance.
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 extends T> 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 extends T> factory) {
final ArrayList> factories = new ArrayList<>(this.factories.size()+1);
factories.addAll(this.factories);
factories.add(factory);
return new ProductFactoryCascade<>(factories);
}
@Override
public DescribedProduct extends T> getProductFromDescriptor(final ProductDescriptor productDescriptor) {
DescribedProduct extends T> product = null;
for(final ProductFactory extends T> 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());
}
}