From 87c1341fe968238dfc705ed352e0d3fcc72133aa Mon Sep 17 00:00:00 2001 From: Vladimir Krivosheev Date: Wed, 2 May 2018 12:36:38 +0200 Subject: [PATCH] =?UTF-8?q?cleanup=20=E2=80=94=20use=20ClearableLazyValue?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../openapi/wm/impl/DesktopLayout.java | 96 +++++++++---------- 1 file changed, 45 insertions(+), 51 deletions(-) diff --git a/platform/platform-impl/src/com/intellij/openapi/wm/impl/DesktopLayout.java b/platform/platform-impl/src/com/intellij/openapi/wm/impl/DesktopLayout.java index 88977280ff67..34b0259dcdb1 100644 --- a/platform/platform-impl/src/com/intellij/openapi/wm/impl/DesktopLayout.java +++ b/platform/platform-impl/src/com/intellij/openapi/wm/impl/DesktopLayout.java @@ -4,6 +4,7 @@ package com.intellij.openapi.wm.impl; import com.intellij.configurationStore.XmlSerializer; import com.intellij.ide.ui.UISettings; import com.intellij.openapi.diagnostic.Logger; +import com.intellij.openapi.util.ClearableLazyValue; import com.intellij.openapi.util.JDOMUtil; import com.intellij.openapi.wm.ToolWindow; import com.intellij.openapi.wm.ToolWindowAnchor; @@ -37,24 +38,36 @@ public final class DesktopLayout { * */ private static final MyWindowInfoComparator ourWindowInfoComparator = new MyWindowInfoComparator(); - /** - * Don't use this member directly. Get it only by {@code getInfos} method. - * It exists here only for optimization purposes. This member can be {@code null} - * if the cached data is invalid. - */ - private List myRegisteredInfos; - /** - * Don't use this member directly. Get it only by {@code getUnregisteredInfos} method. - * It exists here only for optimization purposes. This member can be {@code null} - * if the cached data is invalid. - */ - private List myUnregisteredInfos; - /** - * Don't use this member directly. Get it only by {@code getAllInfos} method. - * It exists here only for optimization purposes. This member can be {@code null} - * if the cached data is invalid. - */ - private List myAllInfos; + + private final ClearableLazyValue> myRegisteredInfos = new ClearableLazyValue>() { + @NotNull + @Override + protected List compute() { + if (myRegisteredId2Info.isEmpty()) { + return Collections.emptyList(); + } + return new ArrayList<>(myRegisteredId2Info.values()); + } + }; + + private final ClearableLazyValue> myUnregisteredInfos = new ClearableLazyValue>() { + @NotNull + @Override + protected List compute() { + if (myUnregisteredId2Info.isEmpty()) { + return Collections.emptyList(); + } + return new ArrayList<>(myUnregisteredId2Info.values()); + } + }; + + private final ClearableLazyValue> myAllInfos = new ClearableLazyValue>() { + @NotNull + @Override + protected List compute() { + return ContainerUtil.concat(getInfos(), getUnregisteredInfos()); + } + }; /** * Copies itself from the passed @@ -76,10 +89,7 @@ public final class DesktopLayout { info.copyFrom(info1); } } - // invalidate caches - myRegisteredInfos = null; - myUnregisteredInfos = null; - myAllInfos = null; + invalidateCaches(); // normalize orders normalizeOrder(getAllInfos(ToolWindowAnchor.TOP)); normalizeOrder(getAllInfos(ToolWindowAnchor.LEFT)); @@ -87,6 +97,12 @@ public final class DesktopLayout { normalizeOrder(getAllInfos(ToolWindowAnchor.RIGHT)); } + private void invalidateCaches() { + myRegisteredInfos.drop(); + myUnregisteredInfos.drop(); + myAllInfos.drop(); + } + /** * Creates or gets {@code WindowInfo} for the specified {@code id}. If tool * window is being registered first time the method uses {@code anchor}. @@ -108,20 +124,14 @@ public final class DesktopLayout { myUnregisteredId2Info.remove(id); } myRegisteredId2Info.put(id, info); - // invalidate caches - myRegisteredInfos = null; - myUnregisteredInfos = null; - myAllInfos = null; + invalidateCaches(); return info; } final void unregister(@NotNull String id) { final WindowInfoImpl info = myRegisteredId2Info.remove(id).copy(); myUnregisteredId2Info.put(id, info); - // invalidate caches - myRegisteredInfos = null; - myUnregisteredInfos = null; - myAllInfos = null; + invalidateCaches(); } /** @@ -152,13 +162,7 @@ public final class DesktopLayout { */ @NotNull final List getInfos() { - if (myRegisteredInfos == null) { - if (myRegisteredId2Info.isEmpty()) { - return Collections.emptyList(); - } - myRegisteredInfos = new ArrayList<>(myRegisteredId2Info.values()); - } - return myRegisteredInfos; + return myRegisteredInfos.getValue(); } /** @@ -166,13 +170,7 @@ public final class DesktopLayout { */ @NotNull private List getUnregisteredInfos() { - if (myUnregisteredInfos == null) { - if (myUnregisteredId2Info.isEmpty()) { - return Collections.emptyList(); - } - myUnregisteredInfos = new ArrayList<>(myUnregisteredId2Info.values()); - } - return myUnregisteredInfos; + return myUnregisteredInfos.getValue(); } /** @@ -180,10 +178,7 @@ public final class DesktopLayout { */ @NotNull private List getAllInfos() { - final List registeredInfos = getInfos(); - final List unregisteredInfos = getUnregisteredInfos(); - myAllInfos = ContainerUtil.concat(registeredInfos, unregisteredInfos); - return myAllInfos; + return myAllInfos.getValue(); } /** @@ -277,12 +272,11 @@ public final class DesktopLayout { } final void setSplitMode(@NotNull String id, boolean split) { - final WindowInfoImpl info = getInfo(id, true); - info.setSplit(split); + getInfo(id, true).setSplit(split); } public final void readExternal(@NotNull Element layoutElement) { - myUnregisteredInfos = null; + myUnregisteredInfos.drop(); for (Element e : layoutElement.getChildren(WindowInfoImpl.TAG)) { WindowInfoImpl info = XmlSerializer.deserialize(e, WindowInfoImpl.class); if (info.getId() == null) {