#region License // Copyright (c) 2010, ClearCanvas Inc. // All rights reserved. // // Redistribution and use in source and binary forms, with or without modification, // are permitted provided that the following conditions are met: // // * Redistributions of source code must retain the above copyright notice, // this list of conditions and the following disclaimer. // * Redistributions in binary form must reproduce the above copyright notice, // this list of conditions and the following disclaimer in the documentation // and/or other materials provided with the distribution. // * Neither the name of ClearCanvas Inc. nor the names of its contributors // may be used to endorse or promote products derived from this software without // specific prior written permission. // // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, // OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE // GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN // ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY // OF SUCH DAMAGE. #endregion #region Backport // This file is a backport from a more recent version of a referenced library. // For a complete file change history, please consult the original file. // // File URL: svn://svn.clearcanvas.ca/source/Xian/Trunk/Dicom/Iod/ContextGroups/ContextGroupItemBase.cs // Revision: 12512 // Backport Date: 2010-07-27 13:58:46 #endregion using System; using ClearCanvas.Common; using ClearCanvas.Dicom.Iod.Macros; namespace Nullstack.ClearCanvasEx.ViewerEx.AnnotationProviders.Backports { partial class ContextGroupBase { /// /// Base class representing a single DICOM context group code item. /// /// /// /// This class is abstract because one typically needs to know the context group under which the code is to be used. /// The context group itself may be extensible, in which case additional user-defined codes may be created and /// used, or it may be non-extensible, in which case only the codes defined in the context group may be used. /// Context group implementors should thus take care to implement appropriate public-scoped constructors /// on the code items if and only if the context group is extensible. /// /// For additional information, please refer to the DICOM Standard 2008 PS 3.16. /// public abstract class ContextGroupItemBase : IEquatable, IEquatable { /// /// Gets the designator of the coding scheme in which this code is defined. /// public readonly string CodingSchemeDesignator; /// /// Gets the version of the coding scheme in which this code is defined. /// /// /// May be null if the version is not explicitly defined for this code. /// public readonly string CodingSchemeVersion; /// /// Gets the value of this code. /// public readonly string CodeValue; /// /// Gets the Human-readable meaning of this code. /// public readonly string CodeMeaning; /// /// Constructs a new where the coding scheme version is not explicitly specified. /// /// The designator of the coding scheme in which this code is defined. /// The value of this code. /// The Human-readable meaning of this code. /// Thrown if or are null or empty. protected ContextGroupItemBase(string codingSchemeDesignator, string codeValue, string codeMeaning) : this(codingSchemeDesignator, null, codeValue, codeMeaning) {} /// /// Constructs a new . /// /// The designator of the coding scheme in which this code is defined. /// The version of the coding scheme in which this code is defined, if known. Should be null if not explicitly specified. /// The value of this code. /// The Human-readable meaning of this code. /// Thrown if or are null or empty. protected ContextGroupItemBase(string codingSchemeDesignator, string codingSchemeVersion, string codeValue, string codeMeaning) { Platform.CheckForEmptyString(codingSchemeDesignator, "codingSchemeDesignator"); Platform.CheckForEmptyString(codeValue, "codeValue"); this.CodingSchemeDesignator = codingSchemeDesignator; this.CodingSchemeVersion = codingSchemeVersion; this.CodeValue = codeValue; this.CodeMeaning = codeMeaning; } /// /// Gets an appropriate hash value. /// /// /// The default implementation computes the hash value based only on the coding scheme designator and the code value. /// public override int GetHashCode() { return 0x27E9AA02 ^ this.CodingSchemeDesignator.GetHashCode() ^ this.CodeValue.GetHashCode(); } /// /// Determines whether or not the specified is equivalent to the current code item. /// /// The object with which to compare the current code item. /// True if the specified object is a or a and they are equivalent; False otherwise. /// Thrown if is null. /// /// The default implementation compares only the coding scheme designator and code value /// by calling or . /// public override bool Equals(object obj) { if (obj is CodeSequenceMacro) return this.Equals((CodeSequenceMacro) obj); else if (obj is ContextGroupItemBase) return this.Equals((ContextGroupItemBase) obj); return false; } /// /// Determines whether or not the specified is equivalent to the current code item. /// /// The code sequence with which to compare the current code item. /// True if the specified code sequence is equivalent to the current code item; False otherwise. /// Thrown if is null. /// /// The default implementation compares only the coding scheme designator and code value /// by calling . /// public virtual bool Equals(CodeSequenceMacro codeSequence) { return this.Equals(codeSequence, false); } /// /// Determines whether or not the specified is equivalent to the current code item. /// /// The code sequence with which to compare the current code item. /// A value indicating whether or not the coding scheme version should be compared. /// True if the specified code sequence is equivalent to the current code item; False otherwise. /// Thrown if is null. /// /// The default implementation compares only the coding scheme designator and code value /// by calling . /// public virtual bool Equals(CodeSequenceMacro codeSequence, bool compareCodingSchemeVersion) { Platform.CheckForNullReference(codeSequence, "codeSequence"); return this.Equals( codeSequence.CodingSchemeDesignator, codeSequence.CodeValue, codeSequence.CodeMeaning, codeSequence.CodingSchemeVersion, compareCodingSchemeVersion); } /// /// Determines whether or not the specified is equivalent to the current code item. /// /// The code item with which to compare the current code item. /// True if the specified code item is equivalent to the current code item; False otherwise. /// Thrown if is null. /// /// The default implementation compares only the coding scheme designator and code value /// by calling . /// public virtual bool Equals(ContextGroupItemBase contextGroupItem) { return this.Equals(contextGroupItem, false); } /// /// Determines whether or not the specified is equivalent to the current code item. /// /// The code item with which to compare the current code item. /// A value indicating whether or not the coding scheme version should be compared. /// True if the specified code item is equivalent to the current code item; False otherwise. /// Thrown if is null. /// /// The default implementation compares only the coding scheme designator and code value /// by calling . /// public virtual bool Equals(ContextGroupItemBase contextGroupItem, bool compareCodingSchemeVersion) { Platform.CheckForNullReference(contextGroupItem, "contextGroupItem"); return this.Equals( contextGroupItem.CodingSchemeDesignator, contextGroupItem.CodeValue, contextGroupItem.CodeMeaning, contextGroupItem.CodingSchemeVersion, compareCodingSchemeVersion); } /// /// Determines whether or not the specified code is equivalent to the current code item. /// /// The coding scheme designator with which to compare with the current code item. /// The coding scheme version with which to compare with the current code item. /// The code value with which to compare with the current code item. /// The code meaning with which to compare with the current code item. /// A value indicating whether or not the coding scheme version should be compared. /// True if the specified code is equivalent to the current code item; False otherwise. /// /// The default implementation compares only the coding scheme designator and code value. /// The coding scheme version is also compared if the flag is specified. /// public virtual bool Equals(string codingSchemeDesignator, string codeValue, string codeMeaning, string codingSchemeVersion, bool compareCodingSchemeVersion) { StringComparer comparer = StringComparer.InvariantCultureIgnoreCase; bool result = comparer.Equals(this.CodeValue, codeValue); result = result && comparer.Equals(this.CodingSchemeDesignator, codingSchemeDesignator); if (compareCodingSchemeVersion) result = result && comparer.Equals(this.CodingSchemeVersion, codingSchemeVersion); return result; } /// /// Sets the attributes of the given code sequence according to the current code item. /// /// A code sequence object. /// Thrown if the code sequence object is null. public virtual void WriteToCodeSequence(CodeSequenceMacro codeSequence) { Platform.CheckForNullReference(codeSequence, "codeSequence"); codeSequence.CodeMeaning = this.CodeMeaning; codeSequence.CodeValue = this.CodeValue; codeSequence.CodingSchemeDesignator = this.CodingSchemeDesignator; if (!string.IsNullOrEmpty(this.CodingSchemeVersion)) codeSequence.CodingSchemeVersion = this.CodingSchemeVersion; } private const string _formatValueAndSchemeDesignator = "{0} ({1})"; private const string _formatValueMeaningAndSchemeDesignator = "{0} {1} ({2})"; /// /// Gets a coded string representation of this code item. /// /// A coded string representation of this code item. public virtual string ToCodeString() { return string.Format(_formatValueAndSchemeDesignator, this.CodeValue, this.CodingSchemeDesignator); } /// /// Gets a plain string representation of this code item. /// /// A coded string representation of this code item. /// /// The default implementation attempts to use the code meaning if available. /// If the code meaning is not specified, it defaults to the coded string repsentation. /// public virtual string ToPlainString() { return string.IsNullOrEmpty(this.CodeMeaning) ? string.Format(_formatValueAndSchemeDesignator, this.CodeValue, this.CodingSchemeDesignator) : this.CodeMeaning; } /// /// Gets a string representation of this code item. /// /// A string representation of this code item. /// /// The default implementation returns a representation consisting of the code value, meaning, and scheme designator. /// public override string ToString() { if (string.IsNullOrEmpty(this.CodeMeaning)) return string.Format(_formatValueAndSchemeDesignator, this.CodeValue, this.CodingSchemeDesignator); return string.Format(_formatValueMeaningAndSchemeDesignator, this.CodeValue, this.CodeMeaning, this.CodingSchemeDesignator); } /// /// Casts a context group item as a code sequence. /// /// A context group item. /// A whose attribute values are those of the specified context group item. public static implicit operator CodeSequenceMacro(ContextGroupItemBase value) { if (value == null) return null; CodeSequenceMacro codeSequence = new CodeSequenceMacro(); value.WriteToCodeSequence(codeSequence); return codeSequence; } /// /// Casts a context group item as a string. /// /// A context group item. /// The string representation of the code item as given by . public static implicit operator string(ContextGroupItemBase value) { if (value == null) return null; return value.ToString(); } } } }