Java Assert Not Null assertStringNotNullOrEmpty_ShorterThan256( String str, String name)

Here you can find the source of assertStringNotNullOrEmpty_ShorterThan256( String str, String name)

Description

Check if the string is null or empty, and assert it is no longer than 255.

License

Open Source License

Parameter

Parameter Description
str the string to check.
name the string's name.

Exception

Parameter Description
IllegalArgumentException if the string is null or empty.

Declaration

public static void assertStringNotNullOrEmpty_ShorterThan256(
        String str, String name) 

Method Source Code

//package com.java2s;

public class Main {
    /** Provide the string max length. */
    private static final int STRING_MAXLEN = 255;

    /**//from www  . j  a va2 s .  c o m
     * <p>
     * Check if the string is null or empty, and assert it is no longer than 255.
     * </p>
     * @param str
     *            the string to check.
     * @param name
     *            the string's name.
     * @throws IllegalArgumentException
     *             if the string is null or empty.
     */
    public static void assertStringNotNullOrEmpty_ShorterThan256(
            String str, String name) {
        assertStringNotNullOrEmpty(str, name);
        if (str.length() > STRING_MAXLEN) {
            throw new IllegalArgumentException(name
                    + " must not be longer than 255");
        }
    }

    /**
     * <p>
     * Check if the string is null or empty.
     * </p>
     * @param str
     *            the string to check.
     * @param name
     *            the string's name.
     * @throws IllegalArgumentException
     *             if the string is null or empty.
     */
    public static void assertStringNotNullOrEmpty(String str, String name) {
        assertObjectNotNull(str, name);
        if (str.trim().length() == 0) {
            throw new IllegalArgumentException(name + " must not be empty.");
        }
    }

    /**
     * <p>
     * Check if the given object is null.
     * </p>
     * @param obj
     *            the object to check.
     * @param name
     *            the object's name
     * @throws IllegalArgumentException
     *             if the object is null.
     */
    public static void assertObjectNotNull(Object obj, String name) {
        if (obj == null) {
            throw new IllegalArgumentException(name + " must not be null.");
        }
    }
}

Related

  1. assertPatternNotNull(String methodName, String pattern)
  2. assertResourceNotNull(String resourcePath, Object resourceHandle)
  3. assertStringNotNullAndNotTrimmedEmpty( String variableName, String value)
  4. assertStringNotNullNorEmpty(String str, String name)
  5. assertStringNotNullOrEmpty(String str, String name)