DOP Underscore

If you tried accessing my post last week you were not able to do so. Sorry about that. I use WordPress and apparently it had a problem with the JetPack plugin. I started using WordPress on a different blog about a decade ago. Since then I have changed my main computer three times. It seems that each time you get a new computer some stuff is left behind. In my case I lost some connection information which prevented me from accessing the site hosting the WordPress blog and addressing it.

That said; it seems that when an issue is detected in a WordPress site that prevents a blog from functioning, the admin receives a daily message with instructions to help mitigate the problem. With the help of my son who was kind enough to retrieve credentials for this blog, I was able to mitigate the issue and get the blog up and running. Sorry about the inconvenience.

The main subject for this post is to create a Maven project to load the Underscore-java library to get ready to experiment with it and Lodash and give a try to Data Oriented Programming (DOP). I did some experimentation with C# and was not able to find much on the web. If you happen to have access to a .NET package that implements Lodash or Underscore-csharp (I just made this name up), please let me know. I would like to experiment with it.

You can learn more about underscore-java here.

I went ahead and downloaded the underscore-java software from GitHub to my C:\ drive.

The documentation shows a Maven dependency for your pom.xml file which makes the creation of a Java Maven project the way to start experimenting with DOP.

Folder:				C:\Users\johnc\workspace18\DopUnderscore
IDE:  				VSCode

Extension:			Maven for Java	v0.40.4
					Microsoft
					Manage Maven projects, execute goals, generate project from archetype, 
					improve user experience for Java developers.

I created a project to hold the Maven project that we will create shortly. For this project I will use Visual Studio Code IDE. I have used it in several Maven projects and it is simple to use. That said; you have to load the specified extension for VSCode.

At this point I need to disclose that I am a Microsoft employee working on the Azure cloud in the group that deals with generating and maintaining billing meters.

Create project:										View -> Command Palette... -> Maven: Create Maven Project
													Select an archetype...	maven-archetype-quickstart

Select version of maven-archetype-quickstart:		1.4

Input groupId of your project:						com.canessa <Enter>

Input articact Id of your project:					learn

Folder:												C:\Users\johnc\workspace18\DopUnderscore

Define value for property 'version' 1.0-SNAPSHOT:	<Enter>

Confirm properties configuration:					<Enter>

[INFO] Scanning for projects...
[INFO] 
[INFO] ------------------< org.apache.maven:standalone-pom >-------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] --------------------------------[ pom ]---------------------------------
[INFO] 
[INFO] >>> maven-archetype-plugin:3.1.2:generate (default-cli) > generate-sources @ standalone-pom >>>
[INFO] 
[INFO] <<< maven-archetype-plugin:3.1.2:generate (default-cli) < generate-sources @ standalone-pom <<<
[INFO] 
[INFO] 
[INFO] --- maven-archetype-plugin:3.1.2:generate (default-cli) @ standalone-pom ---
[INFO] Generating project in Interactive mode
[INFO] Archetype repository not defined. Using the one from [org.apache.maven.archetypes:maven-archetype-quickstart:1.4] found in catalog remote
[INFO] Using property: groupId = com.canessa
[INFO] Using property: artifactId = learn
Define value for property 'version' 1.0-SNAPSHOT: : 
[INFO] Using property: package = com.canessa
Confirm properties configuration:
groupId: com.canessa
artifactId: learn
version: 1.0-SNAPSHOT
package: com.canessa
 Y: : 	<Enter>

We need to create a Maven project using VSCode. My responses to the different prompts are documented by the previous screen capture.

After the Java-Maven project is created we can take a look at the folder/directory structure.

# **** open a command prmpt ****
Microsoft Windows [Version 10.0.22621.1265]
(c) Microsoft Corporation. All rights reserved.

# **** ****
cd workspace18
cd DopUnderscore

# **** ****
dir
02/20/2023  08:23 AM    <DIR>          learn

# **** ****
cd learn

# **** ****
dir
02/20/2023  08:21 AM             2,720 pom.xml
02/20/2023  08:21 AM    <DIR>          src
02/20/2023  08:23 AM    <DIR>          target

# *** open pom.xml and insert lines to support the undersacore-java library ****

We use a command console and get to the DopUnderscore folder which I choose to name and use for this project. Of course you can use the name of our choice.

Of interest at this point is to edit the pom.xml file to include the dependency information to the underscore-java library that we want to use.

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.canessa</groupId>
  <artifactId>learn</artifactId>
  <version>1.0-SNAPSHOT</version>

  <name>learn</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
  </properties>

  <dependencies>

    <!-- add to use the Underscore-java library -->
    <dependency>
      <groupId>com.github.javadev</groupId>
      <artifactId>underscore</artifactId>
      <version>1.86</version>
    </dependency>

    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-jar-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
        <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
        <plugin>
          <artifactId>maven-site-plugin</artifactId>
          <version>3.7.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-project-info-reports-plugin</artifactId>
          <version>3.0.0</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

The “C:\Users\johnc\workspace18\DopUnderscore\learn\pom.xml” file has been edited to include underscore-java library. I added a comment to mark the start of the edited lines.

package com.canessa;

import com.github.underscore.U;

/**
 * Main entry point.
 */
public class App 
{
    public static void main( String[] args )
    {
        System.out.println( "**** Experiment with the underscore-java library ****");
        System.out.println();

        // **** ****
        U.Builder builder = U.objectBuilder()
            .add("firstName", "James")
            .add("lastName", "Bond")
            .add("age", 57)
            .add("address", U.arrayBuilder()
                .add(U.objectBuilder()
                    .add("streetAddress", "85 Albert Embankment")
                    .add("city", "London")
                    .add("cityId", "SE11")
                    .add("state", "5AW")
                    .add("postalCode", "00007")))

            .add("phoneNumber", U.arrayBuilder()
                .add(U.objectBuilder()
                    .add("type", "home")
                    .add("number", "007-007-0007"))
                .add(U.objectBuilder()
                    .add("type", "fax")
                    .add("number", "700-700-7000")));

        // ???? ????
        System.out.println("main <<< builder.toJson:\n" + builder.toJson());
        System.out.println();

        // ???? ????
        System.out.println("main <<< builder.toXml:\n" + builder.toXml());
        System.out.println();
    }
}

This is the updated contents of the file “C:\Users\johnc\workspace18\DopUnderscore\learn\src\main\java\com\canessa\App.java” that was generated by VSCode.

In it we make use of two library calls that convert a data structure to JSON and XML formats. The reason was to make sure we are able to access the library and that the results are sensible, which they are.

Following is the current output generated by this program:

**** Experiment with the underscore-java library ****

main <<< builder.toJson:
{
  "firstName": "James",
  "lastName": "Bond",
  "age": 57,
  "address": [
    {
      "streetAddress": "85 Albert Embankment",
      "city": "London",
      "cityId": "SE11",
      "state": "5AW",
      "postalCode": "00007"
    }
  ],
  "phoneNumber": [
    {
      "type": "home",
      "number": "007-007-0007"
    },
    {
      "type": "fax",
      "number": "700-700-7000"
    }
  ]
}

main <<< builder.toXml:
<?xml version="1.0" encoding="UTF-8"?>
<root>
  <firstName>James</firstName>
  <lastName>Bond</lastName>
  <age number="true">57</age>
  <address array="true">
    <streetAddress>85 Albert Embankment</streetAddress>
    <city>London</city>
    <cityId>SE11</cityId>
    <state>5AW</state>
    <postalCode>00007</postalCode>
  </address>
  <phoneNumber>
    <type>home</type>
    <number>007-007-0007</number>
  </phoneNumber>
  <phoneNumber>
    <type>fax</type>
    <number>700-700-7000</number>
  </phoneNumber>
</root>

At this point we are ready to experiment with the actual underscore-java library which we will do in the next post.

I wanted to add a post on ChatGPT but ran out of time. Will try to generate an associated post next weekend.

If interested in the code for this post, you may find it in GitHub at DopUnderscore.

Keep in mind that one of the best ways to learn is to read and experiment until the subject matter is well understood and you are able to explain it to others.

Enjoy;

John

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.