Thursday, September 30, 2010

You’re never given a dream without also being given the power to make it true

“You’re never given a dream without also being given the power to make it true.”

- Richard Bach through Julie911

How people treat you is their karma; how you react is yours.

“How people treat you is their karma; how you react is yours.”

- Wayne W. Dyer through Julie911

The most significant gifts

“The most significant gifts are the ones most easily overlooked. Small, everyday blessings: woods, health, music, laughter, memories, books, family, friends, second chances, warm fireplaces, and all the footprints scattered throughout our days.”

- Sue Monk Kidd

Losing, in a curious way is winning.

“That’s what learning is, after all; not whether we lose the game, but how we lose and how we’ve changed because of it, and what we take away from it that we never had before, to apply to other games. Losing, in a curious way is winning.”

- “The Bridge Across Forever” by Richard Bach through Julie911

Monday, September 27, 2010

Viewing the maven dependency tree

mvn dependency:tree

 

to see the dependency tree of a project

Thursday, September 23, 2010

An introduction to the default maven build lifecycle and the compiler plugin

Download word document file here.

Frequently used mvn targets

Target

Purpose

compile

generate class files from .java files

test

Run test cases. If need be compile main and test source (all source in ${base-dir}/src/test/java confirming to **/*Test.java, **/Test*.java  **/*TestCase.java and excludes **/Abstract*Test.java and **/Abstract*TestCase.java

test-compile

Compile only test source and not execute it

package

Make the jar/war/ear, deploy into target directory

install

deploy the jar/war/ear (build if necessary) into the local repository (~/.m2/repository)

clean

Removes target directory

eclipse:eclipse

Generate eclipse .project files

 

 

Monday, September 13, 2010

Java Generics - Part 4

Wildcards:

 

Consider the a method that prints all the elements of a collection (pre 1.5 code):

 

private static void printCollection(Collection objectCollection)

{

      for (Object element: objectCollection)

      {

            System.out.println( element );

      }

}

 

The above code when invoked as follows:

 

 

printCollection( Arrays.asList( new String[] {"test", "another test"} ) );

 

 

gives the following output:

 

 

test

another test

 

 

Lets write the same method using generics:

 

private static void printCollection(Collection<Object> objectCollection)

{

      for (Object element: objectCollection)

      {

            System.out.println( element );

      }

}

 

But now the same invocation ( printCollection( Arrays.asList( new String[] {"test", "another test"} ) ); ) will lead to a compile time error! :

 

PrintCollection.java:8: printCollection2(java.util.Collection<java.lang.Object>) in PrintCollection cannot be applied to (java.util.List<java.lang.String>)

                 printCollection2( Arrays.asList( new String[] { "test", "another test" }) );

                 ^

1 error

 

This is because when the String[] is converted to a List, it returns a List<String>. And a List<String> is not a List<Object> (although String is a Object. Why? Read this)

 

To resolve this issue, we could use…wildcards as follows:

 

private static void printCollection2(Collection<?> objectCollection)

{

      for (Object element: objectCollection) // compiler error will result for objectCollection.add(anything). Because Collection<?> specifies an unknown element type<terminology>. And the compiler has no way to check the type of “anything” against the unknown type. Null is exception.

      {

            System.out.println( element );

      }

}

 

 

Saturday, September 11, 2010

Java Generics - Part 3

List<Student> is NOT a List<Person>.

Isa relationships do not apply for generics. Why? Check the following listing:


 

1        List<String> nameList = new ArrayList<String>();

2        List<Object> objectList = nameList; // Now objectList is an alias for nameList

3     obecjtList.add( new Student() ); // a Student would have been added to a List<String>


 

Assuming the above code snippet would compile (it will not), it has introduced a Student object into a List of Strings. This violates one of the very basic reasons for having generics (type checking).  So Isa relationships do not work with generics.

BTB, the above code will fail compilation with the following compiler error:


 

NoIsaInGenerics.java:8: incompatible types

found   : java.util.List<java.lang.String>

required: java.util.List<java.lang.Object>

               List<Object> objectList = nameList; // Now objectList is an alias for nameList

                                         ^

NoIsaInGenerics.java:9: cannot find symbol

symbol  : variable obecjtList

location: class NoIsaInGenerics

               obecjtList.add( new Student() ); // a Student would have been added to a List<String>

               ^

2 errors


 

Java Generics - Part 2

A custom parametrized type:

Consider the following oversimplified DAO interfaces:


 

public interface StudentDAO

{

   Student getStudent( Long studentId );

}

 

class Student

{

        private Long id;

        private String name;

        // accessors and mutators

}


 

The above interface is specific for Student. But the same interfaces will hold good for other entities too, like Professor, Course etc... If the interfaces like the above are to be implemented, then there would be a Professor getProfessor( Long professorId ), Course getCourse( Long courseId). This is redundant, violates the DRY (Do not Repeat Yourself) principle. Since there is a getEntity() method is all the cases, the above interface can be written using generics as follows:

 


 

public interface DataAccessInterface<T> // 1 Parameterized type 2 T is formal type paramter

{

        T getEntity( Long EntityKey ); // 3 T can be used where ordinary types can be used

}


The above DAO interface will be used as follows:


 

        DataAccessInterface<Student> studentDAO; // 4 Student is actual type argument


 

Now, DataAccessInterface<T> is a parameterized type, T is the name of the formal type paramter. After T has been declared in the interface declaration, it can be used instead of normal types throughout the interface declaration.

When a reference to a parameterized type is declared (at 4 above), the actual type to be passed to the formal types is also specified. At this time, all occurrences of the formal type (T) will be replaced by the actual type (Student).