view truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/source/LineLocation.java @ 22157:dc83cc1f94f2

Using fully qualified imports
author Jaroslav Tulach <jaroslav.tulach@oracle.com>
date Wed, 16 Sep 2015 11:33:22 +0200
parents b2d1c8ff592a
children 5573f12b94f8
line wrap: on
line source

/*
 * Copyright (c) 2013, 2015, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Oracle designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Oracle in the LICENSE file that accompanied this code.
 *
 * This code 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 General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */
package com.oracle.truffle.api.source;

/**
 * A specification for a location in guest language source, expressed as a line number in a specific
 * instance of {@link Source}, suitable for hash table keys with equality defined in terms of
 * content.
 */
public final class LineLocation implements Comparable<LineLocation> {
    private final Source source;
    private final int line;

    LineLocation(Source source, int line) {
        assert source != null;
        this.source = source;
        this.line = line;
    }

    public Source getSource() {
        return source;
    }

    /**
     * Gets the 1-based number of a line in the source.
     * 
     * @return value from 1 to infinity
     */
    public int getLineNumber() {
        return line;
    }

    public String getShortDescription() {
        return source.getShortName() + ":" + line;
    }

    @Override
    public String toString() {
        return "Line[" + getShortDescription() + "]";
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + line;
        result = prime * result + source.getHashKey().hashCode();
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (!(obj instanceof LineLocation)) {
            return false;
        }
        LineLocation other = (LineLocation) obj;
        if (line != other.line) {
            return false;
        }
        return source.getHashKey().equals(other.source.getHashKey());
    }

    @Override
    public int compareTo(LineLocation o) {
        final int sourceResult = this.getSource().getPath().compareTo(o.getSource().getPath());
        if (sourceResult != 0) {
            return sourceResult;
        }
        return Integer.compare(this.getLineNumber(), o.getLineNumber());
    }
}