From: Donald L M. Jr. <lu...@us...> - 2004-12-10 18:25:09
|
Update of /cvsroot/nhibernate/NHibernateContrib/src/Nullables In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv5667/src/Nullables Modified Files: NullableBoolean.cs NullableByte.cs NullableDateTime.cs NullableDecimal.cs NullableDouble.cs NullableGuid.cs NullableInt16.cs NullableInt32.cs NullableInt64.cs NullableSingle.cs Added Files: NullableSByte.cs Log Message: Implemented IComparable. Added NullableSByte, but it is inactive until further investigation. Index: NullableInt16.cs =================================================================== RCS file: /cvsroot/nhibernate/NHibernateContrib/src/Nullables/NullableInt16.cs,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** NullableInt16.cs 7 Dec 2004 21:32:49 -0000 1.2 --- NullableInt16.cs 10 Dec 2004 18:24:22 -0000 1.3 *************** *** 4,8 **** { [System.ComponentModel.TypeConverter(typeof(Nullables.TypeConverters.NullableInt16Converter)), Serializable()] ! public struct NullableInt16 : INullableType, IFormattable { public static readonly NullableInt16 Default = new NullableInt16(); --- 4,8 ---- { [System.ComponentModel.TypeConverter(typeof(Nullables.TypeConverters.NullableInt16Converter)), Serializable()] ! public struct NullableInt16 : INullableType, IFormattable, IComparable { public static readonly NullableInt16 Default = new NullableInt16(); *************** *** 166,169 **** --- 166,207 ---- #endregion + + #region IComparable Members + + public int CompareTo(object obj) + { + if (obj is NullableInt16) //chack and unbox + { + NullableInt16 value = (NullableInt16)obj; + + if (value.HasValue == this.HasValue) //both null or not null + { + if (this.HasValue) //this has a value, so they both do + return Value.CompareTo(value.Value); + else + return 0; //both null, so they are equal; + } + else //one is null + { + if (HasValue) //he have a value, so we are greater. + return 1; + else + return -1; + } + } + else if (obj is Int16) + { + Int16 value = (Int16)obj; + + if (HasValue) //not null, so compare the real values. + return Value.CompareTo(value); + else + return -1; //this is null, so less that the real value; + } + + throw new ArgumentException("NullableInt16 can only compare to another NullableInt16 or a System.Int16"); + } + + #endregion } } Index: NullableInt32.cs =================================================================== RCS file: /cvsroot/nhibernate/NHibernateContrib/src/Nullables/NullableInt32.cs,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** NullableInt32.cs 7 Dec 2004 21:32:49 -0000 1.2 --- NullableInt32.cs 10 Dec 2004 18:24:22 -0000 1.3 *************** *** 5,9 **** { [System.ComponentModel.TypeConverter(typeof(Nullables.TypeConverters.NullableInt32Converter)), Serializable()] ! public struct NullableInt32 : INullableType, IFormattable { public static readonly NullableInt32 Default = new NullableInt32(); --- 5,9 ---- { [System.ComponentModel.TypeConverter(typeof(Nullables.TypeConverters.NullableInt32Converter)), Serializable()] ! public struct NullableInt32 : INullableType, IFormattable, IComparable { public static readonly NullableInt32 Default = new NullableInt32(); *************** *** 167,170 **** --- 167,208 ---- #endregion + + #region IComparable Members + + public int CompareTo(object obj) + { + if (obj is NullableInt32) //chack and unbox + { + NullableInt32 value = (NullableInt32)obj; + + if (value.HasValue == this.HasValue) //both null or not null + { + if (this.HasValue) //this has a value, so they both do + return Value.CompareTo(value.Value); + else + return 0; //both null, so they are equal; + } + else //one is null + { + if (HasValue) //he have a value, so we are greater. + return 1; + else + return -1; + } + } + else if (obj is DateTime) + { + Int32 value = (Int32)obj; + + if (HasValue) //not null, so compare the real values. + return Value.CompareTo(value); + else + return -1; //this is null, so less that the real value; + } + + throw new ArgumentException("NullableInt32 can only compare to another NullableInt32 or a System.Int32"); + } + + #endregion } } Index: NullableByte.cs =================================================================== RCS file: /cvsroot/nhibernate/NHibernateContrib/src/Nullables/NullableByte.cs,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** NullableByte.cs 7 Dec 2004 21:32:49 -0000 1.2 --- NullableByte.cs 10 Dec 2004 18:24:22 -0000 1.3 *************** *** 4,8 **** { [System.ComponentModel.TypeConverter(typeof(Nullables.TypeConverters.NullableByteConverter)), Serializable()] ! public struct NullableByte : INullableType, IFormattable { public static readonly NullableByte Default = new NullableByte(); --- 4,8 ---- { [System.ComponentModel.TypeConverter(typeof(Nullables.TypeConverters.NullableByteConverter)), Serializable()] ! public struct NullableByte : INullableType, IFormattable, IComparable { public static readonly NullableByte Default = new NullableByte(); *************** *** 166,169 **** --- 166,207 ---- #endregion + + #region IComparable Members + + public int CompareTo(object obj) + { + if (obj is NullableByte) //chack and unbox + { + NullableByte value = (NullableByte)obj; + + if (value.HasValue == this.HasValue) //both null or not null + { + if (this.HasValue) //this has a value, so they both do + return Value.CompareTo(value.Value); + else + return 0; //both null, so they are equal; + } + else //one is null + { + if (HasValue) //he have a value, so we are greater. + return 1; + else + return -1; + } + } + else if (obj is Byte) + { + Byte value = (Byte)obj; + + if (HasValue) //not null, so compare the real values. + return Value.CompareTo(value); + else + return -1; //this is null, so less that the real value; + } + + throw new ArgumentException("NullableByte can only compare to another NullableByte or a System.Byte"); + } + + #endregion } } --- NEW FILE: NullableSByte.cs --- using System; //Contributed by Sergey Koshcheyev namespace Nullables { [System.ComponentModel.TypeConverter(typeof(Nullables.TypeConverters.NullableSByteConverter)), Serializable()] public struct NullableSByte : INullableType, IFormattable, IComparable { public static readonly NullableSByte Default = new NullableSByte(); SByte _value; bool hasValue; #region Constructors public NullableSByte(SByte value) { _value = value; hasValue = true; } #endregion #region INullable Members object INullableType.Value { get { return Value; } } public bool HasValue { get { return hasValue; } } #endregion public SByte Value { get { return _value; } } #region Casts public static explicit operator SByte(NullableSByte nullable) { if (!nullable.HasValue) throw new NullReferenceException(); return nullable.Value; } public static implicit operator NullableSByte(SByte value) { return new NullableSByte(value); } public static implicit operator NullableSByte(DBNull value) { return NullableSByte.Default; } #endregion public override string ToString() { if (HasValue) return Value.ToString(); else return string.Empty; } public override bool Equals(object obj) { if (obj is DBNull && !HasValue) return true; else if (obj is NullableSByte) return Equals((NullableSByte)obj); else return false; //if this is reached, it is either some other type, or DBnull is compared with this and we have a Value. } public bool Equals(NullableSByte x) { return Equals(this, x); } public static bool Equals(NullableSByte x, NullableSByte y) { if (x.HasValue != y.HasValue) //one is null return false; else if (x.HasValue) //therefor y also HasValue return x.Value == y.Value; else //both are null return true; } public static bool operator ==(NullableSByte x, NullableSByte y) { return x.Equals(y); } public static bool operator ==(NullableSByte x, object y) { return x.Equals(y); } public static bool operator !=(NullableSByte x, NullableSByte y) { return !x.Equals(y); } public static bool operator !=(NullableSByte x, object y) { return !x.Equals(y); } public static NullableInt32 operator +(NullableSByte x, NullableSByte y) { if (!x.HasValue || !y.HasValue) //one or both are null return NullableInt32.Default; return new NullableInt32(x.Value + y.Value); } public static NullableInt32 operator -(NullableSByte x, NullableSByte y) { if (!x.HasValue || !y.HasValue) //one or both are null return NullableInt32.Default; return new NullableInt32(x.Value - y.Value); } public static NullableInt32 operator *(NullableSByte x, NullableSByte y) { if (!x.HasValue || !y.HasValue) //one or both are null return NullableInt32.Default; return new NullableInt32(x.Value * y.Value); } public static NullableInt32 operator /(NullableSByte x, NullableSByte y) { if (!x.HasValue || !y.HasValue) //one or both are null return NullableInt32.Default; return new NullableInt32(x.Value / y.Value); } public override int GetHashCode() { if (HasValue) return Value.GetHashCode(); else return 0; //GetHashCode() doesn't garantee uniqueness, and neither do we. } #region IFormattable Members string System.IFormattable.ToString(string format, IFormatProvider formatProvider) { if (HasValue) return Value.ToString(format, formatProvider); else return string.Empty; } #endregion #region IComparable Members public int CompareTo(object obj) { if (obj is NullableSByte) //chack and unbox { NullableSByte value = (NullableSByte)obj; if (value.HasValue == this.HasValue) //both null or not null { if (this.HasValue) //this has a value, so they both do return Value.CompareTo(value.Value); else return 0; //both null, so they are equal; } else //one is null { if (HasValue) //he have a value, so we are greater. return 1; else return -1; } } else if (obj is DateTime) { SByte value = (SByte)obj; if (HasValue) //not null, so compare the real values. return Value.CompareTo(value); else return -1; //this is null, so less that the real value; } throw new ArgumentException("NullableSByte can only compare to another NullableSByte or a System.SByte"); } #endregion } } Index: NullableDouble.cs =================================================================== RCS file: /cvsroot/nhibernate/NHibernateContrib/src/Nullables/NullableDouble.cs,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** NullableDouble.cs 7 Dec 2004 21:32:49 -0000 1.2 --- NullableDouble.cs 10 Dec 2004 18:24:22 -0000 1.3 *************** *** 4,8 **** { [System.ComponentModel.TypeConverter(typeof(Nullables.TypeConverters.NullableDoubleConverter)), Serializable()] ! public struct NullableDouble : INullableType, IFormattable { public static readonly NullableDouble Default = new NullableDouble(); --- 4,8 ---- { [System.ComponentModel.TypeConverter(typeof(Nullables.TypeConverters.NullableDoubleConverter)), Serializable()] ! public struct NullableDouble : INullableType, IFormattable, IComparable { public static readonly NullableDouble Default = new NullableDouble(); *************** *** 166,169 **** --- 166,207 ---- #endregion + + #region IComparable Members + + public int CompareTo(object obj) + { + if (obj is NullableDouble) //chack and unbox + { + NullableDouble value = (NullableDouble)obj; + + if (value.HasValue == this.HasValue) //both null or not null + { + if (this.HasValue) //this has a value, so they both do + return Value.CompareTo(value.Value); + else + return 0; //both null, so they are equal; + } + else //one is null + { + if (HasValue) //he have a value, so we are greater. + return 1; + else + return -1; + } + } + else if (obj is Double) + { + Double value = (Double)obj; + + if (HasValue) //not null, so compare the real values. + return Value.CompareTo(value); + else + return -1; //this is null, so less that the real value; + } + + throw new ArgumentException("NullableDouble can only compare to another NullableDouble or a System.Double"); + } + + #endregion } } Index: NullableDateTime.cs =================================================================== RCS file: /cvsroot/nhibernate/NHibernateContrib/src/Nullables/NullableDateTime.cs,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** NullableDateTime.cs 7 Dec 2004 21:32:49 -0000 1.2 --- NullableDateTime.cs 10 Dec 2004 18:24:22 -0000 1.3 *************** *** 4,8 **** { [System.ComponentModel.TypeConverter(typeof(Nullables.TypeConverters.NullableDateTimeConverter)), Serializable()] ! public struct NullableDateTime : INullableType, IFormattable { public static readonly NullableDateTime Default = new NullableDateTime(); --- 4,8 ---- { [System.ComponentModel.TypeConverter(typeof(Nullables.TypeConverters.NullableDateTimeConverter)), Serializable()] ! public struct NullableDateTime : INullableType, IFormattable, IComparable { public static readonly NullableDateTime Default = new NullableDateTime(); *************** *** 136,139 **** --- 136,177 ---- #endregion + + #region IComparable Members + + public int CompareTo(object obj) + { + if (obj is NullableDateTime) //chack and unbox + { + NullableDateTime value = (NullableDateTime)obj; + + if (value.HasValue == this.HasValue) //both null or not null + { + if (this.HasValue) //this has a value, so they both do + return Value.CompareTo(value.Value); + else + return 0; //both null, so they are equal; + } + else //one is null + { + if (HasValue) //he have a value, so we are greater. + return 1; + else + return -1; + } + } + else if (obj is DateTime) + { + DateTime value = (DateTime)obj; + + if (HasValue) //not null, so compare the real values. + return Value.CompareTo(value); + else + return -1; //this is null, so less that the real value; + } + + throw new ArgumentException("NullableDateTime can only compare to another NullableDateTime or a System.DateTime"); + } + + #endregion } } Index: NullableDecimal.cs =================================================================== RCS file: /cvsroot/nhibernate/NHibernateContrib/src/Nullables/NullableDecimal.cs,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** NullableDecimal.cs 7 Dec 2004 21:32:49 -0000 1.2 --- NullableDecimal.cs 10 Dec 2004 18:24:22 -0000 1.3 *************** *** 4,8 **** { [System.ComponentModel.TypeConverter(typeof(Nullables.TypeConverters.NullableDecimalConverter)), Serializable()] ! public struct NullableDecimal : INullableType, IFormattable { public static readonly NullableDecimal Default = new NullableDecimal(); --- 4,8 ---- { [System.ComponentModel.TypeConverter(typeof(Nullables.TypeConverters.NullableDecimalConverter)), Serializable()] ! public struct NullableDecimal : INullableType, IFormattable, IComparable { public static readonly NullableDecimal Default = new NullableDecimal(); *************** *** 166,169 **** --- 166,207 ---- #endregion + + #region IComparable Members + + public int CompareTo(object obj) + { + if (obj is NullableDecimal) //chack and unbox + { + NullableDecimal value = (NullableDecimal)obj; + + if (value.HasValue == this.HasValue) //both null or not null + { + if (this.HasValue) //this has a value, so they both do + return Value.CompareTo(value.Value); + else + return 0; //both null, so they are equal; + } + else //one is null + { + if (HasValue) //he have a value, so we are greater. + return 1; + else + return -1; + } + } + else if (obj is Decimal) + { + Decimal value = (Decimal)obj; + + if (HasValue) //not null, so compare the real values. + return Value.CompareTo(value); + else + return -1; //this is null, so less that the real value; + } + + throw new ArgumentException("NullableDecimal can only compare to another NullableDecimal or a System.Decimal"); + } + + #endregion } } Index: NullableBoolean.cs =================================================================== RCS file: /cvsroot/nhibernate/NHibernateContrib/src/Nullables/NullableBoolean.cs,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** NullableBoolean.cs 12 Nov 2004 22:08:25 -0000 1.1 --- NullableBoolean.cs 10 Dec 2004 18:24:21 -0000 1.2 *************** *** 4,8 **** { [System.ComponentModel.TypeConverter(typeof(Nullables.TypeConverters.NullableBooleanConverter)), Serializable()] ! public struct NullableBoolean : INullableType { public static readonly NullableBoolean Default = new NullableBoolean(); --- 4,8 ---- { [System.ComponentModel.TypeConverter(typeof(Nullables.TypeConverters.NullableBooleanConverter)), Serializable()] ! public struct NullableBoolean : INullableType, IComparable { public static readonly NullableBoolean Default = new NullableBoolean(); *************** *** 124,127 **** --- 124,165 ---- //TODO: Operators for bool (or, and, xor, etc) + + #region IComparable Members + + public int CompareTo(object obj) + { + if (obj is NullableBoolean) //chack and unbox + { + NullableBoolean value = (NullableBoolean)obj; + + if (value.HasValue == this.HasValue) //both null or not null + { + if (this.HasValue) //this has a value, so they both do + return Value.CompareTo(value.Value); + else + return 0; //both null, so they are equal; + } + else //one is null + { + if (HasValue) //he have a value, so we are greater. + return 1; + else + return -1; + } + } + else if (obj is Boolean) + { + Boolean value = (Boolean)obj; + + if (HasValue) //not null, so compare the real values. + return Value.CompareTo(value); + else + return -1; //this is null, so less that the real value; + } + + throw new ArgumentException("NullableBoolean can only compare to another NullableBoolean or a System.Boolean"); + } + + #endregion } } Index: NullableSingle.cs =================================================================== RCS file: /cvsroot/nhibernate/NHibernateContrib/src/Nullables/NullableSingle.cs,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** NullableSingle.cs 7 Dec 2004 21:32:49 -0000 1.2 --- NullableSingle.cs 10 Dec 2004 18:24:22 -0000 1.3 *************** *** 4,8 **** { [System.ComponentModel.TypeConverter(typeof(Nullables.TypeConverters.NullableSingleConverter)), Serializable()] ! public struct NullableSingle : INullableType, IFormattable { public static readonly NullableSingle Default = new NullableSingle(); --- 4,8 ---- { [System.ComponentModel.TypeConverter(typeof(Nullables.TypeConverters.NullableSingleConverter)), Serializable()] ! public struct NullableSingle : INullableType, IFormattable, IComparable { public static readonly NullableSingle Default = new NullableSingle(); *************** *** 166,169 **** --- 166,207 ---- #endregion + + #region IComparable Members + + public int CompareTo(object obj) + { + if (obj is NullableSingle) //chack and unbox + { + NullableSingle value = (NullableSingle)obj; + + if (value.HasValue == this.HasValue) //both null or not null + { + if (this.HasValue) //this has a value, so they both do + return Value.CompareTo(value.Value); + else + return 0; //both null, so they are equal; + } + else //one is null + { + if (HasValue) //he have a value, so we are greater. + return 1; + else + return -1; + } + } + else if (obj is Single) + { + Single value = (Single)obj; + + if (HasValue) //not null, so compare the real values. + return Value.CompareTo(value); + else + return -1; //this is null, so less that the real value; + } + + throw new ArgumentException("NullableSingle can only compare to another NullableSingle or a System.Single"); + } + + #endregion } } Index: NullableGuid.cs =================================================================== RCS file: /cvsroot/nhibernate/NHibernateContrib/src/Nullables/NullableGuid.cs,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** NullableGuid.cs 7 Dec 2004 21:32:49 -0000 1.2 --- NullableGuid.cs 10 Dec 2004 18:24:22 -0000 1.3 *************** *** 5,9 **** { [System.ComponentModel.TypeConverter(typeof(Nullables.TypeConverters.NullableGuidConverter)), Serializable()] ! public struct NullableGuid : INullableType, IFormattable { public static readonly NullableGuid Default = new NullableGuid(); --- 5,9 ---- { [System.ComponentModel.TypeConverter(typeof(Nullables.TypeConverters.NullableGuidConverter)), Serializable()] ! public struct NullableGuid : INullableType, IFormattable, IComparable { public static readonly NullableGuid Default = new NullableGuid(); *************** *** 135,138 **** --- 135,176 ---- #endregion + + #region IComparable Members + + public int CompareTo(object obj) + { + if (obj is NullableGuid) //chack and unbox + { + NullableGuid value = (NullableGuid)obj; + + if (value.HasValue == this.HasValue) //both null or not null + { + if (this.HasValue) //this has a value, so they both do + return Value.CompareTo(value.Value); + else + return 0; //both null, so they are equal; + } + else //one is null + { + if (HasValue) //he have a value, so we are greater. + return 1; + else + return -1; + } + } + else if (obj is Guid) + { + Guid value = (Guid)obj; + + if (HasValue) //not null, so compare the real values. + return Value.CompareTo(value); + else + return -1; //this is null, so less that the real value; + } + + throw new ArgumentException("NullableGuid can only compare to another NullableGuid or a System.Guid"); + } + + #endregion } } Index: NullableInt64.cs =================================================================== RCS file: /cvsroot/nhibernate/NHibernateContrib/src/Nullables/NullableInt64.cs,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** NullableInt64.cs 7 Dec 2004 21:32:49 -0000 1.2 --- NullableInt64.cs 10 Dec 2004 18:24:22 -0000 1.3 *************** *** 4,8 **** { [System.ComponentModel.TypeConverter(typeof(Nullables.TypeConverters.NullableInt64Converter)), Serializable()] ! public struct NullableInt64 : INullableType, IFormattable { public static readonly NullableInt64 Default = new NullableInt64(); --- 4,8 ---- { [System.ComponentModel.TypeConverter(typeof(Nullables.TypeConverters.NullableInt64Converter)), Serializable()] ! public struct NullableInt64 : INullableType, IFormattable, IComparable { public static readonly NullableInt64 Default = new NullableInt64(); *************** *** 166,169 **** --- 166,207 ---- #endregion + + #region IComparable Members + + public int CompareTo(object obj) + { + if (obj is NullableInt64) //chack and unbox + { + NullableInt64 value = (NullableInt64)obj; + + if (value.HasValue == this.HasValue) //both null or not null + { + if (this.HasValue) //this has a value, so they both do + return Value.CompareTo(value.Value); + else + return 0; //both null, so they are equal; + } + else //one is null + { + if (HasValue) //he have a value, so we are greater. + return 1; + else + return -1; + } + } + else if (obj is DateTime) + { + Int64 value = (Int64)obj; + + if (HasValue) //not null, so compare the real values. + return Value.CompareTo(value); + else + return -1; //this is null, so less that the real value; + } + + throw new ArgumentException("NullableInt64 can only compare to another NullableInt64 or a System.Int64"); + } + + #endregion } } |