001/* =========================================================== 002 * Orson Charts : a 3D chart library for the Java(tm) platform 003 * =========================================================== 004 * 005 * (C)opyright 2013-2016, by Object Refinery Limited. All rights reserved. 006 * 007 * http://www.object-refinery.com/orsoncharts/index.html 008 * 009 * This program is free software: you can redistribute it and/or modify 010 * it under the terms of the GNU General Public License as published by 011 * the Free Software Foundation, either version 3 of the License, or 012 * (at your option) any later version. 013 * 014 * This program is distributed in the hope that it will be useful, 015 * but WITHOUT ANY WARRANTY; without even the implied warranty of 016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 017 * GNU General Public License for more details. 018 * 019 * You should have received a copy of the GNU General Public License 020 * along with this program. If not, see <http://www.gnu.org/licenses/>. 021 * 022 * [Oracle and Java are registered trademarks of Oracle and/or its affiliates. 023 * Other names may be trademarks of their respective owners.] 024 * 025 * If you do not wish to be bound by the terms of the GPL, an alternative 026 * commercial license can be purchased. For details, please see visit the 027 * Orson Charts home page: 028 * 029 * http://www.object-refinery.com/orsoncharts/index.html 030 * 031 */ 032 033package com.orsoncharts.util; 034 035/** 036 * Utility methods for argument checking. Throughout Orson Charts, arguments 037 * passed to methods are validated and exceptions thrown for invalid cases 038 * (the idea is to fail fast, which usually helps when tracking down errors 039 * in programming logic). 040 */ 041public final class ArgChecks { 042 043 private ArgChecks() { 044 // no need to instantiate this ever 045 } 046 047 /** 048 * Checks if the specified argument is {@code null} and, if it is, 049 * throws an {@code IllegalArgumentException}. 050 * 051 * @param arg the argument to check ({@code null} permitted). 052 * @param name the parameter name ({@code null} not permitted). 053 */ 054 public static void nullNotPermitted(Object arg, String name) { 055 if (arg == null) { 056 throw new IllegalArgumentException("Null '" + name + "' argument."); 057 } 058 } 059 060 /** 061 * Checks if the specified argument is negative and, if it is, throws an 062 * {@code IllegalArgumentException}. 063 * 064 * @param value the value. 065 * @param name the parameter name ({@code null} not permitted). 066 */ 067 public static void negativeNotPermitted(double value, String name) { 068 if (value < 0.0) { 069 throw new IllegalArgumentException("Param '" + name 070 + "' cannot be negative"); 071 } 072 } 073 074 /** 075 * Checks if the specified argument is positive and, if it is NOT, throws an 076 * {@code IllegalArgumentException}. 077 * 078 * @param value the value. 079 * @param name the parameter name ({@code null} not permitted). 080 */ 081 public static void positiveRequired(double value, String name) { 082 if (value <= 0.0) { 083 throw new IllegalArgumentException("Param '" + name 084 + "' must be positive."); 085 } 086 } 087 088 /** 089 * Checks if the specified argument is finite and, if it is NOT, throws an 090 * {@code IllegalArgumentException}. 091 * 092 * @param value the value. 093 * @param name the parameter name ({@code null} not permitted). 094 * 095 * @since 1.4 096 */ 097 public static void finiteRequired(double value, String name) { 098 if (Double.isInfinite(value)) { 099 throw new IllegalArgumentException("Param '" + name 100 + "' must be finite."); 101 } 102 } 103 104 /** 105 * Checks if the specified argument is finite and positive and, 106 * if it is NOT, throws an {@code IllegalArgumentException}. 107 * 108 * @param value the value. 109 * @param name the parameter name ({@code null} not permitted). 110 * 111 * @since 1.4 112 */ 113 public static void finitePositiveRequired(double value, String name) { 114 if (value <= 0.0 || Double.isInfinite(value)) { 115 throw new IllegalArgumentException("Param '" + name 116 + "' must be finite and positive."); 117 } 118 } 119 120 /** 121 * Checks that the index is less than the specified {@code arrayLimit} 122 * and throws an {@code IllegalArgumentException} if it is not. 123 * 124 * @param index the array index. 125 * @param name the parameter name (to display in the error message). 126 * @param arrayLimit the array size. 127 */ 128 public static void checkArrayBounds(int index, String name, 129 int arrayLimit) { 130 if (index >= arrayLimit) { 131 throw new IllegalArgumentException("Requires '" + name 132 + "' in the range 0 to " + (arrayLimit - 1)); 133 } 134 } 135 136}