/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /* * This is a modified version of the original Apache class. It has had unused * members removed. */ package org.ehcache.impl.internal.classes.commonslang; /** * <p>This class assists in validating arguments. The validation methods are * based along the following principles: * <ul> * <li>An invalid {@code null} argument causes a {@link NullPointerException}.</li> * <li>A non-{@code null} argument causes an {@link IllegalArgumentException}.</li> * <li>An invalid index into an array/collection/map/string causes an {@link IndexOutOfBoundsException}.</li> * </ul> * * <p>All exceptions messages are * <a href="http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Formatter.html#syntax">format strings</a> * as defined by the Java platform. For example:</li> * * <pre> * Validate.isTrue(i > 0, "The value must be greater than zero: %d", i); * Validate.notNull(surname, "The surname must not be %s", null); * </pre> * * <p>#ThreadSafe#</li> * @see java.lang.String#format(String, Object...) * @since 2.0 */ public class Validate { /** * Constructor. This class should not normally be instantiated. */ public Validate() { super(); } /** * <p>Validate that the specified argument is not {@code null}; * otherwise throwing an exception with the specified message. * * <pre>Validate.notNull(myObject, "The object must not be null");</pre> * * @param <T> the object type * @param object the object to check * @param message the {@link String#format(String, Object...)} exception message if invalid, not null * @param values the optional values for the formatted exception message * @return the validated object (never {@code null} for method chaining) * @throws NullPointerException if the object is {@code null} * @see #notNull(Object) */ public static <T> T notNull(final T object, final String message, final Object... values) { if (object == null) { throw new NullPointerException(String.format(message, values)); } return object; } }