private void CreateRangeFromRawValues(string lowerBound, string upperBound, MetaAttribute.MetaAttributeDataType type, FormattedOrRaw format) { Contract.Assert(IsValidValues(lowerBound, upperBound, type, format), "Invalid ValueRange."); _lowerBound = (lowerBound.Length > 0) ? ValueContainer.Create(lowerBound, type, format) : null; _upperBound = (upperBound.Length > 0) ? ValueContainer.Create(upperBound, type, format) : null; }
/// <summary> /// To be valid a value must be valid for its type or empty. /// </summary> /// <param name="lowerBound"></param><param name="upperBound"></param><param name="type"></param><param name="format"></param> /// <returns></returns> public static bool IsValidValues(string lowerBound, string upperBound, MetaAttribute.MetaAttributeDataType type, FormattedOrRaw format) { if (lowerBound.Contains("-") || upperBound.Contains("-")) return false; if (!lowerBound.Equals(String.Empty) && !ValueContainerValidator.Validate(type, lowerBound, format)) return false; if (!upperBound.Equals(String.Empty) && !ValueContainerValidator.Validate(type, upperBound, format)) return false; return true; }
/// <summary>Validate a string representing a value (for a preference or attribute).</summary> /// <param name="type"></param><param name="value"></param><param name="representation"></param> /// <returns></returns> public static bool Validate(MetaAttribute.MetaAttributeDataType type, string value, FormattedOrRaw representation) { if (value == null) return false; switch (type) { case MetaAttribute.MetaAttributeDataType.STRING: return ValidateString(value, representation); case MetaAttribute.MetaAttributeDataType.INTEGER: return ValidateInteger(value, representation); case MetaAttribute.MetaAttributeDataType.CURRENCY: return ValidateCurrency(value, representation); default: throw new Exception("Unknown MetaAttributeDataType: " + type.ToString()); } }
/// <summary>Validate a string representing a currency value.</summary> /// <param name="value"></param><param name="representation"></param> /// <returns></returns> private static bool ValidateCurrency(string value, FormattedOrRaw representation) { switch (representation) { case FormattedOrRaw.FORMATTED: { decimal result; if (!decimal.TryParse(value, out result)) return false; if (result > 21474835 || result < 0) return false; // conversion to int will not be possible. return true; } case FormattedOrRaw.RAW: { int result; return (int.TryParse(value, out result) && result >= 0); } default: throw new Exception("Unknown formatting type " + representation.ToString()); } }
/// <summary>Validate a string representing a string value.</summary> /// <param name="value"></param><param name="representation"></param> /// <returns></returns> private static bool ValidateString(string value, FormattedOrRaw representation) { return true; }
/// <summary>Validate a string representing an integer value.</summary> /// <param name="value"></param><param name="representation"></param> /// <returns></returns> private static bool ValidateInteger(string value, FormattedOrRaw representation) { int i; return int.TryParse(value, out i); }
public IntegerValue(string value, FormattedOrRaw formatting) : base(value, formatting) { }
/// <summary> /// Creates a new ValueContainer of the appropriate specialisation. /// </summary> /// <param name="value"></param><param name="type"></param><param name="format"></param> /// <returns></returns> public static ValueContainer Create(string value, MetaAttribute.MetaAttributeDataType type, FormattedOrRaw format) { Contract.Assert(ValueContainerValidator.Validate(type, value, format), "Value " + value + " is not valid."); switch (type) { case MetaAttribute.MetaAttributeDataType.CURRENCY: return (value.Length > 0) ? new CurrencyValue(value, format) : null; case MetaAttribute.MetaAttributeDataType.INTEGER: return (value.Length > 0) ? new IntegerValue(value, format) : null; case MetaAttribute.MetaAttributeDataType.STRING: return (value.Length > 0) ? new StringValue(value, format) : null; default: throw new Exception("Unknown data type " + type.ToString()); } }
public CurrencyValue(string value, FormattedOrRaw formatting) : base(value, formatting) { }
protected ValueContainer(string value, FormattedOrRaw formatting) { Contract.Assert(value != null, "A value cannot be set to null."); _value = value; _formatting = formatting; }
public StringValue(string value, FormattedOrRaw formatting) : base(value, formatting) { }
public ValueRange(string lowerBound, string upperBound, MetaAttribute.MetaAttributeDataType type, FormattedOrRaw format) { CreateRangeFromRawValues(lowerBound, upperBound, type, format); }