Source code: org/progeeks/meta/format/DefaultListPropertyFormat.java
1 /*
2 * $Id: DefaultListPropertyFormat.java,v 1.2 2003/01/02 23:20:05 rconner Exp $
3 *
4 * Copyright (c) 2001-2002, Paul Speed
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * 1) Redistributions of source code must retain the above copyright notice,
12 * this list of conditions and the following disclaimer.
13 * 2) Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3) Neither the names "Progeeks", "Meta-JB", nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 package org.progeeks.meta.format;
34
35 import java.util.Arrays;
36 import java.util.List;
37 import java.util.Iterator;
38
39 import org.progeeks.meta.*;
40 import org.progeeks.util.*;
41
42 /**
43 * Default implementation for converting a list of objects to
44 * and from text data. This assumes that the values'
45 * have a useful toString() method. This formatter can only
46 * be used for rendering.
47 *
48 * @version $Revision: 1.2 $
49 * @author Paul Speed
50 */
51 public class DefaultListPropertyFormat extends AbstractPropertyFormat
52 {
53 private String delimiter;
54 private PropertyFormat valueFormat;
55
56 /**
57 * Creates a property format object that will output a list
58 * of values by converting them individually with the specified
59 * property format object and then appending them together separated
60 * by the specified delimiter.
61 */
62 public DefaultListPropertyFormat( PropertyFormat valueFormat, String delimiter )
63 {
64 this.valueFormat = valueFormat;
65 this.delimiter = delimiter;
66 }
67
68 /**
69 * Returns the formatted String for the specified Object.
70 */
71 public String format( Object obj )
72 {
73 return( format( obj, new StringBuffer() ).toString() );
74 }
75
76 /**
77 * Appends the formatted string to the specified StringBuffer
78 * and returns the supplied StringBuffer.
79 */
80 public StringBuffer format( Object obj, StringBuffer appendTo )
81 {
82 if( obj == null )
83 return( appendTo );
84
85 if( obj.getClass().isArray() )
86 {
87 obj = Arrays.asList( (Object[]) obj );
88 }
89
90 for( Iterator i = ((List)obj).iterator(); i.hasNext(); )
91 {
92 Object val = i.next();
93 appendTo.append( valueFormat.format( val ) );
94 if( delimiter != null && i.hasNext() )
95 appendTo.append( delimiter );
96 }
97
98 return( appendTo );
99 }
100
101 /**
102 * Parses the text starting at the specified index and converts
103 * it into an Object of the appropriate type. If this method is
104 * not supported then an UnsupportedOperationException will be
105 * thrown.
106 */
107 public Object parseObject( String source, int index )
108 {
109 throw new UnsupportedOperationException( "Parsing not supported by this formatter." );
110 }
111 }