001    /*
002     * Copyright 2010-2013 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.jet.lang.types;
018    
019    import org.jetbrains.annotations.NotNull;
020    import org.jetbrains.jet.lang.descriptors.ClassifierDescriptor;
021    import org.jetbrains.jet.lang.descriptors.TypeParameterDescriptor;
022    import org.jetbrains.jet.lang.descriptors.annotations.AnnotatedImpl;
023    import org.jetbrains.jet.lang.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            this.intersectedTypes = new LinkedHashSet<JetType>(typesToIntersect);
034            this.hashCode = intersectedTypes.hashCode();
035        }
036    
037        @NotNull
038        @Override
039        public List<TypeParameterDescriptor> getParameters() {
040            return Collections.emptyList();
041        }
042    
043        @NotNull
044        @Override
045        public Collection<JetType> getSupertypes() {
046            return intersectedTypes;
047        }
048    
049        @Override
050        public boolean isFinal() {
051            return false;
052        }
053    
054        @Override
055        public boolean isDenotable() {
056            return false;
057        }
058    
059        @Override
060        public ClassifierDescriptor getDeclarationDescriptor() {
061            return null;
062        }
063    
064        @Override
065        public String toString() {
066            return makeDebugNameForIntersectionType(intersectedTypes);
067        }
068    
069        private static String makeDebugNameForIntersectionType(Iterable<JetType> resultingTypes) {
070            StringBuilder debugName = new StringBuilder("{");
071            for (Iterator<JetType> iterator = resultingTypes.iterator(); iterator.hasNext(); ) {
072                JetType type = iterator.next();
073    
074                debugName.append(type.toString());
075                if (iterator.hasNext()) {
076                    debugName.append(" & ");
077                }
078            }
079            debugName.append("}");
080            return debugName.toString();
081        }
082    
083        @Override
084        public boolean equals(Object o) {
085            if (this == o) return true;
086            if (o == null || getClass() != o.getClass()) return false;
087    
088            IntersectionTypeConstructor that = (IntersectionTypeConstructor) o;
089    
090            if (intersectedTypes != null ? !intersectedTypes.equals(that.intersectedTypes) : that.intersectedTypes != null) return false;
091    
092            return true;
093        }
094    
095        @Override
096        public int hashCode() {
097            return hashCode;
098        }
099    
100    }