[Mantisconnect-cvs] SF.net SVN: mantisconnect: [144] mantisconnect/trunk/clients/dotnet
Brought to you by:
vboctor
From: <vb...@us...> - 2007-10-20 22:06:23
|
Revision: 144 http://mantisconnect.svn.sourceforge.net/mantisconnect/?rev=144&view=rev Author: vboctor Date: 2007-10-20 15:06:19 -0700 (Sat, 20 Oct 2007) Log Message: ----------- Fixed #350: Replace MantisEnum.Labels with MantisEnum.GetLabels() + several other enhancements Modified Paths: -------------- mantisconnect/trunk/clients/dotnet/MantisSubmit/SubmitIssue.cs mantisconnect/trunk/clients/dotnet/UnitTests/RetrieveEnums.cs mantisconnect/trunk/clients/dotnet/mantisconnect/Enumeration.cs Modified: mantisconnect/trunk/clients/dotnet/MantisSubmit/SubmitIssue.cs =================================================================== --- mantisconnect/trunk/clients/dotnet/MantisSubmit/SubmitIssue.cs 2007-10-20 21:09:23 UTC (rev 143) +++ mantisconnect/trunk/clients/dotnet/MantisSubmit/SubmitIssue.cs 2007-10-20 22:06:19 UTC (rev 144) @@ -425,9 +425,9 @@ projectComboBox.SelectedIndex = 0; PopulateProjectDependentFields(); - priorityComboBox.DataSource = session.Config.PriorityEnum.Labels; - severityComboBox.DataSource = session.Config.SeverityEnum.Labels; - reproducibilityComboBox.DataSource = session.Config.ReproducibilityEnum.Labels; + priorityComboBox.DataSource = session.Config.PriorityEnum.GetLabels(); + severityComboBox.DataSource = session.Config.SeverityEnum.GetLabels(); + reproducibilityComboBox.DataSource = session.Config.ReproducibilityEnum.GetLabels(); } catch( Exception ex ) { Modified: mantisconnect/trunk/clients/dotnet/UnitTests/RetrieveEnums.cs =================================================================== --- mantisconnect/trunk/clients/dotnet/UnitTests/RetrieveEnums.cs 2007-10-20 21:09:23 UTC (rev 143) +++ mantisconnect/trunk/clients/dotnet/UnitTests/RetrieveEnums.cs 2007-10-20 22:06:19 UTC (rev 144) @@ -11,6 +11,7 @@ #endregion using System; +using System.Collections.Generic; using System.Net; using System.Web.Services.Protocols; @@ -39,7 +40,7 @@ public void StatusEnum() { MantisEnum statusEnum = Session.Config.StatusEnum; - Assert.IsTrue( statusEnum.Labels.Length > 0 ); + Assert.IsTrue( statusEnum.Count > 0 ); WriteEnum( "Status", statusEnum ); } @@ -47,7 +48,7 @@ public void PriorityEnum() { MantisEnum priorityEnum = Session.Config.PriorityEnum; - Assert.IsTrue( priorityEnum.Labels.Length > 0 ); + Assert.IsTrue( priorityEnum.Count > 0 ); WriteEnum( "Priority", priorityEnum ); } @@ -55,7 +56,7 @@ public void SeverityEnum() { MantisEnum severityEnum = Session.Config.SeverityEnum; - Assert.IsTrue( severityEnum.Labels.Length > 0 ); + Assert.IsTrue( severityEnum.Count > 0 ); WriteEnum( "Severity", severityEnum ); } @@ -63,7 +64,7 @@ public void ProjectionEnum() { MantisEnum projectionEnum = Session.Config.ProjectionEnum; - Assert.IsTrue( projectionEnum.Labels.Length > 0 ); + Assert.IsTrue( projectionEnum.Count > 0 ); WriteEnum( "Projection", projectionEnum ); } @@ -71,7 +72,7 @@ public void EtaEnum() { MantisEnum etaEnum = Session.Config.EtaEnum; - Assert.IsTrue( etaEnum.Labels.Length > 0 ); + Assert.IsTrue( etaEnum.Count > 0 ); WriteEnum( "Eta", etaEnum ); } @@ -79,7 +80,7 @@ public void ReproducibilityEnum() { MantisEnum reproducibilityEnum = Session.Config.ReproducibilityEnum; - Assert.IsTrue( reproducibilityEnum.Labels.Length > 0 ); + Assert.IsTrue( reproducibilityEnum.Count > 0 ); WriteEnum( "Reproducibility", reproducibilityEnum ); } @@ -87,7 +88,7 @@ public void AccessLevelEnum() { MantisEnum accessLevelEnum = Session.Config.AccessLevelEnum; - Assert.IsTrue( accessLevelEnum.Labels.Length > 0 ); + Assert.IsTrue( accessLevelEnum.Count > 0 ); WriteEnum( "AccessLevel", accessLevelEnum ); } @@ -95,7 +96,7 @@ public void ResolutionEnum() { MantisEnum resolutionEnum = Session.Config.ResolutionEnum; - Assert.IsTrue( resolutionEnum.Labels.Length > 0 ); + Assert.IsTrue( resolutionEnum.Count > 0 ); WriteEnum( "Resolution", resolutionEnum ); } @@ -103,7 +104,7 @@ public void ViewStateEnum() { MantisEnum viewStateEnum = Session.Config.ViewStateEnum; - Assert.IsTrue( viewStateEnum.Labels.Length > 0 ); + Assert.IsTrue( viewStateEnum.Count > 0 ); WriteEnum( "ViewState", viewStateEnum ); } @@ -112,10 +113,12 @@ Console.WriteLine(); Console.WriteLine( enumType ); - string[] labels = mantisEnum.Labels; - - foreach( string label in labels ) - Console.WriteLine( label ); + ICollection<string> labels = mantisEnum.GetLabels(); + + foreach (string label in labels) + { + Console.WriteLine(label); + } } } } Modified: mantisconnect/trunk/clients/dotnet/mantisconnect/Enumeration.cs =================================================================== --- mantisconnect/trunk/clients/dotnet/mantisconnect/Enumeration.cs 2007-10-20 21:09:23 UTC (rev 143) +++ mantisconnect/trunk/clients/dotnet/mantisconnect/Enumeration.cs 2007-10-20 22:06:19 UTC (rev 144) @@ -1,4 +1,4 @@ -#region Copyright \xA9 2004 Victor Boctor +#region Copyright \xA9 2004-2007 Victor Boctor // // MantisConnect is copyrighted to Victor Boctor // @@ -11,7 +11,8 @@ #endregion using System; -using System.Collections; +using System.Collections.Generic; +using System.Globalization; namespace Futureware.MantisConnect { @@ -25,33 +26,90 @@ public sealed class MantisEnum { /// <summary> + /// The enumeration string supplied at construction time. + /// </summary> + private readonly string enumeration; + + /// <summary> + /// A dictionary that maps labels to their corresponding code. + /// </summary> + private Dictionary<string, int> labelToCodeMap; + + /// <summary> + /// A dictionary that maps codes to their corresponding labels. + /// </summary> + private Dictionary<int, string> codeToLabelMap; + + /// <summary> /// Constructor /// </summary> /// <param name="enumeration">Enumeration string to work with.</param> - public MantisEnum( string enumeration ) + public MantisEnum(string enumeration) { + if (String.IsNullOrEmpty(enumeration)) + { + throw new ArgumentNullException(enumeration); + } + this.enumeration = enumeration; + + string[] entries = enumeration.Split(','); + + this.labelToCodeMap = new Dictionary<string, int>(entries.Length); + this.codeToLabelMap = new Dictionary<int, string>(entries.Length); + + int code; + string label; + + foreach (string entry in entries) + { + string[] details = entry.Split(':'); + if (details.Length != 2) + { + throw new FormatException(String.Format(CultureInfo.InvariantCulture, "Invalid enumeration '{0}'.", enumeration)); + } + + if (!Int32.TryParse(details[0].Trim(), out code)) + { + throw new FormatException(String.Format(CultureInfo.InvariantCulture, "Invalid code '{0}' in enumeration '{1}'.", details[0], enumeration)); + } + + label = details[1].Trim(); + if (label.Length == 0) + { + throw new FormatException(String.Format(CultureInfo.InvariantCulture, "Code '{0}' has an empty label in enumeration '{1}'", code, enumeration)); + } + + if (this.labelToCodeMap.ContainsKey(label)) + { + throw new FormatException(String.Format(CultureInfo.InvariantCulture, "Label '{0}' exists more than once in enumeration '{1}'", label, enumeration)); + } + + if (this.codeToLabelMap.ContainsKey(code)) + { + throw new FormatException(String.Format(CultureInfo.InvariantCulture, "Code '{0}' exists more than once in enumeration '{1}'", code, enumeration)); + } + + this.labelToCodeMap[label] = code; + this.codeToLabelMap[code] = label; + } } /// <summary> /// Given an id, this indexer returns the corresponding enumeration name. /// </summary> /// <param name="id">The enumeration value id.</param> - public string this [int id] + /// <returns>The enumeration label.</returns> + public string this[int id] { get { - string[] entries = enumeration.Split( ',' ); - - foreach ( string entry in entries ) + if (this.codeToLabelMap.ContainsKey(id)) { - string[] details = entry.Split( ':' ); - - if ( details[0] == id.ToString() ) - return details[1]; + return this.codeToLabelMap[id]; } - return string.Format( "@{0}@", id ); + return string.Format(CultureInfo.InvariantCulture, "@{0}@", id); } } @@ -59,18 +117,14 @@ /// Given a name, this indexer returns the corresponding enumeration id. /// </summary> /// <param name="name">The enumeration value name.</param> + /// <returns>The enumeration code.</returns> public int this [string name] { get { - string[] entries = enumeration.Split( ',' ); - - foreach ( string entry in entries ) + if (this.labelToCodeMap.ContainsKey(name)) { - string[] details = entry.Split( ':' ); - - if ( details[1] == name ) - return Convert.ToInt32( details[0] ); + return this.labelToCodeMap[name]; } return 0; @@ -78,32 +132,49 @@ } /// <summary> - /// Returns an array of strings containing the labels in the enumerations. + /// The number of values in the enumeration. /// </summary> - /// <value>Array of enumeration labels.</value> - public string[] Labels + public int Count { - get - { - string[] entries = enumeration.Split( ',' ); + get { return this.labelToCodeMap.Count; } + } - string[] labels = new string[ entries.Length ]; + /// <summary> + /// Returns a collection containing the labels in the enumeration. + /// </summary> + /// <returns>A collection of the labels.</returns> + public ICollection<string> GetLabels() + { + Dictionary<string, int>.KeyCollection keys = this.labelToCodeMap.Keys; - int i = 0; - foreach ( string entry in entries ) - { - string[] details = entry.Split( ':' ); - labels[i] = details[1]; - ++i; - } - - return labels; + string[] labels = new string[keys.Count]; + + int i = 0; + foreach ( string label in keys ) + { + labels[i++] = label; } + + return labels; } /// <summary> - /// The enumeration string supplied at construction time. + /// Returns a collection containing the codes in the enumeration. /// </summary> - private readonly string enumeration; + /// <returns>A collection of the codes.</returns> + public ICollection<int> GetCodes() + { + Dictionary<int, string>.KeyCollection keys = this.codeToLabelMap.Keys; + + int[] codes = new int[keys.Count]; + + int i = 0; + foreach (int code in keys) + { + codes[i++] = code; + } + + return codes; + } } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |