Program: 91791
Project: 91791
Buggy source:
/** 
 * Suggests a coordinate reference system which could be a common target for coordinate operations having the given sources. This method compares the   {@linkplain #getGeographicBoundingBox(CoordinateReferenceSystem) domain of validity} of all given CRSs. If a CRS has a domain of validity that contains the domain of all otherCRS, than that CRS is returned. Otherwise this method verifies if a  {@linkplain GeneralDerivedCRS#getBaseCRS() base CRS} (usually a {@linkplain org.apache.sis.referencing.crs.DefaultGeographicCRS geographic CRS} instance)would be suitable. If no suitable CRS is found, then this method returns  {@code null}. <div class="note"><b>Use case:</b> before to test if two arbitrary envelopes   {@linkplain GeneralEnvelope#intersects(Envelope) intersect} each other,they need to be  {@linkplain Envelopes#transform(Envelope,CoordinateReferenceSystem) transformed} in the same CRS.However if one CRS is a Transverse Mercator projection while the other CRS is a world-wide geographic CRS, then attempts to use the Transverse Mercator projection as the common CRS is likely to fail since the geographic envelope may span an area far outside the projection domain of validity. This  {@code suggestTargetCRS(…)} method can usedfor choosing a common CRS which is less likely to fail.</div>
 * @param regionOfInterest  the geographic area for which the coordinate operations will be applied,or  {@code null} if unknown.
 * @param sourceCRS         the coordinate reference systems for which a common target CRS is desired.
 * @return a CRS that may be used as a common target for all the given source CRS in the given region of interest,or  {@code null} if this method did not find a common target CRS. The returned CRS may be different thanall given CRS.
 * @since 0.8
 */
public static CoordinateReferenceSystem suggestTargetCRS(GeographicBoundingBox regionOfInterest,CoordinateReferenceSystem... sourceCRS){
  boolean worldwide=false;
  DefaultGeographicBoundingBox domain=null;
  final GeographicBoundingBox[] domains=new GeographicBoundingBox[sourceCRS.length];
  for (int i=0; i < sourceCRS.length; i++) {
    final CoordinateReferenceSystem crs=sourceCRS[i];
    GeographicBoundingBox bbox=getGeographicBoundingBox(crs);
    if (bbox == null) {
      if (!(crs instanceof GeodeticCRS)) {
        return null;
      }
      if (regionOfInterest == null) {
        return crs;
      }
      bbox=regionOfInterest;
      worldwide=true;
    }
 else     if (!worldwide) {
      if (domain == null) {
        domain=new DefaultGeographicBoundingBox(bbox);
      }
 else {
        domain.add(bbox);
      }
    }
    domains[i]=bbox;
  }
  if (domain != null && !worldwide) {
    if (regionOfInterest != null) {
      domain.intersect(regionOfInterest);
    }
    regionOfInterest=domain;
    domain=null;
  }
  CoordinateReferenceSystem bestCRS=null;
  final double roiArea=Extents.area(regionOfInterest);
  double maxInsideArea=0;
  double minOutsideArea=Double.POSITIVE_INFINITY;
  boolean tryDerivedCRS=false;
  do {
    for (int i=0; i < domains.length; i++) {
      final GeographicBoundingBox bbox=domains[i];
      double insideArea=Extents.area(bbox);
      double outsideArea=0;
      if (regionOfInterest != null) {
        if (domain == null) {
          domain=new DefaultGeographicBoundingBox(bbox);
        }
 else {
          domain.setBounds(bbox);
        }
        domain.intersect(regionOfInterest);
        final double area=insideArea;
        insideArea=Extents.area(bbox);
        outsideArea=area - insideArea;
      }
      if (insideArea > maxInsideArea || (insideArea == maxInsideArea && outsideArea < minOutsideArea)) {
        maxInsideArea=insideArea;
        minOutsideArea=outsideArea;
        bestCRS=sourceCRS[i];
      }
    }
    if (maxInsideArea < roiArea) {
      if (tryDerivedCRS)       break;
      final SingleCRS[] derivedCRS=new SingleCRS[sourceCRS.length];
      for (int i=0; i < derivedCRS.length; i++) {
        GeographicBoundingBox bbox=null;
        final CoordinateReferenceSystem crs=sourceCRS[i];
        if (crs instanceof GeneralDerivedCRS) {
          final SingleCRS baseCRS=((GeneralDerivedCRS)crs).getBaseCRS();
          bbox=getGeographicBoundingBox(baseCRS);
          if (bbox == null) {
            bbox=regionOfInterest;
          }
          tryDerivedCRS=true;
          derivedCRS[i]=baseCRS;
        }
        domains[i]=bbox;
      }
      sourceCRS=derivedCRS;
    }
 else {
      break;
    }
  }
 while (tryDerivedCRS);
  return bestCRS;
}

Fixed version (ground truth):
/** 
 * Suggests a coordinate reference system which could be a common target for coordinate operations having the given sources. This method compares the   {@linkplain #getGeographicBoundingBox(CoordinateReferenceSystem) domain of validity} of all given CRSs. If a CRS has a domain of validity that contains the domain of all otherCRS, than that CRS is returned. Otherwise this method verifies if a  {@linkplain GeneralDerivedCRS#getBaseCRS() base CRS} (usually a {@linkplain org.apache.sis.referencing.crs.DefaultGeographicCRS geographic CRS} instance)would be suitable. If no suitable CRS is found, then this method returns  {@code null}. <div class="note"><b>Use case:</b> before to test if two arbitrary envelopes   {@linkplain GeneralEnvelope#intersects(Envelope) intersect} each other,they need to be  {@linkplain Envelopes#transform(Envelope,CoordinateReferenceSystem) transformed} in the same CRS.However if one CRS is a Transverse Mercator projection while the other CRS is a world-wide geographic CRS, then attempts to use the Transverse Mercator projection as the common CRS is likely to fail since the geographic envelope may span an area far outside the projection domain of validity. This  {@code suggestTargetCRS(…)} method can usedfor choosing a common CRS which is less likely to fail.</div>
 * @param regionOfInterest  the geographic area for which the coordinate operations will be applied,or  {@code null} if unknown.
 * @param sourceCRS         the coordinate reference systems for which a common target CRS is desired.
 * @return a CRS that may be used as a common target for all the given source CRS in the given region of interest,or  {@code null} if this method did not find a common target CRS. The returned CRS may be different thanall given CRS.
 * @since 0.8
 */
public static CoordinateReferenceSystem suggestTargetCRS(GeographicBoundingBox regionOfInterest,CoordinateReferenceSystem... sourceCRS){
  boolean worldwide=false;
  DefaultGeographicBoundingBox domain=null;
  final GeographicBoundingBox[] domains=new GeographicBoundingBox[sourceCRS.length];
  for (int i=0; i < sourceCRS.length; i++) {
    final CoordinateReferenceSystem crs=sourceCRS[i];
    GeographicBoundingBox bbox=getGeographicBoundingBox(crs);
    if (bbox == null) {
      if (!(crs instanceof GeodeticCRS)) {
        return null;
      }
      if (regionOfInterest == null) {
        return crs;
      }
      bbox=regionOfInterest;
      worldwide=true;
    }
 else     if (!worldwide) {
      if (domain == null) {
        domain=new DefaultGeographicBoundingBox(bbox);
      }
 else {
        domain.add(bbox);
      }
    }
    domains[i]=bbox;
  }
  if (domain != null && !worldwide) {
    if (regionOfInterest != null) {
      domain.intersect(regionOfInterest);
    }
    regionOfInterest=domain;
    domain=null;
  }
  CoordinateReferenceSystem bestCRS=null;
  final double roiArea=Extents.area(regionOfInterest);
  double maxInsideArea=0;
  double minOutsideArea=Double.POSITIVE_INFINITY;
  boolean tryDerivedCRS=false;
  do {
    for (int i=0; i < domains.length; i++) {
      final GeographicBoundingBox bbox=domains[i];
      double insideArea=Extents.area(bbox);
      double outsideArea=0;
      if (regionOfInterest != null) {
        if (domain == null) {
          domain=new DefaultGeographicBoundingBox(bbox);
        }
 else {
          domain.setBounds(bbox);
        }
        domain.intersect(regionOfInterest);
        final double area=insideArea;
        insideArea=Extents.area(domain);
        outsideArea=area - insideArea;
      }
      if (insideArea > maxInsideArea || (insideArea == maxInsideArea && outsideArea < minOutsideArea)) {
        maxInsideArea=insideArea;
        minOutsideArea=outsideArea;
        bestCRS=sourceCRS[i];
      }
    }
    if (maxInsideArea < roiArea) {
      if (tryDerivedCRS)       break;
      final SingleCRS[] derivedCRS=new SingleCRS[sourceCRS.length];
      for (int i=0; i < derivedCRS.length; i++) {
        GeographicBoundingBox bbox=null;
        final CoordinateReferenceSystem crs=sourceCRS[i];
        if (crs instanceof GeneralDerivedCRS) {
          final SingleCRS baseCRS=((GeneralDerivedCRS)crs).getBaseCRS();
          bbox=getGeographicBoundingBox(baseCRS);
          if (bbox == null) {
            bbox=regionOfInterest;
          }
          tryDerivedCRS=true;
          derivedCRS[i]=baseCRS;
        }
        domains[i]=bbox;
      }
      sourceCRS=derivedCRS;
    }
 else {
      break;
    }
  }
 while (tryDerivedCRS);
  return bestCRS;
}