{"id":22515357,"url":"https://github.com/scijava/scijava-optional","last_synced_at":"2025-03-28T02:19:27.048Z","repository":{"id":142923354,"uuid":"256493173","full_name":"scijava/scijava-optional","owner":"scijava","description":"Helpers for emulating named and default arguments","archived":false,"fork":false,"pushed_at":"2022-06-14T20:54:08.000Z","size":54,"stargazers_count":0,"open_issues_count":2,"forks_count":1,"subscribers_count":6,"default_branch":"master","last_synced_at":"2025-02-02T03:41:20.728Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-2-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/scijava.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","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}},"created_at":"2020-04-17T12:12:08.000Z","updated_at":"2021-11-08T21:42:21.000Z","dependencies_parsed_at":"2023-04-09T01:34:57.369Z","dependency_job_id":null,"html_url":"https://github.com/scijava/scijava-optional","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/scijava%2Fscijava-optional","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/scijava%2Fscijava-optional/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/scijava%2Fscijava-optional/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/scijava%2Fscijava-optional/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/scijava","download_url":"https://codeload.github.com/scijava/scijava-optional/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245955328,"owners_count":20699891,"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":"2024-12-07T03:30:09.419Z","updated_at":"2025-03-28T02:19:26.416Z","avatar_url":"https://github.com/scijava.png","language":"Java","readme":"[![](https://github.com/scijava/scijava-optional/actions/workflows/build-main.yml/badge.svg)](https://github.com/scijava/scijava-optional/actions/workflows/build-main.yml)\n\n# scijava-optional\n\nHelpers for emulating named and default arguments in builder-like classes.\n\nThe scenario is that certain methods take optional parameters, subsets of which can overlap.\nFor example, `FactoryA` should take `FactoryAOptions` and\n`FactoryB` should take `FactoryBOptions`, where both `FactoryAOptions` and `FactoryBOptions`\nexpose an optional parameter \"`int a`\" with the same meaning and default values.\n\nTo maintain convenience and type-safety, both `FactoryAOptions` and `FactoryBOptions` should expose\na method `a(int)` to set the optional parameter. But `FactoryAOptions::a` should return a `FactoryAOptions`,\nand `FactoryBOptions::a` should return a `FactoryBOptions` to allow chaining more parameters of\n`FactoryAOptions` and `FactoryBOptions` respectively, while retaining the type of the builder.\n\nUsing scijava-optional, this can be achieved as follows (see [full example](src/test/java/org/scijava/optional/examples/Playground.java)):\n\nEach subset of optional parameters (\"`int a`\" in the above example) is implemented as two interfaces,\none exposing methods to set the parameters, one exposing methods to retrieve parameter values.\n\nFor setting:\n```java\ninterface OptionA\u003cT\u003e extends Options\u003cT\u003e {\n    default T a(int a) {\n        return setValue(\"a\", a);\n    }\n}\n```\nwhere the `a()` method records the parameter value (with key `\"a\"`) via the\n`setValue()` method of the `Options` super-interface.\n\nFor getting:\n```java\ninterface ValueA extends Values {\n    ...\n    default int a() {\n        return getValueOrDefault( \"a\", 0 );\n    }\n}\n```\nwhere the `a()` method returns the parameter value (with key `\"a\"` and default value `0`)\nvia the `getValueOrDefault()` method of the `Values` super-interface.\n\nFinally, the implementation of `FactoryAOptions` derives from `AbstractOptions` and all desired\nsubsets of options\n```java\npublic class FactoryAOptions\n         extends AbstractOptions\u003c FactoryAOptions \u003e\n         implements OptionA\u003c FactoryAOptions \u003e, ...\n{\n    public class FactoryAValues\n            extends AbstractValues\n            implements ValueA, ...\n    {}\n\n    public final FactoryAValues values = new FactoryAValues();\n\n    // =======================================================================\n \n    // If in-place modification of the options builder is desired,\n    // the following methods should be left out. \n\n    public FactoryAOptions() {}\n\n    private FactoryAOptions(FactoryAOptions that) {\n        super(that);\n    }\n\n    @Override\n    protected FactoryAOptions copyOrThis() {\n        return new FactoryAOptions(this);\n    }\n}\n```\nThe parameter values are exposed through inner class `FactoryAValues` that derives from `AbstractValues`\nand all desired subsets of option values.\n\nThe only thing that has been omitted from the above example is the parts that provide a nice `toString` implementation\nfor the values. This is be achieved by overriding the `forEach()` methods in the `Values` interfaces and\nimplementation\n```java\ninterface ValueA extends Values {\n    default void forEach(BiConsumer\u003cString, Object\u003e action) {\n        action.accept(\"a\", a());\n        // and so on, for other parameters defined in this Values interface\n    }\n\n    default int a() {\n        return getValueOrDefault(\"a\", 0);\n    }\n}\n```\nand\n```java\npublic class FactoryAValues\n        extends AbstractValues\n        implements ValueA, ...\n{\n    @Override\n    public void forEach(BiConsumer\u003cString, Object\u003e action) {\n        ValueA.super.forEach( action );\n        // and so on, for other implemented Values interfaces\n    }\n}\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fscijava%2Fscijava-optional","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fscijava%2Fscijava-optional","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fscijava%2Fscijava-optional/lists"}