Skip to content

Commit

Permalink
[lang] Replace collection interfaces by interfaces that enable faster…
Browse files Browse the repository at this point in the history
… implementation.

Signed-off-by: Stéphane Galland <galland@arakhne.org>
  • Loading branch information
gallandarakhneorg committed Aug 31, 2017
1 parent cfe0db3 commit 6cd3cdd
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 34 deletions.
14 changes: 7 additions & 7 deletions main/apiplugins/io.sarl.core/src/io/sarl/core/bic.sarl
Expand Up @@ -21,7 +21,6 @@
package io.sarl.core

import java.text.MessageFormat
import java.util.Collection
import java.util.UUID
import java.util.concurrent.TimeUnit
import java.util.Collection
Expand All @@ -40,6 +39,7 @@ import io.sarl.lang.core.Space
import io.sarl.lang.core.SpaceID
import io.sarl.lang.util.SynchronizedCollection
import io.sarl.lang.util.SynchronizedSet
import io.sarl.lang.util.SynchronizedIterable

/**
* Provides functions for accessing and managing the external contexts of an agent.
Expand All @@ -52,7 +52,7 @@ capacity ExternalContextAccess {
* @return the list of the agent contexts.
*/
@Pure
def getAllContexts : SynchronizedCollection<AgentContext>
def getAllContexts : SynchronizedIterable<AgentContext>

/**
* Replies the AgentContext for the given ID.
Expand Down Expand Up @@ -180,7 +180,7 @@ capacity InnerContextAccess {
* @return the identifiers of the members of the internal context of the current agent.
*/
@Pure
def getMemberAgents : SynchronizedSet<UUID>
def getMemberAgents : SynchronizedIterable<UUID>

/** Replies if the given space is the default space of the inner context.
*
Expand Down Expand Up @@ -248,7 +248,7 @@ capacity Behaviors {
* @return the unmodifiable collection of the registered behavior.
* @since 0.5
*/
def getRegisteredBehaviors : Collection<Behavior>
def getRegisteredBehaviors : SynchronizedIterable<Behavior>

/**
* Register a Behavior for the owner agent.
Expand Down Expand Up @@ -328,7 +328,7 @@ capacity Lifecycle {
* @fires AgentSpawned in DefaultSpace
* @since 0.5
*/
def spawn(nbAgents : int, agentType : Class<? extends Agent>, params : Object*) : Collection<UUID> fires AgentSpawned
def spawn(nbAgents : int, agentType : Class<? extends Agent>, params : Object*) : Iterable<UUID> fires AgentSpawned

/**
* Spawns a new member agent in the parent's context (parentID).
Expand All @@ -353,7 +353,7 @@ capacity Lifecycle {
* @fires AgentSpawned inside the default context of the parent. The source of the event is this agent.
* @since 0.5
*/
def spawnInContext(nbAgents : int, agentClass : Class <? extends Agent>, context : AgentContext, params : Object*) : Collection<UUID> fires AgentSpawned
def spawnInContext(nbAgents : int, agentClass : Class <? extends Agent>, context : AgentContext, params : Object*) : Iterable<UUID> fires AgentSpawned

/**
* Spawns a new member agent in the parent's context (parentID).
Expand Down Expand Up @@ -393,7 +393,7 @@ capacity Schedules {
* @return the names of the active tasks.
* @since 0.5
*/
def getActiveTasks : Collection<String>
def getActiveTasks : SynchronizedSet<String>

/**
* Schedule a given task to be executed after the specified delay.
Expand Down
Expand Up @@ -38,6 +38,8 @@
import io.sarl.lang.core.Scope;
import io.sarl.lang.core.Skill;
import io.sarl.lang.util.ClearableReference;
import io.sarl.lang.util.SynchronizedIterable;
import io.sarl.util.Collections3;

/**
* Janus implementation of SARL's {@link Behaviors} built-in capacity.
Expand Down Expand Up @@ -117,10 +119,10 @@ public boolean hasRegisteredBehavior() {
}

@Override
public Collection<Behavior> getRegisteredBehaviors() {
public SynchronizedIterable<Behavior> getRegisteredBehaviors() {
final Collection<Behavior> behaviors = new ArrayList<>();
getSkill(InternalEventBusCapacity.class).getRegisteredEventListeners(Behavior.class, behaviors);
return behaviors;
return Collections3.unmodifiableSynchronizedIterable(behaviors, behaviors);
}

@Override
Expand Down
Expand Up @@ -27,7 +27,7 @@
import io.sarl.core.InnerContextAccess;
import io.sarl.lang.core.Agent;
import io.sarl.lang.core.AgentContext;
import io.sarl.lang.util.SynchronizedCollection;
import io.sarl.lang.util.SynchronizedIterable;

/**
* Utilities that are dedicated to the built-in capacities.
Expand Down Expand Up @@ -60,7 +60,7 @@ private static Method getMethodGetSkill() throws Exception {
* @return the contexts of the agents.
* @throws Exception - when it is not possible to retreive the contexts.
*/
public static SynchronizedCollection<AgentContext> getContextsOf(Agent agent) throws Exception {
public static SynchronizedIterable<AgentContext> getContextsOf(Agent agent) throws Exception {
final Method method = getMethodGetSkill();
final ExternalContextAccess skill = (ExternalContextAccess) method.invoke(agent, ExternalContextAccess.class);
assert skill != null;
Expand Down
Expand Up @@ -39,6 +39,7 @@
import io.sarl.lang.core.Space;
import io.sarl.lang.core.SpaceID;
import io.sarl.lang.util.ClearableReference;
import io.sarl.lang.util.SynchronizedIterable;
import io.sarl.lang.util.SynchronizedSet;
import io.sarl.util.Collections3;
import io.sarl.util.OpenEventSpace;
Expand Down Expand Up @@ -176,7 +177,7 @@ public synchronized int getMemberAgentCount() {
}

@Override
public synchronized SynchronizedSet<UUID> getMemberAgents() {
public synchronized SynchronizedIterable<UUID> getMemberAgents() {
if (this.innerContext != null) {
final SynchronizedSet<UUID> participants = this.innerContext.getDefaultSpace().getParticipants();
assert participants != null;
Expand Down
Expand Up @@ -54,6 +54,8 @@
import io.sarl.lang.core.SREutils;
import io.sarl.lang.core.Skill;
import io.sarl.lang.util.ClearableReference;
import io.sarl.lang.util.SynchronizedSet;
import io.sarl.util.Collections3;

/**
* Skill that permits to execute tasks with an executor service.
Expand Down Expand Up @@ -138,10 +140,10 @@ private void finishTask(AgentTask task, boolean updateSkillReferences, boolean u
}

@Override
public Collection<String> getActiveTasks() {
public SynchronizedSet<String> getActiveTasks() {
synchronized (getTaskListMutex()) {
//TODO: Avoid copy of collection
return Lists.newArrayList(this.tasks.keySet());
return Collections3.unmodifiableSynchronizedSet(this.tasks.keySet(), getTaskListMutex());
}
}

Expand Down
Expand Up @@ -63,7 +63,7 @@
import io.sarl.lang.core.BuiltinCapacitiesProvider;
import io.sarl.lang.core.EventSpace;
import io.sarl.lang.core.Skill;
import io.sarl.lang.util.SynchronizedCollection;
import io.sarl.lang.util.SynchronizedIterable;
import io.sarl.lang.util.SynchronizedSet;
import io.sarl.sarlspecification.SarlSpecificationChecker;
import io.sarl.util.Collections3;
Expand Down Expand Up @@ -467,7 +467,7 @@ protected void fireAgentDestroyed(Agent agent) {
final SpawnServiceListener[] ilisteners2 = this.globalListeners.getListeners(SpawnServiceListener.class);

try {
final SynchronizedCollection<AgentContext> sc = BuiltinCapacityUtil.getContextsOf(agent);
final SynchronizedIterable<AgentContext> sc = BuiltinCapacityUtil.getContextsOf(agent);
synchronized (sc.mutex()) {
for (final AgentContext context : sc) {
final EventSpace defSpace = context.getDefaultSpace();
Expand Down
Expand Up @@ -25,8 +25,11 @@
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.UUID;

import org.junit.Before;
Expand All @@ -46,6 +49,7 @@
import io.sarl.lang.core.Skill;
import io.sarl.lang.util.ClearableReference;
import io.sarl.lang.util.SynchronizedCollection;
import io.sarl.lang.util.SynchronizedIterable;
import io.sarl.tests.api.Nullable;
import io.sarl.util.Collections3;

Expand Down Expand Up @@ -79,21 +83,26 @@ public void setUp() {
public void getContextsOf_emptycontextlist() throws Exception {
Mockito.when(this.contextSkill.getAllContexts())
.thenReturn(Collections3.synchronizedCollection(Collections.<AgentContext> emptyList(), this));
SynchronizedCollection<AgentContext> c = BuiltinCapacityUtil.getContextsOf(this.agent);
SynchronizedIterable<AgentContext> c = BuiltinCapacityUtil.getContextsOf(this.agent);
assertNotNull(c);
assertTrue(c.isEmpty());
assertFalse(c.iterator().hasNext());
}

@Test
public void getContextsOf_onecontext() throws Exception {
AgentContext context = mock(AgentContext.class);
Mockito.when(this.contextSkill.getAllContexts())
.thenReturn(Collections3.synchronizedCollection(Collections.singletonList(context), this));
SynchronizedCollection<AgentContext> c = BuiltinCapacityUtil.getContextsOf(this.agent);
SynchronizedIterable<AgentContext> c = BuiltinCapacityUtil.getContextsOf(this.agent);
assertNotNull(c);
assertFalse(c.isEmpty());
assertEquals(1, c.size());
assertTrue(c.contains(context));
List<AgentContext> list = new ArrayList<>();
Iterator<AgentContext> iterator = c.iterator();
while (iterator.hasNext()) {
list.add(iterator.next());
}
assertFalse(list.isEmpty());
assertEquals(1, list.size());
assertTrue(list.contains(context));
}

@Test
Expand All @@ -102,12 +111,17 @@ public void getContextsOf_twocontexts() throws Exception {
AgentContext context2 = mock(AgentContext.class);
Mockito.when(this.contextSkill.getAllContexts())
.thenReturn(Collections3.synchronizedCollection(Arrays.asList(context1, context2), this));
SynchronizedCollection<AgentContext> c = BuiltinCapacityUtil.getContextsOf(this.agent);
SynchronizedIterable<AgentContext> c = BuiltinCapacityUtil.getContextsOf(this.agent);
assertNotNull(c);
assertFalse(c.isEmpty());
assertEquals(2, c.size());
assertTrue(c.contains(context1));
assertTrue(c.contains(context2));
List<AgentContext> list = new ArrayList<>();
Iterator<AgentContext> iterator = c.iterator();
while (iterator.hasNext()) {
list.add(iterator.next());
}
assertFalse(list.isEmpty());
assertEquals(2, list.size());
assertTrue(list.contains(context1));
assertTrue(list.contains(context2));
}

@Test
Expand Down
Expand Up @@ -26,8 +26,11 @@
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.UUID;

import org.junit.Before;
Expand Down Expand Up @@ -59,7 +62,7 @@
import io.sarl.lang.core.Space;
import io.sarl.lang.core.SpaceID;
import io.sarl.lang.util.ClearableReference;
import io.sarl.lang.util.SynchronizedSet;
import io.sarl.lang.util.SynchronizedIterable;
import io.sarl.tests.api.ManualMocking;
import io.sarl.tests.api.Nullable;
import io.sarl.util.Collections3;
Expand Down Expand Up @@ -191,21 +194,26 @@ public void getMemberAgentCount_member() {

@Test
public void getMemberAgents_nomember() {
SynchronizedSet<UUID> set = this.skill.getMemberAgents();
SynchronizedIterable<UUID> set = this.skill.getMemberAgents();
assertNotNull(set);
assertTrue(set.isEmpty());
assertFalse(set.iterator().hasNext());
}

@Test
public void getMemberAgents_member() {
UUID otherAgent = UUID.randomUUID();
when(this.innerSpace.getParticipants())
.thenReturn(Collections3.synchronizedSet(new HashSet<>(Arrays.asList(this.agentId, otherAgent)), this));
SynchronizedSet<UUID> set = this.skill.getMemberAgents();
SynchronizedIterable<UUID> set = this.skill.getMemberAgents();
assertNotNull(set);
assertFalse(set.isEmpty());
assertEquals(1, set.size());
assertTrue(set.contains(otherAgent));
final List<UUID> list = new ArrayList<>();
Iterator<UUID> iterator = set.iterator();
while (iterator.hasNext()) {
list.add(iterator.next());
}
assertFalse(list.isEmpty());
assertEquals(1, list.size());
assertTrue(list.contains(otherAgent));
}

@Test(expected = NullPointerException.class)
Expand Down

0 comments on commit 6cd3cdd

Please sign in to comment.