View Javadoc

1   /*
2    * Copyright 2004-2006 the Seasar Foundation and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.seasar.tuigwaa.util.functor;
17  
18  import java.util.Iterator;
19  import java.util.List;
20  import java.util.Set;
21  
22  import org.seasar.tuigwaa.view.DataViewUtils;
23  
24  import com.isenshi.util.functor.BinaryFunction;
25  import com.isenshi.util.functor.UnaryFunction;
26  
27  public class ArrayViewerFunction implements UnaryFunction {
28  
29  	private BinaryFunction binaryFunction;
30  
31  	public ArrayViewerFunction(BinaryFunction unaryFunction) {
32  		this.binaryFunction = unaryFunction;
33  	}
34  
35  	public Object evaluate(Object data) {
36  		Set set = (Set) data;
37  		if (set == null || set.size() == 0) {
38  			return "";
39  		}
40  
41  		boolean firstFlag = true;
42  
43  		List list = DataViewUtils.sortById(set);
44  
45  		StringBuffer buf = new StringBuffer();
46  		for (Iterator i = list.iterator(); i.hasNext();) {
47  			if (firstFlag) {
48  				firstFlag = false;
49  			} else {
50  				buf.append(" , ");
51  			}
52  			Object obj = i.next();
53  			if (binaryFunction != null) {
54  				obj = binaryFunction.evaluate(obj, null);
55  			}
56  			buf.append(obj);
57  		}
58  		return buf.toString();
59  	}
60  }