Class BoundablePairDistanceComparator

 1  
 2 /*
 3  * Copyright (c) 2017 Jia Yu.
 4  *
 5  * All rights reserved. This program and the accompanying materials
 6  * are made available under the terms of the Eclipse Public License 2.0
 7  * and Eclipse Distribution License v. 1.0 which accompanies this distribution.
 8  * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v20.html
 9  * and the Eclipse Distribution License is available at
10  *
11  * http://www.eclipse.org/org/documents/edl-v10.php.
12  */
13 package org.locationtech.jts.index.strtree;
14  
15 import java.io.Serializable;
16 import java.util.Comparator;
17  
18  
19 /**
20  * The Class BoundablePairDistanceComparator. It implements Java comparator and is used 
21  * as a parameter to sort the BoundablePair list.
22  */
23 public class BoundablePairDistanceComparator implements Comparator<BoundablePair>, Serializable{
24     
25     /** The normal order. */
26     boolean normalOrder;
27  
28     /**
29      * Instantiates a new boundable pair distance comparator.
30      *
31      * @param normalOrder true puts the lowest record at the head of this queue.
32      * This is the natural order. PriorityQueue peek() will get the least element. 
33      */
34     public BoundablePairDistanceComparator(boolean normalOrder)
35     {
36         this.normalOrder = normalOrder;
37     }
38     
39     /* (non-Javadoc)
40      * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
41      */
42     public int compare(BoundablePair p1, BoundablePair p2) {
43         double distance1 = p1.getDistance();
44         double distance2 = p2.getDistance();
45         if(this.normalOrder)
46         {
47             if (distance1 > distance2) {
48                 return 1;
49             } else if (distance1 == distance2) {
50                 return 0;
51             }
52             return -1;
53         }
54         else
55         {
56             if (distance1 > distance2) {
57                 return -1;
58             } else if (distance1 == distance2) {
59                 return 0;
60             }
61             return 1;
62         }
63  
64     }
65 }
66