//-----------------------------------------------------------------------
// 
//     Copyright (c) Microsoft Corporation.
// 
//-----------------------------------------------------------------------
namespace Microsoft.Isam.Esent.Interop
{
    using System;
    using System.Diagnostics;
    using System.Globalization;
    /// 
    /// Describes one segment of an index.
    /// 
    [Serializable]
    public class IndexSegment : IEquatable
    {
        /// 
        /// The name of the column.
        /// 
        private readonly string columnName;
        /// 
        /// The type of the column.
        /// 
        private readonly JET_coltyp coltyp;
        /// 
        /// True if the column is sorted in ascending order.
        /// 
        private readonly bool isAscending;
        /// 
        /// True if the column is an ASCII column.
        /// 
        private readonly bool isASCII;
        /// 
        /// Initializes a new instance of the IndexSegment class.
        /// 
        /// The name of the indexed column.
        /// The type of the column.
        /// True if the column is ascending.
        /// True if the column is over an ASCII column.
        internal IndexSegment(
            string name,
            JET_coltyp coltyp,
            bool isAscending,
            bool isASCII)
        {
            this.columnName = name;
            this.coltyp = coltyp;
            this.isAscending = isAscending;
            this.isASCII = isASCII;
        }
        /// 
        /// Gets name of the column being indexed.
        /// 
        public string ColumnName
        {
            [DebuggerStepThrough]
            get { return this.columnName; }
        }
        /// 
        /// Gets the type of the column being indexed.
        /// 
        public JET_coltyp Coltyp
        {
            [DebuggerStepThrough]
            get { return this.coltyp; }
        }
        /// 
        /// Gets a value indicating whether the index segment is ascending.
        /// 
        public bool IsAscending
        {
            [DebuggerStepThrough]
            get { return this.isAscending; }
        }
        /// 
        /// Gets a value indicating whether the index segment is over an ASCII text
        /// column. This value is only meaningful for text column segments.
        /// 
        public bool IsASCII
        {
            [DebuggerStepThrough]
            get { return this.isASCII; }
        }
        /// 
        /// Returns a value indicating whether this instance is equal
        /// to another instance.
        /// 
        /// An object to compare with this instance.
        /// True if the two instances are equal.
        public override bool Equals(object obj)
        {
            if (obj == null || this.GetType() != obj.GetType())
            {
                return false;
            }
            return this.Equals((IndexSegment)obj);
        }
        /// 
        /// Generate a string representation of the instance.
        /// 
        /// The structure as a string.
        public override string ToString()
        {
            return string.Format(
                CultureInfo.InvariantCulture, "{0}{1}({2})", this.isAscending ? "+" : "-", this.columnName, this.coltyp);
        }
        /// 
        /// Returns the hash code for this instance.
        /// 
        /// The hash code for this instance.
        public override int GetHashCode()
        {
            return this.columnName.GetHashCode()
                ^ (int)this.coltyp * 31
                ^ (this.isAscending ? 0x10000 : 0x20000)
                ^ (this.isASCII ? 0x40000 : 0x80000);
        }
        /// 
        /// Returns a value indicating whether this instance is equal
        /// to another instance.
        /// 
        /// An instance to compare with this instance.
        /// True if the two instances are equal.
        public bool Equals(IndexSegment other)
        {
            if (null == other)
            {
                return false;
            }
            return this.columnName.Equals(other.columnName, StringComparison.OrdinalIgnoreCase)
                   && this.coltyp == other.coltyp
                   && this.isAscending == other.isAscending
                   && this.isASCII == other.isASCII;
        }
    }
}