Code example for SortedMap

Methods: get, remove

0
  private SortedMap<String, Lease> sortedLeasesByPath = new TreeMap<String, Lease>();
 
  LeaseManager(FSNamesystem fsnamesystem) {this.fsnamesystem = fsnamesystem;}
 
  Lease getLease(String holder) {
    return leases.get(holder);
  } 
   
  SortedSet<Lease> getSortedLeases() {return sortedLeases;}
 
  /** @return the lease containing src */ 
  public Lease getLeaseByPath(String src) {return sortedLeasesByPath.get(src);}
 
  /** @return the number of leases currently in the system */ 
  public synchronized int countLease() {return sortedLeases.size();}
 
  /** @return the number of paths contained in all leases */ 
  synchronized int countPath() { 
    int count = 0;
    for(Lease lease : sortedLeases) {
      count += lease.getPaths().size();
    } 
    return count;
  } 
   
  /** 
   * Adds (or re-adds) the lease for the specified file. 
   */ 
  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");
      } 
    } 
  }