Program: 110756
Project: 110756
Buggy source:
/** 
 * Compute how well this artifact matches the   {@link #desiredDensity}. <p>Various different codebases have different and sometimes contradictory methods for which resources are better in different situations. All of them agree that an exact match is best, but: <p>The android common code (see   {@link FolderConfiguration#getDensityQualifier()} treatslarger densities as better than non-matching smaller densities. <p>aapt code to filter assets by density prefers the smallest density that is larger than or the same as the desired density, or, lacking that, the largest available density. <p>Other implementations of density filtering include Gradle (to filter which resources actually get built into apps) and Android code itself (for the device to decide which resource to use). <p>This particular implementation is based on  {@link com.google.devtools.build.android.DensitySpecificResourceFilter}, which filters resources by density during execution. It prefers to use exact matches when possible, then tries to find resources with exactly double the desired density for particularly easy downsizing, and otherwise prefers resources that are closest to the desired density, relative to the smaller of the available and desired densities. <p>Once we always filter resources during analysis, we should be able to completely remove that code.
 * @return a score for how well the artifact matches. Lower scores indicate better matches.
 */
private double computeAffinity(Artifact artifact){
  DensityQualifier resourceQualifier=getConfigForArtifact(ruleContext,artifact).getDensityQualifier();
  if (resourceQualifier == null) {
    return Double.MAX_VALUE;
  }
  int resourceDensity=resourceQualifier.getValue().getDpiValue();
  int density=desiredDensity.getDpiValue();
  if (resourceDensity == density) {
    return -2;
  }
  if (resourceDensity == 2 * density) {
    return -1;
  }
  double densityRatio=Math.max(density,resourceDensity) / (double)Math.min(density,resourceDensity);
  if (density < resourceDensity) {
    return densityRatio;
  }
  return densityRatio + 0.01;
}

Fixed version (ground truth):
/** 
 * Compute how well this artifact matches the   {@link #desiredDensity}. <p>Various different codebases have different and sometimes contradictory methods for which resources are better in different situations. All of them agree that an exact match is best, but: <p>The android common code (see   {@link FolderConfiguration#getDensityQualifier()} treatslarger densities as better than non-matching smaller densities. <p>aapt code to filter assets by density prefers the smallest density that is larger than or the same as the desired density, or, lacking that, the largest available density. <p>Other implementations of density filtering include Gradle (to filter which resources actually get built into apps) and Android code itself (for the device to decide which resource to use). <p>This particular implementation is based on  {@link com.google.devtools.build.android.DensitySpecificResourceFilter}, which filters resources by density during execution. It prefers to use exact matches when possible, then tries to find resources with exactly double the desired density for particularly easy downsizing, and otherwise prefers resources that are closest to the desired density, relative to the smaller of the available and desired densities. <p>Once we always filter resources during analysis, we should be able to completely remove that code.
 * @return a score for how well the artifact matches. Lower scores indicate better matches.
 */
private double computeAffinity(Artifact artifact){
  DensityQualifier resourceQualifier=getConfigForArtifact(ruleErrorConsumer,artifact).getDensityQualifier();
  if (resourceQualifier == null) {
    return Double.MAX_VALUE;
  }
  int resourceDensity=resourceQualifier.getValue().getDpiValue();
  int density=desiredDensity.getDpiValue();
  if (resourceDensity == density) {
    return -2;
  }
  if (resourceDensity == 2 * density) {
    return -1;
  }
  double densityRatio=Math.max(density,resourceDensity) / (double)Math.min(density,resourceDensity);
  if (density < resourceDensity) {
    return densityRatio;
  }
  return densityRatio + 0.01;
}