A status hander object that keeps the worst status

Ok, so I have an object that is to be compared to a default value and based on the comparison has a status of either normal (0), warning (1), or error (2). Of course, it has an array of child objects that each can also be normal, warning, or error. This parent object should have the status that is the worst or greatest.

So I wrote an object caled StatusHandler.cs that does this in C#. It is simple.

/*Copyright 2010 Jared Barneck All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this
 *    list of conditions and the following disclaimer.
 *
 * 2. 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.
 *
 * THIS SOFTWARE IS PROVIDED BY Jared Barneck ``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 <copyright HOLDER> 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.
 *
 * The views and conclusions contained in the software and documentation are those
 * of the authors and should not be interpreted as representing official policies,
 * either expressed or implied, of Jared Barneck.
 */

namespace StatusHandler
{
    /*
     * The purpose of this class is to hold the worst result
     * received when comparing values in a multicolum row.
     */
    public class StatusHandler
    {
        #region Member Variables
        private CompareResult mResult = CompareResult.Normal;
        #endregion

        #region Constructors

        /*
		 * The default constructor
 		 */
        public StatusHandler()
        {
        }

        public StatusHandler(CompareResult inResult)
        {
            mResult = inResult;
        }

        #endregion

        #region Properties

        public CompareResult Result
        {
            get { return mResult; }
            // Don't allow setting, this should be
            // done by the AddResult function;
        }
        #endregion

        #region Functions
        /*
         *
         */
        public void AddResult(CompareResult inResult)
        {
            // Once an error, always an error
            if (Result == CompareResult.Error)
            {
                return;
            }
            // If a warning, only change the value
            // If the new value is an error.
            if (Result == CompareResult.Warning)
            {
                if (inResult == CompareResult.Error)
                {
                    mResult = inResult;
                }
                return;
            }
            // If result is not an error or warning, then
            // it is most efficient just to set the new
            // value to the incoming value.
            mResult = inResult;
        }

        /*
         * Sets the status back to normal
         */
        public void Reset()
        {
            mResult = CompareResult.Normal;
        }
        #endregion

        #region Enums
        public enum CompareResult
        {
            Normal = 0,
            Warning,
            Error
        }
        #endregion
    }
}

Leave a Reply