Improved code readability
[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 /**
8  * A pointer to a character in a text using row and column.
9  * <p>
10  * It can be used to represent a cursor position in a text.
11  * Also, it can be used to represent beginning and end of a selection.
12  */
13 public class TextPointer implements Comparable<TextPointer> {
14
15     /**
16      * The row of the character. Starts from 0.
17      */
18     public int row;
19
20     /**
21      * The column of the character. Starts from 0.
22      */
23     public int column;
24
25     public TextPointer() {
26         this(0, 0);
27     }
28
29     public TextPointer(final int row, final int column) {
30         this.row = row;
31         this.column = column;
32     }
33
34     public TextPointer(final TextPointer parent) {
35         this(parent.row, parent.column);
36     }
37
38     @Override
39     public boolean equals(final Object o) {
40         if (o == null) return false;
41
42         return o instanceof TextPointer && compareTo((TextPointer) o) == 0;
43     }
44
45     @Override
46     public int hashCode() {
47         int result = row;
48         result = 31 * result + column;
49         return result;
50     }
51
52     @Override
53     public int compareTo(final TextPointer textPointer) {
54
55         if (textPointer.row > row)
56             return -1;
57         if (textPointer.row < row)
58             return 1;
59
60         return Integer.compare(column, textPointer.column);
61
62     }
63
64     /**
65      * Checks if this pointer is between the specified pointers.
66      *
67      * @param start The start pointer.
68      * @param end   The end pointer.
69      * @return True if this pointer is between the specified pointers.
70      */
71     public boolean isBetween(final TextPointer start, final TextPointer end) {
72
73         if (start == null)
74             return false;
75
76         if (end == null)
77             return false;
78
79         // Make sure that start is smaller than end.
80         TextPointer smaller;
81         TextPointer bigger;
82
83         if (end.compareTo(start) >= 0) {
84             smaller = start;
85             bigger = end;
86         } else {
87             smaller = end;
88             bigger = start;
89         }
90
91         // Check if this pointer is between the specified pointers.
92         return (compareTo(smaller) >= 0) && (bigger.compareTo(this) > 0);
93     }
94
95 }