<?xml version="1.0"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
    <channel>
        <title>beryx.org posts</title>
        <link>http://beryx.org</link>
        <atom:link type="application/rss+xml" rel="self" href="http://beryx.org/blog/rss.xml" />
        <description>beryx.org</description>
        <language>en-us</language>
        <pubDate>Wed, 21 Nov 2018 19:32:26 +0100</pubDate>
        <lastBuildDate>Wed, 21 Nov 2018 19:32:26 +0100</lastBuildDate>

        
        <item>
            <title>How to create modular jars that target a Java release before 9</title>
            <link>http://beryx.org/blog/2018-11-21-/modular-jars-targeted-at-pre-java-9/</link>
            <pubDate>Wed, 21 Nov 2018 17:54:07 +0100</pubDate>
            <author>admin@beryx.org</author>
            <guid>http://beryx.org/blog/2018-11-21-/modular-jars-targeted-at-pre-java-9</guid>
            <description>&lt;p&gt;If you’re a library author, you probably want to see your library used in a large number of applications.
One way to increase the number of applications that can include your library is to make it compatible with older Java versions.
At the same time, you may consider modularizing your library in order to make it attractive
for applications that take full advantage of the Java Platform Module System (JPMS).&lt;/p&gt;

&lt;p&gt;However, the JPMS is implemented only by Java 9 and newer versions.
So, how can you build a library that is both modularized and compatible with Java versions before 9?&lt;/p&gt;

&lt;p&gt;A modular jar is just a normal jar that includes a &lt;code class=&quot;highlighter-rouge&quot;&gt;module-info.class&lt;/code&gt; file, which represents the module descriptor.
This module descriptor is typically produced by compiling a corresponding &lt;code class=&quot;highlighter-rouge&quot;&gt;module-info.java&lt;/code&gt; file.
The module descriptor is ignored when running on Java 8 and older versions, because &lt;code class=&quot;highlighter-rouge&quot;&gt;module-info&lt;/code&gt; is not a legal Java identifier.
This means that a modular jar compatible with a pre-Java 9 release can be produced by creating a normal jar
for the target version and inserting a module descriptor into it.&lt;/p&gt;

&lt;p&gt;The standard way to do this implies invoking the java compiler twice:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;compile all sources except &lt;code class=&quot;highlighter-rouge&quot;&gt;module-info.java&lt;/code&gt; using the appropriate &lt;code class=&quot;highlighter-rouge&quot;&gt;javac&lt;/code&gt; flags to
provide compatibility with the target pre-Java 9 version.&lt;/li&gt;
  &lt;li&gt;compile &lt;code class=&quot;highlighter-rouge&quot;&gt;module-info.java&lt;/code&gt; using a Java 9+ compiler.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;maven&quot;&gt;Maven&lt;/h3&gt;

&lt;p&gt;Maven &lt;a href=&quot;https://maven.apache.org/plugins/maven-compiler-plugin/examples/module-info.html&quot;&gt;describes&lt;/a&gt; a build method along the lines of the above mentioned strategy..&lt;/p&gt;

&lt;p&gt;An alternative solution is offered by &lt;a href=&quot;https://github.com/moditect/moditect/blob/master/README.md#adding-a-module-descriptor-to-the-project-jar&quot;&gt;ModiTect&lt;/a&gt;, a Maven plugin for working with the Java 9 module system.
One of the goals provided by ModiTect is &lt;code class=&quot;highlighter-rouge&quot;&gt;add-module-info&lt;/code&gt;, which allows you to add a module descriptor to the JAR produced by the current Maven project:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-xml&quot; data-lang=&quot;xml&quot;&gt;...
&lt;span class=&quot;nt&quot;&gt;&amp;lt;plugin&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;groupId&amp;gt;&lt;/span&gt;org.moditect&lt;span class=&quot;nt&quot;&gt;&amp;lt;/groupId&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;artifactId&amp;gt;&lt;/span&gt;moditect-maven-plugin&lt;span class=&quot;nt&quot;&gt;&amp;lt;/artifactId&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;version&amp;gt;&lt;/span&gt;1.0.0.Beta1&lt;span class=&quot;nt&quot;&gt;&amp;lt;/version&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;executions&amp;gt;&lt;/span&gt;
        &lt;span class=&quot;nt&quot;&gt;&amp;lt;execution&amp;gt;&lt;/span&gt;
            &lt;span class=&quot;nt&quot;&gt;&amp;lt;id&amp;gt;&lt;/span&gt;add-module-infos&lt;span class=&quot;nt&quot;&gt;&amp;lt;/id&amp;gt;&lt;/span&gt;
            &lt;span class=&quot;nt&quot;&gt;&amp;lt;phase&amp;gt;&lt;/span&gt;package&lt;span class=&quot;nt&quot;&gt;&amp;lt;/phase&amp;gt;&lt;/span&gt;
            &lt;span class=&quot;nt&quot;&gt;&amp;lt;goals&amp;gt;&lt;/span&gt;
                &lt;span class=&quot;nt&quot;&gt;&amp;lt;goal&amp;gt;&lt;/span&gt;add-module-info&lt;span class=&quot;nt&quot;&gt;&amp;lt;/goal&amp;gt;&lt;/span&gt;
            &lt;span class=&quot;nt&quot;&gt;&amp;lt;/goals&amp;gt;&lt;/span&gt;
            &lt;span class=&quot;nt&quot;&gt;&amp;lt;configuration&amp;gt;&lt;/span&gt;
                &lt;span class=&quot;nt&quot;&gt;&amp;lt;jvmVersion&amp;gt;&lt;/span&gt;11&lt;span class=&quot;nt&quot;&gt;&amp;lt;/jvmVersion&amp;gt;&lt;/span&gt;
                &lt;span class=&quot;nt&quot;&gt;&amp;lt;module&amp;gt;&lt;/span&gt;
                    &lt;span class=&quot;nt&quot;&gt;&amp;lt;moduleInfoFile&amp;gt;&lt;/span&gt;
                        ${basedir}/src/main/java/module-info.java
                    &lt;span class=&quot;nt&quot;&gt;&amp;lt;/moduleInfoFile&amp;gt;&lt;/span&gt;                
                &lt;span class=&quot;nt&quot;&gt;&amp;lt;/module&amp;gt;&lt;/span&gt;
            &lt;span class=&quot;nt&quot;&gt;&amp;lt;/configuration&amp;gt;&lt;/span&gt;
        &lt;span class=&quot;nt&quot;&gt;&amp;lt;/execution&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;/executions&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/plugin&amp;gt;&lt;/span&gt;
...&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;ModiTect uses a clever technique to create the module descriptor without the need of a Java compiler.
It analyzes the &lt;code class=&quot;highlighter-rouge&quot;&gt;module-info.java&lt;/code&gt; file with the &lt;a href=&quot;https://github.com/javaparser/javaparser&quot;&gt;JavaParser&lt;/a&gt; and uses the
&lt;a href=&quot;https://asm.ow2.io/&quot;&gt;ASM&lt;/a&gt; bytecode manipulation framework to generate the corresponding module descriptor.
This way, it allows you to create a modular jar even if you use a pre-Java 9 compiler.&lt;/p&gt;

&lt;h3 id=&quot;gradle&quot;&gt;Gradle&lt;/h3&gt;

&lt;p&gt;Taking inspiration from ModiTect’s approach, I implemented a &lt;a href=&quot;https://github.com/beryx/badass-jar-plugin/blob/master/README.adoc&quot;&gt;Gradle plugin&lt;/a&gt;
for creating modular jars that target Java 8 or older versions.&lt;/p&gt;

&lt;p&gt;Using this plugin is straightforward: in &lt;code class=&quot;highlighter-rouge&quot;&gt;build.gradle&lt;/code&gt; you just need to add the plugin
and specify the Java version targeted by your library:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-groovy&quot; data-lang=&quot;groovy&quot;&gt;&lt;span class=&quot;n&quot;&gt;plugins&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;id&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'java'&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;id&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'org.beryx.jar'&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;version&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'1.0.0'&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;sourceCompatibility&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;1.8&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;targetCompatibility&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;1.8&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;After placing the &lt;code class=&quot;highlighter-rouge&quot;&gt;module-info.java&lt;/code&gt; file in the &lt;code class=&quot;highlighter-rouge&quot;&gt;src/main/java&lt;/code&gt; directory of your project, you can build the modular jar with:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;./gradlew jar&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;You don’t need a Java 9+ compiler to build the modular jar, because the plugin uses the same technique as ModiTect.
However, keep in mind that this technique cannot check the validity of your module descriptor.
References to nonexistent packages, modules, services, or service implementations go undetected.&lt;/p&gt;

&lt;p&gt;Therefore, it is strongly advised to validate your module descriptor by additionally building your project using a Java 9+ compiler with the &lt;code class=&quot;highlighter-rouge&quot;&gt;sourceCompatibility&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;targetCompatibility&lt;/code&gt; set accordingly.
The good news is that you don’t need to change your build script in order to do this.
The plugin lets you override the &lt;code class=&quot;highlighter-rouge&quot;&gt;sourceCompatibility&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;targetCompatibility&lt;/code&gt; values by setting the project property &lt;code class=&quot;highlighter-rouge&quot;&gt;javaCompatibility&lt;/code&gt;:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;./gradlew &lt;span class=&quot;nt&quot;&gt;-PjavaCompatibility&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;9 jar&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Note that this project property overrides both &lt;code class=&quot;highlighter-rouge&quot;&gt;sourceCompatibility&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;targetCompatibility&lt;/code&gt; with the same value.&lt;/p&gt;

&lt;p&gt;If the plugin detects that at least one of &lt;code class=&quot;highlighter-rouge&quot;&gt;sourceCompatibility&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;targetCompatibility&lt;/code&gt; has an effective value &amp;gt;= 9, it automatically applies the &lt;a href=&quot;https://github.com/zyxist/chainsaw&quot;&gt;Chainsaw&lt;/a&gt; plugin, which adds support for the JPMS.
That’s why no changes are required to your build script when running in Java 9+ compatibility mode.&lt;/p&gt;

&lt;p&gt;In the discussion above we considered that &lt;code class=&quot;highlighter-rouge&quot;&gt;sourceCompatibility&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;targetCompatibility&lt;/code&gt; are configured with pre-Java 9 values in &lt;code class=&quot;highlighter-rouge&quot;&gt;build.gradle&lt;/code&gt; and you override them with 9+ values using the &lt;code class=&quot;highlighter-rouge&quot;&gt;javaCompatibility&lt;/code&gt; project property.
You can also work the other way around: set &lt;code class=&quot;highlighter-rouge&quot;&gt;sourceCompatibility&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;targetCompatibility&lt;/code&gt; with 9+ values in &lt;code class=&quot;highlighter-rouge&quot;&gt;build.gradle&lt;/code&gt; and override them with a pre-Java 9 value (via &lt;code class=&quot;highlighter-rouge&quot;&gt;javaCompatibility&lt;/code&gt;) in order to generate a modular jar targeted to Java releases before 9.&lt;/p&gt;

&lt;p&gt;Both approaches have their pros and cons.
Choose the one that suits you the most.
The important thing is to test your library constantly on both the class-path and the module-path.
The best way to do this is to configure your continuous integration server to execute the Gradle build twice:
once with the &lt;code class=&quot;highlighter-rouge&quot;&gt;javaCompatibility&lt;/code&gt; property set and once without it.&lt;/p&gt;

&lt;p&gt;Typically, a &lt;a href=&quot;https://openjdk.java.net/projects/jigsaw/spec/sotms/#module-artifacts&quot;&gt;modular jar&lt;/a&gt; includes the &lt;code class=&quot;highlighter-rouge&quot;&gt;module-info.class&lt;/code&gt; file in its root directory.
This may cause problems for some older tools, which incorrectly process the module descriptor as if it were a normal Java class.
To provide the best backward compatibility, the plugin creates by default a &lt;a href=&quot;https://openjdk.java.net/jeps/238#Modular-multi-release-JAR-files&quot;&gt;modular multi-release jar&lt;/a&gt;
with the &lt;code class=&quot;highlighter-rouge&quot;&gt;module-info.class&lt;/code&gt; file located in &lt;code class=&quot;highlighter-rouge&quot;&gt;META-INF/versions/9&lt;/code&gt;, as shown below.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/multi-release-jar.png&quot; alt=&quot;Normal and multi-release jar&quot; width=&quot;548&quot; /&gt;&lt;/p&gt;

&lt;p&gt;If, for some reason, you don’t want to create a multi-release jar, set the &lt;code class=&quot;highlighter-rouge&quot;&gt;multiRelease&lt;/code&gt; property to false
in the &lt;code class=&quot;highlighter-rouge&quot;&gt;jar&lt;/code&gt; section of your build script:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-groovy&quot; data-lang=&quot;groovy&quot;&gt;&lt;span class=&quot;n&quot;&gt;jar&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;...&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;multiRelease&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;false&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;...&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;h3 id=&quot;examples&quot;&gt;Examples&lt;/h3&gt;

&lt;p&gt;To illustrate how to use this plugin, I implemented a &lt;a href=&quot;https://github.com/beryx-gist/badass-jar-example-nqueens&quot;&gt;Java library&lt;/a&gt;
and a &lt;a href=&quot;https://github.com/beryx-gist/badass-jar-example-nqueens-kotlin&quot;&gt;Kotlin library&lt;/a&gt; for solving the &lt;a href=&quot;https://en.wikipedia.org/wiki/Eight_queens_puzzle&quot;&gt;N-Queens problem&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Note how &lt;a href=&quot;https://github.com/beryx-gist/badass-jar-example-nqueens/blob/master/travis-build.sh&quot;&gt;Travis&lt;/a&gt; is configured in these projects to test the library on
both the module-path and the class-path:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-groovy&quot; data-lang=&quot;groovy&quot;&gt;&lt;span class=&quot;o&quot;&gt;./&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;gradlew&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PjavaCompatibility&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;11&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;--&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;no&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;daemon&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;build&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;./&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;gradlew&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;--&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;no&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;daemon&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;build&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

</description>
        </item>
        
        <item>
            <title>Add code snippets to your technical presentations</title>
            <link>http://beryx.org/blog/2018-08-01-/add-code-snippets-to-your-presentations/</link>
            <pubDate>Wed, 01 Aug 2018 00:14:42 +0200</pubDate>
            <author>admin@beryx.org</author>
            <guid>http://beryx.org/blog/2018-08-01-/add-code-snippets-to-your-presentations</guid>
            <description>&lt;p&gt;Most of my technical presentations contain code snippets, but putting nice looking code into my slides was always a tedious task.
After years of using PowerPoint, I started looking for alternatives.
There are many good ones, and I was able to find a solution that matches both my personal preferences and my typical use cases.&lt;/p&gt;

&lt;p&gt;My source code is usually Java, Groovy, or Kotlin.
For each presentation, I set up a project containing the code examples I’m going to use in my slides.
I want the presentation files to integrate seamlessly into this project.
Below are a few aspects that I considered in order to tailor my solution.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Version control&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Source code and presentation files should be managed using a version control system (VCS).
I use git, but any decent VCS will do.&lt;/p&gt;

&lt;p&gt;With a VCS, you can use branches to manage several variants of the same presentation, each one refined for a different audience.&lt;/p&gt;

&lt;p&gt;To be able to compare different versions of the presentation files, they need to be written in a text format.
I opted for &lt;a href=&quot;http://www.methods.co.nz/asciidoc/&quot;&gt;AsciiDoc&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Build tool&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Besides building the code examples, the build tool should also convert the presentation files into a slide deck.
&lt;a href=&quot;https://asciidoctor.org/&quot;&gt;Asciidoctor&lt;/a&gt; provides a &lt;a href=&quot;https://asciidoctor.org/docs/asciidoctor-gradle-plugin/&quot;&gt;Gradle plugin&lt;/a&gt;,
a &lt;a href=&quot;https://asciidoctor.org/docs/asciidoctor-maven-plugin/&quot;&gt;Maven plugin&lt;/a&gt;, and an &lt;a href=&quot;https://github.com/asciidoctor/asciidoctor-ant&quot;&gt;Ant task&lt;/a&gt; for this job.&lt;/p&gt;

&lt;p&gt;There are also several &lt;a href=&quot;https://github.com/asciidoctor/asciidoctor-backends/blob/master/README.adoc&quot;&gt;presentation backends&lt;/a&gt; to choose from: &lt;a href=&quot;http://imakewebthings.com/deck.js/&quot;&gt;deck.js&lt;/a&gt;,
&lt;a href=&quot;https://revealjs.com&quot;&gt;reveal.js&lt;/a&gt;, &lt;a href=&quot;http://paulrouget.com/dzslides/&quot;&gt;DZSlides&lt;/a&gt;, &lt;a href=&quot;http://markdalgleish.com/projects/bespoke.js/&quot;&gt;Bespoke.js&lt;/a&gt;.
The build tool should also take care of downloading and configuring the chosen backend.&lt;/p&gt;

&lt;p&gt;I settled on Gradle and deck.js.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Keeping the slide content in sync with the codebase&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To ensure that the slides always contain valid and up-to-date code, the code snippets should be retrieved directly from your codebase.
Luckily, AsciiDoc lets you &lt;a href=&quot;https://asciidoctor.org/docs/user-manual/#include-directive&quot;&gt;include&lt;/a&gt;
whole source code files or &lt;a href=&quot;https://asciidoctor.org/docs/user-manual/#include-partial&quot;&gt;only portions&lt;/a&gt; of them in your presentation files.&lt;/p&gt;

&lt;p&gt;The example below shows how to add a code snippet containing the &lt;em&gt;main&lt;/em&gt; method to your slides.&lt;/p&gt;

&lt;p&gt;&lt;span style=&quot;color: brown; font-style: italic;&quot;&gt;src/main/java/org/example/Hello.java&lt;/span&gt;&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-java&quot; data-lang=&quot;java&quot;&gt;&lt;span class=&quot;kn&quot;&gt;package&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;org&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;example&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Hello&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// tag::main[]&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;out&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Hello, world!&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// end::main[]&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;&lt;span style=&quot;color: brown; font-style: italic;&quot;&gt;asciidoc/presentation.adoc&lt;/span&gt;&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-asciidoc&quot; data-lang=&quot;asciidoc&quot;&gt;:sourcedir: ../src/main/java

[source,java]
----
include::{sourcedir}/org/example/Hello.java[tags=main,indent=0]
----&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;As you can see, the source code portion to be included (in our case, the &lt;em&gt;main&lt;/em&gt; method) is marked with user-defined tags appearing as comments in the code.
I would probably be annoyed if I saw these comments cluttering production code,
but I’m totally fine with them in a codebase used for presentation purposes only.&lt;/p&gt;

&lt;h3 id=&quot;putting-all-together&quot;&gt;Putting all together&lt;/h3&gt;

&lt;p&gt;To make your life easier, I created a &lt;a href=&quot;https://boothub.org/goto/org.boothub.slide-deck&quot;&gt;BootHub template&lt;/a&gt; that sets up a customized presentation project for you.
It lets you configure things like project name, presentation title, author, programming language, or the base package of your project.&lt;/p&gt;

&lt;p&gt;BootHub will generate a presentation file in accordance with your settings.
To give you a quick start, the content of this presentation file illustrates some of the AsciiDoc and deck.js features that allow you to create a great slide deck: tables, images, blockquotes, transitions, incremental reveal, source code inclusion.&lt;/p&gt;

&lt;p&gt;To build the codebase and create the slides execute:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;./gradlew build asciidoc&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Then, open the &lt;em&gt;index.html&lt;/em&gt; file found in &lt;em&gt;build/asciidoc/html5&lt;/em&gt; in your browser.&lt;/p&gt;

&lt;p&gt;Click on the image below to see the slide deck produced by a generated presentation file.&lt;/p&gt;

&lt;p&gt;&lt;span style=&quot;color: brown; font-style: italic;&quot;&gt;Sample slide deck generated by BootHub&lt;/span&gt;&lt;br /&gt;
&lt;a href=&quot;https://slide-deck.boothub.org/sample/&quot;&gt;&lt;img src=&quot;/images/slide-deck-sample.gif&quot; alt=&quot;Sample slide deck generated by BootHub&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you prefer another build tool or another presentation backend, I encourage you to adapt my template to your tastes and publish your version to BooHub.
I’m sure a lot of people will thank you for that.
See &lt;a href=&quot;https://doc.boothub.org/releases/latest/#Templates&quot;&gt;the documentation&lt;/a&gt; about writing BootHub templates.&lt;/p&gt;

&lt;p&gt;And if you want to learn more about BootHub, i recommend you &lt;a href=&quot;https://dzone.com/articles/how-to-bootstrap-your-open-source-projects&quot;&gt;this article&lt;/a&gt;.&lt;/p&gt;

</description>
        </item>
        
        <item>
            <title>Create java.awt.Color from string representation</title>
            <link>http://beryx.org/blog/2018-07-03-/Create-java-awt-color-from-string-representation/</link>
            <pubDate>Tue, 03 Jul 2018 21:37:26 +0200</pubDate>
            <author>admin@beryx.org</author>
            <guid>http://beryx.org/blog/2018-07-03-/Create-java-awt-color-from-string-representation</guid>
            <description>&lt;p&gt;When using the &lt;a href=&quot;https://github.com/beryx/text-io/blob/master/README.md&quot;&gt;Text-IO library&lt;/a&gt;, you can configure the text terminals via properties files.
Colors can be specified using standard HTML names (e.g. &lt;code class=&quot;highlighter-rouge&quot;&gt;orange&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;chocolate&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;crimson&lt;/code&gt;), hex strings (e.g. &lt;code class=&quot;highlighter-rouge&quot;&gt;#ee5533&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;#e53&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;0xEE5533&lt;/code&gt;),
&lt;em&gt;rgb&lt;/em&gt; format strings (e.g. &lt;code class=&quot;highlighter-rouge&quot;&gt;rgb(217,33,119)&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;rgba(112,25%,50%,0.9)&lt;/code&gt;),
or &lt;em&gt;hsl&lt;/em&gt; format strings (e.g. &lt;code class=&quot;highlighter-rouge&quot;&gt;hsl(240,90%,70%)&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;hsla(270,0%,100%,0.7)&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;How does Text-IO create colors from these string representations?
Until recently, the implementation used the static method &lt;a href=&quot;https://docs.oracle.com/javase/10/docs/api/javafx/scene/paint/Color.html#web(java.lang.String)&quot;&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;web(String colorString)&lt;/code&gt;&lt;/a&gt; provided by &lt;code class=&quot;highlighter-rouge&quot;&gt;javafx.scene.Color&lt;/code&gt;.
This method supports all the above mentioned formats and returns a &lt;code class=&quot;highlighter-rouge&quot;&gt;javafx.scene.Color&lt;/code&gt; instance.
Text-IO needs a &lt;code class=&quot;highlighter-rouge&quot;&gt;java.awt.Color&lt;/code&gt;, but creating one from the returned &lt;code class=&quot;highlighter-rouge&quot;&gt;javafx.scene.Color&lt;/code&gt; is straightforward.&lt;/p&gt;

&lt;p&gt;However, I was not happy with this solution.
It made my code depend on JavaFX, although Text-IO doesn’t actually use JavaFX.
And starting with JDK 11, JavaFX is no longer part of the JDK.&lt;/p&gt;

&lt;p&gt;So I decided to write a little library named &lt;a href=&quot;https://github.com/beryx/awt-color-factory/blob/master/README.md&quot;&gt;AWT Color Factory&lt;/a&gt; for creating java.awt.Color objects from string representations.
The simplest way to do this is to copy the code of the &lt;code class=&quot;highlighter-rouge&quot;&gt;javafx.scene.Color.web(String colorString)&lt;/code&gt; method and adapt it to create java.awt.Color instances.&lt;/p&gt;

&lt;p&gt;But, what about licensing?
JavaFX is released under the GPL, so my library must use the same license.
Text-IO is instead released under the Apache-2.0 license. Is it then allowed to use my library?
Yes, it is. The license under which JavaFX is released is actually not GPL, but &lt;a href=&quot;http://openjdk.java.net/legal/gplv2+ce.html&quot;&gt;GPL 2 with Classpath exception&lt;/a&gt;.
The same applies to my library.
This is very important, because the &lt;em&gt;Classpath exception&lt;/em&gt; clause gives you permission to include the &lt;em&gt;AWT Color Factory&lt;/em&gt; library in your executable code, regardless of the license under which your software is distributed.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example usage&lt;/strong&gt;&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-java&quot; data-lang=&quot;java&quot;&gt;&lt;span class=&quot;n&quot;&gt;Color&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ColorFactory&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;valueOf&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;firebrick&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;Color&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ColorFactory&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;valueOf&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;#aa38e0&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;Color&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c3&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ColorFactory&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;valueOf&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;0x40A8CC&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;Color&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c4&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ColorFactory&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;valueOf&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;rgba(112,36,228,0.9)&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;Color&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c5&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ColorFactory&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;web&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;forestgreen&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.7&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;Color&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c6&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ColorFactory&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;web&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;hsl(270,90%,70%)&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.8&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;&lt;em&gt;AWT Color Factory&lt;/em&gt; is available in JCenter and Maven Central.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Maven&lt;/strong&gt;&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-xml&quot; data-lang=&quot;xml&quot;&gt;&lt;span class=&quot;nt&quot;&gt;&amp;lt;dependency&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;groupId&amp;gt;&lt;/span&gt;org.beryx&lt;span class=&quot;nt&quot;&gt;&amp;lt;/groupId&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;artifactId&amp;gt;&lt;/span&gt;awt-color-factory&lt;span class=&quot;nt&quot;&gt;&amp;lt;/artifactId&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;version&amp;gt;&lt;/span&gt;1.0.0&lt;span class=&quot;nt&quot;&gt;&amp;lt;/version&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/dependency&amp;gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;&lt;strong&gt;Gradle&lt;/strong&gt;&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-groovy&quot; data-lang=&quot;groovy&quot;&gt;&lt;span class=&quot;n&quot;&gt;compile&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'org.beryx:awt-color-factory:1.0.0'&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;The library requires Java 7 or newer. The jar artifact is modularized under the name &lt;code class=&quot;highlighter-rouge&quot;&gt;org.beryx.awt.color&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Starting with version 3.2.0, Text-IO uses the &lt;em&gt;AWT Color Factory&lt;/em&gt; library and no longer depends on JavaFX.&lt;/p&gt;

</description>
        </item>
        
        <item>
            <title>Partial mocks for Java 8 interfaces with default methods in Spock</title>
            <link>http://beryx.org/blog/2017-05-05-/mock-default-methods-in-spock/</link>
            <pubDate>Fri, 05 May 2017 01:43:55 +0200</pubDate>
            <author>admin@beryx.org</author>
            <guid>http://beryx.org/blog/2017-05-05-/mock-default-methods-in-spock</guid>
            <description>&lt;p&gt;&lt;a href=&quot;https://github.com/spockframework/spock&quot;&gt;Spock&lt;/a&gt; 1.1 has been released today and includes a series of new features and improvements.
One of them, which is my contribution, is the ability to create partial mocks for Java 8 interfaces that contain default methods.&lt;/p&gt;

&lt;p&gt;Let’s see how it works:&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/74c77618e4caceac94e9.js&quot;&gt; &lt;/script&gt;

&lt;p&gt;By creating a spy for the &lt;em&gt;ISquare&lt;/em&gt; interface and mocking the &lt;em&gt;getLength()&lt;/em&gt; method, we are able to test the default implementation of the &lt;em&gt;getArea()&lt;/em&gt; method.&lt;/p&gt;

&lt;p&gt;Nothing surprising here.
It’s probably exactly what you expected.
But it didn’t work with Spock version 1.0 or earlier.&lt;/p&gt;

</description>
        </item>
        
        <item>
            <title>Java console applications for the Web</title>
            <link>http://beryx.org/blog/2017-01-09-/java-console-applications-for-the-web/</link>
            <pubDate>Mon, 09 Jan 2017 22:07:44 +0100</pubDate>
            <author>admin@beryx.org</author>
            <guid>http://beryx.org/blog/2017-01-09-/java-console-applications-for-the-web</guid>
            <description>&lt;p&gt;A few weeks ago I released the first version of &lt;a href=&quot;https://github.com/beryx/text-io&quot;&gt;TextIO&lt;/a&gt;, which is a library for creating Java console applications that read interactive input from the user. I encourage you to read this &lt;a href=&quot;https://dzone.com/articles/interactive-console-applications-in-java&quot;&gt;DZone Article&lt;/a&gt; and the &lt;a href=&quot;http://text-io.beryx.org&quot;&gt;TextIO documentation&lt;/a&gt; in order to learn more about this library.&lt;/p&gt;

&lt;p&gt;Today I released the version 1.7, which adds a new interesting feature: the ability to access your console application via a browser, by using a &lt;a href=&quot;http://text-io.beryx.org/releases/1.7.1/javadoc/index.html?org/beryx/textio/web/WebTextTerminal.html&quot;&gt;WebTextTerminal&lt;/a&gt;.
See the image below for an example:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/web-terminal-animated.gif&quot; alt=&quot;A console application in a Web Terminal&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Unlike the other text terminals provided by TextIO, the WebTextTerminal works only in conjunction with a web server supporting the
&lt;a href=&quot;http://text-io.beryx.org/releases/1.7.1/javadoc/index.html?org/beryx/textio/web/DataApi.html&quot;&gt;DataApi&lt;/a&gt; (such as the &lt;a href=&quot;http://text-io.beryx.org/releases/1.7.1/javadoc/index.html?org/beryx/textio/web/SparkDataServer.html&quot;&gt;SparkDataServer&lt;/a&gt;)
and a web page that contains code for accessing this API.
This is typically accomplished via &lt;a href=&quot;https://github.com/beryx/text-io/blob/8c32a37ec542d7cbfa2f458914ab3c863fb2f06f/text-io-web/src/main/resources/public-html/textterm.js&quot;&gt;textterm.js&lt;/a&gt;,
as shown in the code snippet below.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&amp;lt;div id=&quot;textterm&quot;&amp;gt;
    &amp;lt;p class=&quot;textterm-pair&quot;&amp;gt;
        &amp;lt;span class=&quot;textterm-prompt&quot;&amp;gt; &amp;lt;/span&amp;gt;
        &amp;lt;span contenteditable=&quot;true&quot; class=&quot;textterm-input&quot;&amp;gt; &amp;lt;/span&amp;gt;
    &amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;

&amp;lt;script src=&quot;textterm.js&quot;&amp;gt;&amp;lt;/script&amp;gt;
&amp;lt;script&amp;gt;
    var textTerm = TextTerm.init(document.getElementById(&quot;textterm&quot;));
&amp;lt;/script&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Look at the source code of &lt;a href=&quot;https://github.com/beryx/text-io/blob/8c32a37ec542d7cbfa2f458914ab3c863fb2f06f/text-io-demo/src/main/java/org/beryx/textio/demo/WebTextIoExecutor.java&quot;&gt;WebTextIoExecutor.java&lt;/a&gt;
and &lt;a href=&quot;https://github.com/beryx/text-io/blob/8c32a37ec542d7cbfa2f458914ab3c863fb2f06f/text-io-demo/src/main/resources/public-html/web-demo.html&quot;&gt;web-demo.html&lt;/a&gt; for more usage details.&lt;/p&gt;

&lt;p&gt;Currently, only WebKit-based browsers (such as Chrome, Opera or Safari) are able to mask input strings.
Keep this in mind when working with sensitive data.&lt;/p&gt;

</description>
        </item>
        
        <item>
            <title>Shuffling very large sequences</title>
            <link>http://beryx.org/blog/2016-10-16-/shuffling-large-sequences/</link>
            <pubDate>Sun, 16 Oct 2016 23:18:43 +0200</pubDate>
            <author>admin@beryx.org</author>
            <guid>http://beryx.org/blog/2016-10-16-/shuffling-large-sequences</guid>
            <description>&lt;p&gt;A few days ago I released the first version of the &lt;a href=&quot;https://github.com/beryx/streamplify&quot;&gt;Streamplify&lt;/a&gt; library, which offers a series of useful streams (most of them related to combinatorics) and other utilities for building streams that allow efficient parallel processing.&lt;/p&gt;

&lt;p&gt;I will not give an overview of the Streamplify library, because I already published an &lt;a href=&quot;https://dzone.com/articles/streamplify-your-code&quot;&gt;article on DZone&lt;/a&gt; about it.
Instead, I will discuss the ability to shuffle the streams created with this library.&lt;/p&gt;

&lt;p&gt;Shuffling a sequence is not a trivial task if the number of elements in the sequence is very large.
Still, such large cardinalities occur frequently when working with combinatorics streams.
Take for example the stream of all possible arrangements of the playing cards in a standard 52-card deck.
There are 52! arrangements, which is a 68-digit number roughly equal to the number of atoms in our galaxy.
Such huge numbers make impossible to store the elements of a stream.
They have to be generated the moment they are needed.&lt;/p&gt;

&lt;p&gt;The elements of a stream provided by Streamplify can be generated based on the value of the previous element in the stream or based on their &lt;em&gt;rank&lt;/em&gt;.
The rank of an element is given by its index in the stream.
Therefore, shuffling the elements of a Streamplify stream implies defining a (pseudo-)random permutation of their indices.
For a stream with N elements, this means to generate a permutation of the numbers &lt;code class=&quot;highlighter-rouge&quot;&gt;0, 1, ..., N-1&lt;/code&gt;.
The problem is that N is often astronomically high.&lt;/p&gt;

&lt;p&gt;An overview of the most common approaches for solving this problem is given in &lt;a href=&quot;http://www.christopia.net/blog/lazy-shuffled-list-generator&quot;&gt;this blog post&lt;/a&gt;.
Some of the methods described in the blog can only generate the permutation elements in a sequential way.
This means, the &lt;code class=&quot;highlighter-rouge&quot;&gt;K&lt;/code&gt; element of the permutation can be obtained only after generating the elements
&lt;code class=&quot;highlighter-rouge&quot;&gt;0, 1, ..., K-1&lt;/code&gt;.
Such methods are not appropriate for Streamplify, because the stream source is sometimes splitted in order to parallelize operations, and the value of the previous element in the stream is not available immediately after a split.
The other methods are based on &lt;a href=&quot;https://en.wikipedia.org/wiki/Format-preserving_encryption&quot;&gt;format-preserving encryption&lt;/a&gt;.
Some of them are restricted to predefined block sizes, while others require encryption keys whose generation is computationally expensive.&lt;/p&gt;

&lt;p&gt;I took a pragmatic approach and devised a &lt;a href=&quot;https://github.com/beryx/streamplify/blob/master/streamplify/src/main/java/org/beryx/streamplify/shuffler/ShufflerImpl.java&quot;&gt;shuffling algorithm&lt;/a&gt; that is fast, memory efficient and decently scatters the elements, although not in a uniformly distributed manner.
For a given index, each byte in its representation is handled separately.
First, it is XORed with its successor byte (if it exists).
Then, a permutation is applied on the resulting byte.
The permutations to be applied to each byte are pseudo-randomly generated during the initialization of the shuffler.
Each byte-permutation has only 256 elements, therefore they are generated fast.
The algorithm uses only 4 byte-permutations, applying them in circular order.
Because the BigInteger representation of a number may have a number of bits such that the most significant ones do not fit exactly in a byte, the algorithm also generates so-called bit-permutations to be applied on byte fragments.
During its initialization, the shuffler creates 7 bit-permutations, corresponding to byte fragments with 1, 2, …, 7 bits.&lt;/p&gt;

&lt;p&gt;Let’s consider that the algorithm has generated the following permutations:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;bytePerm[0]: [3C, B3, 2B, BD, 64, FF, A9, 3B, 2A, EC, 81, 57, 35, 0A, D7, E3, ...] (256 elements)
bytePerm[1]: [99, 41, F6, 8C, E9, C3, 6E, 3C, E0, 22, 89, 15, 42, BC, 4D, A3, ...] (256 elements)
bytePerm[2]: [C5, 14, 42, 8C, 6A, 9F, 85, 86, 7C, 0B, 7A, C7, 93, 3D, BA, 40, ...] (256 elements)
bytePerm[3]: [D9, 74, 6C, 92, E8, DE, 07, C6, DA, 56, A0, F3, BA, A3, CC, 87, ...] (256 elements)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;bitPerm[1]: [01, 00]
bitPerm[2]: [03, 00, 01, 02]
bitPerm[3]: [02, 05, 06, 03, 04, 00, 07, 01]
bitPerm[4]: [0D, 08, 0E, 00, 05, 0A, 09, 0B, 07, 03, 01, 0F, 06, 0C, 04, 02]
bitPerm[5]: [1D, 04, 18, 13, 11, 02, 10, 0A, 1A, 03, 00, 0F, 17, 05, 06, 12, ...] (32 elements)
bitPerm[6]: [06, 29, 20, 26, 01, 15, 3E, 19, 36, 07, 3F, 2E, 12, 0A, 1B, 28, ...] (64 elements)
bitPerm[7]: [15, 65, 5F, 43, 19, 60, 61, 57, 51, 56, 22, 24, 23, 6F, 6E, 21, ...] (128 elements)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;We will apply now the shuffling algorithm to the index &lt;code class=&quot;highlighter-rouge&quot;&gt;2345678765432&lt;/code&gt; of a sequence with &lt;code class=&quot;highlighter-rouge&quot;&gt;7654321234567&lt;/code&gt; elements.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;count: 7654321234567 = [06 F6 29 19 22 87] (43 bits = 5 bytes + 3 bits )
index: 2345678765432 = [02 22 25 59 7D 78]
idx &amp;lt;-- byte[5]       = 78 ^ 00 = 78; shuffled[0] &amp;lt;-- bytePerm[0][78] = FB
idx &amp;lt;-- byte[4] ^ idx = 7D ^ 78 = 05; shuffled[1] &amp;lt;-- bytePerm[1][05] = C3
idx &amp;lt;-- byte[3] ^ idx = 59 ^ 05 = 5C; shuffled[2] &amp;lt;-- bytePerm[2][5C] = 71
idx &amp;lt;-- byte[2] ^ idx = 25 ^ 5C = 79; shuffled[3] &amp;lt;-- bytePerm[3][79] = F7
idx &amp;lt;-- byte[1] ^ idx = 22 ^ 79 = 5B; shuffled[4] &amp;lt;-- bytePerm[0][5B] = 86
idx &amp;lt;-- byte[0] ^ idx = 02 ^ 5B = 01; shuffled[5] &amp;lt;-- bitPerm[3][01] &amp;lt;&amp;lt; 5 = 05 &amp;lt;&amp;lt; 5 = A0
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;On each line in the listing above you can see the two operations performed on a byte:
executing a XOR with its successor byte and applying a permutation.
In the last line a 3-bit permutation is applied, because the representation of &lt;code class=&quot;highlighter-rouge&quot;&gt;count&lt;/code&gt; requires 43 bits, thus containing a 3-bit fragment. Note that the obtained value is then shifted (8-3) = 5 positions to the left in order to join this 3-bit fragment to the other bytes.&lt;/p&gt;

&lt;p&gt;Note also that the &lt;code class=&quot;highlighter-rouge&quot;&gt;shuffled&lt;/code&gt; bytes are computed in reversed order. For example, &lt;code class=&quot;highlighter-rouge&quot;&gt;shuffled[0]&lt;/code&gt; is computed based on the value of &lt;code class=&quot;highlighter-rouge&quot;&gt;byte[5]&lt;/code&gt;.
This way, any two consecutive indices will have corresponding shuffled values that differ in their most significant byte.
This leads to a good scattering of the shuffled values.&lt;/p&gt;

&lt;p&gt;The drawback is that whenever the distance between two indices is a multiple of 256 (which implies that their least significant bytes have identical values), the most significant bytes of the shuffled values of these indices will be identical.
In order to fix this problem, the algorithm performs a XORing of neighboring bytes in the shuffled value, starting with the next-to-last right byte:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;from right to left XOR with next byte: FB C3 71 F7 86 A0 --&amp;gt; 98 63 A0 D1 26 A0
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Finally, the resulted shuffled value is shifted (8-3) = 5 positions to the right in order to obtain a number with the right magnitude:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;shuffledIndex &amp;lt;-- BigInteger([98 63 A0 D1 26 A0]) &amp;gt;&amp;gt; 5 = 167553667245728 &amp;gt;&amp;gt; 5 = 5236052101429
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The BigInteger representation of the shuffled value has at most the same number of bits as the representation of the maximum allowed index (that is, &lt;code class=&quot;highlighter-rouge&quot;&gt;count-1&lt;/code&gt;).
Still, it is possible that the shuffled value is greater than the maximum allowed index.
In this case, the algorithm will be run again with the shuffled value as the input index.
This process is repeated until a shuffled value in the admitted range is obtained.&lt;/p&gt;

&lt;p&gt;As mentioned before, the values generated by this algorithm are not uniformly distributed.
This can be demonstrated for example by applying the &lt;a href=&quot;https://en.wikipedia.org/wiki/Chi-squared_test&quot;&gt;Chi-squared test&lt;/a&gt;.
However, the shuffled values are nicely scattered, which means that the algorithm is good enough for most practical purposes.&lt;/p&gt;

&lt;p&gt;I wanted to see if it’s possible to observe that the shuffled values are not exactly random without using statistics tests.
Therefore I wrote a &lt;a href=&quot;https://github.com/beryx/streamplify/blob/master/streamplify-examples/src/main/java/org/beryx/streamplify/benchmark/ShufflerView.java&quot;&gt;small program&lt;/a&gt; that displays shuffled values based on their binary representations.
Each value is shown as a line with black pixels for 1s and white pixels for 0s.
One drawback of the algorithm is that the permutations are applied on each byte separately, which makes that two indices with one identical byte in the same position will also have one identical byte used in the construction of their corresponding shuffled values.
Thus, it is more probable to observe non-random patterns for indices at distances that are multiples of 256.
The figure below has five columns, each corresponding to a sequence of 256 indices.
In the first column, the indices have consecutive values.
In the second, they are an arithmetic progression with the common difference 2&lt;sup&gt;8&lt;/sup&gt; = 256.
In the third column, they are an arithmetic progression with the common difference 2&lt;sup&gt;16&lt;/sup&gt;, and so on.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/shuffle-no-xor.png&quot; alt=&quot;Visualization without XOR&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Everything looks pretty random in the above image, which means that the algorithm does a good job at hiding its flaws.
But a simple modification will change this.
Starting from left, let’s XOR each byte with its successor.
The result is shown below:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/shuffle-xor.png&quot; alt=&quot;Visualization without XOR&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Except for the first column, where indices have consecutive values, the other ones exhibit non-random patterns.
The number of stripes grows with the exponent used for the common difference in the arithmetic progressions.&lt;/p&gt;

&lt;p&gt;Should you worry about this?
No, unless you intend to use the shuffling algorithm for hardcore scientific research.
In practice, it is very unlikely that you will observe any non-random behavior, so use the shuffler along with all the other goodies provided by Streamplify.&lt;/p&gt;

</description>
        </item>
        
        <item>
            <title>Logic for logic-less templates</title>
            <link>http://beryx.org/blog/2016-07-07-/logic-for-logic-less-templates/</link>
            <pubDate>Thu, 07 Jul 2016 20:36:12 +0200</pubDate>
            <author>admin@beryx.org</author>
            <guid>http://beryx.org/blog/2016-07-07-/logic-for-logic-less-templates</guid>
            <description>&lt;p&gt;There are many situations where content needs to be created automatically.
The most common case is probably the dynamic generation of web pages, for which template engines are a popular choice.
Some template engines take a logic-less approach, by denying or discouraging the use of logic in templates.
Examples of logic-less template systems are &lt;a href=&quot;http://mustache.github.io/&quot;&gt;Mustache&lt;/a&gt; and &lt;a href=&quot;http://handlebarsjs.com/&quot;&gt;Handlebars&lt;/a&gt;, for which Java implementations are also available: &lt;a href=&quot;https://github.com/spullara/mustache.java&quot;&gt;Mustache.java&lt;/a&gt; and &lt;a href=&quot;http://jknack.github.io/handlebars.java/&quot;&gt;Handlebars.java&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Logic-less templates seem to &lt;a href=&quot;http://stackoverflow.com/questions/3896730/whats-the-advantage-of-logic-less-template-such-as-mustache&quot;&gt;polarize&lt;/a&gt; &lt;a href=&quot;http://www.boronine.com/2012/09/07/Cult-Of-Logic-less-Templates/&quot;&gt;opinions&lt;/a&gt;.
Proponents see them as the best way to achieve a clear separation of concerns in MVC architectures.
Opponents argue that forcing the controller to do data-massaging before sending the data to the UI is actually a bad thing, because it forces the controller to be aware of the presentation logic.&lt;/p&gt;

&lt;p&gt;I will not take a side in this debate, because I have very little experience in developing web applications.
However, template engines are also useful for other purposes and I argue that there are cases, especially in non-MVC settings, where using logic in templates is legitimate. Nonetheless, putting too much logic in a template is still a bad idea.&lt;/p&gt;

&lt;p&gt;I used recently Handlebars.java for generating source code and I needed to add some logic to my templates.
Handlebars.java allows you to do this through the use of helpers, therefore I wrote the &lt;strong&gt;&lt;a href=&quot;https://github.com/beryx/handlebars-java-helpers&quot;&gt;Handlebars.java Helpers&lt;/a&gt;&lt;/strong&gt; library, which provides a series of helpers for common use cases.
Most of them are basic helpers that can be used as subexpressions in the built-in block helpers of Handlebars.java.
This lets you write the template logic in a fluent way.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Given the following YAML model:&lt;/p&gt;
&lt;pre&gt;
birthYear: 1997
&lt;/pre&gt;

&lt;p&gt;and the following template:&lt;/p&gt;

&lt;pre&gt;
{{def 'fifteenYear' (math birthYear '+' 15)}}
{{#ifb (or
    (and
        (compare (math fifteenYear '%' 4) '==' 0)
        (compare (math fifteenYear '%' 100) '!=' 0)
    )
    (compare (math fifteenYear '%' 400) '==' 0)
   )
}}
Your fifteenth anniversary was in a leap year!
{{else}}
Your fifteenth anniversary was in a non-leap year!
{{/ifb}}
&lt;/pre&gt;

&lt;p&gt;The resulting text will be:&lt;/p&gt;
&lt;pre&gt;
Your fifteenth anniversary was in a leap year!
&lt;/pre&gt;

&lt;p&gt; 
&lt;br /&gt;
If you use Handlebars.java, you may find this library useful, so read its &lt;strong&gt;&lt;a href=&quot;http://handlebars-java-helpers.beryx.org/&quot;&gt;documentation&lt;/a&gt;&lt;/strong&gt; and give it a try!&lt;/p&gt;

</description>
        </item>
        
        <item>
            <title>In-memory URL content</title>
            <link>http://beryx.org/blog/2016-06-02-/in-memory-url-content/</link>
            <pubDate>Thu, 02 Jun 2016 23:27:32 +0200</pubDate>
            <author>admin@beryx.org</author>
            <guid>http://beryx.org/blog/2016-06-02-/in-memory-url-content</guid>
            <description>&lt;p&gt;A few days ago I had to write a suite of integration tests, where each test case required a URL as input data.
In my first implementation, I stored the URL content of each test case in a separate file and used the corresponding file URL as input data.
However, I was not happy with this solution, because in order to understand what a test case does, one had to open the associated file containing the URL content.
For my test cases, each URL content consisted of only a few lines of text, so I wanted to keep the URL content in the source code of the test case, as a string.&lt;/p&gt;

&lt;p&gt;One idea was that each test case implementation will create a temporary file containing its associated content string.
But this was boring, so I started thinking about providing a URL with a custom protocol, which will retrieve its content from memory.
One way to do this is to escape the content and store it as the URL’s query part.
However, many browsers and servers have &lt;a href=&quot;http://stackoverflow.com/a/417184/2039982&quot;&gt;a limit of about 2000 characters&lt;/a&gt; for the length of a URL.
Because I did not want to restrict the number of characters allowed for the URL content, I decided to keep the URL contents as values in a static map with integer indexes.
You can see the implementation below:&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/6586ace06fdb4f56f374daa0e3f1e1b1.js&quot;&gt; &lt;/script&gt;

&lt;p&gt;The above implementation is in Groovy, but it can be easily ported to Java.&lt;/p&gt;

&lt;p&gt;Of course, this solution works only if the module that retrieves the URL content runs in the same JVM as the code that created the URL.&lt;/p&gt;

&lt;p&gt;In my integration test suite, I used a relatively small number of URLs with a small content size, therefore the growing size of the inMemoryMap was of no concern.
But if your use case involves a huge number of URLs and large content sizes, you should add housekeeping capabilities to the InMemoryProtocol.&lt;/p&gt;

</description>
        </item>
        
        <item>
            <title>JavaFX controls and skins</title>
            <link>http://beryx.org/blog/2016-05-30-/javafx-controls-and-skins/</link>
            <pubDate>Mon, 30 May 2016 23:48:26 +0200</pubDate>
            <author>admin@beryx.org</author>
            <guid>http://beryx.org/blog/2016-05-30-/javafx-controls-and-skins</guid>
            <description>&lt;p&gt;&lt;a href=&quot;https://wiki.openjdk.java.net/display/OpenJFX/UI+Controls+Architecture&quot;&gt;JavaFX controls follow the MVC pattern&lt;/a&gt;, which allows multiple views for the same model.
Therefore, multiple &lt;a href=&quot;https://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/Skin.html&quot;&gt;Skins&lt;/a&gt; can be provided for a &lt;a href=&quot;https://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/Control.html&quot;&gt;Control&lt;/a&gt;.
A good example is the &lt;a href=&quot;https://github.com/HanSolo/Medusa&quot;&gt;Medusa library&lt;/a&gt;, which offers a gauge control with about 30 different skins.&lt;/p&gt;

&lt;p&gt;I wanted to play a bit with JavaFX controls and skins, so I decided to write my own gauge control.
My goal was to keep the code simple and easy to understand, underlining the separation between model (the gauge) and views (the skins).
The result is the &lt;a href=&quot;https://github.com/beryx/jfxgauge&quot;&gt;JFXGauge library&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;JFXGauge provides only two skins.
The first one is probably the most simple skin you can think of.
It consists only of a &lt;a href=&quot;https://docs.oracle.com/javase/8/javafx/api/javafx/scene/text/Text.html&quot;&gt;Text&lt;/a&gt; component.
However, using CSS styling you can customize even this skin in many interesting ways:&lt;br /&gt;
&lt;img src=&quot;https://github.com/beryx/jfxgauge/raw/master/doc/img/textDemo-thumbnail.gif&quot; alt=&quot;text skin&quot; /&gt;&lt;br /&gt;
&lt;a href=&quot;https://github.com/beryx/jfxgauge/raw/master/doc/img/textDemo.gif&quot;&gt;(click for animated gif)&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The second skin is a thermometer skin and is more complex:&lt;br /&gt;
&lt;img src=&quot;https://github.com/beryx/jfxgauge/raw/master/doc/img/thermoDemo-thumbnail.gif&quot; alt=&quot;thermo skin&quot; /&gt;&lt;br /&gt;
&lt;a href=&quot;https://github.com/beryx/jfxgauge/raw/master/doc/img/thermoDemo.gif&quot;&gt;(click for animated gif)&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;For this skin I also defined a few &lt;a href=&quot;https://wiki.openjdk.java.net/display/OpenJFX/CSS+API+to+support+custom+UI+Controls&quot;&gt;styleable properties&lt;/a&gt; that allow configuring the thermometer’s aspect ratio and the position of the range and threshold markings.
Implementing styleable properties involves writing boilerplate code, but Hendrik Ebbers’ &lt;a href=&quot;http://www.guigarage.com/2014/03/javafx-css-utilities&quot;&gt;CSS Helper&lt;/a&gt; reduces the amount needed.&lt;/p&gt;

&lt;p&gt;JFXGauge is available in Maven Central and JCenter.&lt;/p&gt;

&lt;p&gt;Try the demo application included in the &lt;a href=&quot;https://github.com/beryx/jfxgauge/releases/latest&quot;&gt;latest release&lt;/a&gt;, read the &lt;a href=&quot;http://jfxgauge.beryx.org&quot;&gt;documentation&lt;/a&gt; and use the gauge in your projects!&lt;/p&gt;

</description>
        </item>
        

    </channel>
</rss>
