/** * Copyright (C) 2014 - present by OpenGamma Inc. and the OpenGamma group of companies * * Please see distribution for license. */ package com.opengamma.core.link; import java.io.Serializable; 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.ImmutableConstructor; 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.opengamma.core.marketdatasnapshot.NamedSnapshot; import com.opengamma.util.ArgumentChecker; /** * Represents a link to a snapshot object using a name * that can be resolved on demand. * * @param <S> type of the snapshot */ @BeanDefinition public class ResolvableSnapshotLink<S extends NamedSnapshot> extends SnapshotLink<S> implements ImmutableBean, Serializable { /** * The identification data for the object being linked to. */ @PropertyDefinition(validate = "notNull") private final LinkIdentifier<String, S> _identifier; /** * The resolver used to resolve the link on demand, not null. */ // note that the resolver does not form part of the serialized form // of the bean private final LinkResolver<String, S> _resolver; /** * Creates a resolved link. * * @param identifier the identifier for the linked object * @param type the type of the linked object * @param linkResolver the resolver used to resolve the link when requested */ ResolvableSnapshotLink(String identifier, Class<S> type, LinkResolver<String, S> linkResolver) { this(LinkIdentifier.of(identifier, type), linkResolver); } @ImmutableConstructor private ResolvableSnapshotLink(LinkIdentifier<String, S> identifier) { this(identifier, new ServiceContextSnapshotLinkResolver<S>()); } private ResolvableSnapshotLink(LinkIdentifier<String, S> identifier, LinkResolver<String, S> linkResolver) { _identifier = identifier; _resolver = ArgumentChecker.notNull(linkResolver, "linkResolver"); } @Override public S resolve() { return _resolver.resolve(_identifier); } @Override public Class<S> getTargetType() { return _identifier.getType(); } //------------------------- AUTOGENERATED START ------------------------- ///CLOVER:OFF /** * The meta-bean for {@code ResolvableSnapshotLink}. * @return the meta-bean, not null */ @SuppressWarnings("rawtypes") public static ResolvableSnapshotLink.Meta meta() { return ResolvableSnapshotLink.Meta.INSTANCE; } /** * The meta-bean for {@code ResolvableSnapshotLink}. * @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 NamedSnapshot> ResolvableSnapshotLink.Meta<R> metaResolvableSnapshotLink(Class<R> cls) { return ResolvableSnapshotLink.Meta.INSTANCE; } static { JodaBeanUtils.registerMetaBean(ResolvableSnapshotLink.Meta.INSTANCE); } /** * Returns a builder used to create an instance of the bean. * @param <S> the type * @return the builder, not null */ public static <S extends NamedSnapshot> ResolvableSnapshotLink.Builder<S> builder() { return new ResolvableSnapshotLink.Builder<S>(); } @SuppressWarnings("unchecked") @Override public ResolvableSnapshotLink.Meta<S> metaBean() { return ResolvableSnapshotLink.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 identification data for the object being linked to. * @return the value of the property, not null */ public LinkIdentifier<String, S> getIdentifier() { return _identifier; } //----------------------------------------------------------------------- /** * Returns a builder that allows this bean to be mutated. * @return the mutable builder, not null */ public Builder<S> toBuilder() { return new Builder<S>(this); } @Override public boolean equals(Object obj) { if (obj == this) { return true; } if (obj != null && obj.getClass() == this.getClass()) { ResolvableSnapshotLink<?> other = (ResolvableSnapshotLink<?>) obj; return JodaBeanUtils.equal(getIdentifier(), other.getIdentifier()); } return false; } @Override public int hashCode() { int hash = getClass().hashCode(); hash = hash * 31 + JodaBeanUtils.hashCode(getIdentifier()); return hash; } @Override public String toString() { StringBuilder buf = new StringBuilder(64); buf.append("ResolvableSnapshotLink{"); int len = buf.length(); toString(buf); if (buf.length() > len) { buf.setLength(buf.length() - 2); } buf.append('}'); return buf.toString(); } protected void toString(StringBuilder buf) { buf.append("identifier").append('=').append(JodaBeanUtils.toString(getIdentifier())).append(',').append(' '); } //----------------------------------------------------------------------- /** * The meta-bean for {@code ResolvableSnapshotLink}. * @param <S> the type */ public static class Meta<S extends NamedSnapshot> extends DirectMetaBean { /** * The singleton instance of the meta-bean. */ @SuppressWarnings("rawtypes") static final Meta INSTANCE = new Meta(); /** * The meta-property for the {@code identifier} property. */ @SuppressWarnings({"unchecked", "rawtypes" }) private final MetaProperty<LinkIdentifier<String, S>> _identifier = DirectMetaProperty.ofImmutable( this, "identifier", ResolvableSnapshotLink.class, (Class) LinkIdentifier.class); /** * The meta-properties. */ private final Map<String, MetaProperty<?>> _metaPropertyMap$ = new DirectMetaPropertyMap( this, null, "identifier"); /** * Restricted constructor. */ protected Meta() { } @Override protected MetaProperty<?> metaPropertyGet(String propertyName) { switch (propertyName.hashCode()) { case -1618432855: // identifier return _identifier; } return super.metaPropertyGet(propertyName); } @Override public ResolvableSnapshotLink.Builder<S> builder() { return new ResolvableSnapshotLink.Builder<S>(); } @SuppressWarnings({"unchecked", "rawtypes" }) @Override public Class<? extends ResolvableSnapshotLink<S>> beanType() { return (Class) ResolvableSnapshotLink.class; } @Override public Map<String, MetaProperty<?>> metaPropertyMap() { return _metaPropertyMap$; } //----------------------------------------------------------------------- /** * The meta-property for the {@code identifier} property. * @return the meta-property, not null */ public final MetaProperty<LinkIdentifier<String, S>> identifier() { return _identifier; } //----------------------------------------------------------------------- @Override protected Object propertyGet(Bean bean, String propertyName, boolean quiet) { switch (propertyName.hashCode()) { case -1618432855: // identifier return ((ResolvableSnapshotLink<?>) bean).getIdentifier(); } 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 ResolvableSnapshotLink}. * @param <S> the type */ public static class Builder<S extends NamedSnapshot> extends DirectFieldsBeanBuilder<ResolvableSnapshotLink<S>> { private LinkIdentifier<String, S> _identifier; /** * Restricted constructor. */ protected Builder() { } /** * Restricted copy constructor. * @param beanToCopy the bean to copy from, not null */ protected Builder(ResolvableSnapshotLink<S> beanToCopy) { this._identifier = beanToCopy.getIdentifier(); } //----------------------------------------------------------------------- @Override public Object get(String propertyName) { switch (propertyName.hashCode()) { case -1618432855: // identifier return _identifier; default: throw new NoSuchElementException("Unknown property: " + propertyName); } } @SuppressWarnings("unchecked") @Override public Builder<S> set(String propertyName, Object newValue) { switch (propertyName.hashCode()) { case -1618432855: // identifier this._identifier = (LinkIdentifier<String, S>) newValue; break; default: throw new NoSuchElementException("Unknown property: " + propertyName); } return this; } @Override public Builder<S> set(MetaProperty<?> property, Object value) { super.set(property, value); return this; } @Override public Builder<S> setString(String propertyName, String value) { setString(meta().metaProperty(propertyName), value); return this; } @Override public Builder<S> setString(MetaProperty<?> property, String value) { super.setString(property, value); return this; } @Override public Builder<S> setAll(Map<String, ? extends Object> propertyValueMap) { super.setAll(propertyValueMap); return this; } @Override public ResolvableSnapshotLink<S> build() { return new ResolvableSnapshotLink<S>( _identifier); } //----------------------------------------------------------------------- /** * Sets the {@code identifier} property in the builder. * @param identifier the new value, not null * @return this, for chaining, not null */ public Builder<S> identifier(LinkIdentifier<String, S> identifier) { JodaBeanUtils.notNull(identifier, "identifier"); this._identifier = identifier; return this; } //----------------------------------------------------------------------- @Override public String toString() { StringBuilder buf = new StringBuilder(64); buf.append("ResolvableSnapshotLink.Builder{"); int len = buf.length(); toString(buf); if (buf.length() > len) { buf.setLength(buf.length() - 2); } buf.append('}'); return buf.toString(); } protected void toString(StringBuilder buf) { buf.append("identifier").append('=').append(JodaBeanUtils.toString(_identifier)).append(',').append(' '); } } ///CLOVER:ON //-------------------------- AUTOGENERATED END -------------------------- }