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.types;
018
019 import org.jetbrains.annotations.NotNull;
020 import org.jetbrains.annotations.Nullable;
021 import org.jetbrains.kotlin.descriptors.annotations.AnnotationDescriptor;
022 import org.jetbrains.kotlin.descriptors.annotations.AnnotationWithTarget;
023 import org.jetbrains.kotlin.renderer.DescriptorRenderer;
024 import org.jetbrains.kotlin.types.checker.JetTypeChecker;
025
026 import java.util.Iterator;
027 import java.util.List;
028
029 public abstract class AbstractJetType implements JetType {
030 @Nullable
031 @Override
032 public <T extends TypeCapability> T getCapability(@NotNull Class<T> capabilityClass) {
033 return getCapabilities().getCapability(capabilityClass);
034 }
035
036 @NotNull
037 @Override
038 public TypeCapabilities getCapabilities() {
039 return TypeCapabilities.NONE.INSTANCE$;
040 }
041
042 @Override
043 public final int hashCode() {
044 int result = getConstructor().hashCode();
045 result = 31 * result + getArguments().hashCode();
046 result = 31 * result + (isMarkedNullable() ? 1 : 0);
047 return result;
048 }
049
050 @Override
051 public final boolean equals(Object obj) {
052 if (this == obj) return true;
053 if (!(obj instanceof JetType)) return false;
054
055 JetType type = (JetType) obj;
056
057 return isMarkedNullable() == type.isMarkedNullable() && JetTypeChecker.FLEXIBLE_UNEQUAL_TO_INFLEXIBLE.equalTypes(this, type);
058 }
059
060 @Override
061 public String toString() {
062 StringBuilder sb = new StringBuilder();
063
064 for (AnnotationWithTarget annotationWithTarget : getAnnotations().getAllAnnotations()) {
065 sb.append("[");
066 sb.append(DescriptorRenderer.DEBUG_TEXT.renderAnnotation(
067 annotationWithTarget.getAnnotation(), annotationWithTarget.getTarget()));
068 sb.append("] ");
069 }
070
071 sb.append(getConstructor());
072
073 List<TypeProjection> arguments = getArguments();
074 if (!arguments.isEmpty()) {
075 sb.append("<");
076 for (Iterator<TypeProjection> i = arguments.iterator(); i.hasNext(); ) {
077 sb.append(i.next());
078 if (i.hasNext()) {
079 sb.append(", ");
080 }
081 }
082 sb.append(">");
083 }
084
085 if (isMarkedNullable()) {
086 sb.append("?");
087 }
088
089 return sb.toString();
090 }
091 }