KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openlaszlo > utils > ListFormat


1 /* ****************************************************************************
2  * ChainedException.java
3  * ****************************************************************************/

4
5 /* J_LZ_COPYRIGHT_BEGIN *******************************************************
6 * Copyright 2001-2004 Laszlo Systems, Inc. All Rights Reserved. *
7 * Use is subject to license terms. *
8 * J_LZ_COPYRIGHT_END *********************************************************/

9
10 package org.openlaszlo.utils;
11
12 import java.text.*;
13 import java.util.*;
14
15 public class ListFormat extends Format {
16     protected final String JavaDoc junction;
17     
18     public ListFormat(String JavaDoc junction) {
19         this.junction = junction;
20     }
21     
22     public ListFormat() {
23         this("and");
24     }
25     
26     public StringBuffer JavaDoc format(Object JavaDoc obj, StringBuffer JavaDoc buffer,
27                                FieldPosition pos)
28     {
29         List list = (List) obj;
30         int listSize = list.size();
31         for (ListIterator iter = list.listIterator(); iter.hasNext(); ) {
32             if (iter.hasPrevious()) {
33                 if (listSize > 2)
34                     buffer.append(',');
35                 buffer.append(' ');
36                 if (iter.nextIndex() == listSize-1) {
37                     buffer.append(junction);
38                     buffer.append(' ');
39                 }
40             }
41             buffer.append(iter.next());
42         }
43         return buffer;
44     }
45
46     public Object JavaDoc parseObject(String JavaDoc source, ParsePosition parsePosition) {
47         throw new RuntimeException JavaDoc("unimplemented functionality");
48     }
49 }
50
Popular Tags