Program: 91991
Project: 91991
Buggy source:
/** 
 * Given a transformation chain, replaces the elements around   {@code transforms.get(index)} transform byalternative objects to use when formatting WKT. The replacement is performed in-place in the given list. <p>This method shall replace only the previous element and the few next elements that need to be changed as a result of the previous change. This method is not expected to continue the iteration after the changes that are of direct concern to this object.</p> <p>This method is invoked (indirectly) only by  {@link ConcatenatedTransform#getPseudoSteps()} in orderto get the  {@link ParameterValueGroup} of a map projection, or to format a {@code ProjectedCRS} WKT.</p>
 * @param transforms The full chain of concatenated transforms.
 * @param index      The index of this transform in the {@code transforms} chain.
 * @param inverse    Always {@code false}, except if we are formatting the inverse transform.
 * @return Index of this transform in the {@code transforms} chain after processing.
 * @see ConcatenatedTransform#getPseudoSteps()
 * @see AbstractMathTransform#beforeFormat(List,int,boolean)
 */
final int beforeFormat(final List<Object> transforms,int index,final boolean inverse){
  Matrix before=null;
  Matrix after=null;
  if (index != 0) {
    final Object candidate=transforms.get(index - 1);
    if (candidate instanceof MathTransform) {
      before=MathTransforms.getMatrix((MathTransform)candidate);
    }
  }
  if (index + 1 < transforms.size()) {
    final Object candidate=transforms.get(index + 1);
    if (candidate instanceof MathTransform) {
      after=MathTransforms.getMatrix((MathTransform)candidate);
    }
  }
  final boolean hasBefore=(before != null);
  final boolean hasAfter=(after != null);
  MatrixSIS userDefined;
  try {
    userDefined=getMatrix(inverse ? MatrixRole.DENORMALIZATION : MatrixRole.INVERSE_NORMALIZATION);
  }
 catch (  IllegalStateException e) {
    unexpectedException(e);
    return index;
  }
  if (hasBefore) {
    userDefined=userDefined.multiply(before);
  }
  before=Matrices.isIdentity(userDefined,Formulas.ANGULAR_TOLERANCE) ? null : userDefined;
  if (!inverse)   try {
    userDefined=getMatrix(inverse ? MatrixRole.NORMALIZATION : MatrixRole.INVERSE_DENORMALIZATION);
  }
 catch (  IllegalStateException e) {
    unexpectedException(e);
    return index;
  }
  if (hasAfter) {
    userDefined=Matrices.multiply(after,userDefined);
  }
  after=Matrices.isIdentity(userDefined,Formulas.ANGULAR_TOLERANCE) ? null : userDefined;
  if (before == null) {
    if (hasBefore) {
      final Object old=transforms.remove(--index);
      assert(old instanceof LinearTransform);
    }
  }
 else {
    if (hasBefore) {
      final Object old=transforms.set(index - 1,before);
      assert(old instanceof LinearTransform);
    }
 else {
      transforms.add(index++,before);
    }
  }
  transforms.set(index,new WKT(inverse));
  if (after == null) {
    if (hasAfter) {
      final Object old=transforms.remove(index + 1);
      assert(old instanceof LinearTransform);
    }
  }
 else {
    if (hasAfter) {
      final Object old=transforms.set(index + 1,after);
      assert(old instanceof LinearTransform);
    }
 else {
      transforms.add(index + 1,after);
    }
  }
  return index;
}

Fixed version (ground truth):
/** 
 * Given a transformation chain, replaces the elements around   {@code transforms.get(index)} transform byalternative objects to use when formatting WKT. The replacement is performed in-place in the given list. <p>This method shall replace only the previous element and the few next elements that need to be changed as a result of the previous change. This method is not expected to continue the iteration after the changes that are of direct concern to this object.</p> <p>This method is invoked (indirectly) only by  {@link ConcatenatedTransform#getPseudoSteps()} in orderto get the  {@link ParameterValueGroup} of a map projection, or to format a {@code ProjectedCRS} WKT.</p>
 * @param transforms The full chain of concatenated transforms.
 * @param index      The index of this transform in the {@code transforms} chain.
 * @param inverse    Always {@code false}, except if we are formatting the inverse transform.
 * @return Index of this transform in the {@code transforms} chain after processing.
 * @see ConcatenatedTransform#getPseudoSteps()
 * @see AbstractMathTransform#beforeFormat(List,int,boolean)
 */
final int beforeFormat(final List<Object> transforms,int index,final boolean inverse){
  Matrix before=null;
  Matrix after=null;
  if (index != 0) {
    final Object candidate=transforms.get(index - 1);
    if (candidate instanceof MathTransform) {
      before=MathTransforms.getMatrix((MathTransform)candidate);
    }
  }
  if (index + 1 < transforms.size()) {
    final Object candidate=transforms.get(index + 1);
    if (candidate instanceof MathTransform) {
      after=MathTransforms.getMatrix((MathTransform)candidate);
    }
  }
  final boolean hasBefore=(before != null);
  final boolean hasAfter=(after != null);
  MatrixSIS userDefined;
  try {
    userDefined=getMatrix(inverse ? MatrixRole.DENORMALIZATION : MatrixRole.INVERSE_NORMALIZATION);
  }
 catch (  IllegalStateException e) {
    unexpectedException(e);
    return index;
  }
  if (hasBefore) {
    userDefined=userDefined.multiply(before);
  }
  before=Matrices.isIdentity(userDefined,Formulas.ANGULAR_TOLERANCE) ? null : userDefined;
  try {
    userDefined=getMatrix(inverse ? MatrixRole.NORMALIZATION : MatrixRole.INVERSE_DENORMALIZATION);
  }
 catch (  IllegalStateException e) {
    unexpectedException(e);
    return index;
  }
  if (hasAfter) {
    userDefined=Matrices.multiply(after,userDefined);
  }
  after=Matrices.isIdentity(userDefined,Formulas.ANGULAR_TOLERANCE) ? null : userDefined;
  if (before == null) {
    if (hasBefore) {
      final Object old=transforms.remove(--index);
      assert(old instanceof LinearTransform);
    }
  }
 else {
    if (hasBefore) {
      final Object old=transforms.set(index - 1,before);
      assert(old instanceof LinearTransform);
    }
 else {
      transforms.add(index++,before);
    }
  }
  transforms.set(index,new WKT(inverse));
  if (after == null) {
    if (hasAfter) {
      final Object old=transforms.remove(index + 1);
      assert(old instanceof LinearTransform);
    }
  }
 else {
    if (hasAfter) {
      final Object old=transforms.set(index + 1,after);
      assert(old instanceof LinearTransform);
    }
 else {
      transforms.add(index + 1,after);
    }
  }
  return index;
}