Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
T
TeeTime-Framework
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Nelson Tavares de Sousa
TeeTime-Framework
Commits
4591ba39
Commit
4591ba39
authored
10 years ago
by
Nils Christian Ehmke
Browse files
Options
Downloads
Patches
Plain Diff
Implementation of the MultipleInstanceOfFilter.
parent
3868f2c9
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/main/java/teetime/stage/MultipleInstanceOfFilter.java
+34
-0
34 additions, 0 deletions
src/main/java/teetime/stage/MultipleInstanceOfFilter.java
src/test/java/teetime/stage/MultipleInstanceOfFilterTest.java
+72
-0
72 additions, 0 deletions
...test/java/teetime/stage/MultipleInstanceOfFilterTest.java
with
106 additions
and
0 deletions
src/main/java/teetime/stage/MultipleInstanceOfFilter.java
0 → 100644
+
34
−
0
View file @
4591ba39
package
teetime.stage
;
import
java.util.HashMap
;
import
java.util.Map
;
import
java.util.Map.Entry
;
import
teetime.framework.AbstractConsumerStage
;
import
teetime.framework.OutputPort
;
/**
* @author Nils Christian Ehmke
*/
public
final
class
MultipleInstanceOfFilter
<
I
>
extends
AbstractConsumerStage
<
I
>
{
private
final
Map
<
Class
<?
extends
I
>,
OutputPort
<?
super
I
>>
outputPortsMap
=
new
HashMap
<
Class
<?
extends
I
>,
OutputPort
<?
super
I
>>();
@SuppressWarnings
(
"unchecked"
)
public
<
T
extends
I
>
OutputPort
<
T
>
getOutputPortForType
(
final
Class
<
T
>
clazz
)
{
if
(!
this
.
outputPortsMap
.
containsKey
(
clazz
))
{
this
.
outputPortsMap
.
put
(
clazz
,
super
.
createOutputPort
());
}
return
(
OutputPort
<
T
>)
this
.
outputPortsMap
.
get
(
clazz
);
}
@Override
protected
void
execute
(
final
I
element
)
{
for
(
Entry
<
Class
<?
extends
I
>,
OutputPort
<?
super
I
>>
outputPortMapEntry
:
outputPortsMap
.
entrySet
())
{
if
(
outputPortMapEntry
.
getKey
().
isInstance
(
element
))
{
outputPortMapEntry
.
getValue
().
send
(
element
);
}
}
}
}
This diff is collapsed.
Click to expand it.
src/test/java/teetime/stage/MultipleInstanceOfFilterTest.java
0 → 100644
+
72
−
0
View file @
4591ba39
package
teetime.stage
;
import
static
org
.
hamcrest
.
Matchers
.
contains
;
import
static
org
.
hamcrest
.
Matchers
.
empty
;
import
static
org
.
hamcrest
.
Matchers
.
is
;
import
static
org
.
junit
.
Assert
.
assertThat
;
import
java.util.ArrayList
;
import
java.util.Arrays
;
import
java.util.Collection
;
import
java.util.List
;
import
org.junit.Test
;
import
teetime.framework.Analysis
;
import
teetime.framework.AnalysisConfiguration
;
import
teetime.framework.OutputPort
;
import
teetime.framework.pipe.IPipeFactory
;
import
teetime.framework.pipe.PipeFactoryRegistry.PipeOrdering
;
import
teetime.framework.pipe.PipeFactoryRegistry.ThreadCommunication
;
import
teetime.util.Pair
;
public
class
MultipleInstanceOfFilterTest
{
private
static
class
TestConfiguration
extends
AnalysisConfiguration
{
public
TestConfiguration
(
final
List
<
Number
>
initialInput
,
final
List
<
Integer
>
integerList
,
final
List
<
Float
>
floatList
)
{
// Create the stages
final
InitialElementProducer
<
Number
>
producer
=
new
InitialElementProducer
<
Number
>(
initialInput
.
toArray
(
new
Number
[
0
]));
final
MultipleInstanceOfFilter
<
Number
>
filter
=
new
MultipleInstanceOfFilter
<
Number
>();
final
CollectorSink
<
Integer
>
integerSink
=
new
CollectorSink
<
Integer
>(
integerList
);
final
CollectorSink
<
Float
>
floatSink
=
new
CollectorSink
<
Float
>(
floatList
);
// Connect the stages
final
IPipeFactory
factory
=
PIPE_FACTORY_REGISTRY
.
getPipeFactory
(
ThreadCommunication
.
INTRA
,
PipeOrdering
.
ARBITRARY
,
false
);
factory
.
create
(
producer
.
getOutputPort
(),
filter
.
getInputPort
());
factory
.
create
(
filter
.
getOutputPortForType
(
Integer
.
class
),
integerSink
.
getInputPort
());
factory
.
create
(
filter
.
getOutputPortForType
(
Float
.
class
),
floatSink
.
getInputPort
());
super
.
addThreadableStage
(
producer
);
}
}
@Test
@SuppressWarnings
(
"unchecked"
)
public
void
filteringShouldWork
()
{
final
List
<
Number
>
initialInput
=
new
ArrayList
<
Number
>(
Arrays
.
asList
(
1
,
1.5f
,
2
,
2.5f
,
3
,
3.5f
));
final
List
<
Integer
>
integerList
=
new
ArrayList
<
Integer
>();
final
List
<
Float
>
floatList
=
new
ArrayList
<
Float
>();
final
Analysis
analysis
=
new
Analysis
(
new
TestConfiguration
(
initialInput
,
integerList
,
floatList
));
analysis
.
init
();
final
Collection
<
Pair
<
Thread
,
Throwable
>>
errors
=
analysis
.
start
();
assertThat
(
errors
,
is
(
empty
()));
assertThat
(
integerList
,
contains
(
1
,
2
,
3
));
assertThat
(
floatList
,
contains
(
1.5f
,
2.5f
,
3.5f
));
}
@Test
public
void
outputPortForSameTypeShouldBeCached
()
{
final
MultipleInstanceOfFilter
<
Number
>
filter
=
new
MultipleInstanceOfFilter
<
Number
>();
final
OutputPort
<
Float
>
firstPort
=
filter
.
getOutputPortForType
(
Float
.
class
);
final
OutputPort
<
Float
>
secondPort
=
filter
.
getOutputPortForType
(
Float
.
class
);
assertThat
(
firstPort
,
is
(
secondPort
));
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment