{"action":"created","comment":{"url":"https://api.github.com/repos/SpongePowered/SpongeDocs/pulls/comments/48444269","id":48444269,"diff_hunk":"@@ -0,0 +1,195 @@\n+===================\n+Optionals Explained\n+===================\n+\n+Much of the Sponge API makes use of Java's `Optional` system on object accessors, but if you've never used ``Optional``\n+before this might seem like a bit of a peculiar way of doing things. You might be tempted to ask:\n+*\"why do I need to perform an extra step when fetching something from an API object?\"*\n+\n+This section gives a brief summary of ``Optional`` and explains how - and perhaps more importantly why -\n+it's used throughout the Sponge API.\n+\n+Let's start with a little history, and look at how accessors - particularly \"getters\" - typically work when not making\n+use of ``Optional``.\n+\n+1. Implicit Nullable Contracts and Why They Suck\n+================================================\n+\n+Let's say we have a simple API object ``Entity`` with a ``getFoo()`` method which returns the Entity's ``Foo``.\n+\n+.. image:: /images/optionals1.png\n+\n+In the olden times of yore, our plugin might fetch and use the ``Foo`` from the entity using the ``getter`` like this:\n+\n+.. code-block:: java\n+\n+    public void someEventHandler(Entity someEntity) {\n+        Foo entityFoo = someEntity.getFoo();\n+        entityFoo.bar();\n+    }\n+\n+The problem arises because - when designing the API - we have to rely an *implicit* contract on the ``getFoo`` method\n+with respect to whether the method can (or cannot) return ``null``. This *implicit contract* can be defined in one of\n+two ways:\n+\n+* **In the javadoc** - this is bad because it relies on the plugin author reading the javadoc for the method, and the contract may not be clear to the plugin author\n+* **Using nullable annotations** - this is not ideal because in general these annotations require a tool to be of any use, for example relying on the IDE or compiler to handle the annotations.\n+\n+.. image:: /images/optionals2.png\n+\n+Let's assume that the ``getFoo()`` method can - as part of its contract - return null. This suddenly means that our\n+code above is unsafe as it may result in a ``NullPointerException`` if ``entityFoo`` is null.\n+\n+.. code-block:: java\n+\n+    public void someEventHandler(Entity someEntity) {\n+        Foo entityFoo = someEntity.getFoo();\n+        entityFoo.bar();\n+    }\n+\n+Let's assume our plugin author is savvy to the nullable nature of our ``getFoo`` method and decides to fix the problem\n+with null checking. Assuming he has defined a local constant ``Foo``, he resultant code looks like this:\n+\n+\n+.. code-block:: java\n+\n+    public void someEventHandler(Entity someEntity) {\n+        Foo entityFoo = someEntity.getFoo();\n+        if (entityFoo == null) {\n+            entityFoo = MyPlugin.DEFAULT_FOO;\n+        }\n+        entityFoo.bar();\n+    }\n+\n+In this example, the plugin author is aware that the method can return null and has a constant available with a\n+default instance of ``Foo`` which can be used instead. Of course the plugin could just short-circuit the call entirely,\n+or it could attempt to fetch `Foo` from somewhere else. The key take-away is that handling nulls even in simple cases","path":"source/plugin/optional/basic.rst","position":66,"original_position":66,"commit_id":"a34f56dfa63a9f09b39caacbdb2a9b375c8dd10c","original_commit_id":"a34f56dfa63a9f09b39caacbdb2a9b375c8dd10c","user":{"login":"Inscrutable","id":1009532,"avatar_url":"https://avatars.githubusercontent.com/u/1009532?v=3","gravatar_id":"","url":"https://api.github.com/users/Inscrutable","html_url":"https://github.com/Inscrutable","followers_url":"https://api.github.com/users/Inscrutable/followers","following_url":"https://api.github.com/users/Inscrutable/following{/other_user}","gists_url":"https://api.github.com/users/Inscrutable/gists{/gist_id}","starred_url":"https://api.github.com/users/Inscrutable/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Inscrutable/subscriptions","organizations_url":"https://api.github.com/users/Inscrutable/orgs","repos_url":"https://api.github.com/users/Inscrutable/repos","events_url":"https://api.github.com/users/Inscrutable/events{/privacy}","received_events_url":"https://api.github.com/users/Inscrutable/received_events","type":"User","site_admin":false},"body":"take-away -> message. Some colloquial language is OK, but not this far imho.","created_at":"2015-12-26T02:08:33Z","updated_at":"2015-12-26T02:08:33Z","html_url":"https://github.com/SpongePowered/SpongeDocs/pull/359#discussion_r48444269","pull_request_url":"https://api.github.com/repos/SpongePowered/SpongeDocs/pulls/359","_links":{"self":{"href":"https://api.github.com/repos/SpongePowered/SpongeDocs/pulls/comments/48444269"},"html":{"href":"https://github.com/SpongePowered/SpongeDocs/pull/359#discussion_r48444269"},"pull_request":{"href":"https://api.github.com/repos/SpongePowered/SpongeDocs/pulls/359"}}},"pull_request":{"url":"https://api.github.com/repos/SpongePowered/SpongeDocs/pulls/359","id":49458687,"html_url":"https://github.com/SpongePowered/SpongeDocs/pull/359","diff_url":"https://github.com/SpongePowered/SpongeDocs/pull/359.diff","patch_url":"https://github.com/SpongePowered/SpongeDocs/pull/359.patch","issue_url":"https://api.github.com/repos/SpongePowered/SpongeDocs/issues/359","number":359,"state":"open","locked":false,"title":"Add Optionals documentation","user":{"login":"Tzky","id":10940670,"avatar_url":"https://avatars.githubusercontent.com/u/10940670?v=3","gravatar_id":"","url":"https://api.github.com/users/Tzky","html_url":"https://github.com/Tzky","followers_url":"https://api.github.com/users/Tzky/followers","following_url":"https://api.github.com/users/Tzky/following{/other_user}","gists_url":"https://api.github.com/users/Tzky/gists{/gist_id}","starred_url":"https://api.github.com/users/Tzky/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Tzky/subscriptions","organizations_url":"https://api.github.com/users/Tzky/orgs","repos_url":"https://api.github.com/users/Tzky/repos","events_url":"https://api.github.com/users/Tzky/events{/privacy}","received_events_url":"https://api.github.com/users/Tzky/received_events","type":"User","site_admin":false},"body":"Adds @Mumfrey s beautiful Optionals explanation, which he provided in #355.\r\n\r\nIssues to fix:\r\n- [x] comments in code snippets aren't translatable, maybe move them elsewhere\r\n- [x] images aren't translatable, needs a better solution","created_at":"2015-11-02T17:13:31Z","updated_at":"2015-12-26T02:08:33Z","closed_at":null,"merged_at":null,"merge_commit_sha":"cadaab230e05dea27e44f03d8bf55f4e0bcad0a6","assignee":null,"milestone":{"url":"https://api.github.com/repos/SpongePowered/SpongeDocs/milestones/2","html_url":"https://github.com/SpongePowered/SpongeDocs/milestones/Essential%20BETA%20features","labels_url":"https://api.github.com/repos/SpongePowered/SpongeDocs/milestones/2/labels","id":909730,"number":2,"title":"Essential BETA features","description":"The Docs should move towards completion so that plugin developers can easily use Sponge to make stuff. Hopefully we can get this done more or less synchronous with SpongeForge/SpongeVanilla BETA release.","creator":{"login":"Inscrutable","id":1009532,"avatar_url":"https://avatars.githubusercontent.com/u/1009532?v=3","gravatar_id":"","url":"https://api.github.com/users/Inscrutable","html_url":"https://github.com/Inscrutable","followers_url":"https://api.github.com/users/Inscrutable/followers","following_url":"https://api.github.com/users/Inscrutable/following{/other_user}","gists_url":"https://api.github.com/users/Inscrutable/gists{/gist_id}","starred_url":"https://api.github.com/users/Inscrutable/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Inscrutable/subscriptions","organizations_url":"https://api.github.com/users/Inscrutable/orgs","repos_url":"https://api.github.com/users/Inscrutable/repos","events_url":"https://api.github.com/users/Inscrutable/events{/privacy}","received_events_url":"https://api.github.com/users/Inscrutable/received_events","type":"User","site_admin":false},"open_issues":12,"closed_issues":29,"state":"open","created_at":"2014-12-19T06:46:15Z","updated_at":"2015-12-22T23:42:03Z","due_on":"2015-12-30T13:00:00Z","closed_at":null},"commits_url":"https://api.github.com/repos/SpongePowered/SpongeDocs/pulls/359/commits","review_comments_url":"https://api.github.com/repos/SpongePowered/SpongeDocs/pulls/359/comments","review_comment_url":"https://api.github.com/repos/SpongePowered/SpongeDocs/pulls/comments{/number}","comments_url":"https://api.github.com/repos/SpongePowered/SpongeDocs/issues/359/comments","statuses_url":"https://api.github.com/repos/SpongePowered/SpongeDocs/statuses/a34f56dfa63a9f09b39caacbdb2a9b375c8dd10c","head":{"label":"SpongePowered:feature/optional","ref":"feature/optional","sha":"a34f56dfa63a9f09b39caacbdb2a9b375c8dd10c","user":{"login":"SpongePowered","id":8683473,"avatar_url":"https://avatars.githubusercontent.com/u/8683473?v=3","gravatar_id":"","url":"https://api.github.com/users/SpongePowered","html_url":"https://github.com/SpongePowered","followers_url":"https://api.github.com/users/SpongePowered/followers","following_url":"https://api.github.com/users/SpongePowered/following{/other_user}","gists_url":"https://api.github.com/users/SpongePowered/gists{/gist_id}","starred_url":"https://api.github.com/users/SpongePowered/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/SpongePowered/subscriptions","organizations_url":"https://api.github.com/users/SpongePowered/orgs","repos_url":"https://api.github.com/users/SpongePowered/repos","events_url":"https://api.github.com/users/SpongePowered/events{/privacy}","received_events_url":"https://api.github.com/users/SpongePowered/received_events","type":"Organization","site_admin":false},"repo":{"id":27024702,"name":"SpongeDocs","full_name":"SpongePowered/SpongeDocs","owner":{"login":"SpongePowered","id":8683473,"avatar_url":"https://avatars.githubusercontent.com/u/8683473?v=3","gravatar_id":"","url":"https://api.github.com/users/SpongePowered","html_url":"https://github.com/SpongePowered","followers_url":"https://api.github.com/users/SpongePowered/followers","following_url":"https://api.github.com/users/SpongePowered/following{/other_user}","gists_url":"https://api.github.com/users/SpongePowered/gists{/gist_id}","starred_url":"https://api.github.com/users/SpongePowered/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/SpongePowered/subscriptions","organizations_url":"https://api.github.com/users/SpongePowered/orgs","repos_url":"https://api.github.com/users/SpongePowered/repos","events_url":"https://api.github.com/users/SpongePowered/events{/privacy}","received_events_url":"https://api.github.com/users/SpongePowered/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/SpongePowered/SpongeDocs","description":"Documentation for Sponge and its Implementations","fork":false,"url":"https://api.github.com/repos/SpongePowered/SpongeDocs","forks_url":"https://api.github.com/repos/SpongePowered/SpongeDocs/forks","keys_url":"https://api.github.com/repos/SpongePowered/SpongeDocs/keys{/key_id}","collaborators_url":"https://api.github.com/repos/SpongePowered/SpongeDocs/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/SpongePowered/SpongeDocs/teams","hooks_url":"https://api.github.com/repos/SpongePowered/SpongeDocs/hooks","issue_events_url":"https://api.github.com/repos/SpongePowered/SpongeDocs/issues/events{/number}","events_url":"https://api.github.com/repos/SpongePowered/SpongeDocs/events","assignees_url":"https://api.github.com/repos/SpongePowered/SpongeDocs/assignees{/user}","branches_url":"https://api.github.com/repos/SpongePowered/SpongeDocs/branches{/branch}","tags_url":"https://api.github.com/repos/SpongePowered/SpongeDocs/tags","blobs_url":"https://api.github.com/repos/SpongePowered/SpongeDocs/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/SpongePowered/SpongeDocs/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/SpongePowered/SpongeDocs/git/refs{/sha}","trees_url":"https://api.github.com/repos/SpongePowered/SpongeDocs/git/trees{/sha}","statuses_url":"https://api.github.com/repos/SpongePowered/SpongeDocs/statuses/{sha}","languages_url":"https://api.github.com/repos/SpongePowered/SpongeDocs/languages","stargazers_url":"https://api.github.com/repos/SpongePowered/SpongeDocs/stargazers","contributors_url":"https://api.github.com/repos/SpongePowered/SpongeDocs/contributors","subscribers_url":"https://api.github.com/repos/SpongePowered/SpongeDocs/subscribers","subscription_url":"https://api.github.com/repos/SpongePowered/SpongeDocs/subscription","commits_url":"https://api.github.com/repos/SpongePowered/SpongeDocs/commits{/sha}","git_commits_url":"https://api.github.com/repos/SpongePowered/SpongeDocs/git/commits{/sha}","comments_url":"https://api.github.com/repos/SpongePowered/SpongeDocs/comments{/number}","issue_comment_url":"https://api.github.com/repos/SpongePowered/SpongeDocs/issues/comments{/number}","contents_url":"https://api.github.com/repos/SpongePowered/SpongeDocs/contents/{+path}","compare_url":"https://api.github.com/repos/SpongePowered/SpongeDocs/compare/{base}...{head}","merges_url":"https://api.github.com/repos/SpongePowered/SpongeDocs/merges","archive_url":"https://api.github.com/repos/SpongePowered/SpongeDocs/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/SpongePowered/SpongeDocs/downloads","issues_url":"https://api.github.com/repos/SpongePowered/SpongeDocs/issues{/number}","pulls_url":"https://api.github.com/repos/SpongePowered/SpongeDocs/pulls{/number}","milestones_url":"https://api.github.com/repos/SpongePowered/SpongeDocs/milestones{/number}","notifications_url":"https://api.github.com/repos/SpongePowered/SpongeDocs/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/SpongePowered/SpongeDocs/labels{/name}","releases_url":"https://api.github.com/repos/SpongePowered/SpongeDocs/releases{/id}","created_at":"2014-11-23T06:56:04Z","updated_at":"2015-12-24T20:37:25Z","pushed_at":"2015-12-25T23:35:15Z","git_url":"git://github.com/SpongePowered/SpongeDocs.git","ssh_url":"git@github.com:SpongePowered/SpongeDocs.git","clone_url":"https://github.com/SpongePowered/SpongeDocs.git","svn_url":"https://github.com/SpongePowered/SpongeDocs","homepage":"","size":5686,"stargazers_count":49,"watchers_count":49,"language":"Python","has_issues":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":58,"mirror_url":null,"open_issues_count":54,"forks":58,"open_issues":54,"watchers":49,"default_branch":"master"}},"base":{"label":"SpongePowered:master","ref":"master","sha":"4736305fa46988693e943a56b060bf503d05b482","user":{"login":"SpongePowered","id":8683473,"avatar_url":"https://avatars.githubusercontent.com/u/8683473?v=3","gravatar_id":"","url":"https://api.github.com/users/SpongePowered","html_url":"https://github.com/SpongePowered","followers_url":"https://api.github.com/users/SpongePowered/followers","following_url":"https://api.github.com/users/SpongePowered/following{/other_user}","gists_url":"https://api.github.com/users/SpongePowered/gists{/gist_id}","starred_url":"https://api.github.com/users/SpongePowered/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/SpongePowered/subscriptions","organizations_url":"https://api.github.com/users/SpongePowered/orgs","repos_url":"https://api.github.com/users/SpongePowered/repos","events_url":"https://api.github.com/users/SpongePowered/events{/privacy}","received_events_url":"https://api.github.com/users/SpongePowered/received_events","type":"Organization","site_admin":false},"repo":{"id":27024702,"name":"SpongeDocs","full_name":"SpongePowered/SpongeDocs","owner":{"login":"SpongePowered","id":8683473,"avatar_url":"https://avatars.githubusercontent.com/u/8683473?v=3","gravatar_id":"","url":"https://api.github.com/users/SpongePowered","html_url":"https://github.com/SpongePowered","followers_url":"https://api.github.com/users/SpongePowered/followers","following_url":"https://api.github.com/users/SpongePowered/following{/other_user}","gists_url":"https://api.github.com/users/SpongePowered/gists{/gist_id}","starred_url":"https://api.github.com/users/SpongePowered/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/SpongePowered/subscriptions","organizations_url":"https://api.github.com/users/SpongePowered/orgs","repos_url":"https://api.github.com/users/SpongePowered/repos","events_url":"https://api.github.com/users/SpongePowered/events{/privacy}","received_events_url":"https://api.github.com/users/SpongePowered/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/SpongePowered/SpongeDocs","description":"Documentation for Sponge and its Implementations","fork":false,"url":"https://api.github.com/repos/SpongePowered/SpongeDocs","forks_url":"https://api.github.com/repos/SpongePowered/SpongeDocs/forks","keys_url":"https://api.github.com/repos/SpongePowered/SpongeDocs/keys{/key_id}","collaborators_url":"https://api.github.com/repos/SpongePowered/SpongeDocs/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/SpongePowered/SpongeDocs/teams","hooks_url":"https://api.github.com/repos/SpongePowered/SpongeDocs/hooks","issue_events_url":"https://api.github.com/repos/SpongePowered/SpongeDocs/issues/events{/number}","events_url":"https://api.github.com/repos/SpongePowered/SpongeDocs/events","assignees_url":"https://api.github.com/repos/SpongePowered/SpongeDocs/assignees{/user}","branches_url":"https://api.github.com/repos/SpongePowered/SpongeDocs/branches{/branch}","tags_url":"https://api.github.com/repos/SpongePowered/SpongeDocs/tags","blobs_url":"https://api.github.com/repos/SpongePowered/SpongeDocs/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/SpongePowered/SpongeDocs/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/SpongePowered/SpongeDocs/git/refs{/sha}","trees_url":"https://api.github.com/repos/SpongePowered/SpongeDocs/git/trees{/sha}","statuses_url":"https://api.github.com/repos/SpongePowered/SpongeDocs/statuses/{sha}","languages_url":"https://api.github.com/repos/SpongePowered/SpongeDocs/languages","stargazers_url":"https://api.github.com/repos/SpongePowered/SpongeDocs/stargazers","contributors_url":"https://api.github.com/repos/SpongePowered/SpongeDocs/contributors","subscribers_url":"https://api.github.com/repos/SpongePowered/SpongeDocs/subscribers","subscription_url":"https://api.github.com/repos/SpongePowered/SpongeDocs/subscription","commits_url":"https://api.github.com/repos/SpongePowered/SpongeDocs/commits{/sha}","git_commits_url":"https://api.github.com/repos/SpongePowered/SpongeDocs/git/commits{/sha}","comments_url":"https://api.github.com/repos/SpongePowered/SpongeDocs/comments{/number}","issue_comment_url":"https://api.github.com/repos/SpongePowered/SpongeDocs/issues/comments{/number}","contents_url":"https://api.github.com/repos/SpongePowered/SpongeDocs/contents/{+path}","compare_url":"https://api.github.com/repos/SpongePowered/SpongeDocs/compare/{base}...{head}","merges_url":"https://api.github.com/repos/SpongePowered/SpongeDocs/merges","archive_url":"https://api.github.com/repos/SpongePowered/SpongeDocs/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/SpongePowered/SpongeDocs/downloads","issues_url":"https://api.github.com/repos/SpongePowered/SpongeDocs/issues{/number}","pulls_url":"https://api.github.com/repos/SpongePowered/SpongeDocs/pulls{/number}","milestones_url":"https://api.github.com/repos/SpongePowered/SpongeDocs/milestones{/number}","notifications_url":"https://api.github.com/repos/SpongePowered/SpongeDocs/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/SpongePowered/SpongeDocs/labels{/name}","releases_url":"https://api.github.com/repos/SpongePowered/SpongeDocs/releases{/id}","created_at":"2014-11-23T06:56:04Z","updated_at":"2015-12-24T20:37:25Z","pushed_at":"2015-12-25T23:35:15Z","git_url":"git://github.com/SpongePowered/SpongeDocs.git","ssh_url":"git@github.com:SpongePowered/SpongeDocs.git","clone_url":"https://github.com/SpongePowered/SpongeDocs.git","svn_url":"https://github.com/SpongePowered/SpongeDocs","homepage":"","size":5686,"stargazers_count":49,"watchers_count":49,"language":"Python","has_issues":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":58,"mirror_url":null,"open_issues_count":54,"forks":58,"open_issues":54,"watchers":49,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/SpongePowered/SpongeDocs/pulls/359"},"html":{"href":"https://github.com/SpongePowered/SpongeDocs/pull/359"},"issue":{"href":"https://api.github.com/repos/SpongePowered/SpongeDocs/issues/359"},"comments":{"href":"https://api.github.com/repos/SpongePowered/SpongeDocs/issues/359/comments"},"review_comments":{"href":"https://api.github.com/repos/SpongePowered/SpongeDocs/pulls/359/comments"},"review_comment":{"href":"https://api.github.com/repos/SpongePowered/SpongeDocs/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/SpongePowered/SpongeDocs/pulls/359/commits"},"statuses":{"href":"https://api.github.com/repos/SpongePowered/SpongeDocs/statuses/a34f56dfa63a9f09b39caacbdb2a9b375c8dd10c"}}}}