Get Month Name

Map the given integer to a month. Explanation For month = 1, the output should be getMonthName(month) = "Jan" For month = 0, the output should be getMonthName(month) = "invalid month" Solution import java.util.Locale; import java.text.DateFormatSymbols; public class MonthNamer { private final static INVALID_MONTH_LABEL = "invalid month"; private String getMonthName(Integer month){ if(month < 1 || month > 12){ return INVALID_MONTH_LABEL; } return new DateFormatSymbols(Locale.ENGLISH).getShortMonths()[month-1]; } public static void main(String[] args){ Integer month = 1; String result = new MonthNamer(). [Read More]

Is Pangram

Given a sentence, check whether it is a pangram or not. Explanation For sentence = “The quick brown fox jumps over the lazy dog.”, the output should be isPangram(sentence) = true. For sentence = “Hello World!”, the output should be isPangram(sentence) = false. Solution public class PangramVerifier { private static final int ASCII_ALPHABET_SUM = 2015; private Boolean isPangram(String quote){ Integer sum = quote.toUpperCase().chars().filter(x -> x > 64 & x < 91). [Read More]

Stream Merger

Let’s consider the following class: import java.util.Set; import java.util.HashSet; class InputStream { int getStreamId(){ return 0; } } class OutputStream { void emitValue(int value){} } public class StreamMerger { private Set<InputStream> streams; public StreamMerger(Set<InputStream> streams){ this.streams = streams; } public void mergeInto(OutputStream stream){ } public static void main(String[] args){ new StreamMerger(new HashSet<InputStream>()); } } Our goal is to get a collection of streams order it by stream id and then merge those elements into an output stream. [Read More]

Spring Webflux Cucumber

BDD (Behavior-driven development) is a technique very similar to implement UAT (User Acceptance Testing) in a software project. Usually is a good idea to use BDD to represent how users can define application behaviour, so in that way you can represent user stories in test scenarios aka. feature testing. This time I am going to show you how integrate Cucumber to a Spring Webflux application, Cucumber is a very powerful testing framework written in the Ruby programming language, which follows the BDD methodology. [Read More]

Spring Webflux Client

We covered the reactive web server in the previous Spring Boot Server; this time, we will see the client side. Let’s start creating a new project with Webflux and Lombok as dependencies: spring init --dependencies=webflux,lombok --build=gradle --type=gradle-project --language=java client Here is the complete build.gradle file generated: plugins { id 'java' id 'org.springframework.boot' version '3.0.6' id 'io.spring.dependency-management' version '1.1.0' } group = 'com.jos.dem.webflux' version = '0.0.1-SNAPSHOT' sourceCompatibility = 17 configurations { compileOnly { extendsFrom annotationProcessor } } repositories { mavenCentral() } dependencies { implementation('org. [Read More]

Spring Webflux Server

This post walks you through the process of creating endpoints using Spring Webflux. Please read this previous Spring Webflux Basics post before continuing with this information. We are going to use @RestController to serve data from our PersonRepository. Let’s consider the following example: package com.jos.dem.webflux.controller; import reactor.core.publisher.Mono; import reactor.core.publisher.Flux; import lombok.RequiredArgsConstructor; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; import com.jos.dem.webflux.model.Person; import com.jos.dem.webflux.repository.PersonRepository; import lombok.extern.slf4j.Slf4j; @Slf4j @RestController @RequiredArgsConstructor public class PersonController { private final PersonRepository personRepository; @GetMapping("/persons") public Flux<Person> findAll() { log. [Read More]

JFairy

Jfairy is a Java fake data generator project and this time I will show you how to use this library to generate random testing information data. First let’s create an empty basic Java project using lazybones. In order to install lazybones in your computer, please use this tool: sdkman. Then execute this command from your command line: lazybones create java-basic jfairy-workshop You should get this output: Creating project from template java-basic (latest) in 'jfairy-workshop' Java Application project template ------------------------------------ You have just created a basic Java application. [Read More]

From Anonymous to Lambda

Anonymous classes traditionally enable you to declare and instantiate a class at the same time. They are like local classes except that they do not have a name. With lambda expressions introduced in Java 8 we can move anonymous classes to lambda expressions and with that we will have less code, clean code and more assertive functionality definition. This time I will show you how to transfrom a anonymous class to lambda expression in two steps. [Read More]

Lambda Expressions

Since Java 8 we have Lambda expressions which is a function defined under some interface contract and the main purpose is to implement functional programming in Java. Basically lambda expression is an argument list follow by an arrow token and a body or code expression. Example 1: (x, y) -> x + y; Example 2: () -> "Hello World!"; Example 3: x -> x.toLowerCase(); Lambdas should be an expression, not a narrative [Read More]

Functional Interfaces

A functional interface in Java is any with @FunctionalInterface annotation and with SAM(Single Abstract Method). It was introduced to facilitate Lambda expressions. Since a lambda function can only provide the implementation for one method, it is mandatory for the functional interfaces to have only one abstract method. Java has defined a lot of functional interfaces in java.util.function package. Some of them are Consumer, Supplier, Function and Predicate. Basic Functional Interfaces Special Functional Interfaces Consumer has an accept(Object) method and represents an operation that accepts a single input argument and returns no result. [Read More]