Java Assert assertValidItemName(String itemName)

Here you can find the source of assertValidItemName(String itemName)

Description

Ensures that the specified name of the item is valid.

License

Open Source License

Parameter

Parameter Description
itemName the name of the item to be checked (could be null or empty)

Exception

Parameter Description
IllegalArgumentException if the name of the item is invalid

Declaration

public static void assertValidItemName(String itemName) throws IllegalArgumentException 

Method Source Code

//package com.java2s;
/**/*from   w  ww.ja v a 2 s  . c  o  m*/
 * Copyright (c) 2014-2017 by the respective copyright holders.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 */

public class Main {
    /**
     * Ensures that the specified name of the item is valid.
     * <p>
     * If the name of the item is invalid an {@link IllegalArgumentException} is thrown, otherwise this method returns
     * silently.
     * <p>
     * A valid item name must <i>only</i> only consists of the following characters:
     * <ul>
     * <li>a-z</li>
     * <li>A-Z</li>
     * <li>0..9</li>
     * <li>_ (underscore)</li>
     * </ul>
     *
     * @param itemName the name of the item to be checked (could be null or empty)
     *
     * @throws IllegalArgumentException if the name of the item is invalid
     */
    public static void assertValidItemName(String itemName) throws IllegalArgumentException {
        if (!isValidItemName(itemName)) {
            throw new IllegalArgumentException("The specified name of the item '" + itemName + "' is not valid!");
        }
    }

    /**
     * Returns {@code true} if the specified name is a valid item name, otherwise {@code false}.
     * <p>
     * A valid item name must <i>only</i> only consists of the following characters:
     * <ul>
     * <li>a-z</li>
     * <li>A-Z</li>
     * <li>0..9</li>
     * <li>_ (underscore)</li>
     * </ul>
     *
     * @param itemName the name of the item to be checked (could be null or empty)
     *
     * @return true if the specified name is a valid item name, otherwise false
     */
    public static boolean isValidItemName(final String itemName) {
        if (itemName != null && !itemName.isEmpty()) {
            return itemName.matches("[a-zA-Z0-9_]*");
        }

        return false;
    }
}

Related

  1. assertTwoParameters(Class clz, String method, double... args)
  2. assertUnreachable()
  3. assertValid(int piece)
  4. assertValidBrowserType(int aBrowserType)
  5. assertValidColumnName(String name)
  6. assertValidKeys(Iterable keys)
  7. assertValidRange(String name, double value, double min, double max)
  8. assertValidSwfValue(String value)
  9. assertValueSize(final int expectedSize, final int got)