/** * Copyright (C) 2009 - present by OpenGamma Inc. and the OpenGamma group of companies * * Please see distribution for license. */ package com.opengamma.component; import java.net.URI; import java.util.HashMap; import java.util.Map; import java.util.Set; import org.joda.beans.Bean; import org.joda.beans.BeanBuilder; import org.joda.beans.BeanDefinition; 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.DirectBeanBuilder; import org.joda.beans.impl.direct.DirectMetaBean; import org.joda.beans.impl.direct.DirectMetaProperty; import org.joda.beans.impl.direct.DirectMetaPropertyMap; import com.opengamma.util.ArgumentChecker; /** * Information about a principal component of the OpenGamma system. */ @BeanDefinition public class ComponentInfo implements Bean { /** * The component type representing the available functionality. */ @PropertyDefinition(validate = "notNull") private Class<?> _type; /** * The classifier of the type. * This acts as a key to disambiguate multiple options for the same component type. */ @PropertyDefinition(validate = "notNull") private String _classifier; /** * The URI that the component is available at. * This may be absolute (typically another server) or relative (effectively relative to this server). */ @PropertyDefinition private URI _uri; /** * The extensible set of attributes that help describe the component. */ @PropertyDefinition(validate = "notNull") private final Map<String, String> _attributes = new HashMap<String, String>(); /** * Creates an instance. */ protected ComponentInfo() { } /** * Creates an instance. * * @param type the component type, not null * @param classifier the classifier, not null */ public ComponentInfo(Class<?> type, String classifier) { setType(type); setClassifier(classifier); } //------------------------------------------------------------------------- /** * Checks if this component matches the specified type and classifier. * * @param type the type of the component, typically an interface * @param classifier the classifier of the type, used to name instances of the same type * @return true if it matches */ public boolean matches(Class<?> type, String classifier) { return getType().equals(type) && getClassifier().equals(classifier); } /** * Converts this info to a key. * * @return the key for the component, not null */ public ComponentKey toComponentKey() { return ComponentKey.of(_type, _classifier); } //------------------------------------------------------------------------- /** * Adds a key value pair to attributes. * <p> * The value is converted to a string using {@link JodaBeanUtils#stringConverter()}. * * @param key the key to add, not null * @param value the value to add, not null */ public void addAttribute(String key, Object value) { ArgumentChecker.notNull(key, "key"); ArgumentChecker.notNull(value, "value"); String str = JodaBeanUtils.stringConverter().convertToString(value); _attributes.put(key, str); } /** * Gets a single attribute ensuring the value is not null. * * @param key the attribute key, not null * @return the value of the attribute, not null * @throws IllegalArgumentException if there is no attribute mapped to the key */ public String getAttribute(String key) { String attr = _attributes.get(key); if (attr == null) { throw new IllegalArgumentException("Unknown attribute '" + key + "': " + toComponentKey()); } return attr; } //------------------------- AUTOGENERATED START ------------------------- ///CLOVER:OFF /** * The meta-bean for {@code ComponentInfo}. * @return the meta-bean, not null */ public static ComponentInfo.Meta meta() { return ComponentInfo.Meta.INSTANCE; } static { JodaBeanUtils.registerMetaBean(ComponentInfo.Meta.INSTANCE); } @Override public ComponentInfo.Meta metaBean() { return ComponentInfo.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 component type representing the available functionality. * @return the value of the property, not null */ public Class<?> getType() { return _type; } /** * Sets the component type representing the available functionality. * @param type the new value of the property, not null */ public void setType(Class<?> type) { JodaBeanUtils.notNull(type, "type"); this._type = type; } /** * Gets the the {@code type} property. * @return the property, not null */ public final Property<Class<?>> type() { return metaBean().type().createProperty(this); } //----------------------------------------------------------------------- /** * Gets the classifier of the type. * This acts as a key to disambiguate multiple options for the same component type. * @return the value of the property, not null */ public String getClassifier() { return _classifier; } /** * Sets the classifier of the type. * This acts as a key to disambiguate multiple options for the same component type. * @param classifier the new value of the property, not null */ public void setClassifier(String classifier) { JodaBeanUtils.notNull(classifier, "classifier"); this._classifier = classifier; } /** * Gets the the {@code classifier} property. * This acts as a key to disambiguate multiple options for the same component type. * @return the property, not null */ public final Property<String> classifier() { return metaBean().classifier().createProperty(this); } //----------------------------------------------------------------------- /** * Gets the URI that the component is available at. * This may be absolute (typically another server) or relative (effectively relative to this server). * @return the value of the property */ public URI getUri() { return _uri; } /** * Sets the URI that the component is available at. * This may be absolute (typically another server) or relative (effectively relative to this server). * @param uri the new value of the property */ public void setUri(URI uri) { this._uri = uri; } /** * Gets the the {@code uri} property. * This may be absolute (typically another server) or relative (effectively relative to this server). * @return the property, not null */ public final Property<URI> uri() { return metaBean().uri().createProperty(this); } //----------------------------------------------------------------------- /** * Gets the extensible set of attributes that help describe the component. * @return the value of the property, not null */ public Map<String, String> getAttributes() { return _attributes; } /** * Sets the extensible set of attributes that help describe the component. * @param attributes the new value of the property, not null */ public void setAttributes(Map<String, String> attributes) { JodaBeanUtils.notNull(attributes, "attributes"); this._attributes.clear(); this._attributes.putAll(attributes); } /** * Gets the the {@code attributes} property. * @return the property, not null */ public final Property<Map<String, String>> attributes() { return metaBean().attributes().createProperty(this); } //----------------------------------------------------------------------- @Override public ComponentInfo clone() { return JodaBeanUtils.cloneAlways(this); } @Override public boolean equals(Object obj) { if (obj == this) { return true; } if (obj != null && obj.getClass() == this.getClass()) { ComponentInfo other = (ComponentInfo) obj; return JodaBeanUtils.equal(getType(), other.getType()) && JodaBeanUtils.equal(getClassifier(), other.getClassifier()) && JodaBeanUtils.equal(getUri(), other.getUri()) && JodaBeanUtils.equal(getAttributes(), other.getAttributes()); } return false; } @Override public int hashCode() { int hash = getClass().hashCode(); hash = hash * 31 + JodaBeanUtils.hashCode(getType()); hash = hash * 31 + JodaBeanUtils.hashCode(getClassifier()); hash = hash * 31 + JodaBeanUtils.hashCode(getUri()); hash = hash * 31 + JodaBeanUtils.hashCode(getAttributes()); return hash; } @Override public String toString() { StringBuilder buf = new StringBuilder(160); buf.append("ComponentInfo{"); 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("type").append('=').append(JodaBeanUtils.toString(getType())).append(',').append(' '); buf.append("classifier").append('=').append(JodaBeanUtils.toString(getClassifier())).append(',').append(' '); buf.append("uri").append('=').append(JodaBeanUtils.toString(getUri())).append(',').append(' '); buf.append("attributes").append('=').append(JodaBeanUtils.toString(getAttributes())).append(',').append(' '); } //----------------------------------------------------------------------- /** * The meta-bean for {@code ComponentInfo}. */ public static class Meta extends DirectMetaBean { /** * The singleton instance of the meta-bean. */ static final Meta INSTANCE = new Meta(); /** * The meta-property for the {@code type} property. */ @SuppressWarnings({"unchecked", "rawtypes" }) private final MetaProperty<Class<?>> _type = DirectMetaProperty.ofReadWrite( this, "type", ComponentInfo.class, (Class) Class.class); /** * The meta-property for the {@code classifier} property. */ private final MetaProperty<String> _classifier = DirectMetaProperty.ofReadWrite( this, "classifier", ComponentInfo.class, String.class); /** * The meta-property for the {@code uri} property. */ private final MetaProperty<URI> _uri = DirectMetaProperty.ofReadWrite( this, "uri", ComponentInfo.class, URI.class); /** * The meta-property for the {@code attributes} property. */ @SuppressWarnings({"unchecked", "rawtypes" }) private final MetaProperty<Map<String, String>> _attributes = DirectMetaProperty.ofReadWrite( this, "attributes", ComponentInfo.class, (Class) Map.class); /** * The meta-properties. */ private final Map<String, MetaProperty<?>> _metaPropertyMap$ = new DirectMetaPropertyMap( this, null, "type", "classifier", "uri", "attributes"); /** * Restricted constructor. */ protected Meta() { } @Override protected MetaProperty<?> metaPropertyGet(String propertyName) { switch (propertyName.hashCode()) { case 3575610: // type return _type; case -281470431: // classifier return _classifier; case 116076: // uri return _uri; case 405645655: // attributes return _attributes; } return super.metaPropertyGet(propertyName); } @Override public BeanBuilder<? extends ComponentInfo> builder() { return new DirectBeanBuilder<ComponentInfo>(new ComponentInfo()); } @Override public Class<? extends ComponentInfo> beanType() { return ComponentInfo.class; } @Override public Map<String, MetaProperty<?>> metaPropertyMap() { return _metaPropertyMap$; } //----------------------------------------------------------------------- /** * The meta-property for the {@code type} property. * @return the meta-property, not null */ public final MetaProperty<Class<?>> type() { return _type; } /** * The meta-property for the {@code classifier} property. * @return the meta-property, not null */ public final MetaProperty<String> classifier() { return _classifier; } /** * The meta-property for the {@code uri} property. * @return the meta-property, not null */ public final MetaProperty<URI> uri() { return _uri; } /** * The meta-property for the {@code attributes} property. * @return the meta-property, not null */ public final MetaProperty<Map<String, String>> attributes() { return _attributes; } //----------------------------------------------------------------------- @Override protected Object propertyGet(Bean bean, String propertyName, boolean quiet) { switch (propertyName.hashCode()) { case 3575610: // type return ((ComponentInfo) bean).getType(); case -281470431: // classifier return ((ComponentInfo) bean).getClassifier(); case 116076: // uri return ((ComponentInfo) bean).getUri(); case 405645655: // attributes return ((ComponentInfo) bean).getAttributes(); } return super.propertyGet(bean, propertyName, quiet); } @SuppressWarnings("unchecked") @Override protected void propertySet(Bean bean, String propertyName, Object newValue, boolean quiet) { switch (propertyName.hashCode()) { case 3575610: // type ((ComponentInfo) bean).setType((Class<?>) newValue); return; case -281470431: // classifier ((ComponentInfo) bean).setClassifier((String) newValue); return; case 116076: // uri ((ComponentInfo) bean).setUri((URI) newValue); return; case 405645655: // attributes ((ComponentInfo) bean).setAttributes((Map<String, String>) newValue); return; } super.propertySet(bean, propertyName, newValue, quiet); } @Override protected void validate(Bean bean) { JodaBeanUtils.notNull(((ComponentInfo) bean)._type, "type"); JodaBeanUtils.notNull(((ComponentInfo) bean)._classifier, "classifier"); JodaBeanUtils.notNull(((ComponentInfo) bean)._attributes, "attributes"); } } ///CLOVER:ON //-------------------------- AUTOGENERATED END -------------------------- }