Utils Package
The org.apache.juneau.commons.utils package provides general-purpose utility classes for common operations including string manipulation, collection operations, class utilities, file operations, and more.
Key Classes
Utils
Common utility methods for working with collections, strings, objects, and other common operations.
import static org.apache.juneau.commons.utils.Utils.*;
// Array and list creation
String[] arr = a("a", "b", "c");
List<String> list = l("a", "b", "c");
// Object equality with null handling
eq("Hello", "Hello"); // true
eq(null, null); // true
eq("Hello", null); // false
// Null-safe operations
nn(obj); // Returns true if obj is not null
n(obj); // Returns true if obj is null
isArray(obj); // Returns true if obj is an array
// Empty/not-empty checks
e(""); // true (empty string)
e(null); // true (null is empty)
e("Hello"); // false
ne("Hello"); // true (not empty)
ne(""); // false
// Hash code generation
int hash = h("Hello", 123, true);
// Object identity
String id = id(obj); // Returns "ClassName@hashcode"
// Memoization
Supplier<String> memoized = mem(() -> expensiveComputation());
ResettableSupplier<String> resettable = memr(() -> expensiveComputation());
// Optional utilities
Optional<String> opt = opt("value");
Optional<String> empty = opte();
StringUtils
Reusable string utility methods for formatting, parsing, and manipulation.
import static org.apache.juneau.commons.utils.StringUtils.*;
// Format strings (supports both MessageFormat and printf styles)
format("Hello %s, you have %d items", "John", 5);
format("Hello {0}, you have {1} items", "John", 5);
// Case-insensitive comparison
equalsIgnoreCase("Hello", "hello"); // true
// String splitting and joining
split("a,b,c", ","); // Returns ["a", "b", "c"]
join(List.of("a", "b", "c"), ","); // Returns "a,b,c"
// Trimming and padding
trim(" hello "); // Returns "hello"
padLeft("42", 5, '0'); // Returns "00042"
padRight("42", 5, '0'); // Returns "42000"
CollectionUtils
Utility methods for working with collections, arrays, and maps.
import static org.apache.juneau.commons.utils.CollectionUtils.*;
// Create collections
List<String> list = listb(String.class)
.add("a", "b", "c")
.build();
Map<String, Integer> map = mapb(String.class, Integer.class)
.add("one", 1)
.add("two", 2)
.build();
Set<String> set = setb(String.class)
.add("a", "b", "c")
.build();
// Array utilities
String[] arr = array(String.class, "a", "b", "c");
List<String> listFromArray = asList(arr);
ClassUtils
Utility methods for working with classes, including type resolution and class loading.
import static org.apache.juneau.commons.utils.ClassUtils.*;
// Get class from name
Class<?> clazz = forName("java.lang.String");
// Check if class exists
boolean exists = classExists("java.lang.String");
// Get all superclasses
List<Class<?>> superclasses = getSuperclasses(String.class);
FileUtils
Utility methods for file operations.
import static org.apache.juneau.commons.utils.FileUtils.*;
// Read file as string
String content = readFile("path/to/file.txt");
// Write string to file
writeFile("path/to/file.txt", "content");
// Check if file exists
boolean exists = fileExists("path/to/file.txt");
// Get file extension
String ext = getExtension("file.txt"); // Returns "txt"
IoUtils
Utility methods for I/O operations.
import static org.apache.juneau.commons.utils.IoUtils.*;
// Read input stream as string
String content = read(InputStream);
// Write string to output stream
write(OutputStream, "content");
// Copy streams
copy(InputStream, OutputStream);
DateUtils
Utility methods for date and time operations.
import static org.apache.juneau.commons.utils.DateUtils.*;
// Format dates
String formatted = formatDate(LocalDateTime.now(), "yyyy-MM-dd");
// Parse dates
LocalDateTime parsed = parseDate("2023-12-25", "yyyy-MM-dd");
// Get current timestamp
long timestamp = getCurrentTimestamp();
AnnotationUtils
Utility methods for working with annotations.
import static org.apache.juneau.commons.utils.AnnotationUtils.*;
// Check if annotation is present
boolean hasAnnotation = hasAnnotation(MyClass.class, MyAnnotation.class);
// Get annotation
MyAnnotation annotation = getAnnotation(MyClass.class, MyAnnotation.class);
// Get all annotations
List<Annotation> annotations = getAnnotations(MyClass.class);
AssertionUtils
Utility methods for argument validation and assertions.
import static org.apache.juneau.commons.utils.AssertionUtils.*;
// Assert non-null
String value = assertArgNotNull("value", obj);
// Assert not empty
String str = assertArgNotEmpty("str", str);
// Assert condition
assertArg("value > 0", value > 0);
ThrowableUtils
Utility methods for working with exceptions and throwables.
import static org.apache.juneau.commons.utils.ThrowableUtils.*;
// Get root cause
Throwable rootCause = getRootCause(throwable);
// Get stack trace as string
String stackTrace = getStackTrace(throwable);
// Check if throwable is of type
boolean isIOException = isInstanceOf(throwable, IOException.class);
Common Patterns
Fluent Collection Building
import static org.apache.juneau.commons.utils.CollectionUtils.*;
List<String> list = listb(String.class)
.add("a", "b", "c")
.addIf(condition, "d")
.sorted()
.unmodifiable()
.build();
String Formatting
import static org.apache.juneau.commons.utils.StringUtils.*;
// Mixed format styles
String result = format("User {0} has %d items", "Alice", 10);
// Returns: "User Alice has 10 items"
Null-Safe Operations
import static org.apache.juneau.commons.utils.Utils.*;
// Safe equality check
if (eq(obj1, obj2)) {
// Objects are equal (handles nulls)
}
// Safe null checks
if (nn(obj)) {
// Object is not null
}
if (n(obj)) {
// Object is null
}
// Empty checks
if (e(collection)) {
// Collection is null or empty
}
if (ne(collection)) {
// Collection is not null and not empty
}
Share feedback or follow-up questions for this page directly through GitHub.