Exemple #1
0
 public bool Equals(IBeatDivisor other)
 {
     if (other is null)
     {
         return(false);
     }
     if (ReferenceEquals(this, other))
     {
         return(true);
     }
     if (other is RationalBeatDivisor otherRational)
     {
         return(Equals(otherRational));
     }
     return(false);
 }
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (!(value is string str))
            {
                return(new IBeatDivisor[0]);
            }

            var vals         = str.Split(',');
            var beatDivisors = new IBeatDivisor[vals.Length];

            for (int i = 0; i < vals.Length; i++)
            {
                var val = vals[i];

                // Check if it is a positive rational and non-zero and not dividing by zero
                if (Regex.IsMatch(val, "^[\\s]*[1-9][0-9]*[\\s]*/[\\s]*[1-9][0-9]*[\\s]*$"))
                {
                    var ndSplit = val.Split('/');
                    beatDivisors[i] = new RationalBeatDivisor(int.Parse(ndSplit[0], CultureInfo.InvariantCulture),
                                                              int.Parse(ndSplit[1], CultureInfo.InvariantCulture));
                }
                else
                {
                    var valid = TypeConverters.TryParseDouble(val, out double doubleValue);
                    if (valid)
                    {
                        if (doubleValue <= 0)
                        {
                            return(new ValidationResult(false, "Beat divisor must be greater than zero."));
                        }

                        beatDivisors[i] = new IrrationalBeatDivisor(doubleValue);
                    }
                    else
                    {
                        return(new ValidationResult(false, "Double format error."));
                    }
                }
            }

            return(beatDivisors);
        }