java-extensions

Miscellaneous Java utility classes

License

License

Categories

Categories

Java Languages
GroupId

GroupId

com.terheyden
ArtifactId

ArtifactId

java-extensions
Last Version

Last Version

0.2.5
Release Date

Release Date

Type

Type

jar
Description

Description

java-extensions
Miscellaneous Java utility classes
Project URL

Project URL

http://terheyden.com
Source Code Management

Source Code Management

http://github.com/terheyden/java-extensions/tree/master

Download java-extensions

How to add to project

<!-- https://jarcasting.com/artifacts/com.terheyden/java-extensions/ -->
<dependency>
    <groupId>com.terheyden</groupId>
    <artifactId>java-extensions</artifactId>
    <version>0.2.5</version>
</dependency>
// https://jarcasting.com/artifacts/com.terheyden/java-extensions/
implementation 'com.terheyden:java-extensions:0.2.5'
// https://jarcasting.com/artifacts/com.terheyden/java-extensions/
implementation ("com.terheyden:java-extensions:0.2.5")
'com.terheyden:java-extensions:jar:0.2.5'
<dependency org="com.terheyden" name="java-extensions" rev="0.2.5">
  <artifact name="java-extensions" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.terheyden', module='java-extensions', version='0.2.5')
)
libraryDependencies += "com.terheyden" % "java-extensions" % "0.2.5"
[com.terheyden/java-extensions "0.2.5"]

Dependencies

compile (2)

Group / Artifact Type Version
org.jetbrains : annotations jar 15.0
com.google.code.findbugs : jsr305 jar 3.0.2

test (1)

Group / Artifact Type Version
junit : junit jar 4.12

Project Modules

There are no modules declared in this project.

Java Extensions

Utility classes for common Java stuff.

Adding to Maven

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.

What's included

Val

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();
...

Examples

        // 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."));
        }

Result

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);

RegexBuilder

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);

Func

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();

Versions

Version
0.2.5
0.1.8