| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| CollectionFormatter |
|
| 6.0;6 |
| 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.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 (rice.collab@kuali.org) | |
| 26 | * | |
| 27 | */ | |
| 28 | 0 | public class CollectionFormatter extends Formatter { |
| 29 | ||
| 30 | private static final long serialVersionUID = 1074862354812893254L; | |
| 31 | ||
| 32 | /** | |
| 33 | * Formats a collection into a string that looks like an array. | |
| 34 | * | |
| 35 | * @see org.kuali.rice.core.web.format.Formatter#format(java.lang.Object) | |
| 36 | */ | |
| 37 | @Override | |
| 38 | public Object format(Object value) { | |
| 39 | 0 | StringBuilder buf = new StringBuilder(); |
| 40 | 0 | buf.append("["); |
| 41 | ||
| 42 | 0 | if (!(value instanceof Collection)) { |
| 43 | 0 | throw new IllegalArgumentException("value was not an instance of Collection, instead was: " + (value == null ? null : value.getClass())); |
| 44 | } | |
| 45 | ||
| 46 | @SuppressWarnings("unchecked") | |
| 47 | 0 | Collection<Object> collection = (Collection<Object>)value; |
| 48 | 0 | Iterator<Object> i = collection.iterator(); |
| 49 | 0 | boolean hasNext = i.hasNext(); |
| 50 | 0 | while (hasNext) { |
| 51 | 0 | Object elem = i.next(); |
| 52 | 0 | buf.append(elem); |
| 53 | ||
| 54 | 0 | hasNext = i.hasNext(); |
| 55 | 0 | if (hasNext) { |
| 56 | 0 | buf.append(", "); |
| 57 | } | |
| 58 | 0 | } |
| 59 | 0 | buf.append("]"); |
| 60 | 0 | return buf.toString(); |
| 61 | } | |
| 62 | ||
| 63 | } |