assert

  1. /*
  2.  * Copyright 2002-2007 the original author or authors.
  3.  *
  4.  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5.  * use this file except in compliance with the License. You may obtain a copy of
  6.  * the License at
  7.  *
  8.  *      http://www.apache.org/licenses/LICENSE-2.0
  9.  *
  10.  * Unless required by applicable law or agreed to in writing, software
  11.  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12.  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13.  * License for the specific language governing permissions and limitations under
  14.  * the License.
  15.  */
  16. package org.springframework.util;
  17. import java.util.Collection;
  18. import java.util.Map;
  19. /**
  20.  * Assertion utility class that assists in validating arguments.
  21.  * Useful for identifying programmer errors early and clearly at runtime.
  22.  *
  23.  * <p>For example, if the contract of a public method states it does not
  24.  * allow <code>null</code> arguments, Assert can be used to validate that
  25.  * contract. Doing this clearly indicates a contract violation when it
  26.  * occurs and protects the class's invariants.
  27.  *
  28.  * <p>Typically used to validate method arguments rather than configuration
  29.  * properties, to check for cases that are usually programmer errors rather than
  30.  * configuration errors. In contrast to config initialization code, there is
  31.  * usally no point in falling back to defaults in such methods.
  32.  *
  33.  * <p>This class is similar to JUnit's assertion library. If an argument value is
  34.  * deemed invalid, an {@link IllegalArgumentException} is thrown (typically).
  35.  * For example:
  36.  *
  37.  * <pre class="code">
  38.  * Assert.notNull(clazz, "The class must not be null");
  39.  * Assert.isTrue(i > 0, "The value must be greater than zero");</pre>
  40.  *
  41.  * Mainly for internal use within the framework; consider Jakarta's Commons Lang
  42.  * >= 2.0 for a more comprehensive suite of assertion utilities.
  43.  *
  44.  * @author Keith Donald
  45.  * @author Juergen Hoeller
  46.  * @author Colin Sampaleanu
  47.  * @author Rob Harrop
  48.  * @since 1.1.2
  49.  */
  50. public abstract class Assert {
  51.     /**
  52.      * Assert a boolean expression, throwing <code>IllegalArgumentException</code>
  53.      * if the test result is <code>false</code>.
  54.      * <pre class="code">Assert.isTrue(i > 0, "The value must be greater than zero");</pre>
  55.      * @param expression a boolean expression
  56.      * @param message the exception message to use if the assertion fails
  57.      * @throws IllegalArgumentException if expression is <code>false</code>
  58.      */
  59.     public static void isTrue(boolean expression, String message) {
  60.         if (!expression) {
  61.             throw new IllegalArgumentException(message);
  62.         }
  63.     }
  64.     /**
  65.      * Assert a boolean expression, throwing <code>IllegalArgumentException</code>
  66.      * if the test result is <code>false</code>.
  67.      * <pre class="code">Assert.isTrue(i > 0);</pre>
  68.      * @param expression a boolean expression
  69.      * @throws IllegalArgumentException if expression is <code>false</code>
  70.      */
  71.     public static void isTrue(boolean expression) {
  72.         isTrue(expression, "[Assertion failed] - this expression must be true");
  73.     }
  74.     /**
  75.      * Assert that an object is <code>null</code> .
  76.      * <pre class="code">Assert.isNull(value, "The value must be null");</pre>
  77.      * @param object the object to check
  78.      * @param message the exception message to use if the assertion fails
  79.      * @throws IllegalArgumentException if the object is not <code>null</code>
  80.      */
  81.     public static void isNull(Object object, String message) {
  82.         if (object != null) {
  83.             throw new IllegalArgumentException(message);
  84.         }
  85.     }
  86.     /**
  87.      * Assert that an object is <code>null</code> .
  88.      * <pre class="code">Assert.isNull(value);</pre>
  89.      * @param object the object to check
  90.      * @throws IllegalArgumentException if the object is not <code>null</code>
  91.      */
  92.     public static void isNull(Object object) {
  93.         isNull(object, "[Assertion failed] - the object argument must be null");
  94.     }
  95.     /**
  96.      * Assert that an object is not <code>null</code> .
  97.      * <pre class="code">Assert.notNull(clazz, "The class must not be null");</pre>
  98.      * @param object the object to check
  99.      * @param message the exception message to use if the assertion fails
  100.      * @throws IllegalArgumentException if the object is <code>null</code>
  101.      */
  102.     public static void notNull(Object object, String message) {
  103.         if (object == null) {
  104.             throw new IllegalArgumentException(message);
  105.         }
  106.     }
  107.     /**
  108.      * Assert that an object is not <code>null</code> .
  109.      * <pre class="code">Assert.notNull(clazz);</pre>
  110.      * @param object the object to check
  111.      * @throws IllegalArgumentException if the object is <code>null</code>
  112.      */
  113.     public static void notNull(Object object) {
  114.         notNull(object, "[Assertion failed] - this argument is required; it must not be null");
  115.     }
  116.     /**
  117.      * Assert that the given String is not empty; that is,
  118.      * it must not be <code>null</code> and not the empty String.
  119.      * <pre class="code">Assert.hasLength(name, "Name must not be empty");</pre>
  120.      * @param text the String to check
  121.      * @param message the exception message to use if the assertion fails
  122.      * @see StringUtils#hasLength
  123.      */
  124.     public static void hasLength(String text, String message) {
  125.         if (!StringUtils.hasLength(text)) {
  126.             throw new IllegalArgumentException(message);
  127.         }
  128.     }
  129.     /**
  130.      * Assert that the given String is not empty; that is,
  131.      * it must not be <code>null</code> and not the empty String.
  132.      * <pre class="code">Assert.hasLength(name);</pre>
  133.      * @param text the String to check
  134.      * @see StringUtils#hasLength
  135.      */
  136.     public static void hasLength(String text) {
  137.         hasLength(text,
  138.                 "[Assertion failed] - this String argument must have length; it must not be null or empty");
  139.     }
  140.     /**
  141.      * Assert that the given String has valid text content; that is, it must not
  142.      * be <code>null</code> and must contain at least one non-whitespace character.
  143.      * <pre class="code">Assert.hasText(name, "'name' must not be empty");</pre>
  144.      * @param text the String to check
  145.      * @param message the exception message to use if the assertion fails
  146.      * @see StringUtils#hasText
  147.      */
  148.     public static void hasText(String text, String message) {
  149.         if (!StringUtils.hasText(text)) {
  150.             throw new IllegalArgumentException(message);
  151.         }
  152.     }
  153.     /**
  154.      * Assert that the given String has valid text content; that is, it must not
  155.      * be <code>null</code> and must contain at least one non-whitespace character.
  156.      * <pre class="code">Assert.hasText(name, "'name' must not be empty");</pre>
  157.      * @param text the String to check
  158.      * @see StringUtils#hasText
  159.      */
  160.     public static void hasText(String text) {
  161.         hasText(text,
  162.                 "[Assertion failed] - this String argument must have text; it must not be null, empty, or blank");
  163.     }
  164.     /**
  165.      * Assert that the given text does not contain the given substring.
  166.      * <pre class="code">Assert.doesNotContain(name, "rod", "Name must not contain 'rod'");</pre>
  167.      * @param textToSearch the text to search
  168.      * @param substring the substring to find within the text
  169.      * @param message the exception message to use if the assertion fails
  170.      */
  171.     public static void doesNotContain(String textToSearch, String substring, String message) {
  172.         if (StringUtils.hasLength(textToSearch) && StringUtils.hasLength(substring) &
  173.                 textToSearch.indexOf(substring) != -1) {
  174.             throw new IllegalArgumentException(message);
  175.         }
  176.     }
  177.     /**
  178.      * Assert that the given text does not contain the given substring.
  179.      * <pre class="code">Assert.doesNotContain(name, "rod");</pre>
  180.      * @param textToSearch the text to search
  181.      * @param substring the substring to find within the text
  182.      */
  183.     public static void doesNotContain(String textToSearch, String substring) {
  184.         doesNotContain(textToSearch, substring,
  185.                 "[Assertion failed] - this String argument must not contain the substring [" + substring + "]");
  186.     }
  187.     /**
  188.      * Assert that an array has elements; that is, it must not be
  189.      * <code>null</code> and must have at least one element.
  190.      * <pre class="code">Assert.notEmpty(array, "The array must have elements");</pre>
  191.      * @param array the array to check
  192.      * @param message the exception message to use if the assertion fails
  193.      * @throws IllegalArgumentException if the object array is <code>null</code> or has no elements
  194.      */
  195.     public static void notEmpty(Object[] array, String message) {
  196.         if (ObjectUtils.isEmpty(array)) {
  197.             throw new IllegalArgumentException(message);
  198.         }
  199.     }
  200.     /**
  201.      * Assert that an array has elements; that is, it must not be
  202.      * <code>null</code> and must have at least one element.
  203.      * <pre class="code">Assert.notEmpty(array);</pre>
  204.      * @param array the array to check
  205.      * @throws IllegalArgumentException if the object array is <code>null</code> or has no elements
  206.      */
  207.     public static void notEmpty(Object[] array) {
  208.         notEmpty(array, "[Assertion failed] - this array must not be empty: it must contain at least 1 element");
  209.     }
  210.     /**
  211.      * Assert that an array has no null elements.
  212.      * Note: Does not complain if the array is empty!
  213.      * <pre class="code">Assert.noNullElements(array, "The array must have non-null elements");</pre>
  214.      * @param array the array to check
  215.      * @param message the exception message to use if the assertion fails
  216.      * @throws IllegalArgumentException if the object array contains a <code>null</code> element
  217.      */
  218.     public static void noNullElements(Object[] array, String message) {
  219.         if (array != null) {
  220.             for (int i = 0; i < array.length; i++) {
  221.                 if (array[i] == null) {
  222.                     throw new IllegalArgumentException(message);
  223.                 }
  224.             }
  225.         }
  226.     }
  227.     /**
  228.      * Assert that an array has no null elements.
  229.      * Note: Does not complain if the array is empty!
  230.      * <pre class="code">Assert.noNullElements(array);</pre>
  231.      * @param array the array to check
  232.      * @throws IllegalArgumentException if the object array contains a <code>null</code> element
  233.      */
  234.     public static void noNullElements(Object[] array) {
  235.         noNullElements(array, "[Assertion failed] - this array must not contain any null elements");
  236.     }
  237.     /**
  238.      * Assert that a collection has elements; that is, it must not be
  239.      * <code>null</code> and must have at least one element.
  240.      * <pre class="code">Assert.notEmpty(collection, "Collection must have elements");</pre>
  241.      * @param collection the collection to check
  242.      * @param message the exception message to use if the assertion fails
  243.      * @throws IllegalArgumentException if the collection is <code>null</code> or has no elements
  244.      */
  245.     public static void notEmpty(Collection collection, String message) {
  246.         if (CollectionUtils.isEmpty(collection)) {
  247.             throw new IllegalArgumentException(message);
  248.         }
  249.     }
  250.     /**
  251.      * Assert that a collection has elements; that is, it must not be
  252.      * <code>null</code> and must have at least one element.
  253.      * <pre class="code">Assert.notEmpty(collection, "Collection must have elements");</pre>
  254.      * @param collection the collection to check
  255.      * @throws IllegalArgumentException if the collection is <code>null</code> or has no elements
  256.      */
  257.     public static void notEmpty(Collection collection) {
  258.         notEmpty(collection,
  259.                 "[Assertion failed] - this collection must not be empty: it must contain at least 1 element");
  260.     }
  261.     /**
  262.      * Assert that a Map has entries; that is, it must not be <code>null</code>
  263.      * and must have at least one entry.
  264.      * <pre class="code">Assert.notEmpty(map, "Map must have entries");</pre>
  265.      * @param map the map to check
  266.      * @param message the exception message to use if the assertion fails
  267.      * @throws IllegalArgumentException if the map is <code>null</code> or has no entries
  268.      */
  269.     public static void notEmpty(Map map, String message) {
  270.         if (CollectionUtils.isEmpty(map)) {
  271.             throw new IllegalArgumentException(message);
  272.         }
  273.     }
  274.     /**
  275.      * Assert that a Map has entries; that is, it must not be <code>null</code>
  276.      * and must have at least one entry.
  277.      * <pre class="code">Assert.notEmpty(map);</pre>
  278.      * @param map the map to check
  279.      * @throws IllegalArgumentException if the map is <code>null</code> or has no entries
  280.      */
  281.     public static void notEmpty(Map map) {
  282.         notEmpty(map, "[Assertion failed] - this map must not be empty; it must contain at least one entry");
  283.     }
  284.     /**
  285.      * Assert that the provided object is an instance of the provided class.
  286.      * <pre class="code">Assert.instanceOf(Foo.class, foo);</pre>
  287.      * @param clazz the required class
  288.      * @param obj the object to check
  289.      * @throws IllegalArgumentException if the object is not an instance of clazz
  290.      * @see Class#isInstance
  291.      */
  292.     public static void isInstanceOf(Class clazz, Object obj) {
  293.         isInstanceOf(clazz, obj, "");
  294.     }
  295.     /**
  296.      * Assert that the provided object is an instance of the provided class.
  297.      * <pre class="code">Assert.instanceOf(Foo.class, foo);</pre>
  298.      * @param type the type to check against
  299.      * @param obj the object to check
  300.      * @param message a message which will be prepended to the message produced by
  301.      * the function itself, and which may be used to provide context. It should
  302.      * normally end in a ": " or ". " so that the function generate message looks
  303.      * ok when prepended to it.
  304.      * @throws IllegalArgumentException if the object is not an instance of clazz
  305.      * @see Class#isInstance
  306.      */
  307.     public static void isInstanceOf(Class type, Object obj, String message) {
  308.         notNull(type, "Type to check against must not be null");
  309.         if (!type.isInstance(obj)) {
  310.             throw new IllegalArgumentException(message +
  311.                     "Object of class [" + (obj != null ? obj.getClass().getName() : "null") +
  312.                     "] must be an instance of " + type);
  313.         }
  314.     }
  315.     /**
  316.      * Assert that <code>superType.isAssignableFrom(subType)</code> is <code>true</code>.
  317.      * <pre class="code">Assert.isAssignable(Number.class, myClass);</pre>
  318.      * @param superType the super type to check
  319.      * @param subType the sub type to check
  320.      * @throws IllegalArgumentException if the classes are not assignable
  321.      */
  322.     public static void isAssignable(Class superType, Class subType) {
  323.         isAssignable(superType, subType, "");
  324.     }
  325.     /**
  326.      * Assert that <code>superType.isAssignableFrom(subType)</code> is <code>true</code>.
  327.      * <pre class="code">Assert.isAssignable(Number.class, myClass);</pre>
  328.      * @param superType the super type to check against
  329.      * @param subType the sub type to check
  330.      * @param message a message which will be prepended to the message produced by
  331.      * the function itself, and which may be used to provide context. It should
  332.      * normally end in a ": " or ". " so that the function generate message looks
  333.      * ok when prepended to it.
  334.      * @throws IllegalArgumentException if the classes are not assignable
  335.      */
  336.     public static void isAssignable(Class superType, Class subType, String message) {
  337.         notNull(superType, "Type to check against must not be null");
  338.         if (subType == null || !superType.isAssignableFrom(subType)) {
  339.             throw new IllegalArgumentException(message + subType + " is not assignable to " + superType);
  340.         }
  341.     }
  342.     /**
  343.      * Assert a boolean expression, throwing <code>IllegalStateException</code>
  344.      * if the test result is <code>false</code>. Call isTrue if you wish to
  345.      * throw IllegalArgumentException on an assertion failure.
  346.      * <pre class="code">Assert.state(id == null, "The id property must not already be initialized");</pre>
  347.      * @param expression a boolean expression
  348.      * @param message the exception message to use if the assertion fails
  349.      * @throws IllegalStateException if expression is <code>false</code>
  350.      */
  351.     public static void state(boolean expression, String message) {
  352.         if (!expression) {
  353.             throw new IllegalStateException(message);
  354.         }
  355.     }
  356.     /**
  357.      * Assert a boolean expression, throwing {@link IllegalStateException}
  358.      * if the test result is <code>false</code>.
  359.      * <p>Call {@link #isTrue(boolean)} if you wish to
  360.      * throw {@link IllegalArgumentException} on an assertion failure.
  361.      * <pre class="code">Assert.state(id == null);</pre>
  362.      * @param expression a boolean expression
  363.      * @throws IllegalStateException if the supplied expression is <code>false</code>
  364.      */
  365.     public static void state(boolean expression) {
  366.         state(expression, "[Assertion failed] - this state invariant must be true");
  367.     }
  368. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值