com.google.gdata.model
Class MetadataRegistry

java.lang.Object
  extended by com.google.gdata.model.MetadataRegistry

public final class MetadataRegistry
extends java.lang.Object

A mutable, thread-safe registry for metadata. This should be used to construct the default metadata for elements and attributes, the transforms on those default metadata instances, and any adaptations on the element types. The createSchema() method can be called to construct an immutable Schema from this builder.

Thread safety is guaranteed by always guarding access to the element and attribute maps by synchronizing on the builder itself. The individual element and attribute registries are also thread safe.


Constructor Summary
MetadataRegistry()
          Constructs a new empty metadata registry.
MetadataRegistry(MetadataRegistry source)
          Copy constructor, creates a copy of the given metadata registry.
 
Method Summary
<D,E extends Element>
void
adapt(ElementKey<D,E> source, java.lang.String kind, ElementKey<? extends D,? extends E> adaptation)
          Adapts from the source type to the adaptation type on the given kind.
 ElementCreator build(ElementKey<?,?> element)
          Builds element metadata for the given key.
 AttributeCreator build(ElementKey<?,?> parent, AttributeKey<?> attribute)
          Builds the metadata for the attribute inside the parent.
 AttributeCreator build(ElementKey<?,?> parent, AttributeKey<?> attribute, MetadataContext context)
          Builds the metadata for the attribute inside the parent, during the context.
 ElementCreator build(ElementKey<?,?> parent, ElementKey<?,?> element)
          Builds metadata for when the key is inside the parent.
 ElementCreator build(ElementKey<?,?> parent, ElementKey<?,?> element, MetadataContext context)
          Builds metadata for when the key is inside the parent and used in a context compatible with context.
 ElementCreator build(ElementKey<?,?> element, MetadataContext context)
          Builds metadata for when the key is used in a context compatible with context.
 Schema createSchema()
          Creates a Schema out of this registry.
 boolean isRegistered(ElementKey<?,?> key)
          Returns true if the given key has already been registered.
 MetadataRegistry merge(MetadataRegistry other)
          Merges another metadata registry into this metadata registry.
 MetadataRegistry register(ElementKey<?,?> key)
          Registers the metadata for an element key.
 MetadataRegistry registerClass(java.lang.Class<? extends Element> clazz)
          Registers the metadata from an element subclass using its "registerMetadata" method through reflection.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MetadataRegistry

public MetadataRegistry()
Constructs a new empty metadata registry.


MetadataRegistry

public MetadataRegistry(MetadataRegistry source)
Copy constructor, creates a copy of the given metadata registry. This is equivalent to new MetadataRegistry().merge(source).

Method Detail

merge

public MetadataRegistry merge(MetadataRegistry other)
Merges another metadata registry into this metadata registry. Both registries are locked during this time, first this registry and then the other registry. Do not attempt to do a.merge(b) and b.merge(a) in separate threads at the same time or a deadlock may occur.


register

public MetadataRegistry register(ElementKey<?,?> key)
Registers the metadata for an element key. This will call the "registerMetadata" method on the Element subclass that the key refers to, if it refers to a subclass. If it refers to Element directly it will just build the key.

Throws:
java.lang.IllegalArgumentException - if the element type does not support a registration method.

registerClass

public MetadataRegistry registerClass(java.lang.Class<? extends Element> clazz)
Registers the metadata from an element subclass using its "registerMetadata" method through reflection.


createSchema

public Schema createSchema()
Creates a Schema out of this registry. Will return a cached instance if the registry has not changed since last time create() was called.


isRegistered

public boolean isRegistered(ElementKey<?,?> key)
Returns true if the given key has already been registered. This is used to prevent reentrant metadata registration (and cycles).


build

public ElementCreator build(ElementKey<?,?> element)
Builds element metadata for the given key. This will create the default metadata for that key. If the key's element type has a superclass with existing metadata registered at the same ID, the new key will inherit any existing properties.


build

public ElementCreator build(ElementKey<?,?> parent,
                            ElementKey<?,?> element)
Builds metadata for when the key is inside the parent. Note that this will not declare the element as part of the parent element, that must be done using ElementCreator.addElement(ElementKey) or ElementCreator.replaceElement(ElementKey).


build

public ElementCreator build(ElementKey<?,?> element,
                            MetadataContext context)
Builds metadata for when the key is used in a context compatible with context.


build

public ElementCreator build(ElementKey<?,?> parent,
                            ElementKey<?,?> element,
                            MetadataContext context)
Builds metadata for when the key is inside the parent and used in a context compatible with context. Note that this will not declare the element as part of the parent element, that must be done using ElementCreator.addElement(ElementKey) or ElementCreator.replaceElement(ElementKey).

This will also guarantee that any element types that are referenced have been registered in this registry.


build

public AttributeCreator build(ElementKey<?,?> parent,
                              AttributeKey<?> attribute)
Builds the metadata for the attribute inside the parent. Note that this will not declare the attribute as part of the parent element, that must be done using ElementCreator.addAttribute(AttributeKey) or ElementCreator.replaceAttribute(AttributeKey).


build

public AttributeCreator build(ElementKey<?,?> parent,
                              AttributeKey<?> attribute,
                              MetadataContext context)
Builds the metadata for the attribute inside the parent, during the context. Note that this will not declare the attribute as part of the parent element, that must be done using ElementCreator.addAttribute(AttributeKey) or ElementCreator.replaceAttribute(AttributeKey).


adapt

public <D,E extends Element> void adapt(ElementKey<D,E> source,
                                        java.lang.String kind,
                                        ElementKey<? extends D,? extends E> adaptation)
Adapts from the source type to the adaptation type on the given kind.