One of the first topics you will encounter when studying functional programming will probably be currying. For an imperative programmer not used to mathematical notations, chances are you will find the concept hard to grasp. Then let this be the day you will remember as the day you completely understood currying!
Continue reading →
The Optional
class has the orElse
and orElseGet
methods to return a value when the Optional
object is empty. This is useful to return a default value for example. But there is a small difference between the two methods. The orElseGet
method needs a Supplier
argument that returns a value of the type of the Optional
value. The Supplier
is only invoked when the Optional
value is empty. The statement passed as argument to the orElse
method is always executed, even when the Optional
value is not empty. Preferrably we should use orElseGet
as it will only invoke statements if needed.
In the following example code we see when our method getDefaultGreeting
is invoked by using orElse
and orElseGet
with an empty and non-empty Optional
object:
Continue reading →
The time is right. Your work is done. The last letter of your Java code has been written. You let the IDE compile the code and a new running version of your app is ready to be released. You’ve done this a thousand times, there’s nothing new on the horizon. The question of what lies beneath, what happens under the hood, has never occurred to you. Until now!
Continue reading →
Apaches fluent httpclient API is a facade API to simplify the httpclients usage for standard use cases.
It’s also better readable and results in cleaner code.
In this post we’ll see how to use a custom SSLContext with the fluent API.
We’ll use the new 5.0 version because it contains some changes compared to 4.x.
Continue reading →
SDKMAN! is a very useful tool to manage versions of so-called software development kits. There are a lot of SDKs supported by SDKMAN!: Java, Groovy, Kotlin, Scala, Gradle, Maven, Leiningen, Micronaut, Grails, Vert.x, JBake, AsciidoctorJ and more. When we look at Java we can use a simple install java <version>
command from the command-line to install a version of Java on our computer. SDKMAN! will take care of downloading the Java version and setting all the correct system variables to use that Java version. With the use
command we can switch between version in the current shell we are working in. But we can even automatically switch to a specific installed Java version when we enter a directory. This is very useful when we have to work on multiple projects on our computer and each project requires a specific Java version to be used.
To support automatic switching of a Java version we must first run the env init
command in the directory of our project. This creates a new file .sdkmanrc
in the directory. The file contains the Java version that was active when we invoked the env init
command. It is a text file so we can change the Java version in the file, or regenerate the file by running the env init
command again, but with a different active Java version.
Continue reading →
In Java we can use the iterate
method of the Stream
class to create an unbounded stream based on function invocations. We pass to the iterate
method an initial value and a function that can be applied to the value. The first element in the unbounded stream is the initial value, the next element is the result of the function invocation with as argument the value from the previous element and this continues for each new element. Suppose we have a function expressed as lambda expression i → i + 2
. When we use this lambda expression with the iterate
method and a initial value of 1
we get a stream of 1
, 1 → 1 + 2
, 3 → 3 + 2
, ….
As we get an unbounded stream we must for example use limit
to get the values we want from the stream. But we can also use an extra argument for the iterate
method that is a Predicate
definition. The iterate
method will provide elements as long as the result of the Predicate
is true
. This way we the result of the iterate
method is a bounded stream.
Continue reading →
In Java we can use the generate
method of the Stream
class to create an infinite stream of values. The values are coming from a Supplier
instance we pass as argument to the generate
method. The Supplier
instance usually will be a lambda expression. To give back a fixed value we simply implement a Supplier
that returns the value. We can also have different values when we use a method that returns a different value on each invocation, for example the randomUUID
method of the UUID
class. When we use such a method we can create the Supplier
as method reference: UUID::randomUUID
.
The generate
method returns an unbounded stream. We must use methods like limit
and takeWhile
to get a bounded stream again. We must use findFirst
or findAny
to terminate the unbounded stream and get a value.
Continue reading →
Since Java 9 we can use a function as argument for the Matcher.replaceAll
method. The function is invoked with a single argument of type MatchResult
and must return a String
value. The MatchResult
object contains a found match we can get using the group
method. If there are capturing groups in the regular expression used for replacing a value we can use group
method with the capturing group index as argument.
In the following example we use the replaceAll
method and we use a regular expression without and with capturing groups:
Continue reading →
In Java we can define capturing groups in regular expression. We can refer to these groups (if found) by the index from the group as defined in the regular expression. Instead of relying on the index of the group we can give a capturing group a name and use that name to reference the group. The format of the group name is ?<name>
as first element of the group definition. The name of the group can be used with the group
method of the Matcher
class. Also we can use the name when we want to reference the capturing group for example with the replaceAll
method of a Matcher
object. The format is ${name}
to reference the group by name. Finally we can use a named capturing group also as backreference in a regular expression using the syntax \k<name>
.
In the following example we define a regular expression with named groups and use them with several methods:
Continue reading →
This week at work a colleague showed a nice feature of the Pattern
class in Java: we can easily turn a Pattern
into a Predicate
with the asPredicate
and asMatchPredicate
methods. The asPredicate
method return a predicate for testing if the pattern can be found given string. And the asMatchPredicate
return a predicate for testing if the pattern matches a given string.
In the following example code we use both methods to create predicates:
Continue reading →
When using Springfox you can annotate your endpoints to automatically generate OpenAPI docs for your clients.
This blogpost will show how you can prevent Springfox generating a model on an endpoint with ResponseEntity
as return type.
I’ll also cover how to prevent generating default responses.
Take an endpoint like below.
You want to return ResponseEntity
because you want control over the status and body which is returned within your endpoint code.
Click to see the Spingfox configuration used for this example
Now your generated OpenAPI doc contains responses with a $ref
to ResponseEntity
.
Springfox will also generate default responses for 201
, 202
, 400
, 401
, 403
, 404
, which you may never need.
Click to see the generated definition for ResponseEntity
(it is quite long)
Continue reading →
Using the Stream API and the map
method we can transform elements in a stream to another object. Instead of using the map
method we can also write a custom Collector
and transform the elements when we use the collect
method as terminal operation of the stream.
First we have an example where we transform String values using the map
method:
Continue reading →