1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-07-30 04:15:35 +02:00

Using of pooled list refined

This commit is contained in:
Andrew Gvozdev 2011-11-17 16:49:35 -05:00
parent d0407361a8
commit e95e3a02b7
4 changed files with 178 additions and 118 deletions

View file

@ -1,12 +1,12 @@
/******************************************************************************* /*******************************************************************************
* Copyright (c) 2009, 2010 Andrew Gvozdev (Quoin Inc.) and others. * Copyright (c) 2009, 2011 Andrew Gvozdev and others.
* All rights reserved. This program and the accompanying materials * All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0 * are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at * which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html * http://www.eclipse.org/legal/epl-v10.html
* *
* Contributors: * Contributors:
* Andrew Gvozdev (Quoin Inc.) - initial API and implementation * Andrew Gvozdev - initial API and implementation
*******************************************************************************/ *******************************************************************************/
package org.eclipse.cdt.core.language.settings.providers; package org.eclipse.cdt.core.language.settings.providers;
@ -18,7 +18,6 @@ import java.util.List;
import org.eclipse.cdt.core.AbstractExecutableExtensionBase; import org.eclipse.cdt.core.AbstractExecutableExtensionBase;
import org.eclipse.cdt.core.settings.model.ICConfigurationDescription; import org.eclipse.cdt.core.settings.model.ICConfigurationDescription;
import org.eclipse.cdt.core.settings.model.ICLanguageSettingEntry; import org.eclipse.cdt.core.settings.model.ICLanguageSettingEntry;
import org.eclipse.cdt.internal.core.settings.model.SettingsModelMessages;
import org.eclipse.core.resources.IResource; import org.eclipse.core.resources.IResource;
/** /**
@ -131,6 +130,7 @@ public class LanguageSettingsBaseProvider extends AbstractExecutableExtensionBas
* @param languageId - language id. If {@code null}, then entries defined for * @param languageId - language id. If {@code null}, then entries defined for
* the language scope are returned. See {@link #getLanguageScope()} * the language scope are returned. See {@link #getLanguageScope()}
*/ */
@Override
public List<ICLanguageSettingEntry> getSettingEntries(ICConfigurationDescription cfgDescription, IResource rc, String languageId) { public List<ICLanguageSettingEntry> getSettingEntries(ICConfigurationDescription cfgDescription, IResource rc, String languageId) {
if (languageScope==null) { if (languageScope==null) {
if (entries==null) if (entries==null)

View file

@ -1025,17 +1025,17 @@ projects:
} }
} }
private static List<ICLanguageSettingEntry> safeGetSettingEntries(ILanguageSettingsProvider provider, private static List<ICLanguageSettingEntry> getSettingEntriesPooled(ILanguageSettingsProvider provider,
ICConfigurationDescription cfgDescription, IResource rc, String languageId) { ICConfigurationDescription cfgDescription, IResource rc, String languageId) {
try { try {
return provider.getSettingEntries(cfgDescription, rc, languageId); return LanguageSettingsStorage.getPooledList(provider.getSettingEntries(cfgDescription, rc, languageId));
} catch (Throwable e) { } catch (Throwable e) {
String cfgId = cfgDescription!=null ? cfgDescription.getId() : null; String cfgId = cfgDescription!=null ? cfgDescription.getId() : null;
String msg = "Exception in provider "+provider.getId()+": getSettingEntries("+cfgId+", "+rc+", "+languageId+")"; String msg = "Exception in provider "+provider.getId()+": getSettingEntries("+cfgId+", "+rc+", "+languageId+")";
CCorePlugin.log(msg, e); CCorePlugin.log(msg, e);
// return empty array to prevent climbing up the resource tree // return empty list to prevent getting potentially non-empty list from up the resource tree
return new ArrayList<ICLanguageSettingEntry>(0); return LanguageSettingsStorage.getPooledEmptyList();
} }
} }
@ -1050,15 +1050,15 @@ projects:
* @param rc - resource such as file or folder. * @param rc - resource such as file or folder.
* @param languageId - language id. * @param languageId - language id.
* *
* @return the list of setting entries. Never returns {@code null} * @return the list of setting entries which is unmodifiable. Never returns {@code null}
* although individual providers mandated to return {@code null} if no settings defined. * although individual providers mandated to return {@code null} if no settings defined.
*/ */
public static List<ICLanguageSettingEntry> getSettingEntriesUpResourceTree(ILanguageSettingsProvider provider, ICConfigurationDescription cfgDescription, IResource rc, String languageId) { public static List<ICLanguageSettingEntry> getSettingEntriesUpResourceTree(ILanguageSettingsProvider provider, ICConfigurationDescription cfgDescription, IResource rc, String languageId) {
Assert.isTrue( !(rc instanceof IWorkspaceRoot) ); Assert.isTrue( !(rc instanceof IWorkspaceRoot) );
if (provider!=null) { if (provider!=null) {
List<ICLanguageSettingEntry> entries = safeGetSettingEntries(provider, cfgDescription, rc, languageId); List<ICLanguageSettingEntry> entries = getSettingEntriesPooled(provider, cfgDescription, rc, languageId);
if (entries!=null) { if (entries!=null) {
return new ArrayList<ICLanguageSettingEntry>(entries); return entries;
} }
if (rc!=null) { if (rc!=null) {
IResource parentFolder = (rc instanceof IProject) ? null : rc.getParent(); IResource parentFolder = (rc instanceof IProject) ? null : rc.getParent();
@ -1066,14 +1066,14 @@ projects:
return getSettingEntriesUpResourceTree(provider, cfgDescription, parentFolder, languageId); return getSettingEntriesUpResourceTree(provider, cfgDescription, parentFolder, languageId);
} }
// if out of parent resources - get default entries for the applicable language scope // if out of parent resources - get default entries for the applicable language scope
entries = safeGetSettingEntries(provider, null, null, languageId); entries = getSettingEntriesPooled(provider, null, null, languageId);
if (entries!=null) { if (entries!=null) {
return new ArrayList<ICLanguageSettingEntry>(entries); return entries;
} }
} }
} }
return new ArrayList<ICLanguageSettingEntry>(0); return LanguageSettingsStorage.getPooledEmptyList();
} }
/** /**

View file

@ -55,8 +55,8 @@ public class LanguageSettingsStorage {
}; };
/** /**
* <br> Note that this list is <b>unmodifiable</b>. To modify the list copy it, change and use * TODO
* {@link #setSettingEntries(ICConfigurationDescription, IResource, String, List)}. * <br> Note that this list is <b>unmodifiable</b>.
* *
*/ */
public List<ICLanguageSettingEntry> getSettingEntries(ICConfigurationDescription cfgDescription, IResource rc, String languageId) { public List<ICLanguageSettingEntry> getSettingEntries(ICConfigurationDescription cfgDescription, IResource rc, String languageId) {
@ -108,7 +108,7 @@ public class LanguageSettingsStorage {
langMap = new HashMap<String, List<ICLanguageSettingEntry>>(); langMap = new HashMap<String, List<ICLanguageSettingEntry>>();
fStorage.put(languageId, langMap); fStorage.put(languageId, langMap);
} }
List<ICLanguageSettingEntry> sortedEntries = listPool.add(Collections.unmodifiableList(sortEntries(entries))); List<ICLanguageSettingEntry> sortedEntries = getPooledList(sortEntries(entries), false);
langMap.put(rcProjectPath, sortedEntries); langMap.put(rcProjectPath, sortedEntries);
} else { } else {
// do not keep nulls in the tables // do not keep nulls in the tables
@ -319,6 +319,53 @@ public class LanguageSettingsStorage {
} }
} }
/**
* Returns the equal list of entries from the pool to conserve the memory.
*
* @param entries - list of entries to pool.
* @param copy - specify {@code true} to copy the list in order to prevent
* back-door modification on the original list changes.
* @return returns the list of entries from the pool.
*/
private static List<ICLanguageSettingEntry> getPooledList(List<ICLanguageSettingEntry> entries, boolean copy) {
if (entries == null)
return null;
List<ICLanguageSettingEntry> pooledList = listPool.get(entries);
if (pooledList != null) {
return pooledList;
}
if (entries.size() == 0) {
return getPooledEmptyList();
}
if (copy) {
entries = new ArrayList<ICLanguageSettingEntry>(entries);
}
pooledList = Collections.unmodifiableList(entries);
return listPool.add(pooledList);
}
/**
* Returns the equal list of entries from the pool to conserve the memory.
*
* @param entries - list of entries to pool.
* @return returns the list of entries from the pool.
*/
public static List<ICLanguageSettingEntry> getPooledList(List<ICLanguageSettingEntry> entries) {
return getPooledList(entries, true);
}
/**
* @return the empty immutable list which is pooled. Use this call rather than creating
* new empty array to ensure that operator '==' can be used instead of deep equals().
*/
public static List<ICLanguageSettingEntry> getPooledEmptyList() {
List<ICLanguageSettingEntry> pooledEmptyList = Collections.emptyList();
return listPool.add(pooledEmptyList);
}
/** /**
* Clone storage for the entries. Copies references for lists of entries as a whole. * Clone storage for the entries. Copies references for lists of entries as a whole.
* Note that is OK as the lists kept in storage are unmodifiable. * Note that is OK as the lists kept in storage are unmodifiable.

View file

@ -170,16 +170,25 @@ public class LanguageSettingsEntriesTab extends AbstractCPropertyTab {
* Content provider for setting entries tree. * Content provider for setting entries tree.
*/ */
private class EntriesTreeContentProvider implements ITreeContentProvider { private class EntriesTreeContentProvider implements ITreeContentProvider {
@Override
public Object[] getElements(Object inputElement) { public Object[] getElements(Object inputElement) {
return getChildren(inputElement); return getChildren(inputElement);
} }
@Override
public Object[] getChildren(Object parentElement) { public Object[] getChildren(Object parentElement) {
if (parentElement instanceof Object[]) if (parentElement instanceof Object[])
return (Object[]) parentElement; return (Object[]) parentElement;
if (parentElement instanceof ILanguageSettingsProvider) { if (parentElement instanceof ILanguageSettingsProvider) {
ILanguageSettingsProvider lsProvider = (ILanguageSettingsProvider)parentElement; ILanguageSettingsProvider lsProvider = (ILanguageSettingsProvider)parentElement;
List<ICLanguageSettingEntry> entriesList = getSettingEntriesUpResourceTree(lsProvider); List<ICLanguageSettingEntry> entriesList = getSettingEntriesUpResourceTree(lsProvider);
if (entriesList == null) {
return null;
}
// convert to modifiable list
entriesList = new ArrayList<ICLanguageSettingEntry>(entriesList);
if (builtInCheckBox.getSelection()==false) { if (builtInCheckBox.getSelection()==false) {
for (Iterator<ICLanguageSettingEntry> iter = entriesList.iterator(); iter.hasNext();) { for (Iterator<ICLanguageSettingEntry> iter = entriesList.iterator(); iter.hasNext();) {
@ -189,26 +198,28 @@ public class LanguageSettingsEntriesTab extends AbstractCPropertyTab {
} }
} }
} }
return entriesList.toArray();
if (entriesList!=null) {
return entriesList.toArray();
}
} }
return null; return null;
} }
@Override
public Object getParent(Object element) { public Object getParent(Object element) {
return null; return null;
} }
@Override
public boolean hasChildren(Object element) { public boolean hasChildren(Object element) {
Object[] children = getChildren(element); Object[] children = getChildren(element);
return children!=null && children.length>0; return children!=null && children.length>0;
} }
@Override
public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
} }
@Override
public void dispose() { public void dispose() {
} }
@ -323,6 +334,7 @@ public class LanguageSettingsEntriesTab extends AbstractCPropertyTab {
columnLanguages.setToolTipText(Messages.AbstractLangsListTab_Languages); columnLanguages.setToolTipText(Messages.AbstractLangsListTab_Languages);
treeLanguages.addPaintListener(new PaintListener() { treeLanguages.addPaintListener(new PaintListener() {
@Override
public void paintControl(PaintEvent e) { public void paintControl(PaintEvent e) {
int x = treeLanguages.getBounds().width - 5; int x = treeLanguages.getBounds().width - 5;
if (columnLanguages.getWidth() != x) if (columnLanguages.getWidth() != x)
@ -340,6 +352,7 @@ public class LanguageSettingsEntriesTab extends AbstractCPropertyTab {
final TreeColumn treeCol = new TreeColumn(treeEntries, SWT.NONE); final TreeColumn treeCol = new TreeColumn(treeEntries, SWT.NONE);
treeEntries.addPaintListener(new PaintListener() { treeEntries.addPaintListener(new PaintListener() {
@Override
public void paintControl(PaintEvent e) { public void paintControl(PaintEvent e) {
int x = treeEntries.getClientArea().width; int x = treeEntries.getClientArea().width;
if (treeCol.getWidth() != x) if (treeCol.getWidth() != x)
@ -692,7 +705,7 @@ public class LanguageSettingsEntriesTab extends AbstractCPropertyTab {
ICConfigurationDescription cfgDescription = getConfigurationDescription(); ICConfigurationDescription cfgDescription = getConfigurationDescription();
IResource rc = getResource(); IResource rc = getResource();
List<ICLanguageSettingEntry> entries = provider.getSettingEntries(cfgDescription, rc, currentLanguageId); List<ICLanguageSettingEntry> entries = provider.getSettingEntries(cfgDescription, rc, currentLanguageId);
if (entries==null) { if (entries == null) {
entries = getSettingEntriesUpResourceTree(provider); entries = getSettingEntriesUpResourceTree(provider);
} }
entries = new ArrayList<ICLanguageSettingEntry>(entries); entries = new ArrayList<ICLanguageSettingEntry>(entries);
@ -770,7 +783,7 @@ public class LanguageSettingsEntriesTab extends AbstractCPropertyTab {
String providerId = provider.getId(); String providerId = provider.getId();
List<ICLanguageSettingEntry> entries = getWritableEntries(provider); List<ICLanguageSettingEntry> entries = getWritableEntries(provider);
int pos = getExactIndex(getSettingEntriesUpResourceTree(provider), entry); int pos = getExactIndex(entries, entry);
entries.remove(entry); entries.remove(entry);
saveEntries(provider, entries); saveEntries(provider, entries);