aboutsummaryrefslogtreecommitdiffstats
path: root/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/editor/Position.java
blob: 8f892cf86502d9d5d1f1ecbd0a1afd8e13818c03 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.node.admin.task.util.editor;

import java.util.Comparator;
import java.util.Objects;

/**
 * Represents a position in the buffer
 *
 * @author hakon
 */
public class Position implements Comparable<Position> {
    private static final Position START_POSITION = new Position(0, 0);

    private static final Comparator<Position> COMPARATOR = Comparator
            .comparingInt(Position::lineIndex)
            .thenComparingInt(Position::columnIndex);

    private final int lineIndex;
    private final int columnIndex;

    /** Returns the first position at line index 0 and column index 0 */
    public static Position start() {
        return START_POSITION;
    }

    Position(int lineIndex, int columnIndex) {
        this.lineIndex = lineIndex;
        this.columnIndex = columnIndex;
    }

    public int lineIndex() {
        return lineIndex;
    }

    public int columnIndex() {
        return columnIndex;
    }

    @Override
    public int compareTo(Position that) {
        return COMPARATOR.compare(this, that);
    }

    public boolean isAfter(Position that) { return compareTo(that) > 0; }
    public boolean isNotAfter(Position that) { return !isAfter(that); }
    public boolean isBefore(Position that) { return compareTo(that) < 0; }
    public boolean isNotBefore(Position that) { return !isBefore(that); }

    public String coordinateString() {
        return "(" + lineIndex + "," + columnIndex + ")";
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Position position = (Position) o;
        return lineIndex == position.lineIndex &&
                columnIndex == position.columnIndex;
    }

    @Override
    public int hashCode() {
        return Objects.hash(lineIndex, columnIndex);
    }

    @Override
    public String toString() {
        return coordinateString();
    }
}