1 23 24 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 mTypeCast; 44 String mName; 45 Object mData; 46 47 48 ParseResult( int type, Object data) 49 { 50 mType = type; 51 mTypeCast = null; 52 mName = null; 53 mData = data; 54 } 55 56 ParseResult( int type, Object data, String typecast) 57 { 58 mType = type; 59 mTypeCast = typecast; 60 mName = null; 61 mData = data; 62 } 63 64 ParseResult( int type, Object data, String typecast, String 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 ) ) 83 { 84 throw new IllegalArgumentException ( 85 "can't set non-String to type LITERAL_STRING: " + mData.getClass().getName()); 86 } 87 } 88 89 public Object 90 getData() 91 { 92 return( mData ); 93 } 94 95 public void 96 setData( Object data ) 97 { 98 mData = data; 99 } 100 101 public void 102 setTypeCast( String typeCast ) 103 { 104 mTypeCast = typeCast; 105 } 106 107 public String 108 getTypeCast() 109 { 110 return( mTypeCast ); 111 } 112 113 114 public void 115 setName( String name ) 116 { 117 mName = name; 118 } 119 120 public String 121 getName() 122 { 123 return( mName ); 124 } 125 126 public String 127 toString() 128 { 129 return( this.toString( ',' ) ); 130 } 131 132 boolean 133 equalString( String s1, String s2 ) 134 { 135 if ( s1 == s2 ) 136 return( true ); 137 138 140 if ( s1 != null ) 141 { 142 return( s1.equals( s2 ) ); 143 } 144 145 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 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 203 toString( final char delim ) 204 { 205 final int type = getType(); 206 final Object data = getData(); 207 208 String result = ""; 209 210 if ( type == ParseResult.LITERAL_STRING ) 211 { 212 result = (String )data; 213 } 214 else if ( type == ParseResult.OTHER ) 215 { 216 result = (String )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 typeCast = getTypeCast(); 230 if ( typeCast != null ) 231 { 232 result = "(" + typeCast + ")" + result; 233 } 234 235 final String name = getName(); 236 if ( name != null ) 237 { 238 result = name + "=" + result; 239 } 240 241 return( result ); 242 } 243 } 244 245 | Popular Tags |