Пример #1
0
 /// <summary>
 /// Create a range from the specified information.
 /// </summary>
 /// <param name="qualifier">The qualifier for the range.</param>
 /// <param name="from">The lower limit (or value to compare) for the range.</param>
 /// <param name="to">The upper limit for the range.</param>
 /// <returns>The range that was created.</returns>
 protected override DateRangeBroker CreateRange(SearchQualifierValue qualifier, DateTime?from = null, DateTime?to = null) =>
 new DateRangeBroker
 {
     Qualifier = qualifier,
     From      = from,
     To        = to
 };
 /// <summary>
 /// Create a range from the specified information.
 /// </summary>
 /// <param name="qualifier">The qualifier for the range.</param>
 /// <param name="from">The lower limit (or value to compare) for the range.</param>
 /// <param name="to">The upper limit for the range.</param>
 /// <returns>The range that was created.</returns>
 protected override Int32RangeBroker CreateRange(SearchQualifierValue qualifier, int?from = null, int?to = null) =>
 new Int32RangeBroker
 {
     Qualifier = qualifier,
     From      = from,
     To        = to
 };
 /// <summary>
 /// Create a range from the specified information.
 /// </summary>
 /// <param name="qualifier">The qualifier for the range.</param>
 /// <param name="from">The lower limit (or value to compare) for the range.</param>
 /// <param name="to">The upper limit for the range.</param>
 /// <returns>The range that was created.</returns>
 protected abstract TRange CreateRange(SearchQualifierValue qualifier,
                                       TLimit?from = null, TLimit?to = null);
        /// <summary>
        /// Try to parse the specified text.
        /// </summary>
        /// <param name="text">The text to parse.</param>
        /// <param name="result">The resulting range.</param>
        /// <returns>True, if the text was parsed successfully; otherwise, false.</returns>
        public bool TryParse(string text, out TRange result)
        {
            if (string.IsNullOrWhiteSpace(text))
            {
                result = CreateRange(SearchQualifierValue.None);
                return(true);
            }

            SearchQualifierValue qualifier = SearchQualifierValue.Equals;
            int    fromLength   = text.Length;
            int    fromPosition = 0;
            int    toPosition   = -1;
            TLimit?to           = null;

            switch (text[0])
            {
            case '>':
                fromPosition++;

                if (text.Length > 1 && text[1] == '=')
                {
                    qualifier = SearchQualifierValue.GreaterThanOrEqualTo;
                    fromPosition++;
                }
                else
                {
                    qualifier = SearchQualifierValue.GreaterThan;
                }

                fromLength -= fromPosition;
                break;

            case '<':
                fromPosition++;

                if (text.Length > 1 && text[1] == '=')
                {
                    qualifier = SearchQualifierValue.LessThanOrEqualTo;
                    fromPosition++;
                }
                else
                {
                    qualifier = SearchQualifierValue.LessThan;
                }

                fromLength -= fromPosition;
                break;

            case '=':
                qualifier  = SearchQualifierValue.Equals;
                fromLength = text.Length - 1;
                fromPosition++;
                break;

            default:
                int index = text.IndexOf("..");

                if (index >= 0)
                {
                    qualifier  = SearchQualifierValue.Between;
                    toPosition = index + 2;
                    fromLength = index;
                }

                break;
            }

            string toText   = toPosition > 0 ? text.Substring(toPosition).Trim() : null;
            string fromText = text.Substring(fromPosition, fromLength).Trim();

            if (!TryParse(fromText, out TLimit from))
            {
                result = null;
                return(false);
            }

            if (toText != null)
            {
                if (TryParse(toText, out TLimit tmp))
                {
                    to = tmp;
                }
                else
                {
                    result = null;
                    return(false);
                }
            }

            result = CreateRange(qualifier, from, to);
            return(true);
        }