<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title> &#187; Uncategorized</title>
	<atom:link href="http://www.anzaan.com/category/uncategorized/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.anzaan.com</link>
	<description></description>
	<lastBuildDate>Sat, 26 Jun 2010 03:29:01 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Functional Style Programming in Java -List Transformation</title>
		<link>http://www.anzaan.com/2009/05/function-style-programming-in-java-list-transformation/</link>
		<comments>http://www.anzaan.com/2009/05/function-style-programming-in-java-list-transformation/#comments</comments>
		<pubDate>Thu, 21 May 2009 06:06:12 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[java]]></category>

		<guid isPermaLink="false">http://www.anzaan.com/?p=19</guid>
		<description><![CDATA[I have been reading a lot of posts on functional style programming and its amazing how things can be simplified  by using functional style of programming.
I find it hard to get my head around a lot of the functional programming concepts like Monad and Functors.
I seem to understand what they are when I read articles [...]]]></description>
			<content:encoded><![CDATA[<p>I have been reading a lot of posts on functional style programming and its amazing how things can be simplified  by using functional style of programming.<br />
I find it hard to get my head around a lot of the functional programming concepts like Monad and Functors.</p>
<p>I seem to understand what they are when I read articles on them but I keep forgetting  in a few days time and I have to start over again. Perhaps the problem is that I haven&#8217;t been able to find use cases for them in my everyday programming which happens to be Java mostly and javascript and PHP occasionally.</p>
<p>Javascript is actually great for functional style programming as Functions are first-class citizens in Javascript.</p>
<p>But recently I actually managed to apply a little functional style programming to solve a recurring task in a java application I&#8217;m working on.</p>
<p>I need to transform  full-blown objects into combo-box friendly objects that  I can transform into JSON object and send it to webpages for use in forms.</p>
<p>Let&#8217;s take an example of a Person object. The drop-down box for person should have person&#8217;s id as value and<br />
for display text  it should be a combination of surname and name.</p>
<p>For ease of use  for getting display text let&#8217;s   say we have  a Person class that  returns  name and surname combination  through its toString() method.</p>
<p>So, here&#8217;s our person class.</p>
<pre class="brush: java;">
class Person {

private Integer id;
private String name;
private String surname;

// other interesting properties
..........

// getters and setter methods
........
.....
public String toString(){
return surname+&quot;, &quot;+name;
}

}
</pre>
<p>And I have a class called Combovalue.</p>
<pre class="brush: java;">
class Combovalue {

Integer value;
String  label;

public ComboValue(Integer value, String label){
this.value=value;
this.label=label;
}

// getters and setters
.........

}
</pre>
<p>Now that we necessary classes we need a handy way to map  a List of Person to a List of  comboValues.</p>
<p>For that  we&#8217;ll use a Utility class  for mapping and  an interface that has a method to do the transformation .</p>
<p>First is class ListMap which has a static method which takes a list of Type A  objects and returns a List of Type B object.<br />
Well, for our example, the method should take a list of Person and return a List of ComboValue.</p>
<p>And we need an interface that has a method that takes an argument of type A and returns an object of type B.<br />
Of course for our example it should take a person and return a ComboValue.</p>
<p>Here&#8217;s the interface that should be implemented to do the object transformation. Its not really transformation per se, but let&#8217;s call it that for simplicity.</p>
<p>It takes a parameter of type A and return a value of type B.</p>
<pre class="brush: java;">
public interface ITransformer&lt;A,B&gt; {

public  B transform(A value);

}
</pre>
<p>Here&#8217;s the class whose static method  transforms  a list of type A to list of type B.</p>
<pre class="brush: java;">
public  class ListMap {

public  static &lt;A, B  &gt; List&lt;B&gt;  map(  List&lt;A&gt; list, ITransformer&lt;A,B&gt; f ){

List&lt;B&gt; values= new ArrayList&lt;B&gt;();

for( int i=0; i&lt;list.size();i++){

values.add( f.transform(list.get(i)) );

}
return values;
}

}
</pre>
<p>Now the usage:<br />
Say we have a method in one of our classes in our application that handles this transformation.<br />
It retrives a list of Person from database, passes it to the map() method of ListMap  serialize it into JSON and returns it.</p>
<pre class="brush: java;">
Class JSONService{

XStream xstream ;

public JSONService(){
xstream = new XStream(new JettisonMappedXmlDriver());

}

public String getPersonCombos(){

//Assume we have a DAO class to retrieve data for us as List.

List&lt;Person&gt; list= pesonDao.getList();

//now the magic
List&lt;ComboValue&gt; combos = ListMap.map(list, new ITransformer&lt;Person, ComboValue&gt;() {

public ComboValue transform(Person person) {

return new ComboValue(person.getId(), person.toString());

}
});

//use XStream to serialize the list into JSON data.
return xstream.toXML(combos)

}

}
</pre>
<p>That&#8217;s alll the magic.</p>
<p>Transforming object by object can be a pain in the back-side. And the above technique is hardly that useful.<br />
As we&#8217;ll end up creating methods for each domain to return combo data. And if we have say over 50 domain objects its a pain. And the above abstraction doesn&#8217;t ease our pain much</p>
<p>So, Say we define an interface  called Entity that has a single method.</p>
<pre class="brush: java;">
public interface Entity {

public Integer getId();

}
</pre>
<p>Our person class and all our Domain classes implement this interface.</p>
<p>and our Person class becomes:</p>
<pre class="brush: java;">

class Person implements Entity {

// the rest is unchanged
}
</pre>
<p>now lets modify our JSONService class.</p>
<pre class="brush: java;">
Class JSONService{

XStream xstream ;

public JSONService(){
xstream = new XStream(new JettisonMappedXmlDriver());

}

public String getCombo( Class clazz){

//Assume we have a DAO factory with a method that returns a DAO object for a given class  to retrieve data for us as List.

List&lt;clazz&gt; list= DAOFactory.get(clazz).getList();

//Since all our domain classes implement Entity interface we pass in the argument type as Entity

List&lt;ComboValue&gt; combos = ListMap.map(list, new ITransformer&lt;Entity, ComboValue&gt;() {

public ComboValue transform(Entity entity) {

return new ComboValue(entity.getId(),  entity.toString());

}
});

//use XStream to serialize the list into JSON data.
return xstream.toXML(combos)

}

}
</pre>
<p>And now we have a nice way of transforming any entity into ComboValue object and returning it as JSON !</p>
<p>With Java Generics and a bit of inspiration from functional programming we have a reusable classes  that forms a component that will always obey the fundamental rule of  returning List of type B for any supplied list of type A.</p>
<p>Hardly rocket science, but useful piece of abstraction nevertheless.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.anzaan.com/2009/05/function-style-programming-in-java-list-transformation/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Welcome</title>
		<link>http://www.anzaan.com/2009/05/welcome/</link>
		<comments>http://www.anzaan.com/2009/05/welcome/#comments</comments>
		<pubDate>Wed, 20 May 2009 07:01:28 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.anzaan.com/?p=3</guid>
		<description><![CDATA[Welcome to my world of contradictions, idiocy, occasional nonsense and much much more that you could care less about
]]></description>
			<content:encoded><![CDATA[<p>Welcome to my world of contradictions, idiocy, occasional nonsense and much much more that you could care less about</p>
]]></content:encoded>
			<wfw:commentRss>http://www.anzaan.com/2009/05/welcome/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
