Skip to content

Commit

Permalink
Merge pull request #5 from FelixBaensch/HashMap-initialisations
Browse files Browse the repository at this point in the history
Rework of HashMap initialisations
  • Loading branch information
FelixBaensch authored May 2, 2023
2 parents e2edab2 + 5aae9bf commit 4252ec5
Show file tree
Hide file tree
Showing 14 changed files with 156 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import de.unijena.cheminf.mortar.gui.views.SettingsView;
import de.unijena.cheminf.mortar.message.Message;
import de.unijena.cheminf.mortar.model.fragmentation.algorithm.IMoleculeFragmenter;
import de.unijena.cheminf.mortar.model.util.CollectionUtil;
import javafx.beans.property.Property;
import javafx.scene.Scene;
import javafx.scene.control.Tab;
Expand Down Expand Up @@ -85,7 +86,7 @@ public class FragmentationSettingsViewController {
*/
public FragmentationSettingsViewController(Stage aStage, IMoleculeFragmenter[] anArrayOfFragmenters, String aSelectedFragmenterAlgorithmName){
this.mainStage = aStage;
this.recentProperties = new HashMap<>(anArrayOfFragmenters.length);
this.recentProperties = new HashMap<>(CollectionUtil.calculateInitialHashCollectionCapacity(anArrayOfFragmenters.length));
this.fragmenters = anArrayOfFragmenters;
this.selectedFragmenterName = aSelectedFragmenterAlgorithmName;
this.openFragmentationSettingsView();
Expand All @@ -111,7 +112,7 @@ private void openFragmentationSettingsView(){
//
this.addListener();
for (IMoleculeFragmenter tmpFragmenter : this.fragmenters) {
HashMap<String, Object> tmpRecentProperties = new HashMap<>(tmpFragmenter.settingsProperties().size());
HashMap<String, Object> tmpRecentProperties = new HashMap<>(CollectionUtil.calculateInitialHashCollectionCapacity(tmpFragmenter.settingsProperties().size()));
this.recentProperties.put(tmpFragmenter.getFragmentationAlgorithmName(), tmpRecentProperties);
Tab tmpTab = this.settingsView.addTab(this.fragmentationSettingsViewStage,
tmpFragmenter.getFragmentationAlgorithmName(), tmpFragmenter.settingsProperties(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import de.unijena.cheminf.mortar.model.data.FragmentDataModel;
import de.unijena.cheminf.mortar.model.data.MoleculeDataModel;
import de.unijena.cheminf.mortar.model.depict.DepictionUtil;
import de.unijena.cheminf.mortar.model.util.ListUtil;
import de.unijena.cheminf.mortar.model.util.CollectionUtil;

import javafx.beans.binding.Bindings;
import javafx.beans.value.ObservableValue;
Expand Down Expand Up @@ -269,7 +269,7 @@ private BarChart createHistogram(int aFragmentNumber, HistogramView aHistogramVi
ArrayList<String> tmpFullSmilesLength = new ArrayList<>();
String tmpSelectedData = (String) aHistogramView.getChooseDataComoBox().getValue();
if (tmpSelectedData.equals(Message.get("HistogramView.chooseDataComboBoxFragmentFrequency.text"))) {
ListUtil.sortGivenFragmentListByPropertyAndSortType(this.copyList, "absoluteFrequency", "ASCENDING");
CollectionUtil.sortGivenFragmentListByPropertyAndSortType(this.copyList, "absoluteFrequency", "ASCENDING");
for (MoleculeDataModel tmpMoleculeData : this.copyList) {
tmpFragmentData = (FragmentDataModel) tmpMoleculeData;
if (tmpFragmentData.getUniqueSmiles().length() > aSmilesLength) {
Expand All @@ -284,7 +284,7 @@ private BarChart createHistogram(int aFragmentNumber, HistogramView aHistogramVi
tmpFrequencyList.add(tmpFragmentData.getAbsoluteFrequency());
}
} else {
ListUtil.sortGivenFragmentListByPropertyAndSortType(this.copyList, "moleculeFrequency", "ASCENDING");
CollectionUtil.sortGivenFragmentListByPropertyAndSortType(this.copyList, "moleculeFrequency", "ASCENDING");
for (MoleculeDataModel tmpMoleculeData : this.copyList) {
tmpFragmentData = (FragmentDataModel) tmpMoleculeData;
if (tmpFragmentData.getUniqueSmiles().length() > aSmilesLength) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import de.unijena.cheminf.mortar.model.settings.SettingsContainer;
import de.unijena.cheminf.mortar.model.util.BasicDefinitions;
import de.unijena.cheminf.mortar.model.util.ChemUtil;
import de.unijena.cheminf.mortar.model.util.CollectionUtil;
import de.unijena.cheminf.mortar.model.util.FileUtil;
import de.unijena.cheminf.mortar.model.util.LogUtil;
import javafx.application.Platform;
Expand Down Expand Up @@ -239,7 +240,7 @@ public MainViewController(Stage aStage, MainView aMainView, String anAppDir) {
//</editor-fold>
this.isImportRunningProperty = new SimpleBooleanProperty(false);
this.isExportRunningProperty = new SimpleBooleanProperty(false);
this.mapOfFragmentDataModelLists = new HashMap<>(5);
this.mapOfFragmentDataModelLists = new HashMap<>(CollectionUtil.calculateInitialHashCollectionCapacity(5));
this.threadList = new CopyOnWriteArrayList();
this.addListener();
this.addFragmentationAlgorithmCheckMenuItems();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import de.unijena.cheminf.mortar.gui.views.SettingsView;
import de.unijena.cheminf.mortar.message.Message;
import de.unijena.cheminf.mortar.model.settings.SettingsContainer;
import de.unijena.cheminf.mortar.model.util.CollectionUtil;
import javafx.application.Platform;
import javafx.beans.property.Property;
import javafx.scene.Scene;
Expand Down Expand Up @@ -86,7 +87,7 @@ public SettingsViewController(Stage aStage, SettingsContainer aSettingsContainer
this.mainStage = aStage;
this.settingsContainer = aSettingsContainer;
this.recentSettingsContainer = aSettingsContainer;
this.recentProperties = new HashMap<>(this.settingsContainer.settingsProperties().size());
this.recentProperties = new HashMap<>(CollectionUtil.calculateInitialHashCollectionCapacity(this.settingsContainer.settingsProperties().size()));
this.showSettingsView();
}
//
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/de/unijena/cheminf/mortar/gui/util/GuiUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import de.unijena.cheminf.mortar.model.data.MoleculeDataModel;
import de.unijena.cheminf.mortar.model.depict.DepictionUtil;
import de.unijena.cheminf.mortar.model.settings.SettingsContainer;
import de.unijena.cheminf.mortar.model.util.ListUtil;
import de.unijena.cheminf.mortar.model.util.CollectionUtil;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonType;
Expand Down Expand Up @@ -212,7 +212,7 @@ public static void sortTableViewGlobally(SortEvent<TableView> anEvent, Paginatio
return;
String tmpSortProp = ((PropertyValueFactory)((TableColumn) anEvent.getSource().getSortOrder().get(0)).cellValueFactoryProperty().getValue()).getProperty().toString();
TableColumn.SortType tmpSortType = ((TableColumn) anEvent.getSource().getSortOrder().get(0)).getSortType();
ListUtil.sortGivenFragmentListByPropertyAndSortType(((IDataTableView)anEvent.getSource()).getItemsList(), tmpSortProp, tmpSortType.toString());
CollectionUtil.sortGivenFragmentListByPropertyAndSortType(((IDataTableView)anEvent.getSource()).getItemsList(), tmpSortProp, tmpSortType.toString());
int fromIndex = tmpPagination.getCurrentPageIndex() * tmpRowsPerPage;
int toIndex = Math.min(fromIndex + tmpRowsPerPage, ((IDataTableView)anEvent.getSource()).getItemsList().size());
anEvent.getSource().getItems().clear();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import de.unijena.cheminf.mortar.model.settings.SettingsContainer;
import de.unijena.cheminf.mortar.model.util.BasicDefinitions;
import de.unijena.cheminf.mortar.model.util.ChemUtil;
import de.unijena.cheminf.mortar.model.util.CollectionUtil;
import de.unijena.cheminf.mortar.model.util.FileUtil;
import de.unijena.cheminf.mortar.model.util.SimpleEnumConstantNameProperty;
import de.unijena.cheminf.mortar.preference.BooleanPreference;
Expand Down Expand Up @@ -1063,23 +1064,26 @@ private void updatePropertiesFromPreferences(List<Property> aPropertiesList, Pre
* anything does not meet the requirements.
*/
private void checkFragmenters() throws Exception {
HashSet<String> tmpAlgorithmNames = new HashSet<>(this.fragmenters.length + 6, 1.0f);
int tmpAlgorithmNamesSetInitCapacity = CollectionUtil.calculateInitialHashCollectionCapacity(this.fragmenters.length,
BasicDefinitions.DEFAULT_HASH_COLLECTION_LOAD_FACTOR);
HashSet<String> tmpAlgorithmNamesSet = new HashSet<>(tmpAlgorithmNamesSetInitCapacity, BasicDefinitions.DEFAULT_HASH_COLLECTION_LOAD_FACTOR);
for (IMoleculeFragmenter tmpFragmenter : this.fragmenters) {
//algorithm name should be singleton and must be persistable
String tmpAlgName = tmpFragmenter.getFragmentationAlgorithmName();
if (!PreferenceUtil.isValidName(tmpAlgName) || !SingleTermPreference.isValidContent(tmpAlgName)) {
throw new Exception("Algorithm name " + tmpAlgName + " is invalid.");
}
if (tmpAlgorithmNames.contains(tmpAlgName)) {
if (tmpAlgorithmNamesSet.contains(tmpAlgName)) {
throw new Exception("Algorithm name " + tmpAlgName + " is used multiple times.");
} else {
tmpAlgorithmNames.add(tmpAlgName);
tmpAlgorithmNamesSet.add(tmpAlgName);
}
//setting names must be singletons within the respective class
//setting names and values must adhere to the preference input restrictions
//setting values are only tested for their current state, not the entire possible input space! It is tested again at persistence
List<Property> tmpSettingsList = tmpFragmenter.settingsProperties();
HashSet<String> tmpSettingNames = new HashSet<>(tmpSettingsList.size() + 6, 1.0f);
int tmpSettingNamesSetInitCapacity = CollectionUtil.calculateInitialHashCollectionCapacity(tmpSettingsList.size(), BasicDefinitions.DEFAULT_HASH_COLLECTION_LOAD_FACTOR);
HashSet<String> tmpSettingNames = new HashSet<>(tmpSettingNamesSetInitCapacity, BasicDefinitions.DEFAULT_HASH_COLLECTION_LOAD_FACTOR);
for (Property tmpSetting : tmpSettingsList) {
if (!PreferenceUtil.isValidName(tmpSetting.getName())) {
throw new Exception("Setting " + tmpSetting.getName() + " has an invalid name.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import de.unijena.cheminf.mortar.model.data.MoleculeDataModel;
import de.unijena.cheminf.mortar.model.fragmentation.algorithm.IMoleculeFragmenter;
import de.unijena.cheminf.mortar.model.util.ChemUtil;
import de.unijena.cheminf.mortar.model.util.CollectionUtil;
import org.openscience.cdk.exception.CDKException;
import org.openscience.cdk.interfaces.IAtomContainer;

Expand Down Expand Up @@ -134,7 +135,7 @@ public Integer call() throws Exception{
continue;
}
List<FragmentDataModel> tmpFragmentsOfMolList = new ArrayList<>(tmpFragmentsList.size());
HashMap<String, Integer> tmpFragmentFrequenciesOfMoleculeMap = new HashMap<>(tmpFragmentsList.size());
HashMap<String, Integer> tmpFragmentFrequenciesOfMoleculeMap = new HashMap<>(CollectionUtil.calculateInitialHashCollectionCapacity(tmpFragmentsList.size()));
for(IAtomContainer tmpFragment : tmpFragmentsList){
String tmpSmiles = ChemUtil.createUniqueSmiles(tmpFragment);
if (tmpSmiles == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@
import de.unijena.cheminf.mortar.gui.util.GuiUtil;
import de.unijena.cheminf.mortar.message.Message;
import de.unijena.cheminf.mortar.model.io.Importer;
import de.unijena.cheminf.mortar.model.util.BasicDefinitions;
import de.unijena.cheminf.mortar.model.util.ChemUtil;
import de.unijena.cheminf.mortar.model.util.CollectionUtil;
import de.unijena.cheminf.mortar.model.util.SimpleEnumConstantNameProperty;
import javafx.beans.property.Property;
import javafx.beans.property.SimpleBooleanProperty;
Expand Down Expand Up @@ -338,7 +340,11 @@ public static enum CycleFinderOption {
* Constructor, all settings are initialised with their default values as declared in the respective public constants.
*/
public ErtlFunctionalGroupsFinderFragmenter() {
this.settingNameTooltipTextMap = new HashMap(10, 0.9f);
int tmpNumberOfSettingsForTooltipMapSize= 6;
int tmpInitialCapacityForSettingNameTooltipTextMap = CollectionUtil.calculateInitialHashCollectionCapacity(
tmpNumberOfSettingsForTooltipMapSize,
BasicDefinitions.DEFAULT_HASH_COLLECTION_LOAD_FACTOR);
this.settingNameTooltipTextMap = new HashMap(tmpInitialCapacityForSettingNameTooltipTextMap, BasicDefinitions.DEFAULT_HASH_COLLECTION_LOAD_FACTOR);
this.fragmentSaturationSetting = new SimpleEnumConstantNameProperty(this, "Fragment saturation setting",
IMoleculeFragmenter.FRAGMENT_SATURATION_OPTION_DEFAULT.name(), IMoleculeFragmenter.FragmentSaturationOption.class) {
@Override
Expand Down Expand Up @@ -805,7 +811,8 @@ public List<IAtomContainer> fragmentMolecule(IAtomContainer aMolecule)
this.logger.log(Level.WARNING, anException.toString(), anException);
throw new IllegalArgumentException("Unexpected error at aromaticity detection: " + anException.toString());
}
HashMap<Integer, IAtom> tmpIdToAtomMap = new HashMap<>(tmpMoleculeClone.getAtomCount() + 1, 1);
int tmpInitialCapacityForIdToAtomMap = CollectionUtil.calculateInitialHashCollectionCapacity(tmpMoleculeClone.getAtomCount(), BasicDefinitions.DEFAULT_HASH_COLLECTION_LOAD_FACTOR);
HashMap<Integer, IAtom> tmpIdToAtomMap = new HashMap<>(tmpInitialCapacityForIdToAtomMap, BasicDefinitions.DEFAULT_HASH_COLLECTION_LOAD_FACTOR);
for (int i = 0; i < tmpMoleculeClone.getAtomCount(); i++) {
IAtom tmpAtom = tmpMoleculeClone.getAtom(i);
tmpAtom.setProperty(ErtlFunctionalGroupsFinderFragmenter.INTERNAL_INDEX_PROPERTY_KEY, i);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

import de.unijena.cheminf.mortar.gui.util.GuiUtil;
import de.unijena.cheminf.mortar.message.Message;
import de.unijena.cheminf.mortar.model.util.BasicDefinitions;
import de.unijena.cheminf.mortar.model.util.CollectionUtil;
import de.unijena.cheminf.mortar.model.util.SimpleEnumConstantNameProperty;
import de.unijena.cheminf.scaffoldGenerator.ScaffoldGenerator;
import javafx.beans.property.Property;
Expand Down Expand Up @@ -340,7 +342,11 @@ public static enum FragmentationTypeOption {
*/
public ScaffoldGeneratorFragmenter() {
this.scaffoldGeneratorInstance = new ScaffoldGenerator();
this.settingNameTooltipTextMap = new HashMap(16, 0.9f);
int tmpNumberOfSettingsForTooltipMapSize= 10;
int tmpInitialCapacityForSettingNameTooltipTextMap = CollectionUtil.calculateInitialHashCollectionCapacity(
tmpNumberOfSettingsForTooltipMapSize,
BasicDefinitions.DEFAULT_HASH_COLLECTION_LOAD_FACTOR);
this.settingNameTooltipTextMap = new HashMap(tmpInitialCapacityForSettingNameTooltipTextMap, BasicDefinitions.DEFAULT_HASH_COLLECTION_LOAD_FACTOR);
this.fragmentSaturationSetting = new SimpleEnumConstantNameProperty(this, "Fragment saturation setting",
IMoleculeFragmenter.FRAGMENT_SATURATION_OPTION_DEFAULT.name(), IMoleculeFragmenter.FragmentSaturationOption.class) {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@
import de.unijena.cheminf.deglycosylation.SugarRemovalUtility;
import de.unijena.cheminf.mortar.gui.util.GuiUtil;
import de.unijena.cheminf.mortar.message.Message;
import de.unijena.cheminf.mortar.model.util.BasicDefinitions;
import de.unijena.cheminf.mortar.model.util.ChemUtil;
import de.unijena.cheminf.mortar.model.util.CollectionUtil;
import de.unijena.cheminf.mortar.model.util.SimpleEnumConstantNameProperty;
import javafx.beans.property.Property;
import javafx.beans.property.SimpleBooleanProperty;
Expand Down Expand Up @@ -234,8 +236,12 @@ public static enum SRUFragmenterReturnedFragmentsOption {
*/
public SugarRemovalUtilityFragmenter() {
this.sugarRUInstance = new SugarRemovalUtility(SilentChemObjectBuilder.getInstance());
this.settings = new ArrayList<>(15);
this.settingNameTooltipTextMap = new HashMap<>(20, 0.9f);
int tmpNumberOfSettings = 15;
this.settings = new ArrayList<>(tmpNumberOfSettings);
int tmpInitialCapacityForSettingNameTooltipTextMap = CollectionUtil.calculateInitialHashCollectionCapacity(
tmpNumberOfSettings,
BasicDefinitions.DEFAULT_HASH_COLLECTION_LOAD_FACTOR);
this.settingNameTooltipTextMap = new HashMap<>(tmpInitialCapacityForSettingNameTooltipTextMap, BasicDefinitions.DEFAULT_HASH_COLLECTION_LOAD_FACTOR);
this.returnedFragmentsSetting = new SimpleEnumConstantNameProperty(this, "Returned fragments setting",
SugarRemovalUtilityFragmenter.RETURNED_FRAGMENTS_OPTION_DEFAULT.name(), SugarRemovalUtilityFragmenter.SRUFragmenterReturnedFragmentsOption.class) {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import de.unijena.cheminf.mortar.gui.util.GuiUtil;
import de.unijena.cheminf.mortar.message.Message;
import de.unijena.cheminf.mortar.model.util.BasicDefinitions;
import de.unijena.cheminf.mortar.model.util.CollectionUtil;
import de.unijena.cheminf.mortar.model.util.FileUtil;
import de.unijena.cheminf.mortar.model.util.SimpleEnumConstantNameProperty;
import de.unijena.cheminf.mortar.preference.BooleanPreference;
Expand Down Expand Up @@ -609,7 +610,11 @@ public void reloadGlobalSettings() {
* to the list of settings for display to the user.
*/
private void initialiseSettings() {
this.settingNameTooltipTextMap = new HashMap<String, String>(10, 0.9f);
int tmpNumberOfSettings = 8;
int tmpInitialCapacityForSettingNameTooltipTextMap = CollectionUtil.calculateInitialHashCollectionCapacity(
tmpNumberOfSettings,
BasicDefinitions.DEFAULT_HASH_COLLECTION_LOAD_FACTOR);
this.settingNameTooltipTextMap = new HashMap<String, String>(tmpInitialCapacityForSettingNameTooltipTextMap, BasicDefinitions.DEFAULT_HASH_COLLECTION_LOAD_FACTOR);
this.rowsPerPageSetting = new SimpleIntegerProperty(this,
"Rows per page setting",
SettingsContainer.ROWS_PER_PAGE_SETTING_DEFAULT) {
Expand Down Expand Up @@ -752,15 +757,16 @@ private void checkSettings() throws Exception {
//setting names and values must adhere to the preference input restrictions
//setting values are only tested for their current state, not the entire possible input space! It is tested again at persistence
List<Property> tmpSettingsList = this.settings;
HashSet<String> tmpSettingNames = new HashSet<>(tmpSettingsList.size() + 6, 1.0f);
int tmpSettingNamesSetInitCapacity = CollectionUtil.calculateInitialHashCollectionCapacity(tmpSettingsList.size(), BasicDefinitions.DEFAULT_HASH_COLLECTION_LOAD_FACTOR);
HashSet<String> tmpSettingNamesSet = new HashSet<>(tmpSettingNamesSetInitCapacity, BasicDefinitions.DEFAULT_HASH_COLLECTION_LOAD_FACTOR);
for (Property tmpSetting : tmpSettingsList) {
if (!PreferenceUtil.isValidName(tmpSetting.getName())) {
throw new Exception("Setting " + tmpSetting.getName() + " has an invalid name.");
}
if (tmpSettingNames.contains(tmpSetting.getName())) {
if (tmpSettingNamesSet.contains(tmpSetting.getName())) {
throw new Exception("Setting name " + tmpSetting.getName() + " is used multiple times.");
} else {
tmpSettingNames.add(tmpSetting.getName());
tmpSettingNamesSet.add(tmpSetting.getName());
}
if (tmpSetting instanceof SimpleBooleanProperty) {
//nothing to do here, booleans cannot have invalid values
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,5 +186,9 @@ public final class BasicDefinitions {
* Default distance between image and text
*/
public static final int DEFAULT_IMAGE_TEXT_DISTANCE = 15;
/**
* Default load factor for HashMap and HashSet instances, defined based on default value given in the Java documentation.
*/
public static final float DEFAULT_HASH_COLLECTION_LOAD_FACTOR = 0.75f;
//</editor-fold>
}
Loading

0 comments on commit 4252ec5

Please sign in to comment.