{"id":837,"date":"2017-12-18T14:41:42","date_gmt":"2017-12-18T14:41:42","guid":{"rendered":"http:\/\/smartfoxserver.com\/blog\/?p=837"},"modified":"2017-12-18T14:43:07","modified_gmt":"2017-12-18T14:43:07","slug":"using-functional-java-with-sfs2x-java-api","status":"publish","type":"post","link":"https:\/\/smartfoxserver.com\/blog\/using-functional-java-with-sfs2x-java-api\/","title":{"rendered":"Using functional Java with SFS2X Java API"},"content":{"rendered":"<p>With the release of patch 2.13.1 SmartFoxServer 2X adds support for lambda expressions in Java Extensions. Also we have added support for this new JDK 8 feature in the Java client API, starting from release 1.7.1<\/p>\n<p>If you&#8217;re not familiar with lambda expressions we highly recommend to take a look at our <a href=\"http:\/\/smartfoxserver.com\/blog\/server-side-extensions-with-lambda-expressions\/\">introductory article<\/a> where we illustrate the basics and show how to integrate the feature in your server side code.<\/p>\n<p>In this new article we will focus on the client side of things but, before getting started, make sure you are already using the JDK 8 (or later) and the SFS2X Java API 1.7.1 (or higher)<!--more--><\/p>\n<h3>\u00bb Client side functional style<\/h3>\n<p>This is an example of how a simple client looks like before functional Java was supported:<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class SFSClient implements IEventListener\r\n{\r\n\tprivate SmartFox sfs;\r\n\r\n\tprivate void initSmartFox() \r\n\t{\r\n\t\tConfigData cfg = new ConfigData();\r\n\t\tcfg.setZone(&quot;BasicExamples&quot;);\r\n\t\tcfg.setDebug(true);\r\n\t\t\r\n    \tsfs = new SmartFox();\r\n    \tsfs.addEventListener(SFSEvent.CONNECTION, this);\r\n\r\n    \tsfs.connect(cfg);\r\n\t}\r\n\r\n\t@Override\r\n\tpublic void dispatch(BaseEvent event)\r\n\t{\r\n\t    if (event.getType().equals(SFSEvent.CONNECTION))\r\n\t    {\r\n\t    \tboolean success = (Boolean) event.getArguments().get(&quot;success&quot;);\r\n\t    \t\r\n\t    \tif (success)\r\n\t    \t\tSystem.out.println(&quot;We are connected&quot;);\r\n\t\t\t\r\n\t\t\telse\r\n\t\t\t\tSystem.out.println(&quot;Connection failed&quot;);\r\n\r\n\t    }\r\n\t}\t    \t\r\n}\r\n<\/pre>\n<p>When we register client events (via <em>SmartFox.addEventListner<\/em>) we need to provide an object that implements the IEventListener interface. There are a couple of ways of doing this:<\/p>\n<ul>\n<li>Creating a different class implementing IEventListener for every handler<\/li>\n<li>Creating a single IEventListener implementation that deals with all the events via a series of &#8220;if&#8221; expressions<\/li>\n<\/ul>\n<p>Both approaches have pros and cons. The former requires to create many classes and keep each handler separated which in turn can make things more complex when handlers need to talk to each other or access common state.<br \/>\nThe second approach is a more convenient way that works for simple applications but at the cost of jamming one class with all the logic for each event.<\/p>\n<p>With Java 8 lambda expressions we can improve the readability and implementation of handlers like this:<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class LambdaSFSClient\r\n{\r\n\tprivate SmartFox sfs;\r\n\r\n\tprivate void initSmartFox() \r\n\t{\r\n\t\tConfigData cfg = new ConfigData();\r\n\t\tcfg.setZone(&quot;BasicExamples&quot;);\r\n\t\tcfg.setDebug(true);\r\n\t\t\r\n    \tsfs = new SmartFox();\r\n    \tsfs.addEventListener(SFSEvent.CONNECTION, this::onConnection);\r\n\r\n    \tsfs.connect(cfg);\r\n\t}\r\n\r\n\t@Override\r\n\tpublic void onConnection(BaseEvent event)\r\n\t{   \t\r\n\t\tboolean success = (Boolean) event.getArguments().get(&quot;success&quot;);\r\n\t    \t\r\n    \tif (success)\r\n    \t\tSystem.out.println(&quot;We are connected&quot;);\r\n\t\t\r\n\t\telse\r\n\t\t\tSystem.out.println(&quot;Connection failed&quot;);\r\n\t}\t    \t\r\n}\r\n<\/pre>\n<p>Notice how we don&#8217;t need to implement the IEventListener interface in our main class and we&#8217;re now passing directly our custom method via the double colon (::) syntax.<\/p>\n<p>The double colon operator is used to reference a method so that we can pass it around as any other object. Of course we&#8217;re not limited to passing methods from the current class, but we can also reference other methods such as in the example below:<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class LambdaSFSClient\r\n{\r\n\tclass CustomHandler\r\n\t{\r\n\t\tpublic void onConnection(BaseEvent event)\r\n\t\t{   \t\r\n\t\t\tboolean success = (Boolean) event.getArguments().get(&quot;success&quot;);\r\n\t\t    \t\r\n\t    \tif (success)\r\n\t    \t\tSystem.out.println(&quot;We are connected&quot;);\r\n\t\t\t\r\n\t\t\telse\r\n\t\t\t\tSystem.out.println(&quot;Connection failed&quot;);\r\n\t\t}\r\n\t}\r\n\r\n\tprivate SmartFox sfs;\r\n\tprivate CustomHandler ch = new CustomHandler();\r\n\r\n\tprivate void initSmartFox() \r\n\t{\r\n\t\tConfigData cfg = new ConfigData();\r\n\t\tcfg.setZone(&quot;BasicExamples&quot;);\r\n\t\tcfg.setDebug(true);\r\n\t\t\r\n    \tsfs = new SmartFox();\r\n    \tsfs.addEventListener(SFSEvent.CONNECTION, ch::onConnection);\r\n\r\n    \tsfs.connect(cfg);\r\n\t}\t    \t\r\n}\r\n<\/pre>\n<p>Here we are using a custom class where we implement one or more handlers and we reference then directly, with the aforementioned double colon operator (similar to what we did in the previous example).<\/p>\n<p>There is also the possibility to inline anonymous functions (a.k.a. lambda expressions) where appropriate:<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nsfs.addEventListener(SFSEvent.CONNECTION_LOST, (event) -&gt; { System.out.println(&quot;We lost connection&quot; ); });\r\n<\/pre>\n<p>This is usually okay for relatively small sections of code, otherwise referencing a method is the way to go.<\/p>\n<h3>Other resources<\/h3>\n<p>If you&#8217;re interested in learning more about functional Java and lambda expression we highly recommend a number of external articles:<\/p>\n<ul>\n<li><a href='https:\/\/en.wikipedia.org\/wiki\/Anonymous_function' target=\"_blank\">Wikipedia: Anonymous functions<\/a>\n<li><a href='https:\/\/docs.oracle.com\/javase\/tutorial\/java\/javaOO\/lambdaexpressions.html' target=\"_blank\">Oracle: Lambda expressions<\/a>\n<li><a href='https:\/\/www.ibm.com\/developerworks\/library\/j-java8idioms7\/index.html' target=\"_blank\">IBM: Functional interfaces<\/a>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>With the release of patch 2.13.1 SmartFoxServer 2X adds support for lambda expressions in Java Extensions. Also we have added support for this new JDK 8 feature in the Java client API, starting from release 1.7.1 If you&#8217;re not familiar with lambda expressions we highly recommend to take a look at our introductory article where [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[23],"tags":[26,92,12,90],"_links":{"self":[{"href":"https:\/\/smartfoxserver.com\/blog\/wp-json\/wp\/v2\/posts\/837"}],"collection":[{"href":"https:\/\/smartfoxserver.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/smartfoxserver.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/smartfoxserver.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/smartfoxserver.com\/blog\/wp-json\/wp\/v2\/comments?post=837"}],"version-history":[{"count":8,"href":"https:\/\/smartfoxserver.com\/blog\/wp-json\/wp\/v2\/posts\/837\/revisions"}],"predecessor-version":[{"id":861,"href":"https:\/\/smartfoxserver.com\/blog\/wp-json\/wp\/v2\/posts\/837\/revisions\/861"}],"wp:attachment":[{"href":"https:\/\/smartfoxserver.com\/blog\/wp-json\/wp\/v2\/media?parent=837"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/smartfoxserver.com\/blog\/wp-json\/wp\/v2\/categories?post=837"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/smartfoxserver.com\/blog\/wp-json\/wp\/v2\/tags?post=837"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}