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.types.JetType;
022import org.jetbrains.jet.lang.types.Variance;
023
024import java.util.Set;
025
026public class TypeConstraintsImpl implements TypeConstraints {
027    private final Variance varianceOfPosition;
028    private final Set<JetType> upperBounds = Sets.newLinkedHashSet();
029    private final Set<JetType> lowerBounds = Sets.newLinkedHashSet();
030    private final Set<JetType> exactBounds = Sets.newLinkedHashSet();
031
032    public TypeConstraintsImpl(Variance varianceOfPosition) {
033        this.varianceOfPosition = varianceOfPosition;
034    }
035
036    @NotNull
037    @Override
038    public Variance getVarianceOfPosition() {
039        return varianceOfPosition;
040    }
041
042    public void addBound(@NotNull BoundKind boundKind, @NotNull JetType type) {
043        switch (boundKind) {
044            case LOWER_BOUND:
045                lowerBounds.add(type);
046                break;
047            case UPPER_BOUND:
048                upperBounds.add(type);
049                break;
050            case EXACT_BOUND:
051                exactBounds.add(type);
052        }
053    }
054
055    @Override
056    public boolean isEmpty() {
057        return upperBounds.isEmpty() && lowerBounds.isEmpty() && exactBounds.isEmpty();
058    }
059
060    @NotNull
061    @Override
062    public Set<JetType> getLowerBounds() {
063        return lowerBounds;
064    }
065
066    @NotNull
067    @Override
068    public Set<JetType> getUpperBounds() {
069        return upperBounds;
070    }
071
072    @NotNull
073    @Override
074    public Set<JetType> getExactBounds() {
075        return exactBounds;
076    }
077
078    /*package*/ TypeConstraintsImpl copy() {
079        TypeConstraintsImpl typeConstraints = new TypeConstraintsImpl(varianceOfPosition);
080        typeConstraints.upperBounds.addAll(upperBounds);
081        typeConstraints.lowerBounds.addAll(lowerBounds);
082        typeConstraints.exactBounds.addAll(exactBounds);
083        return typeConstraints;
084    }
085
086    public static enum BoundKind {
087        LOWER_BOUND, UPPER_BOUND, EXACT_BOUND
088    }
089}