Skip to content

Commit

Permalink
Improved Map/Set usage apache#5332
Browse files Browse the repository at this point in the history
keySet().contains() -> containsKey()
values().contains() -> containsValue()
keySet() -> entrySet() - both keys and values are used
keySet() -> values() - keys used only with get()
entrySet() -> values() - keys not used
entrySet() -> keySet() - values not used
remove() -> removeAll() - applicable
put() -> putAll()

Targets: performance(1), structure(2), clean code(3)
  • Loading branch information
tbw777 committed Feb 16, 2023
1 parent 31028d2 commit 4261a87
Show file tree
Hide file tree
Showing 129 changed files with 356 additions and 391 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -506,10 +506,9 @@ private static void storeEditableProperties(final EditableProperties p, final Fi

private void loadLocalizedBundlesFromPlatform(final BrandableModule moduleEntry, final Set<String> keys, final Set<BundleKey> bundleKeys) {
Map<String,String> p = localizingBundle(moduleEntry);
for (String key : p.keySet()) {
if (keys.contains(key)) {
String value = p.get(key);
bundleKeys.add(new BundleKey(moduleEntry, key, value));
for (Map.Entry<String,String> entry : p.entrySet()) {
if (keys.contains(entry.getKey())) {
bundleKeys.add(new BundleKey(moduleEntry, entry.getKey(), entry.getValue()));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import javax.swing.table.DefaultTableModel;
import org.netbeans.insane.live.CancelException;
import org.netbeans.insane.live.LiveReferences;
import org.netbeans.insane.live.Path;
import org.openide.DialogDescriptor;
import org.openide.filesystems.FileObject;
import org.openide.loaders.DataObject;
Expand Down Expand Up @@ -439,18 +440,18 @@ public void stateChanged(ChangeEvent arg0) {
inner.paintImmediately(inner.getBounds());
}
});
Map/*<Object,Path>*/ traces = LiveReferences.fromRoots(objects, null, bar.getModel());
Map<Object, Path> traces = LiveReferences.fromRoots(objects, null, bar.getModel());
if (traces == null) {
return "";
}
StringBuffer sb = new StringBuffer();
for (Object inst : traces.keySet()) {

traces.forEach((inst, path) -> {
sb.append(inst);
sb.append(":\n"); // NOI18N
sb.append(traces.get(inst));
sb.append(path);
sb.append("\n\n"); // NOI18N
}
});

return sb.toString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ public void deployDatasources(Set<Datasource> datasources)
continue;
JBossDatasource ds = (JBossDatasource)o;
String jndiName = JBossDatasource.getRawName(ds.getJndiName());
if (ddsMap.keySet().contains(jndiName)) { // conflicting ds found
if (ddsMap.containsKey(jndiName)) { // conflicting ds found
if (!ddsMap.get(jndiName).equals(ds)) { // found ds is not equal
conflictDS.add(ddsMap.get(jndiName)); // NOI18N
}
Expand Down Expand Up @@ -217,7 +217,7 @@ else if (jndiName != null) {
LocalTxDatasource ltxds[] = deployedDSGraph.getLocalTxDatasource();
for (int i = 0; i < ltxds.length; i++) {
String jndiName = ltxds[i].getJndiName();
if (newDS.keySet().contains(jndiName)) //conflict, we must remove it from graph
if (newDS.containsKey(jndiName)) //conflict, we must remove it from graph
deployedDSGraph.removeLocalTxDatasource(ltxds[i]);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public void deployDatasources(Set<Datasource> datasources) throws ConfigurationE

WLDatasource wlDatasource = (WLDatasource) datasource;
String jndiName = wlDatasource.getJndiName();
if (deployed.keySet().contains(jndiName)) { // conflicting ds found
if (deployed.containsKey(jndiName)) { // conflicting ds found
Datasource deployedDatasource = deployed.get(jndiName);

// jndi name is same, but DS differs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public void deployMessageDestinations(Set<MessageDestination> destinations) thro
WLMessageDestination wLMessageDestination = (WLMessageDestination) destination;
// FIXME this is checking only JNDI name collisison, check also module name ???
String name = wLMessageDestination.getName();
if (deployed.keySet().contains(name)) { // conflicting ds found
if (deployed.containsKey(name)) { // conflicting ds found
MessageDestination deployedMessageDestination = deployed.get(name);

// name is same, but message dest differs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,9 +236,7 @@ private void removeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-
for (int i=0; i<rows.length; i++) {
rowsToRemove.add(getTargetsModel().get(rows[i]));
}
for (Object o : rowsToRemove) {
getTargetsModel().remove(o);
}
getTargetsModel().removeAll(rowsToRemove);
saveTargetsModel();
}//GEN-LAST:event_removeButtonActionPerformed

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ public void removeConnectedProfile(OCIProfile profile) {
}
if (profile == getActiveProfile()) {
OCIProfile def = forConfig(defaultConfigPath, profName);
if (profiles.values().contains(def) || profiles.isEmpty()) {
if (profiles.containsValue(def) || profiles.isEmpty()) {
resetToProfile = def;
} else {
resetToProfile = profiles.values().iterator().next();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,16 +102,16 @@ static List<File> processFileset(FileSet fileset, String rootDir)
List<String> paths = fileset.getPaths();
ArrayList<File> result = new ArrayList<>();

for (String dir : filesets.keySet()) {
File d = new File(dir);
for (Map.Entry<String, List<String>> it : filesets.entrySet()) {
File d = new File(it.getKey());
String dirPrefix;
if (!d.isAbsolute()) {
dirPrefix = new File(rootDir, d.getPath()).getAbsolutePath();
} else {
dirPrefix = d.getAbsolutePath();
}

List<Pattern> patterns = compilePatterns(filesets.get(dir));
List<Pattern> patterns = compilePatterns(it.getValue());
File[] fileArray = new File(dirPrefix).listFiles(createFilter(
patterns));
if (fileArray != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,9 +229,9 @@ public Void run( WebAppMetadata data ) {
rootsSet);
intersection.retainAll(oldRoots);
oldRoots.removeAll(rootsSet);
for (FileObject fileObject : oldRoots) {
myRootToFragment.remove( fileObject );
}

myRootToFragment.keySet().removeAll(oldRoots);

rootsSet.removeAll(intersection);
for (FileObject fileObject : rootsSet)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,7 @@ private void filterInvalidServices(Map<String, Object> annotationMap, Object mod
}
}

for(String serviceKey: servicesToRemove) {
annotationMap.remove(serviceKey);
}

return;
annotationMap.keySet().removeAll(servicesToRemove);
}

/** Maps interesting fields from service-ref descriptor to a multi-level property map.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1674,7 +1674,7 @@ public boolean canStartServer() {
}

public boolean isManagerOf(Target target) {
return getTargetMap().keySet().contains(target.getName());
return getTargetMap().containsKey(target.getName());
}

public synchronized ServerTarget getCoTarget() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public void deployDatasources(Set<Datasource> datasources)
}
WildflyDatasource ds = (WildflyDatasource) o;
String jndiName = WildflyDatasource.getRawName(ds.getJndiName());
if (ddsMap.keySet().contains(jndiName)) { // conflicting ds found
if (ddsMap.containsKey(jndiName)) { // conflicting ds found
if (!ddsMap.get(jndiName).equals(ds)) { // found ds is not equal
conflictDS.add(ddsMap.get(jndiName)); // NOI18N
}
Expand Down Expand Up @@ -137,7 +137,7 @@ public void deployDatasources(Set<Datasource> datasources)
DatasourceType ltxds[] = deployedDSGraph.getDatasource();
for (int i = 0; i < ltxds.length; i++) {
String jndiName = ltxds[i].getJndiName();
if (newDS.keySet().contains(jndiName)) //conflict, we must remove it from graph
if (newDS.containsKey(jndiName)) //conflict, we must remove it from graph
{
deployedDSGraph.removeDatasource(ltxds[i]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ public void deployMessageDestinations(Set<MessageDestination> destinations) thro
String name = jbossMessageDestination.getName();
Set<String> jndiNames = new HashSet<String>(jbossMessageDestination.getJndiNames());
jndiNames.retainAll(deployed.keySet());
if (deployed.keySet().contains(jbossMessageDestination.getName()) || !jndiNames.isEmpty()) { // conflicting destination found
if (deployed.containsKey(jbossMessageDestination.getName()) || !jndiNames.isEmpty()) { // conflicting destination found
MessageDestination deployedMessageDestination = deployed.get(name);
// name is same, but message dest differs
if (!deployedMessageDestination.equals(jbossMessageDestination)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,12 @@ public String toString() {
StringBuffer buf = new StringBuffer();
buf.append("JspParseData[prefixes=");
if (prefixMap != null) {
for (String prefix : prefixMap.keySet()) {
prefixMap.forEach((prefix, v) -> {
buf.append(prefix);
buf.append('-');
buf.append(prefixMap.get(prefix));
buf.append(v);
buf.append(',');
}
});
} else {
buf.append("null");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -381,15 +381,16 @@ private void doUpdateJaxWs( Map<String, ServiceInfo> newServices ) {
commonServices.add(key);
}
}

for (String key : commonServices) {
oldServices.remove(key);
newServices.remove(key);
}

// remove old services
boolean needToSave = false;
for (String key : oldServices.keySet()) {
jaxWsSupport.removeService(oldServices.get(key));
for (JaxWsService jaxWsService : oldServices.values()) {
jaxWsSupport.removeService(jaxWsService);
}
// add new services
for (Map.Entry<String, ServiceInfo> entry : newServices.entrySet()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,16 +102,16 @@ static List<File> processFileset(FileSet fileset, String rootDir)
List<String> paths = fileset.getPaths();
ArrayList<File> result = new ArrayList<>();

for (String dir : filesets.keySet()) {
File d = new File(dir);
for (Map.Entry<String, List<String>> it : filesets.entrySet()) {
File d = new File(it.getKey());
String dirPrefix;
if (!d.isAbsolute()) {
dirPrefix = new File(rootDir, d.getPath()).getAbsolutePath();
} else {
dirPrefix = d.getAbsolutePath();
}

List<Pattern> patterns = compilePatterns(filesets.get(dir));
List<Pattern> patterns = compilePatterns(it.getValue());
File[] fileArray = new File(dirPrefix).listFiles(createFilter(
patterns));
if (fileArray != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ protected boolean checkBuiltInBeans( VariableElement element,
Map<String, ? extends AnnotationMirror> qualifiersFqns = helper.
getAnnotationsByType(qualifiers);
boolean hasOnlyDefault = false;
if ( qualifiersFqns.keySet().contains(AnnotationUtil.DEFAULT_FQN)){
if ( qualifiersFqns.containsKey(AnnotationUtil.DEFAULT_FQN)){
HashSet<String> fqns = new HashSet<String>(qualifiersFqns.keySet());
fqns.remove( AnnotationUtil.NAMED );
fqns.remove( AnnotationUtil.ANY );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ private void checkInjectionPointMetadata( VariableElement element,
Map<String, ? extends AnnotationMirror> qualifiersFqns = helper.
getAnnotationsByType(qualifiers);
boolean hasDefault = model.hasImplicitDefaultQualifier( element );
if ( !hasDefault && qualifiersFqns.keySet().contains(AnnotationUtil.DEFAULT_FQN)){
if ( !hasDefault && qualifiersFqns.containsKey(AnnotationUtil.DEFAULT_FQN)){
hasDefault = true;
}
if ( !hasDefault || cancel.get() ){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ private void checkInjectionPointMetadata( VariableElement var,
.getAnnotationsByType(qualifiers);
boolean hasDefault = model.hasImplicitDefaultQualifier(varElement);
if (!hasDefault
&& qualifiersFqns.keySet().contains(AnnotationUtil.DEFAULT_FQN))
&& qualifiersFqns.containsKey(AnnotationUtil.DEFAULT_FQN))
{
hasDefault = true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -693,7 +693,7 @@ private void addAttributeItems(CompletionResultSet result, int offset, JspSyntax
attr = ((TagAttributeInfo)item).getName();
else
attr = (String)item;
boolean isThere = tagDir.getAttributes().keySet().contains(attr);
boolean isThere = tagDir.getAttributes().containsKey(attr);
if (!isThere || attr.equalsIgnoreCase(currentAttr) ||
(currentAttr != null && attr.startsWith(currentAttr) && attr.length()>currentAttr.length() && !isThere)) {
if (item instanceof TagAttributeInfo)
Expand All @@ -704,8 +704,8 @@ private void addAttributeItems(CompletionResultSet result, int offset, JspSyntax
//to do it.
if ("taglib".equalsIgnoreCase(tagDir.getName())){ //NOI18N
if (attr.equalsIgnoreCase("prefix") //NOI18N
|| (attr.equalsIgnoreCase("uri") && !tagDir.getAttributes().keySet().contains("tagdir")) //NOI18N
|| (attr.equalsIgnoreCase("tagdir") && !tagDir.getAttributes().keySet().contains("uri"))) //NOI18N
|| (attr.equalsIgnoreCase("uri") && !tagDir.getAttributes().containsKey("tagdir")) //NOI18N
|| (attr.equalsIgnoreCase("tagdir") && !tagDir.getAttributes().containsKey("uri"))) //NOI18N
result.addItem(JspCompletionItem.createAttribute(offset, (TagAttributeInfo)item));
} else {
result.addItem(JspCompletionItem.createAttribute(offset, (TagAttributeInfo)item));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ public static Set<Named> parseForUndeclaredElements(HtmlParserResult result, Ope

// undeclared tag prefix
if (openTag.namespacePrefix() != null
&& !result.getNamespaces().values().contains(openTag.namespacePrefix().toString())) {
&& !result.getNamespaces().containsValue(openTag.namespacePrefix().toString())) {
undeclaredEntries.add(openTag);
}

Expand All @@ -373,7 +373,7 @@ public boolean accepts(Attribute attribute) {
}
})) {
// undeclared attribute prefix
if (!result.getNamespaces().values().contains(attribute.namespacePrefix().toString())) {
if (!result.getNamespaces().containsValue(attribute.namespacePrefix().toString())) {
undeclaredEntries.add(attribute);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ public boolean isValid() {
String ccLibNamespace = component.getCompositeComponentURI();
//warning if the current library namespace is not already declared, but the prefix is used for
//another library
if (declaredPrefixes != null && !prefix.equals(declaredPrefixes.get(ccLibNamespace)) && declaredPrefixes.values().contains(prefix)) {
if (declaredPrefixes != null && !prefix.equals(declaredPrefixes.get(ccLibNamespace)) && declaredPrefixes.containsValue(prefix)) {
//the selected prefix is already in use, show warning, but let the user finish the wizard
wizard.putProperty(WizardDescriptor.PROP_WARNING_MESSAGE, NbBundle.getMessage(CompositeComponentVisualPanel.class, "MSG_Already_Used_Prefix", component.getPrefix()));//NOI18N
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,8 @@ private void updateWsModel(Map<String, String> services){

// remove old services
boolean needToSave = false;
for (String key : oldServices.keySet()) {
jaxWsModel.removeService(oldServices.get(key).getName());
for (Service service : oldServices.values()) {
jaxWsModel.removeService(service.getName());
needToSave = true;
}
Set<String> removedFromWsdl = new HashSet<String>(
Expand All @@ -240,10 +240,12 @@ private void updateWsModel(Map<String, String> services){


// add new services
for (String key : services.keySet()) { // services from WSDL
for (Map.Entry<String, String> it : services.entrySet()) { // services from WSDL
String key = it.getKey();

if (key.startsWith("fromWsdl:")) { //NOI18N
Service oldServiceFromWsdl = oldServicesFromWsdl.get(key);
String newImplClass = services.get(key);
String newImplClass = it.getValue();
if (oldServiceFromWsdl != null && !oldServiceFromWsdl.getImplementationClass().equals(newImplClass)) {
oldServiceFromWsdl.setImplementationClass(newImplClass);
needToSave = true;
Expand All @@ -252,7 +254,7 @@ private void updateWsModel(Map<String, String> services){
// add only if doesn't exists
if (jaxWsModel.findServiceByImplementationClass(key) == null) {
try {
jaxWsModel.addService(services.get(key), key);
jaxWsModel.addService(it.getValue(), key);
needToSave = true;
} catch (ServiceAlreadyExistsExeption ex) {
// TODO: need to handle this
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,12 +256,8 @@ void setOperations(List<MethodModel> operations) {
}
// op1 contains methods present in model1 that are not in model2
// op2 contains methods present in model2 that are not in model1
for (String key:op1.keySet()) {
removeOperation(op1.get(key));
}
for (String key:op2.keySet()) {
addOperation(op2.get(key));
}
op1.values().forEach(this::removeOperation);
op2.values().forEach(this::addOperation);
}
}

Expand Down
Loading

0 comments on commit 4261a87

Please sign in to comment.