Wednesday, February 27, 2013

JavaUtils and Apache Common Collections

When it comes to the importance not to repeat yourself while writing code, it is even important to take a look at most common activities. For example, instead of writing a lot of nested for statements, making your code illegible, you can use already Java built-in features. Let's take a look at some really simple utils:

Tested with:
  • commons-collections-3.2.1
  • JUnit 4
  • Maven 3

  • CollectionUtils.select - If you want to retrieve one or more objects from a collection that meet your criteria (like id = 4 or age > 18)
  • CollectionUtils.find - If you want to retrieve only one object from a collection that meet your criteria (like id = 6)
  • Collections.max - If you want to retrieve only one object from a collection, the one which best meets one criteria
  • Collections.sort - If you want to have a collection ordered by some criteria

Data to base our examples on:
@Before
 public void init() {
  Person p1 = new Person(1, "ruben", 17);
  Person p2 = new Person(2, "carlos", 45);
  Person p3 = new Person(3, "ismael", 47);
  Person p4 = new Person(4, "raul", 30);

  personList.add(p1);
  personList.add(p2);
  personList.add(p3);
  personList.add(p4);
 }

CollectionUtils.select
@Test
 public void selectListTest() {
  AdultPredicate adultPredicate = new AdultPredicate();
  Collection adultPersons = CollectionUtils.select(personList, adultPredicate);
  
  Assert.assertNotNull(adultPersons);
  Assert.assertTrue(!adultPersons.isEmpty());
 }

public class AdultPredicate implements Predicate {
 
 @Override
 public boolean evaluate(Object arg0) {
  Person person = (Person) arg0;
  if (person.getAge() != null &&
   person.getAge() >= 18) {
   return true;
  }
  return false;
 }
}

CollectionUtils.find
@Test
 public void findObjectTest() {
  Predicate personNumberTwoPredicate = new PersonNumberTwoPredicate();
  Person personNumberTwo = (Person) CollectionUtils.find(personList, personNumberTwoPredicate);
  
  Assert.assertNotNull(personNumberTwo);
  Assert.assertTrue(personNumberTwo.getId().equals(2));
 }

public class PersonNumberTwoPredicate implements Predicate {

 private static final Integer PERSON_ID_TWO = 2;

 @Override
 public boolean evaluate(Object arg0) {
  Person person = (Person) arg0;
  if (person.getId().equals(PERSON_ID_TWO)) {
   return true;
  }
  return false;
 }
}

Collections.max
@Test
 public void oldestPersonTest() {
  PersonAgeComparator personAgeComparator = new PersonAgeComparator();
  Person oldestPerson = Collections.max(personList, personAgeComparator);
  
  Assert.assertNotNull(oldestPerson);
  Assert.assertTrue(oldestPerson.getAge().equals(47));
 }

public class PersonAgeComparator implements Comparator {

 @Override
 public int compare(Person o1, Person o2) {
  if (o1.getAge() > o2.getAge()) {
   return 1;
  }
  if (o1.getAge().equals(o2.getAge())) {
   return 0;
  }
  return -1;
 }
}

Collections.sort
@Test
 public void sortPersonsTest() {
  PersonAgeComparator personAgeComparator = new PersonAgeComparator();
  //Sort order age ascending
  Collections.sort(personList, personAgeComparator);
  //Sort order age descending
  Collections.sort(personList, ComparatorUtils.reversedComparator(personAgeComparator));
 }

Download the complete example here!

Checkout this: https://subversion.assembla.com/svn/pablo-examples/utils-test

Then from a Terminal get into that folder and run: mvn test
To import it into Eclipse IDE, from a Terminal run: mvn eclipse:eclipse and import it as a Java Project

No comments:

Post a Comment