{"id":15043889,"url":"https://github.com/rkonovalov/comparator","last_synced_at":"2026-03-16T19:38:08.270Z","repository":{"id":45924679,"uuid":"184595593","full_name":"rkonovalov/comparator","owner":"rkonovalov","description":"Comparator simplifies if...else branching","archived":false,"fork":false,"pushed_at":"2021-11-29T06:22:56.000Z","size":62,"stargazers_count":0,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-01-20T13:23:27.088Z","etag":null,"topics":["java-8","java-lambda","java-util","java8"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/rkonovalov.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-05-02T14:27:19.000Z","updated_at":"2021-11-29T06:22:59.000Z","dependencies_parsed_at":"2022-08-29T23:01:58.569Z","dependency_job_id":null,"html_url":"https://github.com/rkonovalov/comparator","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rkonovalov%2Fcomparator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rkonovalov%2Fcomparator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rkonovalov%2Fcomparator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rkonovalov%2Fcomparator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rkonovalov","download_url":"https://codeload.github.com/rkonovalov/comparator/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243451605,"owners_count":20293168,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["java-8","java-lambda","java-util","java8"],"created_at":"2024-09-24T20:49:46.466Z","updated_at":"2025-12-25T19:56:28.522Z","avatar_url":"https://github.com/rkonovalov.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003cdiv align=\"center\"\u003e\n  \u003ca href=\"https://rkonovalov.github.io/projects/comparator/1.0.0/\"\u003e\n    \u003cimg src=\"https://rkonovalov.github.io/assets/images/comparator-logo.svg\" alt=\"Comparator Main page\"\u003e\n  \u003c/a\u003e\n  \u003cbr\u003e\n\u003c/div\u003e\n\n[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)\n[![Build Status](https://travis-ci.org/rkonovalov/comparator.svg?branch=master)](https://travis-ci.org/rkonovalov/comparator)\n[![Maven Central](https://maven-badges.herokuapp.com/maven-central/com.github.rkonovalov/comparator/badge.svg?style=blue)](https://search.maven.org/search?q=a:comparator)\n[![Javadocs](https://www.javadoc.io/badge/com.github.rkonovalov/comparator.svg)](https://www.javadoc.io/doc/com.github.rkonovalov/comparator)\n[![codecov](https://codecov.io/gh/rkonovalov/comparator/branch/master/graph/badge.svg)](https://codecov.io/gh/rkonovalov/comparator)\n[![Codacy Badge](https://api.codacy.com/project/badge/Grade/ea8708461ffb49108013aa0f5ec09ede)](https://www.codacy.com/app/rkonovalov/comparator?utm_source=github.com\u0026amp;utm_medium=referral\u0026amp;utm_content=rkonovalov/comparator\u0026amp;utm_campaign=Badge_Grade)\n[![SonarCloud](https://sonarcloud.io/api/project_badges/measure?project=comparator\u0026metric=alert_status)](https://sonarcloud.io/dashboard?id=comparator)\n[![Java CI with Maven](https://github.com/rkonovalov/comparator/actions/workflows/maven.yml/badge.svg)](https://github.com/rkonovalov/comparator/actions/workflows/maven.yml)\n\n# About Comparator\nMost of all languages has conditional statements. And Java is not exception.\nCondition statements help you to test one or more values to correctness.\n\nLook at the next example:\n```java\n    if(condition) {\n      //block of code to be executed if the condition is true\n    }\n```\n\nIf condition is true then we will execute code in braces. And if condition is false we can execute another code, like in example:\n```java\n    if(condition) {\n      //block of code to be executed if the condition is true\n    } else {\n      //block of another code to be executed if the condition is false\n    }\n```\n\nIf we need to compare multiple conditions we can use **else if** statement:\n```java\n    if(strValue.equals(\"result_1\")) {\n      //block of code to be executed if strValue equals result_1\n    } else if(strValue.equals(\"result_2\")) {\n           //block of code to be executed if strValue equals result_2\n    } else if(strValue.equals(\"result_3\")) {\n           //block of code to be executed if strValue equals result_3\n    }\n    ...\n```\nAs you can see with increasing of complexity of comparing the code becomes less readable.\n\nOf course you can use **switch** statement like in next example:\n```java\n    switch(intValue) {\n        case 1: \n            //block of code to be executed if intValue equals 1\n            break;\n        case 2: \n            //block of code to be executed if intValue equals 2\n            break;\n        case 3: \n            //block of code to be executed if intValue equals 3\n            break;\n    }\n    ...\n```\nBut **switch** statement has restrictions. It accepts only types: **char, byte, short, int, Character, Byte, Short, Integer, String or an enum**.\nIf we try to compare some complex types we forced to use \"if...else\" statements\n\nComparator can to solve this type of issues. Let's look closer how it works.\n\n## Installation\nFor using Comparator you need to import dependency\n\n```xml\n\u003cdependency\u003e\n    \u003cgroupId\u003eio.github.rkonovalov\u003c/groupId\u003e\n    \u003cartifactId\u003ecomparator\u003c/artifactId\u003e\n    \u003cversion\u003e1.0.3\u003c/version\u003e\n\u003c/dependency\u003e\n```\nIf you are using another build automation tool, you can find configuration string by this URL:\nhttps://search.maven.org/artifact/com.github.rkonovalov/comparator/1.0.0/jar\n\n## Examples o usage\nComparator uses Java 8 lambda expressions and Optional container. And if you ever had experience with using it, it will be useful.\n\n## Simple comparing\nIf you need just test object for equality you can use next code\n```java\n    String strValue = \"test\";\n    String result = Comparator.of(strValue)\n        .compare(\"test\", \"Found result 1\")\n        .compare(\"test2\", \"Found result 2\")\n        .get();\n    \n    //result value will be equal to \"Found result 1\" string\n```\n\n## Else equivalent\nIn case of Comparator didn't find equality of object value, it can return default value like in next example:\n\n```java\n    String strValue = null;\n    String result = Comparator.of(strValue)\n        .compare(\"test\", \"Found result 1\")\n        .compare(\"test2\", \"Found result 2\")\n        .orElse(\"Not found\");\n    \n    //result value will be equal to \"Not found\" string\n```\n\n## Complex comparing\nHow about testing more than one object value? \nComparator allows you using Java 8 predicates and resultExpression functions.\n\n```java\n   String strValue = \"test\";\n   String result = Comparator.of(strValue)\n       .compare((s -\u003e s.length() == 4), (s -\u003e \"Length of string 4 char\"))\n       .compare((s -\u003e s.startsWith(\"st\")), (s -\u003e \"Found st prefix in string\"))\n       .get();\n   \n   //result value will be equal to \"Length of string 4 char\" string\n```\n## Comparing with strong typing\nIf you need to control return values in mapping functions you can use next example\n```java\n   String strValue = \"test\";\n   String result = Comparator.of(strValue, String.class)\n       .compare((s -\u003e s.length() == 4), (s -\u003e \"Length of string 4 char\"))\n       .compare((s -\u003e s.startsWith(\"st\")), (s -\u003e \"Found st prefix in string\"))\n       .get();\n   \n   //result value will be equal to \"Length of string 4 char\" string\n```\nCompiler will check all return values in match method. All return values should be instantiated from String class\nNext code will not be compiled by compiler\n```java\n   String strValue = \"test\";\n   String result = Comparator.of(strValue, String.class)\n       .compare((s -\u003e s.length() == 4), (s -\u003e \"Length of string 4 char\"))\n       .compare((s -\u003e s.startsWith(\"st\")), (s -\u003e \"Found st prefix in string\".length()))\n       .get();\n   \n   //result value will be equal to \"Length of string 4 char\" string\n```\nBecause next code will return value type of int, not of String\n```java\n    ...\n    .compare((s -\u003e s.startsWith(\"st\")), (s -\u003e \"Found st prefix in string\".length()))\n    ...\n```\n# Release notes\n\n## Version 1.0.1\n    * Refactored methods\n    \n## Version 1.0.0\n    * Initial release\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frkonovalov%2Fcomparator","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frkonovalov%2Fcomparator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frkonovalov%2Fcomparator/lists"}