public class Point2DToleranceComparator extends Object implements Point2DComparator
Description: Implementing comparator of DataPoint2d. The comparator uses a tolerance to specify that when two values are within tolerance of each other, they are equal
This class sounds more complicated that it really is. The whole purpose is for calling the function compare(Object o1, Object o2). The x-coordinates are obtained from each, then this algorythmn determines if the x-values are equal:
Math.abs( x1 - x2 ) <= tolerance)
A tolerance=0 is actually abaout 1e-16 due to the numerical precision of floating point arithmetic (1.0 + 1e-16 = 1.0 )
Note: In general comparators are created so that you can have more than one sorting for a class. Imagine that you have a Javabean with 4 fields, id, first name, last name, date created. Typical for a user record in a database. Now you can build the compareTo() function inside this Javabean, nut then you can only sort on 1 column. What if you present these javabeans in a GUI List, and you want to sort on any field by clicking on the header. You simply make 4 comparators, one for each field. Each header would use the particular comparator for the sorting function. Very nice design pattern.
Now let's say you add another field. You simply make a new Comparator ( almost copy and paste). You don't have to change youre Javabean or your sorting function. Just pass in this new comparator.
| Modifier and Type | Field and Description |
|---|---|
protected double |
tolerance
The tolerance for determining whether two x values are different.
|
| Constructor and Description |
|---|
Point2DToleranceComparator()
No-Argument constructor.
|
Point2DToleranceComparator(double tolerance)
Constructor that sets the tolerance when created.
|
| Modifier and Type | Method and Description |
|---|---|
int |
compare(Point2D o1,
Point2D o2)
Returns 0 if the two Objects are equal, -1 if the first object is less than
the second, or +1 if it's greater.
|
double |
getTolerance()
Tolerance indicates the distance two values can be apart, but still
considered equal.
|
void |
setTolerance(double newTolerance)
Tolerance indicates the distance two values can be apart, but still
considered equal.
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitcomparing, comparing, comparingDouble, comparingInt, comparingLong, equals, naturalOrder, nullsFirst, nullsLast, reversed, reverseOrder, thenComparing, thenComparing, thenComparing, thenComparingDouble, thenComparingInt, thenComparingLongprotected double tolerance
public Point2DToleranceComparator()
public Point2DToleranceComparator(double tolerance)
tolerance - The distance two values can be apart and still considered
equalpublic void setTolerance(double newTolerance)
setTolerance in interface Point2DComparatornewTolerance - The new tolerance valueInvalidRangeException - Thrown if tolerance is negativepublic double getTolerance()
getTolerance in interface Point2DComparatorpublic int compare(Point2D o1, Point2D o2)
One use for this class is to sort a DiscretizedFunction by it's X-Values (independent variable) ascending, to prepare the function for plotting.
compare in interface Comparator<Point2D>o1 - First DataPoint2Do2 - Second DataPoint2D