Initial revision
[beagle.git] / Lucene.Net / Index / Term.cs
blob9f8ec479f2decce59fc2a803fc1aa8e740758f6d
1 using System;
2 using System.Globalization;
3 using System.Runtime.Serialization;
5 namespace Lucene.Net.Index
7 /* ====================================================================
8 * The Apache Software License, Version 1.1
10 * Copyright (c) 2001 The Apache Software Foundation. All rights
11 * reserved.
13 * Redistribution and use _in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions _in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer _in
22 * the documentation and/or other materials provided with the
23 * distribution.
25 * 3. The end-user documentation included with the redistribution,
26 * if any, must include the following acknowledgment:
27 * "This product includes software developed by the
28 * Apache Software Foundation (http://www.apache.org/)."
29 * Alternately, this acknowledgment may appear _in the software itself,
30 * if and wherever such third-party acknowledgments normally appear.
32 * 4. The names "Apache" and "Apache Software Foundation" and
33 * "Apache Lucene" must not be used to endorse or promote products
34 * derived from this software without prior written permission. For
35 * written permission, please contact apache@apache.org.
37 * 5. Products derived from this software may not be called "Apache",
38 * "Apache Lucene", nor may "Apache" appear _in their name, without
39 * prior written permission of the Apache Software Foundation.
41 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
42 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
43 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
44 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
45 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
46 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
47 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
48 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
49 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
50 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
51 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52 * SUCH DAMAGE.
53 * ====================================================================
55 * This software consists of voluntary contributions made by many
56 * individuals on behalf of the Apache Software Foundation. For more
57 * information on the Apache Software Foundation, please see
58 * <http://www.apache.org/>.
61 /// <summary>
62 /// A Term represents a word from text. This is the unit of search. It is
63 /// composed of two elements, the text of the word, as a string, and the name of
64 /// the field that the text occured in, an interned string.
65 /// Note that terms may represent more than words from text fields, but also
66 /// things like dates, email addresses, urls, etc.
67 /// </summary>
68 [Serializable]
69 public sealed class Term : ISerializable
71 internal String field;
72 internal String text;
74 /// <summary>
75 /// Constructs a Term with the given field and text.
76 /// </summary>
77 /// <param name="fld"></param>
78 /// <param name="txt"></param>
79 public Term(String fld, String txt) : this(fld, txt, true)
83 internal Term(String fld, String txt, bool intern)
85 field = intern ? String.Intern(fld) : fld; // field names are interned
86 text = txt; // unless already known to be
89 /// <summary>
90 /// Deserialization constructor
91 /// </summary>
92 /// <param name="info"></param>
93 /// <param name="context"></param>
94 public Term(SerializationInfo info, StreamingContext context)
96 field = (String)info.GetValue("field", typeof(String));
97 text = (String)info.GetValue("text", typeof(String));
98 field = String.Intern(field);
101 /// <summary>
102 /// Serialization function.
103 /// </summary>
104 /// <param name="info"></param>
105 /// <param name="context"></param>
106 public void GetObjectData(SerializationInfo info, StreamingContext context)
108 info.AddValue("field", field);
109 info.AddValue("text", text);
112 /// <summary>
113 /// Returns the field of this term, an interned string. The field indicates
114 /// the part of a document which this term came from.
115 /// </summary>
116 /// <returns></returns>
117 public String Field() { return field; }
119 /// <summary>
120 /// Returns the text of this term. In the case of words, this is simply the
121 /// text of the word. In the case of dates and other types, this is an
122 /// encoding of the object as a string.
123 /// </summary>
124 /// <returns></returns>
125 public String Text() { return text; }
127 /// <summary>
128 /// Compares two terms, returning true iff they have the same
129 /// field and text.
130 /// </summary>
131 /// <param name="o"></param>
132 /// <returns></returns>
133 override public bool Equals(object o)
135 if (o == null)
136 return false;
137 Term other = (Term)o;
138 return field == other.field && text.Equals(other.text);
141 /// <summary>
142 /// Combines the GetHashCode() of the field and the text.
143 /// </summary>
144 /// <returns></returns>
145 public override int GetHashCode()
147 return field.GetHashCode() + text.GetHashCode();
150 /// <summary>
151 /// Compares two terms, returning an integer which is less than zero iff this
152 /// term belongs after the argument, equal zero iff this term is equal to the
153 /// argument, and greater than zero iff this term belongs after the argument.
154 /// The ordering of terms is first by field, then by text.
155 /// </summary>
156 /// <param name="other"></param>
157 /// <returns></returns>
158 public int CompareTo(Term other)
160 if (field == other.field) // fields are interned
162 return String.CompareOrdinal(text, other.text);
164 else
165 return field.CompareTo(other.field);
168 /// <summary>
169 /// Resets the field and text of a Term.
170 /// </summary>
171 /// <param name="fld"></param>
172 /// <param name="txt"></param>
173 internal void Set(String fld, String txt)
175 field = fld;
176 text = txt;
179 override public String ToString() { return field + ":" + text; }