<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Florian Kleedorfer</title>
    <description>The latest articles on DEV Community by Florian Kleedorfer (@fkleedorfer).</description>
    <link>https://dev.to/fkleedorfer</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F883616%2Fad65ec54-93b1-4013-846c-8f5137c01d2a.png</url>
      <title>DEV Community: Florian Kleedorfer</title>
      <link>https://dev.to/fkleedorfer</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/fkleedorfer"/>
    <language>en</language>
    <item>
      <title>Immutable Object Graph with Cycles in Java</title>
      <dc:creator>Florian Kleedorfer</dc:creator>
      <pubDate>Wed, 25 Jan 2023 15:34:11 +0000</pubDate>
      <link>https://dev.to/fkleedorfer/immutable-object-graph-with-cycles-in-java-5c5l</link>
      <guid>https://dev.to/fkleedorfer/immutable-object-graph-with-cycles-in-java-5c5l</guid>
      <description>&lt;p&gt;You are writing a Java program. You model your domain entities with objects referencing one another. You would like your objects to be immutable and therefore use &lt;code&gt;final&lt;/code&gt; members in your classes.&lt;/p&gt;

&lt;p&gt;Now you detect a problem: your object graph contains cycles. How do you instantiate your objects?&lt;/p&gt;

&lt;p&gt;To illustrate, let's assume you have a class &lt;code&gt;Parent&lt;/code&gt; and a class &lt;code&gt;Child&lt;/code&gt;, and each references the other using final members:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Parent {
        final Child child;

        final String name;

        Parent(Child child, String name) {
            this.child = child
            this.name = name;
        }

        public String toString() {
            return  name + " is the parent of " + this.child.name;
        }
    }

    static class Child {
        final Parent parent;

        final String name;

        Child(Parent parent, String name) {
            this.parent = parent;
            this.name = name;
        }

        public String toString() {
            return name + " is the child of " + this.parent.name;
        }

    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When you try to instantiate a parent/child pair, you'll see there is no way to do that.&lt;/p&gt;

&lt;p&gt;The problem is that you need the instance of Child for instantiating the parent and the instance of Parent for instantiating the child, and as these references inside the classes are final, you cannot have the one before you have the other. You somehow have to instantiate them at the same time... or somesuch.&lt;/p&gt;

&lt;p&gt;However, it can be done, and here is how (omitting all the bells and whistles you would need for it to be thread-safe or otherwise nice, just laying out the main idea):&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;use the Builder pattern to create your objects&lt;/li&gt;
&lt;li&gt;in the builder, keep a reference to the instance you return on calls to &lt;code&gt;build()&lt;/code&gt;, and allow this instance to be set via a public method, e.g., &lt;code&gt;setProduct(T instance)&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;pass the builder for class X into the constructor of X&lt;/li&gt;
&lt;li&gt;in the first line of the constructor, set the builder's instance to &lt;code&gt;this&lt;/code&gt;, ie. &lt;code&gt;builder.setProduct(this)&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;instead of passing instances of classes to the constructors, pass Builders that return the instance on &lt;code&gt;build()&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Here is the code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
public class ImmutableCyclicObjectGraphExperiment {


    static class Parent {
        final Child child;

        final String name;

        Parent(ParentBuilder builder, ChildBuilder childBuilder, String name) {
            builder.setInstance(this);
            this.child = childBuilder.build();
            this.name = name;
        }

        public String toString() {
            return  name + " is the parent of " + this.child.name;
        }
    }

    static class Child {
        final Parent parent;

        final String name;

        Child(ChildBuilder builder, ParentBuilder parentBuilder, String name) {
            builder.setInstance(this);
            this.parent = parentBuilder.build();
            this.name = name;
        }

        public String toString() {
            return name + " is the child of " + this.parent.name;
        }

    }

    static class ParentBuilder {
        ChildBuilder childBuilder;
        String name;
        Parent instance = null;

        public ParentBuilder() {
        }

        void setInstance(Parent instance){
            this.instance = instance;
        }

        Parent build() {
            if (this.instance == null) {
                this.instance = new Parent(this, this.childBuilder, this.name);
            }
            return this.instance;
        }

        public ParentBuilder child(ChildBuilder childBuilder) {
            this.childBuilder = childBuilder;
            return this;
        }

        public ParentBuilder name(String name) {
            this.name = name;
            return this;
        }
    }

    static class ChildBuilder {
        ParentBuilder parentBuilder;
        String name;
        Child instance = null;

        Child build() {
            if (this.instance == null) {
                this.instance = new Child(this, parentBuilder, name);
            }
            return this.instance;
        }

        void setInstance(Child instance) {
            this.instance = instance;
        }

        public ChildBuilder parent(ParentBuilder parentBuilder) {
            this.parentBuilder = parentBuilder;
            return this;
        }

        public ChildBuilder name(String name) {
            this.name = name;
            return this;
        }
    }


    public static void main(String[] args) {
        ParentBuilder pb = new ParentBuilder();
        ChildBuilder cb = new ChildBuilder();
        pb
            .name("Anakin")
            .child(cb);
        cb
            .name("Luke")
            .parent(pb);
        Parent p = pb.build();
        Child c = cb.build();
        System.out.println(p);
        System.out.println(c);
    }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What happens is that when &lt;code&gt;ParentBuilder.build()&lt;/code&gt; is first called in the &lt;code&gt;main()&lt;/code&gt; function, the builder calls the constructor of &lt;code&gt;Parent&lt;/code&gt; and the reference &lt;code&gt;this&lt;/code&gt; to the object being created is smuggled back out into the builder immediately. Then, in the next line of the same constructor, the &lt;code&gt;childBuilder&lt;/code&gt;, passed as a constructor parameter, is used to obtain a &lt;code&gt;Child&lt;/code&gt; instance. &lt;code&gt;childBuilder&lt;/code&gt; calls the constructor of &lt;code&gt;Child&lt;/code&gt;, passing a &lt;code&gt;ParentBuilder&lt;/code&gt; instance, which is the same we already used in the &lt;code&gt;main()&lt;/code&gt; function to begin instantiating a &lt;code&gt;Parent&lt;/code&gt;, and which already holds the smuggled reference to the &lt;code&gt;Parent&lt;/code&gt; still being created a few levels up the call stack. In the &lt;code&gt;Child&lt;/code&gt; constructor, consequently, &lt;code&gt;parentBuilder.build()&lt;/code&gt; returns the smuggled reference, which can be assigned to a final member, even though the object it references has not been fully instantiated. When &lt;code&gt;Child c = cb.build()&lt;/code&gt; is called in the &lt;code&gt;main()&lt;/code&gt; function, the instance is already fully instantiated and returned without calling the constructor.&lt;/p&gt;

&lt;p&gt;As long as this happens within the same thread and you promise not to do anything with the instances but referencing them, you should be safe.&lt;/p&gt;

&lt;p&gt;As expected, calling the &lt;code&gt;main()&lt;/code&gt; method prints this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Anakin is the parent of Luke
Luke is the child of Anakin
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Immutable and cyclic, and all the indirection necessary is done at instantiation time. Isn't that nice?&lt;/p&gt;

</description>
      <category>java</category>
      <category>immutability</category>
      <category>graph</category>
    </item>
    <item>
      <title>How to convert between any two units in Java using qudtlib</title>
      <dc:creator>Florian Kleedorfer</dc:creator>
      <pubDate>Thu, 21 Jul 2022 15:10:00 +0000</pubDate>
      <link>https://dev.to/fkleedorfer/introducing-qudtlib-55jh</link>
      <guid>https://dev.to/fkleedorfer/introducing-qudtlib-55jh</guid>
      <description>&lt;p&gt;&lt;a href="https://www.researchstudio.at/studio/smart-applications-technologies/"&gt;We&lt;/a&gt; just released version 1.0 of a new project, &lt;a href="https://github.com/qudtlib/qudtlib-java"&gt;qudtlib&lt;/a&gt;, which offers unit conversion and related functionality for over 1700 units, while being quite small: the jar-with-dependencies is ~400kB in size. The project is based on the &lt;a href="https://qudt.org"&gt;QUDT ontology&lt;/a&gt;. Currently, this is available in Java only, but more is to follow.&lt;/p&gt;

&lt;h2&gt;
  
  
  For example
&lt;/h2&gt;

&lt;p&gt;Let's say you want to convert feet into lightyears, here's what your java code would look like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    Qudt.convert(
        new BigDecimal("1"),
        Qudt.Units.FT, 
        Qudt.Units.LY);
    --&amp;gt; 3.221738542107027933386435678630668E-17ly
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;ok, now how do we know that's correct? Easy: we know that light travels about one foot per nanosecond, so if we multiply that number by the number of nanoseconds in a year, which is...&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    Qudt.convert(
        new BigDecimal("1"),
        Qudt.Units.YR, 
        Qudt.Units.NanoSEC);
    --&amp;gt; 31557600000000000ns
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;... we should get around 1. Let's try:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    Qudt.convert(
        new BigDecimal("1"),
        Qudt.Units.FT, 
        Qudt.Units.LY).getValue()
        .multiply(
            Qudt.convert(
                new BigDecimal("1"), 
                Qudt.Units.YR, 
                Qudt.Units.NanoSEC)
                   .getValue()));
    --&amp;gt; 1.01670336216396744710635782571955168476800000000000
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's about 1. Seems to work ;-)&lt;/p&gt;

&lt;h2&gt;
  
  
  The Hack
&lt;/h2&gt;

&lt;p&gt;The part of the QUDT ontology we need, including some additional data we generate, is about 3.5 MB in size. The libraries needed to access the data for providing the functionality we want have another 30+ MB. We felt that was too much for something as simple as converting celsius into fahrenheit (or feet to lightyears).&lt;/p&gt;

&lt;p&gt;So we moved all the heavy lifting into the preprocessor (ie., maven) to generate a java class that instantiates all the objects representing the relevant part of the QUDT ontology upon startup. No SPARQL queries or other RDF munging at runtime, not a single external runtime dependency, You're welcome.&lt;/p&gt;

&lt;p&gt;The big upside of this approach is that it should be relatively easy to port the solution to other languages.&lt;/p&gt;

&lt;h2&gt;
  
  
  What's on the Roadmap?
&lt;/h2&gt;

&lt;p&gt;Typescript.&lt;/p&gt;

&lt;h2&gt;
  
  
  What else?
&lt;/h2&gt;

&lt;p&gt;Well, you still don't know how many feet is a light year, so why don't you &lt;a href="https://github.com/qudtlib/qudtlib-java/tree/main/qudtlib-example"&gt;try qudtlib&lt;/a&gt; and find out?&lt;/p&gt;

</description>
      <category>unitconversion</category>
      <category>java</category>
      <category>rdf</category>
      <category>library</category>
    </item>
  </channel>
</rss>
