Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

LUCENENET-515: Bring back AttributeSource.GetAttribute(Type)

git-svn-id: https://svn.apache.org/repos/asf/lucene.net/trunk@1423359 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information...
commit 855782fd4be0b15f0ff06588ce817172a9ba0050 1 parent 283e9b5
Simon Svensson authored
Showing with 15 additions and 10 deletions.
  1. +15 −10 src/core/Util/AttributeSource.cs
View
25 src/core/Util/AttributeSource.cs
@@ -267,7 +267,7 @@ 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));
}
@@ -275,7 +275,8 @@ public virtual bool HasAttributes
/// <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");
@@ -287,7 +288,7 @@ 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
@@ -295,7 +296,7 @@ public virtual bool HasAttributes
/// 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));
}
@@ -304,7 +305,7 @@ 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
@@ -312,17 +313,21 @@ public virtual bool HasAttributes
/// 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>
Please sign in to comment.
Something went wrong with that request. Please try again.