Skip to content

Commit

Permalink
LUCENENET-515: Bring back AttributeSource.GetAttribute(Type)
Browse files Browse the repository at this point in the history
git-svn-id: https://svn.apache.org/repos/asf/lucene.net/trunk@1423359 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
Simon Svensson committed Dec 18, 2012
1 parent 283e9b5 commit 855782f
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions src/core/Util/AttributeSource.cs
Expand Up @@ -267,15 +267,16 @@ public virtual bool HasAttributes
/// <summary>
/// Returns true, iff this AttributeSource contains the passed-in attribute type.
/// </summary>
public virtual bool HasAttribute<T>() where T : IAttribute
public bool HasAttribute<T>() where T : IAttribute
{
return HasAttribute(typeof(T));
}

/// <summary>
/// Returns true, iff this AttributeSource contains the passed-in attribute type.
/// </summary>
public virtual bool HasAttribute(Type attrType) {
public virtual bool HasAttribute(Type attrType)
{
if (attrType == null)
throw new ArgumentNullException("attrType");

Expand All @@ -287,15 +288,15 @@ public virtual bool HasAttributes
/// Returns the instance of the passed in Attribute contained in this AttributeSource
/// </summary>
/// <throws>
/// IllegalArgumentException if this AttributeSource does not contain the Attribute.
/// ArgumentException if this AttributeSource does not contain the Attribute.
/// It is recommended to always use <see cref="AddAttribute{T}" /> even in consumers
/// of TokenStreams, because you cannot know if a specific TokenStream really uses
/// a specific Attribute. <see cref="AddAttribute{T}" /> will automatically make the attribute
/// available. If you want to only use the attribute, if it is available (to optimize
/// consuming), use <see cref="HasAttribute" />.
/// </throws>
// NOTE: Java has Class<T>, .NET has no Type<T>, this is not a perfect port
public virtual T GetAttribute<T>() where T : IAttribute
public T GetAttribute<T>() where T : IAttribute
{
return (T)GetAttribute(typeof(T));
}
Expand All @@ -304,25 +305,29 @@ public virtual bool HasAttributes
/// Returns the instance of the passed in attribute type contained in this AttributeSource
/// </summary>
/// <throws>
/// IllegalArgumentException if this AttributeSource does not contain the Attribute.
/// ArgumentException if this AttributeSource does not contain the Attribute.
/// It is recommended to always use <see cref="AddAttribute{T}" /> even in consumers
/// of TokenStreams, because you cannot know if a specific TokenStream really uses
/// a specific Attribute. <see cref="AddAttribute{T}" /> will automatically make the attribute
/// available. If you want to only use the attribute, if it is available (to optimize
/// consuming), use <see cref="HasAttribute" />.
/// </throws>
// NOTE: Java has Class<T>, .NET has no Type<T>, this is not a perfect port
public virtual IAttribute GetAttribute(Type attrType) {
public virtual IAttribute GetAttribute(Type attrType)
{
if (attrType == null)
throw new ArgumentNullException("attrType");

if (attributes.ContainsKey(attrType))
return attributes[attrType].Value;

// This check is done after .ContainsKey to avoid the check
// when called from GetAttribute<T> (where T : IAttribute),
// the compiler enforces that constraint.
if (!typeof(IAttribute).IsAssignableFrom(attrType))
throw new ArgumentException("The passed type is not assignable from IAttribute.", "attrType");

if (!attributes.ContainsKey(attrType))
throw new ArgumentException("This AttributeSource does not have the attribute '" + attrType.FullName + "'.");

return attributes[attrType].Value;
throw new ArgumentException("This AttributeSource does not have the attribute '" + attrType.FullName + "'.");
}

/// <summary> This class holds the state of an AttributeSource.</summary>
Expand Down

0 comments on commit 855782f

Please sign in to comment.