001    /*
002     * Copyright 2010-2013 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.jet.lang.resolve;
018    
019    import org.jetbrains.annotations.NotNull;
020    import org.jetbrains.annotations.Nullable;
021    import org.jetbrains.jet.lang.resolve.name.FqName;
022    import org.jetbrains.jet.lang.resolve.name.Name;
023    
024    public final class ImportPath {
025        private final @NotNull FqName fqName;
026        private final @Nullable Name alias;
027        private final boolean isAllUnder;
028    
029        public ImportPath(@NotNull FqName fqName, boolean isAllUnder) {
030            this(fqName, isAllUnder, null);
031        }
032    
033        public ImportPath(@NotNull FqName fqName, boolean isAllUnder, @Nullable Name alias) {
034            this.fqName = fqName;
035            this.isAllUnder = isAllUnder;
036            this.alias = alias;
037        }
038    
039        public ImportPath(@NotNull String pathStr) {
040            if (pathStr.endsWith(".*")) {
041                this.isAllUnder = true;
042                this.fqName = new FqName(pathStr.substring(0, pathStr.length() - 2));
043            }
044            else {
045                this.isAllUnder = false;
046                this.fqName = new FqName(pathStr);
047            }
048    
049            alias = null;
050        }
051    
052        public String getPathStr() {
053            return fqName.asString() + (isAllUnder ? ".*" : "");
054        }
055    
056        @Override
057        public String toString() {
058            return getPathStr() + (alias != null ? " as " + alias.asString() : "");
059        }
060    
061        @NotNull
062        public FqName fqnPart() {
063            return fqName;
064        }
065    
066        @Nullable
067        public Name getAlias() {
068            return alias;
069        }
070    
071        public boolean hasAlias() {
072            return alias != null;
073        }
074    
075        public boolean isAllUnder() {
076            return isAllUnder;
077        }
078    
079        @Nullable
080        public Name getImportedName() {
081            if (!isAllUnder()) {
082                return alias != null ? alias : fqName.shortName();
083            }
084    
085            return null;
086        }
087    
088        @Override
089        public boolean equals(Object o) {
090            if (this == o) return true;
091            if (o == null || getClass() != o.getClass()) return false;
092    
093            ImportPath path = (ImportPath) o;
094    
095            if (isAllUnder != path.isAllUnder) return false;
096            if (alias != null ? !alias.equals(path.alias) : path.alias != null) return false;
097            if (!fqName.equals(path.fqName)) return false;
098    
099            return true;
100        }
101    
102        @Override
103        public int hashCode() {
104            int result = fqName.hashCode();
105            result = 31 * result + (alias != null ? alias.hashCode() : 0);
106            result = 31 * result + (isAllUnder ? 1 : 0);
107            return result;
108        }
109    }