View Javadoc
1   package org.kuali.common.util.tree;
2   
3   import static org.kuali.common.util.base.Precondition.checkNotNull;
4   
5   import com.google.common.base.Function;
6   
7   public final class NodeStringFunction<T> implements Function<Node<T>, String> {
8   
9   	public NodeStringFunction(Function<T, String> function) {
10  		checkNotNull(function, "function");
11  		this.function = function;
12  	}
13  
14  	/**
15  	 * Convert the element contained in each node to a string by calling it's toString() method
16  	 */
17  	public static <T> NodeStringFunction<T> create() {
18  		return create(new ToStringFunction<T>());
19  	}
20  
21  	/**
22  	 * Convert the element contained in each node to a string by invoking {@code function} on it
23  	 */
24  	public static <T> NodeStringFunction<T> create(Function<T, String> function) {
25  		return new NodeStringFunction<T>(function);
26  	}
27  
28  	private final Function<T, String> function;
29  
30  	@Override
31  	public String apply(Node<T> node) {
32  		checkNotNull(node, "node");
33  		checkNotNull(node.getElement(), "node.element");
34  		return function.apply(node.getElement());
35  	}
36  
37  	private static class ToStringFunction<T> implements Function<T, String> {
38  
39  		@Override
40  		public String apply(T element) {
41  			checkNotNull(element, "element");
42  			return element.toString();
43  		}
44  	}
45  
46  }