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