Google Guava Learning Record "II" precondition

Source: Internet
Author: User
Tags google guava

1  int i = 4;  2          int j = 5; 3          Preconditions.checkargument (I>j, "I should bigger than J, but I am%s and J is%s", i,j);     
Exception in thread "main" java.lang.illegalargumentexception:i should bigger than J, but I am 4 and J is 5 at    Co M.google.common.base.preconditions.checkargument (Preconditions.java:145)    at Google.guava.GuavaDemo.main (Guavademo.java:) at    sun.reflect.NativeMethodAccessorImpl.invoke0 ( Native Method) at    Sun.reflect.NativeMethodAccessorImpl.invoke (nativemethodaccessorimpl.java:+) At    Sun.reflect.DelegatingMethodAccessorImpl.invoke (delegatingmethodaccessorimpl.java:+)    at Java.lang.reflect.Method.invoke (Method.java:497)    at Com.intellij.rt.execution.application.AppMain.main (Appmain.java:140)

First put on the most commonly used checkargument, you can see that when the expression is not satisfied, the output error message can be defined by itself, and very clear.

The following is an explanation of the 3 parameters:

    • No extra arguments. Any exceptions is thrown without error messages. No error messages
    • An extra Object argument. Any exception are thrown with the error message object.tostring (). As long as the error message
    • An extra String argument, with an arbitrary number of additional Object arguments. This behaves something like printf, but for GWT compatibility and efficiency, it is only allows %s indicators plus variable, Then check if the value is incorrect when you make an error

Others should be used very little, in the project ACK found only used checkargument. You don't have to explain.

Checkargument (Boolean) Checks the boolean is true. Use the validating arguments to methods. IllegalArgumentException
Checknotnull (T) Checks The value is not NULL. Returns the value directly, so you can use checknotnull (value) inline. NullPointerException
CheckState (Boolean) Checks some state of the object, not dependent on the method arguments. For example, a Iteratormight use this to check that next have been called before any call to remove< /c6>. IllegalStateException
Checkelementindex (int index, int size) Checks that index is a valid element index to a list, string, or array with the specified size. An element index could range from 0 inclusive to size exclusive. You don ' t pass the list, string, or array directly; You just pass its size.
Returns index.
Indexoutofboundsexception
Checkpositionindex (int index, int size) Checks that index is a valid position index to a list, string, or array with the specified size. A position index may range from 0 inclusive to size inclusive. You don ' t pass the list, string, or array directly; You just pass its size.
Returns index.
Indexoutofboundsexception
checkpositionindexes (int start, int end, int size)

Google Guava Learning Record "II" precondition

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.