Fixed git clone URL
[sixth-3d.git] / src / main / java / eu / svjatoslav / sixth / e3d / gui / TextPointer.java
1 /*
2  * Sixth 3D engine. Author: Svjatoslav Agejenko.
3  * This project is released under Creative Commons Zero (CC0) license.
4  */
5 package eu.svjatoslav.sixth.e3d.gui;
6
7 import static java.lang.Integer.compare;
8
9 /**
10  * A pointer to a character in a text using row and column.
11  * <p>
12  * It can be used to represent a cursor position in a text.
13  * Also, it can be used to represent beginning and end of a selection.
14  */
15 public class TextPointer implements Comparable<TextPointer> {
16
17     /**
18      * The row of the character. Starts from 0.
19      */
20     public int row;
21
22     /**
23      * The column of the character. Starts from 0.
24      */
25     public int column;
26
27     public TextPointer() {
28         this(0, 0);
29     }
30
31     public TextPointer(final int row, final int column) {
32         this.row = row;
33         this.column = column;
34     }
35
36     public TextPointer(final TextPointer parent) {
37         this(parent.row, parent.column);
38     }
39
40     @Override
41     public boolean equals(final Object o) {
42         if (o == null) return false;
43
44         return o instanceof TextPointer && compareTo((TextPointer) o) == 0;
45     }
46
47     @Override
48     public int hashCode() {
49         int result = row;
50         result = 31 * result + column;
51         return result;
52     }
53
54     /**
55      * Compares this pointer to another pointer.
56      *
57      * @param textPointer The pointer to compare to.
58      * @return <ul>
59      *     <li>-1 if this pointer is smaller than the argument pointer.</li>
60      *     <li>0 if they are equal.</li>
61      *     <li>1 if this pointer is bigger than the argument pointer.</li>
62      *     </ul>
63      */
64     @Override
65     public int compareTo(final TextPointer textPointer) {
66
67         if (row < textPointer.row)
68             return -1;
69         if (row > textPointer.row)
70             return 1;
71
72         return compare(column, textPointer.column);
73     }
74
75     /**
76      * Checks if this pointer is between the argument pointers.
77      * <p>
78      * This pointer is considered to be between the pointers if it is bigger or equal to the start pointer
79      * and smaller than the end pointer.
80      *
81      * @param start The start pointer.
82      * @param end   The end pointer.
83      * @return True if this pointer is between the specified pointers.
84      */
85     public boolean isBetween(final TextPointer start, final TextPointer end) {
86
87         if (start == null)
88             return false;
89
90         if (end == null)
91             return false;
92
93         // Make sure that start is smaller than end.
94         TextPointer smaller;
95         TextPointer bigger;
96
97         if (end.compareTo(start) >= 0) {
98             smaller = start;
99             bigger = end;
100         } else {
101             smaller = end;
102             bigger = start;
103         }
104
105         // Check if this pointer is between the specified pointers.
106         return (compareTo(smaller) >= 0) && (bigger.compareTo(this) > 0);
107     }
108
109 }