import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
+import java.util.LinkedList;
+import java.util.List;
import java.util.Map;
import java.util.ServiceConfigurationError;
import java.util.ServiceLoader;
Map<String, Actor> map = new HashMap<>();
- Iterator<Actor> iter = loadActors().iterator();
- while (iter.hasNext()) {
-
- Actor newActor;
- try {
- newActor = iter.next();
- } catch (ServiceConfigurationError e) {
- logger.warn("unable to load actor", e);
- continue;
- }
-
+ for (Actor newActor : buildList()) {
map.compute(newActor.getName(), (name, existingActor) -> {
if (existingActor == null) {
return newActor;
name2actor = ImmutableMap.copyOf(map);
}
+ /**
+ * Builds the list of actors, discarding those that cannot be constructed.
+ *
+ * @return the list of actors, sorted by ascending sequence number
+ */
+ private List<Actor> buildList() {
+ List<Actor> actors = new LinkedList<>();
+
+ Iterator<Actor> iter = loadActors().iterator();
+ while (iter.hasNext()) {
+ try {
+ actors.add(iter.next());
+ } catch (ServiceConfigurationError e) {
+ logger.warn("unable to load actor", e);
+ }
+ }
+
+ actors.sort((actor1, actor2) -> {
+ int cmp = Integer.compare(actor1.getSequenceNumber(), actor2.getSequenceNumber());
+ if (cmp != 0) {
+ return cmp;
+ }
+
+ return actor1.getClass().getName().compareTo(actor2.getClass().getName());
+ });
+
+ return actors;
+ }
+
/**
* Gets a particular actor.
*
* @author pameladragosh
*
*/
-public interface Actor extends Startable, Configurable<Map<String,Object>> {
+public interface Actor extends Startable, Configurable<Map<String, Object>> {
/**
* Gets the name of the actor.
*/
public Set<String> getOperationNames();
+ /**
+ * Gets the actor sequence number. Lower numbered actors take precedence over higher
+ * numbered actors.
+ *
+ * @return the actor sequence number
+ */
+ public int getSequenceNumber();
+
// TODO old code: remove lines down to **HERE**
}
@Test
- public void testActorService() {
+ public void testActorService_testBuildList() {
/*
* make a service where actors two and four have names that are duplicates of the
* others
*/
+
+ /*
+ * actor0 has a higher sequence number than actor1, so it should be discarded,
+ * even though it will be examined first
+ */
+ Actor actor0 = spy(new ActorImpl(ACTOR1) {
+ @Override
+ public int getSequenceNumber() {
+ return 10000;
+ }
+ });
+
actor2 = spy(new ActorImpl(ACTOR1));
actor4 = spy(new ActorImpl(ACTOR3));
- service = makeService(actor1, actor2, actor3, actor4);
+ service = makeService(actor0, actor1, actor2, actor3, actor4);
assertEquals(2, service.getActorNames().size());