01: /*
02: * Copyright 2007 The Kuali Foundation
03: *
04: * Licensed under the Educational Community License, Version 1.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.opensource.org/licenses/ecl1.php
09: *
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.core.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 (kuali-rice@googlegroups.com)
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.core.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: }
|