Skip to content

Commit 6bdd38a

Browse files
authored
Issue#17: add flip() to TrackedLease (#18)
* add flip() to TrackedLease * add currentLimit assertion to testFlip
1 parent 48e2029 commit 6bdd38a

3 files changed

Lines changed: 35 additions & 0 deletions

File tree

src/main/java/com/teragrep/buf_01/buffer/lease/TrackedLease.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
* - Provide next byte
5959
* - Write next byte
6060
* - Check if more bytes are available in current position
61+
* - Flip the "buffer" aka set limit=pos and pos=0
6162
*
6263
* @collaborators
6364
* - Lease
@@ -72,6 +73,7 @@
7273
* + position(p);
7374
* + currentLimit();
7475
* + limit(l);
76+
* + flip();
7577
* }
7678
*
7779
* TrackedLease --> Lease : provides read/write functionality and position state
@@ -85,6 +87,7 @@
8587
* - Provide next byte
8688
* - Write next byte
8789
* - Check if more bytes are available in current position
90+
* - Flip the "buffer" aka set limit=pos and pos=0
8891
*
8992
* Collaborators:
9093
* - Lease
@@ -108,4 +111,6 @@ public interface TrackedLease<T> extends Lease<T> {
108111
public abstract long currentLimit();
109112

110113
public abstract void limit(final long newLimit);
114+
115+
public abstract void flip();
111116
}

src/main/java/com/teragrep/buf_01/buffer/lease/TrackedMemorySegmentLease.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,12 @@ public void limit(final long newLimit) {
225225
}
226226
}
227227

228+
@Override
229+
public void flip() {
230+
limit(currentPosition());
231+
position(0L);
232+
}
233+
228234
@Override
229235
public boolean equals(final Object o) {
230236
if (o == null || getClass() != o.getClass()) {

src/test/java/com/teragrep/buf_01/buffer/lease/TrackedMemorySegmentLeaseTest.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,30 @@ void testResettingPosition() {
247247
Assertions.assertEquals(0L, trackedLease.currentPosition());
248248
}
249249

250+
@Test
251+
void testFlip() {
252+
final OpeningPool pool = new OpeningPool(
253+
new UnboundPool<>(new ArenaMemorySegmentLeaseSupplier(Arena.ofShared(), 5), new MemorySegmentLeaseStub())
254+
);
255+
final TrackedLease<MemorySegment> trackedLease = new TrackedMemorySegmentLease(pool.get());
256+
257+
// Let's read 3 bytes
258+
trackedLease.next();
259+
trackedLease.next();
260+
trackedLease.next();
261+
262+
// Position should be 3, limit -1
263+
Assertions.assertEquals(-1L, trackedLease.currentLimit());
264+
Assertions.assertEquals(3L, trackedLease.currentPosition());
265+
266+
// Flip it
267+
trackedLease.flip();
268+
269+
// Position should be 0, limit 3
270+
Assertions.assertEquals(0L, trackedLease.currentPosition());
271+
Assertions.assertEquals(3L, trackedLease.currentLimit());
272+
}
273+
250274
@Test
251275
void testEqualsContract() {
252276
EqualsVerifier.simple().forClass(TrackedMemorySegmentLease.class).verify();

0 commit comments

Comments
 (0)