Click here to Skip to main content
16,004,458 members
Articles / Web Development / ASP.NET

Secrets for Setting Up Continuous Integration

Rate me:
Please Sign up or sign in to vote.
2.88/5 (7 votes)
23 Feb 2009CPOL5 min read 67.3K   54   41  
A few simple tips that should help when you are considering setting up CI
using System;
using System.Diagnostics;
using System.Globalization;
using System.Text.RegularExpressions;

namespace Agile.Common
{
    /// <summary>
    /// A collection of frequently used functions.
    /// All functions are static.
    /// </summary>
    public class CommonFunctions
    {
        #region Delegates

        /// <summary>
        /// Delegate for simple functions that take no parameters
        /// and have no return value.
        /// </summary>
        public delegate void SimpleFunction();

        /// <summary>
        /// Delegate for a method that takes a string parameter and returns a string
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public delegate string StringDelegate(string value);

        #endregion

        private static readonly Regex _alphaNumericPattern = new Regex("[^a-zA-Z0-9]");
        private static readonly Regex _alphaPattern = new Regex("[^a-zA-Z]");

        private static readonly Regex _numericPattern = new Regex(@"^\d+$");

        /// <summary>
        /// Check that the give string contains all numeric characters.
        /// </summary>
        /// <param name="check">check this string for all numeric characers.</param>
        /// <returns>True if the string contains ALL numeric characters.</returns>
        /// <remarks>Negative numbers will also return true.</remarks>
        public static bool AreAllCharactersNumeric(string check)
        {
            ArgumentValidation.CheckForNullReference(check, "check");
            return _numericPattern.IsMatch(check);
        }

        /// <summary>
        /// Check that the give string contains all numeric characters.
        /// </summary>
        /// <param name="check">check this object and see if it is a number.</param>
        /// <returns>True if the string contains ALL numeric characters.</returns>
        /// <remarks>Negative numbers will also return true.
        /// [Converts to a string and then calls the other override.]</remarks>
        public static bool AreAllCharactersNumeric(object check)
        {
            return AreAllCharactersNumeric(check.ToString());
        }

        /// <summary>
        /// Returns true if the given object is numeric (or its ToString mehtod is numeric).
        /// </summary>
        /// <param name="check"></param>
        /// <returns></returns>
        public static bool IsNumeric(object check)
        {
            string asString = RemoveNegativeSign(check.ToString());
            asString = RemoveDecimalPoint(asString);
            return AreAllCharactersNumeric(asString);
        }

        /// <summary>
        /// Removes the negative sign from a number if it has one, otherwise just returns the number as is.
        /// ie. Changes -932 to 932
        /// </summary>
        /// <param name="number">number to remove the negative sign from.</param>
        /// <returns></returns>
        private static string RemoveNegativeSign(string number)
        {
            ArgumentValidation.CheckForNullReference(number, "number");

            if (number.StartsWith("-"))
                return number.Remove(0, 1);
            return number;
        }

        /// <summary>
        /// Removes the decimal point from a number if it has one, otherwise just returns the number as is.
        /// ie. Changes 5.5 to 55
        /// </summary>
        /// <param name="number">number to remove the decimal point from.</param>
        /// <returns></returns>
        private static string RemoveDecimalPoint(string number)
        {
            ArgumentValidation.CheckForNullReference(number, "number");

            if (Strings.Contains(number, "."))
                return Strings.RemoveFirstInstanceOf(".", number);
            return number;
        }

        /// <summary>
        /// Checks if the given string contains all Alpha characters.
        /// i.e. A-Z
        /// </summary>
        /// <param name="check">Check this string for all alpha characters.</param>
        /// <returns>true if all characters are alpha characters.</returns>
        public static bool IsAlpha(string check)
        {
            ArgumentValidation.CheckForNullReference(check, "check");
            return !_alphaPattern.IsMatch(check);
        }

        /// <summary>
        /// Checks if the given string containss all Alpha Numeric characters.
        /// Warning: Negative numbers will return false (ie "-" is NOT alpha or numeric)
        /// </summary>
        /// <param name="check">Check this string for all alpha numeric characters.</param>
        /// <returns>true if all characters are alpha numeric.</returns>
        /// <remarks>Numeric is not camel cased because of fx cop rule 'CompoundWordsShouldBeCasedCorrectly'</remarks>
        public static bool IsAlphanumeric(string check)
        {
            ArgumentValidation.CheckForNullReference(check, "check");
            return !_alphaNumericPattern.IsMatch(check);
        }

        /// <summary>
        /// Check if theValue string is a currency
        /// </summary>
        /// <param name="theValue">The string contains currency value</param>
        /// <returns></returns>
        public static bool IsCurrency(string theValue)
        {
            ArgumentValidation.CheckForNullReference(theValue, "theValue");
            double result = 0;
            return Double.TryParse(theValue,
                                   NumberStyles.Currency,
                                   null, out result);
        }

        /// <summary>
        /// Gets a string representation of a currency value - done to
        /// ensure consistency across all apps
        /// </summary>
        /// <param name="value">the value</param>
        /// <returns>the value as a string</returns>
        public static string FormatCurrency(double value)
        {
            return FormatCurrency((decimal) value);
        }


        /// <summary>
        /// Gets a string representation of a currency value - done to
        /// ensure consistency across all apps
        /// </summary>
        /// <param name="value">the value</param>
        /// <returns>the value as a string</returns>
        /// <remarks>If the value is over $10000, and does not include
        /// a cents portion, then the resulting string does not 
        /// include cents. i.e: 50000.00 will return $50000 not $50000.00</remarks>
        public static string FormatCurrency(decimal value)
        {
            if (Math.Abs(value) < 10000)
                return value.ToString("$#0.00", CultureInfo.CurrentCulture);
            else if (Math.Floor((double) value) != (double) value)
                return value.ToString("$#0.00", CultureInfo.CurrentCulture);
            else
                return value.ToString("$#0", CultureInfo.CurrentCulture);
        }

        /// <summary>
        /// Gets a string representation of a currency value
        /// including the cents portion.
        /// </summary>
        /// <param name="value">the value</param>
        /// <returns>the value as a string</returns>
        public static string FormatLongCurrency(decimal value)
        {
            return value.ToString("c", CultureInfo.CurrentCulture);
        }

        /// <summary>
        /// get the leftmost n characters of a string, or less if the string
        /// is less
        /// </summary>
        /// <param name="value">the full string</param>
        /// <param name="length">the number of characters we want</param>
        /// <returns>the leftmost n characters </returns>
        public static string Left(string value, int length)
        {
            ArgumentValidation.CheckForNullReference(value, "value");
            if (value.Length < length)
                return value;
            else
                return value.Substring(0, length);
        }

        /// <summary>
        /// gets the rightmost n chracters of a string
        /// </summary>
        /// <param name="value">the full string</param>
        /// <param name="length">the number of characters we want</param>
        /// <returns>the rightmost n characters</returns>
        public static string Right(string value, int length)
        {
            ArgumentValidation.CheckForNullReference(value, "value");
            if (value.Length <= length)
                return value;
            else
                return value.Substring(value.Length - length, length);
        }

        /// <summary>
        /// This trims a value to the bounds given
        /// </summary>
        /// <param name="value">the raw value</param>
        /// <param name="minimum">the minimum allowed value</param>
        /// <param name="maximum">the maximum allowed value</param>
        /// <returns>the value, trimmed to the min, max</returns>
        public static int Bound(int value, int minimum, int maximum)
        {
            return (value < minimum) ? minimum :
                (value > maximum) ? maximum :
                    value;
        }

        /// <summary>
        /// Returns the given value as an integer if it is 'safe'.
        /// i.e. If the value is null or non-numeric this method will return 0.
        /// </summary>
        public static T? SafeNullable<T>(object value) where T : struct
        {
            if (value == null || value is DBNull)
                return new T?();
            return (T) value;
        }

        /// <summary>
        /// Returns the given value as an integer if it is 'safe'.
        /// i.e. If the value is null or non-numeric this method will return 0.
        /// </summary>
        /// <param name="value">The value to return if it is a safe int.</param>
        /// <returns>0 if the value is not safe, otherwise the value as an int.</returns>
        public static int SafeInt(object value)
        {
            return SafeInt(value, 0);
        }

        /// <summary>
        /// Returns the given value as an integer if it is 'safe'.
        /// i.e. If the value is null or non-numeric this method will 
        /// return the given default value.
        /// </summary>
        /// <param name="value">The value to return if it is a safe int.</param>
        /// <param name="defaultValue">Default value to use if the value is not 'safe'</param>
        /// <returns>The default value if the 'value' is not safe, otherwise the 'value' as an int.</returns>
        public static int SafeInt(object value, int defaultValue)
        {
            if (value == null)
                return defaultValue;
            if (!AreAllCharactersNumeric(value.ToString().Trim()))
                return defaultValue;

            return int.Parse(value.ToString());
        }

        /// <summary>
        /// Returns the given value as an short if it is 'safe'.
        /// i.e. If the value is null or non-numeric this method will return 0.
        /// </summary>
        /// <param name="value">The value to return if it is a safe short.</param>
        /// <returns>0 if the value is not safe, otherwise the value as an short.</returns>
        public static short SafeShort(object value)
        {
            return SafeShort(value, 0);
        }

        /// <summary>
        /// Returns the given value as an short if it is 'safe'.
        /// i.e. If the value is null or non-numeric this method will 
        /// return the given default value.
        /// </summary>
        /// <param name="value">The value to return if it is a safe short.</param>
        /// <param name="defaultValue">Default value to use if the value is not 'safe'</param>
        /// <returns>The default value if the 'value' is not safe, otherwise the 'value' as an short.</returns>
        public static short SafeShort(object value, short defaultValue)
        {
            if (value == null)
                return defaultValue;
            if (!IsNumeric(value.ToString().Trim()))
                return defaultValue;

            int number = int.Parse(value.ToString());
            if ((number < -32768) || (number > 32768))
                return defaultValue;

            return short.Parse(value.ToString());
        }

        /// <summary>
        /// Returns the given value as an long if it is 'safe'.
        /// i.e. If the value is null or non-numeric this method will return 0.
        /// </summary>
        /// <param name="value">The value to return if it is a safe long.</param>
        /// <returns>0 if the value is not safe, otherwise the value as an long.</returns>
        public static long SafeLong(object value)
        {
            return SafeLong(value, 0);
        }

        /// <summary>
        /// Returns the given value as an long if it is 'safe'.
        /// i.e. If the value is null or non-numeric this method will 
        /// return the given default value.
        /// </summary>
        /// <param name="value">The value to return if it is a safe long.</param>
        /// <param name="defaultValue">Default value to use if the value is not 'safe'</param>
        /// <returns>The default value if the 'value' is not safe, otherwise the 'value' as an long.</returns>
        public static long SafeLong(object value, long defaultValue)
        {
            if (value == null)
                return defaultValue;
            if (!AreAllCharactersNumeric(value.ToString().Trim()))
                return defaultValue;

            return long.Parse(value.ToString());
        }

        /// <summary>
        /// Returns the given value as a double if it is 'safe'.
        /// i.e. If the value is null or non-numeric this method will return 0.
        /// </summary>
        /// <param name="value">The value to return if it is a safe double.</param>
        /// <returns>0 if the value is not safe, otherwise the value as a double.</returns>
        public static double SafeDouble(object value)
        {
            return SafeDouble(value, 0);
        }

        /// <summary>
        /// Returns the given value as a double if it is 'safe'.
        /// i.e. If the value is null or non-numeric this method will 
        /// return the given default value.
        /// </summary>
        /// <param name="value">The value to return if it is a safe double.</param>
        /// <param name="defaultValue">Default value to use if the value is not 'safe'</param>
        /// <returns>The default value if the 'value' is not safe, otherwise the 'value' as a double.</returns>
        public static double SafeDouble(object value, double defaultValue)
        {
            if (value == null)
                return defaultValue;
            if (!IsNumeric(value.ToString().Trim()))
                return defaultValue;

            return double.Parse(value.ToString());
        }

        /// <summary>
        /// Returns the given value as a float if it is 'safe'.
        /// i.e. If the value is null or non-numeric this method will return 0.
        /// </summary>
        /// <param name="value">The value to return if it is a safe float.</param>
        /// <returns>0 if the value is not safe, otherwise the value as a float.</returns>
        public static float SafeFloat(object value)
        {
            return SafeFloat(value, 0);
        }

        /// <summary>
        /// Returns the given value as a float if it is 'safe'.
        /// i.e. If the value is null or non-numeric this method will 
        /// return the given default value.
        /// </summary>
        /// <param name="value">The value to return if it is a safe float.</param>
        /// <param name="defaultValue">Default value to use if the value is not 'safe'</param>
        /// <returns>The default value if the 'value' is not safe, otherwise the 'value' as a float.</returns>
        public static float SafeFloat(object value, float defaultValue)
        {
            if (value == null)
                return defaultValue;
            if (!IsNumeric(value.ToString().Trim()))
                return defaultValue;

            return float.Parse(value.ToString());
        }

        /// <summary>
        /// Returns the given value as a decimal if it is 'safe'.
        /// i.e. If the value is null or non-numeric this method will return 0.
        /// </summary>
        /// <param name="value">The value to return if it is a safe decimal.</param>
        /// <returns>0 if the value is not safe, otherwise the value as a decimal.</returns>
        public static decimal SafeDecimal(object value)
        {
            return SafeDecimal(value, 0);
        }

        /// <summary>
        /// Returns the given value as a decimal if it is 'safe'.
        /// i.e. If the value is null or non-numeric this method will 
        /// return the given default value.
        /// </summary>
        /// <param name="value">The value to return if it is a safe decimal.</param>
        /// <param name="defaultValue">Default value to use if the value is not 'safe'</param>
        /// <returns>The default value if the 'value' is not safe, otherwise the 'value' as a decimal.</returns>
        public static decimal SafeDecimal(object value, decimal defaultValue)
        {
            if (value == null)
                return defaultValue;
            if (!IsNumeric(value.ToString().Trim()))
                return defaultValue;

            return decimal.Parse(value.ToString());
        }

        /// <summary>
        /// If the value is null this method will return an empty string
        /// OR if it 'is a string' then it returns the value
        /// OTHERWISE it returns the objects .ToString method.
        /// </summary>
        /// <param name="value">The value to return if it is a safe string.</param>
        /// <returns>string.Empty if the value is not safe, otherwise the value as an string.</returns>
        public static string SafeString(object value)
        {
            if (value == null)
                return string.Empty;
            if (value is string)
                return (string) value;

            return value.ToString();
        }

        /// <summary>
        /// If value is a valid guid it gets returned as a guid,
        /// OTHERWISE it returns Guid.Empty
        /// </summary>
        public static Guid SafeGuid(object value)
        {
            try
            {
                // 32 contiguous digits is the minimum (my understanding of it...)
                if (value == null || value.ToString().Length < 32)
                    return Guid.Empty;

                return new Guid(SafeString(value));
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
                return Guid.Empty;
            }
        }
    }
}

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
Software Developer (Senior) Peer Placements Pty Ltd
Australia Australia
I live in Sydney and have been a developer for almost a decade now. I have a passion for technology and a strong interest in discovering 'better, cleaner, faster' ways to get systems out the door because I believe software development takes too long. If I have an idea I want to realise it as quickly as possible...plus writing systems for someone else I want to deliver quickly so I can move onto the next interesting project!

Comments and Discussions