Code example for SortedSet

Methods: add, remove

0
  synchronized Lease addLease(String holder, String src) {
    Lease lease = getLease(holder);
    if (lease == null) {
      lease = new Lease(holder);
      leases.put(holder, lease);
      sortedLeases.add(lease);
    } else { 
      renewLease(lease);
    } 
    sortedLeasesByPath.put(src, lease);
    lease.paths.add(src);
    return lease;
  } 
 
  /** 
   * Remove the specified lease and src. 
   */ 
  synchronized void removeLease(Lease lease, String src) {
    sortedLeasesByPath.remove(src);
    if (!lease.removePath(src)) {
      LOG.error(src + " not found in lease.paths (=" + lease.paths + ")");
    } 
 
    if (!lease.hasPath()) {
      leases.remove(lease.holder);
      if (!sortedLeases.remove(lease)) {
        LOG.error(lease + " not found in sortedLeases");
      } 
    } 
  } 
 
  /** 
   * Remove the lease for the specified holder and src 
   */ 
  synchronized void removeLease(String holder, String src) {
    Lease lease = getLease(holder);
    if (lease != null) {
      removeLease(lease, src);
    } 
  } 
 
  /** 
   * Reassign lease for file src to the new holder. 
   */ 
  synchronized Lease reassignLease(Lease lease, String src, String newHolder) {
    assert newHolder != null : "new lease holder is null";
    if (lease != null) {
      removeLease(lease, src);
    } 
    return addLease(newHolder, src);
  } 
 
  /** 
   * Finds the pathname for the specified pendingFile 
   */ 
  synchronized String findPath(INodeFileUnderConstruction pendingFile)
      throws IOException { 
    Lease lease = getLease(pendingFile.getClientName());
    if (lease != null) {
      String src = lease.findPath(pendingFile);
      if (src != null) {
        return src;
      } 
    } 
    throw new IOException("pendingFile (=" + pendingFile + ") not found."
        + "(lease=" + lease + ")");
  } 
 
  /** 
   * Renew the lease(s) held by the given client 
   */ 
  synchronized void renewLease(String holder) {
    renewLease(getLease(holder));
  } 
  synchronized void renewLease(Lease lease) {
    if (lease != null) {
      sortedLeases.remove(lease);
      lease.renew();
      sortedLeases.add(lease);
    } 
  } 
 
  /************************************************************ 
   * A Lease governs all the locks held by a single client.