From 8a7738690abc455c4fdb5b5522c835aae6ebb249 Mon Sep 17 00:00:00 2001 From: Tagir Valeev Date: Mon, 19 Sep 2016 10:52:42 +0700 Subject: [PATCH] replace to computeIfAbsent() in several places using new Java 8 migration quick-fix --- .../AbstractIdeModifiableModelsProvider.java | 23 +++---------- .../ui/AbstractNavBarUI.java | 12 ++----- .../impl/nodes/AbstractProjectNode.java | 9 ++--- .../AbstractMemberInfoStorage.java | 17 ++-------- .../ide/util/treeView/AbstractTreeUi.java | 7 +--- .../openapi/actionSystem/ActionGroupUtil.java | 9 ++--- .../impl/AbbreviationManagerImpl.java | 33 ++++--------------- .../actionSystem/impl/ActionManagerImpl.java | 9 ++--- .../treeView/AbstractTreeBuilderTest.java | 10 ++---- .../update/AbstractCommonUpdateAction.java | 9 ++--- .../functions/AbstractFunctionContext.java | 9 ++--- 11 files changed, 28 insertions(+), 119 deletions(-) diff --git a/platform/external-system-impl/src/com/intellij/openapi/externalSystem/service/project/AbstractIdeModifiableModelsProvider.java b/platform/external-system-impl/src/com/intellij/openapi/externalSystem/service/project/AbstractIdeModifiableModelsProvider.java index 996514096ac4..1bc0db620904 100644 --- a/platform/external-system-impl/src/com/intellij/openapi/externalSystem/service/project/AbstractIdeModifiableModelsProvider.java +++ b/platform/external-system-impl/src/com/intellij/openapi/externalSystem/service/project/AbstractIdeModifiableModelsProvider.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2009 JetBrains s.r.o. + * Copyright 2000-2016 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -181,23 +181,13 @@ public abstract class AbstractIdeModifiableModelsProvider extends IdeModelsProvi @NotNull private ModuleRootModel getRootModel(Module module) { - ModifiableRootModel result = myModifiableRootModels.get(module); - if (result == null) { - result = doGetModifiableRootModel(module); - myModifiableRootModels.put(module, result); - } - return result; + return myModifiableRootModels.computeIfAbsent(module, k -> doGetModifiableRootModel(module)); } @Override @NotNull public ModifiableFacetModel getModifiableFacetModel(Module module) { - ModifiableFacetModel result = myModifiableFacetModels.get(module); - if (result == null) { - result = doGetModifiableFacetModel(module); - myModifiableFacetModels.put(module, result); - } - return result; + return myModifiableFacetModels.computeIfAbsent(module, k -> doGetModifiableFacetModel(module)); } @Override @@ -233,12 +223,7 @@ public abstract class AbstractIdeModifiableModelsProvider extends IdeModelsProvi @Override public Library.ModifiableModel getModifiableLibraryModel(Library library) { - Library.ModifiableModel result = myModifiableLibraryModels.get(library); - if (result == null) { - result = doGetModifiableLibraryModel(library); - myModifiableLibraryModels.put(library, result); - } - return result; + return myModifiableLibraryModels.computeIfAbsent(library, k -> doGetModifiableLibraryModel(library)); } @NotNull diff --git a/platform/lang-impl/src/com/intellij/ide/navigationToolbar/ui/AbstractNavBarUI.java b/platform/lang-impl/src/com/intellij/ide/navigationToolbar/ui/AbstractNavBarUI.java index 4d52991f8d34..7a6da6373ecc 100644 --- a/platform/lang-impl/src/com/intellij/ide/navigationToolbar/ui/AbstractNavBarUI.java +++ b/platform/lang-impl/src/com/intellij/ide/navigationToolbar/ui/AbstractNavBarUI.java @@ -92,7 +92,6 @@ public abstract class AbstractNavBarUI implements NavBarUI { final boolean selected = item.isSelected() && item.isFocused(); boolean nextSelected = item.isNextSelected() && navbar.hasFocus(); - Map cached = myCache.get(item); ImageType type; if (floating) { @@ -105,16 +104,9 @@ public abstract class AbstractNavBarUI implements NavBarUI { } } - if (cached == null) { - cached = new HashMap<>(); - myCache.put(item, cached); - } + Map cached = myCache.computeIfAbsent(item, k -> new HashMap<>()); - BufferedImage image = cached.get(type); - if (image == null) { - image = drawToBuffer(item, floating, toolbarVisible, selected, navbar); - cached.put(type, image); - } + BufferedImage image = cached.computeIfAbsent(type, k -> drawToBuffer(item, floating, toolbarVisible, selected, navbar)); UIUtil.drawImage(g, image, 0, 0, null); diff --git a/platform/lang-impl/src/com/intellij/ide/projectView/impl/nodes/AbstractProjectNode.java b/platform/lang-impl/src/com/intellij/ide/projectView/impl/nodes/AbstractProjectNode.java index 0695f4d53432..b7e24a7a3cb5 100644 --- a/platform/lang-impl/src/com/intellij/ide/projectView/impl/nodes/AbstractProjectNode.java +++ b/platform/lang-impl/src/com/intellij/ide/projectView/impl/nodes/AbstractProjectNode.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2009 JetBrains s.r.o. + * Copyright 2000-2016 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -46,12 +46,7 @@ public abstract class AbstractProjectNode extends ProjectViewNode { final String[] path = ModuleManager.getInstance(getProject()).getModuleGroupPath(module); if (path != null) { final String topLevelGroupName = path[0]; - List moduleList = groups.get(topLevelGroupName); - if (moduleList == null) { - moduleList = new ArrayList<>(); - groups.put(topLevelGroupName, moduleList); - } - moduleList.add(module); + groups.computeIfAbsent(topLevelGroupName, k -> new ArrayList<>()).add(module); nonGroupedModules.remove(module); } } diff --git a/platform/lang-impl/src/com/intellij/refactoring/classMembers/AbstractMemberInfoStorage.java b/platform/lang-impl/src/com/intellij/refactoring/classMembers/AbstractMemberInfoStorage.java index 6fd43e08bac6..4c887eb253b4 100644 --- a/platform/lang-impl/src/com/intellij/refactoring/classMembers/AbstractMemberInfoStorage.java +++ b/platform/lang-impl/src/com/intellij/refactoring/classMembers/AbstractMemberInfoStorage.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2009 JetBrains s.r.o. + * Copyright 2000-2016 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -110,22 +110,11 @@ public abstract class AbstractMemberInfoStorage getSubclasses(C aClass) { - LinkedHashSet result = myClassToSubclassesMap.get(aClass); - if(result == null) { - result = new LinkedHashSet<>(); - myClassToSubclassesMap.put(aClass, result); - } - return result; + return myClassToSubclassesMap.computeIfAbsent(aClass, k -> new LinkedHashSet<>()); } public Set getDuplicatedMemberInfos(C baseClass) { - HashSet result = myTargetClassToDuplicatedMemberInfosMap.get(baseClass); - - if(result == null) { - result = buildDuplicatedMemberInfos(baseClass); - myTargetClassToDuplicatedMemberInfosMap.put(baseClass, result); - } - return result; + return myTargetClassToDuplicatedMemberInfosMap.computeIfAbsent(baseClass, k -> buildDuplicatedMemberInfos(baseClass)); } private HashSet buildDuplicatedMemberInfos(C baseClass) { diff --git a/platform/platform-api/src/com/intellij/ide/util/treeView/AbstractTreeUi.java b/platform/platform-api/src/com/intellij/ide/util/treeView/AbstractTreeUi.java index edeff2e1eb9c..34e77c37f1ec 100644 --- a/platform/platform-api/src/com/intellij/ide/util/treeView/AbstractTreeUi.java +++ b/platform/platform-api/src/com/intellij/ide/util/treeView/AbstractTreeUi.java @@ -4694,12 +4694,7 @@ public class AbstractTreeUi { private void _addNodeAction(Object element, NodeAction action, @NotNull Map> map) { maybeSetBusyAndScheduleWaiterForReady(true, element); - List list = map.get(element); - if (list == null) { - list = new ArrayList<>(); - map.put(element, list); - } - list.add(action); + map.computeIfAbsent(element, k -> new ArrayList<>()).add(action); addActivity(); } diff --git a/platform/platform-api/src/com/intellij/openapi/actionSystem/ActionGroupUtil.java b/platform/platform-api/src/com/intellij/openapi/actionSystem/ActionGroupUtil.java index 2a95f73b0aa4..6c903183843f 100644 --- a/platform/platform-api/src/com/intellij/openapi/actionSystem/ActionGroupUtil.java +++ b/platform/platform-api/src/com/intellij/openapi/actionSystem/ActionGroupUtil.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2009 JetBrains s.r.o. + * Copyright 2000-2016 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -27,12 +27,7 @@ import java.util.Map; public class ActionGroupUtil { private static Presentation getPresentation(AnAction action, Map action2presentation) { - Presentation presentation = action2presentation.get(action); - if (presentation == null) { - presentation = action.getTemplatePresentation().clone(); - action2presentation.put(action, presentation); - } - return presentation; + return action2presentation.computeIfAbsent(action, k -> action.getTemplatePresentation().clone()); } public static boolean isGroupEmpty(@NotNull ActionGroup actionGroup, @NotNull AnActionEvent e) { diff --git a/platform/platform-impl/src/com/intellij/openapi/actionSystem/impl/AbbreviationManagerImpl.java b/platform/platform-impl/src/com/intellij/openapi/actionSystem/impl/AbbreviationManagerImpl.java index d66e06e90add..0e9f72d7f205 100644 --- a/platform/platform-impl/src/com/intellij/openapi/actionSystem/impl/AbbreviationManagerImpl.java +++ b/platform/platform-impl/src/com/intellij/openapi/actionSystem/impl/AbbreviationManagerImpl.java @@ -25,6 +25,7 @@ import org.jdom.Element; import org.jetbrains.annotations.Nullable; import java.util.*; +import java.util.stream.Collectors; /** * @author Konstantin Bulenkov @@ -82,11 +83,7 @@ public class AbbreviationManagerImpl extends AbbreviationManager implements Pers if (actions != null && actions.size() > 0) { for (Element action : actions) { final String actionId = action.getAttributeValue("id"); - LinkedHashSet values = myActionId2Abbreviations.get(actionId); - if (values == null) { - values = new LinkedHashSet<>(1); - myActionId2Abbreviations.put(actionId, values); - } + LinkedHashSet values = myActionId2Abbreviations.computeIfAbsent(actionId, k -> new LinkedHashSet<>(1)); final List abbreviation = action.getChildren("abbreviation"); if (abbreviation != null) { @@ -94,12 +91,7 @@ public class AbbreviationManagerImpl extends AbbreviationManager implements Pers final String abbrValue = abbr.getAttributeValue("name"); if (abbrValue != null) { values.add(abbrValue); - List actionIds = myAbbreviation2ActionId.get(abbrValue); - if (actionIds == null) { - actionIds = new ArrayList<>(); - myAbbreviation2ActionId.put(abbrValue, actionIds); - } - actionIds.add(actionId); + myAbbreviation2ActionId.computeIfAbsent(abbrValue, k -> new ArrayList<>()).add(actionId); } } } @@ -110,11 +102,7 @@ public class AbbreviationManagerImpl extends AbbreviationManager implements Pers @Override public Set getAbbreviations() { - final Set result = new HashSet<>(); - for (Set abbrs : myActionId2Abbreviations.values()) { - result.addAll(abbrs); - } - return result; + return myActionId2Abbreviations.values().stream().flatMap(Set::stream).collect(Collectors.toSet()); } @Override @@ -134,12 +122,7 @@ public class AbbreviationManagerImpl extends AbbreviationManager implements Pers public void register(String abbreviation, String actionId, Map> storage) { - LinkedHashSet abbreviations = storage.get(actionId); - if (abbreviations == null) { - abbreviations = new LinkedHashSet<>(1); - storage.put(actionId, abbreviations); - } - abbreviations.add(abbreviation); + storage.computeIfAbsent(actionId, k -> new LinkedHashSet<>(1)).add(abbreviation); } public void register(String abbreviation, String actionId, boolean fromPluginXml) { @@ -152,11 +135,7 @@ public class AbbreviationManagerImpl extends AbbreviationManager implements Pers register(abbreviation, actionId, myPluginsActionId2Abbreviations); } - List ids = myAbbreviation2ActionId.get(abbreviation); - if (ids == null) { - ids = new ArrayList<>(0); - myAbbreviation2ActionId.put(abbreviation, ids); - } + List ids = myAbbreviation2ActionId.computeIfAbsent(abbreviation, k -> new ArrayList<>(0)); if (!ids.contains(actionId)) { ids.add(actionId); diff --git a/platform/platform-impl/src/com/intellij/openapi/actionSystem/impl/ActionManagerImpl.java b/platform/platform-impl/src/com/intellij/openapi/actionSystem/impl/ActionManagerImpl.java index a808aa537ad9..405207bf8871 100644 --- a/platform/platform-impl/src/com/intellij/openapi/actionSystem/impl/ActionManagerImpl.java +++ b/platform/platform-impl/src/com/intellij/openapi/actionSystem/impl/ActionManagerImpl.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2015 JetBrains s.r.o. + * Copyright 2000-2016 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -993,12 +993,7 @@ public final class ActionManagerImpl extends ActionManagerEx implements Disposab myId2Index.put(actionId, myRegisteredActionsCount++); myAction2Id.put(action, actionId); if (pluginId != null && !(action instanceof ActionGroup)){ - THashSet pluginActionIds = myPlugin2Id.get(pluginId); - if (pluginActionIds == null){ - pluginActionIds = new THashSet<>(); - myPlugin2Id.put(pluginId, pluginActionIds); - } - pluginActionIds.add(actionId); + myPlugin2Id.computeIfAbsent(pluginId, k -> new THashSet<>()).add(actionId); } action.registerCustomShortcutSet(new ProxyShortcutSet(actionId, myKeymapManager), null); } diff --git a/platform/platform-tests/testSrc/com/intellij/ide/util/treeView/AbstractTreeBuilderTest.java b/platform/platform-tests/testSrc/com/intellij/ide/util/treeView/AbstractTreeBuilderTest.java index 0c0c59c9fa6c..4632d3812560 100644 --- a/platform/platform-tests/testSrc/com/intellij/ide/util/treeView/AbstractTreeBuilderTest.java +++ b/platform/platform-tests/testSrc/com/intellij/ide/util/treeView/AbstractTreeBuilderTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2012 JetBrains s.r.o. + * Copyright 2000-2016 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,7 +17,6 @@ package com.intellij.ide.util.treeView; import com.intellij.ide.projectView.PresentationData; import com.intellij.openapi.util.AsyncResult; -import com.intellij.openapi.util.Condition; import com.intellij.testFramework.PlatformTestUtil; import com.intellij.ui.SimpleTextAttributes; import com.intellij.ui.treeStructure.Tree; @@ -526,12 +525,7 @@ abstract class AbstractTreeBuilderTest extends BaseTreeTestCase new ElementEntry(element)).onElementAction(action); if (myElementUpdateHook != null) { myElementUpdateHook.onElementAction(action, element); diff --git a/platform/vcs-impl/src/com/intellij/openapi/vcs/update/AbstractCommonUpdateAction.java b/platform/vcs-impl/src/com/intellij/openapi/vcs/update/AbstractCommonUpdateAction.java index fffac47e23c9..697265d66e40 100644 --- a/platform/vcs-impl/src/com/intellij/openapi/vcs/update/AbstractCommonUpdateAction.java +++ b/platform/vcs-impl/src/com/intellij/openapi/vcs/update/AbstractCommonUpdateAction.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2014 JetBrains s.r.o. + * Copyright 2000-2016 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -394,12 +394,7 @@ public abstract class AbstractCommonUpdateAction extends AbstractVcsAction { private void putExceptions(final HotfixData key, @NotNull final List list) { if (list.isEmpty()) return; - List exceptionList = myGroupedExceptions.get(key); - if (exceptionList == null) { - exceptionList = new ArrayList<>(); - myGroupedExceptions.put(key, exceptionList); - } - exceptionList.addAll(list); + myGroupedExceptions.computeIfAbsent(key, k -> new ArrayList<>()).addAll(list); } private void doVfsRefresh() { diff --git a/plugins/xpath/xpath-lang/src/org/intellij/lang/xpath/context/functions/AbstractFunctionContext.java b/plugins/xpath/xpath-lang/src/org/intellij/lang/xpath/context/functions/AbstractFunctionContext.java index 96b297f7c27d..10d2ae39bb6d 100644 --- a/plugins/xpath/xpath-lang/src/org/intellij/lang/xpath/context/functions/AbstractFunctionContext.java +++ b/plugins/xpath/xpath-lang/src/org/intellij/lang/xpath/context/functions/AbstractFunctionContext.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2011 JetBrains s.r.o. + * Copyright 2000-2016 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -72,12 +72,7 @@ public abstract class AbstractFunctionContext implements FunctionContext { } protected static synchronized FunctionContext getInstance(ContextType contextType, Factory factory) { - FunctionContext context = ourInstances.get(contextType); - if (context == null) { - context = factory.create(); - ourInstances.put(contextType, context); - } - return context; + return ourInstances.computeIfAbsent(contextType, k -> factory.create()); } public Map, Function> getFunctions() {