Lucene.Net  3.0.3
Lucene.Net is a port of the Lucene search engine library, written in C# and targeted at .NET runtime users.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Pages
Attribute.cs
Go to the documentation of this file.
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 using System;
19 
20 namespace Lucene.Net.Util
21 {
22 
23  /// <summary> Base class for Attributes that can be added to a
24  /// <see cref="Lucene.Net.Util.AttributeSource" />.
25  /// <p/>
26  /// Attributes are used to add data in a dynamic, yet type-safe way to a source
27  /// of usually streamed objects, e. g. a <see cref="Lucene.Net.Analysis.TokenStream" />.
28  /// </summary>
29  [Serializable]
30  public abstract class Attribute : System.ICloneable, IAttribute
31  {
32  /// <summary> Clears the values in this AttributeImpl and resets it to its
33  /// default value. If this implementation implements more than one Attribute interface
34  /// it clears all.
35  /// </summary>
36  public abstract void Clear();
37 
38  /// <summary> The default implementation of this method accesses all declared
39  /// fields of this object and prints the values in the following syntax:
40  ///
41  /// <code>
42  /// public String toString() {
43  /// return "start=" + startOffset + ",end=" + endOffset;
44  /// }
45  /// </code>
46  ///
47  /// This method may be overridden by subclasses.
48  /// </summary>
49  public override System.String ToString()
50  {
51  System.Text.StringBuilder buffer = new System.Text.StringBuilder();
52  System.Type clazz = this.GetType();
53  System.Reflection.FieldInfo[] fields = clazz.GetFields(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.DeclaredOnly | System.Reflection.BindingFlags.Static);
54  try
55  {
56  for (int i = 0; i < fields.Length; i++)
57  {
58  System.Reflection.FieldInfo f = fields[i];
59  if (f.IsStatic)
60  continue;
61  //f.setAccessible(true); // {{Aroush-2.9}} java.lang.reflect.AccessibleObject.setAccessible
62  System.Object value_Renamed = f.GetValue(this);
63  if (buffer.Length > 0)
64  {
65  buffer.Append(',');
66  }
67  if (value_Renamed == null)
68  {
69  buffer.Append(f.Name + "=null");
70  }
71  else
72  {
73  buffer.Append(f.Name + "=" + value_Renamed);
74  }
75  }
76  }
77  catch (System.UnauthorizedAccessException e)
78  {
79  // this should never happen, because we're just accessing fields
80  // from 'this'
81  throw new System.SystemException(e.Message, e);
82  }
83 
84  return buffer.ToString();
85  }
86 
87  /// <summary> Subclasses must implement this method and should compute
88  /// a hashCode similar to this:
89  /// <code>
90  /// public int hashCode() {
91  /// int code = startOffset;
92  /// code = code * 31 + endOffset;
93  /// return code;
94  /// }
95  /// </code>
96  ///
97  /// see also <see cref="Equals(Object)" />
98  /// </summary>
99  abstract public override int GetHashCode();
100 
101  /// <summary> All values used for computation of <see cref="GetHashCode()" />
102  /// should be checked here for equality.
103  ///
104  /// see also <see cref="Object.Equals(Object)" />
105  /// </summary>
106  abstract public override bool Equals(System.Object other);
107 
108  /// <summary> Copies the values from this Attribute into the passed-in
109  /// target attribute. The target implementation must support all the
110  /// Attributes this implementation supports.
111  /// </summary>
112  public abstract void CopyTo(Attribute target);
113 
114  /// <summary> Shallow clone. Subclasses must override this if they
115  /// need to clone any members deeply,
116  /// </summary>
117  public virtual System.Object Clone()
118  {
119  System.Object clone = null;
120  try
121  {
122  clone = base.MemberwiseClone();
123  }
124  catch (System.Exception e)
125  {
126  throw new System.SystemException(e.Message, e); // shouldn't happen
127  }
128  return clone;
129  }
130  }
131 }