refactoring

unite two similar methods (replaceTypeVariables, copy)
This commit is contained in:
Svetlana Isakova
2013-03-06 15:06:36 +04:00
parent 12d8a90b3e
commit 54cd8c82a3
2 changed files with 14 additions and 19 deletions
@@ -17,6 +17,7 @@
package org.jetbrains.jet.lang.resolve.calls.inference; package org.jetbrains.jet.lang.resolve.calls.inference;
import com.google.common.base.Function; import com.google.common.base.Function;
import com.google.common.base.Functions;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import com.google.common.collect.Maps; import com.google.common.collect.Maps;
import com.google.common.collect.Sets; import com.google.common.collect.Sets;
@@ -123,19 +124,19 @@ public class ConstraintSystemImpl implements ConstraintSystem {
@Override @Override
@NotNull @NotNull
public ConstraintSystem copy() { public ConstraintSystem copy() {
ConstraintSystemImpl newConstraintSystem = new ConstraintSystemImpl(); return replaceTypeVariables(Functions.<TypeParameterDescriptor>identity(), true);
for (Map.Entry<TypeParameterDescriptor, TypeConstraintsImpl> entry : typeParameterConstraints.entrySet()) {
TypeParameterDescriptor typeParameter = entry.getKey();
TypeConstraintsImpl typeConstraints = entry.getValue();
newConstraintSystem.typeParameterConstraints.put(typeParameter, typeConstraints.copy());
}
newConstraintSystem.errorConstraintPositions.addAll(errorConstraintPositions);
newConstraintSystem.hasErrorInConstrainingTypes = hasErrorInConstrainingTypes;
return newConstraintSystem;
} }
@NotNull @NotNull
public ConstraintSystem replaceTypeVariables(@NotNull Function<TypeParameterDescriptor, TypeParameterDescriptor> typeVariablesMap) { public ConstraintSystem replaceTypeVariables(@NotNull Function<TypeParameterDescriptor, TypeParameterDescriptor> typeVariablesMap) {
return replaceTypeVariables(typeVariablesMap, false);
}
@NotNull
private ConstraintSystem replaceTypeVariables(
@NotNull Function<TypeParameterDescriptor, TypeParameterDescriptor> typeVariablesMap,
boolean recreateTypeConstraints
) {
ConstraintSystemImpl newConstraintSystem = new ConstraintSystemImpl(); ConstraintSystemImpl newConstraintSystem = new ConstraintSystemImpl();
for (Map.Entry<TypeParameterDescriptor, TypeConstraintsImpl> entry : typeParameterConstraints.entrySet()) { for (Map.Entry<TypeParameterDescriptor, TypeConstraintsImpl> entry : typeParameterConstraints.entrySet()) {
TypeParameterDescriptor typeParameter = entry.getKey(); TypeParameterDescriptor typeParameter = entry.getKey();
@@ -143,7 +144,7 @@ public class ConstraintSystemImpl implements ConstraintSystem {
TypeParameterDescriptor newTypeParameter = typeVariablesMap.apply(typeParameter); TypeParameterDescriptor newTypeParameter = typeVariablesMap.apply(typeParameter);
assert newTypeParameter != null; assert newTypeParameter != null;
newConstraintSystem.typeParameterConstraints.put(newTypeParameter, typeConstraints); newConstraintSystem.typeParameterConstraints.put(newTypeParameter, recreateTypeConstraints ? typeConstraints.copy() : typeConstraints);
} }
newConstraintSystem.errorConstraintPositions.addAll(errorConstraintPositions); newConstraintSystem.errorConstraintPositions.addAll(errorConstraintPositions);
newConstraintSystem.hasErrorInConstrainingTypes = hasErrorInConstrainingTypes; newConstraintSystem.hasErrorInConstrainingTypes = hasErrorInConstrainingTypes;
@@ -77,15 +77,9 @@ public class TypeConstraintsImpl implements TypeConstraints {
/*package*/ TypeConstraintsImpl copy() { /*package*/ TypeConstraintsImpl copy() {
TypeConstraintsImpl typeConstraints = new TypeConstraintsImpl(varianceOfPosition); TypeConstraintsImpl typeConstraints = new TypeConstraintsImpl(varianceOfPosition);
for (JetType upperBound : upperBounds) { typeConstraints.upperBounds.addAll(upperBounds);
typeConstraints.upperBounds.add(upperBound); typeConstraints.lowerBounds.addAll(lowerBounds);
} typeConstraints.exactBounds.addAll(exactBounds);
for (JetType lowerBound : lowerBounds) {
typeConstraints.lowerBounds.add(lowerBound);
}
for (JetType exactBound : exactBounds) {
typeConstraints.exactBounds.add(exactBound);
}
return typeConstraints; return typeConstraints;
} }