001/** 002 * 003 * Copyright 2015-2019 Florian Schmaus 004 * 005 * Licensed under the Apache License, Version 2.0 (the "License"); 006 * you may not use this file except in compliance with the License. 007 * You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.jivesoftware.smack.util; 018 019import java.util.Collection; 020 021public class Objects { 022 023 /** 024 * Checks that the specified object reference is not <code>null</code> and throws a customized 025 * {@link IllegalArgumentException} if it is. 026 * <p> 027 * Note that unlike <code>java.util.Objects</code>, this method throws an {@link IllegalArgumentException} instead 028 * of an {@link NullPointerException}. 029 * </p> 030 * 031 * @param <T> the type of the reference. 032 * @param obj the object reference to check for nullity. 033 * @param message detail message to be used in the event that a {@link IllegalArgumentException} is thrown. 034 * @return <code>obj</code> if not null. 035 * @throws IllegalArgumentException in case <code>obj</code> is <code>null</code>. 036 */ 037 public static <T> T requireNonNull(T obj, String message) throws IllegalArgumentException { 038 if (obj == null) { 039 if (message == null) { 040 message = "Can not provide null argument"; 041 } 042 throw new IllegalArgumentException(message); 043 } 044 return obj; 045 } 046 047 public static <T> T requireNonNull(T obj) { 048 return requireNonNull(obj, null); 049 } 050 051 /** 052 * Require a collection to be neither null, nor empty. 053 * 054 * @param collection collection 055 * @param message error message 056 * @param <T> Collection type 057 * @return collection TODO javadoc me please 058 */ 059 public static <T extends Collection<?>> T requireNonNullNorEmpty(T collection, String message) { 060 if (requireNonNull(collection).isEmpty()) { 061 throw new IllegalArgumentException(message); 062 } 063 return collection; 064 } 065 066 public static boolean equals(Object a, Object b) { 067 return a == b || (a != null && a.equals(b)); 068 } 069}