Skip to content

rkonovalov/comparator

Repository files navigation

License Build Status Maven Central Javadocs codecov Codacy Badge SonarCloud Java CI with Maven

About Comparator

Most of all languages has conditional statements. And Java is not exception. Condition statements help you to test one or more values to correctness.

Look at the next example:

    if(condition) {
      //block of code to be executed if the condition is true
    }

If condition is true then we will execute code in braces. And if condition is false we can execute another code, like in example:

    if(condition) {
      //block of code to be executed if the condition is true
    } else {
      //block of another code to be executed if the condition is false
    }

If we need to compare multiple conditions we can use else if statement:

    if(strValue.equals("result_1")) {
      //block of code to be executed if strValue equals result_1
    } else if(strValue.equals("result_2")) {
           //block of code to be executed if strValue equals result_2
    } else if(strValue.equals("result_3")) {
           //block of code to be executed if strValue equals result_3
    }
    ...

As you can see with increasing of complexity of comparing the code becomes less readable.

Of course you can use switch statement like in next example:

    switch(intValue) {
        case 1: 
            //block of code to be executed if intValue equals 1
            break;
        case 2: 
            //block of code to be executed if intValue equals 2
            break;
        case 3: 
            //block of code to be executed if intValue equals 3
            break;
    }
    ...

But switch statement has restrictions. It accepts only types: char, byte, short, int, Character, Byte, Short, Integer, String or an enum. If we try to compare some complex types we forced to use "if...else" statements

Comparator can to solve this type of issues. Let's look closer how it works.

Installation

For using Comparator you need to import dependency

<dependency>
    <groupId>io.github.rkonovalov</groupId>
    <artifactId>comparator</artifactId>
    <version>1.0.3</version>
</dependency>

If you are using another build automation tool, you can find configuration string by this URL: https://search.maven.org/artifact/com.github.rkonovalov/comparator/1.0.0/jar

Examples o usage

Comparator uses Java 8 lambda expressions and Optional container. And if you ever had experience with using it, it will be useful.

Simple comparing

If you need just test object for equality you can use next code

    String strValue = "test";
    String result = Comparator.of(strValue)
        .compare("test", "Found result 1")
        .compare("test2", "Found result 2")
        .get();
    
    //result value will be equal to "Found result 1" string

Else equivalent

In case of Comparator didn't find equality of object value, it can return default value like in next example:

    String strValue = null;
    String result = Comparator.of(strValue)
        .compare("test", "Found result 1")
        .compare("test2", "Found result 2")
        .orElse("Not found");
    
    //result value will be equal to "Not found" string

Complex comparing

How about testing more than one object value? Comparator allows you using Java 8 predicates and resultExpression functions.

   String strValue = "test";
   String result = Comparator.of(strValue)
       .compare((s -> s.length() == 4), (s -> "Length of string 4 char"))
       .compare((s -> s.startsWith("st")), (s -> "Found st prefix in string"))
       .get();
   
   //result value will be equal to "Length of string 4 char" string

Comparing with strong typing

If you need to control return values in mapping functions you can use next example

   String strValue = "test";
   String result = Comparator.of(strValue, String.class)
       .compare((s -> s.length() == 4), (s -> "Length of string 4 char"))
       .compare((s -> s.startsWith("st")), (s -> "Found st prefix in string"))
       .get();
   
   //result value will be equal to "Length of string 4 char" string

Compiler will check all return values in match method. All return values should be instantiated from String class Next code will not be compiled by compiler

   String strValue = "test";
   String result = Comparator.of(strValue, String.class)
       .compare((s -> s.length() == 4), (s -> "Length of string 4 char"))
       .compare((s -> s.startsWith("st")), (s -> "Found st prefix in string".length()))
       .get();
   
   //result value will be equal to "Length of string 4 char" string

Because next code will return value type of int, not of String

    ...
    .compare((s -> s.startsWith("st")), (s -> "Found st prefix in string".length()))
    ...

Release notes

Version 1.0.1

* Refactored methods

Version 1.0.0

* Initial release

About

Comparator simplifies if...else branching

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages