Program: 49509
Project: 49509
Buggy source:
/** 
 * @param subsToImport Reconciles incoming entitlements to existing subscriptions. Each set is mapped against the upstream pool id. First match attempt will use entitlement id from incoming entitlements for comparison to existing subscriptions. Next attempt will use the exact quantity for comparison. This is to cover scenarios where the intent is to re-establish the distributor from the host. The final attempt will use ordering of the remaining incoming entitlements and of remaining existing subscriptions in descending order by quantity. Either the remaining subscriptions will be deleted, or the unmatched incoming entitlements will be turned into new subscriptions.
 */
public void store(Owner owner,Set<Subscription> subsToImport){
  Map<String,Map<String,Subscription>> existingSubsByUpstreamPool=mapSubsByUpstreamPool(owner);
  Set<Subscription> subscriptionsStillToImport=new HashSet<Subscription>();
  for (  Subscription subscription : subsToImport) {
    Subscription local=null;
    Map<String,Subscription> map=existingSubsByUpstreamPool.get(subscription.getUpstreamPoolId());
    if (map == null || map.isEmpty()) {
      createSubscription(subscription);
      log.info("String_Node_Str" + subscription.getUpstreamEntitlementId() + "String_Node_Str");
      continue;
    }
    local=map.get(subscription.getUpstreamEntitlementId());
    if (local != null) {
      mergeSubscription(subscription,local,map);
      log.info("String_Node_Str" + subscription.getUpstreamEntitlementId() + "String_Node_Str"+ local.getUpstreamEntitlementId()+ "String_Node_Str");
    }
 else {
      subscriptionsStillToImport.add(subscription);
      log.warn("String_Node_Str" + subscription.getUpstreamEntitlementId() + "String_Node_Str"+ "String_Node_Str"+ subscription.getUpstreamPoolId()+ "String_Node_Str");
    }
  }
  List<Subscription> subscriptionsNeedQuantityMatch=new ArrayList<Subscription>();
  for (  Subscription subscription : subscriptionsStillToImport) {
    Subscription local=null;
    Map<String,Subscription> map=existingSubsByUpstreamPool.get(subscription.getUpstreamPoolId());
    if (map == null) {
      map=new HashMap<String,Subscription>();
    }
    for (    Subscription localSub : map.values()) {
      if (localSub.getQuantity() == subscription.getQuantity()) {
        local=localSub;
        break;
      }
    }
    if (local != null) {
      mergeSubscription(subscription,local,map);
      log.info("String_Node_Str" + subscription.getUpstreamEntitlementId() + "String_Node_Str"+ local.getUpstreamEntitlementId()+ "String_Node_Str");
    }
 else {
      subscriptionsNeedQuantityMatch.add(subscription);
      log.warn("String_Node_Str" + subscription.getUpstreamEntitlementId() + "String_Node_Str"+ "String_Node_Str"+ subscription.getUpstreamPoolId()+ "String_Node_Str");
    }
  }
  Subscription[] inNeed=subscriptionsNeedQuantityMatch.toArray(new Subscription[0]);
  Arrays.sort(inNeed,new QuantityComparator());
  for (  Subscription subscription : inNeed) {
    Subscription local=null;
    Map<String,Subscription> map=existingSubsByUpstreamPool.get(subscription.getUpstreamPoolId());
    if (map == null || map.isEmpty()) {
      createSubscription(subscription);
      log.info("String_Node_Str" + subscription.getUpstreamEntitlementId() + "String_Node_Str");
      continue;
    }
    Subscription[] locals=map.values().toArray(new Subscription[0]);
    Arrays.sort(locals,new QuantityComparator());
    local=locals[0];
    mergeSubscription(subscription,local,map);
    log.info("String_Node_Str" + subscription.getUpstreamEntitlementId() + "String_Node_Str"+ local.getUpstreamEntitlementId()+ "String_Node_Str");
  }
  deleteRemainingLocalSubscriptions(existingSubsByUpstreamPool);
}

Fixed version (ground truth):
/** 
 * @param subsToImport Reconciles incoming entitlements to existing subscriptions. Each set is mapped against the upstream pool id. First match attempt will use entitlement id from incoming entitlements for comparison to existing subscriptions. Next attempt will use the exact quantity for comparison. This is to cover scenarios where the intent is to re-establish the distributor from the host. The final attempt will use ordering of the remaining incoming entitlements and of remaining existing subscriptions in descending order by quantity. Either the remaining subscriptions will be deleted, or the unmatched incoming entitlements will be turned into new subscriptions.
 */
public void store(Owner owner,Set<Subscription> subsToImport){
  Map<String,Map<String,Subscription>> existingSubsByUpstreamPool=mapSubsByUpstreamPool(owner);
  Set<Subscription> subscriptionsStillToImport=new HashSet<Subscription>();
  for (  Subscription subscription : subsToImport) {
    Subscription local=null;
    Map<String,Subscription> map=existingSubsByUpstreamPool.get(subscription.getUpstreamPoolId());
    if (map == null || map.isEmpty()) {
      createSubscription(subscription);
      log.info("String_Node_Str" + subscription.getUpstreamEntitlementId() + "String_Node_Str");
      continue;
    }
    local=map.get(subscription.getUpstreamEntitlementId());
    if (local != null) {
      mergeSubscription(subscription,local,map);
      log.info("String_Node_Str" + subscription.getUpstreamEntitlementId() + "String_Node_Str"+ local.getUpstreamEntitlementId()+ "String_Node_Str");
    }
 else {
      subscriptionsStillToImport.add(subscription);
      log.warn("String_Node_Str" + subscription.getUpstreamEntitlementId() + "String_Node_Str"+ "String_Node_Str"+ subscription.getUpstreamPoolId()+ "String_Node_Str");
    }
  }
  List<Subscription> subscriptionsNeedQuantityMatch=new ArrayList<Subscription>();
  for (  Subscription subscription : subscriptionsStillToImport) {
    Subscription local=null;
    Map<String,Subscription> map=existingSubsByUpstreamPool.get(subscription.getUpstreamPoolId());
    if (map == null) {
      map=new HashMap<String,Subscription>();
    }
    for (    Subscription localSub : map.values()) {
      if (localSub.getQuantity().equals(subscription.getQuantity())) {
        local=localSub;
        break;
      }
    }
    if (local != null) {
      mergeSubscription(subscription,local,map);
      log.info("String_Node_Str" + subscription.getUpstreamEntitlementId() + "String_Node_Str"+ local.getUpstreamEntitlementId()+ "String_Node_Str");
    }
 else {
      subscriptionsNeedQuantityMatch.add(subscription);
      log.warn("String_Node_Str" + subscription.getUpstreamEntitlementId() + "String_Node_Str"+ "String_Node_Str"+ subscription.getUpstreamPoolId()+ "String_Node_Str");
    }
  }
  Subscription[] inNeed=subscriptionsNeedQuantityMatch.toArray(new Subscription[0]);
  Arrays.sort(inNeed,new QuantityComparator());
  for (  Subscription subscription : inNeed) {
    Subscription local=null;
    Map<String,Subscription> map=existingSubsByUpstreamPool.get(subscription.getUpstreamPoolId());
    if (map == null || map.isEmpty()) {
      createSubscription(subscription);
      log.info("String_Node_Str" + subscription.getUpstreamEntitlementId() + "String_Node_Str");
      continue;
    }
    Subscription[] locals=map.values().toArray(new Subscription[0]);
    Arrays.sort(locals,new QuantityComparator());
    local=locals[0];
    mergeSubscription(subscription,local,map);
    log.info("String_Node_Str" + subscription.getUpstreamEntitlementId() + "String_Node_Str"+ local.getUpstreamEntitlementId()+ "String_Node_Str");
  }
  deleteRemainingLocalSubscriptions(existingSubsByUpstreamPool);
}