1 package com.puppycrawl.tools.checkstyle.checks.imports; 20 21 import java.util.ArrayList ; 22 import java.util.Iterator ; 23 import java.util.LinkedList ; 24 import java.util.List ; 25 26 33 class PkgControl 34 { 35 36 private final LinkedList mGuards = new LinkedList (); 37 38 private final List mChildren = new ArrayList (); 39 40 private final PkgControl mParent; 41 42 private final String mFullPackage; 43 44 48 PkgControl(final String aPkgName) 49 { 50 assert aPkgName != null; 51 mParent = null; 52 mFullPackage = aPkgName; 53 } 54 55 60 PkgControl(final PkgControl aParent, final String aSubPkg) 61 { 62 assert aParent != null; 63 assert aSubPkg != null; 64 mParent = aParent; 65 mFullPackage = aParent.getFullPackage() + "." + aSubPkg; 66 mParent.mChildren.add(this); 67 } 68 69 73 void addGuard(final Guard aThug) 74 { 75 mGuards.addFirst(aThug); 76 } 77 78 81 String getFullPackage() 82 { 83 return mFullPackage; 84 } 85 86 91 PkgControl locateFinest(final String aForPkg) 92 { 93 if (!aForPkg.startsWith(getFullPackage())) { 97 return null; 98 } 99 100 final Iterator it = mChildren.iterator(); 102 while (it.hasNext()) { 103 final PkgControl pc = (PkgControl) it.next(); 104 final PkgControl match = pc.locateFinest(aForPkg); 105 if (match != null) { 106 return match; 107 } 108 } 109 110 return this; 112 } 113 114 124 AccessResult checkAccess(final String aForImport, final String aInPkg) 125 { 126 final AccessResult retVal = localCheckAccess(aForImport, aInPkg); 127 if (retVal != AccessResult.UNKNOWN) { 128 return retVal; 129 } 130 else if (mParent == null) { 131 return AccessResult.DISALLOWED; 133 } 134 135 return mParent.checkAccess(aForImport, aInPkg); 136 } 137 138 145 private AccessResult localCheckAccess(final String aForImport, 146 final String aInPkg) 147 { 148 final Iterator it = mGuards.iterator(); 149 while (it.hasNext()) { 150 final Guard g = (Guard) it.next(); 151 if (g.isLocalOnly() && !mFullPackage.equals(aInPkg)) { 153 continue; 154 } 155 final AccessResult result = g.verifyImport(aForImport); 156 if (result != AccessResult.UNKNOWN) { 157 return result; 158 } 159 } 160 return AccessResult.UNKNOWN; 161 } 162 } 163 | Popular Tags |