Age.cs :  » Search-Engines » openDICOM » openDicom » Encoding » Type » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Search Engines » openDICOM 
openDICOM » openDicom » Encoding » Type » Age.cs
/*
   
    openDICOM.NET openDICOM# 0.1.1

    openDICOM# provides a library for DICOM related development on Mono.
    Copyright (C) 2006-2007  Albert Gnandt

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA


    $Id$
*/
using System;
using System.Text.RegularExpressions;
using openDicom;


namespace openDicom.Encoding.Type{
    /// <summary>
    ///     All available age measuring units.
    /// </summary>
    public enum AgeContext
    {
        /// <summary>
        ///     DICOM VR Age String (AS) representation is "xxxD".
        /// </summary>
        Days,
        /// <summary>
        ///     DICOM VR Age String (AS) representation is "xxxW".
        /// </summary>
        Weeks,
        /// <summary>
        ///     DICOM VR Age String (AS) representation is "xxxM".
        /// </summary>
        Months,
        /// <summary>
        ///     DICOM VR Age String (AS) representation is "xxxY".
        /// </summary>
        Years
    }


    /// <summary>
    ///     This class represents a single DICOM Age String (AS) value.
    /// </summary>
    public sealed class Age
    {
        private const char days   = 'D';
        private const char weeks  = 'W';
        private const char months = 'M';
        private const char years  = 'Y';

        private AgeContext context = AgeContext.Days;
        /// <summary>
        ///     Returns the choosen age measuring unit.
        /// </summary>
        public AgeContext Context
        {
            get { return context; }
        }

        /// <summary>
        ///     Access <see cref="Context" /> for measuring unit in days.
        /// </summary>
        /// <remarks>
        ///     Only one measuring unit can be assigned to this instance at
        ///     once. If this property is set on true, <see cref="Context" />
        ///     will be re-assigned to days.
        /// </remarks>
        public bool IsDays
        {
            set { if (value) context = AgeContext.Days; }
            get { return context == AgeContext.Days; }
        }

        /// <summary>
        ///     Access <see cref="Context" /> for measuring unit in weeks.
        /// </summary>
        /// <remarks>
        ///     Only one measuring unit can be assigned to this instance at
        ///     once. If this property is set on true, <see cref="Context" />
        ///     will be re-assigned to weeks.
        /// </remarks>
        public bool IsWeeks
        {
            set { if (value) context = AgeContext.Weeks; }
            get { return context == AgeContext.Weeks; }
        }

        /// <summary>
        ///     Access <see cref="Context" /> for measuring unit in months.
        /// </summary>
        /// <remarks>
        ///     Only one measuring unit can be assigned to this instance at
        ///     once. If this property is set on true, <see cref="Context" />
        ///     will re-assigned to months.
        /// </remarks>
        public bool IsMonths
        {
            set { if (value) context = AgeContext.Months; }
            get { return context == AgeContext.Months; }
        }

        /// <summary>
        ///     Access <see cref="Context" /> for measuring unit in years.
        /// </summary>
        /// <remarks>
        ///     Only one measuring unit can be assigned to this instance at
        ///     once. If this property is set on true, <see cref="Context" />
        ///     will be re-assigned to years.
        /// </remarks>
        public bool IsYears
        {
            set { if (value) context = AgeContext.Years; }
            get { return context == AgeContext.Years; }
        }

        private int ageValue = 0;
        /// <summary>
        ///     Access of age value.
        /// </summary>
        public int AgeValue
        {
            set 
            { 
                if (value >= 0)
                    ageValue = value; 
                else
                    throw new DicomException("Age cannot be negativ.", 
                        "Age.AgeValue", value.ToString());
            }
            get { return ageValue; }
        }


        /// <summary>
        ///     Creates a new age instance from specified age string of
        ///     format "xxxM", where "xxx" is a decimal number, the age value, 
        ///     like "013" and "M", is the age context or measuring unit.
        /// </summary>
        public Age(string ageString)
        {
            if (Regex.IsMatch(ageString, 
                "^[0-9]{3}[" + days + weeks + months + years + "]$"))
            {
                AgeValue = int.Parse(ageString.Substring(0, 3));
                char context = (char) ageString[3];
                switch (context)
                {
                    case days: IsDays = true; break;
                    case weeks: IsWeeks = true; break;
                    case months: IsMonths = true; break;
                    case years: IsYears = true; break;
                    default:
                        throw new DicomException("Age context is invalid.",
                            "ageString", ageString);
                        break;
                }
            }
            else
                throw new DicomException("Age string is invalid.", "ageString",
                    ageString);
        }

        /// <summary>
        ///     Creates a new age instance from specified age value and
        ///     context.
        /// </summary>
        public Age(int ageValue, AgeContext context)
        {
            this.context = context;
            AgeValue = ageValue;
        }

        /// <summary>
        ///     Creates a new age instance from specified age value and
        ///     context as single character. Only DICOM VR Age String (AS)
        ///     measuring units are allowed (see <see cref="AgeContext" />).
        /// </summary>
        public Age(int ageValue, char context)
        {
            switch (context)
            {
                case days: IsDays = true; break;
                case weeks: IsWeeks = true; break;
                case months: IsMonths = true; break;
                case years: IsYears = true; break;
                default:
                    throw new DicomException("Age context is invalid.",
                        "context", context.ToString());
                    break;
            }
            AgeValue = ageValue;
        }

        /// <summary>
        ///     Returns the DICOM VR Age String (AS) representation of this
        ///     instance. Format is "xxxM", where "xxx" is a decimal number
        ///     and "M" is a single character that represents the
        ///     <see cref="AgeContext" />.
        /// </summary>
        public override string ToString()
        {
            char charContext = ' ';
            switch (Context)
            {
                case AgeContext.Days: charContext = days; break;
                case AgeContext.Weeks: charContext = weeks; break;
                case AgeContext.Months: charContext = months; break;
                case AgeContext.Years: charContext = years; break;
            }
            return string.Format("{0:D3}", AgeValue) + charContext;
        }
    }

}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.