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
5143b562
Commit
5143b562
authored
10 years ago
by
Nelson Tavares de Sousa
Browse files
Options
Downloads
Patches
Plain Diff
first version of CountingmapMerger
parent
aae42993
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/CountingmapMerger.java
+56
-0
56 additions, 0 deletions
src/main/java/teetime/stage/CountingmapMerger.java
src/main/java/teetime/stage/util/CountingMap.java
+33
-1
33 additions, 1 deletion
src/main/java/teetime/stage/util/CountingMap.java
with
89 additions
and
1 deletion
src/main/java/teetime/stage/CountingmapMerger.java
0 → 100644
+
56
−
0
View file @
5143b562
package
teetime.stage
;
import
java.util.Map
;
import
java.util.Map.Entry
;
import
java.util.Set
;
import
teetime.framework.AbstractConsumerStage
;
import
teetime.framework.OutputPort
;
import
teetime.stage.util.CountingMap
;
/**
* Receives different CountingMap instances and merges them into a single one.
* The result is sent upon termination.
*
* @author Nelson Tavares de Sousa
*
* @param <T>
* Key type of the map to be sent
*/
public
class
CountingmapMerger
<
T
>
extends
AbstractConsumerStage
<
CountingMap
<
T
>>
{
private
final
CountingMap
<
T
>
result
=
new
CountingMap
<
T
>();
private
final
OutputPort
<
Map
<
T
,
Integer
>>
port
=
createOutputPort
();
private
final
int
numberOfInputPorts
;
public
CountingmapMerger
(
final
int
numberOfInputPorts
)
{
for
(
int
i
=
1
;
i
<
numberOfInputPorts
;
i
++)
{
createInputPort
();
}
this
.
numberOfInputPorts
=
numberOfInputPorts
;
}
@Override
protected
void
execute
(
final
CountingMap
<
T
>
element
)
{
Set
<
Map
.
Entry
<
T
,
Integer
>>
entries
=
element
.
entrySet
();
for
(
Entry
<
T
,
Integer
>
entry
:
entries
)
{
Integer
resultValue
=
result
.
get
(
entry
.
getKey
());
if
(
resultValue
==
null
)
{
result
.
put
(
entry
.
getKey
(),
entry
.
getValue
());
}
else
{
Integer
temp
=
result
.
get
(
entry
.
getKey
());
temp
+=
entry
.
getValue
();
result
.
put
(
entry
.
getKey
(),
temp
);
}
}
}
@Override
public
void
onTerminating
()
throws
Exception
{
port
.
send
(
result
);
super
.
onTerminating
();
}
}
This diff is collapsed.
Click to expand it.
src/main/java/teetime/stage/util/CountingMap.java
+
33
−
1
View file @
5143b562
...
@@ -2,6 +2,15 @@ package teetime.stage.util;
...
@@ -2,6 +2,15 @@ package teetime.stage.util;
import
java.util.HashMap
;
import
java.util.HashMap
;
/**
* An implementation of HashMap which can be used to count the occurrence of different keys.
* This conaitns all methods of HashMap, but is enhanched with the {@link #add(T, Integer)} and {@link #increment(T)} methods.
*
* @author Nelson Tavares de Sousa
*
* @param <T>
* Key type to be count
*/
public
class
CountingMap
<
T
>
extends
HashMap
<
T
,
Integer
>
{
public
class
CountingMap
<
T
>
extends
HashMap
<
T
,
Integer
>
{
/**
/**
...
@@ -9,13 +18,36 @@ public class CountingMap<T> extends HashMap<T, Integer> {
...
@@ -9,13 +18,36 @@ public class CountingMap<T> extends HashMap<T, Integer> {
*/
*/
private
static
final
long
serialVersionUID
=
-
8036971796701648200L
;
private
static
final
long
serialVersionUID
=
-
8036971796701648200L
;
/**
* Increments the value of key by one.
*
* @param key
*/
public
void
increment
(
final
T
key
)
{
public
void
increment
(
final
T
key
)
{
if
(
super
.
containsKey
(
key
))
{
if
(
super
.
containsKey
(
key
))
{
Integer
i
=
super
.
get
(
key
);
Integer
i
=
super
.
get
(
key
);
i
++;
i
++;
super
.
put
(
key
,
i
);
super
.
put
(
key
,
i
);
}
else
{
}
else
{
super
.
put
(
key
,
0
);
super
.
put
(
key
,
1
);
}
}
/**
* Adds i to the value of key.
*
* @param key
* Key which is used to add i.
* @param i
* Integer value to be added.
*/
public
void
add
(
final
T
key
,
final
Integer
i
)
{
if
(
super
.
containsKey
(
key
))
{
Integer
j
=
super
.
get
(
key
);
j
+=
i
;
super
.
put
(
key
,
j
);
}
else
{
super
.
put
(
key
,
i
);
}
}
}
}
...
...
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