From 938c9b907497407bbf987c88c18524ec4b6526b3 Mon Sep 17 00:00:00 2001 From: JuanAr Date: Tue, 29 Jan 2019 23:30:39 -0300 Subject: [PATCH] Refactor nested if-else, changed to a switch-case (#1433) --- .../Parsers/BaseMergedDateTimeParser.cs | 108 ++++++++++-------- 1 file changed, 60 insertions(+), 48 deletions(-) diff --git a/.NET/Microsoft.Recognizers.Text.DateTime/Parsers/BaseMergedDateTimeParser.cs b/.NET/Microsoft.Recognizers.Text.DateTime/Parsers/BaseMergedDateTimeParser.cs index 31a5080d0d..b7fdb14661 100644 --- a/.NET/Microsoft.Recognizers.Text.DateTime/Parsers/BaseMergedDateTimeParser.cs +++ b/.NET/Microsoft.Recognizers.Text.DateTime/Parsers/BaseMergedDateTimeParser.cs @@ -106,54 +106,8 @@ public DateTimeParseResult Parse(ExtractResult er, DateObject refTime) } } - if (er.Type.Equals(Constants.SYS_DATETIME_DATE)) - { - pr = this.Config.DateParser.Parse(er, referenceTime); - if (pr.Value == null) - { - pr = Config.HolidayParser.Parse(er, referenceTime); - } - } - else if (er.Type.Equals(Constants.SYS_DATETIME_TIME)) - { - pr = this.Config.TimeParser.Parse(er, referenceTime); - } - else if (er.Type.Equals(Constants.SYS_DATETIME_DATETIME)) - { - pr = this.Config.DateTimeParser.Parse(er, referenceTime); - } - else if (er.Type.Equals(Constants.SYS_DATETIME_DATEPERIOD)) - { - pr = this.Config.DatePeriodParser.Parse(er, referenceTime); - } - else if (er.Type.Equals(Constants.SYS_DATETIME_TIMEPERIOD)) - { - pr = this.Config.TimePeriodParser.Parse(er, referenceTime); - } - else if (er.Type.Equals(Constants.SYS_DATETIME_DATETIMEPERIOD)) - { - pr = this.Config.DateTimePeriodParser.Parse(er, referenceTime); - } - else if (er.Type.Equals(Constants.SYS_DATETIME_DURATION)) - { - pr = this.Config.DurationParser.Parse(er, referenceTime); - } - else if (er.Type.Equals(Constants.SYS_DATETIME_SET)) - { - pr = this.Config.SetParser.Parse(er, referenceTime); - } - else if (er.Type.Equals(Constants.SYS_DATETIME_DATETIMEALT)) - { - pr = this.Config.DateTimeAltParser.Parse(er, referenceTime); - } - else if (er.Type.Equals(Constants.SYS_DATETIME_TIMEZONE)) - { - if ((Config.Options & DateTimeOptions.EnablePreview) != 0) - { - pr = this.Config.TimeZoneParser.Parse(er, referenceTime); - } - } - else + pr = ParseResult(er, referenceTime); + if (pr == null) { return null; } @@ -902,5 +856,63 @@ private string DetermineResolutionDateTimeType(Dictionary pastRe return pastResolutionStr.Keys.First().ToLower(); } } + + private DateTimeParseResult ParseResult(ExtractResult extractResult, DateObject referenceTime) + { + DateTimeParseResult parseResult = null; + switch (extractResult.Type) + { + case Constants.SYS_DATETIME_DATE: + parseResult = this.Config.DateParser.Parse(extractResult, referenceTime); + if (parseResult.Value == null) + { + parseResult = Config.HolidayParser.Parse(extractResult, referenceTime); + } + + break; + case Constants.SYS_DATETIME_TIME: + parseResult = this.Config.TimeParser.Parse(extractResult, referenceTime); + + break; + case Constants.SYS_DATETIME_DATETIME: + parseResult = this.Config.DateTimeParser.Parse(extractResult, referenceTime); + + break; + case Constants.SYS_DATETIME_DATEPERIOD: + parseResult = this.Config.DatePeriodParser.Parse(extractResult, referenceTime); + + break; + case Constants.SYS_DATETIME_TIMEPERIOD: + parseResult = this.Config.TimePeriodParser.Parse(extractResult, referenceTime); + + break; + case Constants.SYS_DATETIME_DATETIMEPERIOD: + parseResult = this.Config.DateTimePeriodParser.Parse(extractResult, referenceTime); + + break; + case Constants.SYS_DATETIME_DURATION: + parseResult = this.Config.DurationParser.Parse(extractResult, referenceTime); + + break; + case Constants.SYS_DATETIME_SET: + parseResult = this.Config.SetParser.Parse(extractResult, referenceTime); + + break; + case Constants.SYS_DATETIME_DATETIMEALT: + parseResult = this.Config.DateTimeAltParser.Parse(extractResult, referenceTime); + + break; + case Constants.SYS_DATETIME_TIMEZONE: + if ((Config.Options & DateTimeOptions.EnablePreview) != 0) + { + parseResult = this.Config.TimeZoneParser.Parse(extractResult, referenceTime); + } + + break; + default: + return null; + } + return parseResult; + } } } \ No newline at end of file