Skip to content

Commit

Permalink
Replace usage of Maps.newHashMapWithExpectedSize() with standard Java
Browse files Browse the repository at this point in the history
In Java-19 there is even static factory method HashMap.newHashMap(int)
that can be used as a drop-in replacement once Java-19 or later is
required.
For now I calculate the necessary capacity explicitly using the formula:
expected-size *4/3+1

Part of eclipse#2975
  • Loading branch information
HannesWell committed Apr 5, 2024
1 parent 77020bb commit 4394639
Show file tree
Hide file tree
Showing 27 changed files with 757 additions and 767 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,14 @@
package org.eclipse.xtend.core.scoping;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.xtext.common.types.JvmType;
import org.eclipse.xtext.naming.QualifiedName;
import org.eclipse.xtext.resource.IEObjectDescription;

import com.google.common.collect.Maps;

/**
* @author Sebastian Zarnekow - Initial contribution and API
*/
Expand All @@ -28,7 +27,7 @@ public class CachingTypeScope extends AbstractScope {

public CachingTypeScope(AbstractScope parent) {
this.parent = parent;
this.cache = Maps.newHashMapWithExpectedSize(50);
this.cache = new HashMap<>(50 * 4 / 3 + 1);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
package org.eclipse.xtext.builder.clustering;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

Expand All @@ -26,7 +27,6 @@
import com.google.common.base.Function;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;
import com.google.common.collect.Maps;

/**
* Makes an eager copy of the exported objects of another resource description.
Expand Down Expand Up @@ -56,7 +56,7 @@ public IEObjectDescription apply(IEObjectDescription from) {
Map<String, String> userData = null;
for (final String key : from.getUserDataKeys()) {
if (userData == null) {
userData = Maps.newHashMapWithExpectedSize(2);
userData = new HashMap<>(3);
}
userData.put(key, from.getUserData(key));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*******************************************************************************/
package org.eclipse.xtext.common.types.access.binary.asm;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

Expand All @@ -25,8 +26,6 @@
import org.objectweb.asm.AnnotationVisitor;
import org.objectweb.asm.Opcodes;

import com.google.common.collect.Maps;

/**
* @author Sebastian Zarnekow - Initial contribution and API
*/
Expand Down Expand Up @@ -88,9 +87,8 @@ protected Map<String, JvmTypeParameter> createTypeParameters(AbstractBinarySigna

List<BinaryTypeParameter> typeParameters = signature.getTypeParameters();
if (typeParameters.size() != 0) {
Map<String, JvmTypeParameter> result = Maps
.newHashMapWithExpectedSize((knownTypeParameters != null ? knownTypeParameters.size() : 0)
+ typeParameters.size());
int expectedSize = (knownTypeParameters != null ? knownTypeParameters.size() : 0) + typeParameters.size();
Map<String, JvmTypeParameter> result = new HashMap<>(expectedSize * 4 / 3 + 1);
if (knownTypeParameters != null)
result.putAll(knownTypeParameters);
InternalEList<JvmTypeParameter> jvmTypeParameters = (InternalEList<JvmTypeParameter>) container
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@
import org.eclipse.xtext.common.types.impl.JvmTypeConstraintImplCustom;
import org.objectweb.asm.Type;

import com.google.common.collect.Maps;

/**
* @author Sebastian Zarnekow - Initial contribution and API
*/
Expand Down Expand Up @@ -326,9 +324,8 @@ public Map<String, JvmTypeParameter> createTypeParameters(AbstractBinarySignatur

List<BinaryTypeParameter> typeParameters = signature.getTypeParameters();
if (typeParameters.size() != 0) {
Map<String, JvmTypeParameter> result = Maps
.newHashMapWithExpectedSize((knownTypeParameters != null ? knownTypeParameters.size() : 0)
+ typeParameters.size());
int expectedSize = (knownTypeParameters != null ? knownTypeParameters.size() : 0) + typeParameters.size();
Map<String, JvmTypeParameter> result = new HashMap<>(expectedSize * 4 / 3 + 1);
if (knownTypeParameters != null)
result.putAll(knownTypeParameters);
InternalEList<JvmTypeParameter> jvmTypeParameters = (InternalEList<JvmTypeParameter>) container
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import org.eclipse.xtext.resource.impl.DefaultResourceDescriptionStrategy;
import org.eclipse.xtext.util.IAcceptor;

import com.google.common.collect.Maps;
import com.google.inject.Inject;
import com.google.inject.Singleton;

Expand Down Expand Up @@ -48,8 +47,7 @@ protected boolean createEObjectDescriptions(IQualifiedNameProvider qualifiedName
try {
QualifiedName qualifiedName = qualifiedNameProvider.getFullyQualifiedName(eObject);
if (qualifiedName != null) {
Map<String, String> userData = Maps.newHashMapWithExpectedSize(1);
userData.put(NS_URI_INDEX_ENTRY, Boolean.toString(isNsURI));
Map<String, String> userData = Map.of(NS_URI_INDEX_ENTRY, Boolean.toString(isNsURI));
IEObjectDescription description = EObjectDescription.create(qualifiedName, eObject, userData);
acceptor.accept(description);
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*******************************************************************************/
package org.eclipse.xtext.ide.editor.contentassist.antlr;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
Expand All @@ -28,7 +29,6 @@
import org.eclipse.xtext.UnorderedGroup;
import org.eclipse.xtext.util.XtextSwitch;

import com.google.common.collect.Maps;
import com.google.common.collect.Sets;

/**
Expand All @@ -37,7 +37,7 @@
public class CallHierarchyHelper extends XtextSwitch<Boolean> {
private final EObject nextGrammarElement;
private Set<AbstractRule> visiting = new HashSet<AbstractRule>();
private Map<AbstractRule, Boolean> visited = Maps.newHashMapWithExpectedSize(4);
private Map<AbstractRule, Boolean> visited = new HashMap<>(4 * 4 / 3 + 1);
private EObject grammarElement;
private EObject queuedGrammarElement;
private Boolean result = Boolean.FALSE;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
package org.eclipse.xtext.ide.editor.contentassist.antlr.internal;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
Expand Down Expand Up @@ -36,7 +37,6 @@
import org.eclipse.xtext.parser.antlr.IUnorderedGroupHelper;

import com.google.common.collect.LinkedHashMultimap;
import com.google.common.collect.Maps;
import com.google.common.collect.Multimap;

/**
Expand Down Expand Up @@ -585,7 +585,8 @@ public Set<FollowElement> getFollowElements() {
@Override
public Map<Integer, String> getTokenDefMap() {
String[] names = getTokenNames();
Map<Integer, String> result = Maps.newHashMapWithExpectedSize(names.length - Token.MIN_TOKEN_TYPE);
int expectedSize = names.length - Token.MIN_TOKEN_TYPE;
Map<Integer, String> result = new HashMap<>(expectedSize * 4 / 3 + 1);
for (int i = Token.MIN_TOKEN_TYPE; i < names.length; i++) {
result.put(i, getValueForTokenName(names[i]));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
package org.eclipse.xtext.ide.serializer.impl;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
Expand All @@ -31,7 +32,6 @@
import com.google.common.base.Preconditions;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Multimap;
import com.google.inject.Inject;

Expand Down Expand Up @@ -59,7 +59,7 @@ public SimpleNameDescription(QualifiedName qName, EObject resolvedObject, IEObje
Map<String, String> userData = null;
for (final String key : source.getUserDataKeys()) {
if (userData == null) {
userData = Maps.newHashMapWithExpectedSize(2);
userData = new HashMap<>(2 + 1);
}
userData.put(key, source.getUserData(key));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import org.eclipse.xtext.resource.impl.DefaultResourceDescriptionStrategy;
import org.eclipse.xtext.util.IAcceptor;

import com.google.common.collect.Maps;
import com.google.inject.Inject;
import com.google.inject.Singleton;

Expand Down Expand Up @@ -48,8 +47,7 @@ protected boolean createEObjectDescriptions(IQualifiedNameProvider qualifiedName
try {
QualifiedName qualifiedName = qualifiedNameProvider.getFullyQualifiedName(eObject);
if (qualifiedName != null) {
Map<String, String> userData = Maps.newHashMapWithExpectedSize(1);
userData.put(NS_URI_INDEX_ENTRY, Boolean.toString(isNsURI));
Map<String, String> userData = Map.of(NS_URI_INDEX_ENTRY, Boolean.toString(isNsURI));
IEObjectDescription description = EObjectDescription.create(qualifiedName, eObject, userData);
acceptor.accept(description);
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
Expand Down Expand Up @@ -74,7 +75,6 @@
import com.google.common.collect.HashMultimap;
import com.google.common.collect.LinkedHashMultimap;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Multimap;
import com.google.common.collect.Sets;
import com.google.inject.Inject;
Expand Down Expand Up @@ -996,7 +996,7 @@ public boolean isOptional(AbstractElement element) {
public static class CallHierarchyHelper extends XtextSwitch<Boolean> {
private final EObject nextGrammarElement;
private Set<AbstractRule> visiting = new HashSet<AbstractRule>();
private Map<AbstractRule, Boolean> visited = Maps.newHashMapWithExpectedSize(4);
private Map<AbstractRule, Boolean> visited = new HashMap<>(4 * 4 / 3 + 1);
private EObject grammarElement;
private EObject queuedGrammarElement;
private Boolean result = Boolean.FALSE;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
package org.eclipse.xtext.ui;

import java.net.URL;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

Expand All @@ -20,7 +21,6 @@
import org.osgi.framework.BundleEvent;
import org.osgi.framework.SynchronousBundleListener;

import com.google.common.collect.Maps;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import com.google.inject.name.Named;
Expand Down Expand Up @@ -49,7 +49,7 @@ public class PluginImageHelper implements IImageHelper, IImageDescriptorHelper,

private static final String PLATFORM_URL_SEPARATOR = "/";

private Map<ImageDescriptor, Image> registry = Maps.newHashMapWithExpectedSize(10);
private Map<ImageDescriptor, Image> registry = new HashMap<>(10 * 4 / 3 + 1);

@Inject
private AbstractUIPlugin plugin;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*******************************************************************************/
package org.eclipse.xtext.ui.compare;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

Expand Down Expand Up @@ -245,7 +246,7 @@ protected IEditorInput getEditorInput(ISourceViewer sourceViewer) {
protected SourceViewer createSourceViewer(Composite parent, int textOrientation) {
if (getSite() != null) {
if (sourceViewerEditorMap == null) {
sourceViewerEditorMap = Maps.newHashMapWithExpectedSize(3);
sourceViewerEditorMap = new HashMap<>(3 * 4 / 3 + 1);
}
DefaultMergeEditor mergeEditor = createMergeEditor();
mergeEditor.setXtextEditorCallback(new CompoundXtextEditorCallback(null));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
package org.eclipse.xtext.ui.editor;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

Expand All @@ -27,7 +28,6 @@
import com.google.common.base.Predicates;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;
import com.google.common.collect.Maps;
import com.google.inject.Provider;

/**
Expand Down Expand Up @@ -61,7 +61,7 @@ public IEObjectDescription apply(IEObjectDescription from) {
Map<String, String> userData = null;
for(String key: from.getUserDataKeys()) {
if (userData == null) {
userData = Maps.newHashMapWithExpectedSize(2);
userData = new HashMap<>(2 + 1);
}
userData.put(key, from.getUserData(key));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
package org.eclipse.xtext.ui.editor.occurrences;

import static com.google.common.collect.Lists.*;
import static com.google.common.collect.Maps.*;
import static java.util.Collections.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -138,7 +138,7 @@ public boolean isCanceled() {
};
referenceFinder.findReferences((TargetURIs) targetURIs, resource, acceptor, localMonitor);
operationCanceledManager.checkCanceled(cancelIndicator);
Map<Annotation, Position> result = newHashMapWithExpectedSize(references.size() + 1);
Map<Annotation, Position> result = new HashMap<>((references.size() + 1) * 4 / 3 + 1);
if (target.eResource() == resource) {
if (!references.isEmpty() || canBeReferencedLocally(target)) {
ITextRegion declarationRegion = locationInFileProvider.getSignificantTextRegion(target);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*******************************************************************************/
package org.eclipse.xtext.ui.editor.validation;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -39,7 +40,6 @@
import com.google.common.collect.HashBiMap;
import com.google.common.collect.Iterators;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Multimap;

/**
Expand Down Expand Up @@ -128,7 +128,7 @@ protected Map<Annotation, Position> getAnnotationsToAdd(Multimap<Position, Annot
if (monitor.isCanceled()) {
return HashBiMap.create();
}
Map<Annotation, Position> annotationToPosition = Maps.newHashMapWithExpectedSize(issues.size());
Map<Annotation, Position> annotationToPosition = new HashMap<>(issues.size() * 4 / 3 + 1);
for (Issue issue : issues) {
if (monitor.isCanceled()) {
return annotationToPosition;
Expand Down
Loading

0 comments on commit 4394639

Please sign in to comment.