Skip to content

Commit

Permalink
[all] Replace "ConcurrentHashMap" class by "ConcurrentMap" interface …
Browse files Browse the repository at this point in the history
…in public API.

Signed-off-by: Stéphane Galland <galland@arakhne.org>
  • Loading branch information
gallandarakhneorg committed Dec 15, 2020
1 parent da48d2c commit b679d47
Show file tree
Hide file tree
Showing 13 changed files with 51 additions and 47 deletions.
Expand Up @@ -24,6 +24,7 @@
import java.lang.reflect.Constructor;
import java.security.InvalidParameterException;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

import com.google.common.reflect.TypeToken;
import org.eclipse.xtext.xbase.lib.Inline;
Expand All @@ -41,7 +42,7 @@ public abstract class AbstractSkillContainer extends AgentProtectedAPIObject imp

/** Skill repository.
*/
private final ConcurrentHashMap<Class<? extends Capacity>, AtomicSkillReference> skillRepository = new ConcurrentHashMap<>();
private final ConcurrentMap<Class<? extends Capacity>, AtomicSkillReference> skillRepository = new ConcurrentHashMap<>();

private DynamicSkillProvider skillProvider;

Expand Down Expand Up @@ -81,7 +82,7 @@ public AbstractSkillContainer(
*
* @return the skill repository.
*/
final ConcurrentHashMap<Class<? extends Capacity>, AtomicSkillReference> $getSkillRepository() {
final ConcurrentMap<Class<? extends Capacity>, AtomicSkillReference> $getSkillRepository() {
return this.skillRepository;
}

Expand Down
Expand Up @@ -26,8 +26,8 @@ import io.sarl.sre.internal.MutableBoolean
import io.sarl.sre.internal.ObjectComparator
import java.util.Set
import java.util.TreeSet
import java.util.concurrent.ConcurrentHashMap
import java.util.concurrent.ConcurrentLinkedDeque
import java.util.concurrent.ConcurrentMap
import java.util.concurrent.ConcurrentSkipListSet
import java.util.concurrent.CopyOnWriteArraySet

Expand All @@ -52,7 +52,7 @@ abstract class AbstractBehaviorGuardEvaluatorRegistry<T> implements IBehaviorGua
* @return the internal data structure
*/
@Pure
abstract def getInternalDataStructure : ConcurrentHashMap<Class<? extends Event>, CopyOnWriteArraySet<T>>
abstract def getInternalDataStructure : ConcurrentMap<Class<? extends Event>, CopyOnWriteArraySet<T>>

/** Replies the mapper from the internal guard evaluator description to the public description.
*
Expand Down
Expand Up @@ -33,6 +33,7 @@ import java.util.concurrent.CopyOnWriteArraySet

import static extension io.sarl.lang.core.SREutils.*
import java.util.Comparator
import java.util.concurrent.ConcurrentMap

/**
* Registry of all {@code IBehaviorGuardEvaluator}.
Expand Down Expand Up @@ -66,7 +67,7 @@ class PolymorphicBehaviorGuardEvaluatorRegistry extends AbstractBehaviorGuardEva
* <p>The {@link CopyOnWriteArraySet} values make it easy and relatively lightweight to get an immutable snapshot of all current
* receivers to an event without any locking.
*/
val evaluators : ConcurrentHashMap<Class<? extends Event>, CopyOnWriteArraySet<PolymorphicBehaviorGuardEvaluator>>
val evaluators : ConcurrentMap<Class<? extends Event>, CopyOnWriteArraySet<PolymorphicBehaviorGuardEvaluator>>

/**
* Instanciates a new registry linked with the {@link PerceptGuardEvaluator} annotation.
Expand All @@ -84,7 +85,7 @@ class PolymorphicBehaviorGuardEvaluatorRegistry extends AbstractBehaviorGuardEva
* @return the internal data structure
*/
@Pure
override getInternalDataStructure : ConcurrentHashMap<Class<? extends Event>, CopyOnWriteArraySet<PolymorphicBehaviorGuardEvaluator>> {
override getInternalDataStructure : ConcurrentMap<Class<? extends Event>, CopyOnWriteArraySet<PolymorphicBehaviorGuardEvaluator>> {
this.evaluators
}

Expand Down
Expand Up @@ -35,6 +35,7 @@ import java.util.concurrent.CopyOnWriteArraySet
import org.eclipse.xtend.lib.annotations.Data

import static extension io.sarl.sre.internal.eventguard.reflect.StaticReflectBehaviorGuardEvaluatorDictionary.*
import java.util.concurrent.ConcurrentMap

/**
* Registry of all {@code BehaviorGuardEvaluator} classes containing a method to evaluate the guard of a
Expand All @@ -55,7 +56,7 @@ class ReflectBehaviorGuardEvaluatorRegistry extends AbstractBehaviorGuardEvaluat
* <p>The {@link CopyOnWriteArraySet} values make it easy and relatively lightweight to get an immutable snapshot of all current
* {@code BehaviorGuardEvaluator}s to an event without any locking.
*/
val evaluators : ConcurrentHashMap<Class<? extends Event>, CopyOnWriteArraySet<GuardedEvaluator>>
val evaluators : ConcurrentMap<Class<? extends Event>, CopyOnWriteArraySet<GuardedEvaluator>>

/**
* Instanciates a new registry linked with the {@link PerceptGuardEvaluator} annotation.
Expand All @@ -69,7 +70,7 @@ class ReflectBehaviorGuardEvaluatorRegistry extends AbstractBehaviorGuardEvaluat
}

@Pure
override getInternalDataStructure : ConcurrentHashMap<Class<? extends Event>, CopyOnWriteArraySet<GuardedEvaluator>> {
override getInternalDataStructure : ConcurrentMap<Class<? extends Event>, CopyOnWriteArraySet<GuardedEvaluator>> {
this.evaluators
}

Expand Down
Expand Up @@ -28,9 +28,9 @@ import io.sarl.core.MemberLeft
import io.sarl.lang.core.Agent
import io.sarl.lang.core.AgentContext
import io.sarl.lang.core.SpaceID
import java.util.Collection
import java.util.EventListener
import java.util.UUID
import java.util.concurrent.ConcurrentLinkedDeque

/**
* This service enables to store the contexts and to manage the spaces in the SRE platform.
Expand Down Expand Up @@ -71,17 +71,19 @@ interface ContextService extends Service {
def createContext(contextID : UUID, defaultSpaceID : UUID, owner : Agent) : Context

/**
* Replies an existing context instance.
* Replies an existing context instance. This function does not create the instance of the context
* if it was not created.
*
* @param contextID the id of the context to create
* @return the context, {@code null}.
*/
def getContext(contextID : UUID) : Context

/**
* Remove the context instance.
*
* @param contextID the id of the context to remove
* @return the removed context, {@code null}.
* @return the removed context, {@code null} if no context with the given ID was found.
*/
def removeContext(contextID : UUID) : Context

Expand All @@ -91,7 +93,7 @@ interface ContextService extends Service {
* @return a view on all the contexts.
* @since 0.10
*/
def getAllContexts : ConcurrentLinkedDeque<Context>
def getAllContexts : Collection<Context>

}

Expand Down
Expand Up @@ -45,7 +45,7 @@ import io.sarl.sre.services.logging.LoggingService
*/
class LocalSpaceRepository extends SpaceRepository {

val spaces : ConcurrentHashMap<UUID, SpaceDescription>
val spaces : ConcurrentMap<UUID, SpaceDescription>

/** Constructor.
*
Expand Down Expand Up @@ -76,7 +76,7 @@ class LocalSpaceRepository extends SpaceRepository {
new (injector : Injector, executor : ExecutorService,
loggingService : LoggingService, config : SreConfig,
specificationFactory : SreSpaceSpecificationFactory,
internalStructure : ConcurrentHashMap<UUID, SpaceDescription>,
internalStructure : ConcurrentMap<UUID, SpaceDescription>,
listenerCollection : SmartListenerCollection<?>) {
super(injector, executor, loggingService, config, specificationFactory, listenerCollection)
if (internalStructure !== null) {
Expand All @@ -91,7 +91,7 @@ class LocalSpaceRepository extends SpaceRepository {
}

protected def existsSpace(spaceUUID : UUID) : boolean {
this.spaces.contains(spaceUUID)
this.spaces.containsKey(spaceUUID)
}

protected def removeSpaceIfEmpty(spaceUUID : UUID) {
Expand Down
Expand Up @@ -23,9 +23,9 @@ package io.sarl.sre.services.context
import com.google.inject.Injector
import io.sarl.sre.KernelScope
import io.sarl.sre.services.logging.LoggingService
import java.util.Map
import java.util.TreeMap
import java.util.UUID
import java.util.concurrent.ConcurrentHashMap
import java.util.concurrent.ConcurrentLinkedDeque
import javax.inject.Inject

/**
Expand All @@ -44,7 +44,7 @@ import javax.inject.Inject
*/
class MemoryBasedContextService extends AbstractInjectionBasedContextService {

var contextRepository : ConcurrentHashMap<UUID, Context>
volatile var contextRepository : Map<UUID, Context>

/** Constructor.
*
Expand All @@ -59,21 +59,17 @@ class MemoryBasedContextService extends AbstractInjectionBasedContextService {
super(rootContext, logger, injector, factory)
}

protected def getContextInternalStructure : ConcurrentHashMap<UUID, Context> {
var repository : ConcurrentHashMap<UUID, Context> = this.contextRepository
protected override getContextInternalStructure : Map<UUID, Context> {
var repository = this.contextRepository

if (repository === null) {
repository = new ConcurrentHashMap<UUID, Context>
repository = new TreeMap<UUID, Context>
this.contextRepository = repository
}
return repository
}

def getAllContexts : ConcurrentLinkedDeque<Context> {
var all = new ConcurrentLinkedDeque<Context>
all.add(rootContext)
all.addAll(this.contextInternalStructure.values)
return all
override getAllContexts : Collection<Context> {
}

}
Expand Up @@ -72,7 +72,7 @@ final class AgentLife {

var innerSpaceAddress : Address

var externalContextInstances : ConcurrentHashMap<UUID, ContextReference>
var externalContextInstances : Map<UUID, ContextReference>

var defaultContextInstance : ContextReference

Expand Down
Expand Up @@ -53,6 +53,7 @@ import javax.inject.Inject
import static io.sarl.sre.services.lifecycle.AgentLife.*
import static io.sarl.sre.services.lifecycle.BehaviorLife.*
import static java.text.MessageFormat.*
import java.util.concurrent.ConcurrentMap

/**
* Skill that permits to execute tasks with an executor service.
Expand All @@ -73,7 +74,7 @@ skill SchedulesSkill extends Skill implements InternalSchedules {

val failureListenerFactory : FailureListenerFactory

var activeTaskRepository : ConcurrentHashMap<String, TaskDescription> = null
var activeTaskRepository : ConcurrentMap<String, TaskDescription> = null

/**
* JUG Time based generator used for naming tasks, enough for this kind of missions not for a string unique UUID
Expand Down Expand Up @@ -131,7 +132,7 @@ skill SchedulesSkill extends Skill implements InternalSchedules {
*
* <p>This function is not thread-safe.
*/
private def getRepository : ConcurrentHashMap<String, TaskDescription> {
private def getRepository : ConcurrentMap<String, TaskDescription> {
if (this.activeTaskRepository === null) {
this.activeTaskRepository = new ConcurrentHashMap<String,TaskDescription>
}
Expand Down
Expand Up @@ -37,6 +37,7 @@ import java.util.logging.Level
import java.util.logging.Logger
import javax.inject.Inject
import org.eclipse.xtend.lib.annotations.Accessors
import java.util.concurrent.ConcurrentMap

/**
* Abstract implementation of an event space.
Expand Down Expand Up @@ -70,9 +71,9 @@ abstract class AbstractEventSpace extends AbstractSpace implements EventSpace, S
@Accessors(PUBLIC_GETTER)
var spaceParticipantListener : SpaceParticipantListener

val strongRepository : ConcurrentHashMap<UUID, Participant>
val strongRepository : ConcurrentMap<UUID, Participant>

val weakRepository : ConcurrentHashMap<UUID, Participant>
val weakRepository : ConcurrentMap<UUID, Participant>

/** Constructor.
*
Expand All @@ -96,7 +97,7 @@ abstract class AbstractEventSpace extends AbstractSpace implements EventSpace, S
spaceId : SpaceID,
participantListener : SpaceParticipantListener,
loggingService : LoggingService,
strongRepository : ConcurrentHashMap<UUID, Participant>,
strongRepository : ConcurrentMap<UUID, Participant>,
weakRepository : ConcurrentHashMap<UUID, Participant>) {
super(spaceId)
this.spaceParticipantListener = participantListener
Expand Down Expand Up @@ -238,12 +239,12 @@ abstract class AbstractEventSpace extends AbstractSpace implements EventSpace, S
scopedParticipants += this.weakRepository.values
} else {
// Scope => add the strong and weak participants that are matching the scope
this.strongRepository.forEachValue(1) [element |
this.strongRepository.forEach [key, element |
if (scope.matches(element.address)) {
scopedParticipants += element
}
]
this.weakRepository.forEachValue(1) [ element |
this.weakRepository.forEach [ key, element |
if (scope.matches(element.address)) {
scopedParticipants += element
}
Expand Down
Expand Up @@ -26,8 +26,15 @@ import io.sarl.core.Initialize
import io.sarl.lang.core.Agent
import io.sarl.lang.core.Behavior
import io.sarl.lang.core.Event
import io.sarl.sre.internal.eventguard.polymorphic.PolymorphicBehaviorGuardEvaluator
import io.sarl.sre.internal.eventguard.polymorphic.PolymorphicBehaviorGuardEvaluatorRegistry
import io.sarl.sre.tests.units.internal.eventguard.polymorphic.mocks.TestAgent0
import io.sarl.sre.tests.units.internal.eventguard.polymorphic.mocks.TestAgent1
import io.sarl.sre.tests.units.internal.eventguard.polymorphic.mocks.TestAgent2
import io.sarl.sre.tests.units.internal.eventguard.polymorphic.mocks.TestEvent0
import io.sarl.tests.api.Nullable
import java.util.concurrent.ConcurrentHashMap
import java.util.UUID
import java.util.concurrent.ConcurrentMap
import java.util.concurrent.CopyOnWriteArraySet
import org.eclipse.xtext.xbase.lib.Functions.Function1
import org.eclipse.xtext.xbase.lib.Procedures.Procedure1
Expand All @@ -43,13 +50,6 @@ import static extension io.sarl.tests.api.tools.TestMockito.mock
import static extension org.junit.jupiter.api.Assertions.*
import static extension org.mockito.ArgumentCaptor.*
import static extension org.mockito.Mockito.*
import io.sarl.sre.internal.eventguard.polymorphic.PolymorphicBehaviorGuardEvaluatorRegistry
import io.sarl.sre.internal.eventguard.polymorphic.PolymorphicBehaviorGuardEvaluator
import io.sarl.sre.tests.units.internal.eventguard.polymorphic.mocks.TestAgent1
import io.sarl.sre.tests.units.internal.eventguard.polymorphic.mocks.TestEvent0
import io.sarl.sre.tests.units.internal.eventguard.polymorphic.mocks.TestAgent2
import io.sarl.sre.tests.units.internal.eventguard.polymorphic.mocks.TestAgent0
import java.util.UUID

/**
* @author $Author: sgalland$
Expand All @@ -68,7 +68,7 @@ class PolymorphicBehaviorGuardEvaluatorRegistryTest {
var registry : PolymorphicBehaviorGuardEvaluatorRegistry

@Nullable
var internalStructure : ConcurrentHashMap<Class<? extends Event>, CopyOnWriteArraySet<PolymorphicBehaviorGuardEvaluator>>
var internalStructure : ConcurrentMap<Class<? extends Event>, CopyOnWriteArraySet<PolymorphicBehaviorGuardEvaluator>>

@BeforeEach
def setUp : void {
Expand Down
Expand Up @@ -34,7 +34,7 @@ import io.sarl.sre.tests.units.internal.eventguard.reflect.mocks.TestAgent1
import io.sarl.sre.tests.units.internal.eventguard.reflect.mocks.TestAgent2
import io.sarl.sre.tests.units.internal.eventguard.reflect.mocks.TestEvent0
import io.sarl.tests.api.Nullable
import java.util.concurrent.ConcurrentHashMap
import java.util.concurrent.ConcurrentMap
import java.util.concurrent.CopyOnWriteArraySet
import org.eclipse.xtext.xbase.lib.Functions.Function1
import org.eclipse.xtext.xbase.lib.Procedures.Procedure1
Expand Down Expand Up @@ -68,7 +68,7 @@ class ReflectBehaviorGuardEvaluatorRegistryTest {
var registry : ReflectBehaviorGuardEvaluatorRegistry

@Nullable
var internalStructure : ConcurrentHashMap<Class<? extends Event>, CopyOnWriteArraySet<GuardedEvaluator>>
var internalStructure : ConcurrentMap<Class<? extends Event>, CopyOnWriteArraySet<GuardedEvaluator>>

@BeforeEach
def setUp : void {
Expand Down
Expand Up @@ -56,6 +56,7 @@ import static org.junit.jupiter.api.Assertions.*

import static extension org.junit.jupiter.api.Assertions.assertEquals
import io.sarl.api.bootiquebase.config.LogConfig
import java.util.concurrent.ConcurrentMap

/**
* Context for running the SRE. This context is thread-safe.
Expand Down Expand Up @@ -219,7 +220,7 @@ class SreRunContext {
* Replies all the results for all the agents.
* @return the results.
*/
def getAllResultsPerAgent : ConcurrentHashMap<UUID, CopyOnWriteArrayList<Object>> {
def getAllResultsPerAgent : ConcurrentMap<UUID, CopyOnWriteArrayList<Object>> {
return globalResults
}

Expand Down Expand Up @@ -497,7 +498,7 @@ class SreRunContext {
* @throws Exception - if the kernel cannot be launched.
*/
@SuppressWarnings("discouraged_reference")
def waitForTheKernel(timeout : int, predicate : (ConcurrentHashMap<UUID, CopyOnWriteArrayList<Object>>)=>boolean) {
def waitForTheKernel(timeout : int, predicate : (ConcurrentMap<UUID, CopyOnWriteArrayList<Object>>)=>boolean) {
val th = Thread::currentThread
val oldName = th.name
try {
Expand Down

0 comments on commit b679d47

Please sign in to comment.