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