Tuesday, February 16, 2021

Developing a Module with Java 9 in Eclipse IDE, Part 1

Core Java, Java 9, Oracle Java Tutorial and Material, Oracle Java Certification, Java Career

The main objectives of Project Jigsaw were as follows:

◉ Make it easier to construct and maintain Java libraries and large applications for Java SE and Java EE platforms.

◉ Improve application performance by enabling scaling down of Java SE platform and JDK.

The JSR 376 specification defines a module system for the following requirements of large Java applications:

◉ Reliable Configuration: One of the problems with the class-path mechanism for making program components available to a Java application is that it does not define the relationships between components, making it difficult to ascertain if all the necessary components have been specified. If a required component is missing, it may not be known and, when the component is made use of or invoked in an application, an error results. With JSR 376, dependency on other components is declared so that all the needed components are available when an application is run. Another issue with the class-path mechanism which is fixed with JSR 376 is that it allows classes in the same package to be loaded from different components, resulting in error.

◉ Strong Encapsulation: JSR 376 has the provision to let a component declare which of its packages or public types are accessible and which are not. Previously, the access-control mechanism allows access to any internal packages of a component.

JSR 376 provides the following additional benefits:

◉ Scalability: Modularized custom configurations may be developed that consist of only the minimum required set of components and define minimum required functionality.

◉ Platform Integrity: The encapsulation provided by the JSR prevents access to the internal APIs of Java libraries, which was not needed to start with but not prevented, either.

◉ With class dependencies clearly defined, program optimization may be applied.

What Is a Module?

A module is a named set of Java packages, resources, and native libraries. A module could depend on other module/s, and a module declares which other modules are required to compile and run the code in the packages in the module. A module also declares which of its packages are exported for use by other modules and which are not. A module declaration is made with module, a new keyword in Java SE 9. A module consists of two source files: the module-info.java file for a module declaration, and the main class file for the Main class declaration. The source code for the two files is in a directory by the same name as the module by convention.

Class-path vs. Module System Class Loading

Unlike the class-path–based class loading which loads all the classes specified in the class path, the module loads only the code of the required modules. Unlike the class-path–based class loading, the module system does not let packages by the same name cause any interference. Only the packages exported by the modules declared a dependency on are loaded for access.

Modular Platform Objectives

The objectives of the modular platform or system, as defined in the Java 9 specification, include the capability to provide for varied configurations by dividing the Java SE platform into modules that may be combined at build time, compile time, or run time. A configuration could correspond to the complete Java SE platform, or could consist of a specific set of modules. A configuration could also correspond to one of the Compact Profiles in Java SE 8.

Modular Platform Structure or Design

The module system distinguishes between standard modules and non-standard modules. Standard modules have their specification managed by the Java Community Process (JCP) and have module names starting with "java.". Non-standard modules must not have their names start with "java.". At the base of the module structure is the module java.base, which contains essential classes, such as java.lang.Object and java.lang.String. At the top of the module structure is the java.se.ee module, an aggregator module, which contains all the modules of the Java SE Platform.

Exporting Packages

By default, all packages in a module are exported. A module optionally exports specific public types in its packages with the exports directive. A module declaration may contain multiple exports directives and each exports directive must declare only one package. An exports directive provides other modules access at compile and run time to public and protected types in the package and the public and protected members of those types. A non-standard module must not export any standard API packages.

Declaring Dependency on Other Modules

Core Java, Java 9, Oracle Java Tutorial and Material, Oracle Java Certification, Java Career
A module may depend on other modules. Dependency on another module is declared with the requires directive in the module declaration. The requiresdirective introduces three new concepts:

◉ Reliable configuration

◉ Readability

◉ Accessibility

As an example, if module A requires module B, it provides reliable configuration for the presence of B. It allows A to read B; this is called readability. It allows code in A to access code B; this is called accessibility.

Implicitly declared dependence on a module is declared with the requires transitive directive. Any module that requires (with requires) a module that contains a requires transitive directive also implicitly requires the module declared in the requires transitive directive. The requires transitive directive introduces implied readability. A standard module may depend on a non-standard module, as declared with a requires directive, but it must not grant implied readability to a non-standard module as declared with requires transitive. A non-standard module may grant implied readability to a standard module.

Source: developer.com

Monday, February 15, 2021

Everyone Could Use a Buddy

Core Java, Oracle Java Tutorial and Material, Oracle Java Exam Prep, Oracle Java Preparation, Oracle Java Career

This is not about Buddy Holly, and while it’s going to cover Big O notation, it’s not about The Big O himself: Roy Orbison.

I’d like to share a problem and solution with you.

Read More: 1Z0-900: Java EE 7 Application Developer

Consider these data structures in Java (other languages are also available):

public class Element {

    private String name;

    private ElementData someData;

    private ... // other stuff

    // getters and setters etc

}

public class UserData {

    private List<Element> elements;

}

The above data object, where UserData has some elements may be a deliberately anemic data model. The data may be in this format owing to some sort of wire format – say JSON for a REST API. We may wish to consume this in our services in a variety of ways, and we shouldn’t expect the raw model itself to get complicated by any of the needs of a service.

However, the problem with the above is that repeated lookups of an element by name would be time consuming:

public Optional<Element> getByName(String name) {

    for (Element element : elements) {

        if (element.getName().equals(name)) {

            return Optional.of(element);

        }

    }

    return Optional.empty();

}

Written like the above it also looks clumsy, though we can refactor it to a Stream operation:

public Optional<Element> getByName(String name) {

    return elements.stream()

        .filter(element -> 

           element.getName().equals(name))

        .findFirst()

}

And though that looks nicer (to me at least), it’s still fundamentally slow – after the first one!

If we wanted to do one search of these elements, then it doesn’t really matter. If, however, we happen to have a task that is intended to take each element by its different name and do something with that, then we run into a problem.

The search big O of a list is n. In other words, searching a list takes the whole of the list’s size to determine whether the element is in there (unless you get lucky and it’s in the first position).

If we’re doing the worst case of processing every element, but choosing them by their name/identity, then a data set of size n ends up with an n-squared complexity. In other words with, say, 8 entries, we’ve got approximately 8 x 8 = 64 operations to do on the list.

This is not incredibly efficient, and it would be better for this use case if the items were in a Map like structure. However, we don’t want the plain data object to carry this map around, as it’s not necessarily the job of the data object to optimise such a look up, and the pure data structure shouldn’t be concerned with this use case.

There are two elements of what I consider to be a nice solution here:

◉ Externalise an algorithm to produce an appropriate lookup for use cases when we want to do this sort of thing

◉ Give the data object a factory method to produce the lookup object, which a caller can use: this buddy is a good friend of the source object, so knows how to produce the useful view, and is also a nice ambassador to consumers that need this use case

So let’s define a class ElementLookup:

public class ElementLookup {

    private Map<String, Element> elements;

    public ElementLookup(List<Element> elements) {

        this.elements = produceLookupFrom(elements);

    }

    public Optional<Element> getByName(String name) {

        // just look it up

        return Optional.ofNullable(elements.get(name));

    }

}

We can put the factory method in the class in which we want to do looking up:

public class UserData {

    private List<Element> elements;

    // if you want to do a lookup

    public ElementLookup createLookup() {

        // this object has control of its internals

        // and is passing them to its buddy

        return new ElementLookup(elements);

    }

}

Which means it’s easy to do lookups with the above object:

UserData userData = someData();

// for some use cases this is still fine

Optional<Element> gotTheSlowWay = 

    userData.getByName("myelement");

// for several gets

ElementLookup lookup = userData.createLookup();

Optional<Element> el1 = lookup.getByName("thing1");

Optional<Element> el2 = lookup.getByName("thing2");

... etc

So how do we build the map?

This is possibly smaller than you might expect:

private static Map<String, Element> produceLookupFrom(

        List<Element> elements) {

    return elements.stream()

        .collect(toMap(element -> element.getName(),

          Function.identity());

}

Core Java, Oracle Java Tutorial and Material, Oracle Java Exam Prep, Oracle Java Preparation, Oracle Java Career
What’s nice about this is it’s easy to use, it’s made of small pieces, and it’s low impact to an anemic data object.

The lookup could always be made away from the data object with the same techniques, but it seems like a friendly thing for this sort of object to be able to do for us.

So What’s The Big O?

The big O of a single search in the list is n. If we were always going to search for every item this way, then that means it would be an n-squared.

The cost of producing the lookup is also of complexity n. However, we can assume that the complexity of looking up from the completed lookup table is 1. The HashMap is probably so efficient that items can either be present in one place, or are absent.

Source: javacodegeeks.com

Friday, February 12, 2021

Testing with Hoverfly and Java Part 6: JSON and JsonPath matchers

Core Java, Oracle Java Exam Prep, Oracle Java Tutorial and Material, Java Preparation, Java Learning, Oracle Java Career

Previously we used the XML and Xpath Hoverfly matchers.

On this blog we shall focus on rules that assist us with the data exchanged using Json.

The default Json matcher will compare the Json submitted with the Json expected. This means that the submitted Json shall be validated for all the elements and their value. New lines or any extra spaces as long as they don’t change the information that the JSON carries, will not prevent the request from being a success.

Let’s put our initial configuration that will make the Json match.

@BeforeEach

    void setUp() {

        var simulation = SimulationSource.dsl(service("http://localhost:8085")

                .post("/json")

                .body(RequestFieldMatcher.newJsonMatcher("{\"document\":\"document-a\"}"))

                .willReturn(success(SUCCESS_RESPONSE, "application/json"))

                .post("/json/partial")

                .body(RequestFieldMatcher.newJsonPartialMatcher("{\"document\":\"document-a\"}"))

                .willReturn(success(SUCCESS_RESPONSE, "application/json"))

                .post("/jsonpath")

                .body(RequestFieldMatcher.newJsonPathMatch("$.document[1].description"))

                .willReturn(success(SUCCESS_RESPONSE, "application/json"))

        );

        var localConfig = HoverflyConfig.localConfigs().disableTlsVerification().asWebServer().proxyPort(8085);

        hoverfly = new Hoverfly(localConfig, SIMULATE);

        hoverfly.start();

        hoverfly.simulate(simulation);

    }

    @AfterEach

    void tearDown() {

        hoverfly.close();

    }

In our first example we will try to match the Json of our request with the Json expected.

@Test

    void testJsonExactMatch() {

        var client = HttpClient.newHttpClient();

        var exactRequest = HttpRequest.newBuilder()

                .uri(URI.create("http://localhost:8085/json"))

                .POST(HttpRequest.BodyPublishers.ofString("   {\"document\":    \"document-a\"}"))

                .build();

        var exactResponse = client.sendAsync(exactRequest, HttpResponse.BodyHandlers.ofString())

                .thenApply(HttpResponse::body)

                .join();

        Assertions.assertEquals(SUCCESS_RESPONSE, exactResponse);

    }

Also let’s make sure there is going to be a failure on an extra element.

@Test

    void testJsonNoMatch() {

        var client = HttpClient.newHttpClient();

        var exactRequest = HttpRequest.newBuilder()

                .uri(URI.create("http://localhost:8085/json"))

                .POST(HttpRequest.BodyPublishers.ofString("{\"doc2\":\"value\", \"document\":\"document-a\"}"))

                .build();

        var exactResponse = client.sendAsync(exactRequest, HttpResponse.BodyHandlers.ofString())

                .join();

        Assertions.assertEquals(502, exactResponse.statusCode());

    }

Now let’s see the non exact matcher.

@Test

    void testJsonPartialMatch() {

        var client = HttpClient.newHttpClient();

        var exactRequest = HttpRequest.newBuilder()

                .uri(URI.create("http://localhost:8085/json/partial"))

                .POST(HttpRequest.BodyPublishers.ofString("{\"doc2\":\"value\", \"document\":\"document-a\"}"))

                .build();

        var exactResponse = client.sendAsync(exactRequest, HttpResponse.BodyHandlers.ofString())

                .thenApply(HttpResponse::body)

                .join();

        Assertions.assertEquals(SUCCESS_RESPONSE, exactResponse);

    }

Core Java, Oracle Java Exam Prep, Oracle Java Tutorial and Material, Java Preparation, Java Learning, Oracle Java Career
So far we checked matching the whole payload. Let’s try the Jsonpath approach. The example below does match.

@Test

    void testJsonPathMatch() {

        var client = HttpClient.newHttpClient();

        var exactRequest = HttpRequest.newBuilder()

                .uri(URI.create("http://localhost:8085/jsonpath"))

                .POST(HttpRequest.BodyPublishers.ofString("{\"document\":[{\"description\":\"description-1\"},{\"description\":\"description-2\"}]}"))

                .build();

        var exactResponse = client.sendAsync(exactRequest, HttpResponse.BodyHandlers.ofString())

                .thenApply(HttpResponse::body)

                .join();

        Assertions.assertEquals(SUCCESS_RESPONSE, exactResponse);

    }

But the example below won’t match

@Test

    void testJsonPathNoMatch() {

        var client = HttpClient.newHttpClient();

        var exactRequest = HttpRequest.newBuilder()

                .uri(URI.create("http://localhost:8085/jsonpath"))

                .POST(HttpRequest.BodyPublishers.ofString("{\"document\":[{\"description\":\"description-1\"}]}"))

                .build();

        var exactResponse = client.sendAsync(exactRequest, HttpResponse.BodyHandlers.ofString())

                .join();

        Assertions.assertEquals(502, exactResponse.statusCode());

    }

That’s it we did use the Json and JsonPath matchers for the Json based data!

Wednesday, February 10, 2021

Code a Java Game with (almost) Zero Coding Skills

Oracle Java Tutorial and Material, Oracle Java Certification, Oracle Java Preparation, Oracle Java Guides

Today, the gaming industry is getting better day by day with the latest tools and technology. Video games are not only popular among children, and even elders play video games with great enthusiasm. If you are not that into games before, you must look at the graphics and animations included in today’s gaming. Gaming is an industry where there is a tremendous demand of developers to turn gaming into reality with their coding and innovative skills. For developing a game, you do not have to be professional. There are many platforms that will provide you exciting features that can be easily implemented within your games. 

In the below article, we are addressing especially to the Java developers both beginners and experts who want to develop Java game projects. This article will give you a start on how to begin and what you should be learning to develop a good game. 

1. Is Java good for developing games?

There is always a discussion about whether Java is good enough to develop innovative games. The answer may vary among different developers depending on what type of game you want to develop. Java works well for some platforms and not good enough for some platforms. Java may not work well if you’re going to publish it in the Standard app store. For running a Java application, you will require the JRE (Java Runtime Environment) and may not be available on people’s systems to run the game. So developers try to include JRE within your game or JAVA application, which can be a tedious process. But if developers tend to manage this process in a simpler way, you are good to go with running Java games on any platform.

2. Why Not Java?

Below are some reasons why Java is not suitable for game development.

◉ Earlier, the developer requires “direct access” for enhancing performance and UI. Using direct access will outdate the VM languages like Java and C#.

◉ Most gaming consoles like 360, PS3 do not come with JVM, which will not allow you to reuse the code from the PC version. Compiling C++ code is much easier than running JVM to support various gaming devices.

◉ The most commonly used gaming engines as Unreal comes with C++ bindings. You can also use some Java connectors, for example, OpenGL.

◉ For Windows PC gaming, DirectX is a necessity to run games properly. But, DirectX does not have concrete Java support.

◉ If you want to run Web-based games, you can run the games using JavaScript or Flash. You can design the games in Java using various features like GWT.

Java is primarily used to create and design Android games these days because Java is the Android platform’s primary language. Also, you create web-based games using Java in conjunction with the Flash.

3. Why Java?

Though the gaming world is diverse and you can divide the games into the below-mentioned categories. 

◉ Big games like 3D- shooters and action RPG are high budget games, including AAA-level gaming projects. C++ is used to develop these games along with the game engines. Such games are designed for a mass audience. Java is merely used for developing Big games due to the JVM requirement. But, you can use Java for creating the backend.

◉ Indie games are small and amateur game projects that do not require a large team to design and develop the game. These games require innovative scripts and subjective vision instead of including high-end graphics. One of the indie games is Minecraft, which was created by a single Java developer, and the game has gained immense popularity. Thus, we can say Java works well for Indie game projects. 

◉ Mobile games- it has gained much more popularity than PC games with various gaming applications. People mostly spend more time on their phones and like to play games in their free time. But the question is Java good for developing mobile games? The answer is a direct yes. Also, we know that Java is the primary language for creating Android applications. 

So, we can say that Java is the best suitable language for creating Indie and Android gaming applications. Not only this, Java supports and manages the high-loaded online gaming server. While learning Java, you can learn concepts for developing games. Java is a universal language that can be used anywhere like server-side, back-end mobile applications, web-based applications, and great animations. Once you are set with Java, you can easily switch to any other language efficiently. 

4. How can a beginner create their game?

Creating video games is not an easy task and requires excellent coding skills. Even if you are a beginner, you do not have to worry about creating games using Java. In this advanced technology world, we have all solutions where you can follow instructions to create small games. Below, we will highlight some options for developing games in Java.

4.1 Option 1: using CodeGym, game section

This feature allows beginners to create classical games with knowledge of Java basics. The code section on the CodeGym is free to all users. 

◉ This game section allows you to create customized video games like Minesweeper, snake, etc. some of the CodeGym games are still in development, like Moon lander, Racer.

◉ Each game consists of various sub-task with instructions. The student will write the code and run it through the auto CodeGym system based on the instruction. Students can move to the next steps if everything goes right. You can code directly to the CodeGym site with the help of a select plugin.

◉ Once you complete the last step, your game is ready to publish and share among other people. 

◉ These games are elementary and require Java initial knowledge and concepts like primitive data types, loops, branches, classes, objects, string, and structure.

◉ CodeGym is available for free. If you have Java knowledge, the CodeGym provides you the opportunity to customize games.

Oracle Java Tutorial and Material, Oracle Java Certification, Oracle Java Preparation, Oracle Java Guides
CodeGym

4.2 Option 2: For Intermediate Java developers

If you like Minecraft, then it is an excellent option to make modifications with Java. After modification, you will be able to change the game content that provides players more chances to interact with the Minecraft world. You can create small games within Minecraft. To make customized modifications to the code, you must know how to work with decompiled code and modify it.

◉ For modding, you require IDE and JDK along with modding tools. You can use Forge as one of the most popular Java Modding tools. 

◉ Java knowledge for making mods depends on how complex your modifications are. 

◉ You must have class, objects, data structures, Java collection, and threads knowledge.

◉ Minecraft is not free, and you have to pay for the Minecraft version. You have to create an account on Minecraft and get it validated using Java email validation feature.

4.3 Option 3: For experienced Java core developers

Writing Java games is not an easy task and requires good programming skills. But, today, various frameworks and library options help you to create games efficiently. A good game will need Graphics, animation, GUI, sound, and Artificial intelligence. 

◉ For rendering 2D and 3D vector graphics, you can use OpenGL API. Java offers functions and libraries for OpenGL. Java provides cross-platform libraries that are free and open-source. 

◉ libGDX can work for cross-platform games that offer the Box 2D engine for creating game physics, graphics classes, and special tools for audio. It comes with various modules to create AI-based characters. Creating games with libGDX can run on any platform and do not require different code for each platform.

Monday, February 8, 2021

Java Fibonacci Series Recursive Optimized using Dynamic Programming

A quick guide to write a java program print Fibonacci series and find the nth Fibonacci number using recursive optimized using dynamic programming.

1. Overview

In this article, we will learn how to print the fibonacci series and find the nth fibonacci number using recursive approach.

Generating a stream of Fibonacci numbers

In the following sections we will try to run the program for below scenarios.

Saturday, February 6, 2021

Java Heap Sizing in a Container: Quickly and Easily

Oracle Java Exam Preparation, Oracle Java Tutorial and Material, Oracle Java Certification, Core Java

We have seen that Java has made improvements to identify the memory based on a running environment i.e. either a physical machine or a Container (docker). The initial problem with java was that It wasn't able to figure out that it was running in a container and It used to capture the memory for whole hardware where the container was running.

Now a Java Program running in a container is able to identify the cgroup limit and assign the memory (heap) according to that, (If we do not specify the min and max heap size, which we used to define earlier). So we can run our java program in a container and utilize hardware memory properly, but can we very sure that Java program is using heap size according to cgroup definition?

We have a solution to this problem as XshowSettings:category. This is a handy HotSpot JVM flag (option for the Java launcher java) is the -XshowSettings option. This option is described in the Oracle Java launcher description page as follows:

-XshowSettings:category

Shows settings and continues. Possible category arguments for this option include the following:

all

Shows all categories of settings. This is the default value.

locale

Shows settings related to locale.

properties

Shows settings related to system properties.

vm

Shows the settings of the JVM.

[These types of flags described in the Java tech note - as https://docs.oracle.com/javase/8/docs/technotes/tools/unix/java.html ]

The showSettings flag provides lots of details about the program running in that JVM, however, our interest is to find out the memory utilization so we will stick to the argument as -> -XshowSettings:vm -version

Let's verify this by running a simple container program without specifying the cgroup flag (which informs java that the program is running in a Java container)

(I set a container memory of 100MB and my JVM sets a max heap of 3.24G)

[root java-8]# docker run -m 100MB oracle-server-jre java -XshowSettings:vm -version

VM settings:

    Max. Heap Size (Estimated): 3.24G

    Ergonomics Machine Class: server

    Using VM: Java HotSpot(TM) 64-Bit Server VM

java version "1.8.0_181"

Java(TM) SE Runtime Environment (build 1.8.0_181-b13)

Java HotSpot(TM) 64-Bit Server VM (build 25.181-b13, mixed mode)

We can clearly see that Java Heap is capturing the heap available to the hardware however the docker size was specified to 100 mb only, Now let's tell Java that the program is running inside the container and see the results.

With java version 1.8.0_181 (including unlock experimental version and Cgroup limit with a specification of memory 1GB MB) with only jdk Docker 

[root]# docker run -m 1GB oracle-server-jre java -XX:+UnlockExperimentalVMOptions -XX:+UseCGroupMemoryLimitForHeap -XshowSettings:vm -version

VM settings:

    Max. Heap Size (Estimated): 228.00M

    Ergonomics Machine Class: server

    Using VM: Java HotSpot(TM) 64-Bit Server VM

java version "1.8.0_181"

Java(TM) SE Runtime Environment (build 1.8.0_181-b13)

Java HotSpot(TM) 64-Bit Server VM (build 25.181-b13, mixed mode)

Oracle Java Exam Preparation, Oracle Java Tutorial and Material, Oracle Java Certification, Core Java
The JVM was able to detect the container has only 1GB and set the max heap to 228M, which is almost 1/4th of the total available size.

We can see that the Java Heap is set to 1/4th of the docker size and rest of the 3/4th memory is utilized by docker, but what if our container doesn't require 3/4 memory,  

If the container or the program configuration doesn't need that much of memory then this is not the correct utilization of memory. The ratio between Java heap and Container size is defined inside JVM code, so the question is - can we configure this ratio?? 

Yes, Java did a very good thing by providing another option to change the ratio of container memory and java heap size. Java introduced another flag as MaxRAMFraction ( the default value to this flag is 4, so heap size takes 1/4th of container size and we can change it by defining it explicitly)

we have defined RAM fraction =2, Which settles heap size on almost 50% of the total.

[root]# docker run -m 1GB oracle-server-jre java -XX:+UnlockExperimentalVMOptions -XX:+UseCGroupMemoryLimitForHeap -XX:MaxRAMFraction=2 -XshowSettings:vm -version

VM settings:    

    Max. Heap Size (Estimated): 455.50M

    Ergonomics Machine Class: server

    Using VM: Java HotSpot(TM) 64-Bit Server VM

java version "1.8.0_181"

Java(TM) SE Runtime Environment (build 1.8.0_181-b13)

Java HotSpot(TM) 64-Bit Server VM (build 25.181-b13, mixed mode)

The heap size reaches approximately to 50% of the total container size.

Now we can change the ratio as well according to our Java program comfort level, but this leads to another thought that is is safe to run a container, if we define this ratio to 1? (however JVM won't capture whole container memory and It leaves some memory to run some other container programs like container debug shell (docker exec) and diagnostics, OS processes, etc. If the program or container needs more memory under load then it will kill the container, so I feel defining the least ratio

-XX:MaxRAMFraction=2

 seems safe(ish), if we want to customize the default JVM ratio which seems to be the safest ratio.