Use List instead of SortedMap for scenario events 33/77933/2
authorStephen Kitt <skitt@redhat.com>
Mon, 19 Nov 2018 10:11:01 +0000 (11:11 +0100)
committerStephen Kitt <skitt@redhat.com>
Mon, 19 Nov 2018 10:24:09 +0000 (11:24 +0100)
ScenarioService::getEventsFromScenario uses a SortedMap to enforce
iteration order; this can be achieved less expensively using a List.

Change-Id: I51877cd6fe76ee3a7c18d7f61a6b46d1367d9f08
Signed-off-by: Stephen Kitt <skitt@redhat.com>
samples/simple-client/src/main/java/org/opendaylight/openflowjava/protocol/impl/clients/ScenarioFactory.java
samples/simple-client/src/main/java/org/opendaylight/openflowjava/protocol/impl/clients/ScenarioService.java
samples/simple-client/src/main/java/org/opendaylight/openflowjava/protocol/impl/clients/ScenarioServiceImpl.java

index 76fc3e6cef808a1cf211ad2ba6aadf5096b559d9..8f29a1b6e70106f4ef5c8aef08b19775dfcdaedf 100644 (file)
@@ -11,7 +11,6 @@ package org.opendaylight.openflowjava.protocol.impl.clients;
 import java.io.IOException;
 import java.util.ArrayDeque;
 import java.util.Deque;
-import java.util.Map;
 import javax.xml.bind.JAXBException;
 import org.opendaylight.openflowjava.util.ByteBufUtils;
 import org.xml.sax.SAXException;
@@ -80,9 +79,9 @@ public final class ScenarioFactory {
             throws JAXBException, SAXException, IOException {
         ScenarioService scenarioService = new ScenarioServiceImpl(scenarioFile);
         Deque<ClientEvent> stack = new ArrayDeque<>();
-        for (Map.Entry<Integer, ClientEvent> clientEvent : scenarioService.getEventsFromScenario(
-                scenarioService.unMarshallData(scenarioName)).entrySet()) {
-            stack.addFirst(clientEvent.getValue());
+        for (ClientEvent clientEvent : scenarioService.getEventsFromScenario(
+                scenarioService.unMarshallData(scenarioName))) {
+            stack.addFirst(clientEvent);
         }
         return stack;
     }
index 15dd637955ea1ae01a03c89e65d56e1d169838fa..7ef6ac4206e27873e3ef090e77c90c7af8ad1b43 100644 (file)
@@ -8,7 +8,7 @@
 package org.opendaylight.openflowjava.protocol.impl.clients;
 
 import java.io.IOException;
-import java.util.SortedMap;
+import java.util.List;
 import javax.xml.bind.JAXBException;
 import org.xml.sax.SAXException;
 
@@ -32,6 +32,6 @@ interface ScenarioService {
      */
     Scenario unMarshallData(String scenarioName) throws SAXException, JAXBException;
 
-    SortedMap<Integer, ClientEvent> getEventsFromScenario(Scenario scenario) throws IOException;
+    List<ClientEvent> getEventsFromScenario(Scenario scenario) throws IOException;
 
 }
index 703e97b508644a8de90c1b603102e2bafd16ec9f..8f400f6973ac664cbe6dbe4cce1038eba45386c1 100644 (file)
@@ -10,8 +10,8 @@ package org.opendaylight.openflowjava.protocol.impl.clients;
 import com.google.common.base.Preconditions;
 import java.io.File;
 import java.io.IOException;
-import java.util.SortedMap;
-import java.util.TreeMap;
+import java.util.ArrayList;
+import java.util.List;
 import javax.xml.XMLConstants;
 import javax.xml.bind.JAXBContext;
 import javax.xml.bind.JAXBException;
@@ -72,22 +72,22 @@ public class ScenarioServiceImpl implements ScenarioService {
     }
 
     @Override
-    public SortedMap<Integer, ClientEvent> getEventsFromScenario(Scenario scenario) throws IOException {
+    public List<ClientEvent> getEventsFromScenario(Scenario scenario) throws IOException {
         Preconditions.checkNotNull(scenario, "Scenario name not found. Check XML file, scenario name or directories.");
-        SortedMap<Integer, ClientEvent> events = new TreeMap<>();
-        Integer counter = 0;
-        for (Step step : scenario.getStep()) {
+        List<Step> steps = scenario.getStep();
+        List<ClientEvent> events = new ArrayList<>(steps.size());
+        for (Step step : steps) {
             LOG.debug("Step {}: {}, type {}, bytes {}", step.getOrder(), step.getName(), step.getEvent().value(),
                     step.getBytes().toArray());
             switch (step.getEvent()) {
                 case SLEEP_EVENT:
-                    events.put(counter++, new SleepEvent(1000));
+                    events.add(new SleepEvent(1000));
                     break;
                 case SEND_EVENT:
-                    events.put(counter++, new SendEvent(ByteBufUtils.serializeList(step.getBytes())));
+                    events.add(new SendEvent(ByteBufUtils.serializeList(step.getBytes())));
                     break;
                 case WAIT_FOR_MESSAGE_EVENT:
-                    events.put(counter++, new WaitForMessageEvent(ByteBufUtils.serializeList(step.getBytes())));
+                    events.add(new WaitForMessageEvent(ByteBufUtils.serializeList(step.getBytes())));
                     break;
                 default:
                     break;