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.kotlin.builtins.KotlinBuiltIns;
021 import org.jetbrains.kotlin.descriptors.ClassifierDescriptor;
022 import org.jetbrains.kotlin.descriptors.TypeParameterDescriptor;
023 import org.jetbrains.kotlin.descriptors.annotations.AnnotatedImpl;
024 import org.jetbrains.kotlin.descriptors.annotations.Annotations;
025
026 import java.util.*;
027
028 public class IntersectionTypeConstructor extends AnnotatedImpl implements TypeConstructor {
029 private final Set<KotlinType> intersectedTypes;
030 private final int hashCode;
031
032 public IntersectionTypeConstructor(Annotations annotations, Collection<KotlinType> typesToIntersect) {
033 super(annotations);
034 assert !typesToIntersect.isEmpty() : "Attempt to create an empty intersection";
035
036 this.intersectedTypes = new LinkedHashSet<KotlinType>(typesToIntersect);
037 this.hashCode = intersectedTypes.hashCode();
038 }
039
040 @NotNull
041 @Override
042 public List<TypeParameterDescriptor> getParameters() {
043 return Collections.emptyList();
044 }
045
046 @NotNull
047 @Override
048 public Collection<KotlinType> getSupertypes() {
049 return intersectedTypes;
050 }
051
052 @Override
053 public boolean isFinal() {
054 return false;
055 }
056
057 @Override
058 public boolean isDenotable() {
059 return false;
060 }
061
062 @Override
063 public ClassifierDescriptor getDeclarationDescriptor() {
064 return null;
065 }
066
067 @NotNull
068 @Override
069 public KotlinBuiltIns getBuiltIns() {
070 return intersectedTypes.iterator().next().getConstructor().getBuiltIns();
071 }
072
073 @Override
074 public String toString() {
075 return makeDebugNameForIntersectionType(intersectedTypes);
076 }
077
078 private static String makeDebugNameForIntersectionType(Iterable<KotlinType> resultingTypes) {
079 StringBuilder debugName = new StringBuilder("{");
080 for (Iterator<KotlinType> iterator = resultingTypes.iterator(); iterator.hasNext(); ) {
081 KotlinType type = iterator.next();
082
083 debugName.append(type.toString());
084 if (iterator.hasNext()) {
085 debugName.append(" & ");
086 }
087 }
088 debugName.append("}");
089 return debugName.toString();
090 }
091
092 @Override
093 public boolean equals(Object o) {
094 if (this == o) return true;
095 if (o == null || getClass() != o.getClass()) return false;
096
097 IntersectionTypeConstructor that = (IntersectionTypeConstructor) o;
098
099 if (intersectedTypes != null ? !intersectedTypes.equals(that.intersectedTypes) : that.intersectedTypes != null) return false;
100
101 return true;
102 }
103
104 @Override
105 public int hashCode() {
106 return hashCode;
107 }
108
109 }