Skip to content

Commit

Permalink
[lang][core] Add setSkill(Skill,Capacities[]).
Browse files Browse the repository at this point in the history
close #210

Signed-off-by: Stéphane Galland <galland@arakhne.org>
  • Loading branch information
gallandarakhneorg committed Jul 8, 2016
1 parent 21e14f5 commit c023168
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 41 deletions.
Expand Up @@ -26,6 +26,7 @@
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;

import org.eclipse.xtext.xbase.lib.Inline;
import org.eclipse.xtext.xbase.lib.Pure;

import io.sarl.lang.SARLVersion;
Expand Down Expand Up @@ -119,20 +120,39 @@ public UUID getID() {
* @param capacity capacity to set.
* @param skill implementaion of <code>capacity</code>.
* @return the skill that was set.
* @deprecated since 0.4, see {@link #setSkill(Skill, Class...)}
*/
@Inline("setSkill($2, $1)")
@Deprecated
protected <S extends Skill> S setSkill(Class<? extends Capacity> capacity, S skill) {
assert capacity != null : "the capacity parameter must not be null"; //$NON-NLS-1$
assert capacity.isInterface() : "the capacity parameter must be an interface"; //$NON-NLS-1$
return setSkill(skill, capacity);
}

/**
* Set the skill for the {@link Capacity} <code>capacity</code>.
*
* @param <S> - type of the skill.
* @param capacities the capacity or the capacities to set.
* @param skill implementaion of <code>capacity</code>.
* @return the skill that was set.
* @since 0.4
*/
@SafeVarargs
protected final <S extends Skill> S setSkill(S skill, Class<? extends Capacity>... capacities) {
assert skill != null : "the skill parameter must not be null"; //$NON-NLS-1$
if (!capacity.isInstance(skill)) {
throw new InvalidParameterException(
"the skill must implement the given capacity " //$NON-NLS-1$
+ capacity.getName());
}
skill.setOwner(this);
final Skill oldS = this.capacities.put(capacity, skill);
if (oldS != null) {
oldS.uninstall();
for (final Class<? extends Capacity> capacity : capacities) {
assert capacity != null : "the capacity parameter must not be null"; //$NON-NLS-1$
assert capacity.isInterface() : "the capacity parameter must be an interface"; //$NON-NLS-1$
if (!capacity.isInstance(skill)) {
throw new InvalidParameterException(
"the skill must implement the given capacity " //$NON-NLS-1$
+ capacity.getName());
}
final Skill oldS = this.capacities.put(capacity, skill);
if (oldS != null) {
oldS.uninstall();
}
}
skill.install();
return skill;
Expand All @@ -145,7 +165,7 @@ protected <S extends Skill> S setSkill(Class<? extends Capacity> capacity, S ski
* @param skill - the skill to be mapped to.
*/
protected <S extends Skill> void operator_mappedTo(Class<? extends Capacity> capacity, S skill) {
setSkill(capacity, skill);
setSkill(skill, capacity);
}

/**
Expand Down
Expand Up @@ -112,7 +112,7 @@ protected <S extends Capacity> S getSkill(Class<S> capacity) {
* @param skill - the skill to associate to the capacity.
*/
protected <S extends Skill & Capacity> void operator_mappedTo(Class<? extends Capacity> capacity, S skill) {
getOwner().setSkill(capacity, skill);
getOwner().setSkill(skill, capacity);
}

}
Expand Up @@ -50,6 +50,7 @@
* @mavengroupid $GroupId$
* @mavenartifactid $ArtifactId$
*/
@SuppressWarnings("all")
public class AgentTest extends AbstractSarlTest {

@NonNullByDefault
Expand Down Expand Up @@ -126,47 +127,47 @@ public void setSkill() {
assertNoSkill(Skill2.class);

s1 = new Skill1();
r = this.agent.setSkill(Capacity1.class, s1);
r = this.agent.setSkill_Fake(s1, Capacity1.class);
assertSame(s1, r);
assertSkill(Capacity1.class, s1);
assertNoSkill(Capacity2.class);
assertNoSkill(Skill1.class);
assertNoSkill(Skill2.class);

try {
this.agent.setSkill(Capacity1.class, new Skill2());
this.agent.setSkill_Fake(new Skill2(), Capacity1.class);
fail("Expecting the exception InvalidParameterException, but got no exception."); //$NON-NLS-1$
}
catch(InvalidParameterException exception) {
//
}

try {
this.agent.setSkill(Capacity1.class, new Skill3());
this.agent.setSkill_Fake(new Skill3(), Capacity1.class);
fail("Expecting the exception InvalidParameterException, but got no exception."); //$NON-NLS-1$
}
catch(InvalidParameterException exception) {
//
}

s2 = new Skill2();
r = this.agent.setSkill(Capacity2.class, s2);
r = this.agent.setSkill_Fake(s2, Capacity2.class);
assertSame(s2, r);
assertSkill(Capacity1.class, s1);
assertSkill(Capacity2.class, s2);
assertNoSkill(Skill1.class);
assertNoSkill(Skill2.class);

try {
this.agent.setSkill(Capacity2.class, new Skill1());
this.agent.setSkill_Fake(new Skill1(), Capacity2.class);
fail("Expecting the exception InvalidParameterException, but got no exception."); //$NON-NLS-1$
}
catch(InvalidParameterException exception) {
//
}

try {
this.agent.setSkill(Capacity2.class, new Skill3());
this.agent.setSkill_Fake(new Skill3(), Capacity2.class);
fail("Expecting the exception InvalidParameterException, but got no exception."); //$NON-NLS-1$
}
catch(InvalidParameterException exception) {
Expand All @@ -178,8 +179,8 @@ public void setSkill() {
*/
@Test
public void clearSkill() {
this.agent.setSkill(Capacity1.class, new Skill1());
this.agent.setSkill(Capacity2.class, new Skill2());
this.agent.setSkill_Fake(new Skill1(), Capacity1.class);
this.agent.setSkill_Fake(new Skill2(), Capacity2.class);
assertSkill(Capacity1.class);
assertSkill(Capacity2.class);
assertNoSkill(Skill1.class);
Expand Down Expand Up @@ -212,8 +213,8 @@ public void clearSkill() {
*/
@Test
public void hasSkill() {
this.agent.setSkill(Capacity1.class, new Skill1());
this.agent.setSkill(Capacity2.class, new Skill2());
this.agent.setSkill_Fake(new Skill1(), Capacity1.class);
this.agent.setSkill_Fake(new Skill2(), Capacity2.class);
assertTrue(this.agent.hasSkill(Capacity1.class));
assertTrue(this.agent.hasSkill(Capacity2.class));

Expand Down Expand Up @@ -355,58 +356,41 @@ public AgentMock(UUID parentID) {
super(mock(BuiltinCapacitiesProvider.class), parentID, null);
}

/** {@inheritDoc}
*/
@Override
public <S extends Skill> S setSkill(Class<? extends Capacity> capacity, S skill) {
return super.setSkill(capacity, skill);
public <S extends Skill> S setSkill_Fake(S skill, Class<? extends Capacity>... capacity) {
return setSkill(skill, capacity);
}

/** {@inheritDoc}
*/
@Override
public <S extends Capacity> S getSkill(Class<S> capacity) {
return super.getSkill(capacity);
}

/** {@inheritDoc}
*/
@Override
public <S extends Capacity> S clearSkill(Class<S> capacity) {
return super.clearSkill(capacity);
}

/** {@inheritDoc}
*/
@Override
public boolean hasSkill(Class<? extends Capacity> capacity) {
return super.hasSkill(capacity);
}

/** {@inheritDoc}
*/
@Override
public <S extends Skill> void operator_mappedTo(
Class<? extends Capacity> capacity, S skill) {
super.operator_mappedTo(capacity, skill);
}

/** {@inheritDoc}
*/
@Override
public boolean isMe(Address address) {
return super.isMe(address);
}

/** {@inheritDoc}
*/
@Override
public boolean isMe(UUID id) {
return super.isMe(id);
}

/** {@inheritDoc}
*/
@Override
protected boolean isFromMe(Event event) {
return super.isFromMe(event);
Expand Down

0 comments on commit c023168

Please sign in to comment.