{"id":24919789,"url":"https://github.com/steinfletcher/either","last_synced_at":"2025-03-28T10:12:55.832Z","repository":{"id":57728709,"uuid":"105147194","full_name":"steinfletcher/either","owner":"steinfletcher","description":"A simple data type that represents the result of an operation that might fail","archived":false,"fork":false,"pushed_at":"2017-11-16T21:34:58.000Z","size":66,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-02T10:37:28.450Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/steinfletcher.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":"2017-09-28T12:46:29.000Z","updated_at":"2017-09-29T14:10:49.000Z","dependencies_parsed_at":"2022-09-09T09:31:01.399Z","dependency_job_id":null,"html_url":"https://github.com/steinfletcher/either","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/steinfletcher%2Feither","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/steinfletcher%2Feither/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/steinfletcher%2Feither/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/steinfletcher%2Feither/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/steinfletcher","download_url":"https://codeload.github.com/steinfletcher/either/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246009071,"owners_count":20708881,"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":[],"created_at":"2025-02-02T10:37:30.200Z","updated_at":"2025-03-28T10:12:55.813Z","avatar_url":"https://github.com/steinfletcher.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"# either\n\n[![CircleCI](https://circleci.com/gh/steinfletcher/either/tree/master.svg?style=shield)](https://circleci.com/gh/steinfletcher/either/tree/master)\n[![Lambda](https://img.shields.io/maven-central/v/com.steinf/either.svg)](http://search.maven.org/#search%7Cga%7C1%7Ccom.steinf.either)\n[![codecov](https://codecov.io/gh/steinfletcher/either/branch/master/graph/badge.svg)](https://codecov.io/gh/steinfletcher/either)\n\nA simple algebraic data type for java. An Either is commonly used to represent a result that might fail,\nwhere the left side contains the error result and the right side contains the success result. You can think\nof this as a more powerful alternative to `Optional`. Plays nicely with Java 8 types.\n\n## Artifacts\n\nGradle\n\n    compile 'com.steinf:either:1.0.4'\n\nMaven\n\n    \u003cdependency\u003e\n        \u003cgroupId\u003ecom.steinf\u003c/groupId\u003e\n        \u003cartifactId\u003eeither\u003c/artifactId\u003e\n        \u003cversion\u003e1.0.4\u003c/version\u003e\n    \u003c/dependency\u003e\n\n## Examples\n\nCreate an either\n```Java\nEither\u003cString, Integer\u003e e = Either.right(2);\nEither\u003cString, Integer\u003e e = Either.left(\"FAIL\");\nEither\u003cString, Integer\u003e e = Either.either(() -\u003e \"FAIL\", () -\u003e 2); // right biased\nEither\u003cString, Integer\u003e e = Either.fromOptional(Optional.of(2));\nEither\u003cString, Integer\u003e e = Either.fromOptionalOrElse(Optional.empty(), () -\u003e \"FAIL\");\n```\n\nExtract the `right` or `left` value\n```Java\nEither\u003cString, Integer\u003e e = Either.right(20);\ne.getRight() // == 20\ne.getLeft() // throws NoSuchElementException\n\nEither\u003cString, Integer\u003e e = Either.left(20);\ne.getLeft() // == 20\ne.getRight() // throws NoSuchElementException\n```\n\nCheck whether the either `isLeft` or `isRight`\n```Java\nEither.right(20).isRight() // true\nEither.right(20).isLeft() // false\n```\n\n`map` over an either\n```Java\nEither.right(20)\n      .map(e -\u003e e * 10); // == Either.right(200)\n```\n\nCombine either with `flatMap` or `andThen`\n```Java\nEither.right(\"right\")\n      .flatMap(rightValue -\u003e Either.right(rightValue + \" side\")); // == EIther.right(\"right side\")\n```\n\n`fold` an either into a value\n```Java\nEither.right(\"right\")\n      .fold(f -\u003e \"left\", f -\u003e f + \" side\"); // == \"right side\"\n\nEither.left(\"left\")\n      .fold(f -\u003e f + \" side\", f -\u003e \"right\"); // == \"left side\"\n```\n\n`accept` an either\n```Java\nEither.right(\"right\")\n      .accept(l -\u003e {}, r -\u003e {});\n\nEither.right(\"right\")\n      .accept(r -\u003e {});\n```\n\nExtract the value `orElse` get the provided value if Left\n```Java\nEither.left(FALSE);\n      .orElse(\"right\");  // == \"right\"\n\nEither.right(10);\n      .orElse(99);  // == 10\n```\n\nExtract the value `orElseGet` get the provided value if Left\n```Java\nEither.left(FALSE);\n      .orElseGet(() -\u003e \"right\");  // == \"right\"\n\nEither.right(10);\n      .orElseGet(() -\u003e 99);  // == 10\n```\n\nExtract the value `orElseThrow` if Left\n```Java\nEither.left(FALSE);\n      .orElseThrow(() -\u003e new RuntimeException(\"Errr\"));  // throws RuntimeException\n\nEither.right(100);\n      .orElseThrow(() -\u003e new RuntimeException(\"Errr\"));  // == 100\n```\n\nFilter the right side `values` from a stream\n```Java\nStream\u003cEither\u003cString, String\u003e\u003e eithers = Stream.of(\n    Either.left(\"1\"),\n    Either.right(\"2\"),\n    Either.right(\"3\"));\n\neithers\n    .flatMap(Either.values())\n    .collect(toList()); // == [2, 3]\n```\n\nConvert `toOptional`\n```Java\nEither.left(FALSE)\n      .toOptional(); // == Optional.empty()\n\nEither.right(10);\n      .toOptional(); // == Optional.of(10)\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsteinfletcher%2Feither","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsteinfletcher%2Feither","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsteinfletcher%2Feither/lists"}