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