/**
* Copyright (C) 2009 - 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.convention.Convention;
import com.opengamma.id.ExternalIdBundle;
import com.opengamma.util.ArgumentChecker;
/**
* Represents a link to a Convention object using an ExternalIdBundle
* that can be resolved on demand.
*
* @param <T> type of the convention
*/
@BeanDefinition
public class ResolvableConventionLink<T extends Convention> extends ConventionLink<T> implements ImmutableBean, Serializable {
@PropertyDefinition(validate = "notNull")
private final LinkIdentifier<ExternalIdBundle, T> _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<ExternalIdBundle, T> _resolver;
/* package */ ResolvableConventionLink(ExternalIdBundle identifier,
Class<T> type,
LinkResolver<ExternalIdBundle, T> linkResolver) {
this(LinkIdentifier.of(identifier, type), linkResolver);
}
@ImmutableConstructor
private ResolvableConventionLink(LinkIdentifier<ExternalIdBundle, T> linkIdentifier) {
this(linkIdentifier, new ServiceContextConventionLinkResolver<T>());
}
private ResolvableConventionLink(LinkIdentifier<ExternalIdBundle, T> linkIdentifier,
LinkResolver<ExternalIdBundle, T> linkResolver) {
_identifier = linkIdentifier;
_resolver = ArgumentChecker.notNull(linkResolver, "linkResolver");
}
@Override
public T resolve() {
return _resolver.resolve(_identifier);
}
@Override
public Class<T> getTargetType() {
return _identifier.getType();
}
//------------------------- AUTOGENERATED START -------------------------
///CLOVER:OFF
/**
* The meta-bean for {@code ResolvableConventionLink}.
* @return the meta-bean, not null
*/
@SuppressWarnings("rawtypes")
public static ResolvableConventionLink.Meta meta() {
return ResolvableConventionLink.Meta.INSTANCE;
}
/**
* The meta-bean for {@code ResolvableConventionLink}.
* @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 Convention> ResolvableConventionLink.Meta<R> metaResolvableConventionLink(Class<R> cls) {
return ResolvableConventionLink.Meta.INSTANCE;
}
static {
JodaBeanUtils.registerMetaBean(ResolvableConventionLink.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 Convention> ResolvableConventionLink.Builder<T> builder() {
return new ResolvableConventionLink.Builder<T>();
}
@SuppressWarnings("unchecked")
@Override
public ResolvableConventionLink.Meta<T> metaBean() {
return ResolvableConventionLink.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 identifier.
* @return the value of the property, not null
*/
public LinkIdentifier<ExternalIdBundle, T> getIdentifier() {
return _identifier;
}
//-----------------------------------------------------------------------
/**
* 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()) {
ResolvableConventionLink<?> other = (ResolvableConventionLink<?>) 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("ResolvableConventionLink{");
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 ResolvableConventionLink}.
* @param <T> the type
*/
public static class Meta<T extends Convention> 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<ExternalIdBundle, T>> _identifier = DirectMetaProperty.ofImmutable(
this, "identifier", ResolvableConventionLink.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 ResolvableConventionLink.Builder<T> builder() {
return new ResolvableConventionLink.Builder<T>();
}
@SuppressWarnings({"unchecked", "rawtypes" })
@Override
public Class<? extends ResolvableConventionLink<T>> beanType() {
return (Class) ResolvableConventionLink.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<ExternalIdBundle, T>> identifier() {
return _identifier;
}
//-----------------------------------------------------------------------
@Override
protected Object propertyGet(Bean bean, String propertyName, boolean quiet) {
switch (propertyName.hashCode()) {
case -1618432855: // identifier
return ((ResolvableConventionLink<?>) 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 ResolvableConventionLink}.
* @param <T> the type
*/
public static class Builder<T extends Convention> extends DirectFieldsBeanBuilder<ResolvableConventionLink<T>> {
private LinkIdentifier<ExternalIdBundle, T> _identifier;
/**
* Restricted constructor.
*/
protected Builder() {
}
/**
* Restricted copy constructor.
* @param beanToCopy the bean to copy from, not null
*/
protected Builder(ResolvableConventionLink<T> 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<T> set(String propertyName, Object newValue) {
switch (propertyName.hashCode()) {
case -1618432855: // identifier
this._identifier = (LinkIdentifier<ExternalIdBundle, 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 ResolvableConventionLink<T> build() {
return new ResolvableConventionLink<T>(
_identifier);
}
//-----------------------------------------------------------------------
/**
* Sets the {@code identifier} property in the builder.
* @param identifier the new value, not null
* @return this, for chaining, not null
*/
public Builder<T> identifier(LinkIdentifier<ExternalIdBundle, T> identifier) {
JodaBeanUtils.notNull(identifier, "identifier");
this._identifier = identifier;
return this;
}
//-----------------------------------------------------------------------
@Override
public String toString() {
StringBuilder buf = new StringBuilder(64);
buf.append("ResolvableConventionLink.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 --------------------------
}