Sunday, January 25, 2009

TransformedMap and Transformers: Examples of Strategy, Decorator, and Factory

I mentioned some software design patterns in my last two examples one was a Factory, Decorator, and the other a Strategy. Before we get into the next example, I want to make sure you understand these patterns because, clearly the developers at Apache where thinking of these patterns when they developed the Commons Collections API. Let’s talk about a Factory first.

Factories:
Factories are a subset of what is called creational design patterns (See the wikipedia Abstract factory pattern). There are a couple of variations on the factory pattern but basically, you ask the factory for a named object, it assembles it and returns it. Just like a factory, hence the name. This is the building block of a lot of my web applications.

Decorator:
A decorator pattern is simply a way of receiving one value and decorating it into something suitable for a different view. For example, if you have a bean and it has a value of 12.599999999 you might want to decorator it to say something like $12.54. The apache team decided that the TransformedMap.decorate method was actually decorating the key not really a strategy.

Strategy:
A strategy pattern (See the wikipedia Strategy pattern) is a subset of behavioral design patterns. This pattern is analogous to a socket wrench (the socket part not the wrench part). So, as you might know, the socket can be replaced with different sizes at anytime. It simply plugs into the wrench and can be turned right or left. The concept here is we create an interface (which is a contract or in the wrench analogy the part that connects the socket to the wrench) whereby algorithms can be selected and dynamically installed at runtime.

Personally, I feel the TransformedMap.decorate is a borderline case between a strategy and a decorator…. On one hand it decorates the values going into the key, but on the other hand it changes the behavior at run time...Also, Im using a MultiValueMap for the backing map which totally changes the behavior. So, I will refer to it as a Strategy. I know some people will take issue with this, oh well. Lets declare some simple objects, like the Gender and State enum.


package com.blogspot.apachecommonstipsandtricks;
public enum Gender
{
Male, Female
}



package com.blogspot.apachecommonstipsandtricks;
/**
* Official USPS Abbreviations
*/
public enum State
{
AL("ALABAMA"), AK("ALASKA"), AS("AMERICAN SAMOA"),
AZ("ARIZONA "), AR("ARKANSAS"), CA("CALIFORNIA "),
CO("COLORADO "), CT("CONNECTICUT"), DE("DELAWARE"),
DC("DISTRICT OF COLUMBIA"), FM("FEDERATED STATES OF MICRONESIA"), FL("FLORIDA"),
GA("GEORGIA"), GU("GUAM "), HI("HAWAII"),
ID("IDAHO"), IL("ILLINOIS"), IN("INDIANA"),
IA("IOWA"), KS("KANSAS"), KY("KENTUCKY"), LA("LOUISIANA"),
ME("MAINE"), MH("MARSHALL ISLANDS"), MD("MARYLAND"),
MA("MASSACHUSETTS"), MI("MICHIGAN"), MN("MINNESOTA"),
MS("MISSISSIPPI"), MO("MISSOURI"), MT("MONTANA"),
NE("NEBRASKA"), NV("NEVADA"), NH("NEW HAMPSHIRE"),
NJ("NEW JERSEY"), NM("NEW MEXICO"), NY("NEW YORK"),
NC("NORTH CAROLINA"), ND("NORTH DAKOTA"), MP("NORTHERN MARIANA ISLANDS"),
OH("OHIO"), OK("OKLAHOMA"), OR("OREGON"),
PW("PALAU"), PA("PENNSYLVANIA"), PR("PUERTO RICO"),
RI("RHODE ISLAND"), SC("SOUTH CAROLINA"), SD("SOUTH DAKOTA"),
TN("TENNESSEE"), TX("TEXAS"), UT("UTAH"),
VT("VERMONT"), VI("VIRGIN ISLANDS"), VA("VIRGINIA "),
WA("WASHINGTON"), WV("WEST VIRGINIA"), WI("WISCONSIN"), WY("WYOMING");
private String fullyQualifiedName;
State(String fullyQualifiedName)
{
this.fullyQualifiedName = fullyQualifiedName;
}
public String getFullyQualifiedName()
{
return fullyQualifiedName;
}
}


Ok, now define our Data Transfer Object (DTO), just like the last example.


package com.blogspot.apachecommonstipsandtricks;
public class DTO
{
private int id;
private String name;
private Gender gender;
private State state;

public DTO(int id, String name, Gender gender, State state)
{
this.id = id;
this.name = name;
this.gender = gender;
this.state = state;
}
public int getId()
{
return id;
}
public void setId(int id)
{
this.id = id;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public Gender getGender()
{
return gender;
}
public void setGender(Gender gender)
{
this.gender = gender;
}
public State getState()
{
return state;
}
public void setState(State state)
{
this.state = state;
}
@Override
public String toString()
{
return "com.blogspot.apachecommonstipsandtricks.DTO{id=" + id + ", name='" + name + '\'' + ", gender=" + gender + ", state=" + state + '}';
}
}


And now, ( drum roll please ) the factory.


package com.blogspot.apachecommonstipsandtricks;
import java.util.*;
import org.apache.commons.collections.map.*;
import org.apache.commons.collections.*;
public class MapFactory
{
public static Map getMap(MapFactoryEnum whichFactory)
{
Map returnMap = null;
if (null != whichFactory)
{
Transformer transformer;
switch (whichFactory)
{
case NAME:
transformer = TransformerUtils.invokerTransformer("getName");
break;
case STATE:
transformer = TransformerUtils.invokerTransformer("getState");
break;
case GENDER:
transformer = TransformerUtils.invokerTransformer("getGender");
break;
default:
throw new IllegalArgumentException("Unknown Map");
}
returnMap = TransformedMap.decorate( new MultiValueMap(), transformer, TransformerUtils.nopTransformer());
}
return returnMap ;
}
public enum MapFactoryEnum
{
NAME, STATE, GENDER
}
}

So, when you call getMap, you request a map by a name. The factory builds a new MultiValueMap and decorates it, as in a Strategy with the transformer.

The code:

package com.blogspot.apachecommonstipsandtricks;
import java.util.*;
import org.apache.commons.collections.*;
import org.apache.commons.lang.*;
public class TestMapFactory
{
public static void main(String[] args)
{
int i = 0;
List<dto> list = Arrays.asList(new DTO(i++,"Bob", Gender.Male, State.WI), new DTO(i++,"Larry",Gender.Male, State.WI),
new DTO(i++,"Bill", Gender.Male, State.WI), new DTO(i++,"Sue", Gender.Female, State.AZ),
new DTO(i++,"Joe", Gender.Male, State.AZ), new DTO(i++,"Zoe", Gender.Female, State.WI));

List<dto> dtosFromTheMap;
Collection names;
Map map;

System.out.println("------------------------- By State -------------------------");
map = MapFactory.getMap(MapFactory.MapFactoryEnum.STATE);

for (DTO dto : list)
{
map.put( dto, dto );
}

dtosFromTheMap = (List<dto>) map.get(State.WI);
names = CollectionUtils.collect(dtosFromTheMap, TransformerUtils.invokerTransformer("getName"));
System.out.println(StringUtils.join(names.iterator(),",") + " are in " + State.WI.getFullyQualifiedName() );

dtosFromTheMap = (List<dto>) map.get(State.AZ);
names = CollectionUtils.collect(dtosFromTheMap, TransformerUtils.invokerTransformer("getName"));
System.out.println(StringUtils.join(names.iterator(),",") + " are in " + State.AZ.getFullyQualifiedName() );

System.out.println("------------------------- By Gender -------------------------");
map = MapFactory.getMap(MapFactory.MapFactoryEnum.GENDER);

for (DTO dto : list)
{
map.put( dto, dto );
}

dtosFromTheMap = (List<dto>) map.get(Gender.Male);
names = CollectionUtils.collect(dtosFromTheMap, TransformerUtils.invokerTransformer("getName"));
System.out.println(StringUtils.join(names.iterator(),",") + " are " + Gender.Male );

dtosFromTheMap = (List<dto>) map.get(Gender.Female);
names = CollectionUtils.collect(dtosFromTheMap, TransformerUtils.invokerTransformer("getName"));
System.out.println(StringUtils.join(names.iterator(),",") + " are " + Gender.Female );
}
}


This code could be very useful if you where caching items or grouping selections together inside a controller. Just a side note, maps are always non-thread safe. I will explain in my next example what that means.

The results:

------------------------- By State -------------------------
Bob,Larry,Bill,Zoe are in WISCONSIN
Sue,Joe are in ARIZONA
------------------------- By Gender -------------------------
Bob,Larry,Bill,Joe are Male
Sue,Zoe are Female

Author: Philip A Senger

Tuesday, January 20, 2009

TransformedMap and Transformers: Decorated "put" strategy

This is a long example regarding decorated maps and transformers. It’s important to understand the concepts I will cover, because I will use them in the next example, and continue to build on them.

The object TransformedMap has a static method called decorate (http://commons.apache.org/collections/api-release/org/apache/commons/collections/map/TransformedMap.html), this method allows you to load a backing map and two transformers (http://commons.apache.org/collections/api-release/org/apache/commons/collections/Transformer.html), one for the key and the other for the value. The returning object is a decorated map. So, when you put an item into this new decorated map, the key and values are transformed. Unfortunately, the transformer interface doesn’t allow you to gain access to the backing object through the interface. You could be cleaver and jam it into a constructor ( more on this later ). Be forewarned, modifying a map while in the midst of another modification method will result in a big ugly run time exception ( just in case you thought you could us this method to build your own MultiValueMap ).

Enough chatter… lets look at some code

The code:

package com;
import java.util.*;
import org.apache.commons.collections.map.*;
import org.apache.commons.collections.*;
public class MapDecorator
{
public static void main(String[] args)
{
int i = 0;
List<DTO> list = Arrays.asList(new DTO(i++,"Bob",Gender.Male,State.WI), new DTO(i++,"Larry",Gender.Male,State.WI),
new DTO(i++,"Bill", Gender.Male, State.WI), new DTO(i++,"Sue", Gender.Female, State.AZ),
new DTO(i++,"Joe", Gender.Male, State.AZ), new DTO(i++,"Zoe", Gender.Female, State.WI));
// Decorate a map where the key is the id, and the value is the object.
Map exampleOne = TransformedMap.decorate( new HashMap(),
TransformerUtils.invokerTransformer("getId"),
TransformerUtils.nopTransformer());
// Decorate a map where the key is the name and the value is the id
Map exampleTwo = TransformedMap.decorate( new HashMap(),
new Transformer() {
public Object transform(Object o)
{
return ((DTO)o).getName();
}
},
new Transformer() {
public Object transform(Object o)
{
return ((DTO)o).getId();
}
} );
// load up the maps.
for (DTO dto : list)
{
exampleOne.put(dto, dto);
exampleTwo.put(dto, dto);
}
printTheMap("exampleOne",exampleOne);
printTheMap("exampleTwo",exampleTwo);
}

private static void printTheMap(String mapName, Map exampleOne)
{
System.out.println("Map Name = " + mapName );
System.out.println("------------ Keys ------------");
for (Object key : exampleOne.keySet())
{
System.out.println("key = " + key);
}
System.out.println("------------ Values ------------");
for (Object value : exampleOne.values())
{
System.out.println("value = " + value);
}
System.out.println("------------------------------");
}
public static class DTO
{
private int id;
private String name;
private Gender gender;
private State state;
public DTO(int id, String name, Gender gender, State state)
{
this.id = id;
this.name = name;
this.gender = gender;
this.state = state;
}
public int getId()
{
return id;
}
public void setId(int id)
{
this.id = id;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public Gender getGender()
{
return gender;
}
public void setGender(Gender gender)
{
this.gender = gender;
}
public State getState()
{
return state;
}
public void setState(State state)
{
this.state = state;
}
@Override public String toString()
{
return "DTO{id=" + id + ", name='" + name + '\'' + ", gender=" + gender + ", state=" + state + '}';
}
}
public static enum Gender
{
Male, Female
}
/**
* Official USPS Abbreviations
*/
public static enum State
{
AL("ALABAMA"), AK("ALASKA"), AS("AMERICAN SAMOA"),
AZ("ARIZONA "), AR("ARKANSAS"), CA("CALIFORNIA "),
CO("COLORADO "), CT("CONNECTICUT"), DE("DELAWARE"),
DC("DISTRICT OF COLUMBIA"), FM("FEDERATED STATES OF MICRONESIA"), FL("FLORIDA"),
GA("GEORGIA"), GU("GUAM "), HI("HAWAII"),
ID("IDAHO"), IL("ILLINOIS"), IN("INDIANA"),
IA("IOWA"), KS("KANSAS"), KY("KENTUCKY"), LA("LOUISIANA"),
ME("MAINE"), MH("MARSHALL ISLANDS"), MD("MARYLAND"),
MA("MASSACHUSETTS"), MI("MICHIGAN"), MN("MINNESOTA"),
MS("MISSISSIPPI"), MO("MISSOURI"), MT("MONTANA"),
NE("NEBRASKA"), NV("NEVADA"), NH("NEW HAMPSHIRE"),
NJ("NEW JERSEY"), NM("NEW MEXICO"), NY("NEW YORK"),
NC("NORTH CAROLINA"), ND("NORTH DAKOTA"), MP("NORTHERN MARIANA ISLANDS"),
OH("OHIO"), OK("OKLAHOMA"), OR("OREGON"),
PW("PALAU"), PA("PENNSYLVANIA"), PR("PUERTO RICO"),
RI("RHODE ISLAND"), SC("SOUTH CAROLINA"), SD("SOUTH DAKOTA"),
TN("TENNESSEE"), TX("TEXAS"), UT("UTAH"),
VT("VERMONT"), VI("VIRGIN ISLANDS"), VA("VIRGINIA "),
WA("WASHINGTON"), WV("WEST VIRGINIA"), WI("WISCONSIN"), WY("WYOMING");
private String fullyQualifiedName;
State(String fullyQualifiedName)
{
this.fullyQualifiedName = fullyQualifiedName;
}
public String getFullyQualifiedName()
{
return fullyQualifiedName;
}
}
}



The results:

Map Name = exampleOne
------------ Keys ------------
key = 2
key = 4
key = 1
key = 3
key = 5
key = 0
------------ Values ------------
value = DTO{id=2, name='Bill', gender=Male, state=WI}
value = DTO{id=4, name='Joe', gender=Male, state=AZ}
value = DTO{id=1, name='Larry', gender=Male, state=WI}
value = DTO{id=3, name='Sue', gender=Female, state=AZ}
value = DTO{id=5, name='Zoe', gender=Female, state=WI}
value = DTO{id=0, name='Bob', gender=Male, state=WI}
------------------------------
Map Name = exampleTwo
------------ Keys ------------
key = Bob
key = Larry
key = Zoe
key = Joe
key = Sue
key = Bill
------------ Values ------------
value = 0
value = 1
value = 5
value = 4
value = 3
value = 2
------------------------------


The decorated Map, exampleOne, is backed by a HashMap and when put is called on the map, it calls the TransformerUtils.invokerTransformer("getId") (http://commons.apache.org/collections/api-release/org/apache/commons/collections/TransformerUtils.html#invokerTransformer(java.lang.String)) on the Object for the key. This results in the invoker using reflections to pull the id off the bean. In the same stroke, the value is transformed by TransformerUtils.nopTransformer() (http://commons.apache.org/collections/api-release/org/apache/commons/collections/TransformerUtils.html#nopTransformer()). This literally does nothing to the object used as the value; it is a kind of pass through.

// Decorate a map where the key is the id, and the value is the object.
Map exampleOne = TransformedMap.decorate( new HashMap(),
TransformerUtils.invokerTransformer("getId"),
TransformerUtils.nopTransformer());


For example

DTO dto = new DTO(100,"Bob",Gender.Male,State.WI);
exampleOne.put(dto, dto);

Results in an entry that has a key of 100 and a value of DTO(100,"Bob",Gender.Male,State.WI).

You might want to get a couple of values out and build a multi-value key or something else. The Map, exampleTwo, shows two anonymous inner implementations of the transformer interface.

Map exampleTwo = TransformedMap.decorate( new HashMap(),
new Transformer() {
public Object transform(Object o)
{
return ((DTO)o).getName();
}
},
new Transformer() {
public Object transform(Object o)
{
return ((DTO)o).getId();
}
} );


For example if you did this.

DTO dto = new DTO(100,"Bob",Gender.Male,State.WI);
exampleTwo.put(dto, dto);

The results would be a key of Bob pointing to a value of 100… are you beginning to see how awesome this api is? Are your gears grinding yet? Wait to you see what we do next time.
Author: Philip A Senger

Sunday, January 18, 2009

Transformer Invoker: For a collection of beans, collect a property

How many times have you had to collect a property from a list of beans, statically or dynamically? I use this little method a lot. For-loops are nice, but because this uses reflection to get the value, you can cook up a factory to get the data.

The code:

package com;
import java.util.*;
import org.apache.commons.collections.*;
public class TransformerExample
{
public static void main(String[] args)
{
List list = Arrays.asList(new DTO("Bob"), new DTO("Larry"), new DTO("Mo"), new DTO("Joe"));
Collection<String> names = CollectionUtils.collect(list, TransformerUtils.invokerTransformer("getName"));
for (String name : names)
{
System.out.println("name = " + name);
}
}
public static class DTO
{
private String name;
public DTO(String name)
{
this.name = name;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
}
}



The results:

name = Bob
name = Larry
name = Mo
name = Joe
Author: Philip A Senger

Friday, January 16, 2009

Multi Value Map : The values are a List

NOTE:
Be aware that the method put does more than just "put" an object into the map. Therefore, if this object is the by-product of a method it would be advisable to wrap the map in an un-modifiable map. A lot of developers argue that this object violates the map interface. Frankly I think this argument is akin to splitting hairs, it works for me and I use it everyday.

The code:

MultiMap mhm = new MultiValueMap();

key = "Group One";
mhm.put(key, "Item One");
mhm.put(key, "Item Two");
mhm.put(key, "Item Three");

key = "Group Two";
mhm.put(key, "Item Four");
mhm.put(key, "Item Five");

Set keys = mhm.keySet();
for (Object k : keys) {
out.println("("+k+“ : "+mhm.get(k)+")");
}

The results:

(Group One : [Item One, Item Two, Item Three])
(Group Two : [Item Four, Item Five])
Author: Philip A Senger

Bi-Directional Map : The values can be turned into the keys.

NOTE:
This example is a Hash Map on both the key and value. Notice that last-in wins. For example X kills C when D is put twice as the value.

The code:

BidiMap aMap = new DualHashBidiMap();
aMap.put("B", "A");
aMap.put("A", "B");
aMap.put("C", "D");
aMap.put("X", "D");
MapIterator it = aMap.mapIterator();
System.out.println("Before Inverse");
while (it.hasNext()) {
key = it.next();
value = it.getValue();
out.println(key + " -> " + value);
}
aMap = aMap.inverseBidiMap();
System.out.println("After Inverse");
it = aMap.mapIterator();
while (it.hasNext()) {
key = it.next();
value = it.getValue();
out.println(key + " -> " + value);
}

The results:

Before Inverse
A -> B
B -> A
X -> D
After Inverse
D -> X
A -> B
B -> A
Author: Philip A Senger

Case Insensitive Map : The keys, a String, are case insensitive.

The code...
Map aMap = new CaseInsensitiveMap();
aMap.put("A", "The letter A or a");
aMap.put("b", "The letter B or b");
System.out.println("aMap.get(\"a\") = " + aMap.get("a"));
System.out.println("aMap.get(\"A\") = " + aMap.get("A"));
System.out.println("aMap.get(\"b\") = " + aMap.get("b"));
System.out.println("aMap.get(\"B\") = " + aMap.get("B"));

The results..
aMap.get("a") = The letter A or a
aMap.get("A") = The letter A or a
aMap.get("b") = The letter B or b
aMap.get("B") = The letter B or b
Author: Philip A Senger

Iterable Map Interface : Iterate over a map with keys and values.

Just about every map in the commons collection implements this interface. It allows you to iterate over the key and values with a MapIterator. I know that Java 1.5 has features that allow map iteration, but if you are 1.4... this is nice.

MapIterator it = aMap.mapIterator();
while (it.hasNext()) {
   key = it.next();
   value = it.getValue();
   out.println(key + " -> " + value);
}
Author: Philip A Senger