From ffce141c4bf7d80bd8c24b66fd46f97b52d906be Mon Sep 17 00:00:00 2001
From: Piotr Gawron <piotr.gawron@uni.lu>
Date: Wed, 16 Jan 2019 16:26:11 +0100
Subject: [PATCH] line comparator implemented

---
 .../common/comparator/LineComparator.java     | 56 +++++++++++++++++++
 .../common/comparator/AllComparatorTests.java |  1 +
 .../common/comparator/LineComparatorTest.java | 53 ++++++++++++++++++
 3 files changed, 110 insertions(+)
 create mode 100644 commons/src/main/java/lcsb/mapviewer/common/comparator/LineComparator.java
 create mode 100644 commons/src/test/java/lcsb/mapviewer/common/comparator/LineComparatorTest.java

diff --git a/commons/src/main/java/lcsb/mapviewer/common/comparator/LineComparator.java b/commons/src/main/java/lcsb/mapviewer/common/comparator/LineComparator.java
new file mode 100644
index 0000000000..f1942bd191
--- /dev/null
+++ b/commons/src/main/java/lcsb/mapviewer/common/comparator/LineComparator.java
@@ -0,0 +1,56 @@
+package lcsb.mapviewer.common.comparator;
+
+import java.awt.geom.Line2D;
+import java.util.Comparator;
+
+import lcsb.mapviewer.common.Configuration;
+
+/**
+ * Comparator used for {@link Line2D} class.
+ * 
+ * @author Piotr Gawron
+ * 
+ */
+public class LineComparator implements Comparator<Line2D> {
+
+  private PointComparator pointComparator;
+
+  /**
+   * Constructor that requires {@link #epsilon} parameter.
+   * 
+   * @param epsilon
+   *          {@link #epsilon}
+   */
+  public LineComparator(double epsilon) {
+    this.pointComparator = new PointComparator(epsilon);
+  }
+
+  /**
+   * Default constructor.
+   */
+  public LineComparator() {
+    this(Configuration.EPSILON);
+  }
+
+  @Override
+  public int compare(Line2D arg0, Line2D arg1) {
+    if (arg0 == null) {
+      if (arg1 == null) {
+        return 0;
+      } else {
+        return 1;
+      }
+
+    } else if (arg1 == null) {
+      return -1;
+    }
+    if (pointComparator.compare(arg0.getP1(), arg1.getP1()) != 0) {
+      return pointComparator.compare(arg0.getP1(), arg1.getP1());
+    } else if (pointComparator.compare(arg0.getP2(), arg1.getP2()) != 0) {
+      return pointComparator.compare(arg0.getP2(), arg1.getP2());
+    } else {
+      return 0;
+    }
+  }
+
+}
diff --git a/commons/src/test/java/lcsb/mapviewer/common/comparator/AllComparatorTests.java b/commons/src/test/java/lcsb/mapviewer/common/comparator/AllComparatorTests.java
index 7dbfdf2a60..deb50f94a6 100644
--- a/commons/src/test/java/lcsb/mapviewer/common/comparator/AllComparatorTests.java
+++ b/commons/src/test/java/lcsb/mapviewer/common/comparator/AllComparatorTests.java
@@ -10,6 +10,7 @@ import org.junit.runners.Suite.SuiteClasses;
     DoubleComparatorTest.class,
     EnumComparatorTest.class,
     IntegerComparatorTest.class,
+    LineComparatorTest.class,
     ListComparatorTest.class,
     PointComparatorTest.class,
     StringComparatorTest.class,
diff --git a/commons/src/test/java/lcsb/mapviewer/common/comparator/LineComparatorTest.java b/commons/src/test/java/lcsb/mapviewer/common/comparator/LineComparatorTest.java
new file mode 100644
index 0000000000..d776645605
--- /dev/null
+++ b/commons/src/test/java/lcsb/mapviewer/common/comparator/LineComparatorTest.java
@@ -0,0 +1,53 @@
+package lcsb.mapviewer.common.comparator;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+import java.awt.geom.Line2D;
+import java.awt.geom.Point2D;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+public class LineComparatorTest {
+  
+  Point2D p1 = new Point2D.Double(106, 5.01);
+  Point2D p1close = new Point2D.Double(106, 5.11);
+  Point2D p2 = new Point2D.Double(106, 2.73);
+  Point2D p2far = new Point2D.Double(106, 202.73);
+  
+
+  @Before
+  public void setUp() throws Exception {
+  }
+
+  @After
+  public void tearDown() throws Exception {
+  }
+
+  @Test
+  public void testNotNullComparison() {
+    LineComparator comp = new LineComparator();
+    assertTrue(comp.compare(new Line2D.Double(p1, p2), new Line2D.Double(p1, p2)) == 0);
+    assertTrue(comp.compare(new Line2D.Double(p2, p1), new Line2D.Double(p2, p1)) == 0);
+    assertFalse(comp.compare(new Line2D.Double(p1, p2), new Line2D.Double(p2, p1)) == 0);
+    assertFalse(comp.compare(new Line2D.Double(p2, p1), new Line2D.Double(p1, p2)) == 0);
+  }
+
+  @Test
+  public void testEpsilonComp() {
+    LineComparator comp = new LineComparator(10);
+    assertFalse(comp.compare(new Line2D.Double(p1, p2), new Line2D.Double(p1, p2far)) == 0);
+    assertTrue(comp.compare(new Line2D.Double(p1, p2), new Line2D.Double(p1close, p2)) == 0);
+  }
+
+  @Test
+  public void testNullComparison() {
+    LineComparator comp = new LineComparator();
+    assertTrue(comp.compare(null, null) == 0);
+    assertFalse(comp.compare(new Line2D.Double(p1, p2), null) == 0);
+    assertFalse(comp.compare(null, new Line2D.Double(p1, p2)) == 0);
+  }
+
+}
-- 
GitLab