Archive

Posts Tagged ‘Java’

A Handy and Highly Reusable JPA 2.0 Data Access Object

January 4th, 2011 negarnil 5 comments

Hi ! In this post I’ll show you a highly reusable Data Access Object for Java web applications which will reduce the development time of your DAOs.

Consider the following DAO and it’s implementation.

public interface UserDao {

     User storeUser(User user);

     void removeUser(User user);

     List<User> findAllUsers();

     User findUserById(long id);

}
public class JpaUserDao implements UserDao {

     @PersistentContext
     private EntityManager em;

     @Override
     public User storeUser(User user) {
          User merged = em.merge(user);
          em.persist(merged);
          return merged;
     }

     @Override
     public void removeUser(User user) {
          em.remove(user);
     }

     @Override
     public List<User> findAllUsers() {
          return (List <User>) em.createQuery("from User").getResultList();
     }

     @Override
     public User findUserById(Long id) {
        return em.find(User.class, id);
     }
}

It looks pretty cool ! We just coded a JPA 2.0 Data Access Object. This means we used standard Java libraries (javax.persistence package).

The problem with the above code is that you are going to repeat it for each entity in your application. Having those 40 lines of CRUD (Create-Remove-Update-Delete) code doesn’t only lead you to more development effort but only to a poorly maintainable application. A code generator like Spring Roo can do the work but we can avoid any code generation tools by applying some Java inheritance and spicy code like using Java Generics and Reflection. You’ll see how to use those forty lines for infinite entities DAOs.

First of all let’s create a generic DAO:

public interface BaseDao<T> {

	T store(T t);

	void remove(T t);

	List<T> findAll();

	T findById(long id);

}

The above interface will be extended by all your DAO interfaces.

We should now create a generic implementation of the above interface which will be extended by all the DAO implementations of your application.

@SuppressWarnings("unchecked")
@Repository
public abstract class JpaBaseDao<T extends Serializable> implements BaseDao<T> {

	private Class<T> clazz;

	@PersistenceContext
	protected EntityManager em;

	protected JpaBaseDao() {
		Class<T> getClass = (Class<T>) getClass();
		ParameterizedType genericSuperclass = (ParameterizedType) getClass.getGenericSuperclass();
		clazz = (Class<T>) genericSuperclass.getActualTypeArguments()[0];
	}

	@Override
	public T store(T t) {
		T merged = em.merge(t);
		em.persist(merged);
		return merged;
	}

	@Override
	public void remove(T t) {
		em.remove(t);
	}

	@Override
	public List<T> findAll() {
		CriteriaBuilder cb = em.getCriteriaBuilder();
		CriteriaQuery<T> query = cb.createQuery(clazz);
		query.from(clazz);
		return em.createQuery(query).getResultList();
	}

	@Override
	public T findById(long id) {
		return em.find(clazz, id);
	}
}

The @Repository annotation is a Spring annotation which Indicates that an annotated class is a “Repository” (or “DAO”). A class thus annotated is eligible for Spring DataAccessException translation. The annotated class is also clarified as to its role in the overall application architecture for the purpose of tools, aspects, etc. Note that you don’t need it if you are not using Spring.

The @PersistenContext is a javax.persistence annotation which expresses a dependency on an EntityManager persistence context.

The final code:

public interface UserDao extends BaseDao<User> {

     // Custom access methods (which are not CRUD's) go here

}
public class JpaUserDao extends JpaBaseDao<User> implements UserDao {

   // Custom access methods implementations go here

}

Then in a service layer, let’s say a Spring service, you can access the BaseDao CRUD methods from any DAO which inherits it.

@Service
public class UserService {

     @Autowired
     private UserDao userDao;

     @Transactional
     public User saveUser(User user) {
          return userDao.store(user);
     }

}

Well, that’s all ! I hope you find it useful.

If you enjoyed this post follow me on twitter or leave a feedback !

Share

Singleton your JRapid Services

August 27th, 2010 negarnil 1 comment

Hi ! In this post I’ll explain the Singleton Pattern and how you can use it to increase the performance of your web applications, specially in JRapid ’s.

There is no need to use a Profiler to know that the Java ‘new’ operator increases the memory allocation in the heap. The JVM’s heap stores all objects created by an executing Java program. Objects are created by Java’s “new” operator, and memory for new objects is allocated on the heap at run time.

But, fortunately, the Java Garbage Collector “throws away” the objects which are not needed anymore freeing space in the heap for new objects to be allocated. This frees the programmer from having to keep track of when to free allocated memory.

A potential disadvantage of a garbage-collected heap is that it adds an overhead that can affect program performance. The JVM has to keep track of which objects are being referenced by the executing program, and finalize and free unreferenced objects on the fly. This activity will likely require more CPU time than would have been required if the program explicitly freed unnecessary memory. In addition, programmers in a garbage-collected environment have less control over the scheduling of CPU time devoted to freeing objects that are no longer needed.

There is a common antipattern in mainly all Java applications which consists in instantiating a new Object each time it is required to so something.

In the case of JRapid applications, there is no need to instantiate a new Service each time there is a need to call a find(), store() or any method inside the class.

Example of the antipattern in JRapid applications:

public void pokeUser(User fromUser, User toUser) {

    Poke poke = new Poke();
    poke.setFrom(fromUser);
    poke.setToUser(toUser);
    poke.store();

    new EmailServices().notifyPoked(toUser);
}

This method instantiates a new EmailServices object each time a user pokes another user. Imagine you have an average of 1000 pokes per minute (or imagine you are Facebook).

A possible solution for this excessive memory allocation is to Singleton the EmailServices class and call always the same instance when a new email has to be send.

public class EmailServices extends EmailServicesAbstract {

    // Static instance
    private static EmailServices INSTANCE = null;

    // Singleton classes should have private constructors but the XMLRPC server needs this constructor to make the remote      calls
    public EmailServices() {
    }

    // Static getter of the instance, if it is null then call the constructor, else return it.
    public static synchronized EmailServices getInstance() {
        if (INSTANCE == null) {
            INSTANCE = new EmailServices();
        }
        return INSTANCE;
    }

    public void notifyPoked(User user) {
        // Send a mail to user
    }
}

So now, the pokeUser method will look like this:

public void pokeUser (User fromUser, User toUser) {

    Poke poke = new Poke();
    poke.setFrom(fromUser);
    poke.setToUser(toUser);
    poke.store();

    EmailServices.getInstance().notifyPoked(toUser);
}

This way, only one EmailServices instance will be created in the app the server and the CPU usage will be reduced considerably.

Keep around !

If you enjoyed this post follow me on twitter or leave a feedback !

Share
Categories: JRapid, Java Tags: ,

Applet: Java Graph Algorithms

May 16th, 2010 negarnil No comments

Hi ! I have one more Applet to share with you (it turned that I was a Java 2D developer before discovering JEE !). This Applet was built to watch the 6 Java Graph Algorithms in “action”. It’s pretty cool !

Powered by Cincopa WordPress plugin

Launch the applet from here.

Bye, keep around !

If you enjoyed this post follow me on twitter or leave a feedback !

Share
Categories: Java Tags: ,

6 Clean implementations of Java Graph algorithms

May 15th, 2010 negarnil 1 comment

Hi ! In this post I will share with you 6 Java Graph algorithms I had to code for a discrete mathematics final exam in college. The algorithms were presented in an Applet I built and which I also include for you to play around with them.

Before presenting the algorithms I will give a general background about Graph Theory.

What is a graph?

(from Wikipedia) A graph is an abstract representation of a set of objects where some pairs of the objects are connected by links. The interconnected objects are represented by mathematical abstractions called vertices, and the links that connect some pairs of vertices are called edges.

Type of graphs

There are many, many, many type graphs, most of them associated to maths properties. For the purpose of the API I developed for the algorithms implementations you should know the following:

In terms of representation, a graph can be represented either by an adjacency matrix or an adjacency list. In an adjacency matrix, a true/false (x,y) cell represents if exists a link between node “x” and node “y”. This is the one which I used because of its better performance. In an adjacency list, each node has an own list for storing referenced lists.

Graphs can be also classified by its links, we say that a graph is “weighted” when all links haves a “weight” o “cost” for going from one node to another.

Finally, graphs can be “directed” o “undirected” which means if it’s links are bidirectional or not.

So, the API of my implementations is something like:

public abstract class GraphADT { ... }

Implementations:

public class Graph extends GraphADT { ... }
public class WeightedGraph extends GraphADT { ... }

Prim

Given a connected weighted graph and a source vertex, returns a minimum spanning tree which is a tree that includes every vertex but the total weight of all the edges in the tree is minimized.

WeightedGraph prim(WeightedGraph graph, int s) { ... }

Warshall

Warshall’s algorithm calculates a transitive closure for a given graph. In mathematics, the transitive closure of a binary relation R on a set X is the smallest transitive relation on X that contains R.

void warshall(Graph g) { ... }

BFS

Breadth-first search (BFS) is a graph search algorithm that begins at the root node and explores all the neighboring nodes. Then for each of those nearest nodes, it explores their unexplored neighbor nodes, and so on, until it finds the goal.

int BFS(GraphADT graph, int v, int[] levelOrder) { ... }

DFS

Depth-first search (DFS) is an algorithm for traversing or searching a tree, tree structure, or graph. One starts at the root (selecting some node as the root in the graph case) and explores as far as possible along each branch before backtracking.

int DFS(GraphADT graph, int v, int[] pre, int[] post) { ... }

Dijkstra

Dijkstra’s algorithm, conceived by Dutch computer scientist Edsger Dijkstra in 1959, is a graph search algorithm that solves the single-source shortest path problem for a graph with nonnegative edge path costs, producing a shortest path tree. This algorithm is often used in routing.

Integer[] dijkstra(WeightedGraph graph, int source) { ... }

Floyd

The Floyd–Warshall algorithm is a graph analysis algorithm for finding shortest paths in a weighted graph (with positive or negative edge weights).

int[][] floyd(WeightedGraph graph) { ... }

Applet

I built an Applet to test the algorithms and graph implementations.

In it you can build many type of graphs and run the discussed algorithms.

Hava a look !


Download !

Checkout the project from http://code.google.com/p/javagraphalgorithms/.

Direct download from here !

Bye ! Keep around !

If you enjoyed this post follow me on twitter or leave a feedback !

Share
Categories: Java Tags: , ,

Applet: Pictorial representations of Java Sorting Algorithms

May 4th, 2010 negarnil No comments

Hi ! In this occasion I will share with you a Java Applet I built to visualize the dynamic characteristics of some of the 17 Clean implementations of Java Sorting Algorithms I posted some days ago.

Sorting algorithms are especially amenable to pictorial representations that make plain their dynamic characteristics. Visual representations are useful for understanding the operations of sorting algorithms.

Powered by Cincopa WordPress plugin

I had to add thread interruptions to the fastest sorters (QuickSort, MergeSort, etc…) because if not, the animations were too fast. So don’t ever, never, use this Applet to take conclusions about which sorter is the fastest.

Launch it !

If you enjoyed this post follow me on twitter or leave a feedback !

Share
Categories: Java Tags: , ,

17 Clean implementations of Java Sorting Algorithms

May 2nd, 2010 negarnil 17 comments

Well, in this post I will share with you 17 Java Sorting implementations I had to code in an algorithms course at college ! The package includes the following implementations (from the easiest to the more complex ones).

  • Bubble Sort (SorterType.BUBBLE)
  • Selection Sort (SorterType.SELECTION)
  • Insertion Sort (SorterType.INSERTION)
  • HSort (SorterType.H)
  • Shell Sort (SorterType.SHELL)
  • Quick Sort (SorterType.QUICK)
  • Quick “Cut” Sort (SorterType.QUICK_CUT)
  • Quick “Median of Three” Sort (SorterType.QUICK_MED_OF_THREE)
  • Quick “Non recursive” Sort (SorterType.QUICK_NON_RECURSIVE)
  • Quick “Three way Partition” Sort (SorterType.QUICK_THREE_PARTITION)
  • Bottom-Up Merge Sort (SorterType.MERGE_BOTTOM_UP)
  • Bottom-Up Linked-List Merge Sort
  • Top-Down Merge Sort (SorterType.MERGE)
  • Top-Down Linked-List Merge Sort
  • Binary Heap Sort (SorterType.HEAP_BINARY)
  • Ternary Heap Sort (SorterType.HEAP_TERNARY)

All implementations come with a brief explanation for you to understand the basic idea of how the algorithm sorts the elements !

API

All Sorting implementations extends AbstractSorter which implements Sorter. The Sorter interface is generified. This allows subsequent implementations to be capable to sort any collection of objects as long as exists a Comparator for that Type of objects.

public interface Sorter {

<T> void sort(Comparator<T> comparator, List<T> list) ;

SorterType getType();

}
public abstract class AbstractSorter implements Sorter { ... }

Implementations:

public class InsertionSorter extends AbstractSorter { ... }

How to use?

  • Get a DataGenerator instance:
DataSetGenerator<Integer> dataGenerator = new IntegerDataSetGenerator();
  • Get a SorterProvider instance:
SorterProvider sorterProvider = new SorterProvider();
  • Create a random list and sort it using QuickSort:
List<Integer> randomList = dataGenerator.createRandom(1000);
Sorter quickSort = sorterProvider.getSorterForType(SorterType.QUICK);
quickSort.sort(dataGenerator.getComparator(), randomList);
  • Create a descending list and sort it using SelectionSort:
List<Integer> descendingList = dataGenerator.createDescending(1000);
Sorter selectionSort = sorterProvider.getSorterForType(SorterType.SELECTION);
selectionSort.sort(dataGenerator.getComparator(), descendingList);
  • It isn’t a must to create a DataGenerator instance but at some point you will have to implement a java.util.Comparator.
public class StringComparator implements Comparator<String>  {

@Override
public int compare(String s1, String s2) {
return s1.compareTo(s2);
}

}
String[] names = new String[]{"Mery", "Paul", "Bruce"};
Sorter mergeSort = sorterProvider.getSorterForType(SorterType.MERGE);
mergeSort.sort(new StringComparator(), Arrays.asList(names));
  • It also isn’t a must to instance a SorterProvider. You can directly instance the Sorter implementation.
String[] surnames = new String[]{"Smith", "Garnil", "Valentino"};
Sorter shellSort = new ShellSorter();
shellSort.sort(new StringComparator(),  Arrays.asList(surnames));

Testing the sorters

You can find a JUnit TestCase named SortersTest.java inside the anaydis.ngarnil.test package. This class tests correctness of the Sorter implementations.

A JUnit JAR (junit.jar) file is included in the lib folder.

public class SortersTest extends TestCase { ... }

I also built an Applet to run the sorters and study their dynamic characteristics.

Powered by Cincopa WordPress plugin

Download !

Checkout the project from http://javasorting.googlecode.com. Don’t doubt to make questions or post issues, I will answer them as soon as I can.

Direct download: From here!

If you liked this post please vote it in dzone !

Bye !

If you enjoyed this post follow me on twitter or leave a feedback !

Share
Categories: Java Tags: ,

Big Modular Java with Google Guice

March 1st, 2010 negarnil No comments

Put simply, Guice alleviates the need for factories and the use of new in your Java code. Think of Guice’s @Inject as the new new. You will still need to write factories in some cases, but your code will not depend directly on them. Your code will be easier to change, unit test and reuse in other contexts.

Guice embraces Java’s type safe nature, especially when it comes to features introduced in Java 5 such as generics and annotations. You might think of Guice as filling in missing features for core Java. Ideally, the language itself would provide most of the same features, but until such a language comes along, we have Guice.

Guice helps you design better APIs, and the Guice API itself sets a good example. Guice is not a kitchen sink. We justify each feature with at least three use cases. When in doubt, we leave it out. We build general functionality which enables you to extend Guice rather than adding every feature to the core framework.

Guice aims to make development and debugging easier and faster, not harder and slower. In that vein, Guice steers clear of surprises and magic. You should be able to understand code with or without tools, though tools can make things even easier. When errors do occur, Guice goes the extra mile to generate helpful messages.

For an introduction to Guice and a comparison to new and the factory pattern, see Bob Lee’s video presentation. After that, check out our user’s guide.

The following video is a presentation of Guice in the Google I/O 2009 conference. Are you interested in Guice? Want to know how Google Wave was developed? Play it !

Share

Developing Aspects with Spring AOP

January 28th, 2010 negarnil 2 comments

Hi, I found this video which is an extract of a 4-day (and US$ 1600) of a Spring’s course. It covers the basics of AOP (Aspect Orientated Programming) and explains the Spring’s approach. It contains lots of code examples and screen records which also helps to better understand the Spring framework.

Taken from the Spring website:

In this highly practical SpringSource course Jeff Brown will teach you how to develop aspects with Spring AOP. This training gives you both theoretical as well as practical knowledge on Spring AOP.

In this training you will be provided with an:

  • - Introduction to AOP
  • - What problem does AOP solve
  • - The Spring AOP approach
  • - Defining pointcuts
  • - Implementing advice

File: s2university_aop2.mov (open with Quicktime, Itunes, etc…)
Size: 185 mb

Download

Share
Categories: JEE Development Tags: , ,