Fixed git clone URL
[sixth-3d.git] / src / main / java / eu / svjatoslav / sixth / e3d / gui / TextPointer.java
index c01b8e2..91cd6e6 100755 (executable)
@@ -1,17 +1,27 @@
 /*
- * Sixth 3D engine. Copyright ©2012-2017, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of version 3 of the GNU Lesser General Public License
- * or later as published by the Free Software Foundation.
- *
+ * Sixth 3D engine. Author: Svjatoslav Agejenko.
+ * This project is released under Creative Commons Zero (CC0) license.
  */
-
 package eu.svjatoslav.sixth.e3d.gui;
 
+import static java.lang.Integer.compare;
+
+/**
+ * A pointer to a character in a text using row and column.
+ * <p>
+ * It can be used to represent a cursor position in a text.
+ * Also, it can be used to represent beginning and end of a selection.
+ */
 public class TextPointer implements Comparable<TextPointer> {
 
+    /**
+     * The row of the character. Starts from 0.
+     */
     public int row;
+
+    /**
+     * The column of the character. Starts from 0.
+     */
     public int column;
 
     public TextPointer() {
@@ -41,22 +51,37 @@ public class TextPointer implements Comparable<TextPointer> {
         return result;
     }
 
+    /**
+     * Compares this pointer to another pointer.
+     *
+     * @param textPointer The pointer to compare to.
+     * @return <ul>
+     *     <li>-1 if this pointer is smaller than the argument pointer.</li>
+     *     <li>0 if they are equal.</li>
+     *     <li>1 if this pointer is bigger than the argument pointer.</li>
+     *     </ul>
+     */
     @Override
     public int compareTo(final TextPointer textPointer) {
 
-        if (textPointer.row > row)
+        if (row < textPointer.row)
             return -1;
-        if (textPointer.row < row)
+        if (row > textPointer.row)
             return 1;
 
-        if (textPointer.column > column)
-            return -1;
-        if (textPointer.column < column)
-            return 1;
-
-        return 0;
+        return compare(column, textPointer.column);
     }
 
+    /**
+     * Checks if this pointer is between the argument pointers.
+     * <p>
+     * This pointer is considered to be between the pointers if it is bigger or equal to the start pointer
+     * and smaller than the end pointer.
+     *
+     * @param start The start pointer.
+     * @param end   The end pointer.
+     * @return True if this pointer is between the specified pointers.
+     */
     public boolean isBetween(final TextPointer start, final TextPointer end) {
 
         if (start == null)
@@ -65,6 +90,7 @@ public class TextPointer implements Comparable<TextPointer> {
         if (end == null)
             return false;
 
+        // Make sure that start is smaller than end.
         TextPointer smaller;
         TextPointer bigger;
 
@@ -76,8 +102,8 @@ public class TextPointer implements Comparable<TextPointer> {
             bigger = start;
         }
 
+        // Check if this pointer is between the specified pointers.
         return (compareTo(smaller) >= 0) && (bigger.compareTo(this) > 0);
-
     }
 
 }