Working with multiple systems require mapping of data from one object to another of different types. Let us take a look at a lightweight way to convert objects utilizing guava functions
Detailed Video Notes
Screencast #2: Intro to transforming objects with guava
Hey everyone, this is Justin at Level up Lunch and today we’re going talk about transforming objects in Java or converting objects in java. We will look at guava functions and the transform method in a few of the utility classes.
Before we get started looking at code we are going navigate to the guava library page which contains lots of information. The guava explained page has a topic named functional idioms that discusses usages of functions. One thing to mention before discussing functions is there an important caveat to be aware of. "Excessive use of Guava's functional programming idioms can lead to verbose, confusing, unreadable, and inefficient code...". Guava team has created predicates and functions that allow for clean code but if you use it everywhere it really becomes excessive and hard to read and misused. If you are trying to loop over, use a for loop to make it happen vs using a function everywhere.
All the function is a specific type of class that has an apply method that can be overridden. In the example, the apply method accepts a string as a parameter and returns an integer or the length of the string. Scrolling a bit further in the documentation, the most common use of functions is transforming collections.
Convert string to Enum
I have seeded some data so we can get right to the examples and you don't have to watch me type. The first example to show is transforming a string to Enum. Enums are quite popular and you may want to transform as it might be stored as a string in a database or some value.
Taking a look at the Day enum we created, it is a simple class that represents the days of the week:
We may have a list of strings with various strings representing days, Wednesday, Sunday, Monday... What we want to do is convert the list of strings to enums. There is a function in the Enum utility class in guava valueOfFunction that allows you to pass in the enum you want to convert to. In our case, Day.Class. Since we just upgraded to Guava 16.0, the valueOfFunction has been deprecated in preference of stringConverter. The stringConverter will return a function that converts a string to an Enum. Now we have a function that can be passed to guava utilities, in this case Iterables utility class by calling the transform method which will convert each string of list to a day enum.
Convert from one object to another
The next example will look at is how to convert one object to another. Quite often we need to go to a legacy system to get data and populate a new set of domains for our new system. Or we may get data from a web service or whatever it may be. For this example, I created two objects.
TdAmeritradeInvestment which contain similar attributes of different types.
There is a number of other utilities such as
BeanUtils.copyProperties in apache commons or written your own if statements and have made it work that way. If we don't have a list of objects, we can call a method on function that will return a just a single object. We want to create the function that will map the
ETradeInvestment. If you ever get lost, you can use code assist or just remember the <F, T> just means from object, to object. For each iteration of the list, a new
ETradeInvestment object will be created while mapping the
TdAmeritradeInvestment to it. For the key, we will use the Ints.stringConverter which allows us to convert from a string to an int.
If we want to get some reuse out of this function, we could extract it outside this method or we can just use it inline. We will use the Lists utility to transform the list, above we use Iterables and there is also FluentIterables and Collections2. There is a lot of different ways which guava provides to use a function. If we run this code, we have
EtradeInvestment object with the key, name and price. As a disclosure, I don't own any of these investments and were pulled from Google home page under the trending section. That did it, we converted from the
Convert an object
If you have one single investment or object you want to convert, you can call the apply method directly on the function that will do the conversion.
Convert list to map
One other really neat way to use function convert a list to a map. You may want to look up an object based on some object property. In this example, we want to create a map of
TdAmeritradeInvestment based on the investment key. Taking a look we can use the Maps utility calling the uniqueIndex method which accepts a list and a function. The function, or the keyfunction, is used to produce the key for each value in the iterable. In this instance, the function will map from a
TdAmeritradeInvestment and return an integer which will represent the key in the map.
Thanks for joining today's leveluplunch, have a great day.