/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.pdfbox.text;

import java.util.Comparator;

/**
 * This class is a comparator for TextPosition operators.  It handles
 * pages with text in different directions by grouping the text based
 * on direction and sorting in that direction. This allows continuous text
 * in a given direction to be more easily grouped together.  
 *
 * @author Ben Litchfield
 */
public class TextPositionComparator implements Comparator<TextPosition>
{
    @Override
    public int compare(TextPosition pos1, TextPosition pos2)
    {
        // only compare text that is in the same direction
        if (pos1.getDir() < pos2.getDir())
        {
            return -1;
        }
        else if (pos1.getDir() > pos2.getDir())
        {
            return 1;
        }
        
        // get the text direction adjusted coordinates
        float x1 = pos1.getXDirAdj();
        float x2 = pos2.getXDirAdj();
        
        float pos1YBottom = pos1.getYDirAdj();
        float pos2YBottom = pos2.getYDirAdj();

        // note that the coordinates have been adjusted so 0,0 is in upper left
        float pos1YTop = pos1YBottom - pos1.getHeightDir();
        float pos2YTop = pos2YBottom - pos2.getHeightDir();

        float yDifference = Math.abs(pos1YBottom - pos2YBottom);

        // we will do a simple tolerance comparison
        if (yDifference < .1 ||
            pos2YBottom >= pos1YTop && pos2YBottom <= pos1YBottom ||
            pos1YBottom >= pos2YTop && pos1YBottom <= pos2YBottom)
        {
            if (x1 < x2)
            {
                return -1;
            }
            else if (x1 > x2)
            {
                return 1;
            }
            else
            {
                return 0;
            }
        }
        else if (pos1YBottom < pos2YBottom)
        {
            return - 1;
        }
        else
        {
            return 1;
        }
    }
}

@@ -1396,18 +1396,39 @@
        i
-f 
+nt cmp1 = Float.compare
 (pos1.ge
@@ -1433,18 +1433,17 @@
 getDir()
- %3C
+,
  pos2.ge
@@ -1453,98 +1453,31 @@
 r())
-%0A        %7B%0A            return -1;%0A        %7D%0A        else if (pos1.getDir() %3E pos2.getDir()
+;%0A        if (cmp1 != 0
 )%0A  
@@ -1495,32 +1495,35 @@
          return 
+cmp
 1;%0A        %7D%0A   
@@ -2276,230 +2276,37 @@
     
-if (x1 %3C x2)%0A            %7B%0A                return -1;%0A            %7D%0A            else if (x1 %3E
+return Float.compare(x1,
  x2)
-%0A            %7B%0A                return 1;%0A            %7D%0A            else%0A            %7B%0A                return 0;%0A            %7D
+;
 %0A   
@@ -2358,32 +2358,32 @@
 ttom)%0A        %7B%0A
+
             retu
@@ -2386,17 +2386,16 @@
 return -
- 
 1;%0A     

use of the relational operators < and > in compareTo methods is verbose and error-prone and no longer recommended (Effective Java 2018, p69)