Replace Map operations with computeIfAbsent

This commit is contained in:
Alexander Udalov
2017-04-01 01:54:14 +03:00
parent 5ebee6ceca
commit a9f35ae898
11 changed files with 16 additions and 64 deletions
@@ -177,11 +177,7 @@ public class ClassFileFactory implements OutputFileCollection {
private PackagePartRegistry buildNewPackagePartRegistry(@NotNull FqName packageFqName) {
String packageFqNameAsString = packageFqName.asString();
return (partShortName, facadeShortName) -> {
PackageParts packageParts = partsGroupedByPackage.get(packageFqNameAsString);
if (packageParts == null) {
packageParts = new PackageParts(packageFqNameAsString);
partsGroupedByPackage.put(packageFqNameAsString, packageParts);
}
PackageParts packageParts = partsGroupedByPackage.computeIfAbsent(packageFqNameAsString, PackageParts::new);
packageParts.addPart(partShortName, facadeShortName);
};
}
@@ -56,11 +56,8 @@ public abstract class FieldOwnerContext<T extends DeclarationDescriptor> extends
String defaultPropertyName = KotlinTypeMapper.mapDefaultFieldName(descriptor, isDelegated);
Map<PropertyDescriptor, String> descriptor2Name = fieldNames.get(defaultPropertyName);
if (descriptor2Name == null) {
descriptor2Name = new HashMap<PropertyDescriptor, String>();
fieldNames.put(defaultPropertyName, descriptor2Name);
}
Map<PropertyDescriptor, String> descriptor2Name =
fieldNames.computeIfAbsent(defaultPropertyName, unused -> new HashMap<PropertyDescriptor, String>());
String actualName = descriptor2Name.get(descriptor);
if (actualName != null) return actualName;
@@ -524,11 +524,7 @@ public class AnonymousObjectTransformer extends ObjectTransformer<AnonymousObjec
@NotNull
private String addUniqueField(@NotNull String name) {
List<String> existNames = fieldNames.get(name);
if (existNames == null) {
existNames = new LinkedList<String>();
fieldNames.put(name, existNames);
}
List<String> existNames = fieldNames.computeIfAbsent(name, unused -> new LinkedList<String>());
String suffix = existNames.isEmpty() ? "" : "$" + existNames.size();
String newName = name + suffix;
existNames.add(newName);
@@ -48,12 +48,7 @@ public class NameGenerator {
}
public NameGenerator subGenerator(String inliningMethod) {
NameGenerator generator = subGenerators.get(inliningMethod);
if (generator == null) {
generator = new NameGenerator(generatorClass + "$" + inliningMethod);
subGenerators.put(inliningMethod, generator);
}
return generator;
return subGenerators.computeIfAbsent(inliningMethod, method -> new NameGenerator(generatorClass + "$" + method));
}
@NotNull
@@ -120,12 +120,7 @@ public class AsmTypes {
@NotNull
public static Type getType(@NotNull Class<?> javaClass) {
Type type = TYPES_MAP.get(javaClass);
if (type == null) {
type = Type.getType(javaClass);
TYPES_MAP.put(javaClass, type);
}
return type;
return TYPES_MAP.computeIfAbsent(javaClass, k -> Type.getType(javaClass));
}
private AsmTypes() {
@@ -76,9 +76,7 @@ public class CompilerConfiguration {
public <T> void add(@NotNull CompilerConfigurationKey<List<T>> key, @NotNull T value) {
checkReadOnly();
Key<List<T>> ideaKey = key.ideaKey;
if (map.get(ideaKey) == null) {
map.put(ideaKey, new ArrayList<T>());
}
map.computeIfAbsent(ideaKey, k -> new ArrayList<T>());
List<T> list = (List<T>) map.get(ideaKey);
list.add(value);
}
@@ -86,9 +84,7 @@ public class CompilerConfiguration {
public <K, V> void put(@NotNull CompilerConfigurationKey<Map<K, V>> configurationKey, @NotNull K key, @NotNull V value) {
checkReadOnly();
Key<Map<K, V>> ideaKey = configurationKey.ideaKey;
if (map.get(ideaKey) == null) {
map.put(ideaKey, new HashMap<K, V>());
}
map.computeIfAbsent(ideaKey, k -> new HashMap<K, V>());
Map<K, V> data = (Map<K, V>) map.get(ideaKey);
data.put(key, value);
}
@@ -97,9 +93,7 @@ public class CompilerConfiguration {
checkReadOnly();
checkForNullElements(values);
Key<List<T>> ideaKey = key.ideaKey;
if (map.get(ideaKey) == null) {
map.put(ideaKey, new ArrayList<T>());
}
map.computeIfAbsent(ideaKey, k -> new ArrayList<T>());
List<T> list = (List<T>) map.get(ideaKey);
list.addAll(values);
}
@@ -332,11 +332,7 @@ public class ValueArgumentsToParametersMapper {
private void putVararg(ValueParameterDescriptor valueParameterDescriptor, ValueArgument valueArgument) {
if (valueParameterDescriptor.getVarargElementType() != null) {
VarargValueArgument vararg = varargs.get(valueParameterDescriptor);
if (vararg == null) {
vararg = new VarargValueArgument();
varargs.put(valueParameterDescriptor, vararg);
}
VarargValueArgument vararg = varargs.computeIfAbsent(valueParameterDescriptor, k -> new VarargValueArgument());
vararg.addArgument(valueArgument);
}
else {
@@ -348,13 +348,9 @@ public class CommonSupertypes {
new DFS.NodeHandlerWithListResult<SimpleType, TypeConstructor>() {
@Override
public boolean beforeChildren(SimpleType current) {
TypeConstructor constructor = current.getConstructor();
Set<SimpleType> instances = constructorToAllInstances.get(constructor);
if (instances == null) {
instances = new HashSet<SimpleType>();
constructorToAllInstances.put(constructor, instances);
}
Set<SimpleType> instances = constructorToAllInstances.computeIfAbsent(
current.getConstructor(), k -> new HashSet<SimpleType>()
);
instances.add(current);
return true;
@@ -33,11 +33,7 @@ public class TrackingSlicedMap extends SlicedMapImpl {
}
private <K, V> SliceWithStackTrace<K, V> wrapSlice(ReadOnlySlice<K, V> slice) {
SliceWithStackTrace<?, ?> translated = sliceTranslationMap.get(slice);
if (translated == null) {
translated = new SliceWithStackTrace<K, V>(slice);
sliceTranslationMap.put(slice, translated);
}
SliceWithStackTrace<?, ?> translated = sliceTranslationMap.computeIfAbsent(slice, k -> new SliceWithStackTrace<K, V>(slice));
//noinspection unchecked
return (SliceWithStackTrace) translated;
}
@@ -179,12 +179,7 @@ public final class RhinoUtils {
@NotNull
private static Scriptable getParentScope(@NotNull EcmaVersion version, @NotNull Context context, @NotNull List<String> jsLibraries) {
ScriptableObject parentScope = versionToScope.get(version);
if (parentScope == null) {
parentScope = initScope(version, context, jsLibraries);
versionToScope.put(version, parentScope);
}
return parentScope;
return versionToScope.computeIfAbsent(version, v -> initScope(v, context, jsLibraries));
}
@NotNull
@@ -237,11 +237,7 @@ public final class StaticContext {
@NotNull
private JsExpression getQualifiedExpression(@NotNull DeclarationDescriptor descriptor) {
JsExpression fqn = fqnCache.get(descriptor);
if (fqn == null) {
fqn = buildQualifiedExpression(descriptor);
fqnCache.put(descriptor, fqn);
}
JsExpression fqn = fqnCache.computeIfAbsent(descriptor, this::buildQualifiedExpression);
return fqn.deepCopy();
}