/**
 * 
 */
package ca.eandb.sortable;

import java.util.LinkedList;
import java.util.List;

import ca.eandb.sortable.Product.Field;

/**
 * @author brad
 *
 */
public final class ProductTrieBuilder implements ProductVisitor {
	
	private final TrieNode root = new TrieNode();

	/*(non-Javadoc)
	 * @see ca.eandb.sortable.ProductVisitor#visit(ca.eandb.sortable.Product)
	 */
	@Override
	public void visit(Product product) {
		addProduct(product);
	}
	
	public void addProduct(Product product) {
		processField(product, Field.MANUFACTURER, product.getManufacturer());
		processField(product, Field.FAMILY, product.getFamily());
		processField(product, Field.MODEL, product.getModel());
	}
	
	private void processField(Product product, Field field, String value) {		
		value = StringUtil.normalize(value);
		String[] words = value.split(" ");
		ProductMatch match = new ProductMatch(product, field);
		
		for (String word : words) {
			TrieNode node = root.insert(word);
			List<ProductMatch> products = (List<ProductMatch>) node.getData();
			
			if (products == null) {
				products = new LinkedList<ProductMatch>();
				node.setData(products);
			}

			products.add(match);
		}
	}
	
	public TrieNode getRoot() {
		return root;
	}

}

@@ -593,14 +593,13 @@
 eld.
-FAMILY
+MODEL
 , pr
@@ -611,19 +611,56 @@
 .get
-Family());%0A
+Model());%0A%09%09if (product.getFamily() != null) %7B%0A%09
 %09%09pr
@@ -677,37 +677,38 @@
 (product, Field.
-MODEL
+FAMILY
 , product.getMod
@@ -704,25 +704,30 @@
 duct.get
-Model());
+Family());%0A%09%09%7D
 %0A%09%7D%0A%09%0A%09p

Check if the optional family field is null before attempting to insert.