2 * Sixth 3D engine. Author: Svjatoslav Agejenko.
3 * This project is released under Creative Commons Zero (CC0) license.
5 package eu.svjatoslav.sixth.e3d.gui;
7 import static java.lang.Integer.compare;
10 * A pointer to a character in a text using row and column.
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.
15 public class TextPointer implements Comparable<TextPointer> {
18 * The row of the character. Starts from 0.
23 * The column of the character. Starts from 0.
27 public TextPointer() {
31 public TextPointer(final int row, final int column) {
36 public TextPointer(final TextPointer parent) {
37 this(parent.row, parent.column);
41 public boolean equals(final Object o) {
42 if (o == null) return false;
44 return o instanceof TextPointer && compareTo((TextPointer) o) == 0;
48 public int hashCode() {
50 result = 31 * result + column;
55 * Compares this pointer to another pointer.
57 * @param textPointer The pointer to compare to.
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>
65 public int compareTo(final TextPointer textPointer) {
67 if (row < textPointer.row)
69 if (row > textPointer.row)
72 return compare(column, textPointer.column);
76 * Checks if this pointer is between the argument pointers.
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.
81 * @param start The start pointer.
82 * @param end The end pointer.
83 * @return True if this pointer is between the specified pointers.
85 public boolean isBetween(final TextPointer start, final TextPointer end) {
93 // Make sure that start is smaller than end.
97 if (end.compareTo(start) >= 0) {
105 // Check if this pointer is between the specified pointers.
106 return (compareTo(smaller) >= 0) && (bigger.compareTo(this) > 0);