using System; using System.Collections.Generic; using System.Text; namespace ScrewTurn.Wiki.SearchEngine { /// /// Describes the location of a word in a document. /// public class WordLocation : IComparable, IEquatable { private byte location; private string label; private float relativeRelevance; /// /// Initializes a new instance of the class. /// /// A number representing the location. /// The label of the instance. /// The relative relevance of the instance. protected WordLocation(byte location, string label, float relativeRelevance) { this.location = location; this.label = label; this.relativeRelevance = relativeRelevance; } /// /// Gets the location identifier. /// /// This property should only be used for serialization purposes. public byte Location { get { return location; } } /// /// Gets the relative relevance of the word location. /// public float RelativeRelevance { get { return relativeRelevance; } } /// /// Gets a string representation of the current instance. /// /// The string representation. public override string ToString() { return label; } /// /// Represents a word that is in the title of a document. /// public static WordLocation Title { get { return new WordLocation(1, "Title", 2); } } /// /// Represents a word that is in the keywords of a document. /// public static WordLocation Keywords { get { return new WordLocation(2, "Keywords", 1.5F); } } /// /// Represents a word that is in the content of a document. /// public static WordLocation Content { get { return new WordLocation(3, "Content", 1); } } /// /// Gets the correct instance from the location identifier. /// /// The location identifier. /// The correct instance. /// If is different from 1, 2, 3. public static WordLocation GetInstance(byte location) { switch(location) { case 1: return Title; case 2: return Keywords; case 3: return Content; default: throw new ArgumentOutOfRangeException("Invalid location", "location"); } } /// /// Compares the current instance to another. /// /// The other instance. /// The comparison result. public int CompareTo(WordLocation other) { if(object.ReferenceEquals(other, null)) return 1; if(location > other.location) return 1; else if(location < other.location) return -1; else return 0; //return location.CompareTo(other.location); } /// /// Determines whether the current instance equals an object. /// /// The object. /// true if the current instance equals the object, false otherwise. public override bool Equals(object obj) { if(obj is WordLocation) return Equals((WordLocation)obj); else return false; } /// /// Returns the hash code of the current instance. /// /// The hash code. public override int GetHashCode() { return location; } /// /// Determines whether the current instance equals another. /// /// The other instance. /// true if the current instance equals the other, false otherwise. public bool Equals(WordLocation other) { if(object.ReferenceEquals(other, null)) return false; else return location == other.location; } /// /// Applies the value-equality operator to two objects. /// /// The first object. /// The second object. /// true if the objects are value-equal, false otherwise. public static bool operator ==(WordLocation x, WordLocation y) { if(object.ReferenceEquals(x, null) && !object.ReferenceEquals(y, null)) return false; if(!object.ReferenceEquals(x, null) && object.ReferenceEquals(y, null)) return false; if(object.ReferenceEquals(x, null) && object.ReferenceEquals(y, null)) return true; return x.Equals(y); } /// /// Applies the value-inequality operator to two objects. /// /// The first object. /// The second object. /// true if the objects are not value-equal, false otherwise. public static bool operator !=(WordLocation x, WordLocation y) { return !(x == y); } } }