diff options
author | Carsten Hammer | 2020-01-12 15:41:12 +0000 |
---|---|---|
committer | Lars Vogel | 2020-02-07 06:46:17 +0000 |
commit | aada320ac9b1d463031f9356923568a511e9278c (patch) | |
tree | 32c75434be17f230e47044fa4c85686010060999 /org.eclipse.ui.cheatsheets/src/org/eclipse | |
parent | 403b7ce270e2f3874f924dd30710127a9c9456cc (diff) | |
download | eclipse.platform.ua-aada320ac9b1d463031f9356923568a511e9278c.tar.gz eclipse.platform.ua-aada320ac9b1d463031f9356923568a511e9278c.tar.xz eclipse.platform.ua-aada320ac9b1d463031f9356923568a511e9278c.zip |
Use switch over strings where possible
Change cascades of ifs which can be converted to switch over Strings.
A switch statement might be faster than an if-then-else chain. And it
improves clarity.
The problem with if..else chain is that I have to look into all the if
conditions to understand what the program is doing. And the variable
might change in the chain processing.
Change-Id: If77bc5ffee41c236b56d82b226df4e55f6619bfc
Signed-off-by: Carsten Hammer <carsten.hammer@t-online.de>
Diffstat (limited to 'org.eclipse.ui.cheatsheets/src/org/eclipse')
2 files changed, 94 insertions, 42 deletions
diff --git a/org.eclipse.ui.cheatsheets/src/org/eclipse/ui/internal/cheatsheets/CheatsheetSearchParticipant.java b/org.eclipse.ui.cheatsheets/src/org/eclipse/ui/internal/cheatsheets/CheatsheetSearchParticipant.java index 312e10633..45e318034 100644 --- a/org.eclipse.ui.cheatsheets/src/org/eclipse/ui/internal/cheatsheets/CheatsheetSearchParticipant.java +++ b/org.eclipse.ui.cheatsheets/src/org/eclipse/ui/internal/cheatsheets/CheatsheetSearchParticipant.java @@ -85,19 +85,27 @@ public class CheatsheetSearchParticipant extends SearchParticipantXML { @Override protected void handleStartElement(String name, Attributes attributes, IParsedXMLContent data) { - if (name.equals(IParserTags.CHEATSHEET)) { + switch (name) { + case IParserTags.CHEATSHEET: data.setTitle(attributes.getValue(IParserTags.TITLE)); data.addText(attributes.getValue(IParserTags.TITLE)); - } else if (name.equals(ICompositeCheatsheetTags.COMPOSITE_CHEATSHEET)) { + break; + case ICompositeCheatsheetTags.COMPOSITE_CHEATSHEET: data.addText(attributes.getValue(ICompositeCheatsheetTags.NAME)); data.setTitle(attributes.getValue(ICompositeCheatsheetTags.NAME)); - } else if (name.equals(IParserTags.ITEM)) { + break; + case IParserTags.ITEM: data.addText(attributes.getValue(IParserTags.TITLE)); - } else if (name.equals(IParserTags.SUBITEM)) { + break; + case IParserTags.SUBITEM: data.addText(attributes.getValue(IParserTags.LABEL)); - } else if (name.equals(ICompositeCheatsheetTags.TASK ) - || name.equals(ICompositeCheatsheetTags.TASK_GROUP)) { + break; + case ICompositeCheatsheetTags.TASK: + case ICompositeCheatsheetTags.TASK_GROUP: data.addText(attributes.getValue(ICompositeCheatsheetTags.NAME)); + break; + default: + break; } } diff --git a/org.eclipse.ui.cheatsheets/src/org/eclipse/ui/internal/cheatsheets/data/CheatSheetParser.java b/org.eclipse.ui.cheatsheets/src/org/eclipse/ui/internal/cheatsheets/data/CheatSheetParser.java index 296af80a3..18f4bb3f0 100644 --- a/org.eclipse.ui.cheatsheets/src/org/eclipse/ui/internal/cheatsheets/data/CheatSheetParser.java +++ b/org.eclipse.ui.cheatsheets/src/org/eclipse/ui/internal/cheatsheets/data/CheatSheetParser.java @@ -443,18 +443,22 @@ public class CheatSheetParser implements IStatusContainer { isLeadingTrimRequired = false; } else if(node.getNodeType() == Node.ELEMENT_NODE) { // handle <b></b> and <br/> - if(node.getNodeName().equals(IParserTags.BOLD)) { + switch (node.getNodeName()) { + case IParserTags.BOLD: containsMarkup = true; text.append(IParserTags.BOLD_START_TAG); text.append(node.getFirstChild().getNodeValue()); text.append(IParserTags.BOLD_END_TAG); isLeadingTrimRequired = false; - } else if(node.getNodeName().equals(IParserTags.BREAK)) { + break; + case IParserTags.BREAK: containsMarkup = true; text.append(IParserTags.BREAK_TAG); isLeadingTrimRequired = true; - } else { + break; + default: warnUnknownMarkupElement(startNode, nodeName, node); + break; } } } @@ -565,13 +569,17 @@ public class CheatSheetParser implements IStatusContainer { if (attribute == null || attributeName == null) continue; - if (attributeName.equals(IParserTags.CONTEXTID)) { + switch (attributeName) { + case IParserTags.CONTEXTID: item.setContextId(attribute.getNodeValue()); - } else if (attributeName.equals(IParserTags.HREF)) { + break; + case IParserTags.HREF: item.setHref(attribute.getNodeValue()); - } else { + break; + default: String message = NLS.bind(Messages.WARNING_PARSING_UNKNOWN_ATTRIBUTE, (new Object[] {attributeName, introNode.getNodeName()})); addStatus(IStatus.WARNING, message, null); + break; } } } @@ -593,11 +601,14 @@ public class CheatSheetParser implements IStatusContainer { for (int i = 0; i < nodes.getLength(); i++) { Node node = nodes.item(i); checker.checkElement(node.getNodeName()); - if(node.getNodeName().equals(IParserTags.ACTION)) { + switch (node.getNodeName()) { + case IParserTags.ACTION: handleExecutable(item, node, new Action()); - } else if(node.getNodeName().equals(IParserTags.COMMAND)) { + break; + case IParserTags.COMMAND: handleExecutable(item, node, new CheatSheetCommand()); - } else if(node.getNodeName().equals(IParserTags.DESCRIPTION)) { + break; + case IParserTags.DESCRIPTION: if (hasDescription) { String message = NLS.bind(Messages.ERROR_PARSING_MULTIPLE_DESCRIPTION, (new Object[] {itemNode.getNodeName()})); addStatus(IStatus.ERROR, message, null); @@ -605,21 +616,28 @@ public class CheatSheetParser implements IStatusContainer { hasDescription = true; handleDescription(item, node); } - } else if(node.getNodeName().equals(IParserTags.ON_COMPLETION)) { + break; + case IParserTags.ON_COMPLETION: handleOnCompletion(item, node); - } else if(node.getNodeName().equals(IParserTags.SUBITEM)) { + break; + case IParserTags.SUBITEM: handleSubItem(item, node); - } else if(node.getNodeName().equals(IParserTags.CONDITIONALSUBITEM)) { + break; + case IParserTags.CONDITIONALSUBITEM: handleConditionalSubItem(item, node); - } else if(node.getNodeName().equals(IParserTags.REPEATEDSUBITM)) { + break; + case IParserTags.REPEATEDSUBITM: handleRepeatedSubItem(item, node); - } else if(node.getNodeName().equals(IParserTags.PERFORMWHEN)) { + break; + case IParserTags.PERFORMWHEN: handlePerformWhen(item, node); - } else { + break; + default: if(node.getNodeType() != Node.TEXT_NODE && node.getNodeType() != Node.COMMENT_NODE ) { String message = NLS.bind(Messages.WARNING_PARSING_UNKNOWN_ELEMENT, (new Object[] {node.getNodeName(), itemNode.getNodeName()})); addStatus(IStatus.WARNING, message, null); } + break; } } @@ -647,18 +665,24 @@ public class CheatSheetParser implements IStatusContainer { if (attribute == null || attributeName == null) continue; - if (attributeName.equals(IParserTags.TITLE)) { + switch (attributeName) { + case IParserTags.TITLE: title = true; item.setTitle(attribute.getNodeValue()); - } else if (attributeName.equals(IParserTags.CONTEXTID)) { + break; + case IParserTags.CONTEXTID: item.setContextId(attribute.getNodeValue()); - } else if (attributeName.equals(IParserTags.HREF)) { + break; + case IParserTags.HREF: item.setHref(attribute.getNodeValue()); - } else if (attributeName.equals(IParserTags.SKIP)) { + break; + case IParserTags.SKIP: item.setSkip(attribute.getNodeValue().equals(TRUE_STRING)); - } else if (attributeName.equals(IParserTags.DIALOG)) { + break; + case IParserTags.DIALOG: item.setDialog(attribute.getNodeValue().equals(TRUE_STRING)); - } else { + break; + default: AbstractItemExtensionElement[] ie = handleUnknownItemAttribute(attribute, itemNode); if (ie != null) { itemExtensionElements.add(ie); @@ -666,6 +690,7 @@ public class CheatSheetParser implements IStatusContainer { String message = NLS.bind(Messages.WARNING_PARSING_UNKNOWN_ATTRIBUTE, (new Object[] {attributeName, itemNode.getNodeName()})); addStatus(IStatus.WARNING, message, null); } + break; } } } @@ -718,17 +743,21 @@ public class CheatSheetParser implements IStatusContainer { NodeList nodes = performWhenNode.getChildNodes(); for (int i = 0; i < nodes.getLength(); i++) { Node node = nodes.item(i); - if(node.getNodeName().equals(IParserTags.ACTION)) { + switch (node.getNodeName()) { + case IParserTags.ACTION: exeutable = true; handleExecutable(performWhen, node, new Action()); - } else if(node.getNodeName().equals(IParserTags.COMMAND)) { + break; + case IParserTags.COMMAND: exeutable = true; handleExecutable(performWhen, node, new CheatSheetCommand()); - } else { + break; + default: if(node.getNodeType() != Node.TEXT_NODE && node.getNodeType() != Node.COMMENT_NODE ) { String message = NLS.bind(Messages.WARNING_PARSING_UNKNOWN_ELEMENT, (new Object[] {node.getNodeName(), performWhenNode .getNodeName()})); addStatus(IStatus.WARNING, message, null); } + break; } } @@ -813,13 +842,17 @@ public class CheatSheetParser implements IStatusContainer { Node node = nodes.item(i); checker.checkElement(node.getNodeName()); - if(node.getNodeName().equals(IParserTags.ACTION)) { + switch (node.getNodeName()) { + case IParserTags.ACTION: handleExecutable(subItem, node, new Action()); - } else if(node.getNodeName().equals(IParserTags.COMMAND)) { + break; + case IParserTags.COMMAND: handleExecutable(subItem, node, new CheatSheetCommand()); - } else if(node.getNodeName().equals(IParserTags.PERFORMWHEN)) { + break; + case IParserTags.PERFORMWHEN: handlePerformWhen(subItem, node); - } else if (node.getNodeName().equals(IParserTags.DESCRIPTION)) { + break; + case IParserTags.DESCRIPTION: if (subItem.isFormatted()) { String message = NLS.bind( Messages.ERROR_PARSING_MULTIPLE_DESCRIPTION, @@ -828,11 +861,13 @@ public class CheatSheetParser implements IStatusContainer { } else { handleSubItemDescription(subItem, node); } - } else { + break; + default: if(node.getNodeType() != Node.TEXT_NODE && node.getNodeType() != Node.COMMENT_NODE ) { String message = NLS.bind(Messages.WARNING_PARSING_UNKNOWN_ELEMENT, (new Object[] {node.getNodeName(), subItemNode.getNodeName()})); addStatus(IStatus.WARNING, message, null); } + break; } } handleSubItemAttributes(subItem, subItemNode); @@ -851,15 +886,20 @@ public class CheatSheetParser implements IStatusContainer { if (attribute == null || attributeName == null) continue; - if (attributeName.equals(IParserTags.LABEL)) { + switch (attributeName) { + case IParserTags.LABEL: subItem.setLabel(attribute.getNodeValue()); - } else if (attributeName.equals(IParserTags.SKIP)) { + break; + case IParserTags.SKIP: subItem.setSkip(attribute.getNodeValue().equals(TRUE_STRING)); - } else if (attributeName.equals(IParserTags.WHEN)) { + break; + case IParserTags.WHEN: subItem.setWhen(attribute.getNodeValue()); - } else { + break; + default: String message = NLS.bind(Messages.WARNING_PARSING_UNKNOWN_ATTRIBUTE, (new Object[] {attributeName, subItemNode.getNodeName()})); addStatus(IStatus.WARNING, message, null); + break; } } } @@ -1028,22 +1068,26 @@ public class CheatSheetParser implements IStatusContainer { for (int i = 0; i < nodes.getLength(); i++) { Node node = nodes.item(i); - if(node.getNodeName().equals(IParserTags.ITEM)) { + switch (node.getNodeName()) { + case IParserTags.ITEM: hasItem = true; Item item = handleItem(node); cheatSheet.addItem(item); - } else if(node.getNodeName().equals(IParserTags.INTRO)) { + break; + case IParserTags.INTRO: if (hasIntro) { addStatus(IStatus.ERROR, Messages.ERROR_PARSING_MORE_THAN_ONE_INTRO, null); } else { hasIntro = true; handleIntroNode(cheatSheet, node); } - } else { + break; + default: if(node.getNodeType() != Node.TEXT_NODE && node.getNodeType() != Node.COMMENT_NODE ) { String message = NLS.bind(Messages.WARNING_PARSING_UNKNOWN_ELEMENT, (new Object[] {node.getNodeName(), rootnode.getNodeName()})); addStatus(IStatus.WARNING, message, null); } + break; } } |