fCraft.IntKeyAttribute.Validate C# (CSharp) Method

Validate() public method

public Validate ( string value ) : void
value string
return void
        public override void Validate( string value )
        {
            base.Validate( value );
            int parsedValue;
            if ( !Int32.TryParse( value, out parsedValue ) ) {
                throw new FormatException( "Value cannot be parsed as an integer." );
            }

            if ( AlwaysAllowZero && parsedValue == 0 ) {
                return;
            }

            if ( MinValue != int.MinValue && parsedValue < MinValue ) {
                throw new FormatException( String.Format( "Value is too low ({0}); expected at least {1}.", parsedValue, MinValue ) );
            }

            if ( MaxValue != int.MaxValue && parsedValue > MaxValue ) {
                throw new FormatException( String.Format( "Value is too high ({0}); expected at most {1}.", parsedValue, MaxValue ) );
            }

            if ( MultipleOf != 0 && ( parsedValue % MultipleOf != 0 ) ) {
                throw new FormatException( String.Format( "Value ({0}) is not a multiple of {1}.", parsedValue, MultipleOf ) );
            }
            if ( PowerOfTwo ) {
                bool found = false;
                for ( int i = 0; i < 31; i++ ) {
                    if ( parsedValue == ( 1 << i ) ) {
                        found = true;
                        break;
                    }
                }
                if ( !found && parsedValue != 0 ) {
                    throw new FormatException( String.Format( "Value ({0}) is not a power of two.", parsedValue ) );
                }
            }
            if ( ValidValues != null ) {
                if ( !ValidValues.Any( t => parsedValue == t ) ) {
                    throw new FormatException( String.Format( "Value ({0}) is not on the list of valid values.", parsedValue ) );
                }
            }
            if ( InvalidValues != null ) {
                if ( !InvalidValues.All( t => parsedValue != t ) ) {
                    throw new FormatException( String.Format( "Value ({0}) is on the list of invalid values.", parsedValue ) );
                }
            }
        }