View Javadoc

1   /*
2    * Copyright 2007-2008 The Kuali Foundation
3    *
4    * Licensed under the Educational Community 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.opensource.org/licenses/ecl2.php
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, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.kns.web.format;
17  
18  import java.util.Collection;
19  import java.util.Iterator;
20  
21  /**
22   * Formats a collection into a string that looks like an array.  To print out each element,
23   * the toString method of each element is called. 
24   * 
25   * @author Kuali Rice Team (rice.collab@kuali.org)
26   *
27   */
28  public class CollectionFormatter extends Formatter {
29  
30      /**
31       * Formats a collection into a string that looks like an array.
32       * 
33       * @see org.kuali.rice.kns.web.format.Formatter#format(java.lang.Object)
34       */
35      @Override
36      public Object format(Object value) {
37  	StringBuilder buf = new StringBuilder();
38  	buf.append("[");
39  	
40  	Collection collection = (Collection) value;
41  	Iterator i = collection.iterator();
42  	boolean hasNext = i.hasNext();
43  	while (hasNext) {
44  	    Object elem = i.next();
45  	    buf.append(elem);
46  	    
47  	    hasNext = i.hasNext();
48  	    if (hasNext) {
49  		buf.append(", ");
50  	    }
51  	}
52  	buf.append("]");
53  	return buf.toString();
54      }
55  
56  }