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