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
017package org.jetbrains.jet.lang.resolve.calls.inference;
018
019import com.google.common.collect.Sets;
020import org.jetbrains.annotations.NotNull;
021import org.jetbrains.jet.lang.descriptors.TypeParameterDescriptor;
022import org.jetbrains.jet.lang.types.JetType;
023import org.jetbrains.jet.lang.types.Variance;
024
025import java.util.Set;
026
027public class TypeValue implements BoundsOwner {
028    private final Set<TypeValue> upperBounds = Sets.newLinkedHashSet();
029    private final Set<TypeValue> lowerBounds = Sets.newLinkedHashSet();
030
031    private final Variance positionVariance;
032    private final TypeParameterDescriptor typeParameterDescriptor; // Null for known types
033    private final JetType originalType;
034    private JetType value; // For an unknown - the value found by constraint resolution, for a known - just it's value
035
036    // Unknown type
037    public TypeValue(@NotNull TypeParameterDescriptor typeParameterDescriptor, @NotNull Variance positionVariance) {
038        this.positionVariance = positionVariance;
039        this.typeParameterDescriptor = typeParameterDescriptor;
040        this.originalType = typeParameterDescriptor.getDefaultType();
041    }
042
043    // Known type
044    public TypeValue(@NotNull JetType knownType) {
045        this.positionVariance = null;
046        this.typeParameterDescriptor = null;
047        this.originalType = knownType;
048        this.value = knownType;
049    }
050
051    public boolean isKnown() {
052        return typeParameterDescriptor == null;
053    }
054
055    public TypeParameterDescriptor getTypeParameterDescriptor() {
056        return typeParameterDescriptor;
057    }
058
059    @NotNull
060    public Variance getPositionVariance() {
061        return positionVariance;
062    }
063
064    @Override
065    @NotNull
066    public Set<TypeValue> getUpperBounds() {
067        return upperBounds;
068    }
069
070    @Override
071    @NotNull
072    public Set<TypeValue> getLowerBounds() {
073        return lowerBounds;
074    }
075
076    @NotNull
077    public JetType getType() {
078        return value;
079    }
080
081    @NotNull
082    public JetType getOriginalType() {
083        return originalType;
084    }
085
086    public void addUpperBound(@NotNull TypeValue bound) {
087        upperBounds.add(bound);
088    }
089
090    public void addLowerBound(@NotNull TypeValue bound) {
091        lowerBounds.add(bound);
092    }
093
094    public void setValue(@NotNull JetType value) {
095        this.value = value;
096    }
097
098    public boolean hasValue() {
099        return value != null;
100    }
101
102    @Override
103    public String toString() {
104        return isKnown() ? getType().toString() : (getTypeParameterDescriptor() + (hasValue() ? " |-> " + getType() : ""));
105    }
106}