KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

30  
31 package com.sun.appserv.management.util.stringifier;
32
33 import java.lang.reflect.Array JavaDoc;
34 import java.util.Arrays JavaDoc;
35 import java.util.Iterator JavaDoc;
36
37 /**
38     Stringifies an array, using an optional array element Stringifier
39  */

40  
41 public final class ArrayStringifier implements Stringifier
42 {
43     final String JavaDoc mDelim;
44     final Stringifier mElementStringifier;
45     boolean mAddBraces;
46     
47     static final char LEFT_BRACE = '{';
48     static final char RIGHT_BRACE = '}';
49     static final String JavaDoc DEFAULT_DELIM=",";
50     
51         public
52     ArrayStringifier()
53     {
54         this( SmartStringifier.DEFAULT );
55         mAddBraces = false;
56     }
57         public
58     ArrayStringifier( boolean addBraces )
59     {
60         this( DEFAULT_DELIM, SmartStringifier.DEFAULT, addBraces );
61     }
62     
63         public
64     ArrayStringifier( String JavaDoc delim )
65     {
66         this( delim, false );
67     }
68     
69         public
70     ArrayStringifier( String JavaDoc delim, boolean addBraces)
71     {
72         this( delim, SmartStringifier.DEFAULT, addBraces );
73     }
74     
75         public
76     ArrayStringifier( Stringifier elementStringifier )
77     {
78         this( DEFAULT_DELIM, elementStringifier );
79     }
80     
81         public
82     ArrayStringifier( String JavaDoc delim, Stringifier elementStringifier )
83     {
84         this( delim, elementStringifier, false );
85     }
86     
87         public
88     ArrayStringifier( String JavaDoc delim, Stringifier elementStringifier, boolean addBraces)
89     {
90         mDelim = delim;
91         mElementStringifier = elementStringifier;
92         mAddBraces = addBraces;
93     }
94     
95         static String JavaDoc
96     addBraces( boolean add, String JavaDoc s )
97     {
98         String JavaDoc out = s;
99         if ( add )
100         {
101             out = LEFT_BRACE + s + RIGHT_BRACE;
102         }
103         return( out );
104     }
105     
106         public String JavaDoc
107     stringify( Object JavaDoc o )
108     {
109         final String JavaDoc s = this.stringify( (Object JavaDoc [])o, mDelim, mElementStringifier );
110         
111         return( addBraces( mAddBraces, s ) );
112     }
113     
114     /**
115         Static variant when direct call will suffice.
116      */

117         public static String JavaDoc
118     stringify( Object JavaDoc [] o, String JavaDoc delim, Stringifier stringifier )
119     {
120         final Iterator JavaDoc iter = Arrays.asList( o ).iterator();
121         final IteratorStringifier iterStringifier = new IteratorStringifier( delim, stringifier );
122         
123         final String JavaDoc s = iterStringifier.stringify( iter );
124         return( s );
125     }
126     
127     /**
128         Static variant when direct call will suffice.
129      */

130         public static String JavaDoc
131     stringify( Object JavaDoc [] o, String JavaDoc delim )
132     {
133         return( stringify( o, delim, SmartStringifier.DEFAULT ) );
134     }
135     
136     public final static ArrayStringifier DEFAULT = new ArrayStringifier( "," );
137 }
138
139
Popular Tags