From 568378b22d7ff8d5b576a6ab6f9ffbf98e4a658a Mon Sep 17 00:00:00 2001 From: Dmitry Avdeev Date: Fri, 25 May 2012 17:04:05 +0400 Subject: [PATCH] IDEA-83955 Latest 11.1 build keeps displaying out of memory dialog no matter how much max heap size is allowed --- .../lang/properties/xml/SoftLazyValue.java | 41 +++++++++++++++++++ .../properties/xml/XmlPropertiesFile.java | 36 +++++++++------- 2 files changed, 63 insertions(+), 14 deletions(-) create mode 100644 plugins/properties/src/com/intellij/lang/properties/xml/SoftLazyValue.java diff --git a/plugins/properties/src/com/intellij/lang/properties/xml/SoftLazyValue.java b/plugins/properties/src/com/intellij/lang/properties/xml/SoftLazyValue.java new file mode 100644 index 000000000000..237e4276e818 --- /dev/null +++ b/plugins/properties/src/com/intellij/lang/properties/xml/SoftLazyValue.java @@ -0,0 +1,41 @@ +/* + * Copyright 2000-2012 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.intellij.lang.properties.xml; + +import org.jetbrains.annotations.NotNull; + +import java.lang.ref.SoftReference; + +/** + * @author Dmitry Avdeev + * Date: 5/25/12 + */ +public abstract class SoftLazyValue { + + private SoftReference myReference; + + public T getValue() { + T t; + if (myReference == null || (t = myReference.get()) == null) { + t = compute(); + myReference = new SoftReference(t); + } + return t; + } + + @NotNull + protected abstract T compute(); +} diff --git a/plugins/properties/src/com/intellij/lang/properties/xml/XmlPropertiesFile.java b/plugins/properties/src/com/intellij/lang/properties/xml/XmlPropertiesFile.java index 14ac2f4629ea..91fb1fe96673 100644 --- a/plugins/properties/src/com/intellij/lang/properties/xml/XmlPropertiesFile.java +++ b/plugins/properties/src/com/intellij/lang/properties/xml/XmlPropertiesFile.java @@ -33,8 +33,25 @@ public class XmlPropertiesFile implements PropertiesFile { private static final Key> KEY = Key.create("xml properties file"); private final XmlFile myFile; - private final List myProperties = new ArrayList(); - private final MultiMap myPropertiesMap = new MultiMap(); + private final SoftLazyValue> myPropertiesMap = new SoftLazyValue>() { + @NotNull + @Override + protected MultiMap compute() { + XmlTag rootTag = myFile.getRootTag(); + if (rootTag == null) { + return MultiMap.emptyInstance(); + } + + XmlTag[] entries = rootTag.findSubTags("entry"); + MultiMap map = new MultiMap(); + + for (XmlTag entry : entries) { + XmlProperty property = new XmlProperty(entry, XmlPropertiesFile.this); + map.putValue(property.getKey(), property); + } + return map; + } + }; @Nullable public static PropertiesFile getPropertiesFile(final PsiFile file) { @@ -55,15 +72,6 @@ public class XmlPropertiesFile implements PropertiesFile { private XmlPropertiesFile(XmlFile file) { myFile = file; - XmlTag rootTag = file.getRootTag(); - if (rootTag != null) { - XmlTag[] entries = rootTag.findSubTags("entry"); - for (XmlTag entry : entries) { - XmlProperty property = new XmlProperty(entry, this); - myProperties.add(property); - myPropertiesMap.putValue(property.getKey(), property); - } - } } @NotNull @@ -75,19 +83,19 @@ public class XmlPropertiesFile implements PropertiesFile { @NotNull @Override public List getProperties() { - return myProperties; + return new ArrayList(myPropertiesMap.getValue().values()); } @Override public IProperty findPropertyByKey(@NotNull @NonNls String key) { - Collection properties = myPropertiesMap.get(key); + Collection properties = myPropertiesMap.getValue().get(key); return properties.isEmpty() ? null : properties.iterator().next(); } @NotNull @Override public List findPropertiesByKey(@NotNull @NonNls String key) { - return new ArrayList(myPropertiesMap.get(key)); + return new ArrayList(myPropertiesMap.getValue().get(key)); } @NotNull