{"id":28523641,"url":"https://github.com/deplant/commons-result","last_synced_at":"2025-06-30T23:39:39.222Z","repository":{"id":296004008,"uuid":"991984988","full_name":"deplant/commons-result","owner":"deplant","description":"Implementation of Result pattern for modern Java inspired by Rust std result crate. Perfectly suited for pattern matching.","archived":false,"fork":false,"pushed_at":"2025-05-28T15:11:19.000Z","size":79,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-06-09T10:09:49.115Z","etag":null,"topics":["pattern-matching","result-pattern"],"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/deplant.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2025-05-28T12:51:16.000Z","updated_at":"2025-05-28T15:11:23.000Z","dependencies_parsed_at":"2025-05-28T14:37:46.938Z","dependency_job_id":"2089c448-587e-4ec1-bbd2-fdb744948b78","html_url":"https://github.com/deplant/commons-result","commit_stats":null,"previous_names":["deplant/commons-result"],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/deplant/commons-result","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/deplant%2Fcommons-result","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/deplant%2Fcommons-result/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/deplant%2Fcommons-result/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/deplant%2Fcommons-result/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/deplant","download_url":"https://codeload.github.com/deplant/commons-result/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/deplant%2Fcommons-result/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":262869376,"owners_count":23377279,"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":["pattern-matching","result-pattern"],"created_at":"2025-06-09T10:09:18.422Z","updated_at":"2025-06-30T23:39:39.213Z","avatar_url":"https://github.com/deplant.png","language":"Java","readme":"# Deplant Commons Result API\n\n[![JDK version](https://img.shields.io/badge/Java-21-green.svg)](https://shields.io/)\n[![License](https://img.shields.io/badge/License-Apache%202.0-brown.svg)](https://shields.io/)\n\n* Discuss in\n  Telegram: [![Channel on Telegram](https://img.shields.io/badge/chat-on%20telegram-9cf.svg)](https://t.me/deplant\\_chat\\_en)\n* Read full\n  docs: [![javadoc](https://javadoc.io/badge2/tech.deplant.commons/commons-core/javadoc.svg)](https://javadoc.io/doc/tech.deplant.commons/result)\n\n**Deplant Commons Result API** implements **Result Pattern** for Java. It is a small \nJava library with a wrapper class named `Result` with sealed implementations `Ok` \nand `Err`. \nIdea comes from Rust [std.Result crate](https://doc.rust-lang.org/std/result/) with \nsome changes in API to better suite Java needs.\n\nMade in functional style, it perfectly matches with **Pattern matching** techniques \nor more traditional conditional statements to manage results. Supports chaining results, \nmost of the functions are lazy and don't do unnecessary work.\n\nLibrary is pure Java and has no dependencies aside from test ones.\n\n## Writing function that can throw\n```java\npublic static Result\u003cInteger\u003e functionCanFail(Integer i) {\n    return Result.of(() -\u003e 2 / i);\n}\n```\n\n## Pattern matching result variants\n\nAs `Result` is a **sealed** interface, you can **pattern match** it without `default` branch.\n\n```java\nvar result = Result.of(() -\u003e 8 / 0);\n\nswitch (result) {\n    case Ok(Integer i) when i \u003e 2 -\u003e System.out.println(\"Good!\");\n    case Ok ok -\u003e System.out.println(\"Ok: \" + ok.result());\n    case Err err -\u003e System.out.println(\"Err: \" + err.error().getMessage());\n}\n// prints \"Err: / by zero\"\n```\n\n## Mapping and unwrapping results\n\nResults can be chained with other results through `.result.mapResult( functionThatCanFail )`,\nwrapped value and type can be transformed though `.map( functionThatShouldntFail )`. \nIf you like to transpose `Result\u003c\u003e` to `Optional\u003c\u003e` you can do it with `Result.ofOptional()` \n, `.ok()` and `.err()` methods.\n\n```java\nResult\u003cInteger\u003e goodResult = Result.of(() -\u003e 7);\nResult\u003cInteger\u003e multipliedResult = goodResult.map(i -\u003e i * 2); // transformations\nResult\u003cString\u003e stringResult = goodResult.map(Object::toString); // type transformations\nResult\u003cInteger\u003e combinedResult = goodResult.mapResult(s -\u003e functionCanFail(s)); // transformations with other results\nInteger digit = combinedResult.orElse(2); // get or default when fail\nOptional\u003cInteger\u003e optionalDigit = combinedResult.ok(); // get as optional (empty when fail)\n```\n\n## Requirements\n\n* OpenJDK 21 (or more)\n*\n## Maven or Gradle setup:\n\n* Gradle\n\n```groovy\ndependencies {\n    implementation 'tech.deplant.commons:result:0.1.0'\n}\n```\n\n* Maven\n\n```xml\n\u003cdependency\u003e\n    \u003cgroupId\u003etech.deplant.commons\u003c/groupId\u003e\n    \u003cartifactId\u003eresult\u003c/artifactId\u003e\n    \u003cversion\u003e0.1.0\u003c/version\u003e\n\u003c/dependency\u003e\n```","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdeplant%2Fcommons-result","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdeplant%2Fcommons-result","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdeplant%2Fcommons-result/lists"}