Java Assert assertStringNotEmpty(String string, String fieldName)

Here you can find the source of assertStringNotEmpty(String string, String fieldName)

Description

Asserts that the string is not empty.

License

Amazon Software License

Parameter

Parameter Description
string the string.
fieldName the field name.

Return

the string.

Declaration

public static String assertStringNotEmpty(String string, String fieldName) 

Method Source Code

//package com.java2s;
//License from project: Amazon Software License 

public class Main {
    /**/*from ww w.  j a v  a2  s.  c  o  m*/
     * Asserts that the string is not empty.
     * @param string the string.
     * @param fieldName the field name.
     * @return the string.
     */
    public static String assertStringNotEmpty(String string, String fieldName) {
        assertNotNull(string, fieldName);
        if (string.isEmpty()) {
            throw new IllegalArgumentException(String.format("%s cannot be empty", fieldName));
        }
        return string;
    }

    /**
     * Asserts that the given object is non-null and returns it.
     *
     * @param object Object to assert on
     * @param fieldName Field name to display in exception message if null
     * @param <T> the type of object.
     * @return Object if non null
     */
    public static <T> T assertNotNull(T object, String fieldName) {
        if (object == null) {
            throw new IllegalArgumentException(String.format("%s cannot be null", fieldName));
        }
        return object;
    }
}

Related

  1. assertSquare(double[]... d)
  2. assertState(boolean expression)
  3. assertStringMatch(String first, String second)
  4. assertStringNotEmpty(String str, String name)
  5. assertStringNotEmpty(String str, String name)
  6. assertTesting()
  7. assertThrows(Class exception, Runnable action)
  8. assertTwoParameters(Class clz, String method, double... args)
  9. assertUnreachable()