KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > common > ParamList


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 package com.sun.enterprise.admin.common;
25
26 import java.io.Serializable JavaDoc;
27 import java.util.*;
28
29
30 /**
31     ParamList encapsulates a list of Param objects
32     <p>
33     The exact storage method is an implementation detail. ParamList is
34     intended to be used by building it, then using it; it is not intended
35     for removal, sorting, etc operations.
36     @author Lloyd Chambers
37     @version 1.0
38 */

39
40 public class ParamList implements Serializable JavaDoc
41 {
42         public static long serialVersionUID = 5433279640964105821L;
43
44     private Vector mParams;
45
46     private static final String JavaDoc kDelimiterString = ", ";
47     private static final int kInitialCapacity = 4;
48
49     /**
50         Constructs a new, empty ParamList with a small initial capacity.
51     */

52     
53     public ParamList()
54     {
55         mParams = new Vector( kInitialCapacity );
56     }
57
58     /**
59         Adds a new paramater to the list. If the <it>same</it>
60                 parameter is passed again, it will replace the existing one.
61
62         @param param the Param to be added to the list
63     */

64         
65     public void addParam( Param param )
66     {
67             //Assert.insist(param!=null, "null");
68
int elementIndex = mParams.indexOf( param );
69             boolean paramExists = ( elementIndex != -1 );
70             
71             if( paramExists )
72             {
73                 mParams.setElementAt ( param, elementIndex );
74             }
75             else
76             {
77                 mParams.add( param );
78             }
79     }
80
81     /**
82             Adds a new paramater to the end of the list by creating it
83             from the specified name and value.
84             
85             @param name the name to be used to create the Param
86             @param value the value to be used to create the Param
87     */

88     
89     public void addParam( String JavaDoc name, Serializable JavaDoc value )
90     {
91         this.addParam( new Param (name, value ) );
92     }
93
94     /**
95         Finds a parameter, given its name. Names are case-sensitive.
96
97         @param name the name of the parameter
98
99         @return a Param if found, otherwise null
100     */

101     
102     public Param getParam( String JavaDoc name )
103     {
104         Param resultParam = null;
105
106         final int numItems = mParams.size( );
107         
108                 Iterator iter = getElements( );
109                 while ( iter.hasNext( ) )
110                 {
111                     Param aParam = ( Param )iter.next( );
112                     
113                     if( name.equals( aParam.mName ) )
114                     {
115             resultParam = aParam;
116             break;
117                     }
118                 }
119
120                 return ( resultParam );
121     }
122
123     /**
124         Returns an Iterator which can be used to iterate through all Params
125         in the list.
126
127         @return an Iterator object
128     */

129         
130     public Iterator getElements( )
131     {
132         // rely on built-in capability of vector
133
return ( mParams.iterator( ) );
134     }
135
136     /**
137         Constructs a String representation of the ParamList.
138         <p>
139         The resulting String is of the form:<n>
140             "name: value, name: value, ..."
141
142         @return an String represention of the list
143     */

144     public String JavaDoc toString()
145     {
146         Iterator iter = getElements();
147
148         StringBuffer JavaDoc buf = new StringBuffer JavaDoc( 256 );
149
150         while ( iter.hasNext() )
151         {
152             Param item = (Param)iter.next();
153
154             buf.append( item );
155             buf.append( kDelimiterString );
156         }
157         // strip last trailing comma
158
if ( buf.length() != 0 )
159         {
160             buf.setLength( buf.length() - kDelimiterString.length() );
161         }
162
163         return( new String JavaDoc( buf ) );
164     }
165 }
Popular Tags