1 package com.puppycrawl.tools.checkstyle.checks.imports; 20 21 25 class Guard 26 { 27 28 private final boolean mAllowed; 29 30 private final String mPkgName; 31 32 private final String mClassName; 33 34 38 private final boolean mExactMatch; 39 40 private final boolean mLocalOnly; 41 42 49 Guard(final boolean aAllow, final boolean aLocalOnly, 50 final String aPkgName, final boolean aExactMatch) 51 { 52 mAllowed = aAllow; 53 mLocalOnly = aLocalOnly; 54 mPkgName = aPkgName; 55 mClassName = null; 56 mExactMatch = aExactMatch; 57 } 58 59 65 Guard(final boolean aAllow, final boolean aLocalOnly, 66 final String aClassName) 67 { 68 mAllowed = aAllow; 69 mLocalOnly = aLocalOnly; 70 mPkgName = null; 71 mClassName = aClassName; 72 mExactMatch = true; } 74 75 80 AccessResult verifyImport(final String aForImport) 81 { 82 assert aForImport != null; 83 if (mClassName != null) { 84 final boolean classMatch = mClassName.equals(aForImport); 85 return calculateResult(classMatch); 86 } 87 88 assert mPkgName != null; 93 boolean pkgMatch = aForImport.startsWith(mPkgName + "."); 95 if (pkgMatch && mExactMatch) { 96 pkgMatch = (aForImport.indexOf('.', (mPkgName.length() + 1)) == -1); 97 } 98 return calculateResult(pkgMatch); 99 } 100 101 104 boolean isLocalOnly() 105 { 106 return mLocalOnly; 107 } 108 109 115 private AccessResult calculateResult(final boolean aMatched) 116 { 117 if (aMatched) { 118 return mAllowed ? AccessResult.ALLOWED : AccessResult.DISALLOWED; 119 } 120 return AccessResult.UNKNOWN; 121 } 122 } 123 | Popular Tags |