Program: 152095
Project: 152095
Buggy source:
/** 
 * Guess gateway according to phone number or phone number prefix. Searches through all visible (non-hidden) gateways and finds the best suited ones (one or many) supporting this phone number. <br/><br/> Algorithm: <ol> <li>Any fake gateway is disqualified.</li> <li>Any gateway that has some supported prefixes listed and yet is not matching the number is disqualified.</li> <li>If some gateways are marked as favorite: <ol type=a> <li>Discard gateways that have the number outside their preferred prefixes.</li> <li>If there are some remaining, return all of them, sorted by the number of contacts assigned. Selection ends here.</li> </ol> </li> <li>If user has some contacts defined, count their numbers for each gateway: <ol type=a> <li>Discard gateways that have the number outside their preferred prefixes.</li> <li>Gateway with the highest number of contacts win.</li> <li>If multiple gateways have the same (highest) score, return all of them.</li> <li>Selection ends here.</li> </ol> </li> <li>Do the last-resort algorithm: <ol type=a> <li>All gateways have 0 points by default.</li> <li>If a gateway requires login, but no credentials are filled in, subtract 1 point.</li> <li>If a gateway has preferred prefixes defined and they don't match the number, subtract 1 point.</li> <li>Return all gateways with the highest score. Selection ends here.</li> </ol> </li> </ol>
 * @param number phone number or its prefix. The minimum length is two characters,for shorter input (or null) the method does nothing.
 * @return tuple consisting of: 1. list of suggested gateways (may be empty); 2. boolean whether this suggestionis recommended (the decision was based on favorite gateways or the number of gateways users) or completely arbitrary (the last-resort algorithm was used).
 */
public Tuple<ArrayList<Gateway>,Boolean> suggestGateway(String number){
  if (number == null || number.length() < 2) {
    return new Tuple<ArrayList<Gateway>,Boolean>(new ArrayList<Gateway>(),false);
  }
  SortedSet<Gateway> selectedGateways=getVisible();
  ArrayList<Gateway> result=new ArrayList<Gateway>();
  final HashMap<String,Integer> usage=computeGatewayUsage();
  for (Iterator<Gateway> it=selectedGateways.iterator(); it.hasNext(); ) {
    Gateway gw=it.next();
    if (isFakeGateway(gw.getName())) {
      it.remove();
    }
 else     if (!isNumberSupported(gw,number)) {
      it.remove();
    }
 else {
    }
  }
  for (  Gateway gw : selectedGateways) {
    if (!gw.isFavorite()) {
      continue;
    }
    if (!isNumberPreferred(gw,number)) {
      continue;
    }
    result.add(gw);
  }
  if (!result.isEmpty()) {
    Collections.sort(result,new Comparator<Gateway>(){
      @Override public int compare(      Gateway o1,      Gateway o2){
        Integer popularity1=usage.get(o1.getName());
        Integer popularity2=usage.get(o2.getName());
        return popularity1.compareTo(popularity2);
      }
    }
);
    return new Tuple<ArrayList<Gateway>,Boolean>(result,true);
  }
  int max=1;
  for (  Gateway gw : selectedGateways) {
    if (!isNumberPreferred(gw,number)) {
      continue;
    }
    int popularity=usage.get(gw.getName());
    if (popularity > max) {
      max=popularity;
      result.clear();
      result.add(gw);
    }
 else     if (popularity == max) {
      result.add(gw);
    }
  }
  if (!result.isEmpty()) {
    return new Tuple<ArrayList<Gateway>,Boolean>(result,true);
  }
  HashMap<Gateway,Integer> scores=new HashMap<Gateway,Integer>();
  for (  Gateway gw : selectedGateways) {
    scores.put(gw,0);
    if (gw.hasFeature(Feature.LOGIN_ONLY) && keyring.getKey(gw.getName()) == null) {
      scores.put(gw,scores.get(gw) - 1);
    }
    if (!isNumberPreferred(gw,number)) {
      scores.put(gw,scores.get(gw) - 1);
    }
  }
  max=Integer.MIN_VALUE;
  for (  Gateway gw : selectedGateways) {
    int score=scores.get(gw);
    if (score > max) {
      max=score;
      result.clear();
      result.add(gw);
    }
 else     if (score == max) {
      result.add(gw);
    }
  }
  return new Tuple<ArrayList<Gateway>,Boolean>(result,false);
}

Fixed version (ground truth):
/** 
 * Guess gateway according to phone number or phone number prefix. Searches through all visible (non-hidden) gateways and finds the best suited ones (one or many) supporting this phone number. <br><br> Algorithm: <ol> <li>Any fake gateway is disqualified.</li> <li>Any gateway that has some supported prefixes listed and yet is not matching the number is disqualified.</li> <li>If some gateways are marked as favorite: <ol type=a> <li>Discard gateways that have the number outside their preferred prefixes.</li> <li>If there are some remaining, return all of them, sorted by the number of contacts assigned. Selection ends here.</li> </ol> </li> <li>If user has some contacts defined, count their numbers for each gateway: <ol type=a> <li>Discard gateways that have the number outside their preferred prefixes.</li> <li>Gateway with the highest number of contacts win.</li> <li>If multiple gateways have the same (highest) score, return all of them.</li> <li>Selection ends here.</li> </ol> </li> <li>Do the last-resort algorithm: <ol type=a> <li>All gateways have 0 points by default.</li> <li>If a gateway requires login, but no credentials are filled in, subtract 1 point.</li> <li>If a gateway has preferred prefixes defined and they don't match the number, subtract 1 point.</li> <li>Return all gateways with the highest score. Selection ends here.</li> </ol> </li> </ol>
 * @param number phone number or its prefix. The minimum length is two characters,for shorter input (or null) the method does nothing.
 * @return tuple consisting of: 1. list of suggested gateways (may be empty); 2. boolean whether this suggestionis recommended (the decision was based on favorite gateways or the number of gateways users) or completely arbitrary (the last-resort algorithm was used).
 */
public Tuple<ArrayList<Gateway>,Boolean> suggestGateway(String number){
  if (number == null || number.length() < 2) {
    return new Tuple<ArrayList<Gateway>,Boolean>(new ArrayList<Gateway>(),false);
  }
  SortedSet<Gateway> selectedGateways=getVisible();
  ArrayList<Gateway> result=new ArrayList<Gateway>();
  final HashMap<String,Integer> usage=computeGatewayUsage();
  for (Iterator<Gateway> it=selectedGateways.iterator(); it.hasNext(); ) {
    Gateway gw=it.next();
    if (isFakeGateway(gw.getName())) {
      it.remove();
    }
 else     if (!isNumberSupported(gw,number)) {
      it.remove();
    }
 else {
    }
  }
  for (  Gateway gw : selectedGateways) {
    if (!gw.isFavorite()) {
      continue;
    }
    if (!isNumberPreferred(gw,number)) {
      continue;
    }
    result.add(gw);
  }
  if (!result.isEmpty()) {
    Collections.sort(result,new Comparator<Gateway>(){
      @Override public int compare(      Gateway o1,      Gateway o2){
        Integer popularity1=usage.get(o1.getName());
        Integer popularity2=usage.get(o2.getName());
        return popularity1.compareTo(popularity2);
      }
    }
);
    return new Tuple<ArrayList<Gateway>,Boolean>(result,true);
  }
  int max=1;
  for (  Gateway gw : selectedGateways) {
    if (!isNumberPreferred(gw,number)) {
      continue;
    }
    int popularity=usage.get(gw.getName());
    if (popularity > max) {
      max=popularity;
      result.clear();
      result.add(gw);
    }
 else     if (popularity == max) {
      result.add(gw);
    }
  }
  if (!result.isEmpty()) {
    return new Tuple<ArrayList<Gateway>,Boolean>(result,true);
  }
  HashMap<Gateway,Integer> scores=new HashMap<Gateway,Integer>();
  for (  Gateway gw : selectedGateways) {
    scores.put(gw,0);
    if (gw.hasFeature(Feature.LOGIN_ONLY) && keyring.getKey(gw.getName()) == null) {
      scores.put(gw,scores.get(gw) - 1);
    }
    if (!isNumberPreferred(gw,number)) {
      scores.put(gw,scores.get(gw) - 1);
    }
  }
  max=Integer.MIN_VALUE;
  for (  Gateway gw : selectedGateways) {
    int score=scores.get(gw);
    if (score > max) {
      max=score;
      result.clear();
      result.add(gw);
    }
 else     if (score == max) {
      result.add(gw);
    }
  }
  return new Tuple<ArrayList<Gateway>,Boolean>(result,false);
}