001 /*
002 * Copyright 2002 - 2007 JEuclid, http://jeuclid.sf.net
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017 /* $Id: Defense.java 310 2007-05-18 20:26:36Z maxberger $ */
018
019 package net.sourceforge.jeuclid;
020
021 import java.io.File;
022
023 /**
024 * Internal class for defensive programming.
025 * <p>
026 * Even though these methods are declared as public, there are not guaranteed
027 * to be stable or work outside JEuclid.
028 * <p>
029 * http://en.wikipedia.org/wiki/Defensive_programming
030 *
031 * @author putrycze
032 * @version $Revision: 310 $
033 *
034 */
035 public final class Defense {
036
037 /** Default constructor. */
038 private Defense() {
039 // Empty on purpose
040 }
041
042 /**
043 * Makes sure a parameter is not null.
044 *
045 * @param o
046 * parameter
047 * @param name
048 * name of the parameter
049 */
050 public static void notNull(final Object o, final String name) {
051 if (o != null) {
052 return;
053 }
054 throw new NullPointerException("'" + name + "' cannot be null");
055 }
056
057 /**
058 * Makes sure a file exists.
059 *
060 * @param file
061 * the file
062 */
063 public static void fileExists(final File file) {
064 if (!file.exists()) {
065 throw new AssertionError(file.toString() + " doesn't exist");
066 }
067
068 }
069
070 /**
071 * Makes sure a condition is true.
072 *
073 * @param b
074 * condition
075 * @param string
076 * value
077 */
078 public static void assertTrue(final boolean b, final String string) {
079 if (!b) {
080 throw new AssertionError("Condition failed:" + string);
081 }
082 }
083
084 }