If you're interested in functional programming, you might also want to checkout my second blog which i'm actively working on!!

Tuesday, December 17, 2013

Even Guava doesn't make Java appealing anymore once you tasted Scala

I just hacked some small test case together to give an impression of how one could use Guava. Still I don't like the verbosity... sigh :(
package com.pelssers.guava;
import com.google.common.base.Function;
public class Person {
private String firstName;
private String lastName;
private Integer age;
public Person(String firstName, String lastName, Integer age) {
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
}
/************ Extractor Functions *********************************/
public static Function<Person, String> getFirstName() {
return new Function<Person, String>() {
@Override
public String apply(Person person) {
return person.firstName;
}
};
}
public static Function<Person, String> getLastName() {
return new Function<Person, String>() {
@Override
public String apply(Person person) {
return person.lastName;
}
};
}
public static Function<Person, Integer> getAge() {
return new Function<Person, Integer>() {
@Override
public Integer apply(Person person) {
return person.age;
}
};
}
}
view raw gistfile1.java hosted with ❤ by GitHub
package com.pelssers.guava;
import com.google.common.base.Function;
import com.google.common.base.Predicate;
public final class GuavaHelper {
private GuavaHelper() {
}
/**
* Generic function that creates a predicate function from an extractor and predicate
* return a predicate for the value returned by extractor
* @param extractor a function that extracts a property from some input object
* @param predicate a predicate operating on the extracted property
* @return a new composed predicate
*/
public static <I,O> Predicate<I> getPredicate(final Function<I,O> extractor, final Predicate<O> predicate) {
return new Predicate<I>() {
@Override
public boolean apply(I input) {
return predicate.apply(extractor.apply(input));
}
};
}
}
view raw gistfile1.java hosted with ❤ by GitHub
package com.pelssers.guava;
import static com.pelssers.guava.GuavaHelper.*;
import java.util.Collection;
import java.util.List;
import junit.framework.Assert;
import org.junit.Before;
import org.junit.Test;
import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.common.collect.Range;
public class PersonsTest {
private List<Person> persons;
@Before
public void setUp() {
Person person1 = new Person("Robin", "Walker", 35);
Person person2 = new Person("Sander", "Peters", 20);
Person person3 = new Person("Brian", "Maex", 30);
Person person4 = new Person("Sander", "Tielens", 23);
persons = Lists.newArrayList(person1, person2, person3, person4);
}
@Test
public void testTransform() {
//map the list of persons to a list of their ages
List<Integer> ages = Lists.transform(persons, Person.getAge());
Assert.assertEquals(Integer.valueOf(35), ages.get(0));
}
@Test
public void testFilterInteger() {
//filter all persons with age above 25
List<Person> filtered = Lists.newArrayList(Iterables.filter(persons,
getPredicate(Person.getAge(), Range.greaterThan(25))));
int sizeExpected = 2;
Assert.assertEquals(sizeExpected, filtered.size());
}
@Test
public void testFilterString() {
//filter all persons who's firstname is Sander
List<Person> filtered = Lists.newArrayList(Iterables.filter(persons,
getPredicate(Person.getFirstName(), Predicates.equalTo("Sander"))));
int sizeExpected = 2;
Assert.assertEquals(sizeExpected, filtered.size());
Assert.assertEquals("Sander", Person.getFirstName().apply(filtered.get(0)));
}
@Test
public void testFilterComposedPredicate() {
//filter all Sanders older than 22
Predicate<Person> isSanderPredicate = getPredicate(Person.getFirstName(), Predicates.equalTo("Sander"));
Predicate<Person> olderThan22Predicate = getPredicate(Person.getAge(), Range.greaterThan(22));
List<Person> filtered = Lists.newArrayList(Iterables.filter(persons,
Predicates.and(isSanderPredicate, olderThan22Predicate)));
int sizeExpected = 1;
Assert.assertEquals(sizeExpected, filtered.size());
Assert.assertEquals("Tielens", Person.getLastName().apply(filtered.get(0)));
}
}
view raw gistfile1.java hosted with ❤ by GitHub
package com.pelssers.guava
import scala.beans.BeanProperty
object ScalaVersion extends App {
class ScalaPerson(@BeanProperty val firstName:String, @BeanProperty val lastName: String, @BeanProperty val age:Int)
val person1 = new ScalaPerson("Robin", "Walker", 35);
val person2 = new ScalaPerson("Sander", "Peters", 20);
val person3 = new ScalaPerson("Brian", "Maex", 30);
val person4 = new ScalaPerson("Sander", "Tielens", 23);
val persons = List(person1, person2, person3, person4)
//map persons to their ages
val ages = persons.map(_.getAge())
//find all persons older than 25
val personsOlderThan25 = persons.filter(_.getAge() > 25)
//find all Sanders
val personsNamedSander = persons.filter(_.getFirstName().equals("Sander"))
//find all Sanders older than 22
val sandersolderThan22 = persons.filter(p => p.getFirstName().equals("Sander") && p.getAge() > 22)
}
view raw gistfile1.scala hosted with ❤ by GitHub