Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[util][docs] Add scope of identifiers.
see #639 Signed-off-by: Stéphane Galland <galland@arakhne.org>
- Loading branch information
1 parent
54f1c97
commit dd1652a
Showing
8 changed files
with
274 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
main/apiplugins/io.sarl.util/src/io/sarl/util/IdentifierScope.sarl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/* | ||
* $Id$ | ||
* | ||
* SARL is an general-purpose agent programming language. | ||
* More details on http://www.sarl.io | ||
* | ||
* Copyright (C) 2014-2017 the original authors or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.sarl.util | ||
|
||
import java.util.Arrays | ||
import java.util.UUID | ||
|
||
import io.sarl.lang.core.Address | ||
import io.sarl.lang.core.Scope | ||
|
||
/** | ||
* Scope using {@link Address} for EventSpace's. | ||
* | ||
* @author $Author: srodriguez$ | ||
* @version $FullVersion$ | ||
* @mavengroupid $GroupId$ | ||
* @mavenartifactid $ArtifactId$ | ||
* @since 0.6 | ||
*/ | ||
class IdentifierScope implements Scope<Address> { | ||
|
||
static val SCOPE_ID = "id://" | ||
|
||
static val NO_IDENTIFIER : UUID[] = newArrayOfSize(0) | ||
|
||
val identifiers : UUID[] | ||
|
||
/** | ||
* @param ids - identifiers to put in the scope. | ||
*/ | ||
new (ids : UUID*) { | ||
if (ids === null) { | ||
this.identifiers = NO_IDENTIFIER | ||
} else { | ||
this.identifiers = ids | ||
} | ||
} | ||
|
||
/** Replies the identifiers that are matched by this scope. | ||
* | ||
* @return the identifiers. | ||
*/ | ||
def getIdentifiers : UUID[] { | ||
this.identifiers | ||
} | ||
|
||
override toString : String { | ||
SCOPE_ID + Arrays::toString(this.identifiers) | ||
} | ||
|
||
override matches(address : Address) : boolean { | ||
assert address !== null | ||
// TODO Do dichotomic search. | ||
for (element : this.identifiers) { | ||
if (element == address.UUID) { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
tests/io.sarl.util.tests/src/test/java/io/sarl/util/tests/util/IdentifierScopeTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/* | ||
* $Id$ | ||
* | ||
* SARL is an general-purpose agent programming language. | ||
* More details on http://www.sarl.io | ||
* | ||
* Copyright (C) 2014-2017 the original authors or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.sarl.util.tests.util; | ||
|
||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertTrue; | ||
import static org.mockito.Mockito.when; | ||
|
||
import java.util.UUID; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.mockito.Mock; | ||
import org.mockito.MockitoAnnotations; | ||
|
||
import io.sarl.lang.core.Address; | ||
import io.sarl.tests.api.AbstractSarlTest; | ||
import io.sarl.tests.api.ManualMocking; | ||
import io.sarl.tests.api.Nullable; | ||
import io.sarl.util.IdentifierScope; | ||
|
||
/** | ||
* @author $Author: sgalland$ | ||
* @version $FullVersion$ | ||
* @mavengroupid $GroupId$ | ||
* @mavenartifactid $ArtifactId$ | ||
*/ | ||
@SuppressWarnings("all") | ||
@ManualMocking | ||
public class IdentifierScopeTest extends AbstractSarlTest { | ||
|
||
@Nullable | ||
private UUID base1; | ||
|
||
@Nullable | ||
private UUID base2; | ||
|
||
@Mock | ||
private Address adr1; | ||
|
||
@Mock | ||
private Address adr2; | ||
|
||
@Mock | ||
private Address adr3; | ||
|
||
private IdentifierScope scope; | ||
|
||
@Before | ||
public void setUp() { | ||
this.base1 = UUID.randomUUID(); | ||
this.base2 = UUID.randomUUID(); | ||
MockitoAnnotations.initMocks(this); | ||
when(this.adr1.getUUID()).thenReturn(this.base1); | ||
when(this.adr2.getUUID()).thenReturn(this.base2); | ||
when(this.adr3.getUUID()).thenReturn(UUID.randomUUID()); | ||
this.scope = new IdentifierScope(this.base1, this.base2); | ||
} | ||
|
||
@Test | ||
public void matches() { | ||
assertTrue(this.scope.matches(this.adr1)); | ||
assertTrue(this.scope.matches(this.adr2)); | ||
assertFalse(this.scope.matches(this.adr3)); | ||
} | ||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters