1 package com.puppycrawl.tools.checkstyle.api; 20 21 import java.text.MessageFormat ; 22 import java.util.Arrays ; 23 import java.util.Collections ; 24 import java.util.HashMap ; 25 import java.util.Locale ; 26 import java.util.Map ; 27 import java.util.MissingResourceException ; 28 import java.util.ResourceBundle ; 29 30 31 40 public final class LocalizedMessage 41 implements Comparable 42 { 43 44 private static final int HASH_MULT = 29; 45 46 47 private static Locale sLocale = Locale.getDefault(); 48 49 54 private static final Map BUNDLE_CACHE = 55 Collections.synchronizedMap(new HashMap ()); 56 57 58 private final int mLineNo; 59 60 private final int mColNo; 61 62 63 private final SeverityLevel mSeverityLevel; 64 65 66 private final String mModuleId; 67 68 69 private static final SeverityLevel DEFAULT_SEVERITY = SeverityLevel.ERROR; 70 71 72 private final String mKey; 73 74 75 private final Object [] mArgs; 76 77 78 private final String mBundle; 79 80 81 private final Class mSourceClass; 82 83 84 public boolean equals(Object aObject) 85 { 86 if (this == aObject) { 87 return true; 88 } 89 if (!(aObject instanceof LocalizedMessage)) { 90 return false; 91 } 92 93 final LocalizedMessage localizedMessage = (LocalizedMessage) aObject; 94 95 if (mColNo != localizedMessage.mColNo) { 96 return false; 97 } 98 if (mLineNo != localizedMessage.mLineNo) { 99 return false; 100 } 101 if (!mKey.equals(localizedMessage.mKey)) { 102 return false; 103 } 104 105 if (!Arrays.equals(mArgs, localizedMessage.mArgs)) { 106 return false; 107 } 108 110 112 return true; 113 } 114 115 118 public int hashCode() 119 { 120 int result; 121 result = mLineNo; 122 result = HASH_MULT * result + mColNo; 123 result = HASH_MULT * result + mKey.hashCode(); 124 for (int i = 0; i < mArgs.length; i++) { 125 result = HASH_MULT * result + mArgs[i].hashCode(); 126 } 127 return result; 128 } 129 130 142 public LocalizedMessage(int aLineNo, 143 int aColNo, 144 String aBundle, 145 String aKey, 146 Object [] aArgs, 147 SeverityLevel aSeverityLevel, 148 String aModuleId, 149 Class aSourceClass) 150 { 151 mLineNo = aLineNo; 152 mColNo = aColNo; 153 mKey = aKey; 154 mArgs = aArgs; 155 mBundle = aBundle; 156 mSeverityLevel = aSeverityLevel; 157 mModuleId = aModuleId; 158 mSourceClass = aSourceClass; 159 } 160 161 172 public LocalizedMessage(int aLineNo, 173 int aColNo, 174 String aBundle, 175 String aKey, 176 Object [] aArgs, 177 String aModuleId, 178 Class aSourceClass) 179 { 180 this(aLineNo, 181 aColNo, 182 aBundle, 183 aKey, 184 aArgs, 185 DEFAULT_SEVERITY, 186 aModuleId, 187 aSourceClass); 188 } 189 190 201 public LocalizedMessage(int aLineNo, 202 String aBundle, 203 String aKey, 204 Object [] aArgs, 205 SeverityLevel aSeverityLevel, 206 String aModuleId, 207 Class aSourceClass) 208 { 209 this(aLineNo, 0, aBundle, aKey, aArgs, aSeverityLevel, aModuleId, 210 aSourceClass); 211 } 212 213 224 public LocalizedMessage( 225 int aLineNo, 226 String aBundle, 227 String aKey, 228 Object [] aArgs, 229 String aModuleId, 230 Class aSourceClass) 231 { 232 this(aLineNo, 0, aBundle, aKey, aArgs, DEFAULT_SEVERITY, aModuleId, 233 aSourceClass); 234 } 235 236 237 public String getMessage() 238 { 239 try { 240 final ResourceBundle bundle = getBundle(mBundle); 245 final String pattern = bundle.getString(mKey); 246 return MessageFormat.format(pattern, mArgs); 247 } 248 catch (final MissingResourceException ex) { 249 return MessageFormat.format(mKey, mArgs); 253 } 254 } 255 256 263 private ResourceBundle getBundle(String aBundleName) 264 { 265 synchronized (BUNDLE_CACHE) { 266 ResourceBundle bundle = (ResourceBundle ) BUNDLE_CACHE 267 .get(aBundleName); 268 if (bundle == null) { 269 bundle = ResourceBundle.getBundle(aBundleName, sLocale, 270 mSourceClass.getClassLoader()); 271 BUNDLE_CACHE.put(aBundleName, bundle); 272 } 273 return bundle; 274 } 275 } 276 277 278 public int getLineNo() 279 { 280 return mLineNo; 281 } 282 283 284 public int getColumnNo() 285 { 286 return mColNo; 287 } 288 289 290 public SeverityLevel getSeverityLevel() 291 { 292 return mSeverityLevel; 293 } 294 295 296 public String getModuleId() 297 { 298 return mModuleId; 299 } 300 301 307 public String getKey() 308 { 309 return mKey; 310 } 311 312 313 public String getSourceName() 314 { 315 return mSourceClass.getName(); 316 } 317 318 319 public static void setLocale(Locale aLocale) 320 { 321 sLocale = aLocale; 322 } 323 324 328 329 public int compareTo(Object aOther) 330 { 331 final LocalizedMessage lt = (LocalizedMessage) aOther; 332 if (getLineNo() == lt.getLineNo()) { 333 if (getColumnNo() == lt.getColumnNo()) { 334 return getMessage().compareTo(lt.getMessage()); 335 } 336 return (getColumnNo() < lt.getColumnNo()) ? -1 : 1; 337 } 338 339 return (getLineNo() < lt.getLineNo()) ? -1 : 1; 340 } 341 } 342 | Popular Tags |