KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > cli > jmx > support > ParseResult


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-cli/cli-api/src/java/com/sun/cli/jmx/support/ParseResult.java,v 1.3 2005/12/25 03:45:49 tcfujii Exp $
26  * $Revision: 1.3 $
27  * $Date: 2005/12/25 03:45:49 $
28  */

29  
30
31  
32 package com.sun.cli.jmx.support;
33
34 import com.sun.cli.util.stringifier.ArrayStringifier;
35
36 final class ParseResult
37 {
38     final static int LITERAL_STRING = 0;
39     final static int OTHER = 1;
40     final static int ARRAY = 2;
41     
42     int mType;
43     String JavaDoc mTypeCast;
44     String JavaDoc mName;
45     Object JavaDoc mData;
46     
47
48     ParseResult( int type, Object JavaDoc data)
49     {
50         mType = type;
51         mTypeCast = null;
52         mName = null;
53         mData = data;
54     }
55     
56     ParseResult( int type, Object JavaDoc data, String JavaDoc typecast)
57     {
58         mType = type;
59         mTypeCast = typecast;
60         mName = null;
61         mData = data;
62     }
63     
64     ParseResult( int type, Object JavaDoc data, String JavaDoc typecast, String JavaDoc name)
65     {
66         mType = type;
67         mTypeCast = typecast;
68         mName = name;
69         mData = data;
70     }
71     
72         public int
73     getType()
74     {
75         return( mType );
76     }
77     
78         public void
79     setType( int type )
80     {
81         mType = type;
82         if ( ! (mData instanceof String JavaDoc) )
83         {
84             throw new IllegalArgumentException JavaDoc(
85                 "can't set non-String to type LITERAL_STRING: " + mData.getClass().getName());
86         }
87     }
88     
89         public Object JavaDoc
90     getData()
91     {
92         return( mData );
93     }
94     
95         public void
96     setData( Object JavaDoc data )
97     {
98         mData = data;
99     }
100     
101         public void
102     setTypeCast( String JavaDoc typeCast )
103     {
104         mTypeCast = typeCast;
105     }
106     
107         public String JavaDoc
108     getTypeCast()
109     {
110         return( mTypeCast );
111     }
112     
113     
114         public void
115     setName( String JavaDoc name )
116     {
117         mName = name;
118     }
119     
120         public String JavaDoc
121     getName()
122     {
123         return( mName );
124     }
125     
126         public String JavaDoc
127     toString()
128     {
129         return( this.toString( ',' ) );
130     }
131     
132         boolean
133     equalString( String JavaDoc s1, String JavaDoc s2 )
134     {
135         if ( s1 == s2 )
136             return( true );
137         
138         // they can't both be null now
139

140         if ( s1 != null )
141         {
142             return( s1.equals( s2 ) );
143         }
144         
145         // s2 != null
146
return( s2.equals( s1 ) );
147     }
148     
149         public static boolean
150     checkEqualArrays( final ParseResult [] lhs, final ParseResult [] rhs )
151     {
152         if ( lhs == rhs )
153             return( true );
154             
155         boolean equal = lhs.length == rhs.length;
156         
157         if ( equal )
158         {
159             for( int i = 0; i < lhs.length; ++i )
160             {
161                 equal = lhs[ i ].mData.equals( rhs[ i ].mData );
162                 if ( ! equal )
163                     break;
164             }
165         }
166         
167         return( equal );
168     }
169     
170         public boolean
171     equals( final Object JavaDoc o )
172     {
173         if ( o == this )
174             return( true );
175         if ( ! (o instanceof ParseResult) )
176             return( false );
177         
178         
179         final ParseResult rhs = (ParseResult)o;
180         
181         boolean dataEqual = false;
182         
183         if ( mData instanceof ParseResult [] )
184         {
185             dataEqual = checkEqualArrays( (ParseResult [])mData, (ParseResult [])rhs.mData);
186         }
187         else
188         {
189             dataEqual = mData.equals( rhs.mData );
190         }
191         
192         final boolean equal =
193             mType == rhs.mType &&
194             dataEqual &&
195             equalString( mName, rhs.mName ) &&
196             equalString( mTypeCast, rhs.mTypeCast );
197         
198             
199         return( equal );
200     }
201     
202         public String JavaDoc
203     toString( final char delim )
204     {
205         final int type = getType();
206         final Object JavaDoc data = getData();
207         
208         String JavaDoc result = "";
209         
210         if ( type == ParseResult.LITERAL_STRING )
211         {
212             result = (String JavaDoc)data;
213         }
214         else if ( type == ParseResult.OTHER )
215         {
216             result = (String JavaDoc)data;
217         }
218         else if ( type == ParseResult.ARRAY )
219         {
220             final ParseResult [] contents = (ParseResult [])data;
221             
222             result = "{" + ArrayStringifier.stringify( contents, "" + delim ) + "}";
223         }
224         else
225         {
226             assert( false );
227         }
228         
229         final String JavaDoc typeCast = getTypeCast();
230         if ( typeCast != null )
231         {
232             result = "(" + typeCast + ")" + result;
233         }
234         
235         final String JavaDoc name = getName();
236         if ( name != null )
237         {
238             result = name + "=" + result;
239         }
240         
241         return( result );
242     }
243 }
244
245
Popular Tags