{"id":1415,"date":"2012-07-02T22:20:39","date_gmt":"2012-07-02T20:20:39","guid":{"rendered":"http:\/\/www.tutego.de\/blog\/javainsel\/?p=1415"},"modified":"2012-07-02T22:20:39","modified_gmt":"2012-07-02T20:20:39","slug":"java-8-bekommt-auch-eine-optional-klasse-wie-guava-und-scala","status":"publish","type":"post","link":"https:\/\/www.tutego.de\/blog\/javainsel\/2012\/07\/java-8-bekommt-auch-eine-optional-klasse-wie-guava-und-scala\/","title":{"rendered":"Java 8 bekommt auch eine Optional-Klasse (wie Guava und Scala)"},"content":{"rendered":"<pre><p>+\/*\n<\/p><p>+ * Copyright (c) 2012, Oracle and\/or its affiliates. All rights reserved.\n<\/p><p>+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.\n<\/p><p>+ *\n<\/p><p>+ * This code is free software; you can redistribute it and\/or modify it\n<\/p><p>+ * under the terms of the GNU General Public License version 2 only, as\n<\/p><p>+ * published by the Free Software Foundation.  Oracle designates this\n<\/p><p>+ * particular file as subject to the &quot;Classpath&quot; exception as provided\n<\/p><p>+ * by Oracle in the LICENSE file that accompanied this code.\n<\/p><p>+ *\n<\/p><p>+ * This code is distributed in the hope that it will be useful, but WITHOUT\n<\/p><p>+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or\n<\/p><p>+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License\n<\/p><p>+ * version 2 for more details (a copy is included in the LICENSE file that\n<\/p><p>+ * accompanied this code).\n<\/p><p>+ *\n<\/p><p>+ * You should have received a copy of the GNU General Public License version\n<\/p><p>+ * 2 along with this work; if not, write to the Free Software Foundation,\n<\/p><p>+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.\n<\/p><p>+ *\n<\/p><p>+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA\n<\/p><p>+ * or visit www.oracle.com if you need additional information or have any\n<\/p><p>+ * questions.\n<\/p><p>+ *\/\n<\/p><p>+package java.util;\n<\/p><p>+\n<\/p><p>+import java.util.functions.Factory;\n<\/p><p>+import java.util.functions.Mapper;\n<\/p><p>+\n<\/p><p>+\/**\n<\/p><p>+ * Optional\n<\/p><p>+ *\n<\/p><p>+ * @author Brian Goetz\n<\/p><p>+ *\/\n<\/p><p>+public class Optional&lt;T&gt; {\n<\/p><p>+    private final static Optional&lt;?&gt; EMPTY = new Optional&lt;&gt;();\n<\/p><p>+\n<\/p><p>+    private final T value;\n<\/p><p>+    private final boolean present;\n<\/p><p>+\n<\/p><p>+    public Optional(T value) {\n<\/p><p>+        this.value = value;\n<\/p><p>+        this.present = true;\n<\/p><p>+    }\n<\/p><p>+\n<\/p><p>+    private Optional() {\n<\/p><p>+        this.value = null;\n<\/p><p>+        this.present = false;\n<\/p><p>+    }\n<\/p><p>+\n<\/p><p>+    public static&lt;T&gt; Optional&lt;T&gt; empty() {\n<\/p><p>+        return (Optional&lt;T&gt;) EMPTY;\n<\/p><p>+    }\n<\/p><p>+\n<\/p><p>+    public T get() {\n<\/p><p>+        if (!present)\n<\/p><p>+            throw new NoSuchElementException();\n<\/p><p>+        return value;\n<\/p><p>+    }\n<\/p><p>+\n<\/p><p>+    public boolean isPresent() {\n<\/p><p>+        return present;\n<\/p><p>+    }\n<\/p><p>+\n<\/p><p>+    public T orElse(T other) {\n<\/p><p>+        return present ? value : other;\n<\/p><p>+    }\n<\/p><p>+\n<\/p><p>+    public T orElse(Factory&lt;T&gt; other) {\n<\/p><p>+        return present ? value : other.make();\n<\/p><p>+    }\n<\/p><p>+\n<\/p><p>+    public&lt;V extends Throwable&gt; T orElseThrow(Factory&lt;V&gt; exceptionFactory) throws V {\n<\/p><p>+        if (present)\n<\/p><p>+            return value;\n<\/p><p>+        else\n<\/p><p>+            throw exceptionFactory.make();\n<\/p><p>+    }\n<\/p><p>+\n<\/p><p>+    public&lt;V extends Throwable&gt; T orElseThrow(Class&lt;V&gt; exceptionClass) throws V {\n<\/p><p>+        if (present)\n<\/p><p>+            return value;\n<\/p><p>+        else\n<\/p><p>+            try {\n<\/p><p>+                throw exceptionClass.newInstance();\n<\/p><p>+            }\n<\/p><p>+            catch (InstantiationException | IllegalAccessException e) {\n<\/p><p>+                throw new IllegalStateException(&quot;Unexpected exception: &quot; + e, e);\n<\/p><p>+            }\n<\/p><p>+    }\n<\/p><p>+\n<\/p><p>+    public&lt;V&gt; Optional&lt;V&gt; map(Mapper&lt;T, V&gt; mapper) {\n<\/p><p>+        return present ? new Optional&lt;&gt;(mapper.map(value)) : Optional.&lt;V&gt;empty();\n<\/p><p>+    }\n<\/p><p>+\n<\/p><p>+    @Override\n<\/p><p>+    public boolean equals(Object o) {\n<\/p><p>+        if (this == o) return true;\n<\/p><p>+        if (o == null || getClass() != o.getClass()) return false;\n<\/p><p>+\n<\/p><p>+        Optional optional = (Optional) o;\n<\/p><p>+\n<\/p><p>+        if (present != optional.present) return false;\n<\/p><p>+        if (value != null ? !value.equals(optional.value) : optional.value != null) return false;\n<\/p><p>+\n<\/p><p>+        return true;\n<\/p><p>+    }\n<\/p><p>+\n<\/p><p>+    @Override\n<\/p><p>+    public int hashCode() {\n<\/p><p>+        int result = value != null ? value.hashCode() : 0;\n<\/p><p>+        result = 31 * result + (present ? 1 : 0);\n<\/p><p>+        return result;\n<\/p><p>+    }\n<\/p><p>+}\n<\/p><\/pre>\n<pre><br \/><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>+\/* + * Copyright (c) 2012, Oracle and\/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and\/or modify it + * under the terms of the GNU General Public License version 2 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","_links_to":"","_links_to_target":""},"categories":[66],"tags":[],"class_list":["post-1415","post","type-post","status-publish","format-standard","hentry","category-java-8"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.tutego.de\/blog\/javainsel\/wp-json\/wp\/v2\/posts\/1415","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.tutego.de\/blog\/javainsel\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tutego.de\/blog\/javainsel\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tutego.de\/blog\/javainsel\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tutego.de\/blog\/javainsel\/wp-json\/wp\/v2\/comments?post=1415"}],"version-history":[{"count":1,"href":"https:\/\/www.tutego.de\/blog\/javainsel\/wp-json\/wp\/v2\/posts\/1415\/revisions"}],"predecessor-version":[{"id":1416,"href":"https:\/\/www.tutego.de\/blog\/javainsel\/wp-json\/wp\/v2\/posts\/1415\/revisions\/1416"}],"wp:attachment":[{"href":"https:\/\/www.tutego.de\/blog\/javainsel\/wp-json\/wp\/v2\/media?parent=1415"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tutego.de\/blog\/javainsel\/wp-json\/wp\/v2\/categories?post=1415"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tutego.de\/blog\/javainsel\/wp-json\/wp\/v2\/tags?post=1415"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}