java-predicates

collection of predefined predicates for all kind of types

License

License

Categories

Categories

Java Languages
GroupId

GroupId

io.github.codejanovic
ArtifactId

ArtifactId

java-predicates
Last Version

Last Version

0.1.0
Release Date

Release Date

Type

Type

jar
Description

Description

java-predicates
collection of predefined predicates for all kind of types
Project URL

Project URL

https://github.com/codejanovic/java-predicates
Source Code Management

Source Code Management

https://github.com/codejanovic/java-predicates.git

Download java-predicates

How to add to project

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

Dependencies

test (3)

Group / Artifact Type Version
junit : junit jar 4.12
org.assertj : assertj-core jar 3.7.0
org.jusecase : builders jar 0.3.0

Project Modules

There are no modules declared in this project.

Build Status Coverage Status Code Climate License

java-predicates

collection of predefined predicates for all basic types

Maven

Release artifact

<dependency>
    <groupId>io.github.codejanovic</groupId>
    <artifactId>java-predicates</artifactId>
    <version>0.1.0</version>
</dependency>

Snapshot artifact

<dependency>
    <groupId>io.github.codejanovic</groupId>
    <artifactId>java-predicates</artifactId>
    <version>0.2.0-SNAPSHOT</version>
</dependency>

Motivation

The motivation behind this project is to achieve a more readable and maintainable way of chaining checks on all kind of basic types (primitives, strings, arrays, etc ) we use in our everyday code, by providing predefined Predicate<T> implementations.

So lets be naive and try to check that a given String conforms to a valid .de and .com URL.

    public void doSomethingWhenUrlIsValid(final String url) {
        if (url != null 
            && !url.isEmpty() 
            && url.startsWith("http://") 
            && (url.endsWith(".de") || url.endsWith(".com"))) {
            doSomethingWithThatUrl();
        } else {
            throw new IllegalArgumentException("invalid url: " + url);
        }
    }

Basically this code has two issues, a major and a minor one. the major one is that whenever the condition on what a valid URL is changes, you have to change the if statement. the minor one is that its not as fluent as it could be for the reader.

Now lets use a separate method and the Predicate<T> interface from Java 8 to fix our major issue ...

    public void doSomethingWhenUrlIsValid(final String url) {
        if (isValidUrl().test(url)) {
            doSomethingWithThatUrl();
        } else {
            throw new IllegalArgumentException("invalid url: " + url);
        }
    }

    public Predicate<String> isValidUrl() {
        return string -> string != null 
            && !string.isEmpty() 
            && string.startsWith("http://") 
            && (string.endsWith(".de") || string.endsWith(".com"));
    }

... and rewrite isValidUrl() method using our fluent Predicates interface ...

    public Predicate<String> isValidUrl() {
        final Predicates checking = new Predicates.Default();
        final StringPredicates it = checking.string();

        return checking.that().valid().when().string().isNotNull()
                .and(it.isNotEmpty())
                .and(it.startsWith("http://"))
                .and(it.endsWith(".de").or(it.endsWith(".com")));
    }

Versions

Version
0.1.0