Extremely Serious

Month: March 2020

Sample Usage of Teeing Collector

//A Stream of Integers
var ints = Stream.of(10, 20, 30 , 40);

//Calculate the average using the Collectors.teeing method.
long average = ints.collect(
        Collectors.teeing(
                
                //Sum all of the integers in the stream.
                Collectors.summingInt(Integer::valueOf),
                
                //Count the content of the stream.
                Collectors.counting(),
                
                //Calculate the average.
                ( sum, count) -> sum / count
        )
);

System.out.println(average);

Sample HttpClient.sendAsync() Method Usage

//Create an instance of HttpClient using its builder.
HttpClient httpClient = HttpClient.newBuilder()
        .version(HttpClient.Version.HTTP_2)
        .build();

//Create an instance of HttpRequest using its builder.
HttpRequest req = HttpRequest.newBuilder(URI.create("https://www.google.com"))
            .GET()
            .build();

/* Use the httpClient.sendAsync() method and encapsulate the response
in the CompletableFuture. */
CompletableFuture<HttpResponse> resFuture =
        httpClient.sendAsync(req, HttpResponse.BodyHandlers.ofString());

//Use the resFuture.thenAccept to wait for the async response.
resFuture.thenAccept(res -> System.out.println(res.version()));

//Wait for the resFuture to to complete.
resFuture.join();

Java Switch Expression

Syntax

switch(VARIABLE) {
	case MATCH_1 [, MATCH_2][, MATCH_N] -> EXPRESSION; | THROW-STATEMENT; | BLOCK
	default -> EXPRESSION; | THROW-STATEMENT; | BLOCK
}
Token Description
VARIABLE The variable to test.
MATCH_1, MATCH_2, MATCH_N The value or values that can match the VARIABLE.
EXPRESSION A expression to execute.
THROW-STATEMENT A expression that throws an exception.
BLOCK A block of statements to execute.

If a return value is required use the yield keyword instead of return.

Example

public enum Day { 
	SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY; 
}

Day day = Day.WEDNESDAY;

int numLetters = switch (day) {
    case MONDAY, FRIDAY, SUNDAY -> 6;
    case TUESDAY -> 7;
    case THURSDAY, SATURDAY -> {
        System.out.println(8);
        yield 8;
    }
    case WEDNESDAY -> {
        System.out.println(9);
        yield 9;
    }
    default -> {
        throw new IllegalStateException("Invalid day: " + day);
    }
};

System.out.println(numLetters);

Testable Codes

  • Favor composition over inheritance.
  • Block of codes must not be more than 60 lines.
  • Cyclomatic complexity of 1 to 10 as long as you can still write unit tests.
  • Idempotent implementation.

ShowCodeDetailsInExceptionMessages Java Option

Use the following java option to have a more verbose exception message especially for NullPointerException:

-XX:+ShowCodeDetailsInExceptionMessages

For example, if we have the following snippet:

var a = null;
a.b = 1;

Running the above snippet will have the following exception:

Exception in thread "main" java.lang.NullPointerException: Cannot assign field "b" because "a" is null

Note: This is more useful if the classes were compiled with debugging info (i.e. javac -g )

Parallel Extensions

This extension calculates the most efficient way of dividing the task to the different cores available.

Note: Parallel extensions will block the calling thread.

Method Description
Parallel.For Executes for that may run in parallel.
Parallel.ForEach Executes foreach that may run in parallel.
Parallel.Invoke Executes actions that may run in parallel.

Asynchronous Programming Keywords

Async modifier

A modifier that indicates that the method can be run asynchornously.

Await keyword

Wait for a result of the asynchronous operation once the data is available without blocking the current thread. It also validates the success of the asynchronous operation. If everything is good, the execution will continue after the await keywords on its original thread.

Task.Run method

Run a task on a separate thread

ContinueWith Method

A task method that will be invoked after the running async task has returned a result.
The parameter TaskContinuationOption of this method can be use to control if the continue must be invoked if there are no exception or only with exception.

CancellationTokenSource

CancellationTokenSource is used to signal that you want to cancel an operation that utilize the token. This token be passed as a second parameter for the Task.Run method.

Dispatcher.Invoke() in WPF

Communicate with the thread that owns our UI.

ConcurrentBag Class

A thread safe collection.