forked from jdereg/java-util
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
69 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package com.cedarsoftware.util; | ||
|
||
import java.util.Map; | ||
|
||
public class Convention { | ||
|
||
/** | ||
* statically accessed class | ||
*/ | ||
private Convention() { | ||
} | ||
|
||
/** | ||
* Throws an exception if null | ||
* | ||
* @param value object to check if null | ||
* @param message message to use when thrown | ||
* @throws IllegalArgumentException if the string passed in is null or empty | ||
*/ | ||
public static void throwIfNull(Object value, String message) { | ||
if (value == null) { | ||
throw new IllegalArgumentException(message); | ||
} | ||
} | ||
|
||
/** | ||
* Throws an exception if null or empty | ||
* | ||
* @param value string to check | ||
* @param message message to use when thrown | ||
* @throws IllegalArgumentException if the string passed in is null or empty | ||
*/ | ||
public static void throwIfNullOrEmpty(String value, String message) { | ||
if (value == null || value.isEmpty()) { | ||
throw new IllegalArgumentException(message); | ||
} | ||
} | ||
|
||
public static void throwIfClassNotFound(String fullyQualifiedClassName, ClassLoader loader) { | ||
throwIfNullOrEmpty(fullyQualifiedClassName, "fully qualified ClassName cannot be null or empty"); | ||
throwIfNull(loader, "loader cannot be null"); | ||
|
||
Class<?> c = ClassUtilities.forName(fullyQualifiedClassName, loader); | ||
if (c == null) { | ||
throw new IllegalArgumentException("Unknown class: " + fullyQualifiedClassName + " was not found."); | ||
} | ||
} | ||
|
||
public static <K, V> void throwIfKeyExists(Map<K, V> map, K key, String message) { | ||
throwIfNull(map, "map cannot be null"); | ||
throwIfNull(key, "key cannot be null"); | ||
|
||
if (map.containsKey(key)) { | ||
throw new IllegalArgumentException(message); | ||
} | ||
} | ||
|
||
/** | ||
* Throws an exception if the logic is false. | ||
* | ||
* @param logic test to see if we need to throw the exception. | ||
* @param message to include in the exception explaining why the the assertion failed | ||
*/ | ||
public static void throwIfFalse(boolean logic, String message) { | ||
if (!logic) { | ||
throw new IllegalArgumentException(message); | ||
} | ||
} | ||
} |