/** * Copyright (C) 2014 - present by OpenGamma Inc. and the OpenGamma group of companies * * Please see distribution for license. */ package com.opengamma.sesame.marketdata.scenarios; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Map; import java.util.NoSuchElementException; import java.util.Set; import org.joda.beans.Bean; import org.joda.beans.BeanDefinition; import org.joda.beans.ImmutableBean; import org.joda.beans.JodaBeanUtils; import org.joda.beans.MetaProperty; import org.joda.beans.Property; import org.joda.beans.PropertyDefinition; import org.joda.beans.impl.direct.DirectFieldsBeanBuilder; import org.joda.beans.impl.direct.DirectMetaBean; import org.joda.beans.impl.direct.DirectMetaProperty; import org.joda.beans.impl.direct.DirectMetaPropertyMap; import com.google.common.collect.ImmutableList; /** * Contains one or more market data perturbations and a filter that decides what market data they apply to. * <p> * The perturbations can apply to the input data used to build another piece of market data or the built market * data itself. */ @BeanDefinition public final class PerturbationMapping<T extends Perturbation> implements ImmutableBean { /** The filter that decides whether the perturbations should be applied to a piece of market data. */ @PropertyDefinition(validate = "notNull") private final MarketDataFilter _filter; /** Perturbations that should be applied to market data over multiple calculation cycles as part of a scenario. */ @PropertyDefinition(validate = "notEmpty") private final ImmutableList<T> _perturbations; /** * Returns a mapping containing a single perturbation. * * @param filter the filter used to choose the market data * @param perturbation the perturbation applied to any market data matching the filter * @return a mapping containing a single perturbation */ public static <T extends Perturbation> PerturbationMapping<T> of(MarketDataFilter filter, T perturbation) { return new PerturbationMapping<>(filter, ImmutableList.of(perturbation)); } /** * Returns a mapping containing multiple perturbations. * * @param filter the filter used to choose the market data * @param perturbations the perturbations applied to any market data matching the filter * @return a mapping containing multiple perturbations */ @SafeVarargs public static <T extends Perturbation> PerturbationMapping<T> of(MarketDataFilter filter, T... perturbations) { return new PerturbationMapping<>(filter, ImmutableList.copyOf(perturbations)); } /** * Returns a mapping containing multiple perturbations. * * @param filter the filter used to choose the market data * @param perturbations the perturbations applied to any market data matching the filter * @return a mapping containing multiple perturbations */ public static <T extends Perturbation> PerturbationMapping<T> of(MarketDataFilter filter, List<T> perturbations) { return new PerturbationMapping<>(filter, ImmutableList.copyOf(perturbations)); } //------------------------- AUTOGENERATED START ------------------------- ///CLOVER:OFF /** * The meta-bean for {@code PerturbationMapping}. * @return the meta-bean, not null */ @SuppressWarnings("rawtypes") public static PerturbationMapping.Meta meta() { return PerturbationMapping.Meta.INSTANCE; } /** * The meta-bean for {@code PerturbationMapping}. * @param <R> the bean's generic type * @param cls the bean's generic type * @return the meta-bean, not null */ @SuppressWarnings("unchecked") public static <R extends Perturbation> PerturbationMapping.Meta<R> metaPerturbationMapping(Class<R> cls) { return PerturbationMapping.Meta.INSTANCE; } static { JodaBeanUtils.registerMetaBean(PerturbationMapping.Meta.INSTANCE); } /** * Returns a builder used to create an instance of the bean. * @param <T> the type * @return the builder, not null */ public static <T extends Perturbation> PerturbationMapping.Builder<T> builder() { return new PerturbationMapping.Builder<T>(); } private PerturbationMapping( MarketDataFilter filter, List<T> perturbations) { JodaBeanUtils.notNull(filter, "filter"); JodaBeanUtils.notEmpty(perturbations, "perturbations"); this._filter = filter; this._perturbations = ImmutableList.copyOf(perturbations); } @SuppressWarnings("unchecked") @Override public PerturbationMapping.Meta<T> metaBean() { return PerturbationMapping.Meta.INSTANCE; } @Override public <R> Property<R> property(String propertyName) { return metaBean().<R>metaProperty(propertyName).createProperty(this); } @Override public Set<String> propertyNames() { return metaBean().metaPropertyMap().keySet(); } //----------------------------------------------------------------------- /** * Gets the filter that decides whether the perturbations should be applied to a piece of market data. * @return the value of the property, not null */ public MarketDataFilter getFilter() { return _filter; } //----------------------------------------------------------------------- /** * Gets perturbations that should be applied to market data over multiple calculation cycles as part of a scenario. * @return the value of the property, not empty */ public ImmutableList<T> getPerturbations() { return _perturbations; } //----------------------------------------------------------------------- /** * Returns a builder that allows this bean to be mutated. * @return the mutable builder, not null */ public Builder<T> toBuilder() { return new Builder<T>(this); } @Override public boolean equals(Object obj) { if (obj == this) { return true; } if (obj != null && obj.getClass() == this.getClass()) { PerturbationMapping<?> other = (PerturbationMapping<?>) obj; return JodaBeanUtils.equal(getFilter(), other.getFilter()) && JodaBeanUtils.equal(getPerturbations(), other.getPerturbations()); } return false; } @Override public int hashCode() { int hash = getClass().hashCode(); hash = hash * 31 + JodaBeanUtils.hashCode(getFilter()); hash = hash * 31 + JodaBeanUtils.hashCode(getPerturbations()); return hash; } @Override public String toString() { StringBuilder buf = new StringBuilder(96); buf.append("PerturbationMapping{"); buf.append("filter").append('=').append(getFilter()).append(',').append(' '); buf.append("perturbations").append('=').append(JodaBeanUtils.toString(getPerturbations())); buf.append('}'); return buf.toString(); } //----------------------------------------------------------------------- /** * The meta-bean for {@code PerturbationMapping}. * @param <T> the type */ public static final class Meta<T extends Perturbation> extends DirectMetaBean { /** * The singleton instance of the meta-bean. */ @SuppressWarnings("rawtypes") static final Meta INSTANCE = new Meta(); /** * The meta-property for the {@code filter} property. */ private final MetaProperty<MarketDataFilter> _filter = DirectMetaProperty.ofImmutable( this, "filter", PerturbationMapping.class, MarketDataFilter.class); /** * The meta-property for the {@code perturbations} property. */ @SuppressWarnings({"unchecked", "rawtypes" }) private final MetaProperty<ImmutableList<T>> _perturbations = DirectMetaProperty.ofImmutable( this, "perturbations", PerturbationMapping.class, (Class) ImmutableList.class); /** * The meta-properties. */ private final Map<String, MetaProperty<?>> _metaPropertyMap$ = new DirectMetaPropertyMap( this, null, "filter", "perturbations"); /** * Restricted constructor. */ private Meta() { } @Override protected MetaProperty<?> metaPropertyGet(String propertyName) { switch (propertyName.hashCode()) { case -1274492040: // filter return _filter; case 1397849260: // perturbations return _perturbations; } return super.metaPropertyGet(propertyName); } @Override public PerturbationMapping.Builder<T> builder() { return new PerturbationMapping.Builder<T>(); } @SuppressWarnings({"unchecked", "rawtypes" }) @Override public Class<? extends PerturbationMapping<T>> beanType() { return (Class) PerturbationMapping.class; } @Override public Map<String, MetaProperty<?>> metaPropertyMap() { return _metaPropertyMap$; } //----------------------------------------------------------------------- /** * The meta-property for the {@code filter} property. * @return the meta-property, not null */ public MetaProperty<MarketDataFilter> filter() { return _filter; } /** * The meta-property for the {@code perturbations} property. * @return the meta-property, not null */ public MetaProperty<ImmutableList<T>> perturbations() { return _perturbations; } //----------------------------------------------------------------------- @Override protected Object propertyGet(Bean bean, String propertyName, boolean quiet) { switch (propertyName.hashCode()) { case -1274492040: // filter return ((PerturbationMapping<?>) bean).getFilter(); case 1397849260: // perturbations return ((PerturbationMapping<?>) bean).getPerturbations(); } return super.propertyGet(bean, propertyName, quiet); } @Override protected void propertySet(Bean bean, String propertyName, Object newValue, boolean quiet) { metaProperty(propertyName); if (quiet) { return; } throw new UnsupportedOperationException("Property cannot be written: " + propertyName); } } //----------------------------------------------------------------------- /** * The bean-builder for {@code PerturbationMapping}. * @param <T> the type */ public static final class Builder<T extends Perturbation> extends DirectFieldsBeanBuilder<PerturbationMapping<T>> { private MarketDataFilter _filter; private List<T> _perturbations = new ArrayList<T>(); /** * Restricted constructor. */ private Builder() { } /** * Restricted copy constructor. * @param beanToCopy the bean to copy from, not null */ private Builder(PerturbationMapping<T> beanToCopy) { this._filter = beanToCopy.getFilter(); this._perturbations = new ArrayList<T>(beanToCopy.getPerturbations()); } //----------------------------------------------------------------------- @Override public Object get(String propertyName) { switch (propertyName.hashCode()) { case -1274492040: // filter return _filter; case 1397849260: // perturbations return _perturbations; default: throw new NoSuchElementException("Unknown property: " + propertyName); } } @SuppressWarnings("unchecked") @Override public Builder<T> set(String propertyName, Object newValue) { switch (propertyName.hashCode()) { case -1274492040: // filter this._filter = (MarketDataFilter) newValue; break; case 1397849260: // perturbations this._perturbations = (List<T>) newValue; break; default: throw new NoSuchElementException("Unknown property: " + propertyName); } return this; } @Override public Builder<T> set(MetaProperty<?> property, Object value) { super.set(property, value); return this; } @Override public Builder<T> setString(String propertyName, String value) { setString(meta().metaProperty(propertyName), value); return this; } @Override public Builder<T> setString(MetaProperty<?> property, String value) { super.setString(property, value); return this; } @Override public Builder<T> setAll(Map<String, ? extends Object> propertyValueMap) { super.setAll(propertyValueMap); return this; } @Override public PerturbationMapping<T> build() { return new PerturbationMapping<T>( _filter, _perturbations); } //----------------------------------------------------------------------- /** * Sets the {@code filter} property in the builder. * @param filter the new value, not null * @return this, for chaining, not null */ public Builder<T> filter(MarketDataFilter filter) { JodaBeanUtils.notNull(filter, "filter"); this._filter = filter; return this; } /** * Sets the {@code perturbations} property in the builder. * @param perturbations the new value, not empty * @return this, for chaining, not null */ public Builder<T> perturbations(List<T> perturbations) { JodaBeanUtils.notEmpty(perturbations, "perturbations"); this._perturbations = perturbations; return this; } /** * Sets the {@code perturbations} property in the builder * from an array of objects. * @param perturbations the new value, not empty * @return this, for chaining, not null */ public Builder<T> perturbations(T... perturbations) { return perturbations(Arrays.asList(perturbations)); } //----------------------------------------------------------------------- @Override public String toString() { StringBuilder buf = new StringBuilder(96); buf.append("PerturbationMapping.Builder{"); buf.append("filter").append('=').append(JodaBeanUtils.toString(_filter)).append(',').append(' '); buf.append("perturbations").append('=').append(JodaBeanUtils.toString(_perturbations)); buf.append('}'); return buf.toString(); } } ///CLOVER:ON //-------------------------- AUTOGENERATED END -------------------------- }