Posts

Showing posts from February, 2020

Quick Start: Building a RESTful Web Service by Spring Tool Suite

Image
This is an alternative Quick Start tutorial of building a simple RESTful web service.  Instead of offering choices of tools, I have chosen the specific ones. What to Build Part 1: A web service that replies with a greeting; this part is primarily based on the guidance from spring.io Part 2: A web service that replies with an imaginary product price quote; an addition to the original guide Note: The replies, in JSON format, are from Tomcat Required Tools (1) Spring Tool Suite (version 4 in this tutorial) (2) JDK 8 or above (version 13 in this tutorial) PART 1: A web service that replies with a greeting Step 1 Install Spring Tool Suite, and launch it. Step 2 Fill out the New Spring Starter Project form to generate the starter codes. To launch the form: File > New > Spring Starter Project Fill out the two pages of the form as below. Page 1: Notice that Gradle type is chosen for this example. Change project Name, Group, Artifact, and Package to yours...

Parallel Stream Examples

The Stream API allows running in parallel in multiple CPU cores to speed up processing when the algorithm requires a very large number of loops.  Below are some examples that I created as mnemonic and experimental usage. Note that list.stream().parallel() can also be written as list.parallelStream() . Function < Integer , List < Integer >> getList = ( n ) -> { return Stream . iterate ( 1 , i -> i + 1 ) . limit ( n ) . collect ( Collectors . toList ()); }; List < Integer > dataList = getList . apply ( 20 ); // Run in sequence to print dataList . forEach ( x -> System . out . print ( x + " " )); System . out . println (); // Run in parallel to print dataList . stream () . parallel () . forEach ( x -> System . out . print ( x + " " )); System . out . println (); // Run in paraellel to map, collect, and print dataList . stream () ...

Quick Start: Building a Web App by Spring Tool Suite

Image
Small purple plants in springtime There are some examples on spring.io to help developers to get started with using Spring.  While the official Quick Start guide demonstrates a simple way to generate "Hello World", it requires developers to choose their own favorite Integrated Developer Environment (IDE) or lightweight editor, and build the project by Maven at the command prompt.  Here is an alternative Quick Start guide that I have chosen the specific tools. What to build A classic "Hello World!" message replied to a web browser from Tomcat server. What tools are required Spring Tool Suite (version 4 in this example) JDK (version 8 or above) Note: Unlike the official Quick Start guide, installation of Spring Tool Suite but not Maven or Gradle is required. Step 1 Install Spring Tool Suite, and launch it. Step 2 Fill out the New Spring Starter Project form to generate the starter codes. To launch the form: File > New > Spring Starter Proje...

Quick Reference & Example: Optional

Quick Reference Optional is primarily for use as a return type where there is a clear need to prevent Null Pointer Exception.  A variable of type Optional should never itself be null; it should always point to an Optional instance. Methods in the Optional class include: empty, filter, flatMap, get, ifPresent, ifPresentOrElse, isPresent, map, of, ofNullable, or, orElse, orElseGet, orElseThrow, stream An example to demonstrate: flatMap, get, isPresent, map, ofNullable, orElse Imagine that there is a car dealership offering a promotional package for selected car models.  Each Car has a model name and an optional PromoPackage .  Each PromoPackage has a promo name and an optional list of promo items. class PromoPackage { private String promoName; private Optional<List<String>> optItems; PromoPackage (String promoName, Optional<List<String>> optItems) { this .promoName = promoName; this .optItems = optItems...

Strategy Pattern by Lambda Expression - Part 2

Image
Earlier it was mentioned that the restaurant decided to add a veggie burger recipe in their menu.  The veggie burger turned out successful, and the ambitious restaurant owner decided to further expand the food menu by offering a number of pasta dishes. Assume that the developer would need to follow the original Strategy pattern, a concrete class of OperationVeggieBurger would need to be created; similarly for the pasta dishes, a PastaStrategy interface and all the corresponding concrete classes. Imagine that the restaurant would continue expanding the business by adding more food categories, say, Appetizers, Soups, Pizzas, Entrees, Deserts and so on.  The more categories to add, the more food strategy interfaces and the corresponding concrete classes would need to be created. Here is the diagram of the Strategy Pattern that contains BurgerStrategy, PastaStrategy and a potential NewFoodStrategy: Instead of following the original Strategy Pattern, the developer would li...

Strategy Pattern by Lambda Expression - Part 1

Image
Design patterns help solving common problems in designing software,  A major contribution of this knowledge was made by the book Design Patterns - Elements of Reusable Object-Oriented Software written by the "Gang of Four" in 1994, and that was a time before the initial release of Java.  In the book, there are 23 patterns documented, and one of those is called Strategy.  In this blog post, I would like to demonstrate how the Strategy pattern can be written, and how this object-oriented design can be transformed into Lambda Expression.  Since the first release in 2014, Lambda Expression has been an important feature of the Java core language. The Strategy Design Pattern As an example, let's imagine that there is a restaurant that requires organizing its workflows.  The restaurant requires having a BurgerStrategy that is an interface.  The concrete classes that implement the BurgerStrategy are OperationBeefBurger, OperationFishBurger, and OperationChicke...