NullableTypes/src/Types NullableTimeSpan.cs,NONE,1.1 DbNullConvert.cs,1.5,1.6 NullConvert.cs,1.6,1.7
Status: Inactive
Brought to you by:
lukadotnet
From: Damien G. <dam...@us...> - 2005-10-06 16:05:43
|
Update of /cvsroot/nullabletypes/NullableTypes/src/Types In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv1576/src/Types Modified Files: DbNullConvert.cs NullConvert.cs NullableString.cs Types.csproj Added Files: NullableTimeSpan.cs Log Message: Implement NullableTimeSpan as per issue 1278920. Index: NullableString.cs =================================================================== RCS file: /cvsroot/nullabletypes/NullableTypes/src/Types/NullableString.cs,v retrieving revision 1.16 retrieving revision 1.17 diff -C2 -d -r1.16 -r1.17 *** NullableString.cs 26 Sep 2005 13:34:17 -0000 1.16 --- NullableString.cs 6 Oct 2005 16:05:35 -0000 1.17 *************** *** 1430,1433 **** --- 1430,1453 ---- } + /// <summary> + /// Converts the <see cref="NullableTimeSpan"/> parameter to a + /// <see cref="NullableString"/> structure. + /// </summary> + /// <param name="x"> + /// A <see cref="NullableTimeSpan"/> to be converted to a + /// <see cref="NullableString"/> structure. + /// </param> + /// <returns> + /// <see cref="Null"/> if <paramref name="x"/> is + /// <see cref="NullableTimeSpan.Null"/> otherwise a new + /// <see cref="NullableString"/> structure that's the string representation + /// of <paramref name="x"/>. + /// </returns> + public static explicit operator NullableString(NullableTimeSpan x) { + if (x.IsNull) + return NullableString.Null; + + return new NullableString(x.Value.ToString()); + } /// <summary> Index: DbNullConvert.cs =================================================================== RCS file: /cvsroot/nullabletypes/NullableTypes/src/Types/DbNullConvert.cs,v retrieving revision 1.5 retrieving revision 1.6 diff -C2 -d -r1.5 -r1.6 *** DbNullConvert.cs 23 Sep 2005 09:47:22 -0000 1.5 --- DbNullConvert.cs 6 Oct 2005 16:05:35 -0000 1.6 *************** *** 15,19 **** // 06-Oct-2003 Luca Upgrade Code upgrade: Replaced tabs with spaces // 28-Jan-2005 DamienG Upgrade Added support for NullableGuid and added regions. ! // 05-Sep-2005 DamienG Upgrade Added // --- 15,19 ---- // 06-Oct-2003 Luca Upgrade Code upgrade: Replaced tabs with spaces // 28-Jan-2005 DamienG Upgrade Added support for NullableGuid and added regions. ! // 05-Sep-2005 DamienG Upgrade Added NullableTimeSpan. // *************** *** 437,440 **** --- 437,482 ---- "Value is not a {0} neither a DBNull Value."), "System.String")); } + + /// <summary> + /// Converts an object value that is DBNull or System.TimeSpan to an + /// equivalent NullableTimeSpan value. + /// </summary> + /// <param name="x"> + /// A value of <see cref="System.TimeSpan"/> type from either a + /// <see cref="System.Data.DataRow"/> Item value, a + /// <see cref="System.Data.IDbCommand.Parameters"/> Item value or a + /// <see cref="System.Data.IDataRecord"/> GetValue returned value.. + /// </param> + /// <returns> + /// <see cref="NullableTimeSpan.Null"/> if <paramref name="x"/> is + /// <see cref="System.DBNull"/> otherwise a + /// <see cref="NullableTimeSpan"/> constructed from <paramref name="x"/>. + /// </returns> + /// <exception cref="System.ArgumentNullException"> + /// <paramref name="x"/> is null. + /// </exception> + /// <exception cref="System.InvalidFormatException"> + /// <paramref name="x"/> is not DBNull, <see cref="System.TimeSpan"/> or <see cref="System.Int6$" />. + /// </exception> + public static NullableTimeSpan ToNullableTimeSpan(object x) { + if (x == sys.DBNull.Value) + return NullableTimeSpan.Null; + + if (x is long) + return new NullableTimeSpan((long)x); + + if (x is sys.TimeSpan) + return new NullableTimeSpan((sys.TimeSpan) x); + + if (x is string) + return NullableTimeSpan.Parse((string) x); + + if (x == null) + throw new sys.ArgumentNullException("x"); + + throw new sys.InvalidCastException( + string.Format(sysGlb.CultureInfo.CurrentCulture, Locale.GetText( + "Value is not a {0} neither a DBNull Value."), "System.TimeSpan, System.String, System.Int64")); + } #endregion // To converters *************** *** 682,685 **** --- 724,749 ---- return x.Value; } + + /// <summary> + /// Converts the specified value to an + /// equivalent <see cref="System.Object"/> value that can be assigned to + /// a <see cref="System.Data.DataRow"/> Item value, a + /// <see cref="System.Data.IDbCommand.Parameters"/> Item value or a + /// <see cref="System.Data.IDataRecord"/> Item value. + /// </summary> + /// <param name="x"> + /// A <see cref="NullableTimeSpan"/> value. + /// </param> + /// <returns> + /// <see cref="System.DBNull"/> if <paramref name="x"/> is + /// <see cref="NullableTimeSpan.Null"/> otherwise + /// the <see cref="NullableTimeSpan.Value"/> of <paramref name="x"/>. + /// </returns> + public static object From(NullableTimeSpan x) { + if (x.IsNull) + return sys.DBNull.Value; + + return x.Value; + } #endregion // From converters } Index: Types.csproj =================================================================== RCS file: /cvsroot/nullabletypes/NullableTypes/src/Types/Types.csproj,v retrieving revision 1.18 retrieving revision 1.19 diff -C2 -d -r1.18 -r1.19 *** Types.csproj 12 Feb 2005 03:44:34 -0000 1.18 --- Types.csproj 6 Oct 2005 16:05:35 -0000 1.19 *************** *** 72,78 **** <Reference Name = "System.XML" ! AssemblyName = "System.XML" HintPath = "H:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\System.XML.dll" /> </References> </Build> --- 72,83 ---- <Reference Name = "System.XML" ! AssemblyName = "System.Xml" HintPath = "H:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\System.XML.dll" /> + <Reference + Name = "System.Data" + AssemblyName = "System.Data" + HintPath = "..\..\..\..\..\WINDOWS\Microsoft.NET\Framework\v1.1.4322\System.Data.dll" + /> </References> </Build> *************** *** 150,153 **** --- 155,163 ---- /> <File + RelPath = "NullableTimeSpan.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File RelPath = "NullableTypeException.cs" SubType = "Code" Index: NullConvert.cs =================================================================== RCS file: /cvsroot/nullabletypes/NullableTypes/src/Types/NullConvert.cs,v retrieving revision 1.6 retrieving revision 1.7 diff -C2 -d -r1.6 -r1.7 *** NullConvert.cs 12 Feb 2005 03:44:34 -0000 1.6 --- NullConvert.cs 6 Oct 2005 16:05:35 -0000 1.7 *************** *** 12,15 **** --- 12,16 ---- // 06-Oct-2003 Luca Upgrade Code upgrade: Replaced tabs with spaces // 28-Jan-2005 DamienG Upgrade Added helpers for NullableGuid and added regions. + // 26-Sep-2005 DamienG Upgrade Added helpers for NullableTimeSpan. // *************** *** 672,676 **** return x.Value; } ! #endregion // From converters #region To converters --- 673,712 ---- return x.Value; } ! ! /// <summary> ! /// Converts the specified <see cref="NullableTimeSpan"/> value to an equivalent ! /// <see cref="System.String"/> value using a conventional <see cref="System.String"/> value to ! /// represent the <see cref="NullableTimeSpan.Null"/> value. ! /// </summary> ! /// <param name="x"> ! /// The <see cref="NullableTimeSpan"/> value to convert. ! /// </param> ! /// <param name="conventionalNullValue"> ! /// The <see cref="System.String"/> value that conventionally represent the ! /// <see cref="NullableTimeSpan.Null"/> value. ! /// </param> ! /// <param name="format"> ! /// A string that specifies the return format of <paramref name="x"/> if it is not ! /// <see cref="NullableTimeSpan.Null"/>. ! /// </param> ! /// <returns> ! /// <paramref name="conventionalNullValue"/> if <paramref name="x"/> is ! /// <see cref="NullableTimeSpan.Null"/> otherwise the equivalent string of the ! /// <see cref="NullableTimeSpan.Value"/> of <paramref name="x"/> formatted as specified by ! /// <paramref name="format"/>. ! /// </returns> ! /// <exception cref="System.FormatException"> ! /// The length of <paramref name="format"/> is 1, and it is not one of the format specifier characters ! /// defined for DateTimeFormatInfo. ! /// <para> -or- </para> ! /// <paramref name="format"/> does not contain a valid custom format pattern. ! /// </exception> ! public static sys.TimeSpan From(NullableTimeSpan x, sys.TimeSpan conventionalNullValue) { ! if (x.IsNull) ! return conventionalNullValue; ! ! return x.Value; ! } ! #endregion // From converters #region To converters *************** *** 1242,1246 **** return new NullableDateTime(x); - } --- 1278,1281 ---- *************** *** 1316,1320 **** return new NullableString(x); } ! #endregion // To converters } } --- 1351,1362 ---- return new NullableString(x); } ! ! public static NullableTimeSpan ToNullableTimeSpan(sys.TimeSpan x, sys.TimeSpan conventionalNullValue) { ! if (x == conventionalNullValue) ! return NullableTimeSpan.Null; ! ! return new NullableTimeSpan(x); ! } ! #endregion // To converters } } --- NEW FILE: NullableTimeSpan.cs --- // // NullableTypes.NullableTimeSpan // // Authors: Damien Guard (dam...@us...) // // Date Author Changes Reasons // 07-Apr-2003 DamienG Created New class // namespace NullableTypes { using sys = System; using sysGlb = System.Globalization; using sysXml = System.Xml; using sysXmlSrl = System.Xml.Serialization; using sysXmlScm = System.Xml.Schema; [sys.Serializable] public struct NullableTimeSpan : INullable, sys.IComparable, sysXmlSrl.IXmlSerializable { #region Fields sys.TimeSpan _value; private bool _notNull; public static readonly NullableTimeSpan Null; /// <summary> /// A constant representing the largest possible value of a /// <see cref="NullableTimeSpan"/>. /// </summary> public static readonly NullableTimeSpan MaxValue = new NullableTimeSpan(sys.TimeSpan.MaxValue); /// <summary> /// A constant representing the smallest possible value of a /// <see cref="NullableTimeSpan"/>. /// </summary> public static readonly NullableTimeSpan MinValue = new NullableTimeSpan(sys.TimeSpan.MinValue); /// <summary> /// Represents a zero value that can be assigned to an instance of the /// <see cref="NullableTimeSpan"/> structure. /// </summary> /// <remarks> /// Zero field is a constant of the <see cref="NullableTimeSpan"/> structure. /// </remarks> public static readonly NullableTimeSpan Zero = new NullableTimeSpan(0); #endregion // Fields #region Constructors public NullableTimeSpan (sys.TimeSpan timeSpan) { _value = timeSpan; _notNull = true; } public NullableTimeSpan (long ticks) { _value = new sys.TimeSpan (ticks); _notNull = true; } public NullableTimeSpan(int hours, int minutes, int seconds) { _value = new sys.TimeSpan(hours, minutes, seconds); _notNull = true; } public NullableTimeSpan(int days, int hours, int minutes, int seconds) : this(days, hours, minutes, seconds, 0) { } public NullableTimeSpan(int days, int hours, int minutes, int seconds, int milliseconds) { _value = new sys.TimeSpan(days, hours, minutes, seconds, milliseconds); _notNull = true; } #endregion // Constructors #region INullable public bool IsNull { get { return !_notNull; } } #endregion // INullable #region IComparable - Ordering public int CompareTo(object value) { if (value == null) return 1; if (!(value is NullableTimeSpan)) throw new sys.ArgumentException(string.Format(sysGlb.CultureInfo.CurrentCulture, Locale.GetText("Value is not a {0}."), "NullableTypes.NullableTimeSpan")); NullableTimeSpan iValue = (NullableTimeSpan)value; if (iValue.IsNull && this.IsNull) return 0; if (iValue.IsNull) return 1; if (this.IsNull) return -1; return _value.CompareTo(iValue.Value); } #endregion // IComparable - Ordering #region Equivalence public override bool Equals(object value) { if (!(value is NullableTimeSpan)) return false; else if (this.IsNull && ((NullableTimeSpan)value).IsNull) return true; else if (this.IsNull) return false; else if (((NullableTimeSpan)value).IsNull) return false; else return (bool) (this == (NullableTimeSpan)value); } public static NullableBoolean NotEquals(NullableTimeSpan x, NullableTimeSpan y) { return (x != y); } public static NullableBoolean operator == (NullableTimeSpan x, NullableTimeSpan y) { if (x.IsNull || y.IsNull) return NullableBoolean.Null; return new NullableBoolean (x.Value == y.Value); } public static NullableBoolean operator != (NullableTimeSpan x, NullableTimeSpan y) { if (x.IsNull || y.IsNull) return NullableBoolean.Null; return new NullableBoolean (!(x.Value == y.Value)); } public static NullableBoolean Equals(NullableTimeSpan x, NullableTimeSpan y) { return (x == y); } public override int GetHashCode() { return _value.GetHashCode(); } #endregion // Equivalence #region IXmlSerializable /// <summary> /// This member supports the .NET Framework infrastructure and is not intended to be used directly /// from your code. /// </summary> [sys.ComponentModel.EditorBrowsable(sys.ComponentModel.EditorBrowsableState.Never)] sysXml.Schema.XmlSchema sysXmlSrl.IXmlSerializable.GetSchema() { // <?xml version="1.0"?> // <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" // targetNamespace="http://NullableTypes.SourceForge.Net/NullableTimeSpanXMLSchema" // xmlns="http://NullableTypes.SourceForge.Net/NullableTimeSpanXMLSchema" // elementFormDefault="qualified"> // <xs:element name="NullableTimeSpan" type="xs:duration" nillable="true" /> // </xs:schema> // Element: NullableTimeSpan sysXmlScm.XmlSchemaElement rootElement = new sysXmlScm.XmlSchemaElement(); rootElement.Name = "NullableTimeSpan"; rootElement.SchemaTypeName = new sysXml.XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema"); rootElement.IsNillable = true; // Xml Schema sysXmlScm.XmlSchema xsd = new sysXmlScm.XmlSchema(); xsd.Id = "NullableTimeSpan"; xsd.Namespaces.Add("xs", "http://www.w3.org/2001/XMLSchema"); xsd.TargetNamespace = "http://NullableTypes.SourceForge.Net/NullableTimeSpanXMLSchema"; xsd.Items.Add(rootElement); xsd.ElementFormDefault = sysXmlScm.XmlSchemaForm.Qualified; return xsd; } /// <summary> /// This member supports the .NET Framework infrastructure and is not intended to be used directly /// from your code. /// </summary> [sys.ComponentModel.EditorBrowsable(sys.ComponentModel.EditorBrowsableState.Never)] void sysXmlSrl.IXmlSerializable.WriteXml(sysXml.XmlWriter writer) { if (!IsNull) writer.WriteString(sysXml.XmlConvert.ToString(_value)); else writer.WriteAttributeString("xsi", "nil", "http://www.w3.org/2001/XMLSchema-instance", "true"); } /// <summary> /// This member supports the .NET Framework infrastructure and is not intended to be used directly /// from your code. /// </summary> [sys.ComponentModel.EditorBrowsable(sys.ComponentModel.EditorBrowsableState.Never)] void sysXmlSrl.IXmlSerializable.ReadXml(sysXml.XmlReader reader) { string nilValue = reader["nil", "http://www.w3.org/2001/XMLSchema-instance"]; string elementValue = reader.IsEmptyElement ? null : reader.ReadElementString(); sys.Console.WriteLine(elementValue); if (nilValue == null || nilValue == "false" || nilValue == "0") { _value = sysXml.XmlConvert.ToTimeSpan(elementValue); _notNull = true; } else _notNull = false; } #endregion // IXmlSerializable #region Properties public sys.TimeSpan Value { get { if (this.IsNull) throw new NullableNullValueException(); return _value; } } #endregion // Properties #region Methods public static NullableBoolean GreaterThan(NullableTimeSpan x, NullableTimeSpan y) { return (x > y); } public static NullableBoolean GreaterThanOrEqual(NullableTimeSpan x, NullableTimeSpan y) { return (x >= y); } public static NullableBoolean LessThan(NullableTimeSpan x, NullableTimeSpan y) { return (x < y); } public static NullableBoolean LessThanOrEqual(NullableTimeSpan x, NullableTimeSpan y) { return (x <= y); } public static NullableTimeSpan Parse(string s) { return sys.TimeSpan.Parse (s); } public static NullableTimeSpan Add(NullableTimeSpan x, NullableTimeSpan y) { return (x + y); } #endregion // Methods #region Operators public static NullableTimeSpan operator + (NullableTimeSpan x, NullableTimeSpan y) { if (x.IsNull || y.IsNull) return NullableTimeSpan.Null; checked { return new NullableTimeSpan(x.Value + y.Value); } } public static NullableTimeSpan operator - (NullableTimeSpan x, NullableTimeSpan y) { if (x.IsNull || y.IsNull) return NullableTimeSpan.Null; checked { return new NullableTimeSpan(x.Value - y.Value); } } public static NullableBoolean operator > (NullableTimeSpan x, NullableTimeSpan y) { if (x.IsNull || y.IsNull) return NullableBoolean.Null; if (x.Value.CompareTo (y.Value) > 0) return new NullableBoolean (true); else return new NullableBoolean (false); } public static NullableBoolean operator >= (NullableTimeSpan x, NullableTimeSpan y) { if (x.IsNull || y.IsNull) return NullableBoolean.Null; if (x.Value.CompareTo (y.Value) >= 0) return new NullableBoolean (true); else return new NullableBoolean (false); } public static NullableBoolean operator < (NullableTimeSpan x, NullableTimeSpan y) { if (x.IsNull || y.IsNull) return NullableBoolean.Null; if (x.Value.CompareTo (y.Value) < 0) return new NullableBoolean (true); else return new NullableBoolean (false); } public static NullableBoolean operator <= (NullableTimeSpan x, NullableTimeSpan y) { if (x.IsNull || y.IsNull) return NullableBoolean.Null; if (x.Value.CompareTo (y.Value) <= 0) return new NullableBoolean (true); else return new NullableBoolean (false); } #endregion // Operators #region Conversion Operators public static explicit operator sys.TimeSpan(NullableTimeSpan x) { return x.Value; } public static explicit operator NullableTimeSpan(NullableString x) { if (x.IsNull) return NullableTimeSpan.Null; else return NullableTimeSpan.Parse (x.Value); } public static implicit operator NullableTimeSpan(sys.TimeSpan x) { return new NullableTimeSpan (x); } #endregion // Conversion Operators #region Conversions /// <summary> /// Converts to a NullableString representing the time span. /// </summary> /// <returns>A NullableString representative of the NullableTimeSpan.</returns> public NullableString ToNullableString () { return (NullableString) this; } /// <summary> /// Converts the NullableTimeSpan value to a string. /// </summary> /// <returns>A string representative of the NullableTimeSpan.</returns> public override string ToString () { if (this.IsNull) return Locale.GetText("Null"); else return _value.ToString (); } #endregion // Conversions } } |