KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > appserv > management > util > misc > StringifiedList


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 package com.sun.appserv.management.util.misc;
24
25 import java.util.List JavaDoc;
26 import java.util.ArrayList JavaDoc;
27
28 import com.sun.appserv.management.util.stringifier.ArrayStringifier;
29
30
31 /**
32     Maintain a list of Strings, ensuring that a String constructor may be
33     used to repopulate the list and that toString() generates a String appropriate
34     for such use.
35     
36     NOTE: this should be improved so that items may contain the delimiter
37  */

38 public class StringifiedList
39 {
40     final List JavaDoc<String JavaDoc> mItems;
41     final char mDelim;
42     
43     public final static char DEFAULT_DELIM = ',';
44     
45     /**
46         Create a new list with the delimiter DEFAULT_DELIM
47      */

48         public
49     StringifiedList( final String JavaDoc listString )
50     {
51         this( listString, DEFAULT_DELIM );
52     }
53     
54     /**
55         Create a new list with the specified delimiter
56      */

57         public
58     StringifiedList( final String JavaDoc[] items, final char delim )
59     {
60         mDelim = delim;
61         mItems = new ArrayList JavaDoc<String JavaDoc>();
62         
63         for( int i = 0; i < items.length; ++i )
64         {
65             append( items[ i ] );
66         }
67     }
68     
69     /**
70         Create a new list with the specified delimiter, with contents taken from the
71         supplied String.
72         
73         @param listString the string containing 0 or more items for the list
74         @param delim the delimiter between items
75      */

76         public
77     StringifiedList( final String JavaDoc listString, final char delim )
78     {
79         mDelim = delim;
80         
81         mItems = new ArrayList JavaDoc<String JavaDoc>();
82         
83         if ( listString != null )
84         {
85             final StringEscaper escaper = new StringEscaper( "" + mDelim );
86         
87             final String JavaDoc [] list = listString.trim().split( "" + delim );
88             
89             // first listed should be first in priority, so add to end
90
for ( int i = 0; i < list.length; ++i )
91             {
92                 mItems.add( escaper.unescape( list[ i ] ) );
93             }
94         }
95     }
96     
97         public String JavaDoc
98     toString()
99     {
100         final StringEscaper escaper = new StringEscaper( "" + mDelim );
101         final String JavaDoc[] items = toArray();
102         
103         for( int i = 0; i < items.length; ++i )
104         {
105             items[ i ] = escaper.escape( items[ i ] );
106         }
107
108         return( ArrayStringifier.stringify( items, "" + mDelim ) );
109     }
110     
111         public String JavaDoc []
112     toArray()
113     {
114         return( (String JavaDoc [])mItems.toArray( new String JavaDoc[ mItems.size() ] ) );
115     }
116     
117         public boolean
118     exists( final String JavaDoc name )
119     {
120         return( mItems.contains( name ) );
121     }
122     
123         public java.util.Iterator JavaDoc
124     iterator()
125     {
126         return( mItems.iterator() );
127     }
128     
129     
130         public void
131     prepend( String JavaDoc item )
132     {
133         mItems.add( 0, item );
134     }
135     
136         public void
137     append( String JavaDoc item )
138     {
139         mItems.add( item );
140     }
141     
142     
143         public void
144     remove( String JavaDoc item )
145     {
146         if ( exists( item ) )
147         {
148             mItems.remove( item );
149         }
150     }
151 }
152
153
Popular Tags