KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > appserv > management > util > stringifier > IteratorStringifierBase


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23  
24 /*
25  * $Header: /cvs/glassfish/admin-core/mbeanapi/src/java/com/sun/appserv/management/util/stringifier/IteratorStringifierBase.java,v 1.3 2005/12/25 03:51:58 tcfujii Exp $
26  * $Revision: 1.3 $
27  * $Date: 2005/12/25 03:51:58 $
28  */

29  
30 package com.sun.appserv.management.util.stringifier;
31
32 import java.util.Iterator JavaDoc;
33
34
35 /**
36     Stringifies an Iterator, using an optional element Stringifier.
37     
38     Must be subclassed to provide Stringification of an element.
39  */

40  
41 public abstract class IteratorStringifierBase implements Stringifier
42 {
43     public final String JavaDoc mDelim;
44     public final Stringifier mElementStringifier;
45     
46         public
47     IteratorStringifierBase()
48     {
49         this( ObjectStringifier.DEFAULT );
50     }
51     
52         public
53     IteratorStringifierBase( String JavaDoc delim )
54     {
55         this( delim, new SmartStringifier( delim ) );
56     }
57     
58         public
59     IteratorStringifierBase( Stringifier elementStringifier )
60     {
61         this( ",", elementStringifier );
62     }
63     
64         public
65     IteratorStringifierBase( String JavaDoc delim, Stringifier elementStringifier )
66     {
67         mDelim = delim;
68         mElementStringifier = elementStringifier;
69     }
70     
71         public String JavaDoc
72     stringify( Object JavaDoc o )
73     {
74         assert( o != null );
75         Iterator JavaDoc iter = (Iterator JavaDoc)o;
76         
77         return( this.stringify( iter, mDelim, mElementStringifier ) );
78     }
79     
80     
81     /*
82         Subclass may choose to override this.
83      */

84         protected abstract void
85     stringifyElement(
86         Object JavaDoc elem,
87         String JavaDoc delim,
88         StringBuffer JavaDoc buf);
89         
90         
91     
92         public String JavaDoc
93     stringify( Iterator JavaDoc iter, String JavaDoc delim, Stringifier stringifier )
94     {
95         assert( iter != null );
96         
97         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
98     
99         while ( iter.hasNext() )
100         {
101             final Object JavaDoc elem = iter.next();
102             
103             stringifyElement( elem, delim, buf );
104             buf.append( delim );
105         }
106         
107         // strip trailing delimiter
108
final int length = buf.length();
109         if ( length != 0 )
110         {
111             buf.setLength( length - delim.length() );
112         }
113
114         return( buf.toString() );
115     }
116     
117     public final static IteratorStringifier DEFAULT = new IteratorStringifier( "," );
118 }
119
120
Popular Tags