001    /*
002     * Copyright 2010-2015 JetBrains s.r.o.
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    
017    package org.jetbrains.kotlin.cli.common.messages;
018    
019    import org.jetbrains.annotations.Nullable;
020    
021    public class CompilerMessageLocation {
022    
023        public static final CompilerMessageLocation NO_LOCATION = new CompilerMessageLocation(null, -1, -1);
024    
025        public static CompilerMessageLocation create(@Nullable String path, int line, int column) {
026            if (path == null) {
027                return NO_LOCATION;
028            }
029            return new CompilerMessageLocation(path, line, column);
030        }
031    
032        private final String path;
033        private final int line;
034        private final int column;
035    
036        private CompilerMessageLocation(@Nullable String path, int line, int column) {
037            this.path = path;
038            this.line = line;
039            this.column = column;
040        }
041    
042        @Nullable
043        public String getPath() {
044            return path;
045        }
046    
047        public int getLine() {
048            return line;
049        }
050    
051        public int getColumn() {
052            return column;
053        }
054    
055        @Override
056        public boolean equals(Object o) {
057            if (this == o) return true;
058            if (o == null || getClass() != o.getClass()) return false;
059    
060            CompilerMessageLocation location = (CompilerMessageLocation) o;
061    
062            if (column != location.column) return false;
063            if (line != location.line) return false;
064            if (path != null ? !path.equals(location.path) : location.path != null) return false;
065    
066            return true;
067        }
068    
069        @Override
070        public int hashCode() {
071            int result = path != null ? path.hashCode() : 0;
072            result = 31 * result + line;
073            result = 31 * result + column;
074            return result;
075        }
076    
077        @Override
078        public String toString() {
079            return path + ((line != -1 || column != -1) ? " (" + line + ":" + column + ")" : "");
080        }
081    }