“Functional programming” with google-collection API (now guava)

I was messing around with the google collection API. It has the ability to do functional-like programming.

When using inner classes where you should use closures, one looks forward to Java with closures.

import java.util.Collection;
import java.util.Set;

import org.junit.Test;

import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.collect.Collections2;
import com.google.common.collect.ImmutableSet;


public class GoogleCollectionTest {
	
	public static class Employee {
		private String firstName;
		private String lastName;
		private String phone;
		public Employee(String firstName, String lastName, String phone) {
			this.firstName = firstName;
			this.lastName = lastName;
			this.phone = phone;
		}

		public Employee() {
		}

		public String getFirstName() {
			return firstName;
		}

		public String getLastName() {
			return lastName;
		}

		public String getPhone() {
			return phone;
		}
		
		public String toString() {
			return new StringBuilder(80).append(firstName).append(' ').append(lastName).toString();
		}

	}
	
	public static class Phone {
		private String areaCode;
		private String prefix;
		private String number;
		
		public Phone() {
		}

		public Phone(String areaCode, String prefix, String number) {
			this.areaCode = areaCode;
			this.prefix = prefix;
			this.number = number;
		}

		public String getAreaCode() {
			return areaCode;
		}

		public String getPrefix() {
			return prefix;
		}

		public String getNumber() {
			return number;
		}

		@Override
		public String toString() {
			return new StringBuilder(50).append('(').append(areaCode).append(')')
			.append(prefix).append('-').append(number).toString();
		}
		
	}
	public static Employee employee(String firstName, String lastName, String phone) {
		return new Employee(firstName, lastName, phone);
	}
	
	public static Set employees (Employee... arguments) {
		return  ImmutableSet.of(arguments);
	}
	
	public static class OnlyTucsonEmployees implements Predicate{

		public boolean apply(Employee input) {
			if (input.getPhone().startsWith("520")) {
				return true;
			}
			return false;
		}
		
	}
	
	private static final OnlyTucsonEmployees tucsonEmployeesPredicate = new OnlyTucsonEmployees();
	
	
	
	public static class EmployeeToPhone implements Function {

		@Override
		public Phone apply(Employee from) {
			String[] phoneComps = from.phone.split("-");
			return new Phone(phoneComps[0], phoneComps[1], phoneComps[2]);
		}
		
	}
	
	private static final EmployeeToPhone employeeToPhoneFunction = new EmployeeToPhone();
	
	@Test 
	public void Test() {
		Set  employees = employees(
				employee("Rick", "Hightower", "520-555-1212"),
				employee("Noah", "Hightower", "520-555-1212"),
				employee("Ryan", "Hightower", "520-555-1212"),
				employee("Lucas", "Hightower", "520-555-1212"),
				employee("Whitney", "Hightower", "520-555-1212"),
				employee("Stephane", "Loonadi", "614-888-1515")
		);
		
		Collection tucsonEmployees = Collections2.filter(employees, tucsonEmployeesPredicate);
		System.out.println(tucsonEmployees);
		
		Collection phoneNumbers = Collections2.transform(employees, employeeToPhoneFunction);
		System.out.println(phoneNumbers);
		
		Collection tucsonPhoneNumbers = Collections2.transform(Collections2.filter(employees, tucsonEmployeesPredicate), employeeToPhoneFunction);
		System.out.println(tucsonPhoneNumbers);
	}

}

Post a Comment

Required fields are marked *

*
*