Home » Java » Core Java » Page 29

Core Java

Give Me a break, or: How to Make Awesome Puzzlers with Java 12

Java 12 provides, in experimental form, a switch expression and new forms of the switch and break statements. There is a profusion of new syntax and semantics for constructs that may find little use—except, of course, for authors of puzzlers and certification exam questions for whom this is a wonderful gift. If you enjoy Java puzzlers and would perhaps like ...

Read More »

Functional Java by Example | Part 6 – Functions as Parameters

This is part 6 of the series called “Functional Java by Example”. The example I’m evolving in each part of the series is some kind of “feed handler” which processes documents. In previous part we tried to make our functions as pure possible by moving as much of the side-effects, such as IO, to the outside of the system. Now ...

Read More »

In JDK 9 (and well 8) and above everything can be a stream

In JDK 8 we finally got to use streams and everything was good apart from the times when the API you used couldn’t produce a stream. Then you ended up writing a wrapper class method that allowed you to convert an iterator into a Stream, because you missed streams. public static <T> Stream<T> asStream(Iterator<T> it) { return StreamSupport.stream(Spliterators.spliteratorUnknownSize(it, Spliterator.IMMUTABLE | ...

Read More »

Java: Simulating various connection problems with Toxiproxy

Simulate various connection problems with Toxiproxy and Java’s HttpURLConnection to see what kind of errors get produced: connect timed out vs. read timed out vs. connection refused … . Results: System: openjdk 11.0.1 2018-10-16 (.setConnectTimeout 1) => java.net.SocketTimeoutException: connect timed out (.setReadTimeout 1) => javax.net.ssl.SSLProtocolException: Read timed out on HTTPS, java.net.SocketTimeoutException: Read timed out on HTTP (or Toxiproxy with 5s latency or timeout ) Nothing listening at the port => ...

Read More »

Migrating from Commons CLI to picocli

Apache Commons CLI, initially released in 2002, is perhaps the most widely used java command line parser, but its API shows its age. Applications looking for a modern approach with a minimum of boilerplate code may be interested in picocli. Why is it worth the trouble to migrate, and how do you migrate your Commons CLI-based application to picocli? Picocli ...

Read More »

Behavioural Design Patterns: Mediator

Previously we had a look at the iterator pattern. The mediator pattern is way different on what it tries to achieve. It is one of the behavioural patterns and its purpose is to alter the way objects communicate with each other. Instead of the objects communicating with each other directly the mediator will handle the objects interaction. For example imagine ...

Read More »

Java optional parameters

When you design a method in a Java class, some parameters may be optional for its execution. No matter it is inside a DTO, a fat model domain object, or a simple stateless service class, optional method parameters are common. From this article you will learn how to handle optional parameters in Java. We’ll focus on regular method, class constructors ...

Read More »

In any incomprehensible situation go scripting


Scripting is one of the most popular ways to make your application adjustable for client needs right at runtime. As always, this approach brings not only good, e.g. there is a well-known trade-off between flexibility and manageability. This article is not one of those which theoretically discuss pros and cons, it practically shows different ways of how to adopt scripting ...

Read More »