Skip to content

Commit

Permalink
Pulled over conventiosn
Browse files Browse the repository at this point in the history
  • Loading branch information
kpartlow committed Jan 9, 2024
1 parent 65bf73e commit a254352
Showing 1 changed file with 69 additions and 0 deletions.
69 changes: 69 additions & 0 deletions src/main/java/com/cedarsoftware/util/Convention.java
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);
}
}
}

0 comments on commit a254352

Please sign in to comment.