added 'commonSupertypeForPossiblyNumberTypes'

This commit is contained in:
Svetlana Isakova
2013-07-31 18:13:33 +04:00
parent e4325cd92d
commit c5b6ee4df3
2 changed files with 32 additions and 10 deletions
@@ -48,16 +48,11 @@ public class ConstraintsUtil {
}
values.addAll(exactBounds);
Collection<JetType> lowerBounds = Sets.newHashSet();
Collection<JetType> numberLowerBounds = Sets.newHashSet();
for (JetType lowerBound : typeConstraintsWithoutErrorTypes.getLowerBounds()) {
if (lowerBound.getConstructor() instanceof NumberValueTypeConstructor) {
numberLowerBounds.add(lowerBound);
}
else {
lowerBounds.add(lowerBound);
}
}
Pair<Collection<JetType>, Collection<JetType>> pair =
TypeUtils.filterNumberTypes(typeConstraintsWithoutErrorTypes.getLowerBounds());
Collection<JetType> lowerBounds = pair.getFirst();
Collection<JetType> numberLowerBounds = pair.getSecond();
JetType superTypeOfLowerBounds = commonSupertype(lowerBounds);
if (trySuggestion(superTypeOfLowerBounds, typeConstraints)) {
return Collections.singleton(superTypeOfLowerBounds);
@@ -21,6 +21,7 @@ import com.google.common.collect.Collections2;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import com.intellij.openapi.util.Pair;
import com.intellij.util.Processor;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -616,4 +617,30 @@ public class TypeUtils {
}
return getDefaultPrimitiveNumberType(numberValueTypeConstructor);
}
@NotNull
public static Pair<Collection<JetType>, Collection<JetType>> filterNumberTypes(@NotNull Collection<JetType> types) {
Collection<JetType> numberTypes = Sets.newLinkedHashSet();
Collection<JetType> otherTypes = Sets.newLinkedHashSet();
for (JetType type : types) {
if (type.getConstructor() instanceof NumberValueTypeConstructor) {
numberTypes.add(type);
}
else {
otherTypes.add(type);
}
}
return Pair.create(otherTypes, numberTypes);
}
@NotNull
public static JetType commonSupertypeForPossiblyNumberTypes(@NotNull Collection<JetType> types) {
Pair<Collection<JetType>, Collection<JetType>> pair = filterNumberTypes(types);
Collection<JetType> numberTypes = pair.getSecond();
Collection<JetType> otherTypes = pair.getFirst();
if (!numberTypes.isEmpty()) {
otherTypes.add(commonSupertypeForNumberTypes(numberTypes));
}
return CommonSupertypes.commonSupertype(otherTypes);
}
}