{"id":1035,"date":"2023-11-04T12:46:54","date_gmt":"2023-11-04T03:46:54","guid":{"rendered":"https:\/\/daba-no-heya.com\/?p=1035"},"modified":"2023-11-04T12:46:55","modified_gmt":"2023-11-04T03:46:55","slug":"post-1035","status":"publish","type":"post","link":"https:\/\/daba-no-heya.com\/?p=1035","title":{"rendered":"Java\u306eOptional\u3092\u4f7f\u3046"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Java 8\u3067\u8ffd\u52a0\u3055\u308c\u305fOptional\u3068\u3044\u3046\u6a5f\u80fd\u304c\u3042\u308a\u307e\u3059\u3002<br>null\u304b\u3082\u3057\u308c\u306a\u3044\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u30e9\u30c3\u30d7\u3057\u3066\u4fbf\u5229\u306b\u6271\u3048\u308b\u3088\u3046\u306b\u3057\u305f\u30af\u30e9\u30b9\u3067\u3059\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u3053\u3053\u306e\u7ba1\u7406\u4eba\u306f\u6065\u305a\u304b\u3057\u306a\u304c\u3089\u3053\u306e\u6a5f\u80fd\u3092\u6700\u8fd1\u307e\u3067\u77e5\u3089\u306a\u304b\u3063\u305f\u306e\u3067\u3001\u52c9\u5f37\u306e\u610f\u5473\u3082\u517c\u306d\u3066\u3053\u3053\u306b\u8a18\u4e8b\u3068\u3057\u3066\u6b8b\u3057\u3066\u304a\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Optional\u3092\u7528\u3044\u305f\u30b5\u30f3\u30d7\u30eb\u30b3\u30fc\u30c9\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u611f\u3058\u3067\u3059\u3002<\/p>\n\n\n\n<pre class=\"wp-block-luxe-blocks-syntaxhighlighter line-numbers language-java\"><code class=\"language-java\">package com.github.maeda6uiui.useoptional;\n\nimport java.util.Optional;\nimport java.util.Random;\n\npublic class Main {\n    private static void func1() {\n        \/\/Optional\u3092\u4f5c\u6210\u3059\u308b(non-null)\n        String s = \"\u3042\u3044\u3046\u3048\u304a\";\n        Optional&lt;String> op = Optional.of(s);\n\n        \/\/Optional\u304b\u3089\u5024\u3092\u53d6\u5f97\u3059\u308b\n        System.out.println(op.get());\n    }\n\n    private static void func2() {\n        \/\/\u7a7a\u306eOptional\u3092\u4f5c\u6210\u3059\u308b\n        Optional&lt;String> op = Optional.empty();\n        System.out.println(op.isEmpty());\n    }\n\n    private static void func3() {\n        \/\/null\u304b\u3082\u3057\u308c\u306a\u3044\u5024\u3092\u3082\u3064Optional\u3092\u4f5c\u6210\u3059\u308b\n        var random = new Random();\n        String s = random.nextInt() % 2 == 0 ? \"\u3042\u3044\u3046\u3048\u304a\" : null;\n        Optional&lt;String> op = Optional.ofNullable(s);\n\n        \/\/\u5024\u304c\u5b58\u5728\u3059\u308b\u5834\u5408\u306f\u51fa\u529b\u3059\u308b\n        op.ifPresent(v -> {\n            System.out.println(v);\n        });\n        \/\/\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u66f8\u304f\u3053\u3068\u3082\u53ef\u80fd\n        op.ifPresent(System.out::println);\n\n        \/\/\u5024\u304c\u5b58\u5728\u3059\u308b\u5834\u5408\u3068\u5b58\u5728\u3057\u306a\u3044\u5834\u5408\u306e\u51e6\u7406\u3092\u4e21\u65b9\u8a18\u8ff0\u3059\u308b\n        op.ifPresentOrElse(\n                System.out::println,    \/\/\u5024\u304c\u5b58\u5728\u3059\u308b\u5834\u5408\n                () -> System.out.println(\"\u7a7a\u306e\u5024\u3067\u3059\")   \/\/\u5024\u304c\u5b58\u5728\u3057\u306a\u3044\u5834\u5408\n        );\n    }\n\n    public static void main(String[] args) {\n        func1();\n        func2();\n        func3();\n    }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Optional\u3092\u4f5c\u6210\u3059\u308b\u305f\u3081\u306b\u306f<code>Optional.of<\/code>\u3001<code>Optional.empty<\/code>\u3001<code>Optional.ofNullable<\/code>\u306e\u3044\u305a\u308c\u304b\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>Optional.of<\/td><td>\u5024\u304c\u5b58\u5728\u3059\u308bOptional\u3092\u4f5c\u6210\u3057\u307e\u3059\u3002<br>\u5f15\u6570\u306b\u6e21\u3059\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306f\u975enull\u3067\u3042\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002<\/td><\/tr><tr><td>Optional.empty<\/td><td>\u5024\u3092\u3082\u305f\u306a\u3044\u7a7a\u306eOptional\u3092\u4f5c\u6210\u3057\u307e\u3059\u3002<\/td><\/tr><tr><td>Optional.ofNullable<\/td><td>null\u304b\u3082\u3057\u308c\u306a\u3044\u5024\u3092\u3082\u3064Optional\u3092\u4f5c\u6210\u3057\u307e\u3059\u3002<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Optional\u306b\u5024\u304c\u5b58\u5728\u3059\u308b\u5834\u5408\u306f\u5024\u3092\u53d6\u5f97\u3057\u3066\u51e6\u7406\u3059\u308b\u3001\u3068\u3044\u3046\u6d41\u308c\u306f\u3001<code>ifPresent<\/code>\u3084<code>ifPresentOrElse<\/code>\u3092\u7528\u3044\u3066\u5b9f\u73fe\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<br>(func3\u3092\u53c2\u7167)<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p class=\"wp-block-paragraph\">\u305f\u3068\u3048\u3070\u3001null\u3092\u8fd4\u3059\u53ef\u80fd\u6027\u306e\u3042\u308b\u30e1\u30bd\u30c3\u30c9\u306e\u623b\u308a\u5024\u3092Optional\u306b\u3057\u3066\u304a\u3051\u3070\u3001\u4ed6\u306e\u958b\u767a\u8005\u306b\u5024\u306e\u6709\u7121\u3092\u610f\u8b58\u3057\u3066\u3082\u3089\u3046\u3053\u3068\u304c\u3067\u304d\u305d\u3046\u3067\u3059\u3002<br>\u307e\u305f\u3001\u610f\u56f3\u3057\u306a\u3044NullPointerException\u3092\u767a\u751f\u3055\u305b\u3066\u3057\u307e\u3046\u53ef\u80fd\u6027\u3082\u6e1b\u3089\u305b\u308b\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u305f\u3060\u6ce8\u610f\u70b9\u3068\u3057\u3066\u3001Optional\u306f\u30e1\u30bd\u30c3\u30c9\u306e\u623b\u308a\u5024\u306b\u5bfe\u3057\u3066\u4f7f\u3046\u3053\u3068\u304c\u60f3\u5b9a\u3055\u308c\u3066\u3044\u308b\u3088\u3046\u3067\u3001\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306e\u30d5\u30a3\u30fc\u30eb\u30c9\u3084\u30e1\u30bd\u30c3\u30c9\u306e\u5f15\u6570\u306b\u5bfe\u3057\u3066\u4f7f\u3046\u3082\u306e\u3067\u306f\u306a\u3044\u3088\u3046\u3067\u3059\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/blogs.oracle.com\/otnjp\/post\/the-java-optional-class-11-more-recipes-for-preventing-null-pointer-exceptions-ja\">https:\/\/blogs.oracle.com\/otnjp\/post\/the-java-optional-class-11-more-recipes-for-preventing-null-pointer-exceptions-ja<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/stackoverflow.com\/questions\/29033518\/is-it-a-good-practice-to-use-optional-as-an-attribute-in-a-class\">https:\/\/stackoverflow.com\/questions\/29033518\/is-it-a-good-practice-to-use-optional-as-an-attribute-in-a-class<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/zenn.dev\/ayumukob\/articles\/552d0c7b544838\">https:\/\/zenn.dev\/ayumukob\/articles\/552d0c7b544838<\/a><\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u3044\u3044\u4f7f\u3044\u65b9\u304b\u306f\u308f\u304b\u308a\u307e\u305b\u3093\u304c\u3001\u7ba1\u7406\u4eba\u304c\u73fe\u5728\u8da3\u5473\u3067\u4f5c\u6210\u3057\u3066\u3044\u308b\u30b3\u30fc\u30c9\u3060\u3068\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u5834\u9762\u3067Optional\u3092\u4f7f\u3063\u3066\u3044\u307e\u3059\u3002<br>CSS\u306e\u6587\u5b57\u5217\u3092\u8fd4\u3059\u30e1\u30bd\u30c3\u30c9\u3067\u3001\u6307\u5b9a\u3055\u308c\u305f\u5024\u304c\u4e0d\u660e\u306a\u3082\u306e\u3060\u3063\u305f\u308a\u30d5\u30a1\u30a4\u30eb\u306e\u8aad\u8fbc\u307f\u306b\u5931\u6557\u3057\u305f\u308a\u3057\u305f\u5834\u5408\u306b\u3001\u7a7a\u306eOptional\u3092\u8fd4\u3059\u3088\u3046\u306b\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n\n\n\n<pre class=\"wp-block-luxe-blocks-syntaxhighlighter line-numbers language-java\"><code class=\"language-java\">\/**\n * Returns CSS string.\n * This method attempts to load custom CSS from file if {@code name} is \"custom\"\n * and a valid string is set to {@code fromFile}.\n * It returns no CSS if {@code name} is \"system\".\n *\n * @return CSS string\n *\/\n@JsonIgnore\npublic Optional&lt;String> getCSS() {\n    if (name != null &amp;&amp; !name.isEmpty()) {\n        if (name.equals(\"system\")) {\n            logger.info(\"'system' is specified as a theme. Return no CSS\");\n            return Optional.empty();\n        }\n        if (name.equals(\"custom\")) {\n            if (fromFile == null || fromFile.isEmpty()) {\n                logger.warn(\n                        \"'custom' is specified as a theme, \" +\n                                \"but fromFile is not a valid string. Return no CSS\");\n                return Optional.empty();\n            }\n\n            return Optional.ofNullable(this.loadCustomCSS());\n        }\n\n        return Optional.ofNullable(this.getBuiltInCSS());\n    }\n\n    logger.warn(\"Theme name is not specified. Return no CSS\");\n    return Optional.empty();\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u547c\u3073\u51fa\u3059\u5074\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u611f\u3058\u3067\u3059\u3002<br>CSS\u6587\u5b57\u5217\u306e\u5024\u304c\u5b58\u5728\u3059\u308b\u5834\u5408\u306e\u307f\u3001JavaFX\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u306e\u30b9\u30bf\u30a4\u30eb\u30b7\u30fc\u30c8\u3092\u8a2d\u5b9a\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<pre class=\"wp-block-luxe-blocks-syntaxhighlighter line-numbers language-java\"><code class=\"language-java\">settings.themeSettings.getCSS().ifPresent(Application::setUserAgentStylesheet);<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Java 8\u3067\u8ffd\u52a0\u3055\u308c\u305fOptional\u3068\u3044\u3046\u6a5f\u80fd\u304c\u3042\u308a\u307e\u3059\u3002null\u304b\u3082\u3057\u308c\u306a\u3044\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u30e9\u30c3\u30d7\u3057\u3066\u4fbf\u5229\u306b\u6271\u3048\u308b\u3088\u3046\u306b\u3057\u305f\u30af\u30e9\u30b9\u3067\u3059\u3002 \u3053\u3053\u306e\u7ba1\u7406\u4eba\u306f\u6065\u305a\u304b\u3057\u306a\u304c\u3089\u3053\u306e\u6a5f\u80fd\u3092\u6700\u8fd1\u307e\u3067\u77e5\u3089\u306a\u304b\u3063\u305f\u306e\u3067\u3001\u52c9\u5f37\u306e\u610f\u5473\u3082\u517c [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17,16],"tags":[],"class_list":["post-1035","post","type-post","status-publish","format-standard","hentry","category-java","category-16"],"_links":{"self":[{"href":"https:\/\/daba-no-heya.com\/index.php?rest_route=\/wp\/v2\/posts\/1035","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/daba-no-heya.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/daba-no-heya.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/daba-no-heya.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/daba-no-heya.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1035"}],"version-history":[{"count":2,"href":"https:\/\/daba-no-heya.com\/index.php?rest_route=\/wp\/v2\/posts\/1035\/revisions"}],"predecessor-version":[{"id":1037,"href":"https:\/\/daba-no-heya.com\/index.php?rest_route=\/wp\/v2\/posts\/1035\/revisions\/1037"}],"wp:attachment":[{"href":"https:\/\/daba-no-heya.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1035"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/daba-no-heya.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1035"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/daba-no-heya.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1035"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}