/**
   * Check and see if this collection of permissions implies the permissions expressed in
   * "permission".
   *
   * @param p the Permission object to compare
   * @return true if "permission" is a proper subset of a permission in the collection, false if
   *     not.
   */
  public boolean implies(Permission permission) {
    if (!(permission instanceof ServicePermission)) return false;

    ServicePermission np = (ServicePermission) permission;
    int desired = np.getMask();
    int effective = 0;
    int needed = desired;

    synchronized (this) {
      int len = perms.size();

      // need to deal with the case where the needed permission has
      // more than one action and the collection has individual permissions
      // that sum up to the needed.

      for (int i = 0; i < len; i++) {
        ServicePermission x = (ServicePermission) perms.get(i);

        // System.out.println("  trying "+x);
        if (((needed & x.getMask()) != 0) && x.impliesIgnoreMask(np)) {
          effective |= x.getMask();
          if ((effective & desired) == desired) return true;
          needed = (desired ^ effective);
        }
      }
    }
    return false;
  }