org.feijoas.mango.common.base

Preconditions

Related Doc: package base

object Preconditions

Simple static methods to be called at the start of your own methods to verify correct arguments and state. This allows constructs such as

if (count <= 0) {
    throw new IllegalArgumentException("must be positive: " + count)
}

to be replaced with the more compact

checkArgument(count > 0, "must be positive: %s", count)

Note that the sense of the expression is inverted with Preconditions you declare what you expect to be true, just as you do with an assert or a JUnit assertTrue call.

Warning: only the "%s" specifier is recognized as a placeholder in these messages.

Take care not to confuse precondition checking with other similar types of checks! Precondition exceptions -- including those provided here, but also IndexOutOfBoundsException, NoSuchElementException, UnsupportedOperationException and others -- are used to signal that the calling method has made an error. This tells the caller that it should not have invoked the method when it did, with the arguments it did, or perhaps ever. Postcondition or other invariant failures should not throw these types of exceptions.

See also the Guava User Guide on using Preconditions

Since

0.7 (copied from guava-libraries)

Linear Supertypes
AnyRef, Any
Ordering
  1. Alphabetic
  2. By inheritance
Inherited
  1. Preconditions
  2. AnyRef
  3. Any
  1. Hide All
  2. Show all
Learn more about member selection
Visibility
  1. Public
  2. All

Value Members

  1. final def !=(arg0: Any): Boolean

    Definition Classes
    AnyRef → Any
  2. final def ##(): Int

    Definition Classes
    AnyRef → Any
  3. final def ==(arg0: Any): Boolean

    Definition Classes
    AnyRef → Any
  4. final def asInstanceOf[T0]: T0

    Definition Classes
    Any
  5. def checkArgument(expression: Boolean, errorMessageTemplate: String, errorMessageArg: Any, moreArgs: Any*): Unit

    Ensures the truth of an expression involving one or more parameters to the calling method.

    Ensures the truth of an expression involving one or more parameters to the calling method.

    expression

    an expression: Boolean

    errorMessageTemplate

    a template for the exception message should the check fail. The message is formed by replacing each %s placeholder in the template with an argument. These are matched by position - the first %s gets errorMessageArgs[0], etc. Unmatched arguments will be appended to the formatted message in square braces. Unmatched placeholders will be left as-is. Arguments are converted to strings using String#valueOf(Any).

    errorMessageArg

    the first argument to be substituted into the message template.

    moreArgs

    the other arguments to be substituted into the message template.

    Exceptions thrown

    IllegalArgumentException if expression is false

    NullPointerException if the check fails and either errorMessageTemplate or errorMessageArgs is null (don't let this happen)

  6. def checkArgument(expression: Boolean, errorMessage: Any): Unit

    Ensures the truth of an expression involving one or more parameters to the calling method.

    Ensures the truth of an expression involving one or more parameters to the calling method.

    expression

    an expression: Boolean

    errorMessage

    the exception message to use if the check fails will be converted to a string using String#valueOf(Any)

    Exceptions thrown

    IllegalArgumentException if expression is false

  7. def checkArgument(expression: Boolean): Unit

    Ensures the truth of an expression involving one or more parameters to the calling method.

    Ensures the truth of an expression involving one or more parameters to the calling method.

    expression

    an expression: Boolean

    Exceptions thrown

    IllegalArgumentException if expression is false

  8. def checkElementIndex(index: Int, size: Int, desc: String): Int

    Ensures that index specifies a valid element in an array, list or string of size size.

    Ensures that index specifies a valid element in an array, list or string of size size. An element index may range from zero, inclusive, to size, exclusive.

    index

    a user-supplied index identifying an element of an array, list or string

    size

    the size of that array, list or string

    desc

    the text to use to describe this index in an error message

    returns

    the value of index

    Exceptions thrown

    IllegalArgumentException if size is negative

    IndexOutOfBoundsException if index is negative or is not less than size

  9. def checkElementIndex(index: Int, size: Int): Int

    Ensures that index specifies a valid element in an array, list or string of size size.

    Ensures that index specifies a valid element in an array, list or string of size size. An element index may range from zero, inclusive, to size, exclusive.

    index

    a user-supplied index identifying an element of an array, list or string

    size

    the size of that array, list or string

    returns

    the value of index

    Exceptions thrown

    IllegalArgumentException if size is negative

    IndexOutOfBoundsException if index is negative or is not less than size

  10. def checkNotNull[T](reference: T, errorMessageTemplate: String, errorMessageArg: Any, moreArgs: Any*): T

    Ensures that an objecreference: T passed as a parameter to the calling method is not null.

    Ensures that an objecreference: T passed as a parameter to the calling method is not null.

    reference

    an objecreference: T

    errorMessageTemplate

    a template for the exception message should the check fail. The message is formed by replacing each %s placeholder in the template with an argument. These are matched by position - the first %s gets errorMessageArgs[0], etc. Unmatched arguments will be appended to the formatted message in square braces. Unmatched placeholders will be left as-is. Arguments are converted to strings using String#valueOf(Any).

    errorMessageArg

    the first argument to be substituted into the message template.

    moreArgs

    the other arguments to be substituted into the message template.

    returns

    the non-null reference that was validated

    Exceptions thrown

    NullPointerException if reference is null

  11. def checkNotNull[T](reference: T, errorMessage: Any): T

    Ensures that an objecreference: T passed as a parameter to the calling method is not null.

    Ensures that an objecreference: T passed as a parameter to the calling method is not null.

    reference

    an objecreference: T

    errorMessage

    the exception message to use if the check fails will be converted to a string using String#valueOf(Any)

    returns

    the non-null reference that was validated

    Exceptions thrown

    NullPointerException if reference is null

  12. def checkNotNull[T](reference: T): T

    Ensures that an objecreference: T passed as a parameter to the calling method is not null.

    Ensures that an objecreference: T passed as a parameter to the calling method is not null.

    reference

    an objecreference: T

    returns

    the non-null reference that was validated

    Exceptions thrown

    NullPointerException if reference is null

  13. def checkPositionIndex(index: Int, size: Int, desc: String): Int

    Ensures that index specifies a valid position in an array, list or string of size size.

    Ensures that index specifies a valid position in an array, list or string of size size. A position index may range from zero to size, inclusive.

    index

    a user-supplied index identifying a position in an array, list or string

    size

    the size of that array, list or string

    desc

    the text to use to describe this index in an error message

    returns

    the value of index

    Exceptions thrown

    IllegalArgumentException if size is negative

    IndexOutOfBoundsException if index is negative or is greater than size

  14. def checkPositionIndex(index: Int, size: Int): Int

    Ensures that index specifies a valid position in an array, list or string of size size.

    Ensures that index specifies a valid position in an array, list or string of size size. A position index may range from zero to size, inclusive.

    index

    a user-supplied index identifying a position in an array, list or string

    size

    the size of that array, list or string

    returns

    the value of index

    Exceptions thrown

    IllegalArgumentException if size is negative

    IndexOutOfBoundsException if index is negative or is greater than size

  15. def checkPositionIndexes(start: Int, end: Int, size: Int): Unit

    Ensures that start and end specify a valid positions in an array, list or string of size size, and are in order.

    Ensures that start and end specify a valid positions in an array, list or string of size size, and are in order. A position index may range from zero to size, inclusive.

    start

    a user-supplied index identifying a starting position in an array, list or string

    end

    a user-supplied index identifying a ending position in an array, list or string

    size

    the size of that array, list or string

    Exceptions thrown

    IllegalArgumentException if size is negative

    IndexOutOfBoundsException if either index is negative or is greater than size, or if end is less than start

  16. def checkState(expression: Boolean, errorMessageTemplate: String, errorMessageArg: Any, moreArgs: Any*): Unit

    Ensures the truth of an expression involving the state of the calling instance, but not involving any parameters to the calling method.

    Ensures the truth of an expression involving the state of the calling instance, but not involving any parameters to the calling method.

    expression

    a expression: Boolean

    errorMessageTemplate

    a template for the exception message should the check fail. The message is formed by replacing each %s placeholder in the template with an argument. These are matched by position - the first %s gets errorMessageArgs[0], etc. Unmatched arguments will be appended to the formatted message in square braces. Unmatched placeholders will be left as-is. Arguments are converted to strings using String#valueOf(Any).

    errorMessageArg

    the first argument to be substituted into the message template.

    moreArgs

    the other arguments to be substituted into the message template.

    Exceptions thrown

    IllegalStateException if expression is false

    NullPointerException if the check fails and either errorMessageTemplate or errorMessageArgs is null (don't let this happen)

  17. def checkState(expression: Boolean, errorMessage: Any): Unit

    Ensures the truth of an expression involving the state of the calling instance, but not involving any parameters to the calling method.

    Ensures the truth of an expression involving the state of the calling instance, but not involving any parameters to the calling method.

    expression

    an expression: Boolean

    errorMessage

    the exception message to use if the check fails will be converted to a string using String#valueOf(Any)

    Exceptions thrown

    IllegalStateException if expression is false

  18. def checkState(expression: Boolean): Unit

    Ensures the truth of an expression involving the state of the calling instance, but not involving any parameters to the calling method.

    Ensures the truth of an expression involving the state of the calling instance, but not involving any parameters to the calling method.

    expression

    an expression: Boolean

    Exceptions thrown

    IllegalStateException if expression is false

  19. def clone(): AnyRef

    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  20. final def eq(arg0: AnyRef): Boolean

    Definition Classes
    AnyRef
  21. def equals(arg0: Any): Boolean

    Definition Classes
    AnyRef → Any
  22. def finalize(): Unit

    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( classOf[java.lang.Throwable] )
  23. final def getClass(): Class[_]

    Definition Classes
    AnyRef → Any
  24. def hashCode(): Int

    Definition Classes
    AnyRef → Any
  25. final def isInstanceOf[T0]: Boolean

    Definition Classes
    Any
  26. final def ne(arg0: AnyRef): Boolean

    Definition Classes
    AnyRef
  27. final def notify(): Unit

    Definition Classes
    AnyRef
  28. final def notifyAll(): Unit

    Definition Classes
    AnyRef
  29. final def synchronized[T0](arg0: ⇒ T0): T0

    Definition Classes
    AnyRef
  30. def toString(): String

    Definition Classes
    AnyRef → Any
  31. final def wait(): Unit

    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  32. final def wait(arg0: Long, arg1: Int): Unit

    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  33. final def wait(arg0: Long): Unit

    Definition Classes
    AnyRef
    Annotations
    @throws( ... )

Inherited from AnyRef

Inherited from Any

Ungrouped