Click here to Skip to main content
15,896,606 members
Articles / Programming Languages / C# 4.0

Units of Measure Validator for C#

Rate me:
Please Sign up or sign in to vote.
4.66/5 (28 votes)
2 Jul 2012CPOL7 min read 81.5K   2.4K   41  
This library provides a MSBuild task for compile time validation of units of measurement within C# code.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace HDLibrary.UnitsOfMeasure
{
    /// <summary>
    /// Describes a transformation for quantities. A quantity is a number with an unit.
    /// </summary>
    public abstract class QuantityTransformation : IEquatable<QuantityTransformation>
    {
        public QuantityTransformation(Unit sourceUnit, Unit targetUnit)
        {
            if (sourceUnit == null)
                throw new ArgumentNullException("sourceUnit");

            if (targetUnit == null)
                throw new ArgumentNullException("targetUnit");

            SourceUnit = sourceUnit;
            TargetUnit = targetUnit;
        }

        /// <summary>
        /// The source unit.
        /// </summary>
        public Unit SourceUnit { get; private set; }

        /// <summary>
        /// The target unit.
        /// </summary>
        public Unit TargetUnit { get; private set; }

        /// <summary>
        /// Transforms a value from source unit to target unit.
        /// </summary>
        /// <param name="value">The value to transform in the source unit.</param>
        /// <returns>The transformed value in the target unit.</returns>
        public abstract double Transform(double value);

        /// <summary>
        /// Chains two transformations. This is only possible if this target unit is equal to the others source unit.
        /// </summary>
        /// <param name="other">The other transformation.</param>
        /// <returns>The chained transformation.</returns>
        public abstract QuantityTransformation Chain(QuantityTransformation other);

        /// <summary>
        /// Reverses this transformation, so that reversed(this(value)) = value. The source and target units are exchanged.
        /// </summary>
        /// <returns>The reversed transformation.</returns>
        public abstract QuantityTransformation Reverse();


        public sealed override bool Equals(object obj)
        {
            QuantityTransformation qt = obj as QuantityTransformation;
            if (qt != null)
                return Equals(qt);
            return false;
        }

        public override int GetHashCode()
        {
            return SourceUnit.GetHashCode() ^ TargetUnit.GetHashCode();
        }

        public virtual bool Equals(QuantityTransformation other)
        {
            return TargetUnit.Equals(other.TargetUnit);
        }

        public override string ToString()
        {
            return string.Format("{0:eDIlf} => {1:eDIlf}", SourceUnit, TargetUnit);
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Student
Germany Germany
Presently I am a student of computer science at the Karlsruhe Institute of Technology in Germany.

Comments and Discussions