Skip to main content

Posts

Maven Tips... and Tricks

Maven, one of the central actors in the Java World, resposible for managing the building life-cycles of many projects, is full of little features, that sometimes we forget to explore. Let us go straight away and take a look at some very useful Maven features that will make your builds shine. From where it stopped Sometimes it is needed to build a bunch of projects all together, artifact-a , artifact-b and so on. What do we usually do when one of them fail? Build it all again! But not anymore: By using this option you can run the build from the project that failed. Two out of ten Ok, suppose you have 10 projects, and you only want to build 2 of them, how would you do? The option -pl will do the job Multi-threaded Build If in the machine you run the build you have many Cores, tou can take advantage of them by using the following option(it means 2 Threads per Core): It is also possible to define 3 Threads per Core(T3C) Skip your Tests when you want to With a lot of test
Recent posts

The Scala's Equivalent of Java ArrayList

ArrayList   is a widely misused "default"  List  in Java. It has terrible performance when elements are added/removed frequently, but works pretty well as a replacement for  Array .  But what about Scala? What is Scala's default collection? What Scala collection has characteristics similar to  ArrayList ? What's a good replacement for  Array   in Scala? So here are the answers for these: What is Scala's default collection? Scala's equivalent of Java's  List  interface is the  Seq . A more general interface exists as well, which is the  GenSeq  -- the main difference being that a  GenSeq  may have operations processed serially or in parallel, depending on the implementation. Because Scala allows programmers to use  Seq  as a factory, they don't often bother with defining a particular implementation unless they care about it. When they do, they'll usually pick either Scala's List  or  Vector . They are both immutable, and  Vector

Enum Types in Scala - How to?

Java is well known for provinding Enum Types since its version 1.5. Powerful, simple to use, they make your code easy to understand, and provide a nice way to implement Singleton Objects , since an Java Application will have only one instance of the each Enum Type. Among some of its advantages Enums variables cannot have any other type other than those defined in the Enum class. Let us model Gender in a Java Enum Type Reviewing Java Type-Safe Enum Type In the above code, there is only to options for any variable of type Gender : MALE  or  Female. Now    what if we want model Enum Types like this in other JVM Languages? Such as Scala, our focus today. Scala , a Functional and yet Object Oriented language for the Java Platform has no built-in Enumeration type like Java's. Instead, it provides an abstract class Enumeration which can be used :

Always Use StringBuilder while concatenating Strings within loops

A common tendency of Java programmers is to always concatenate Strings using + operator. Which is actually very good, and simplifies the code by improves readability, since we would have to use StringBuilder.append(String), if the single + operator was not allowed. In fact if we look in byte code generate from such concatenation style, we will see a StringBuilder being used to perform the action. Check the JSL:    JLS Now , the point is, although this facility, you should not use the + operator in loop concatenation. Why? A new  StringBuilder  Object will be constructed at every single loop iteration (with initial value of str) and at the end of every iteration there will be concatenation with initial String (actually  StringBuilder  with initial value of  str ). So you need to create StringBuilder by yourself only when you work with String concatenation in loop. Let us procuce the evidence First, run this code, and see how long it takes to be executed: Now, bellow is th

Understanding What Instrumentation is

It would be possible to change the Java bytecode of your classes? It would be possible to create new procedures and change the body of his methods, even at runtime? Yes, the answer is yes, this is possible! Thanks to the APIs lowest level or higher level as java.lang.instrumentat and Apache BCEL among others.   For better transparency, you can always automatically generate code and add it to their classes. There are several frameworks. The java developer may be adding a simple enhancer code in their applications, which is relatively easy to do, with little intrusion into your creative process. Byte code enhancement Programs written in Java to compile a form of code called bytecode. The idea is through instrumentation, inject byte code to add services and still maintain transparency. For example, most implementations use an enhancer JDO bytecode. Another excellent example was a project at IBM where test scenarios were created dynamically through bytecode instrumentation. Often

Covariant Return Types - Java 6 or Greater

In OOP, a  covariant return type  of a method is one that can be replaced by a "specialized" type when the method is  overridden  in a subclass.  C# does not support return type covariance. Covariant return types have been (partially) allowed in the  Java language  since the release of JDK5.0, so the following example wouldn't compile on a previous release: // Classes used as return types: class Person { } class Guy extends Person { } // "Class Guy is more specific than class Person" // Classes demonstrating method overriding: class Department { public Person getPerson ( ) { return new Person ( ) ; } } class DepartmentOfGuys extends Department { public Guy getPerson ( ) { return new Guy ( ) ; } } More specifically, covariant (wide to narrower) or contravariant (narrow to wider) return type refers to a situation where the return type of the overriding method is changed

Java NIO.2 - The Path Class - An overview

The Java NIO.2 is a new API for file manipulation. Added to the Java Platform 7. This API comes with many benefits, and new classes. Those classes bring new functionalities, and solve problems of the traditional I/O API, as well as allow a more robust and simpler code. In this article, we are going to talk about one of the base classes: The Path class, present in the java.nio.File package. This class is basically one of the entry class of the Java NIO API. The models the Path of File or a directory in a File System in a particular Operational System - Microsoft Windows, for example. As we are a bout to see, this class offers many facilities that make it very important and present in other I/O Operations, as well as, it plays an important role with other classes in the NIO API. Let us get started in the journey of the Path class and its use. 1. Intancing a Path The Path, is actually an Interface, that is instanced with a static factory method from the utility class Paths(java.