{"id":299386,"date":"2020-02-27T21:00:12","date_gmt":"2020-02-27T21:00:12","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=299386"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=299386","title":{"rendered":"Sealed \u0442\u0438\u043f\u044b \u0432 Java"},"content":{"rendered":"\n<div class=\"post__text post__text-html\" id=\"post-content-body\" data-io-article-url=\"https:\/\/habr.com\/ru\/post\/490298\/\">\u042f\u0437\u044b\u043a Java \u0441 \u043d\u0435\u0434\u0430\u0432\u043d\u0435\u0433\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u0441\u0442\u0430\u043b \u0430\u043a\u0442\u0438\u0432\u043d\u043e \u0440\u0430\u0437\u0432\u0438\u0432\u0430\u0442\u044c\u0441\u044f. \u0428\u0435\u0441\u0442\u0438\u043c\u0435\u0441\u044f\u0447\u043d\u044b\u0439 \u0440\u0435\u043b\u0438\u0437 \u0432\u0435\u0440\u0441\u0438\u0439 Java \u043d\u0435 \u043c\u043e\u0436\u0435\u0442 \u043d\u0435 \u0440\u0430\u0434\u043e\u0432\u0430\u0442\u044c Java \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0430 \u043d\u043e\u0432\u044b\u043c\u0438 \u0444\u0438\u0447\u0430\u043c\u0438. <br \/>  \u041e\u0434\u043d\u0438\u043c \u0438\u0437 \u043f\u0440\u0438\u043e\u0440\u0438\u0442\u0435\u0442\u043d\u044b\u0445 \u043d\u0430\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u0439 \u0440\u0430\u0437\u0432\u0438\u0442\u0438\u044f Java \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0441\u043e\u043f\u043e\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0441 \u043e\u0431\u0440\u0430\u0437\u0446\u043e\u043c (pattern matching). Pattern matching \u0440\u0430\u0441\u043a\u0440\u044b\u0432\u0430\u044e\u0442 \u043f\u0435\u0440\u0435\u0434 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u043e\u043c \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u043f\u0438\u0441\u0430\u0442\u044c \u043a\u043e\u0434 \u0431\u043e\u043b\u0435\u0435 \u0433\u0438\u0431\u043a\u043e \u0438 \u043a\u0440\u0430\u0441\u0438\u0432\u0435\u0435, \u043f\u0440\u0438 \u044d\u0442\u043e\u043c \u043e\u0441\u0442\u0430\u0432\u043b\u044f\u044f \u0435\u0433\u043e \u043f\u043e\u043d\u044f\u0442\u043d\u044b\u043c. <\/p>\n<p>  \u041a\u043b\u044e\u0447\u0435\u0432\u044b\u043c\u0438 \u0431\u043b\u043e\u043a\u0430\u043c\u0438 \u0434\u043b\u044f pattern matching \u0432 Java \u043f\u043b\u0430\u043d\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u0437\u0430\u043f\u0438\u0441\u0438 (record) \u0438 \u0437\u0430\u043f\u0435\u0447\u0430\u0442\u0430\u043d\u044b\u0435 (sealed) \u0442\u0438\u043f\u044b.<br \/>  <a href=\"http:\/\/openjdk.java.net\/jeps\/359\" rel=\"nofollow\">\u0417\u0430\u043f\u0438\u0441\u0438 (record)<\/a> \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u044e\u0442 \u043b\u0430\u043a\u043e\u043d\u0438\u0447\u043d\u044b\u0439 \u0441\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0441 \u0434\u043b\u044f \u043e\u0431\u044a\u044f\u0432\u043b\u0435\u043d\u0438\u044f \u043a\u043b\u0430\u0441\u0441\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u044f\u0432\u043b\u044f\u044e\u0442\u0441\u044f \u043f\u0440\u043e\u0441\u0442\u044b\u043c\u0438 \u043d\u043e\u0441\u0438\u0442\u0435\u043b\u044f\u043c\u0438 \u043f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u044b\u0445, \u043d\u0435\u0438\u0437\u043c\u0435\u043d\u044f\u0435\u043c\u044b\u0445 \u043d\u0430\u0431\u043e\u0440\u043e\u0432 \u0434\u0430\u043d\u043d\u044b\u0445.<br \/>  \u041f\u043e\u044f\u0432\u043b\u044f\u0442\u0441\u044f \u0432 Java 14 \u0432 \u0432\u0438\u0434\u0435 preview feature.<\/p>\n<pre><code class=\"java\">record Person(String name, int age) { } <\/code><\/pre>\n<p>  <a name=\"habracut\"><\/a><br \/>  <a href=\"http:\/\/openjdk.java.net\/jeps\/360\" rel=\"nofollow\">\u0417\u0430\u043f\u0435\u0447\u0430\u0442\u0430\u043d\u043d\u044b\u0435 (sealed) \u0442\u0438\u043f\u044b<\/a> \u2014 \u044d\u0442\u043e \u043a\u043b\u0430\u0441\u0441\u044b \u0438\u043b\u0438 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u044b, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043d\u0430\u043a\u043b\u0430\u0434\u044b\u0432\u0430\u044e\u0442 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044f \u043d\u0430 \u0434\u0440\u0443\u0433\u0438\u0435 \u043a\u043b\u0430\u0441\u0441\u044b \u0438\u043b\u0438 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u044b, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u043e\u0433\u0443\u0442 \u0440\u0430\u0441\u0448\u0438\u0440\u044f\u0442\u044c \u0438\u043b\u0438 \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u044b\u0432\u0430\u0442\u044c \u0438\u0445. \u0421 \u0431\u043e\u043b\u044c\u0448\u043e\u0439 \u0434\u043e\u043b\u0435\u0439 \u0432\u0435\u0440\u043e\u044f\u0442\u043d\u043e\u0441\u0442\u0438 \u043c\u043e\u0433\u0443\u0442 \u043f\u043e\u044f\u0432\u0438\u0442\u0441\u044f \u0432 Java 15. \u041f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u044e\u0442 \u0441 \u0441\u0435\u0431\u044f enum \u0442\u0438\u043f\u044b \u043d\u0430 \u0441\u0442\u0435\u0440\u043e\u0438\u0434\u0430\u0445.<\/p>\n<pre><code class=\"java\">sealed interface Color permits BiColor, TriColor { } \t record BiColor(int r, int g, int b) implements Color {} record TriColor(int r, int g, int b) implements Color {} <\/code><\/pre>\n<p>  \u0414\u043b\u044f \u043e\u0431\u044a\u044f\u0432\u043b\u0435\u043d\u0438\u044f sealed \u043a\u043b\u0430\u0441\u0441\u0430 \u0438\u043b\u0438 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u0430 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f sealed \u043c\u043e\u0434\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440. \u0421\u043f\u0438\u0441\u043e\u043a \u043f\u043e\u0434\u0442\u0438\u043f\u043e\u0432 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043f\u0435\u0440\u0435\u0447\u0438\u0441\u043b\u0435\u043d\u043e \u0432\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u043e\u0431\u044a\u044f\u0432\u043b\u0435\u043d\u0438\u044f sealed \u043a\u043b\u0430\u0441\u0441\u0430 \u0438\u043b\u0438 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u0430 <br \/>  \u043f\u043e\u0441\u043b\u0435 \u043a\u043b\u044e\u0447\u0435\u0432\u043e\u0433\u043e \u0441\u043b\u043e\u0432\u0430 permits. \u0412 \u0441\u043b\u0443\u0447\u0430\u0435 \u0435\u0441\u043b\u0438 \u043f\u043e\u0434\u0442\u0438\u043f\u044b \u043d\u0430\u0445\u043e\u0434\u044f\u0442\u0441\u044f \u0432 \u0442\u043e\u043c \u0436\u0435 \u043f\u0430\u043a\u0435\u0442\u0435 \u0438\u043b\u0438 \u043c\u043e\u0434\u0443\u043b\u0435, \u0442\u043e \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440 \u0441\u0430\u043c \u043c\u043e\u0436\u0435\u0442 \u0432\u044b\u0432\u0435\u0441\u0442\u0438 \u0441\u043f\u0438\u0441\u043e\u043a \u043f\u043e\u0434\u0442\u0438\u043f\u043e\u0432 \u0438 permits \u0432 \u043e\u0431\u044a\u044f\u0432\u043b\u0435\u043d\u0438\u0438 sealed<br \/>  \u043a\u043b\u0430\u0441\u0441\u0430 \u0438\u043b\u0438 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u0430 \u043c\u043e\u0436\u043d\u043e \u043e\u043f\u0443\u0441\u0442\u0438\u0442\u044c.<\/p>\n<p>  \u0415\u0441\u043b\u0438 \u043f\u043e\u0434\u0442\u0438\u043f \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0430\u0431\u0441\u0442\u0440\u0430\u043a\u0442\u043d\u044b\u043c, \u0442\u043e \u043e\u043d \u043d\u0435\u044f\u0432\u043d\u043e \u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0441\u044f \u043f\u043e\u043c\u0435\u0447\u0435\u043d\u043d\u044b\u0439 sealed \u043c\u043e\u0434\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440\u043e\u043c, \u0435\u0441\u043b\u0438 \u0435\u0433\u043e \u044f\u0432\u043d\u043e \u043d\u0435 \u043f\u043e\u043c\u0435\u0442\u0438\u0442\u044c \u043c\u043e\u0434\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440\u043e\u043c non-sealed.<br \/>  \u041a\u0430\u043a \u0432\u0438\u0434\u043d\u043e Java \u0432\u0432\u043e\u0434\u0438\u0442 \u043d\u043e\u0432\u044b\u0439 \u0442\u0438\u043f \u043a\u043b\u044e\u0447\u0435\u0432\u044b\u0445 \u0441\u043b\u043e\u0432 \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043d\u0430\u0437\u044b\u0432\u0430\u044e\u0442\u0441\u044f <a href=\"https:\/\/openjdk.java.net\/jeps\/8223002\" rel=\"nofollow\">hyphenated keywords<\/a>. <br \/>  \u0422\u0430\u043a\u0438\u0435 \u043a\u043b\u044e\u0447\u0435\u0432\u044b\u0435 \u0441\u043b\u043e\u0432\u0430 \u0431\u0443\u0434\u0443 \u0441\u043e\u0441\u0442\u043e\u044f\u0442\u044c \u0437 \u0434\u0432\u0443\u0445 \u0441\u043b\u043e\u0432 \u0440\u0430\u0437\u0434\u0435\u043b\u0435\u043d\u043d\u044b\u0445 \u0447\u0435\u0440\u0442\u043e\u0447\u043a\u043e\u0439. <\/p>\n<p>  \u041a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u044b\u0435 \u043f\u043e\u0434\u0442\u0438\u043f\u044b \u043d\u0435\u044f\u0432\u043d\u043e \u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0441\u044f final, \u0435\u0441\u043b\u0438 \u0435\u0433\u043e \u044f\u0432\u043d\u043e \u043d\u0435 \u043f\u043e\u043c\u0435\u0442\u0438\u0442\u044c \u043c\u043e\u0434\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440\u043e\u043c non-sealed. \u0425\u043e\u0442\u044f \u043c\u043d\u0435 \u043a\u0430\u0436\u0435\u0442\u0441\u044f, \u0431\u044b\u043b\u043e \u0431\u044b \u043b\u0443\u0447\u0448\u0435 \u0437\u0430\u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c non-final, \u0435\u0441\u043b\u0438 \u043d\u0435 \u0445\u043e\u0447\u0435\u0442\u0441\u044f \u0435\u0433\u043e \u0434\u0435\u043b\u0430\u0442\u044c \u043a\u043e\u043d\u0435\u0447\u043d\u044b\u043c.<\/p>\n<p>  \u0414\u043b\u044f \u0442\u043e\u0433\u043e, \u0447\u0442\u043e\u0431\u044b \u043e\u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0438\u0442\u044c \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u0443 sealed \u0442\u0438\u043f\u043e\u0432 \u0432 class-\u0444\u0430\u0439\u043b\u044b \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043d\u043e\u0432\u044b\u0439 \u0430\u0442\u0440\u0438\u0431\u0443\u0442 PermittedSubtypes, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0435\u0442 \u0441\u043f\u0438\u0441\u043e\u043a \u043f\u043e\u0434\u0442\u0438\u043f\u043e\u0432.<\/p>\n<pre><code class=\"java\">PermittedSubtypes_attribute { \tu2 attribute_name_index; \tu4 attribute_length; \tu2 permitted_subtypes_count; \tu2 classes[permitted_subtypes_count]; } <\/code><\/pre>\n<p>  \u0422\u0430\u043a\u0436\u0435 \u0434\u043b\u044f \u0442\u043e\u0433\u043e \u0447\u0442\u043e\u0431\u044b \u043c\u043e\u0436\u043d\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0441 sealed \u0442\u0438\u043f\u0430\u043c\u0438 \u0447\u0435\u0440\u0435\u0437 \u0440\u0435\u0444\u043b\u0435\u043a\u0441\u0438\u044e \u0432 java.lang.Class \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u044e\u0442\u0441\u044f \u0434\u0432\u0430 \u043c\u0435\u0442\u043e\u0434\u0430.<\/p>\n<pre><code class=\"java\">java.lang.constant.ClassDesc&lt;?&gt;[] getPermittedSubtypes() boolean isSealed() <\/code><\/pre>\n<p>  \u041f\u0435\u0440\u0432\u044b\u0439 \u043c\u0435\u0442\u043e\u0434 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u043c\u0430\u0441\u0441\u0438\u0432 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 java.lang.constant.ClassDesc, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u044e\u0442 \u0441\u043f\u0438\u0441\u043e\u043a \u043f\u043e\u0434\u0442\u0438\u043f\u043e\u0432, \u0435\u0441\u043b\u0438 \u043a\u043b\u0430\u0441\u0441 \u043f\u043e\u043c\u0435\u0447\u0435\u043d\u043d\u044b\u0439 sealed \u043c\u043e\u0434\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440. \u0415\u0441\u043b\u0438 \u043a\u043b\u0430\u0441\u0441 \u043d\u0435 \u043f\u043e\u043c\u0435\u0447\u0435\u043d\u043d\u044b\u0439 sealed \u043c\u043e\u0434\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440, \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f \u043f\u0443\u0441\u0442\u043e\u0439 \u043c\u0430\u0441\u0441\u0438\u0432. \u0412\u0442\u043e\u0440\u043e\u0439 \u043c\u0435\u0442\u043e\u0434 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 true, \u0435\u0441\u043b\u0438 \u0434\u0430\u043d\u043d\u044b\u0439 \u043a\u043b\u0430\u0441\u0441 \u0438\u043b\u0438 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 \u043f\u043e\u043c\u0435\u0447\u0435\u043d\u043d\u044b\u0439 sealed \u043c\u043e\u0434\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440.<\/p>\n<p>  \u0412\u0441\u0435 \u044d\u0442\u043e \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043f\u0440\u0438 \u043f\u0435\u0440\u0435\u0431\u043e\u0440\u0435 \u0441\u043f\u0438\u0441\u043a\u0430 \u043f\u043e\u0434\u0442\u0438\u043f\u043e\u0432 \u0432 switch-\u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u0438 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440\u0443 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c, \u043f\u0435\u0440\u0435\u0431\u0440\u0430\u043d\u044b \u0432\u0441\u0435 \u043f\u043e\u0434\u0442\u0438\u043f\u044b \u0438\u043b\u0438 \u043d\u0435\u0442.<\/p>\n<pre><code class=\"java\">var result = switch (color) { \tcase BiColor bc -&gt; 0x1; \tcase TriColor tc -&gt; 0x2; } <\/code><\/pre>\n<p>  \u0412\u0435\u0442\u043a\u0443 default \u043d\u0435\u043e\u0431\u044a\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0442\u044c, \u0432\u0435\u0434\u044c \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u0442 \u0432\u0441\u0435 \u0434\u043e\u043f\u0443\u0441\u0442\u0438\u043c\u044b\u0435 \u043f\u043e\u0434\u0442\u0438\u043f\u044b. <br \/>  \u0418 \u0435\u0441\u043b\u0438 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043d\u043e\u0432\u044b\u0439 \u043f\u043e\u0434\u0442\u0438\u043f, \u0442\u043e \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442, \u0447\u0442\u043e \u0432 switch \u043d\u0435 \u0432\u0441\u0435 \u043f\u043e\u0434\u0442\u0438\u043f\u044b \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0435\u043d\u044b, \u0438 \u0432\u044b\u043a\u0438\u043d\u0435\u0442 \u043e\u0448\u0438\u0431\u043a\u0443 \u0432\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0446\u0438\u0438.<\/p>\n<p>  \u0418\u0434\u0435\u044f sealed \u0442\u0438\u043f\u043e\u0432 \u043d\u0435 \u043d\u043e\u0432\u0430\u044f. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0432 \u044f\u0437\u044b\u043a\u0435 Kotlin \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u044e\u0442 sealed \u043a\u043b\u0430\u0441\u0441\u044b. \u041d\u043e sealed \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u043e\u0432 \u043d\u0435\u0442\u0443.<\/p>\n<pre><code class=\"kotlin\">sealed class Color \t data class BiColor(val r: Int, val g: Int, val b: Int) : Color  data class TriColor(val r: Int, val g: Int, val b: Int) : Color <\/code><\/pre>\n<p>  sealed \u043a\u043b\u0430\u0441\u0441\u044b \u0432 \u044f\u0437\u044b\u043a\u0435 Kotlin \u043d\u0435\u044f\u0432\u043d\u043e \u044f\u0432\u043b\u044f\u044e\u0442\u0441\u044f \u0430\u0431\u0441\u0442\u0440\u0430\u043a\u0442\u043d\u044b\u043c\u0438 \u0438 \u0438\u043c\u0435\u044e\u0442 \u043f\u0440\u0438\u0432\u0430\u0442\u043d\u044b\u0439 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e. \u0421\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e, \u043f\u043e\u0434\u0442\u0438\u043f\u044b \u0434\u043e\u043b\u0436\u043d\u044b \u0431\u044b\u0442\u044c \u0432\u043b\u043e\u0436\u0435\u043d\u043d\u044b\u043c\u0438 \u0432 \u043a\u043b\u0430\u0441\u0441. \u041d\u0430 \u044f\u0437\u044b\u043a\u0435 Java \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c.<\/p>\n<pre><code class=\"java\">public abstract class Color { \tprivate Color() {} \t \tpublic static class BiColor(int r, int g, int b) extends Color {} \tpublic static class TriColor(int r, int g, int b) extends Color {} } <\/code><\/pre>\n<p>  \u0412 \u044d\u0442\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043c\u043e\u0436\u043d\u043e \u0433\u0430\u0440\u0430\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c, \u0447\u0442\u043e \u0432\u0441\u0435 \u043f\u043e\u0434\u0442\u0438\u043f\u044b \u043c\u043e\u0436\u043d\u043e \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u0432\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0446\u0438\u0438.<\/p>\n<p>  \u042d\u0442\u043e \u0432\u0441\u0435 \u0445\u043e\u0440\u043e\u0448\u043e, \u043d\u043e \u0447\u0442\u043e \u0435\u0441\u043b\u0438 \u0445\u043e\u0447\u0435\u0442\u0441\u044f \u043f\u043e\u043f\u0440\u043e\u0431\u043e\u0432\u0430\u0442\u044c sealed \u043a\u043b\u0430\u0441\u0441\u044b, \u0430 \u043e\u043d\u0438 \u0435\u0449\u0435 \u043d\u0435 \u0432\u044b\u0448\u043b\u0438. \u0411\u043b\u0430\u0433\u043e \u043d\u0438\u0447\u0435\u0433\u043e \u043d\u0435 \u043c\u0435\u0448\u0430\u0435\u0442 \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u043f\u0440\u043e\u0441\u0442\u0435\u043d\u044c\u043a\u0438\u0439 \u0432\u0438\u0437\u0438\u0442\u0435\u0440, \u043f\u043e\u0434\u043e\u0431\u043d\u043e \u0442\u043e\u043c\u0443 \u043a\u0430\u043a \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 when-\u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u0435 \u0432 Kotlin.<\/p>\n<pre><code class=\"java\"> matches(color).as(        Color.BiColor.class,  bc -&gt; System.out.println(&quot;bi color:  &quot; + bc),        Color.TriColor.class, tc -&gt; System.out.println(&quot;tri color:  &quot; + tc)  ); <\/code><\/pre>\n<p>  \u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0432\u0438\u0437\u0438\u0442\u0435\u0440\u0430 \u043e\u0447\u0435\u043d\u044c \u043f\u0440\u043e\u0441\u0442\u0430. \u0411\u0435\u0440\u0435\u043c \u0442\u0438\u043f \u0446\u0435\u043b\u0435\u0432\u043e\u0433\u043e \u043e\u0431\u044a\u0435\u043a\u0442\u0430 \u0438 \u0432 \u0432 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438 \u043e\u0442 \u0442\u0438\u043f\u0430 \u0432\u0435\u0442\u043a\u0438, \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u043c \u043b\u044f\u043c\u0431\u0434\u0430-\u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u044f.<\/p>\n<pre><code class=\"java\"> public static &lt;V, T1, T2&gt;  void matches(V value,                 Class&lt;T1&gt; firstClazz,  Consumer&lt;T1&gt; firstBranch,                 Class&lt;T2&gt; secondClazz, Consumer&lt;T2&gt; secondBranch) {         verifyExhaustiveness(value, new Class&lt;?&gt;[]{ firstClazz, secondClazz });         Class&lt;?&gt; valueClass = value.getClass();          if (firstClazz == valueClass) {             firstBranch.accept((T1) value);         } else if (secondClazz == valueClass) {             secondBranch.accept((T2) value);         } } <\/code><\/pre>\n<p>  \u041d\u043e \u043a\u0440\u043e\u043c\u0435 \u0442\u043e\u0433\u043e, \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u0437\u043d\u0430\u0442\u044c \u043f\u043e\u0434\u0442\u0438\u043f\u044b \u0446\u0435\u043b\u0435\u0432\u043e\u0433\u043e \u043e\u0431\u044a\u0435\u043a\u0442\u0430 \u0438 \u0441\u0440\u0430\u0432\u043d\u0438\u0442\u044c \u0441 \u0442\u0438\u043f\u0430\u043c\u0438 \u0443\u043a\u0430\u0437\u0430\u043d\u043d\u044b\u0445 \u0432 \u0432\u0435\u0442\u043a\u0430\u0445. \u0418 \u0432 \u0441\u043b\u0443\u0447\u0430\u0435 \u0435\u0441\u043b\u0438 \u043a\u0430\u043a\u043e\u0439-\u0442\u043e \u043f\u043e\u0434\u0442\u0438\u043f \u043d\u0435 \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u043b\u0438 \u0431\u0440\u043e\u0441\u0438\u043c \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435.<\/p>\n<pre><code class=\"java\">public static &lt;V&gt; void verifyExhaustiveness(V value, Class&lt;?&gt;[] inputClasses) {         SealedAttribute data = cacheSubclasses.computeIfAbsent(value.getClass(), SealedAttribute::new);         Class&lt;?&gt;[] subClasses = data.getSubClasses();         StringBuilder builder = new StringBuilder();         boolean flag = false;          if (subClasses.length != inputClasses.length) {             throw new PatternException(&quot;Require &quot; + inputClasses.length + &quot; subclasses. &quot; +                                        &quot;But checked class has &quot; + subClasses.length + &quot; subclasses.&quot;);         }          for (Class&lt;?&gt; subClass : subClasses) {             for (Class&lt;?&gt; inputClass : inputClasses) {                 if (subClass == inputClass) {                     flag = true;                     break;                 }             }              if (!flag) {                 builder.append(subClass).append(&quot;,&quot;);             }              flag = false;         }          if (builder.length() &gt;= 1) {             throw new PatternException(&quot;Must to be exhaustive, add necessary &quot; + builder.toString() +                                        &quot; branches or else branch instead&quot;);         } } <\/code><\/pre>\n<p>  \u0421\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e, \u0447\u0442\u043e\u0431\u044b \u043a\u0430\u0436\u0434\u044b\u0439 \u0440\u0430\u0437 \u043d\u0435 \u0432\u044b\u0447\u0438\u0441\u043b\u044f\u0442\u044c \u043f\u043e\u0434\u0442\u0438\u043f\u044b \u043a\u043b\u0430\u0441\u0441\u0430, \u043c\u043e\u0436\u0435\u043c \u043a\u0435\u0448\u0438\u0440\u043e\u0432\u0430\u0442\u044c. \u041f\u0440\u0438 \u044d\u0442\u043e\u043c \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c, \u0447\u0442\u043e \u0446\u0435\u043b\u0435\u0432\u043e\u0439 \u043a\u043b\u0430\u0441\u0441 \u0430\u0431\u0441\u0442\u0440\u0430\u043a\u0442\u043d\u044b\u0439, \u0438\u043c\u0435\u0435\u0442 \u043f\u0440\u0438\u0432\u0430\u0442\u043d\u044b\u0439 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440, \u0438 \u0432\u0441\u0435 \u0432\u043b\u043e\u0436\u0435\u043d\u043d\u044b\u0435 \u043a\u043b\u0430\u0441\u0441\u044b \u0443\u043d\u0430\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u043d\u043d\u044b\u0435 \u043e\u0442 \u0446\u0435\u043b\u0435\u0432\u043e\u0433\u043e \u043a\u043b\u0430\u0441\u0441\u0430. <\/p>\n<pre><code class=\"java\">public final class SealedAttribute {     private Class&lt;?&gt;[] subClasses;      public SealedAttribute(Class&lt;?&gt; clazz) {         Class&lt;?&gt; sealedClass = clazz.getSuperclass();          if (!sealedClass.isAnnotationPresent(Sealed.class)) {             throw new PatternException(&quot;Checked class must to be mark as sealed&quot;);         }          if (!Modifier.isAbstract(sealedClass.getModifiers())) {             throw new PatternException(&quot;Checked class must to be abstract&quot;);         }          try {             final Constructor&lt;?&gt; constructor = sealedClass.getDeclaredConstructor();              if (!Modifier.isPrivate(constructor.getModifiers())) {                 throw new PatternException(&quot;Default constructor must to be private&quot;);             }              this.subClasses = sealedClass.getClasses();              if (subClasses.length == 0) {                 throw new PatternException(&quot;Checked class must to has one or more visible subClasses&quot;);             }              for (Class&lt;?&gt; subclass : subClasses) {                 if (!Modifier.isStatic(subclass.getModifiers())) {                     throw new PatternException(&quot;Subclass must to be static&quot;);                 }                  if (subclass.getSuperclass() != sealedClass) {                     throw new PatternException(&quot;Subclass must to inherit from checked class&quot;);                 }             }         } catch (NoSuchMethodException e) {             throw new PatternException(&quot;Checked class must to has default constructor &quot; + e.getMessage());         }     }      public Class&lt;?&gt;[] getSubClasses() {         return subClasses;     } } <\/code><\/pre>\n<p>  \u041d\u0430\u043f\u0438\u0448\u0435\u043c \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u0431\u0435\u043d\u0447\u043c\u0430\u0440\u043a \u0438 \u043f\u043e\u043c\u0435\u0440\u044f\u0435\u043c \u0432 \u0441\u0440\u0435\u0434\u043d\u0435\u043c \u043d\u0430 \u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043e\u0442\u043b\u0438\u0447\u0430\u044e\u0442\u0441\u044f \u0441\u043a\u043e\u0440\u043e\u0441\u0442\u044c \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0432\u0438\u0437\u0438\u0442\u0435\u0440\u0430 \u0438 \u043a\u043e\u0434\u0430 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u043e\u0433\u043e \u043d\u0430 \u0447\u0438\u0441\u0442\u043e\u0439 Java. \u041f\u043e\u043b\u043d\u044b\u0439 \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u0431\u0435\u043d\u0447\u043c\u0430\u0440\u043a\u0430 \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c <a href=\"https:\/\/github.com\/klappdev\/jpml\/tree\/master\/test\/org\/kl\/benchmark\/UnionPatternBenchmark.java\" rel=\"nofollow\">\u0437\u0434\u0435\u0441\u044c<\/a>.<\/p>\n<pre><code class=\"java\"> @Warmup(iterations = 3, time = 1) @Measurement(iterations = 3, time = 1) @Fork(3) @State(Scope.Thread) @BenchmarkMode(Mode.AverageTime) @OutputTimeUnit(TimeUnit.NANOSECONDS) public class UnionPatternBenchmark {      private BiColor biColor; \t     @Setup     public void setup() {         biColor = new BiColor.Red();     }      @Benchmark     public int matchesSealedBiExpressionPlain() {         if (biColor instanceof BiColor.Red) {                return 0x1;         } else if (biColor instanceof BiColor.Blue) {                return 0x2;         }          return 0x0;     }      @Benchmark     public int matchesSealedBiExpressionReflective() {         return matches(biColor).as(                 BiColor.Red.class,  r -&gt; 0x1,                 BiColor.Blue.class, b -&gt; 0x2         );     } }\t  UnionPatternBenchmark.matchesSealedBiExpressionPlain           avgt    9   5,992 \u00b1  0,332  ns\/op UnionPatternBenchmark.matchesSealedTriExpressionPlain          avgt    9   7,199 \u00b1  0,356  ns\/op  UnionPatternBenchmark.matchesSealedBiExpressionReflective      avgt    9  45,192 \u00b1 11,951  ns\/op UnionPatternBenchmark.matchesSealedTriExpressionReflective     avgt    9  43,413 \u00b1  0,702  ns\/op <\/code><\/pre>\n<p>  \u041f\u043e\u0434\u0432\u0435\u0434\u044f \u0438\u0442\u043e\u0433, \u043c\u043e\u0436\u043d\u043e \u0441\u043a\u0430\u0437\u0430\u0442\u044c sealed \u0442\u0438\u043f\u044b \u043e\u0447\u0435\u043d\u044c \u0445\u043e\u0440\u043e\u0448\u0430\u044f \u0444\u0438\u0447\u0430, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u0443\u043f\u0440\u043e\u0441\u0442\u0438\u0442\u044c \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u044f \u043a\u043e\u0434\u0430 \u0438 \u0441\u0434\u0435\u043b\u0430\u0435\u0442 \u043a\u043e\u0434 \u0431\u043e\u043b\u0435\u0435 \u043d\u0430\u0434\u0435\u0436\u043d\u0435\u0435.<\/p>\n<p>  \u041f\u043e\u043b\u043d\u044b\u0439 \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0439 \u043a\u043e\u0434 \u0432\u0438\u0437\u0438\u0442\u043e\u0440\u0430 \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u043d\u0430 <a href=\"https:\/\/github.com\/klappdev\/jpml\/blob\/master\/src\/org\/kl\/pattern\/UnionPattern.java\" rel=\"nofollow\">github<\/a>.<\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/490298\/\"> https:\/\/habr.com\/ru\/post\/490298\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"\n<div class=\"post__text post__text-html\" id=\"post-content-body\" data-io-article-url=\"https:\/\/habr.com\/ru\/post\/490298\/\">\u042f\u0437\u044b\u043a Java \u0441 \u043d\u0435\u0434\u0430\u0432\u043d\u0435\u0433\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u0441\u0442\u0430\u043b \u0430\u043a\u0442\u0438\u0432\u043d\u043e \u0440\u0430\u0437\u0432\u0438\u0432\u0430\u0442\u044c\u0441\u044f. \u0428\u0435\u0441\u0442\u0438\u043c\u0435\u0441\u044f\u0447\u043d\u044b\u0439 \u0440\u0435\u043b\u0438\u0437 \u0432\u0435\u0440\u0441\u0438\u0439 Java \u043d\u0435 \u043c\u043e\u0436\u0435\u0442 \u043d\u0435 \u0440\u0430\u0434\u043e\u0432\u0430\u0442\u044c Java \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0430 \u043d\u043e\u0432\u044b\u043c\u0438 \u0444\u0438\u0447\u0430\u043c\u0438. <br \/>  \u041e\u0434\u043d\u0438\u043c \u0438\u0437 \u043f\u0440\u0438\u043e\u0440\u0438\u0442\u0435\u0442\u043d\u044b\u0445 \u043d\u0430\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u0439 \u0440\u0430\u0437\u0432\u0438\u0442\u0438\u044f Java \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0441\u043e\u043f\u043e\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0441 \u043e\u0431\u0440\u0430\u0437\u0446\u043e\u043c (pattern matching). Pattern matching \u0440\u0430\u0441\u043a\u0440\u044b\u0432\u0430\u044e\u0442 \u043f\u0435\u0440\u0435\u0434 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u043e\u043c \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u043f\u0438\u0441\u0430\u0442\u044c \u043a\u043e\u0434 \u0431\u043e\u043b\u0435\u0435 \u0433\u0438\u0431\u043a\u043e \u0438 \u043a\u0440\u0430\u0441\u0438\u0432\u0435\u0435, \u043f\u0440\u0438 \u044d\u0442\u043e\u043c \u043e\u0441\u0442\u0430\u0432\u043b\u044f\u044f \u0435\u0433\u043e \u043f\u043e\u043d\u044f\u0442\u043d\u044b\u043c. <\/p>\n<p>  \u041a\u043b\u044e\u0447\u0435\u0432\u044b\u043c\u0438 \u0431\u043b\u043e\u043a\u0430\u043c\u0438 \u0434\u043b\u044f pattern matching \u0432 Java \u043f\u043b\u0430\u043d\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u0437\u0430\u043f\u0438\u0441\u0438 (record) \u0438 \u0437\u0430\u043f\u0435\u0447\u0430\u0442\u0430\u043d\u044b\u0435 (sealed) \u0442\u0438\u043f\u044b.<br \/>  <a href=\"http:\/\/openjdk.java.net\/jeps\/359\" rel=\"nofollow\">\u0417\u0430\u043f\u0438\u0441\u0438 (record)<\/a> \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u044e\u0442 \u043b\u0430\u043a\u043e\u043d\u0438\u0447\u043d\u044b\u0439 \u0441\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0441 \u0434\u043b\u044f \u043e\u0431\u044a\u044f\u0432\u043b\u0435\u043d\u0438\u044f \u043a\u043b\u0430\u0441\u0441\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u044f\u0432\u043b\u044f\u044e\u0442\u0441\u044f \u043f\u0440\u043e\u0441\u0442\u044b\u043c\u0438 \u043d\u043e\u0441\u0438\u0442\u0435\u043b\u044f\u043c\u0438 \u043f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u044b\u0445, \u043d\u0435\u0438\u0437\u043c\u0435\u043d\u044f\u0435\u043c\u044b\u0445 \u043d\u0430\u0431\u043e\u0440\u043e\u0432 \u0434\u0430\u043d\u043d\u044b\u0445.<br \/>  \u041f\u043e\u044f\u0432\u043b\u044f\u0442\u0441\u044f \u0432 Java 14 \u0432 \u0432\u0438\u0434\u0435 preview feature.<\/p>\n<pre><code class=\"java\">record Person(String name, int age) { } <\/code><\/pre>\n<p>  <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-299386","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/299386","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=299386"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/299386\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=299386"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=299386"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=299386"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}