Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
F
floodlight
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
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
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
croft1
floodlight
Commits
dbc61db2
Commit
dbc61db2
authored
8 years ago
by
Ryan Izard
Committed by
GitHub
8 years ago
Browse files
Options
Downloads
Plain Diff
Merge pull request #712 from rizard/master
add ConcurrentCircularBuffer
parents
5c2434b4
649cff0d
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/main/java/net/floodlightcontroller/util/ConcurrentCircularBuffer.java
+90
-0
90 additions, 0 deletions
...t/floodlightcontroller/util/ConcurrentCircularBuffer.java
with
90 additions
and
0 deletions
src/main/java/net/floodlightcontroller/util/ConcurrentCircularBuffer.java
0 → 100644
+
90
−
0
View file @
dbc61db2
package
net.floodlightcontroller.util
;
import
java.util.concurrent.atomic.AtomicInteger
;
import
java.lang.reflect.Array
;
public
class
ConcurrentCircularBuffer
<
T
>
{
private
final
AtomicInteger
cursor
=
new
AtomicInteger
();
private
final
Object
[]
buffer
;
private
final
Class
<
T
>
type
;
public
ConcurrentCircularBuffer
(
final
Class
<
T
>
type
,
final
int
bufferSize
)
{
if
(
bufferSize
<
1
)
{
throw
new
IllegalArgumentException
(
"Buffer size must be a positive value"
);
}
this
.
type
=
type
;
this
.
buffer
=
new
Object
[
bufferSize
];
}
public
void
add
(
T
sample
)
{
buffer
[
cursor
.
getAndIncrement
()
%
buffer
.
length
]
=
sample
;
}
@SuppressWarnings
(
"unchecked"
)
public
T
[]
snapshot
()
{
Object
[]
snapshots
=
new
Object
[
buffer
.
length
];
/* Identify the start-position of the buffer. */
int
before
=
cursor
.
get
();
/* Terminate early for an empty buffer. */
if
(
before
==
0
)
{
return
(
T
[])
Array
.
newInstance
(
type
,
0
);
}
System
.
arraycopy
(
buffer
,
0
,
snapshots
,
0
,
buffer
.
length
);
int
after
=
cursor
.
get
();
int
size
=
buffer
.
length
-
(
after
-
before
);
int
snapshotCursor
=
before
-
1
;
/* The entire buffer was replaced during the copy. */
if
(
size
<=
0
)
{
return
(
T
[])
Array
.
newInstance
(
type
,
0
);
}
int
start
=
snapshotCursor
-
(
size
-
1
);
int
end
=
snapshotCursor
;
if
(
snapshotCursor
<
snapshots
.
length
)
{
size
=
snapshotCursor
+
1
;
start
=
0
;
}
/* Copy the sample snapshot to a new array the size of our stable
* snapshot area.
*/
T
[]
result
=
(
T
[])
Array
.
newInstance
(
type
,
size
);
int
startOfCopy
=
start
%
snapshots
.
length
;
int
endOfCopy
=
end
%
snapshots
.
length
;
/* If the buffer space wraps the physical end of the array, use two
* copies to construct the new array.
*/
if
(
startOfCopy
>
endOfCopy
)
{
System
.
arraycopy
(
snapshots
,
startOfCopy
,
result
,
0
,
snapshots
.
length
-
startOfCopy
);
System
.
arraycopy
(
snapshots
,
0
,
result
,
(
snapshots
.
length
-
startOfCopy
),
endOfCopy
+
1
);
}
else
{
/* Otherwise it's a single continuous segment, copy the whole thing
* into the result.
*/
System
.
arraycopy
(
snapshots
,
startOfCopy
,
result
,
0
,
size
);
}
return
(
T
[])
result
;
}
}
\ No newline at end of file
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