Utility classes for common Java stuff.
You can use java-extensions
via maven:
<dependency>
<groupId>com.terheyden</groupId>
<artifactId>java-extensions</artifactId>
<version>0.2.5</version>
</dependency>
TODO: Add JAR releases here for non-maven folks.
Short for "value functions." Better methods than Optional<>
to deal with nulls.
TODO - write more about this.
isEmpty(var); // Better empty checking with support for Strings, Collections, and Maps.
notEmpty(var); // Better not-empty checking. More readable than !isEmpty();
anyEmpty(v1, v2); //
allEmpty(v1, v2); //
orIfEmpty(var, def); // Return [var] if not empty, else return [def].
throwIfEmpty(var, errMsg); // Throw [errMsg] if [var] is empty.
safe(myVar); // For avoiding nulls - returns [myVar] or a non-null empty value.
equals(v1, v2);
notEquals(v1, v2);
Val also has a comprehensive set of empty values.
emptyString();
emptyInt();
...
emptyCollection();
emptySet();
emptyList();
emptyObject();
emptyFile();
emptyPath();
...
// If they hit no just wait for another input.
if (!"YES".equals(butt.clickedButtonAction)) {
return EventResponse.ok(cbs.slack.finishInteractiveMsg(butt, Color.Grey, "Okay, sorry about that."));
}
Represents the result of a method call, or a work item.
Can be used instead of nulls or Optional<>
.
// Examples:
// BEFORE:
User u = loadUser(100);
if (u == null) {
throw new Exception("User could not be found.");
}
// AFTER:
Result<User> res = loadUser(100);
return res.getValueOrThrow("User could not be found.");
// Or just:
return loadUser(100).getValueOrThrow("User could not be found.");
// BEFORE:
User u = loadUser(100);
if (u != null) {
users.add(u);
}
// AFTER:
// Supports streams - more readable than Optional<>:
loadUser(100)
.ifHasValue().forEach(users::add);
Regular expression management, simplified. Builds normal regular expressions, and also offers some optional niceties.
// This example is a nicer way to create the regex: "I\s+have\s+a\s+pet\s+(?:dog|cat)".
RegexBuilder.regex("I have a pet dog|cat")
.simpleSpaces()
.simpleOrs()
.buildPattern();
// Supports transitive variables!
// Reads so much nicer than: "Email:\s*(?<email>[A-Za-z0-9._-]+@[A-Za-z0-9._-]+)"
RegexBuilder.regex("Email: {email}")
.simpleSpaces()
.var("{word}", "[A-Za-z0-9._-]+")
.var("{email}", "{word}@{word}", "email")
.buildPattern();
// That third argument, "email", up above is a group name.
// It allows us to later do:
String email = matcher.group("email"); // Instead of matcher.group(1);
Utils for methods and functional programming. Easier exception handling, especially for Java 8 lambdas. TODO - write about functional programming and checked exceptions in Java.
// This example builds a Supplier<> that wraps checked exception methods,
// ignores thrown exceptions, and instead returns a default value.
// Fantastic for lambdas.
Func.supplier(suppFunc(y))
.ignoreExceptions()
.defaultValue(z)
.get();