/AvalonEdit/ICSharpCode.AvalonEdit/Utils/ThrowUtil.cs

http://github.com/icsharpcode/ILSpy · C# · 71 lines · 34 code · 6 blank · 31 comment · 5 complexity · 0ceb8213dc2fe3c02213a3812d9f7bba MD5 · raw file

  1. // Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy of this
  4. // software and associated documentation files (the "Software"), to deal in the Software
  5. // without restriction, including without limitation the rights to use, copy, modify, merge,
  6. // publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
  7. // to whom the Software is furnished to do so, subject to the following conditions:
  8. //
  9. // The above copyright notice and this permission notice shall be included in all copies or
  10. // substantial portions of the Software.
  11. //
  12. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  13. // INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  14. // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
  15. // FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  16. // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  17. // DEALINGS IN THE SOFTWARE.
  18. using System;
  19. using System.Globalization;
  20. namespace ICSharpCode.AvalonEdit.Utils
  21. {
  22. /// <summary>
  23. /// Contains exception-throwing helper methods.
  24. /// </summary>
  25. static class ThrowUtil
  26. {
  27. /// <summary>
  28. /// Throws an ArgumentNullException if <paramref name="val"/> is null; otherwise
  29. /// returns val.
  30. /// </summary>
  31. /// <example>
  32. /// Use this method to throw an ArgumentNullException when using parameters for base
  33. /// constructor calls.
  34. /// <code>
  35. /// public VisualLineText(string text) : base(ThrowUtil.CheckNotNull(text, "text").Length)
  36. /// </code>
  37. /// </example>
  38. public static T CheckNotNull<T>(T val, string parameterName) where T : class
  39. {
  40. if (val == null)
  41. throw new ArgumentNullException(parameterName);
  42. return val;
  43. }
  44. public static int CheckNotNegative(int val, string parameterName)
  45. {
  46. if (val < 0)
  47. throw new ArgumentOutOfRangeException(parameterName, val, "value must not be negative");
  48. return val;
  49. }
  50. public static int CheckInRangeInclusive(int val, string parameterName, int lower, int upper)
  51. {
  52. if (val < lower || val > upper)
  53. throw new ArgumentOutOfRangeException(parameterName, val, "Expected: " + lower.ToString(CultureInfo.InvariantCulture) + " <= " + parameterName + " <= " + upper.ToString(CultureInfo.InvariantCulture));
  54. return val;
  55. }
  56. public static InvalidOperationException NoDocumentAssigned()
  57. {
  58. return new InvalidOperationException("Document is null");
  59. }
  60. public static InvalidOperationException NoValidCaretPosition()
  61. {
  62. return new InvalidOperationException("Could not find a valid caret position in the line");
  63. }
  64. }
  65. }