diff options
author | Mickael Istria | 2018-04-30 13:30:22 +0000 |
---|---|---|
committer | Mickael Istria | 2018-04-30 13:30:22 +0000 |
commit | 162bca1da5a0d6a154a7a58aae5810f564f13fbf (patch) | |
tree | 0cd8149829e8e291afb7f82a7e0ab8ffde3706c7 | |
parent | cc244ba39902c89fe397d5f4369288a38c370910 (diff) | |
download | eclipse.platform.text-162bca1da5a0d6a154a7a58aae5810f564f13fbf.tar.gz eclipse.platform.text-162bca1da5a0d6a154a7a58aae5810f564f13fbf.tar.xz eclipse.platform.text-162bca1da5a0d6a154a7a58aae5810f564f13fbf.zip |
Improve some constructs for performance
Issues reported by SonarQube:
* iterating on keySet() instead of entrySet() or values()
* String += in a loop
Change-Id: I3dec0e683305ab3382605168c0505d5ce2bcc35e
Signed-off-by: Mickael Istria <mistria@redhat.com>
8 files changed, 70 insertions, 70 deletions
diff --git a/org.eclipse.jface.text/projection/org/eclipse/jface/text/source/projection/ProjectionSummary.java b/org.eclipse.jface.text/projection/org/eclipse/jface/text/source/projection/ProjectionSummary.java index abd2fd7f664..de0d042547e 100644 --- a/org.eclipse.jface.text/projection/org/eclipse/jface/text/source/projection/ProjectionSummary.java +++ b/org.eclipse.jface.text/projection/org/eclipse/jface/text/source/projection/ProjectionSummary.java @@ -15,6 +15,7 @@ import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; +import java.util.Map.Entry; import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.NullProgressMonitor; @@ -222,10 +223,9 @@ class ProjectionSummary { if (!isCanceled(monitor)) extension.replaceAnnotations(null, additions); } else { - Iterator<Annotation> e1= additions.keySet().iterator(); - while (e1.hasNext()) { - AnnotationBag bag= (AnnotationBag) e1.next(); - Position position= additions.get(bag); + for (Entry<Annotation, Position> entry : additions.entrySet()) { + AnnotationBag bag= (AnnotationBag) entry.getKey(); + Position position= entry.getValue(); if (isCanceled(monitor)) return; visualAnnotationModel.addAnnotation(bag, position); diff --git a/org.eclipse.search/new search/org/eclipse/search2/internal/ui/text/EditorAccessHighlighter.java b/org.eclipse.search/new search/org/eclipse/search2/internal/ui/text/EditorAccessHighlighter.java index 406e46a3c8b..8f34edd43a5 100644 --- a/org.eclipse.search/new search/org/eclipse/search2/internal/ui/text/EditorAccessHighlighter.java +++ b/org.eclipse.search/new search/org/eclipse/search2/internal/ui/text/EditorAccessHighlighter.java @@ -132,9 +132,8 @@ public class EditorAccessHighlighter extends Highlighter { } } - for (IAnnotationModel model : setsByAnnotationModel.keySet()) { - Set<Annotation> set= setsByAnnotationModel.get(model); - removeAnnotations(model, set); + for (Entry<IAnnotationModel, HashSet<Annotation>> entry : setsByAnnotationModel.entrySet()) { + removeAnnotations(entry.getKey(), entry.getValue()); } } @@ -144,9 +143,8 @@ public class EditorAccessHighlighter extends Highlighter { IAnnotationModelExtension ame= (IAnnotationModelExtension) model; ame.replaceAnnotations(new Annotation[0], annotationToPositionMap); } else { - for (Annotation element : annotationToPositionMap.keySet()) { - Position p= annotationToPositionMap.get(element); - model.addAnnotation(element, p); + for (Entry<Annotation, Position> entry : annotationToPositionMap.entrySet()) { + model.addAnnotation(entry.getKey(), entry.getValue()); } } } diff --git a/org.eclipse.text.tests/src/org/eclipse/text/tests/TextStoreTest.java b/org.eclipse.text.tests/src/org/eclipse/text/tests/TextStoreTest.java index f31626cf32b..f2dbd78ac2a 100644 --- a/org.eclipse.text.tests/src/org/eclipse/text/tests/TextStoreTest.java +++ b/org.eclipse.text.tests/src/org/eclipse/text/tests/TextStoreTest.java @@ -63,7 +63,7 @@ public abstract class TextStoreTest { fTextStore= null; fTracker= null; } - + @Test public void testGet1() throws Exception { set("xxxxx"); @@ -77,7 +77,7 @@ public abstract class TextStoreTest { } } - + @Test public void testGet2() throws Exception { set("xxxxx"); @@ -91,7 +91,7 @@ public abstract class TextStoreTest { } } - + @Test public void testEditScript1() throws Exception { replace(0, fTextStore.getLength(), "x"); @@ -121,7 +121,7 @@ public abstract class TextStoreTest { } catch (IndexOutOfBoundsException e) { } } - + @Test public void testEmptyLines() throws Exception { @@ -131,7 +131,7 @@ public abstract class TextStoreTest { replace(0, 0, "\n\n\n\n\n"); assertTextStoreContents("\n\n\n\n\n"); } - + @Test public void testInsert1() throws Exception { @@ -150,7 +150,7 @@ public abstract class TextStoreTest { replace(11, 5, "y\nxyz"); assertTextStoreContents("x\nxyyyy\nxy\ny\nxyz\nx\n"); } - + @Test public void testInsert2() throws Exception { replace(3, 0, "yyyy"); @@ -159,7 +159,7 @@ public abstract class TextStoreTest { replace(9, 0, "y\ny\ny"); assertTextStoreContents("x\nxyyyy\nxy\ny\ny\nx\nx\n"); } - + @Test public void testLinesNumbers() throws Exception { replace(0, 10, "\na\nbb\nccc\ndddd\neeeee\n"); @@ -174,7 +174,7 @@ public abstract class TextStoreTest { offset+= (i + 1); } } - + @Test public void testOffsets() throws Exception { for (int i= 0; i < 5; i++) { @@ -196,7 +196,7 @@ public abstract class TextStoreTest { assertTrue("invalid line number " + line + " for position " + i + " should be " + l, l == line); } } - + @Test public void testRemove() throws Exception { replace(3, 1, null); @@ -211,13 +211,13 @@ public abstract class TextStoreTest { replace(0, 3, null); assertTextStoreContents(""); } - + @Test public void testReplace() throws Exception { replace(0, fTextStore.getLength(), "\tx\n\tx\n\tx\n\tx\n\tx\n"); assertTextStoreContents("\tx\n\tx\n\tx\n\tx\n\tx\n"); } - + @Test public void testReplace2() throws Exception { replace(0, fTextStore.getLength(), "x"); @@ -226,13 +226,13 @@ public abstract class TextStoreTest { replace(0, fTextStore.getLength(), "x\nx\nx\n"); assertTextStoreContents("x\nx\nx\n"); } - + @Test public void testReplace3() throws Exception { replace(1, 1, "\n"); assertTextStoreContents("x\nx\nx\nx\nx\n"); } - + @Test public void testReplace4() throws Exception { int lines= fTracker.getNumberOfLines(); @@ -245,7 +245,7 @@ public abstract class TextStoreTest { previous= fTracker.getLineInformation(i); } } - + @Test public void testShiftLeft() throws Exception { replace(0, fTextStore.getLength(), "\tx\n\tx\n\tx\n\tx\n\tx\n"); @@ -258,7 +258,7 @@ public abstract class TextStoreTest { assertTextStoreContents("x\nx\nx\nx\nx\n"); } - + @Test public void testShiftRight() throws Exception { for (int i= 0; i < 5; i++) { @@ -268,7 +268,7 @@ public abstract class TextStoreTest { assertTextStoreContents("\tx\n\tx\n\tx\n\tx\n\tx\n"); } - + @Test public void testDeleteEmptyLine() throws Exception { set("x\nx\n\nx\n\n"); @@ -291,15 +291,15 @@ public abstract class TextStoreTest { } } - + @Test public void testDeleteLines() throws Exception { - String content= ""; + StringBuilder content= new StringBuilder(); for (int i= 0; i < 50; i++) { - content += "x\nx\n\nx\n\n"; - set(content); + content.append("x\nx\n\nx\n\n"); + set(content.toString()); - String expected= content; + String expected= content.toString(); int lines= fTracker.getNumberOfLines(); for (int line= 0; line < lines; line++) { int offset= fTracker.getLineOffset(0); @@ -309,12 +309,12 @@ public abstract class TextStoreTest { assertTextStoreContents(expected); } } - content= ""; + content = new StringBuilder(); for (int i= 0; i < 50; i++) { - content += "x\nx\n\nx\n\n"; - set(content); + content.append("x\nx\n\nx\n\n"); + set(content.toString()); - String expected= content; + String expected= content.toString(); int lines= fTracker.getNumberOfLines(); for (int line= lines - 1; line >= 0; line--) { int offset= fTracker.getLineOffset(line); @@ -325,15 +325,15 @@ public abstract class TextStoreTest { } } } - + @Test public void testDeleteLines2() throws Exception { - String content= ""; + StringBuilder content= new StringBuilder(); for (int i= 0; i < 50; i++) { - content += "xxxxxxxxxxxxxx"; - set(content); + content.append("xxxxxxxxxxxxxx"); + set(content.toString()); - String expected= content; + String expected= content.toString(); int lines= fTracker.getNumberOfLines(); for (int line= 0; line < lines; line++) { int offset= fTracker.getLineOffset(0); @@ -343,12 +343,12 @@ public abstract class TextStoreTest { assertTextStoreContents(expected); } } - content= ""; + content= new StringBuilder(); for (int i= 0; i < 50; i++) { - content += "xxxxxxxxxxxxxx"; - set(content); + content.append("xxxxxxxxxxxxxx"); + set(content.toString()); - String expected= content; + String expected= content.toString(); int lines= fTracker.getNumberOfLines(); for (int line= lines - 1; line >= 0; line--) { int offset= fTracker.getLineOffset(line); @@ -359,24 +359,26 @@ public abstract class TextStoreTest { } } } - + @Test public void testSet() throws Exception { - String content= ""; + StringBuilder content = new StringBuilder(); for (int i= 0; i < 35; i++) { int[] lenghts= new int[i + 1]; - for (int j= 0; j < i + 1; j++) + for (int j= 0; j < i + 1; j++) { lenghts[j]= j; - for (int j= 0; j < i; j++) - content+= "x"; + } + for (int j= 0; j < i; j++) { + content.append("x"); + } - set(content); - assertTextStoreContents(content); + set(content.toString()); + assertTextStoreContents(content.toString()); - content+= "\n"; + content.append('\n'); } } - + @Test public void testFunnyLastLineCompatibility() throws Exception { /* empty last line */ diff --git a/org.eclipse.text/src/org/eclipse/jface/text/TextUtilities.java b/org.eclipse.text/src/org/eclipse/jface/text/TextUtilities.java index 736dc58b8de..96e6098881b 100644 --- a/org.eclipse.text/src/org/eclipse/jface/text/TextUtilities.java +++ b/org.eclipse.text/src/org/eclipse/jface/text/TextUtilities.java @@ -16,6 +16,7 @@ import java.util.Iterator; import java.util.List; import java.util.ListIterator; import java.util.Map; +import java.util.Map.Entry; import java.util.Set; import org.eclipse.core.runtime.Assert; @@ -340,10 +341,9 @@ public class TextUtilities { public static void addDocumentPartitioners(IDocument document, Map<String, ? extends IDocumentPartitioner> partitioners) { if (document instanceof IDocumentExtension3) { IDocumentExtension3 extension3= (IDocumentExtension3) document; - Iterator<String> e= partitioners.keySet().iterator(); - while (e.hasNext()) { - String partitioning= e.next(); - IDocumentPartitioner partitioner= partitioners.get(partitioning); + for (Entry<String, ? extends IDocumentPartitioner> entry : partitioners.entrySet()) { + String partitioning= entry.getKey(); + IDocumentPartitioner partitioner= entry.getValue(); partitioner.connect(document); extension3.setDocumentPartitioner(partitioning, partitioner); } diff --git a/org.eclipse.text/src/org/eclipse/jface/text/link/LinkedModeManager.java b/org.eclipse.text/src/org/eclipse/jface/text/link/LinkedModeManager.java index 5c3d4d1a3fd..15c6980b03b 100644 --- a/org.eclipse.text/src/org/eclipse/jface/text/link/LinkedModeManager.java +++ b/org.eclipse.text/src/org/eclipse/jface/text/link/LinkedModeManager.java @@ -166,9 +166,8 @@ class LinkedModeManager { } private void removeManager() { - for (Iterator<IDocument> it= fgManagers.keySet().iterator(); it.hasNext();) { - IDocument doc= it.next(); - if (fgManagers.get(doc) == this) + for (Iterator<LinkedModeManager> it= fgManagers.values().iterator(); it.hasNext();) { + if (it.next() == this) it.remove(); } } diff --git a/org.eclipse.text/src/org/eclipse/jface/text/link/LinkedModeModel.java b/org.eclipse.text/src/org/eclipse/jface/text/link/LinkedModeModel.java index cad71b6d0c8..37c9b028c0c 100644 --- a/org.eclipse.text/src/org/eclipse/jface/text/link/LinkedModeModel.java +++ b/org.eclipse.text/src/org/eclipse/jface/text/link/LinkedModeModel.java @@ -15,6 +15,7 @@ import java.util.Arrays; import java.util.HashSet; import java.util.List; import java.util.Map; +import java.util.Map.Entry; import java.util.Set; import org.eclipse.core.runtime.Assert; @@ -221,8 +222,9 @@ public class LinkedModeModel { if (result != null) { // edit all documents - for (IDocument doc : result.keySet()) { - TextEdit edit= result.get(doc); + for (Entry<IDocument, TextEdit> entry : result.entrySet()) { + IDocument doc = entry.getKey(); + TextEdit edit= entry.getValue(); Replace replace= new Replace(edit); // apply the edition, either as post notification replace diff --git a/org.eclipse.text/src/org/eclipse/jface/text/link/LinkedPositionGroup.java b/org.eclipse.text/src/org/eclipse/jface/text/link/LinkedPositionGroup.java index 8b7685b0288..bbe3e7b0eb9 100644 --- a/org.eclipse.text/src/org/eclipse/jface/text/link/LinkedPositionGroup.java +++ b/org.eclipse.text/src/org/eclipse/jface/text/link/LinkedPositionGroup.java @@ -16,6 +16,7 @@ import java.util.Iterator; import java.util.LinkedList; import java.util.List; import java.util.Map; +import java.util.Map.Entry; import org.eclipse.core.runtime.Assert; @@ -271,10 +272,10 @@ public class LinkedPositionGroup { try { Map<IDocument, TextEdit> result= new HashMap<>(); - for (IDocument d : map.keySet()) { - TextEdit edit= new MultiTextEdit(0, d.getLength()); - edit.addChildren(map.get(d).toArray(new TextEdit[0])); - result.put(d, edit); + for (Entry<IDocument, List<ReplaceEdit>> edits : map.entrySet()) { + TextEdit edit= new MultiTextEdit(0, edits.getKey().getLength()); + edit.addChildren(edits.getValue().toArray(new TextEdit[edits.getValue().size()])); + result.put(edits.getKey(), edit); } return result; diff --git a/org.eclipse.text/src/org/eclipse/jface/text/source/AnnotationModel.java b/org.eclipse.text/src/org/eclipse/jface/text/source/AnnotationModel.java index efe35879966..48fdcbfcac8 100644 --- a/org.eclipse.text/src/org/eclipse/jface/text/source/AnnotationModel.java +++ b/org.eclipse.text/src/org/eclipse/jface/text/source/AnnotationModel.java @@ -493,8 +493,7 @@ public class AnnotationModel implements IAnnotationModel, IAnnotationModelExtens connected(); } - for (Object object : fAttachments.keySet()) { - IAnnotationModel model= fAttachments.get(object); + for (IAnnotationModel model : fAttachments.values()) { model.connect(document); } } @@ -518,8 +517,7 @@ public class AnnotationModel implements IAnnotationModel, IAnnotationModelExtens Assert.isTrue(fDocument == document); - for (Object object : fAttachments.keySet()) { - IAnnotationModel model= fAttachments.get(object); + for (IAnnotationModel model : fAttachments.values()) { model.disconnect(document); } |