Posts

Service Provider Framework Pattern Example

Image
This example is created based on the explanation found in the “Effective Java, Second Edition, Chapter 2: Creating and destroying objects”. This pattern about the framework which provide multiple implementations with the compact API. Complete decoupling of implementations made this pattern interesting to me always .  There are three main components: Service , provider, registration and Service Access API . In this example, there can be number of traveling implementations, for example, using bus, train, aircraft, car and so on. They are service providers. All these service implementations need be hide behind the registration. User doesn’t worry about the service implementation an only stick to the Service API. Let’s see the important TravelService interface which is the client completely rely on. I would like to keep this very simple with one service. package com.ojitha.travel.service; public interface TravelService { String getVehical(); } The particular service must ...

Developer Notes: Apache Commons FilterIterator

Image
Filtering the collection based on the criteria is a great advantage of FilterIterator. package com.blogspot.ojitha.commonexamples; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import org.apache.commons.collections.Predicate; import org.apache.commons.collections.iterators.FilterIterator; public class ListFilteringExample { /** * @param args */ public static void main(String[] args) { Person a = new Person("Smith", "Wills"); a.setAge(39); Person b = new Person("Kris", "Torano"); b.setAge(15); Person c = new Person("Mark","Anthony"); c.setAge(25); Person d = new Person("Mad", "Max"); d.setAge(19); List<Person> persons = new ArrayList<Person>(); persons.add(a); persons.add(b); persons.add(c); persons.add(d); Predicate predicate = new AdultFilter(18); Iterator<Person> adults = new FilterIterator(persons.iterator()...

Apache Commons Collections: Object equality using Predicate

The method equals() is the way to check the object equality in Java. You need to override equals() and hashCode() method to makes objects equals. However, in this blog, instead of obj1.equals(obj2) method, I am going to use more powerful Predicate from the Apache commons collection. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.blogspot.ojitha.commonexamples</groupId> <artifactId>Apache-common-example</artifactId> <version>1.0</version> <packaging>jar</packaging> <name>Apache-common-example</name> <url>http://ojitha.blogspot.com</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties...

Developer Notes: Spring Web Workflow 1.0.6 Maven 2 environment for development

Version 1.0.9 is very old version. However, almost all the examples found in the google search is based on Ant and IVY configuration. Therefore, I thought this blog will be beneficial to the people who are still in Ant configuration.  Time to move to Maven. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.ojitha.swffirst</groupId> <artifactId>swf-first</artifactId> <packaging>war</packaging> <version>1.0-SNAPSHOT</version> <name>swf-first Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version...

OGNL Part 8: Lambda without closures

The part 7 is a tutorial on Selection. Here I explain the very easy example from the OGNL language guide . This is factorial function as shown in the following example. package au.com.ojitha.ongl; import ognl.Ognl; import ognl.OgnlException; public class Lambda { /** * @param args * @throws OgnlException */ public static void main(String[] args) throws OgnlException { System.out.println(Ognl.getValue( "#fact = :[#this <=1 ? 1 : #this*#fact(#this-1)], #fact(4)", null)); } }   The output is 4*3*2*1=24.

OGNL Part 7: Tutorial on OGNL Selection

We’ve already discussed the  Selection in  the part 6 . Here the example of selecting first element or last element in the filtered array or list. ^ for the first element (lines 19-20) $ for the last element (lines 23-24) package au.com.ojitha.ongl; import ognl.Ognl; import ognl.OgnlException; public class SelectionTutorial { /** * @param args * @throws OgnlException */ public static void main(String[] args) throws OgnlException { Root root = new Root(); // selection System.out.println("slect integer which are greater than 2: " + Ognl.getValue("array.{? #this > 2}", root)); // return the first element System.out.println("select the first element in the array: " + Ognl.getValue("array.{^ #this > 2}", root)); //return the last element System.out.println("select the last element in the array: " + Ognl.getValue("array.{$ #this > 2}", root)); } } class R...

OGNL Part 6: Selection

In the part 5 of this series, we’ve discussed the Projection concept. In this part, see the example for the selection which is very similar to concept of SQL selection. package au.com.ojitha.ongl; import java.util.List; import ognl.Ognl; import ognl.OgnlException; public class ProjectingExmaple { /** * @param args * @throws OgnlException */ public static void main(String[] args) throws OgnlException { List<Integer> classes =(List<Integer>)Ognl.getValue("array.{? #this > 2}", new MyRoot()); for (Integer class1 : classes) { System.out.println(class1); } System.out.println("size of the output "+classes.size()); } } class MyRoot { private int[] array = {1,2,3,4}; public int[] getArray() { return array; } public void setArray(int[] array) { this.array = array; } } Here the output 3 4 size of the output 2 Reference OGNL language guide for more information.