All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.kaazing.robot.lang.LocationInfo Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (c) 2014 "Kaazing Corporation," (www.kaazing.com)
 *
 * This file is part of Robot.
 *
 * Robot is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see .
 */

package org.kaazing.robot.lang;

import static java.lang.String.format;

public final class LocationInfo implements Comparable {
    public final int line;
    public final int column;

    public LocationInfo(int line, int column) {
        this.line = line;
        this.column = column;
    }

    @Override
    public int hashCode() {
        return line ^ column;
    }

    @Override
    public boolean equals(Object obj) {
        return (this == obj) || ((obj instanceof LocationInfo) && equalTo((LocationInfo) obj));
    }

    @Override
    public String toString() {
        return format("%d:%d", line, column);
    }

    @Override
    public int compareTo(LocationInfo that) {
        final int LESS = -1;
        final int EQUAL = 0;
        final int MORE = 1;

        if (this.equalTo(that)) {
            return EQUAL;
        }

        if (this.line < that.line) {
            return LESS;
        } else if (this.line > that.line) {
            return MORE;
        } else if (this.column < that.column) {
            return LESS;
        } else if (this.column > that.column) {
            return MORE;
        }

        assert this.equals(that) : "compareTo inconsitant with equals";
        return EQUAL;
    }

    /* Inclusive. */
    public boolean isBetween(LocationInfo start, LocationInfo end) {
        assert start.compareTo(end) <= 0 : String.format("end |%s| before start |%s|", end, start);
        return start.compareTo(this) <= 0 && end.compareTo(this) >= 0;
    }

    private boolean equalTo(LocationInfo that) {
        return this.line == that.line && this.column == that.column;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy