Autovalue and Eclipse = success

I needed to add the folder target\…\generated_sources amon the src folders..

That was all what was needed .

Neither enablig maven annotation processor in eclipse project, installing of apt plugin  or the following snippet in pom were needed:

<annotationProcessors> <annotationProcessor>com.google.auto.value.processor.AutoValueProcessor</annotationProcessor>

</annotationProcessors–>

Introduction to Java 8

Are you new to Java?
Are you coming back to programming after a long pause?
Good!
It is the best way to enjoy Java 8 .
Forget all the old Java things and jump directly to Java 8.
I was skeptical about it , but I realize that Java 8 is a great thing.

Here we come to some reading advice:

  • General book about Java SE 8

The best available book is Horstmann – Java SE 8 for the Really Impatient (2014) .

  • About error and exception handling

My advice is to read 7 Error Handling by Michael Feathers in the book “Clean Code A Handbook of Agile Software Craftsmanship”. The author as a voice interview online as well.

Bidirectional traversing with ListIterator

Do you need a bidirectional way to skip through a list?

A rewind – forward behavior?

Traversing the list in one way and then in the opposite direction?

The Java ListIterator in the java.util package is here ready made to your help.

An iterator for lists that allows the programmer to traverse the list in either direction.

The previous() and hasPrevious() methods are available in this class and will make the trick.

You can build a ‘media tape’ behaviour on top of it.

 

It has some loose relationships with the Elevator Algorithm used for seeking a disk in its most simplest implementation strategy, the FCFS, First Come First Served. I used this one before for other purposes than seeking a disk.

ArrayList algorithm complexity, performance and gotchas

This piece of advice is about the Java ArrayList and LinkedList. It is not a insight about a general not specified List algorithm.

ArrayList is a random access data store.

LinkedList is sequential access data store.

There are these very important performance issues to know:

  • Remove: ArrayList remove(E ) is painful slow. Please consider LinkedList if this operation is used often in your application. Also the ArrayList positional remove(index) is painful bad. These operations require constant-time in a LinkedList and linear-time in an ArrayList. (*)
  • Use ArrayList constructor that take a size argument. You can measure by your self the defference when using large lists.
  • List Interface : Iterating over the elements in a list is preferable to indexing through it if the caller does not know the implementation.
  • Remember that you have the method addAll() for bulk operations.
  • Add positional add(index) is painful O(n) on ArrayList and O(1) in LinkedList (*)
  • Set positional set(index) is painful O(n) on LinkedList and O(1) in ArrayList   (*)

(*) Also the outline of the Collections Framework states that LinkedList is better than ArrayList for accessing in the middle of the list.

Also:

Do not do this:

 int a1[] = { -23, 12, 21 };
 
for (int v : a1)
           myarraylist.add(new Integer(v));

prefer this instead:

myarrayList.addAll(Arrays.asList(23,12,21)

Also

Arrays.asList

for translating arrays to strings is obsolete. Use this instead:

System.out.println(Arrays.toString(myArray));

Static Lock (ReentrantLock) and SonarQube destroyed my day

Yes, the idea of letting a module pass through a SonarQube validation throw us in putting ‘static’ on a lot of fields. A static happen to be attached to a ReentrantLock …
And many wasted hours of debugging followed because the applications using this module became crazy and not correct.
This piece of code can destry your application:

private static final Lock = new ReentrantLock();

Benchmark , Performance StringJoiner StringBuffer “+” plus operator

This is a benchmark for performance and behaviour of joining strings with:

  • StringJoiner
  • StringBuffer
  • The + operator

The result is that

StringJoiner is the winner.

StringBuffer and ‘+’ are worse and have the same performance.

package org.sample;
 
import java.util.ArrayList;
import java.util.List;
 
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.Fork;
import org.openjdk.jmh.annotations.Level;
import org.openjdk.jmh.annotations.Measurement;
import org.openjdk.jmh.annotations.Param;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.Warmup;
 
@Warmup(iterations = 10, time = 1)
@Measurement(iterations = 10, time = 1)
@State(Scope.Benchmark)
@Fork(1)
public class JoinString extends JoinStringBase {
	@Param({ "10000" })
	private int CYCLES;
 
	private final int P = 4;
 
	private List listCounter;
 
	private List listA;
	private List listC;
	private List listD;
	private List listXP;
	private List listJOINER;
 
	@Setup(Level.Trial)
	public void setUp() {
		listCounter = new ArrayList&lt;&gt;(CYCLES + 1);
 
		for (int i = 0; i &lt; CYCLES; i++) {
			listCounter.add("" + i);
		}
 
		listA = new ArrayList&lt;&gt;(CYCLES + 1);
		listC = new ArrayList&lt;&gt;(CYCLES + 1);
		listD = new ArrayList&lt;&gt;(CYCLES + 1);
		listXP = new ArrayList&lt;&gt;(CYCLES + 1);
		listJOINER = new ArrayList&lt;&gt;(CYCLES + 1);
 
		System.setProperty("java.util.concurrent.ForkJoinPool.common.parallelism", Integer.toString(P));
	}
 
	@Benchmark
	public long useStringBuffer() {
		for (int i = 0; i &lt; CYCLES; i++) {
			listC.add(bufferDateString(TEXT));
		}
		return listC.size();
	}
 
	@Benchmark
	public long plusOperator() {
		for (int i = 0; i &lt; CYCLES; i++) {
			listA.add(plusOperator(TEXT));
		}
		return listA.size();
	}
 
	@Benchmark
	public long useStringJoiner() {
		for (int i = 0; i &lt; CYCLES; i++) {
			listJOINER.add(stringJoiner(TEXT));
		}
		return listJOINER.size();
	}
}
 
 
 
import java.util.StringJoiner;
 
class JoinStringBase {
 
	private static final String TWENTY = "20";
	private static final String POINT = ".";
 
	protected static final String TEXT = "_20ge20";
 
	String stringJoiner(String pScad) {
		StringJoiner sj = new StringJoiner("");
		String day = pScad.substring(1, 3);
		String month = pScad.substring(3, 5);
		String monthInt = MONTHS.valueOf(month).month;
		String yearSuffix = pScad.substring(5, 7);
 
		sj.add(day);
		sj.add(POINT);
		sj.add(monthInt);
		sj.add(POINT);
		sj.add(TWENTY);
		sj.add(yearSuffix);
		return sj.toString();
 
	}
 
	final String bufferDateString(String pScad) {
		StringBuffer buffer = new StringBuffer();
		String day = pScad.substring(1, 3);
		String month = pScad.substring(3, 5);
		String monthInt = MONTHS.valueOf(month).month;
		String yearSuffix = pScad.substring(5, 7);
 
		buffer.append(day);
		buffer.append(POINT);
 
		buffer.append(monthInt);
		buffer.append(POINT);
 
		buffer.append(TWENTY);
		buffer.append(yearSuffix);
		return buffer.toString();
	}
 
	final String plusOperator(String pScad) {
		String day = pScad.substring(1, 3);
		String month = pScad.substring(3, 5);
		String monthInt = MONTHS.valueOf(month).month;
		String yearSuffix = pScad.substring(5, 7);
 
		String scadFormat = day + POINT + monthInt + POINT + TWENTY + yearSuffix;
		return scadFormat;
	}
 
	enum MONTHS {
		ge("01"), fb("02"), mz("03"), ap("04"), mg("05"), gn("06"), lg("07"), ag("08"), st("09"), ot("10"), nv("11"), dc("12");
 
		final String month;
 
		MONTHS(String month) {
			this.month = month;
		}
	}
 
	final String plusOperatorXP(String pScad) {
		return pScad.substring(1, 3) + POINT + MONTHS.valueOf(pScad.substring(3, 5)).month + POINT + TWENTY
				+ pScad.substring(5, 7);
	}
 
	final String bufferDateStringXP(String pScad) {
		StringBuffer buffer = new StringBuffer();
 
		String month = pScad.substring(3, 5);
 
		buffer.append(pScad.substring(1, 3));
		buffer.append(POINT);
 
		buffer.append(MONTHS.valueOf(month).month);
		buffer.append(POINT);
 
		buffer.append(TWENTY);
		buffer.append(pScad.substring(5, 7));
		return buffer.toString();
	}
 
}

 

The results in details:

blogJoinString2