public static T TryParse <T>(CrontabFieldKind kind, string expression, Converter <CrontabField, T> valueSelector, Converter <ExceptionProvider, T> errorSelector) { var field = new CrontabField(CrontabFieldImpl.FromKind(kind)); var error = field._impl.TryParse(expression, field.Accumulate, null, e => e); return(error == null?valueSelector(field) : errorSelector(error)); }
/// <summary> /// /// </summary> /// <typeparam name="T"></typeparam> /// <param name="expression"></param> /// <param name="options"></param> /// <param name="valueSelector"></param> /// <param name="errorSelector"></param> /// <returns></returns> public static T TryParse <T>(string expression, ParseOptions options, Converter <CrontabSchedule, T> valueSelector, Converter <ExceptionProvider, T> errorSelector) { if (expression == null) { throw new ArgumentNullException("expression"); } var tokens = expression.Split(StringSeparatorStock.Space, StringSplitOptions.RemoveEmptyEntries); var includingSeconds = options != null && options.IncludingSeconds; var expectedTokenCount = includingSeconds ? 6 : 5; if (tokens.Length < expectedTokenCount || tokens.Length > expectedTokenCount) { return(errorSelector(() => { var message = string.Format( "'{0}' is an invalid crontab expression. It must contain {1}.", expression, includingSeconds ? "6 components of a schedule in the sequence of seconds, minutes, hours, days, months, and days of week" : "5 components of a schedule in the sequence of minutes, hours, days, months, and days of week"); return new CrontabException(message); })); } var fields = new CrontabField[6]; var offset = includingSeconds ? 0 : 1; for (var i = 0; i < tokens.Length; i++) { var kind = (CrontabFieldKind)i + offset; var field = CrontabField.TryParse(kind, tokens[i], v => new { ErrorProvider = (ExceptionProvider)null, Value = v }, e => new { ErrorProvider = e, Value = (CrontabField)null }); if (field.ErrorProvider != null) { return(errorSelector(field.ErrorProvider)); } fields[i + offset] = field.Value; } return(valueSelector(new CrontabSchedule(fields[0], fields[1], fields[2], fields[3], fields[4], fields[5]))); }
CrontabSchedule( CrontabField seconds, CrontabField minutes, CrontabField hours, CrontabField days, CrontabField months, CrontabField daysOfWeek) { Debug.Assert(minutes != null); Debug.Assert(hours != null); Debug.Assert(days != null); Debug.Assert(months != null); Debug.Assert(daysOfWeek != null); _seconds = seconds; _minutes = minutes; _hours = hours; _days = days; _months = months; _daysOfWeek = daysOfWeek; }