001// *************************************************************************************************************************** 002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file * 003// * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file * 004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance * 005// * with the License. You may obtain a copy of the License at * 006// * * 007// * http://www.apache.org/licenses/LICENSE-2.0 * 008// * * 009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an * 010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the * 011// * specific language governing permissions and limitations under the License. * 012// *************************************************************************************************************************** 013package org.apache.juneau.internal; 014 015import java.text.*; 016 017import org.apache.juneau.*; 018 019/** 020 * Various utility methods for creating and working with throwables. 021 */ 022public class ThrowableUtils { 023 024 /** 025 * Throws an {@link IllegalArgumentException} if the specified object is <jk>null</jk>. 026 * 027 * @param o The object to check. 028 * @param msg The message of the IllegalArgumentException. 029 * @param args Optional {@link MessageFormat}-style arguments. 030 * @throws IllegalArgumentException Constructed exception. 031 */ 032 public static void assertNotNull(Object o, String msg, Object...args) throws IllegalArgumentException { 033 if (o == null) 034 throw new FormattedIllegalArgumentException(msg, args); 035 } 036 037 /** 038 * Throws an {@link IllegalArgumentException} if the specified field is <jk>null</jk>. 039 * 040 * @param fieldValue The object to check. 041 * @param fieldName The name of the field. 042 * @throws IllegalArgumentException Constructed exception. 043 */ 044 public static void assertFieldNotNull(Object fieldValue, String fieldName) throws IllegalArgumentException { 045 if (fieldValue == null) 046 throw new IllegalArgumentException("Field '"+fieldName+"' cannot be null."); 047 } 048 049 /** 050 * Throws an {@link IllegalArgumentException} if the specified field is <c><=0</c>. 051 * 052 * @param fieldValue The object to check. 053 * @param fieldName The name of the field. 054 * @throws IllegalArgumentException Constructed exception. 055 */ 056 public static void assertFieldPositive(int fieldValue, String fieldName) throws IllegalArgumentException { 057 if (fieldValue <= 0) 058 throw new IllegalArgumentException("Field '"+fieldName+"' must be a positive integer."); 059 } 060 061 /** 062 * Shortcut for calling <code><jk>new</jk> IllegalArgumentException(MessageFormat.<jsm>format</jsm>(msg, args));</code> 063 * 064 * @param msg The message of the IllegalArgumentException. 065 * @param args Optional {@link MessageFormat}-style arguments. 066 * @throws IllegalArgumentException Constructed exception. 067 */ 068 public static void illegalArg(String msg, Object...args) throws IllegalArgumentException { 069 throw new FormattedIllegalArgumentException(msg, args); 070 } 071 072 /** 073 * Throws an exception if the specified thread ID is not the same as the current thread. 074 * 075 * @param threadId The ID of the thread to compare against. 076 * @param msg The message of the IllegalStateException. 077 * @param args Optional {@link MessageFormat}-style arguments. 078 * @throws IllegalStateException Constructed exception. 079 */ 080 public static void assertSameThread(long threadId, String msg, Object...args) throws IllegalStateException { 081 if (Thread.currentThread().getId() != threadId) 082 throw new FormattedIllegalArgumentException(msg, args); 083 } 084}