{"id":1515,"date":"2020-10-03T11:54:02","date_gmt":"2020-10-03T10:54:02","guid":{"rendered":"https:\/\/benjiweber.co.uk\/blog\/?p=1515"},"modified":"2020-10-03T11:54:04","modified_gmt":"2020-10-03T10:54:04","slug":"sealed-java-state-machines","status":"publish","type":"post","link":"https:\/\/benjiweber.co.uk\/blog\/2020\/10\/03\/sealed-java-state-machines\/","title":{"rendered":"Sealed Java State Machines"},"content":{"rendered":"\n<p class=\"lead\">A few years back I posted about <a href=\"https:\/\/benjiweber.co.uk\/blog\/2015\/08\/24\/optionally-typechecked-statemachines\/\">how to implement state machines<\/a> that only permit valid transitions at compile time in Java. <\/p>\n\n\n\n<p>This used interfaces instead of enums, which had a big drawback\u2014you couldn&#8217;t guarantee that you know all the states involved. Someone could add another state elsewhere in your codebase by implementing the interface.<\/p>\n\n\n\n<p>Java 15 brings a preview feature of <a href=\"https:\/\/openjdk.java.net\/jeps\/360\">sealed classes<\/a>. Sealed classes enable us to solve this downside. Now our interface based state machines can not only prevent invalid transitions but also be enumerable like enums.<\/p>\n\n\n\n<p>If you&#8217;re using jdk 15 with preview features enabled you can <a href=\"https:\/\/github.com\/benjiman\/statemachine\">try out the code<\/a>. This is how it looks to define a state machine with interfaces.<\/p>\n\n\n\n<pre lang=\"java\">sealed interface TrafficLight\n       extends State<TrafficLight>\n       permits Green, SolidAmber, FlashingAmber, Red {}\nstatic final class Green implements TrafficLight, TransitionTo<SolidAmber> {}\nstatic final class SolidAmber implements TrafficLight, TransitionTo<Red> {}\nstatic final class Red implements TrafficLight, TransitionTo<FlashingAmber> {}\nstatic final class FlashingAmber implements TrafficLight, TransitionTo<Green> {}<\/pre>\n\n\n\n<p>The new part is <em>&#8220;sealed&#8221;<\/em> and <em>&#8220;permits&#8221;<\/em>. Now it becomes a compile failure to define a new implementation of TrafficLight\u00a0<\/p>\n\n\n\n<a href=\"https:\/\/benjiweber.co.uk\/statemachine_sealed1.png\"><img src=\"https:\/\/benjiweber.co.uk\/statemachine_sealed1.png\"\/><\/a>\n\n\n\n<p>As well as the existing behaviour where it&#8217;s a compile time failure to perform a transition that traffic lights do not allow.&nbsp;<\/p>\n\n\n\n<a href=\"https:\/\/benjiweber.com\/statemachine_sealed2.png\"><img src=\"https:\/\/benjiweber.com\/statemachine_sealed2.png\"\/><\/a>\n\n\n\n<p>n.b. you can also skip the compile time checked version and <a href=\"https:\/\/benjiweber.co.uk\/blog\/2015\/08\/24\/optionally-typechecked-statemachines\/#:~:text=Runtime%20checking\">still use the type definitions to runtime check the transitions<\/a>.\u00a0<\/p>\n\n\n\n<p>Multiple transitions are possible from a state too<\/p>\n\n\n\n<pre lang=\"java\">static final class Pending \n  implements OrderStatus, BiTransitionTo<CheckingOut, Cancelled> {}<\/pre>\n\n\n\n<a href=\"https:\/\/benjiweber.co.uk\/statemachine_sealed3.png\"><img src=\"https:\/\/benjiweber.co.uk\/statemachine_sealed3.png\"\/><\/a>\n\n\n\n<p>Thanks to sealed classes we can also now do enum style enumeration and lookups on our interface based state machines.<\/p>\n\n\n\n<pre lang=\"java\">sealed interface OrderStatus\n       extends State<OrderStatus>\n       permits Pending, CheckingOut, Purchased, Shipped, Cancelled, Failed, Refunded {}\n\n\n@Test public void enumerable() {\n  assertArrayEquals(\n    array(Pending.class, CheckingOut.class, Purchased.class, Shipped.class, Cancelled.class, Failed.class, Refunded.class),\n    State.values(OrderStatus.class)\n  );\n\n  assertEquals(0, new Pending().ordinal());\n  assertEquals(3, new Shipped().ordinal());\n\n  assertEquals(Purchased.class, State.valueOf(OrderStatus.class, \"Purchased\"));\n  assertEquals(Cancelled.class, State.valueOf(OrderStatus.class, \"Cancelled\"));\n}\n<\/pre>\n\n\n\n<p>These are possible because <a href=\"https:\/\/openjdk.java.net\/jeps\/360#Reflection-API\">JEP 360 provides a reflection API<\/a> with which one can enumerate the permitted subclasses of an interface. ( side note the JEP says getPermittedSubclasses() but the implementation seems to use <a href=\"https:\/\/docs.oracle.com\/en\/java\/javase\/15\/docs\/api\/java.base\/java\/lang\/Class.html#permittedSubclasses()\">permittedSubclasses<\/a>() )\u00a0<br>We can add use this to add the above <a href=\"https:\/\/github.com\/benjiman\/statemachine\/blob\/master\/src\/main\/java\/com\/benjiweber\/statemachine\/State.java#L97\">convenience methods to our State interface<\/a> to allow the <a href=\"https:\/\/github.com\/benjiman\/statemachine\/blob\/master\/src\/main\/java\/com\/benjiweber\/statemachine\/State.java#L146\">values<\/a>(), <a href=\"https:\/\/github.com\/benjiman\/statemachine\/blob\/master\/src\/main\/java\/com\/benjiweber\/statemachine\/State.java#L156\">ordinal<\/a>(), and <a href=\"https:\/\/github.com\/benjiman\/statemachine\/blob\/master\/src\/main\/java\/com\/benjiweber\/statemachine\/State.java#L129\">valueOf<\/a>() lookups.<\/p>\n\n\n\n<pre lang=\"java\">static <T extends State<T>> List<Class> valuesList(Class<T> stateMachineType) {\n   assertSealed(stateMachineType);\n\n   return Stream.of(stateMachineType.permittedSubclasses())\n       .map(State::classFromDesc)\n       .collect(toList());\n}\n\nstatic <T extends State<T>> Class<T> valueOf(Class<T> stateMachineType, String name) {\n   assertSealed(stateMachineType);\n\n   return valuesList(stateMachineType)\n       .stream()\n       .filter(c -> Objects.equals(c.getSimpleName(), name))\n       .findFirst()\n       .orElseThrow(IllegalArgumentException::new);\n}\nstatic <T extends State<T>, U extends T> int ordinal(Class<T> stateMachineType, Class<U> instanceType) {\n   return valuesList(stateMachineType).indexOf(instanceType);\n}<\/pre>\n\n\n\n<p>There are more details on how the transition checking works and more examples of where this might be useful in <a href=\"https:\/\/benjiweber.co.uk\/blog\/2015\/08\/24\/optionally-typechecked-statemachines\/\">the original post<\/a>. Code is <a href=\"https:\/\/github.com\/benjiman\/statemachine\">on github<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A few years back I posted about how to implement state machines that only permit valid transitions at compile time in Java. This used interfaces instead of enums, which had a big drawback\u2014you can&#8217;t guarantee that you know all the states involved. Java 15 brings a preview feature of sealed classes, solving this downside. <\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[8],"tags":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v14.9 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/benjiweber.co.uk\/blog\/2020\/10\/03\/sealed-java-state-machines\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Sealed Java State Machines - Benji&#039;s Blog\" \/>\n<meta property=\"og:description\" content=\"A few years back I posted about how to implement state machines that only permit valid transitions at compile time in Java. This used interfaces instead of enums, which had a big drawback\u2014you can&#039;t guarantee that you know all the states involved. Java 15 brings a preview feature of sealed classes, solving this downside.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/benjiweber.co.uk\/blog\/2020\/10\/03\/sealed-java-state-machines\/\" \/>\n<meta property=\"og:site_name\" content=\"Benji&#039;s Blog\" \/>\n<meta property=\"article:published_time\" content=\"2020-10-03T10:54:02+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-10-03T10:54:04+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/benjiweber.co.uk\/statemachine_sealed1.png\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"https:\/\/benjiweber.co.uk\/blog\/#website\",\"url\":\"https:\/\/benjiweber.co.uk\/blog\/\",\"name\":\"Benji&#039;s Blog\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":\"https:\/\/benjiweber.co.uk\/blog\/?s={search_term_string}\",\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/benjiweber.co.uk\/blog\/2020\/10\/03\/sealed-java-state-machines\/#primaryimage\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/benjiweber.co.uk\/statemachine_sealed1.png\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/benjiweber.co.uk\/blog\/2020\/10\/03\/sealed-java-state-machines\/#webpage\",\"url\":\"https:\/\/benjiweber.co.uk\/blog\/2020\/10\/03\/sealed-java-state-machines\/\",\"name\":\"Sealed Java State Machines - Benji&#039;s Blog\",\"isPartOf\":{\"@id\":\"https:\/\/benjiweber.co.uk\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/benjiweber.co.uk\/blog\/2020\/10\/03\/sealed-java-state-machines\/#primaryimage\"},\"datePublished\":\"2020-10-03T10:54:02+00:00\",\"dateModified\":\"2020-10-03T10:54:04+00:00\",\"author\":{\"@id\":\"https:\/\/benjiweber.co.uk\/blog\/#\/schema\/person\/45ecb36b51f4ce99e6929d2d31ca5c09\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/benjiweber.co.uk\/blog\/2020\/10\/03\/sealed-java-state-machines\/\"]}]},{\"@type\":\"Person\",\"@id\":\"https:\/\/benjiweber.co.uk\/blog\/#\/schema\/person\/45ecb36b51f4ce99e6929d2d31ca5c09\",\"name\":\"benji\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/benjiweber.co.uk\/blog\/#personlogo\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/05fb47b31a0b329e1b790074a9b624ef?s=96&d=mm&r=g\",\"caption\":\"benji\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","amp_enabled":false,"_links":{"self":[{"href":"https:\/\/benjiweber.co.uk\/blog\/wp-json\/wp\/v2\/posts\/1515"}],"collection":[{"href":"https:\/\/benjiweber.co.uk\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/benjiweber.co.uk\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/benjiweber.co.uk\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/benjiweber.co.uk\/blog\/wp-json\/wp\/v2\/comments?post=1515"}],"version-history":[{"count":16,"href":"https:\/\/benjiweber.co.uk\/blog\/wp-json\/wp\/v2\/posts\/1515\/revisions"}],"predecessor-version":[{"id":1532,"href":"https:\/\/benjiweber.co.uk\/blog\/wp-json\/wp\/v2\/posts\/1515\/revisions\/1532"}],"wp:attachment":[{"href":"https:\/\/benjiweber.co.uk\/blog\/wp-json\/wp\/v2\/media?parent=1515"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/benjiweber.co.uk\/blog\/wp-json\/wp\/v2\/categories?post=1515"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/benjiweber.co.uk\/blog\/wp-json\/wp\/v2\/tags?post=1515"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}